Skip to content

Commit

Permalink
feat: add consumes and produces (#76)
Browse files Browse the repository at this point in the history
* feat: add consumes and produces

* doc

Co-authored-by: Thomas Robert de Saint Vincent <[email protected]>
  • Loading branch information
desaintvincent and Thomas Robert de Saint Vincent authored Jun 13, 2022
1 parent 51132f3 commit ff8740a
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 3 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions src/ServerlessAutoSwagger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
14 changes: 14 additions & 0 deletions src/schemas/function-event-properties.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": []
Expand Down
4 changes: 4 additions & 0 deletions src/types/serverless-plugin.types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ export type PathParameters = {
export interface CustomHttpEvent extends Http {
method: Uppercase<HttpMethod> | Lowercase<HttpMethod>;
swaggerTags?: string[];
produces?: string[];
consumes?: string[];
summary?: string;
description?: string;
responseData?: HttpResponses;
Expand All @@ -108,6 +110,8 @@ export interface CustomHttpEvent extends Http {
export interface CustomHttpApiEvent extends HttpApiEvent {
method: Lowercase<HttpMethod> | Uppercase<HttpMethod>;
swaggerTags?: string[];
produces?: string[];
consumes?: string[];
description?: string;
summary?: string;
responseData?: HttpResponses;
Expand Down
74 changes: 73 additions & 1 deletion tests/ServerlessAutoSwagger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const log = {
verbose: jest.fn(),
} as Partial<Logging['log']> as Logging['log'];

const logging: Logging = { log, writeText: () => undefined };
const logging: Logging = { log, writeText: () => undefined } as unknown as Logging;

const options: Options = {
stage: 'test',
Expand Down Expand Up @@ -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([
Expand Down

0 comments on commit ff8740a

Please sign in to comment.