diff --git a/README.md b/README.md index ceaedaf..b7a9f7d 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,8 @@ Files generated are ready to be hosted as static website content in this structu ## Quick start +### In Node.js environment + You can have it installed globally like this: ```sh-session @@ -74,6 +76,23 @@ $ npx apig-swagger-ui ... By passing `-h` or `--help` to the command line, you can see all supported arguments and options. +It can also be used in your own code, like this: + +```javascript +import ApigSwaggerUi from 'apig-swagger-ui'; + +class Generator extends ApigSwaggerUi { + argv: string[] = ['api-doc/prod', '-r', 'ap-southeast-2']; +} +await Generator.run(); +``` + +### Install pre-built binary + +*apig-swagger-ui* can be installed through Homebrew (brew install handy-common-utils/tap/apig-swagger-ui for Linux or MacOS), +snap (snap install apig-swagger-ui for Linux except WSL), +or manual download (https://github.com/james-hu/apig-swagger-ui/releases for Windows, Linux, and MacOS ). + ## Manual diff --git a/src/context.ts b/src/context.ts index a5f41ee..8ff2a7c 100644 --- a/src/context.ts +++ b/src/context.ts @@ -1,8 +1,10 @@ // eslint-disable-next-line unicorn/import-style, unicorn/prefer-node-protocol import * as path from 'path'; import ApigSwaggerUi = require('.'); +import type { CredentialsOptions } from 'aws-sdk/lib/credentials'; export class Context { + awsCredentialsOption?: CredentialsOptions; swaggerUiFolder: string; swaggerUiIndexFile: string; swaggerUiInitializerJsFile: string; @@ -18,6 +20,16 @@ export class Context { this.swaggerUiInitializerJsFile = path.join(this.swaggerUiFolder, 'swagger-initializer.js'); this.apiFolder = path.join(this.options.args.path, this.basePathApi); this.homePageFile = path.join(this.options.args.path, this.pathHomePage); + + const accessKeyId = this.options.flags.key; + const secretAccessKey = this.options.flags.secret; + const sessionToken = this.options.flags.token; + + this.awsCredentialsOption = (accessKeyId && secretAccessKey) ? { + accessKeyId, + secretAccessKey, + sessionToken, + } : undefined; } domainFolder(domain: string) { diff --git a/src/generator.ts b/src/generator.ts index 96e574d..0d0d49b 100644 --- a/src/generator.ts +++ b/src/generator.ts @@ -27,8 +27,9 @@ export class Generator { } async generateForOneRegion(homePage: HomePage, transformer: Transformer, region?: string) { - const apig = new APIGateway({ region }); - const apig2 = new ApiGatewayV2({ region }); + const awsClientConfig = { region, credentials: this.context.awsCredentialsOption }; + const apig = new APIGateway(awsClientConfig); + const apig2 = new ApiGatewayV2(awsClientConfig); // eslint-disable-next-line unicorn/no-await-expression-member const domainNameObjects = (await withRetry(() => apig.getDomainNames({ limit: 500 }).promise()))?.items; if (domainNameObjects != null) { diff --git a/src/index.ts b/src/index.ts index 3d93e2d..b4b9046 100644 --- a/src/index.ts +++ b/src/index.ts @@ -21,6 +21,9 @@ class ApigSwaggerUi extends Command { 'update-readme.md': Flags.boolean({ hidden: true, description: 'For developers only, don\'t use' }), region: Flags.string({ char: 'r', multiple: true, description: 'AWS region' }), + key: Flags.string({ char: 'k', description: 'AWS access key ID' }), + secret: Flags.string({ char: 'e', description: 'AWS secret access key' }), + token: Flags.string({ char: 't', description: 'AWS session token' }), include: Flags.string({ char: 'i', default: ['*/*', '*/'], multiple: true, description: 'custom domains and base path mappings to include' }), exclude: Flags.string({ char: 'x', multiple: true, description: 'custom domains and base path mappings to exclude' }), diff --git a/src/transformer.ts b/src/transformer.ts index 8a1884c..4d52832 100644 --- a/src/transformer.ts +++ b/src/transformer.ts @@ -1,7 +1,7 @@ /* eslint-disable unicorn/no-array-for-each */ import { Context } from './context'; import { OpenAPIV3, OpenAPIV2 } from 'openapi-types'; -import { APIGateway, ApiGatewayV2 } from 'aws-sdk'; +import type { APIGateway, ApiGatewayV2 } from 'aws-sdk'; export type OpenApiDocument = OpenAPIV3.Document & { securityDefinitions?: OpenAPIV2.Document['securityDefinitions'];