Skip to content

Commit

Permalink
Merge pull request #1 from yndlingsfar/feature/cors
Browse files Browse the repository at this point in the history
Feature/cors
  • Loading branch information
yndlingsfar authored Apr 19, 2021
2 parents 0c74fd4 + 73c08a5 commit f56fcf5
Show file tree
Hide file tree
Showing 28 changed files with 920 additions and 1,151 deletions.
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## 1.1.0 (2021-13-04)
## 1.2.0 (2021-04-19)
- added support for CORS generation

## 1.1.0 (2021-04-13)
- change the supported serverless version to 2.x

## 1.0.0 (2021-04-8)
## 1.0.0 (2021-04-08)
- The very first release
50 changes: 48 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ The plugin provides helper functions for separating the x-amazon-apigateway exte
- spin up different x-amazon-apigateway integrations based on your stage
- separate infrastructure (aws) from openapi specification
- use mock integrations for functional testing
- **[NEW]:** auto-generating CORS methods, headers and api gateway mocking response

## Table of contents

- [Install](#install)
- [Configuration](#configuration)
- [Basic Usage](#basic-usage)
- [CORS Generator](#cors-generator)
- [Configuration Reference](#configuration-reference)
- [Example](#example)

Expand All @@ -28,15 +31,59 @@ Add the plugin to your serverless.yml file
plugins:
- serverless-openapi-integration-helper
```
## Configuration
You can configure the plugin under the key **openApiIntegration**. See
See [Configuration Reference](#configuration-reference) for a list of available options
The mapping array must be used to tell the plugin where to look for files containing the **x-amazon-apigateway-integration** blocks. The mapping is done per stage.
```yml
openApiIntegration:
inputFile: schema.yml
mapping:
- path: schemas
stage: dev
- path: schemas
stage: prod
- path: mocks/customer.yml
stage: test
```
For example if in dev environment all .yml files inside the schemas directory will be processed when running
```
serverless integration merge --stage=test && serverless deploy --stage=dev
```

## CORS generator

The plugin can generate full CORS support out of the box.
```yml
openApiIntegration:
cors: true
...
```

If enabled, the plugin will generate all required OPTIONS methods as well as the required header informations and adds a mocking response to api gateway.
You can customize the CORS templates by placing your own files inside a directory called **openapi-integration/** (in the directory root). The following files can be overwritten:

| Filebame | Description |
| ------------- |:-------------:|
| headers.yml | All headers required for CORS support |
| integration.yml | Contains the x-amazon-apigateway-integration block |
| path.yml| OpenApi specification for the OPTIONS method |

See the EXAMPLES directory for detailed instructions.

## Configuration Reference

configure the plugin under the key **openApiIntegration**


```yml
openApiIntegration:
inputFile: schema.yml #required
inputDirectory: ./ #optional, defaults to ./
cors: true #optional, defaults to false
mapping: #required for at least one stage, where to read the aws integration files from (file or directory)
- path: schemas
stage: dev
Expand All @@ -49,13 +96,13 @@ openApiIntegration:
```
## Basic usage
**Given you have following OpenApi Specification 3 (OAS3) file *schema.yml***
```yml
openapi: 3.0.0
info:
description: User Registration
version: 1.0.0
title: UserRegistration
paths:
/api/v1/user:
post:
Expand Down Expand Up @@ -151,7 +198,6 @@ resources:
```
## Example
```yml
service:
name: user-registration
Expand Down
32 changes: 32 additions & 0 deletions examples/api.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
openapi: 3.0.0
info:
description: User Registration
version: 1.0.0
title: UserRegistrationDemo
paths:
/api/v1/user:
post:
summary: adds a user
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/Customer'
responses:
'201':
description: user created
components:
schemas:
Customer:
type: object
required:
- email_address
- password
properties:
email_address:
type: string
example: [email protected]
password:
type: string
format: password
example: someStrongPassword#
11 changes: 11 additions & 0 deletions examples/integrations/production.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
paths:
/api/v1/user:
post:
x-amazon-apigateway-integration:
httpMethod: "POST"
uri: ${self:custom.url}
type: "http"
passthroughBehavior: "when_no_match"
responses:
"2\\d{2}":
statusCode: "201"
15 changes: 15 additions & 0 deletions examples/mocks/customer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
paths:
/api/v1/user:
post:
x-amazon-apigateway-integration:
httpMethod: "POST"
type: "mock"
passthroughBehavior: "when_no_match"
requestTemplates:
application/json: |
{
"statusCode" : 204
}
responses:
"2\\d{2}":
statusCode: "201"
15 changes: 15 additions & 0 deletions examples/openapi-integration/headers.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Access-Control-Allow-Origin:
style: simple
explode: false
schema:
type: string
Access-Control-Allow-Methods:
style: simple
explode: false
schema:
type: string
Access-Control-Allow-Headers:
style: simple
explode: false
schema:
type: string
14 changes: 14 additions & 0 deletions examples/openapi-integration/integration.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
x-amazon-apigateway-integration:
type: mock
requestTemplates:
application/json: |
{
"statusCode" : 204
}
responses:
default:
statusCode: "204"
responseParameters:
method.response.header.Access-Control-Allow-Headers: '''Content-Type,X-Amz-Date,Authorization,X-Api-Key'''
method.response.header.Access-Control-Allow-Methods: '''*'''
method.response.header.Access-Control-Allow-Origin: '''*'''
11 changes: 11 additions & 0 deletions examples/openapi-integration/path.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
summary: Provide CORS
description: Basic CORS functionality with an aws mocking integration
responses:
"204":
description: No data
headers:
X-Some-Custom-Header:
style: simple
explode: false
schema:
type: string
53 changes: 53 additions & 0 deletions examples/serverless.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Welcome to Serverless!
#
# This file is the main config file for your service.
# It's very minimal at this point and uses default values.
# You can always add more config options for more control.
# We've included some commented out config examples here.
# Just uncomment any of them to get that config option.
#
# For full config options, check the docs:
# docs.serverless.com
#
# Happy Coding!

service: myservice
# app and org for use with dashboard.serverless.com
#app: your-app-name
#org: your-org-name

# You can pin your service to only deploy with a specific Serverless version
# Check out our docs for more details
frameworkVersion: '2'

provider:
name: aws
runtime: nodejs12.x
lambdaHashingVersion: 20201221

plugins:
- serverless-openapi-integration-helper

openApiIntegration:
inputFile: api.yml
cors: true
mapping:
- stage: dev
path: integrations
- stage: test
path: mocks

functions:

Resources:
ApiGatewayRestApi:
Type: AWS::ApiGateway::RestApi
Properties:
ApiKeySourceType: HEADER
Body: ${file(openapi-integration/api.yml)}
Description: DCP - Public address verification Gateway (${opt:stage, self:provider.stage})
FailOnWarnings: false
Name: ${opt:stage, self:provider.stage}-dcp-public-addressVerification-gateway
EndpointConfiguration:
Types:
- REGIONAL
51 changes: 0 additions & 51 deletions lib/YamlParser.js

This file was deleted.

Loading

0 comments on commit f56fcf5

Please sign in to comment.