Skip to content

Commit

Permalink
Add StreamFile(s)Param decorators
Browse files Browse the repository at this point in the history
  • Loading branch information
Teddy Chambard authored and t.chambard committed Dec 31, 2019
1 parent fcd2ae9 commit cb45ed9
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
23 changes: 23 additions & 0 deletions src/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,26 @@ export function IsFloat(target: any, propertyKey: string, parameterIndex?: numbe
export function IsDouble(target: any, propertyKey: string, parameterIndex?: number) {
return;
}

/**
* Creates a mapping between a file on a multipart request and a method
* argument.
* Unlike @FileParam provided by typescript-rest, this decorator allows to pipe the request.
*/
export function StreamFileParam(name: string) {
return function (...args: any[]) {
return;
};
}

/**
* Creates a mapping between multiple files on a multipart request and a method
* argument.
* Unlike @FileParam provided by typescript-rest, this decorator allows to pipe the request.
*/
export function StreamFilesParam(name: string) {
return function (...args: any[]) {
return;
};
}

10 changes: 7 additions & 3 deletions src/metadata/parameterGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ export class ParameterGenerator {
return this.getFileParameter(this.parameter);
case 'FilesParam':
return this.getFilesParameter(this.parameter);
case 'StreamFileParam':
return this.getFileParameter(this.parameter);
case 'StreamFilesParam':
return this.getFilesParameter(this.parameter);
case 'Context':
case 'ContextRequest':
case 'ContextResponse':
Expand Down Expand Up @@ -89,7 +93,7 @@ export class ParameterGenerator {
return {
description: this.getParameterDescription(parameter),
in: 'formData',
name: getDecoratorTextValue(this.parameter, ident => ident.text === 'FileParam') || parameterName,
name: getDecoratorTextValue(this.parameter, ident => ident.text === 'FileParam' || ident.text === 'StreamFileParam') || parameterName,
parameterName,
required: !parameter.questionToken,
type: { typeName: 'file' }
Expand All @@ -106,7 +110,7 @@ export class ParameterGenerator {
return {
description: this.getParameterDescription(parameter),
in: 'formData',
name: getDecoratorTextValue(this.parameter, ident => ident.text === 'FilesParam') || parameterName,
name: getDecoratorTextValue(this.parameter, ident => ident.text === 'FilesParam' || ident.text === 'StreamFilesParam') || parameterName,
parameterName,
required: !parameter.questionToken,
type: { typeName: 'file' }
Expand Down Expand Up @@ -255,7 +259,7 @@ export class ParameterGenerator {
return ['HeaderParam', 'QueryParam', 'Param', 'FileParam',
'PathParam', 'FilesParam', 'FormParam', 'CookieParam',
'Context', 'ContextRequest', 'ContextResponse', 'ContextNext',
'ContextLanguage', 'ContextAccept'].some(d => d === decoratorName);
'ContextLanguage', 'ContextAccept', 'StreamFileParam', 'StreamFilesParam'].some(d => d === decoratorName);
}

private supportPathDataType(parameterType: Type) {
Expand Down
12 changes: 12 additions & 0 deletions test/data/apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,18 @@ export class ParameterizedEndpoint {
test(@PathParam('objectId') objectId: string): PrimitiveClassModel {
return new PrimitiveClassModel();
}

@Path('/file')
@POST
file(@FileParam('file') file: Express.Multer.File): PrimitiveClassModel {
return new PrimitiveClassModel();
}

@Path('/stream')
@POST
stream(@swagger.StreamFileParam('stream') file: Express.Multer.File): PrimitiveClassModel {
return new PrimitiveClassModel();
}
}

export abstract class Entity {
Expand Down
10 changes: 10 additions & 0 deletions test/unit/definitions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,16 @@ describe('Definition generation', () => {
const expression = jsonata('paths."/parameterized/{objectId}/test".get.parameters[0].in');
expect(expression.evaluate(spec)).to.eq('path');
});

it('should generate formData param for params declared on method', () => {
const expression = jsonata('paths."/parameterized/{objectId}/file".post.parameters[0].in');
expect(expression.evaluate(spec)).to.eq('formData');
});

it('should generate path param for params declared on class', () => {
const expression = jsonata('paths."/parameterized/{objectId}/stream".post.parameters[0].in');
expect(expression.evaluate(spec)).to.eq('formData');
});
});

describe('AbstractEntityEndpoint', () => {
Expand Down

0 comments on commit cb45ed9

Please sign in to comment.