Skip to content

Commit 39743a1

Browse files
daviskohchris-olszewski
authored andcommitted
Use region from package.json when generating api.json (#298)
* add vim swp files * use region from shep config in package.json if provided * fix lint errors
1 parent 6625d5e commit 39743a1

File tree

3 files changed

+49
-10
lines changed

3 files changed

+49
-10
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ node_modules/*
22
npm-debug.log
33
lib/
44
tmp/
5+
*.swp

src/generate-endpoint/index.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const { MissingShepConfiguration } = load
88

99
const integration = 'x-amazon-apigateway-integration'
1010

11-
export default async function ({ accountId, path, method, logger = () => {} }) {
11+
export default async function ({ accountId, path, method, region, logger = () => {} }) {
1212
if (!accountId) {
1313
throw new MissingShepConfiguration('Unable to determine your AWS Account ID. Please set it in the `shep` section of package.json')
1414
}
@@ -23,7 +23,7 @@ export default async function ({ accountId, path, method, logger = () => {} }) {
2323
await generateFunction({ name, quiet: true })
2424

2525
logger({ type: 'start', body: 'Setup Endpoint' })
26-
addPath(api, path, method, accountId, fullName)
26+
addPath(api, path, method, accountId, fullName, region)
2727

2828
logger({ type: 'start', body: 'Setup CORS' })
2929
setupCORS(api, path)
@@ -39,7 +39,7 @@ export default async function ({ accountId, path, method, logger = () => {} }) {
3939
return path
4040
}
4141

42-
function addPath (api, path, method, accountId, functionName) {
42+
function addPath (api, path, method, accountId, functionName, region = 'us-east-1') {
4343
if (method === 'any') { method = 'x-amazon-apigateway-any-method' }
4444

4545
if (!api.paths) {
@@ -50,7 +50,7 @@ function addPath (api, path, method, accountId, functionName) {
5050
if (api.paths[path][method] !== undefined) { throw new DuplicateEndpointError(method, path) }
5151
api.paths[path][method] = api.paths[path][method] || {}
5252
api.paths[path][method][integration] = {
53-
uri: `arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:${accountId}:function:${functionName}:\${stageVariables.functionAlias}/invocations`,
53+
uri: `arn:aws:apigateway:${region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${region}:${accountId}:function:${functionName}:\${stageVariables.functionAlias}/invocations`,
5454
passthroughBehavior: 'when_no_match',
5555
httpMethod: 'POST',
5656
type: 'aws_proxy'

test-ava/generate-endpoint/index.js

+44-6
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,57 @@ const path = '/foo'
1010
const method = 'get'
1111
const accountId = 'testid'
1212

13+
let shep
14+
1315
td.when(fs.readJSON('package.json')).thenResolve({ name: 'bar', shep: {} })
1416
td.when(fs.writeJSON(), { ignoreExtraArgs: true }).thenResolve()
15-
td.when(load.api()).thenResolve({ paths: {} })
1617

17-
test.before(() => {
18-
const shep = require('../../src/index')
19-
return shep.generateEndpoint({ accountId, path, method })
18+
const functionName = 'noice-name-bro'
19+
const generateName = td.replace('../../src/util/generate-name')
20+
td.when(generateName(), { ignoreExtraArgs: true }).thenResolve({
21+
fullName: functionName
22+
})
23+
24+
test.before(async () => {
25+
shep = require('../../src/index')
2026
})
2127

22-
test('Writes a new api.json', () => {
28+
test.beforeEach((t) => {
29+
t.context.paths = {}
30+
31+
td.when(load.api()).thenResolve({ paths: t.context.paths })
32+
})
33+
34+
test('Writes a new api.json file', async () => {
35+
await shep.generateEndpoint({ accountId, path, method })
36+
2337
td.verify(fs.writeJSON('api.json'), { ignoreExtraArgs: true })
2438
})
2539

26-
test('Generates a new function', () => {
40+
test('Generates a new function', async () => {
41+
await shep.generateEndpoint({ accountId, path, method })
2742
td.verify(generateFunction(td.matchers.contains({ name: '/foo get' })))
2843
})
44+
45+
const integration = 'x-amazon-apigateway-integration'
46+
47+
const constructIntegrationObject = (accountId, region) => ({
48+
uri: `arn:aws:apigateway:${region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${region}:${accountId}:function:${functionName}:\${stageVariables.functionAlias}/invocations`,
49+
passthroughBehavior: 'when_no_match',
50+
httpMethod: 'POST',
51+
type: 'aws_proxy'
52+
})
53+
54+
test('adds a basic api path', async (t) => {
55+
const region = 'us-west-2'
56+
57+
await shep.generateEndpoint({ accountId, path, method, region })
58+
59+
t.deepEqual(t.context.paths[path][method][integration], constructIntegrationObject(accountId, region))
60+
})
61+
62+
test('defaults to us-east-1 region', async (t) => {
63+
await shep.generateEndpoint({ accountId, path, method })
64+
65+
t.deepEqual(t.context.paths[path][method][integration], constructIntegrationObject(accountId, 'us-east-1'))
66+
})

0 commit comments

Comments
 (0)