Skip to content

Commit e77819c

Browse files
committed
enabled uploading the spec to S3
1 parent 8a458f8 commit e77819c

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

src/serverless-openapi-typescript.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ import {createGenerator, SchemaGenerator} from "ts-json-schema-generator";
55
import {camelCase, find, get, isArray, isEmpty, kebabCase, mergeWith, set, upperFirst} from "lodash";
66
import {ApiGatewayEvent} from "serverless/plugins/aws/package/compile/events/apiGateway/lib/validate";
77
import {mapKeysDeep, mapValuesDeep} from 'deepdash/standalone'
8+
import path from "path";
9+
import {promisify} from "util";
10+
import {exec} from 'child_process';
11+
12+
const execAsync = promisify(exec);
813

914
interface Options {
1015
typescriptApiPath?: string;
@@ -208,14 +213,29 @@ export default class ServerlessOpenapiTypeScript {
208213
}
209214
}
210215

211-
postProcessOpenApi() {
216+
async postProcessOpenApi() {
212217
// @ts-ignore
213218
const outputFile = this.serverless.processedInput.options.output || 'openapi.json';
214219
const openApi = yaml.load(fs.readFileSync(outputFile));
215220
this.patchOpenApiVersion(openApi);
216221
this.enrichMethodsInfo(openApi);
217222
const encodedOpenAPI = this.encodeOpenApiToStandard(openApi);
218223
fs.writeFileSync(outputFile, outputFile.endsWith('json') ? JSON.stringify(encodedOpenAPI, null, 2) : yaml.dump(encodedOpenAPI));
224+
225+
const s3Bucket = this.serverless.service.custom.documentation?.s3Bucket;
226+
if (s3Bucket) {
227+
await this.uploadFileToS3UsingCLI(outputFile, s3Bucket);
228+
}
229+
}
230+
231+
async uploadFileToS3UsingCLI(filePath: string, bucketName: string) {
232+
const s3Path = `s3://${bucketName}/${path.basename(filePath)}`;
233+
try {
234+
await execAsync(`aws s3 cp "${filePath}" "${s3Path}"`);
235+
this.log(`File uploaded successfully to ${s3Path}`);
236+
} catch (error) {
237+
this.log(`Error uploading file: ${error.message}`);
238+
}
219239
}
220240

221241
// OpenApi spec define ^[a-zA-Z0-9\.\-_]+$ for legal fields - https://spec.openapis.org/oas/v3.1.0#components-object
@@ -292,8 +312,8 @@ export default class ServerlessOpenapiTypeScript {
292312
if (typeof schema === "object" && schema !== null) {
293313
const newSchema = JSON.parse(JSON.stringify(schema));
294314
if (newSchema.hasOwnProperty("const")) {
295-
const { const: _, ...rest } = newSchema;
296-
return { ...rest, enum: [newSchema.const] };
315+
const {const: _, ...rest} = newSchema;
316+
return {...rest, enum: [newSchema.const]};
297317
}
298318

299319
for (const key of Object.keys(newSchema)) {

0 commit comments

Comments
 (0)