Skip to content

Commit

Permalink
feat: implementation of multiple upload fields
Browse files Browse the repository at this point in the history
  • Loading branch information
jackey8616 committed Jan 23, 2024
1 parent 5869115 commit 5545ecb
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 12 deletions.
8 changes: 5 additions & 3 deletions packages/cli/src/routeGeneration/routeGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,18 @@ export abstract class AbstractRouteGenerator<Config extends ExtendedRoutesConfig

const normalisedFullPath = normalisePath(`${normalisedBasePath}${normalisedControllerPath}${normalisedMethodPath}`, '/', '', false);

const uploadFileParameter = method.parameters.find(parameter => parameter.type.dataType === 'file');
const uploadFilesWithDifferentFieldParameter = method.parameters.filter(parameter => parameter.type.dataType === 'file');
const uploadFilesParameter = method.parameters.find(parameter => parameter.type.dataType === 'array' && parameter.type.elementType.dataType === 'file');
return {
fullPath: normalisedFullPath,
method: method.method.toLowerCase(),
name: method.name,
parameters: parameterObjs,
path: normalisedMethodPath,
uploadFile: !!uploadFileParameter,
uploadFileName: uploadFileParameter?.name,
uploadFile: uploadFilesWithDifferentFieldParameter.length > 0,
uploadFileName: uploadFilesWithDifferentFieldParameter.map((parameter) => ({
'name': parameter.name,
})),
uploadFiles: !!uploadFilesParameter,
uploadFilesName: uploadFilesParameter?.name,
security: method.security,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export function RegisterRoutes(app: Router) {
authenticateMiddleware({{json security}}),
{{/if}}
{{#if uploadFile}}
upload.single('{{uploadFileName}}'),
upload.fields({{json uploadFileName}}),
{{/if}}
{{#if uploadFiles}}
upload.array('{{uploadFilesName}}'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,11 @@ export class ExpressTemplateService implements TemplateService<ExRequest, ExResp
case 'body-prop':
return this.validationService.ValidateParam(args[key], request.body[name], name, fieldErrors, 'body.', this.minimalSwaggerConfig);
case 'formData':
if (args[key].dataType === 'file') {
return this.validationService.ValidateParam(args[key], request.file, name, fieldErrors, undefined, this.minimalSwaggerConfig);
const files = Object.keys(args).filter((argKey) => args[argKey].dataType === 'file');

Check failure on line 76 in packages/cli/src/routeGeneration/templates/express/expressTemplateService.ts

View workflow job for this annotation

GitHub Actions / Build (18, ubuntu-latest, ^4.9.5)

Unexpected lexical declaration in case block

Check failure on line 76 in packages/cli/src/routeGeneration/templates/express/expressTemplateService.ts

View workflow job for this annotation

GitHub Actions / Build (20, ubuntu-latest, ^5.2.2)

Unexpected lexical declaration in case block

Check failure on line 76 in packages/cli/src/routeGeneration/templates/express/expressTemplateService.ts

View workflow job for this annotation

GitHub Actions / Build (20, ubuntu-latest, ^4.9.5)

Unexpected lexical declaration in case block

Check failure on line 76 in packages/cli/src/routeGeneration/templates/express/expressTemplateService.ts

View workflow job for this annotation

GitHub Actions / Build (18, ubuntu-latest, ^5.2.2)

Unexpected lexical declaration in case block
if (files.length > 0) {
const requestFiles = request.files as {[fileName: string]: Express.Multer.File[]};
const fileArgs = this.validationService.ValidateParam(args[key], requestFiles[name], name, fieldErrors, undefined, this.minimalSwaggerConfig);
return fileArgs.length === 1 ? fileArgs[0] : fileArgs;
} else if (args[key].dataType === 'array' && args[key].array.dataType === 'file') {
return this.validationService.ValidateParam(args[key], request.files, name, fieldErrors, undefined, this.minimalSwaggerConfig);
} else {
Expand Down
4 changes: 3 additions & 1 deletion packages/cli/src/routeGeneration/templates/hapi/hapi.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,11 @@ export function RegisterRoutes(server: any) {
},
{{/if}}
{{#if uploadFile}}
{{#each uploadFileName}}
{
method: fileUploadMiddleware('{{uploadFileName}}', false)
method: fileUploadMiddleware('{{name}}', false)
},
{{/each}}
{{/if}}
{{#if uploadFiles}}
{
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/routeGeneration/templates/koa/koa.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export function RegisterRoutes(router: KoaRouter) {
authenticateMiddleware({{json security}}),
{{/if}}
{{#if uploadFile}}
upload.single('{{uploadFileName}}'),
upload.fields({{json uploadFileName}}),
{{/if}}
{{#if uploadFiles}}
upload.array('{{uploadFilesName}}'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,15 @@ export class KoaTemplateService implements TemplateService<any, Context> {
case 'body-prop':
return this.validationService.ValidateParam(args[key], (context.request as any).body[name], name, errorFields, 'body.', this.minimalSwaggerConfig);
case 'formData':
if (args[key].dataType === 'file') {
return this.validationService.ValidateParam(args[key], (context.request as any).file, name, errorFields, undefined, this.minimalSwaggerConfig);
const files = Object.keys(args).filter((argKey) => args[argKey].dataType === 'file');

Check failure on line 78 in packages/cli/src/routeGeneration/templates/koa/koaTemplateService.ts

View workflow job for this annotation

GitHub Actions / Build (18, ubuntu-latest, ^4.9.5)

Unexpected lexical declaration in case block

Check failure on line 78 in packages/cli/src/routeGeneration/templates/koa/koaTemplateService.ts

View workflow job for this annotation

GitHub Actions / Build (20, ubuntu-latest, ^5.2.2)

Unexpected lexical declaration in case block

Check failure on line 78 in packages/cli/src/routeGeneration/templates/koa/koaTemplateService.ts

View workflow job for this annotation

GitHub Actions / Build (20, ubuntu-latest, ^4.9.5)

Unexpected lexical declaration in case block

Check failure on line 78 in packages/cli/src/routeGeneration/templates/koa/koaTemplateService.ts

View workflow job for this annotation

GitHub Actions / Build (18, ubuntu-latest, ^5.2.2)

Unexpected lexical declaration in case block
const contextRequest = context.request as any;

Check failure on line 79 in packages/cli/src/routeGeneration/templates/koa/koaTemplateService.ts

View workflow job for this annotation

GitHub Actions / Build (18, ubuntu-latest, ^4.9.5)

Unexpected lexical declaration in case block

Check failure on line 79 in packages/cli/src/routeGeneration/templates/koa/koaTemplateService.ts

View workflow job for this annotation

GitHub Actions / Build (20, ubuntu-latest, ^5.2.2)

Unexpected lexical declaration in case block

Check failure on line 79 in packages/cli/src/routeGeneration/templates/koa/koaTemplateService.ts

View workflow job for this annotation

GitHub Actions / Build (20, ubuntu-latest, ^4.9.5)

Unexpected lexical declaration in case block

Check failure on line 79 in packages/cli/src/routeGeneration/templates/koa/koaTemplateService.ts

View workflow job for this annotation

GitHub Actions / Build (18, ubuntu-latest, ^5.2.2)

Unexpected lexical declaration in case block
if (files.length > 0) {
const fileArgs = this.validationService.ValidateParam(args[key], contextRequest.files[name], name, errorFields, undefined, this.minimalSwaggerConfig);
return fileArgs.length === 1 ? fileArgs[0] : fileArgs;
} else if (args[key].dataType === 'array' && args[key].array.dataType === 'file') {
return this.validationService.ValidateParam(args[key], (context.request as any).files, name, errorFields, undefined, this.minimalSwaggerConfig);
return this.validationService.ValidateParam(args[key], contextRequest.files, name, errorFields, undefined, this.minimalSwaggerConfig);
} else {
return this.validationService.ValidateParam(args[key], (context.request as any).body[name], name, errorFields, undefined, this.minimalSwaggerConfig);
return this.validationService.ValidateParam(args[key], contextRequest.body[name], name, errorFields, undefined, this.minimalSwaggerConfig);
}
case 'res':
return this.responder(context, next);
Expand Down

0 comments on commit 5545ecb

Please sign in to comment.