Skip to content

Commit

Permalink
feat: allow aws credentials to be passed in through command line
Browse files Browse the repository at this point in the history
  • Loading branch information
james-hu committed Jan 16, 2024
1 parent 22f2228 commit 7f7898f
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 3 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

<!-- help start -->
Expand Down
12 changes: 12 additions & 0 deletions src/context.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
Expand Down
5 changes: 3 additions & 2 deletions src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' }),
Expand Down
2 changes: 1 addition & 1 deletion src/transformer.ts
Original file line number Diff line number Diff line change
@@ -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'];
Expand Down

0 comments on commit 7f7898f

Please sign in to comment.