Skip to content

Commit

Permalink
add support for variables syntax #2
Browse files Browse the repository at this point in the history
  • Loading branch information
ikkyu-3 committed Aug 6, 2019
1 parent 9e01324 commit 5faf720
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 69 deletions.
1 change: 1 addition & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class ServerlessOpenapi3Plugin {
}
}).then((res) => res.resolved);
this.replaceOpenAPi(resources, openApi);
await this.serverless.variables.populateService(this.serverless.pluginManager.cliOptions);
}
catch (e) {
console.error(e);
Expand Down
2 changes: 1 addition & 1 deletion serverless/resources/open-api/paths/pets-id.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ get:
schema:
$ref: "#/components/schemas/Pet"
x-amazon-apigateway-integration:
uri: "http://petstore.execute-api.ap-northeast-1.amazonaws.com/petstore/pets/{petId}"
uri: "${self:custom.baseUrl}/petstore/pets/{petId}"
responses:
default:
statusCode: "200"
Expand Down
4 changes: 2 additions & 2 deletions serverless/resources/open-api/paths/pets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ get:
schema:
$ref: "#/components/schemas/Pets"
x-amazon-apigateway-integration:
uri: "http://petstore.execute-api.ap-northeast-1.amazonaws.com/petstore/pets"
uri: "${self:custom.baseUrl}/petstore/pets"
responses:
default:
statusCode: "200"
Expand Down Expand Up @@ -53,7 +53,7 @@ post:
schema:
$ref: "#/components/schemas/NewPetResponse"
x-amazon-apigateway-integration:
uri: "http://petstore.execute-api.ap-northeast-1.amazonaws.com/petstore/pets"
uri: "${self:custom.baseUrl}/petstore/pets"
responses:
default:
statusCode: "200"
Expand Down
1 change: 1 addition & 0 deletions serverless/serverless.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ provider:

custom:
openApiPath: './resources/open-api/index.yaml'
baseUrl: 'http://petstore.execute-api.ap-northeast-1.amazonaws.com'

resources:
- ${file(./resources/api-gateway.yaml)}
9 changes: 8 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import util from "util";
import { isObject, isArray } from "lodash";
import { safeLoad } from "js-yaml";
import { resolveRefs } from "json-refs";
import Serverless from "serverless";
import * as Serverless from "serverless";

interface Variables {
populateService(cliOptions: any): Promise<void>;
}

const readFile = util.promisify(fs.readFile);

Expand Down Expand Up @@ -52,6 +56,9 @@ class ServerlessOpenapi3Plugin {
);

this.replaceOpenAPi(resources, openApi);
await (this.serverless.variables as Variables).populateService(
this.serverless.pluginManager.cliOptions
);
} catch (e) {
console.error(e);
}
Expand Down
15 changes: 10 additions & 5 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import Serverless from "serverless";
import ServerlessOpenapi3Plugin from "../src/index";
import { dummyServerless, dummyConfig, openApi } from "./testData";
import { service, options, openApi } from "./testData";

describe("ServerlessOpenapi3Plugin Test", () => {
const plugin = new ServerlessOpenapi3Plugin(dummyServerless, dummyConfig);

it("should replace OpenApi Object of RestApi Resource with resolved OpenApi Object", async () => {
const serverless = new Serverless();
serverless.config.servicePath = __dirname;
Object.assign((serverless.variables as any).service, service);

const plugin = new ServerlessOpenapi3Plugin(serverless, options);

const func = plugin.hooks["package:createDeploymentArtifacts"];
await func();

expect(
dummyServerless.variables.service.resources.Resources.RestApi.Properties
.Body
(serverless.variables as any).service.resources.Resources.RestApi
.Properties.Body
).toEqual(openApi);
});
});
2 changes: 1 addition & 1 deletion test/open-api/paths/pets-id.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ get:
schema:
$ref: "#/components/schemas/Pet"
x-amazon-apigateway-integration:
uri: "http://petstore.execute-api.ap-northeast-1.amazonaws.com/petstore/pets/{petId}"
uri: "${self:custom.baseUrl}/petstore/pets/{petId}"
responses:
default:
statusCode: "200"
Expand Down
4 changes: 2 additions & 2 deletions test/open-api/paths/pets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ get:
schema:
$ref: "#/components/schemas/Pets"
x-amazon-apigateway-integration:
uri: "http://petstore.execute-api.ap-northeast-1.amazonaws.com/petstore/pets"
uri: "${self:custom.baseUrl}/petstore/pets"
responses:
default:
statusCode: "200"
Expand Down Expand Up @@ -53,7 +53,7 @@ post:
schema:
$ref: "#/components/schemas/NewPetResponse"
x-amazon-apigateway-integration:
uri: "http://petstore.execute-api.ap-northeast-1.amazonaws.com/petstore/pets"
uri: "${self:custom.baseUrl}/petstore/pets"
responses:
default:
statusCode: "200"
Expand Down
108 changes: 51 additions & 57 deletions test/testData.ts
Original file line number Diff line number Diff line change
@@ -1,75 +1,69 @@
import Serverless from "serverless";

export const dummyServerless: any = {
variables: {
service: {
resources: {
Resources: {
RestApi: {
Type: "AWS::ApiGateway::RestApi",
Properties: {
Name: "rest-api",
Body: {
openapi: "3.0.1",
info: {
title: "PetStore",
version: "2019-07-27T09:07:00Z"
},
servers: [
{
url:
"https://ifi2axc0wi.execute-api.ap-northeast-1.amazonaws.com/{basePath}",
variables: {
basePath: {
default: "/dev"
}
}
}
],
paths: {
"/pets": {
$ref: "./paths/pets.yaml"
},
"/pets/{petId}": {
$ref: "./paths/pets-id.yaml"
},
"/": {
$ref: "./paths/root.yaml"
}
},
components: {
schemas: {
$ref: "./components/schemas.yaml"
export const service: any = {
custom: {
openApiPath: "./open-api/index.yaml",
baseUrl: "http://petstore.execute-api.ap-northeast-1.amazonaws.com"
},
resources: {
Resources: {
RestApi: {
Type: "AWS::ApiGateway::RestApi",
Properties: {
Name: "rest-api",
Body: {
openapi: "3.0.1",
info: {
title: "PetStore",
version: "2019-07-27T09:07:00Z"
},
servers: [
{
url:
"https://ifi2axc0wi.execute-api.ap-northeast-1.amazonaws.com/{basePath}",
variables: {
basePath: {
default: "/dev"
}
}
}
}
},
RestApiDeployment: {
Type: "AWS::ApiGateway::Deployment",
Properties: {
RestApiId: {
Ref: "RestApi"
],
paths: {
"/pets": {
$ref: "./paths/pets.yaml"
},
"/pets/{petId}": {
$ref: "./paths/pets-id.yaml"
},
StageName: "dev"
"/": {
$ref: "./paths/root.yaml"
}
},
DependsOn: "RestApi"
components: {
schemas: {
$ref: "./components/schemas.yaml"
}
}
}
}
},
custom: {
openApiPath: "./open-api/index.yaml"
RestApiDeployment: {
Type: "AWS::ApiGateway::Deployment",
Properties: {
RestApiId: {
Ref: "RestApi"
},
StageName: "dev"
},
DependsOn: "RestApi"
}
}
},
config: {
servicePath: __dirname
}
};

export const dummyConfig: Serverless.Options = {
stage: null,
region: null
export const options: Serverless.Options = {
stage: "dev",
region: "us-east-1"
};

export const openApi = {
Expand Down

0 comments on commit 5faf720

Please sign in to comment.