Skip to content

Commit

Permalink
feat: add option to exclude specific stages (#46) (#47)
Browse files Browse the repository at this point in the history
* feat: Only deploy swagger on specified stages (#46)

* Refactor to excluded stages

* update readme with excludeStages option

* Update src/ServerlessAutoSwagger.ts

Co-authored-by: Brandon Faulkner <[email protected]>

* Update src/ServerlessAutoSwagger.ts

Co-authored-by: Brandon Faulkner <[email protected]>

Co-authored-by: jochem <[email protected]>
Co-authored-by: Brandon Faulkner <[email protected]>
  • Loading branch information
3 people authored Feb 23, 2022
1 parent 5ab25dc commit 89335dd
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ custom:
useStage?: true | false
basePath?: '/string'
schemes?: ['http', 'https', 'ws', 'wss']
excludeStages?: ['production', 'anyOtherStage']
```

`generateSwaggerOnDeploy` is a boolean which decides whether to generate a new swagger file on deployment. Default is `true`.
Expand All @@ -64,6 +65,8 @@ apiKeyHeaders: ['Authorization', 'x-api-key']

`schemes` is an optional array (containing one of `http`, `https`, `ws`, or `wss`) for specifying schemes. If not provided, uses the scheme used to serve the API specification (reflecting Swagger's behavior)

`excludeStages` is an optional array of strings that should contain stages you do **not** want Swagger UI and Swagger JSON to be deployed in.

## Adding more details

The default swagger file from vanilla Serverless framework will have the correct paths and methods but no details about the requests or responses.
Expand Down
6 changes: 6 additions & 0 deletions src/ServerlessAutoSwagger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ class ServerlessAutoSwagger {
};

predeploy = async () => {
const stage = this.serverless.service.provider.stage;
const excludedStages = this.serverless.service.custom?.autoswagger?.excludeStages;
if (excludedStages?.includes(stage)) {
console.log(`Swagger lambdas will not be deployed for stage [${stage}], as they have been marked for exclusion.`);
return;
}
const generateSwaggerOnDeploy =
this.serverless.service.custom?.autoswagger?.generateSwaggerOnDeploy;
if (generateSwaggerOnDeploy === undefined || generateSwaggerOnDeploy) {
Expand Down
1 change: 1 addition & 0 deletions src/serverlessPlugin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export interface AutoSwaggerCustomConfig {
swaggerPath?: string
basePath?: string
schemes?: SwaggerScheme[]
excludeStages?: string[]
}
}

Expand Down

0 comments on commit 89335dd

Please sign in to comment.