Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
Loading items

Target

Select target project
  • roshan.suvarnkar/hdfc-common-utility
1 result
Select Git revision
Loading items
Show changes

Commits on Source 2

......@@ -24,7 +24,8 @@ export * from './utils/validate-dto.utils';
export * from './utils/client-proxy-wrapper';
export * from './utils/prisma-seed.util';
export * from './utils/custom-exception.utils';
export * from './utils/prisma-exception.utils'
export * from './utils/prisma-exception.utils';
export * from './utils/filenet.utils';
export * from './guards/http-throttler.guard';
......
import axios, { AxiosInstance } from 'axios';
import FormData from 'form-data';
export class FileNetClient {
private client: AxiosInstance;
private authToken: string | null = null;
constructor(
private baseUrl: string,
private username: string,
private password: string
) {
this.client = axios.create({
baseURL: baseUrl,
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
},
});
this.attachInterceptors();
}
private attachInterceptors() {
this.client.interceptors.request.use(async (config) => {
if (!this.authToken) {
await this.ensureAuthenticated();
}
if (this.authToken) {
config.headers['Authorization'] = `Bearer ${this.authToken}`;
}
return config;
});
this.client.interceptors.response.use(
(response) => response,
async (error) => {
const originalRequest = error.config;
if (
error.response?.status === 401 &&
!originalRequest._retry
) {
originalRequest._retry = true;
this.authToken = null;
await this.ensureAuthenticated();
originalRequest.headers['Authorization'] = `Bearer ${this.authToken}`;
return this.client(originalRequest);
}
return Promise.reject(error);
}
);
}
private loginPromise: Promise<void> | null = null;
private async ensureAuthenticated(): Promise<void> {
if (this.authToken) return;
if (!this.loginPromise) {
this.loginPromise = this.performLogin();
}
try {
await this.loginPromise;
} finally {
this.loginPromise = null;
}
}
private async performLogin(): Promise<void> {
try {
const response = await axios.post(`${this.baseUrl}/auth/login`, {
username: this.username,
password: this.password,
});
this.authToken = response.data.token;
} catch (error: any) {
throw new Error('FileNet auto-login failed: ' + (error.response?.data?.message || error.message));
}
}
async getDocument(documentId: string): Promise<any> {
return this.safeRequest(() => this.client.get(`/documents/${documentId}`));
}
async uploadDocument(
folderId: string,
file: Buffer,
filename: string,
metadata: Record<string, any> = {}
): Promise<any> {
const formData = new FormData();
formData.append('file', file, filename);
for (const [key, value] of Object.entries(metadata)) {
formData.append(key, value);
}
return this.safeRequest(() =>
this.client.post(`/folders/${folderId}/documents`, formData, {
headers: formData.getHeaders(),
})
);
}
async deleteDocument(documentId: string) {
return this.safeRequest(() => this.client.delete(`/documents/${documentId}`));
}
async searchDocuments(query: Record<string, any>) {
return this.safeRequest(() => this.client.post(`/search`, query));
}
async listFolderContents(folderId: string): Promise<any[]> {
return this.safeRequest(() => this.client.get(`/folders/${folderId}/contents`));
}
async updateDocumentMetadata(documentId: string, metadata: Record<string, any>): Promise<any> {
return this.safeRequest(() =>
this.client.put(`/documents/${documentId}/metadata`, metadata)
);
}
async getDocumentMetadata(documentId: string): Promise<Record<string, any>> {
return this.safeRequest(() =>
this.client.get(`/documents/${documentId}/metadata`)
);
}
private async safeRequest<T>(req: () => Promise<{ data: T }>): Promise<T> {
try {
const response = await req();
return response.data;
} catch (error: any) {
const message =
error?.response?.data?.message || error.message || 'FileNet request failed';
throw new Error(message);
}
}
}