Skip to content

Commit 779e543

Browse files
committed
feat(openapi): bundle schema refs into openapi
1 parent f98ec63 commit 779e543

File tree

8 files changed

+107
-17
lines changed

8 files changed

+107
-17
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/out-tsc
77

88
# dependencies
9-
/node_modules
9+
**/node_modules
1010

1111
# IDEs and editors
1212
/.idea

package-lock.json

Lines changed: 71 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@
3636
"packages/**"
3737
],
3838
"dependencies": {
39+
"@apidevtools/json-schema-ref-parser": "^10.1.0",
3940
"js-yaml": "^4.1.0",
4041
"openapi-types": "^12.0.2",
4142
"tslib": "^2.4.1"
4243
}
4344
}
44-

packages/serverless-openapi/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"dependencies": {
1919
"js-yaml": "^4.1.0",
2020
"tslib": "^2.0.0",
21-
"openapi-types": "^10.0.0"
21+
"openapi-types": "^10.0.0",
22+
"@apidevtools/json-schema-ref-parser": "^10.1.0"
2223
}
2324
}

packages/serverless-openapi/src/index.ts

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
import Serverless from 'serverless';
22
import { CustomProperties, customProperties } from './lib/custom.properties';
3-
import { OpenAPIV3 } from 'openapi-types';
43
import { writeFileSync } from 'fs';
54
import { functioneventProperties } from './lib/functionEvent.properties';
65
import { dump } from 'js-yaml';
76
import { CommandsDefinition } from './lib/comand.types';
87
import { Generator } from './lib/generator';
98
import { Log } from './lib/sls.types';
109

10+
import $RefParser from '@apidevtools/json-schema-ref-parser';
11+
import { readFile } from 'fs/promises';
12+
import * as path from 'path';
13+
1114
export class ServerlessPlugin {
1215
serverless: Serverless;
1316
options: Serverless.Options & { [key: string]: any };
1417
hooks: { [key: string]: Serverless.FunctionDefinitionHandler };
1518
commands: CommandsDefinition;
16-
log: Log
19+
log: Log;
1720

1821
constructor(
1922
serverless: Serverless,
2023
options: Serverless.Options & { [key: string]: any },
21-
{ log } : {log: Log}
24+
{ log }: { log: Log }
2225
) {
2326
this.serverless = serverless;
2427
this.options = options;
25-
this.log = log
28+
this.log = log;
2629

2730
this.commands = {
2831
openapi: {
@@ -55,15 +58,31 @@ export class ServerlessPlugin {
5558
};
5659
}
5760

58-
private generate() {
61+
private async generate() {
5962
this.log.notice('Generate open api');
6063
const openApi = new Generator(this.log).generate(this.serverless);
6164
const customOpenApi = this.serverless.service.custom
6265
.openapi as CustomProperties;
63-
this.saveToFile(openApi, customOpenApi.out);
66+
67+
68+
const api = await $RefParser.bundle(openApi as any, {
69+
resolve: {
70+
file: {
71+
canRead: ['.yml', '.json'],
72+
read: async (ref) => {
73+
const orgRef = (ref.url as string).replace(process.cwd(), "")
74+
const realPath = path.join(process.cwd(), customOpenApi.schemaFolder, orgRef )
75+
return await readFile(realPath)
76+
}
77+
}
78+
}
79+
});
80+
81+
this.log.debug(`API name: ${openApi.info.title}, Version: ${openApi.info.version}`);
82+
this.saveToFile(api, customOpenApi.out);
6483
}
6584

66-
private saveToFile(openApi: OpenAPIV3.Document, out = 'openapi.json') {
85+
private saveToFile(openApi: any, out = 'openapi.json') {
6786
if (this.options['out']) {
6887
out = this.options['out'];
6988
}
@@ -72,7 +91,7 @@ export class ServerlessPlugin {
7291
if (out.endsWith('.yaml') || out.endsWith('.yml')) {
7392
output = dump(JSON.parse(output));
7493
}
75-
this.log.notice('Saved open api to '+ out);
94+
this.log.notice('Saved open api to ' + out);
7695

7796
writeFileSync(out, output);
7897
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export interface CustomProperties {
1414
defaultResponse?: {
1515
'application/json': Schema;
1616
};
17+
schemaFolder?: string;
1718
}
1819

1920
export const customProperties: JSONSchema7 = {
@@ -22,6 +23,9 @@ export const customProperties: JSONSchema7 = {
2223
type: 'object',
2324
additionalProperties: false,
2425
properties: {
26+
schemaFolder: {
27+
type: 'string',
28+
},
2529
out: {
2630
type: 'string',
2731
},

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { OpenAPIV3 } from 'openapi-types';
33
import { Schema } from './response.types';
44
import { CustomProperties } from './custom.properties';
55
import Aws, {
6-
HttpAuthorizer,
76
HttpRequestParametersValidation,
87
} from 'serverless/plugins/aws/provider/awsProvider';
98
import { Log } from './sls.types';

tsconfig.base.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"importHelpers": true,
1111
"target": "es2015",
1212
"module": "esnext",
13-
"lib": ["es2017", "dom"],
13+
"lib": ["es2017"],
1414
"skipLibCheck": true,
1515
"skipDefaultLibCheck": true,
1616
"baseUrl": ".",

0 commit comments

Comments
 (0)