Skip to content

Commit

Permalink
enabled uploading the spec to S3
Browse files Browse the repository at this point in the history
  • Loading branch information
gdlin committed Nov 10, 2023
1 parent 8a458f8 commit e77819c
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/serverless-openapi-typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import {createGenerator, SchemaGenerator} from "ts-json-schema-generator";
import {camelCase, find, get, isArray, isEmpty, kebabCase, mergeWith, set, upperFirst} from "lodash";
import {ApiGatewayEvent} from "serverless/plugins/aws/package/compile/events/apiGateway/lib/validate";
import {mapKeysDeep, mapValuesDeep} from 'deepdash/standalone'
import path from "path";
import {promisify} from "util";
import {exec} from 'child_process';

const execAsync = promisify(exec);

interface Options {
typescriptApiPath?: string;
Expand Down Expand Up @@ -208,14 +213,29 @@ export default class ServerlessOpenapiTypeScript {
}
}

postProcessOpenApi() {
async postProcessOpenApi() {
// @ts-ignore
const outputFile = this.serverless.processedInput.options.output || 'openapi.json';
const openApi = yaml.load(fs.readFileSync(outputFile));
this.patchOpenApiVersion(openApi);
this.enrichMethodsInfo(openApi);
const encodedOpenAPI = this.encodeOpenApiToStandard(openApi);
fs.writeFileSync(outputFile, outputFile.endsWith('json') ? JSON.stringify(encodedOpenAPI, null, 2) : yaml.dump(encodedOpenAPI));

const s3Bucket = this.serverless.service.custom.documentation?.s3Bucket;
if (s3Bucket) {
await this.uploadFileToS3UsingCLI(outputFile, s3Bucket);
}
}

async uploadFileToS3UsingCLI(filePath: string, bucketName: string) {
const s3Path = `s3://${bucketName}/${path.basename(filePath)}`;
try {
await execAsync(`aws s3 cp "${filePath}" "${s3Path}"`);
this.log(`File uploaded successfully to ${s3Path}`);
} catch (error) {
this.log(`Error uploading file: ${error.message}`);
}
}

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

for (const key of Object.keys(newSchema)) {
Expand Down

0 comments on commit e77819c

Please sign in to comment.