Skip to content

Add StreamFile(s)Param decorators #102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,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: 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: 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
17 changes: 15 additions & 2 deletions test/data/apis.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
'use strict';

import {
Accept, DELETE, FormParam, GET, Path,
PathParam, POST, PUT, QueryParam,
Accept, DELETE, FileParam, FormParam, GET,
Path, PathParam, POST, PUT,
QueryParam,
Return,
Security
} from 'typescript-rest';
Expand Down Expand Up @@ -399,6 +400,18 @@ export class ParameterizedEndpoint {
public test(@PathParam('objectId') objectId: string): PrimitiveClassModel {
return new PrimitiveClassModel();
}

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

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

export abstract class Entity {
Expand Down
20 changes: 20 additions & 0 deletions test/unit/definitions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,26 @@ describe('Definition generation', () => {
const expression = jsonata('paths."/parameterized/{objectId}/test".get.parameters[0].in');
expect(expression.evaluate(spec)).toEqual('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)).toEqual('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)).toEqual('formData');
});

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)).toEqual('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)).toEqual('formData');
});
});

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