Skip to content

Commit cb45ed9

Browse files
Teddy Chambardt.chambard
Teddy Chambard
authored and
t.chambard
committed
Add StreamFile(s)Param decorators
1 parent fcd2ae9 commit cb45ed9

File tree

4 files changed

+52
-3
lines changed

4 files changed

+52
-3
lines changed

src/decorators.ts

+23
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,26 @@ export function IsFloat(target: any, propertyKey: string, parameterIndex?: numbe
110110
export function IsDouble(target: any, propertyKey: string, parameterIndex?: number) {
111111
return;
112112
}
113+
114+
/**
115+
* Creates a mapping between a file on a multipart request and a method
116+
* argument.
117+
* Unlike @FileParam provided by typescript-rest, this decorator allows to pipe the request.
118+
*/
119+
export function StreamFileParam(name: string) {
120+
return function (...args: any[]) {
121+
return;
122+
};
123+
}
124+
125+
/**
126+
* Creates a mapping between multiple files on a multipart request and a method
127+
* argument.
128+
* Unlike @FileParam provided by typescript-rest, this decorator allows to pipe the request.
129+
*/
130+
export function StreamFilesParam(name: string) {
131+
return function (...args: any[]) {
132+
return;
133+
};
134+
}
135+

src/metadata/parameterGenerator.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ export class ParameterGenerator {
3131
return this.getFileParameter(this.parameter);
3232
case 'FilesParam':
3333
return this.getFilesParameter(this.parameter);
34+
case 'StreamFileParam':
35+
return this.getFileParameter(this.parameter);
36+
case 'StreamFilesParam':
37+
return this.getFilesParameter(this.parameter);
3438
case 'Context':
3539
case 'ContextRequest':
3640
case 'ContextResponse':
@@ -89,7 +93,7 @@ export class ParameterGenerator {
8993
return {
9094
description: this.getParameterDescription(parameter),
9195
in: 'formData',
92-
name: getDecoratorTextValue(this.parameter, ident => ident.text === 'FileParam') || parameterName,
96+
name: getDecoratorTextValue(this.parameter, ident => ident.text === 'FileParam' || ident.text === 'StreamFileParam') || parameterName,
9397
parameterName,
9498
required: !parameter.questionToken,
9599
type: { typeName: 'file' }
@@ -106,7 +110,7 @@ export class ParameterGenerator {
106110
return {
107111
description: this.getParameterDescription(parameter),
108112
in: 'formData',
109-
name: getDecoratorTextValue(this.parameter, ident => ident.text === 'FilesParam') || parameterName,
113+
name: getDecoratorTextValue(this.parameter, ident => ident.text === 'FilesParam' || ident.text === 'StreamFilesParam') || parameterName,
110114
parameterName,
111115
required: !parameter.questionToken,
112116
type: { typeName: 'file' }
@@ -255,7 +259,7 @@ export class ParameterGenerator {
255259
return ['HeaderParam', 'QueryParam', 'Param', 'FileParam',
256260
'PathParam', 'FilesParam', 'FormParam', 'CookieParam',
257261
'Context', 'ContextRequest', 'ContextResponse', 'ContextNext',
258-
'ContextLanguage', 'ContextAccept'].some(d => d === decoratorName);
262+
'ContextLanguage', 'ContextAccept', 'StreamFileParam', 'StreamFilesParam'].some(d => d === decoratorName);
259263
}
260264

261265
private supportPathDataType(parameterType: Type) {

test/data/apis.ts

+12
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,18 @@ export class ParameterizedEndpoint {
339339
test(@PathParam('objectId') objectId: string): PrimitiveClassModel {
340340
return new PrimitiveClassModel();
341341
}
342+
343+
@Path('/file')
344+
@POST
345+
file(@FileParam('file') file: Express.Multer.File): PrimitiveClassModel {
346+
return new PrimitiveClassModel();
347+
}
348+
349+
@Path('/stream')
350+
@POST
351+
stream(@swagger.StreamFileParam('stream') file: Express.Multer.File): PrimitiveClassModel {
352+
return new PrimitiveClassModel();
353+
}
342354
}
343355

344356
export abstract class Entity {

test/unit/definitions.spec.ts

+10
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,16 @@ describe('Definition generation', () => {
298298
const expression = jsonata('paths."/parameterized/{objectId}/test".get.parameters[0].in');
299299
expect(expression.evaluate(spec)).to.eq('path');
300300
});
301+
302+
it('should generate formData param for params declared on method', () => {
303+
const expression = jsonata('paths."/parameterized/{objectId}/file".post.parameters[0].in');
304+
expect(expression.evaluate(spec)).to.eq('formData');
305+
});
306+
307+
it('should generate path param for params declared on class', () => {
308+
const expression = jsonata('paths."/parameterized/{objectId}/stream".post.parameters[0].in');
309+
expect(expression.evaluate(spec)).to.eq('formData');
310+
});
301311
});
302312

303313
describe('AbstractEntityEndpoint', () => {

0 commit comments

Comments
 (0)