11import Serverless from 'serverless' ;
22import { CustomProperties , customProperties } from './lib/custom.properties' ;
3- import { OpenAPIV3 } from 'openapi-types' ;
43import { writeFileSync } from 'fs' ;
54import { functioneventProperties } from './lib/functionEvent.properties' ;
65import { dump } from 'js-yaml' ;
76import { CommandsDefinition } from './lib/comand.types' ;
87import { Generator } from './lib/generator' ;
98import { 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+
1114export 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 }
0 commit comments