From ff8740a30291a73a82abf1cb65af1975ce1917cf Mon Sep 17 00:00:00 2001 From: Thomas Robert de Saint Vincent Date: Mon, 13 Jun 2022 18:17:34 +0200 Subject: [PATCH] feat: add consumes and produces (#76) * feat: add consumes and produces * doc Co-authored-by: Thomas Robert de Saint Vincent --- README.md | 12 +++ src/ServerlessAutoSwagger.ts | 4 +- .../function-event-properties.schema.json | 14 ++++ src/types/serverless-plugin.types.d.ts | 4 + tests/ServerlessAutoSwagger.test.ts | 74 ++++++++++++++++++- 5 files changed, 105 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index bc37d76..e16f6c4 100644 --- a/README.md +++ b/README.md @@ -217,6 +217,18 @@ http: { } ``` +### MIME Types +You can specify the MIME types by adding `consumes` and `produces` the http event. +Default for both is `['application/json']` +```js +http: { + path: 'hello', + method: 'post', + consumes: ['application/json', 'application/pdf'], + produces: ['application/json', 'application/pdf'], +} +``` + ## with Serverless Offline In the plugin list, you must list `serverless-auto-swagger` before the `serverless-offline` plugin. diff --git a/src/ServerlessAutoSwagger.ts b/src/ServerlessAutoSwagger.ts index 4bdf443..80359fa 100644 --- a/src/ServerlessAutoSwagger.ts +++ b/src/ServerlessAutoSwagger.ts @@ -252,8 +252,8 @@ export default class ServerlessAutoSwagger { description: http.description ?? '', tags: http.swaggerTags, operationId: `${functionName}.${method}.${http.path}`, - consumes: ['application/json'], - produces: ['application/json'], + consumes: http.consumes ?? ['application/json'], + produces: http.produces ?? ['application/json'], // This is actually type `HttpEvent | HttpApiEvent`, but we can lie since only HttpEvent params (or shared params) are used parameters: this.httpEventToParameters(http as CustomHttpEvent), responses: this.formatResponses(http.responseData ?? http.responses), diff --git a/src/schemas/function-event-properties.schema.json b/src/schemas/function-event-properties.schema.json index 671f168..970192e 100644 --- a/src/schemas/function-event-properties.schema.json +++ b/src/schemas/function-event-properties.schema.json @@ -132,6 +132,20 @@ }, "nullable": true, "type": "array" + }, + "consumes": { + "default": ["application/json"], + "items": { + "type": "string" + }, + "type": "array" + }, + "produces": { + "default": ["application/json"], + "items": { + "type": "string" + }, + "type": "array" } }, "required": [] diff --git a/src/types/serverless-plugin.types.d.ts b/src/types/serverless-plugin.types.d.ts index 1b7fcb8..eedfcc0 100644 --- a/src/types/serverless-plugin.types.d.ts +++ b/src/types/serverless-plugin.types.d.ts @@ -95,6 +95,8 @@ export type PathParameters = { export interface CustomHttpEvent extends Http { method: Uppercase | Lowercase; swaggerTags?: string[]; + produces?: string[]; + consumes?: string[]; summary?: string; description?: string; responseData?: HttpResponses; @@ -108,6 +110,8 @@ export interface CustomHttpEvent extends Http { export interface CustomHttpApiEvent extends HttpApiEvent { method: Lowercase | Uppercase; swaggerTags?: string[]; + produces?: string[]; + consumes?: string[]; description?: string; summary?: string; responseData?: HttpResponses; diff --git a/tests/ServerlessAutoSwagger.test.ts b/tests/ServerlessAutoSwagger.test.ts index 468da15..6c0171b 100644 --- a/tests/ServerlessAutoSwagger.test.ts +++ b/tests/ServerlessAutoSwagger.test.ts @@ -12,7 +12,7 @@ const log = { verbose: jest.fn(), } as Partial as Logging['log']; -const logging: Logging = { log, writeText: () => undefined }; +const logging: Logging = { log, writeText: () => undefined } as unknown as Logging; const options: Options = { stage: 'test', @@ -475,6 +475,78 @@ describe('ServerlessAutoSwagger', () => { }); }); + it('should generate an endpoint with consumes parameter', () => { + const serverlessAutoSwagger = new ServerlessAutoSwagger( + generateServerlessFromAnEndpoint([ + { + http: { + path: 'goodbye', + method: 'get', + consumes: ['application/json', 'application/pdf'], + }, + }, + ]), + options, + logging + ); + serverlessAutoSwagger.generatePaths(); + + expect(serverlessAutoSwagger.swagger.paths).toEqual({ + '/goodbye': { + get: { + summary: 'mocked', + description: '', + tags: undefined, + operationId: 'mocked.get.goodbye', + consumes: ['application/json', 'application/pdf'], + produces: ['application/json'], + parameters: [], + responses: { + 200: { + description: '200 response', + }, + }, + }, + }, + }); + }); + + it('should generate an endpoint with produces parameter', () => { + const serverlessAutoSwagger = new ServerlessAutoSwagger( + generateServerlessFromAnEndpoint([ + { + http: { + path: 'goodbye', + method: 'get', + produces: ['application/json', 'application/pdf'], + }, + }, + ]), + options, + logging + ); + serverlessAutoSwagger.generatePaths(); + + expect(serverlessAutoSwagger.swagger.paths).toEqual({ + '/goodbye': { + get: { + summary: 'mocked', + description: '', + tags: undefined, + operationId: 'mocked.get.goodbye', + consumes: ['application/json'], + produces: ['application/json', 'application/pdf'], + parameters: [], + responses: { + 200: { + description: '200 response', + }, + }, + }, + }, + }); + }); + it('should generate an endpoint with header parameters using builtin params', () => { const serverlessAutoSwagger = new ServerlessAutoSwagger( generateServerlessFromAnEndpoint([