Skip to content

Commit

Permalink
Merge pull request #74 from souravbhowmik1999/middleware
Browse files Browse the repository at this point in the history
multiple file and get field fiels name from formdata
  • Loading branch information
Shubham4026 authored Dec 18, 2024
2 parents 9adc825 + 84b3ea9 commit 2a64ede
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
18 changes: 8 additions & 10 deletions src/common/middleware/apiConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,19 +384,13 @@ export const apiList = {
},
}),
'/user/v1/tenant/create': createRouteObject({
post: {
ROLE_CHECK: rolesGroup.admin_mme,
},
post: {},
}),
'/user/v1/tenant/update/:identifier': createRouteObject({
post: {
ROLE_CHECK: rolesGroup.admin_mme,
},
'/user/v1/tenant/update/:tenantId': createRouteObject({
patch: {},
}),
'/user/v1/tenant/delete/:identifier': createRouteObject({
post: {
ROLE_CHECK: rolesGroup.admin_mme,
},
post: {},
}),
'/user/v1/academicyears/create': createRouteObject({
post: {},
Expand Down Expand Up @@ -1556,12 +1550,16 @@ export const apiList = {
};
export const urlPatterns = Object.keys(apiList);


//add public api
export const publicAPI = [
'/user/v1/auth',
'/user/v1/create',
'/user/v1/fields/options/read',
'/user/v1/tenant/read',
'/user/v1/tenant/create',
'/user/v1/tenant/update/:tenantId',
'/user/v1/tenant/delete/:identifier',
'/user/v1/auth/login',
'/user/v1/auth',
'/api/question/v2/list',
Expand Down
19 changes: 12 additions & 7 deletions src/common/middleware/middleware.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,23 +231,28 @@ export class MiddlewareServices {
// Prepare FormData for Axios call
prepareFormData(reqObject: any): FormData {
const formData = new FormData();


// Check if there are files to process
if (reqObject.files && reqObject.files.length > 0) {
const file = reqObject.files[0];
formData.append('file', file.buffer, {
filename: file.originalname,
contentType: file.mimetype,
reqObject.files.forEach((file: any) => {
// Append each file to the formData, using the fieldname and file details
formData.append(file.fieldname, file.buffer, {
filename: file.originalname,
contentType: file.mimetype,
});
});
}


// Append other form data (e.g., text fields)
if (reqObject.data) {
Object.keys(reqObject.data).forEach((key) => {
formData.append(key, reqObject.data[key]);
});
}

return formData;
}


getMicroserviceUrl(url: string): string | undefined {
// Mapping of URL prefixes to their corresponding service configuration keys
Expand Down
7 changes: 4 additions & 3 deletions src/middleware/gateway.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,15 @@ export class GatewayService {
url: string,
formData: any,
token?: string,
) {
try {
const response = await axios.post(url, formData, {
) {
try {
const response = await axios.patch(url, formData, {
headers: {
...formData.getHeaders(),
...(token ? { Authorization: `Bearer ${token}` } : {}),
},
});

res.locals.responseBody = response.data;
res.status(response.status);
return response.data;
Expand Down

0 comments on commit 2a64ede

Please sign in to comment.