Skip to content

Commit 5673c05

Browse files
committed
feat(serverless-openapi): render openapi automatically on package hook and add output option to serverless.yaml
1 parent 322e194 commit 5673c05

File tree

5 files changed

+38
-21
lines changed

5 files changed

+38
-21
lines changed

packages/serverless-openapi/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ If you want to change the format (json or yaml) or the file name use
3131
serverless openapi -o openapi.yaml
3232
```
3333

34+
or set it via serverless yaml
35+
36+
```yml
37+
custom:
38+
openapi:
39+
out: openapi.yaml
40+
```
41+
3442
### Add basic info and tags
3543
3644
Under `custom` add

packages/serverless-openapi/src/index.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Serverless from 'serverless';
2-
import { customProperties } from './lib/custom.properties';
2+
import { CustomProperties, customProperties } from './lib/custom.properties';
33
import { OpenAPIV3 } from 'openapi-types';
44
import { writeFileSync } from 'fs';
55
import { functioneventProperties } from './lib/functionEvent.properties';
@@ -47,17 +47,19 @@ export class ServerlessPlugin {
4747

4848
this.hooks = {
4949
'openapi:serverless': this.generate.bind(this),
50+
'package:initialize': this.generate.bind(this),
5051
};
5152
}
5253

5354
private generate() {
5455
this.serverless.cli.log('Generate open api');
5556
const openApi = new Generator().generate(this.serverless);
56-
this.saveToFile(openApi);
57+
const customOpenApi = this.serverless.service.custom
58+
.openapi as CustomProperties;
59+
this.saveToFile(openApi, customOpenApi.out);
5760
}
5861

59-
private saveToFile(openApi: OpenAPIV3.Document) {
60-
let out = 'openapi.json';
62+
private saveToFile(openApi: OpenAPIV3.Document, out = 'openapi.json') {
6163
if (this.options['out']) {
6264
out = this.options['out'];
6365
}

packages/serverless-openapi/src/lib/custom.properties.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
11
import { JSONSchema7 } from 'json-schema';
2+
import { Schema } from './response.types';
3+
4+
export interface CustomProperties {
5+
out?: string;
6+
version: string;
7+
title: string;
8+
description?: string;
9+
tags?: { name: string; description?: string }[];
10+
defaultResponse?: {
11+
'application/json': Schema;
12+
};
13+
}
214

315
export const customProperties: JSONSchema7 = {
416
properties: {
517
openapi: {
618
type: 'object',
719
additionalProperties: false,
820
properties: {
21+
out: {
22+
type: 'string',
23+
},
924
version: {
1025
type: 'string',
1126
},

packages/serverless-openapi/src/lib/generator.ts

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,8 @@
11
import Serverless from 'serverless';
2-
import { JSONSchema7 } from 'json-schema';
32
import { OpenAPIV3 } from 'openapi-types';
43
import { HttpMethod } from 'serverless/plugins/aws/package/compile/events/apiGateway/lib/validate';
5-
6-
interface Schema {
7-
schema?: JSONSchema7;
8-
name: string;
9-
description?: string;
10-
}
11-
12-
interface CustomProperties {
13-
version: string;
14-
title: string;
15-
description?: string;
16-
tags?: { name: string; description?: string }[];
17-
defaultResponse?: {
18-
'application/json': Schema;
19-
};
20-
}
4+
import { Schema } from './response.types';
5+
import { CustomProperties } from './custom.properties';
216

227
interface HttpEvent {
238
path: string;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { JSONSchema7 } from 'json-schema';
2+
3+
export interface Schema {
4+
schema?: JSONSchema7;
5+
name: string;
6+
description?: string;
7+
}

0 commit comments

Comments
 (0)