Skip to content

Commit

Permalink
Add default swagger definition with path matching
Browse files Browse the repository at this point in the history
  • Loading branch information
YoruNoHikage committed Aug 31, 2016
1 parent ed3c903 commit ff30cf7
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
71 changes: 71 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Apex API Gateway
`apex-api-gateway` helps you deploying your Apex project into API Gateway. This is an early version, only basic commands are available.
If you need more advanced features, you can use [AWS CLI](https://aws.amazon.com/fr/cli/).

[![npm version](https://img.shields.io/npm/v/apex-api-gateway.svg?style=flat-square)](https://www.npmjs.com/package/apex-api-gateway)

Installation
------------

Expand Down Expand Up @@ -34,3 +36,72 @@ You can check out the [Apex API Gateway Boilerplate](https://github.com/YoruNoHi
And to update API :

`apex-api-gateway update`

Writing your swagger definitions
--------------------------------

### project.json

After initiating your Apex project, you'll get a `project.json` containing everything Apex needs to run your lambas.
We can then use it to define other definitions under a `x-api-gateway` key, and here's what you can define.

- `base_path`
- `stage_name`
- `rest-api-id`: You can define this key yourself or let the `create` command do it for you

#### `paths`

You can use this key to match your functions's path and add default Swagger definition.
For example, adding CORS to API Gateway can be done like this:

```js
"paths": {
".+": { // this will match every path
"options": { // adding options for preflight requests
/*
You can add CORS definitions for OPTIONS request here.
Check out config example: http://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-cors.html#enable-cors-for-resource-using-swagger-importer-tool
*/
}
},
"/sweets(.+)": { // match a /sweets path and all the descendants
"get": { /* ... */ },
"put": { /* ... */ }
}
}
```

**Warning:** `^` is prepended and `$` is appended to the regex matching because I find it easier to match a plain path just writing it.
This is questionable and if you disagree, you can open an issue to argue. :P

#### `swagger-func-template`

This template represents the default swagger definition for every function defined in your Apex project.
_The property's behavior is very similar to `paths` property and will likely be integrated into it in the future.
The only difference is this definition is method agnostic._

Example:
```js
"swagger-func-template": {
"consumes": ["application/json"],
"produces": ["application/json"],
"responses": { /* ... */ },
"x-amazon-apigateway-integration": { /* ... */ },
"x-amazon-apigateway-auth" : { /* ... */ }
}
```

### myFunction/function.json

This one is easier to define, just define your method in a `x-api-gateway` key like this:

```js
"x-api-gateway": {
"method": "post",
"path": "/sweets",
"parameters":[/* ... */]
/* ... */
}
```

`method` and `paths` are required since it will be used as a key for the Swagger main file. The rest is only regular Swagger definition.
10 changes: 10 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const fs = require('fs');
const path = require('path');
const defaultsDeep = require('lodash.defaultsDeep');
const entries = require('lodash.topairs');
const yargs = require('yargs');
const AWS = require('aws-sdk');
const apigateway = new AWS.APIGateway();
Expand Down Expand Up @@ -93,6 +94,15 @@ function update({ config, stdout }) {
paths[path][method] = renderMethod(name, definition);
});

entries(projectConfig['x-api-gateway']['paths']).forEach(([key, value]) => {
const keyPattern = new RegExp(`^${key}$`);
const matchedPaths = entries(paths).filter(([path]) => keyPattern.test(path));

matchedPaths.forEach(([path, pathValue]) => {
defaultsDeep(pathValue, value); // paths local mutation seems to be the best
});
});

return paths;
};

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apex-api-gateway",
"version": "0.1.0",
"version": "0.2.0",
"licence": "MIT",
"description": "Create and deploy your Apex project on AWS API Gateway using Swagger configuration",
"keywords": [
Expand All @@ -15,6 +15,7 @@
"dependencies": {
"aws-sdk": "^2.5.2",
"lodash.defaultsdeep": "^4.5.1",
"lodash.topairs": "^4.3.0",
"yargs": "^5.0.0"
},
"bin": {
Expand Down

0 comments on commit ff30cf7

Please sign in to comment.