Skip to content

Commit

Permalink
feat: serverlessExpress({ app }) can now be used as well as serverles…
Browse files Browse the repository at this point in the history
…sExpress({ app }).handler
  • Loading branch information
brett-vendia committed Feb 8, 2021
1 parent 184f63e commit 1eb3100
Show file tree
Hide file tree
Showing 14 changed files with 31 additions and 32 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ The only AWS Lambda specific code you need to write is a simple handler like bel
// lambda.js
const serverlessExpress = require('@vendia/serverless-express')
const app = require('./app')
exports.handler = serverlessExpress({ app }).handler
exports.handler = serverlessExpress({ app })
```

## 4.x
Expand Down
2 changes: 1 addition & 1 deletion UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ exports.handler = (event, context) => { awsServerlessExpress.proxy(server, event
const serverlessExpress = require('@vendia/serverless-express')
const app = require('./app')

exports.handler = serverlessExpress({ app }).handler
exports.handler = serverlessExpress({ app })
```

In v4.x, `binaryMimeTypes` isn't required as the `isBase64Encoded` Lambda response value is automatically determined based on the `content-encoding` and `content-type` headers returned by your application. If you need additional control over this, you can specify `binarySettings`:
Expand Down
30 changes: 15 additions & 15 deletions __tests__/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe.each(EACH_MATRIX)('%s:%s: integration tests', (eventSourceName, framewo
path: '/',
httpMethod: 'GET'
})
const response = await serverlessExpressInstance.handler(event)
const response = await serverlessExpressInstance(event)
const expected = '<!DOCTYPE html><html><body>/</body></html>'
expect(response.body).toEqual(expected)
const expectedResponse = makeResponse({
Expand Down Expand Up @@ -87,7 +87,7 @@ describe.each(EACH_MATRIX)('%s:%s: integration tests', (eventSourceName, framewo
queryStringParameters,
multiValueQueryStringParameters
})
const response = await serverlessExpressInstance.handler(event)
const response = await serverlessExpressInstance(event)
const expectedResponse = makeResponse({
eventSourceName,
body: JSON.stringify({
Expand Down Expand Up @@ -127,7 +127,7 @@ describe.each(EACH_MATRIX)('%s:%s: integration tests', (eventSourceName, framewo
httpMethod: 'GET'
})
const serverlessExpressInstanceWithCallbackResolutionMode = serverlessExpress({ app, resolutionMode: 'CALLBACK' })
serverlessExpressInstanceWithCallbackResolutionMode.handler(event, {}, callback)
serverlessExpressInstanceWithCallbackResolutionMode(event, {}, callback)
})

test('resolutionMode = CONTEXT', (done) => {
Expand All @@ -154,7 +154,7 @@ describe.each(EACH_MATRIX)('%s:%s: integration tests', (eventSourceName, framewo
httpMethod: 'GET'
})
const serverlessExpressInstanceWithContextResolutionMode = serverlessExpress({ app, resolutionMode: 'CONTEXT' })
serverlessExpressInstanceWithContextResolutionMode.handler(event, context)
serverlessExpressInstanceWithContextResolutionMode(event, context)
})

test('GET missing route', async () => {
Expand All @@ -163,7 +163,7 @@ describe.each(EACH_MATRIX)('%s:%s: integration tests', (eventSourceName, framewo
path: '/nothing-here',
httpMethod: 'GET'
})
const response = await serverlessExpressInstance.handler(event)
const response = await serverlessExpressInstance(event)
expect(response.body.startsWith('<!DOCTYPE html>')).toBe(true)
const expectedResponse = makeResponse({
eventSourceName,
Expand All @@ -189,7 +189,7 @@ describe.each(EACH_MATRIX)('%s:%s: integration tests', (eventSourceName, framewo
path: '/users/3',
httpMethod: 'GET'
})
const response = await serverlessExpressInstance.handler(event)
const response = await serverlessExpressInstance(event)
const expectedResponse = makeResponse({
eventSourceName,
body: JSON.stringify({ id: '3' }),
Expand All @@ -213,7 +213,7 @@ describe.each(EACH_MATRIX)('%s:%s: integration tests', (eventSourceName, framewo
httpMethod: 'GET'
})

const response = await serverlessExpressInstance.handler(event)
const response = await serverlessExpressInstance(event)

const samLogoImage = fs.readFileSync(samLogoPath)
const samLogoBase64 = Buffer.from(samLogoImage).toString('base64')
Expand Down Expand Up @@ -275,7 +275,7 @@ describe.each(EACH_MATRIX)('%s:%s: integration tests', (eventSourceName, framewo
'content-type': ['application/json']
}
})
const response = await serverlessExpressInstance.handler(event)
const response = await serverlessExpressInstance(event)
const expectedResponse = makeResponse({
eventSourceName,
body: JSON.stringify({ data: { name: name } }),
Expand All @@ -297,7 +297,7 @@ describe.each(EACH_MATRIX)('%s:%s: integration tests', (eventSourceName, framewo
path: '/users/1',
httpMethod: 'DELETE'
})
const response = await serverlessExpressInstance.handler(event)
const response = await serverlessExpressInstance(event)
const expectedResponse = makeResponse({
eventSourceName,
body: '[]',
Expand Down Expand Up @@ -328,7 +328,7 @@ describe.each(EACH_MATRIX)('%s:%s: integration tests', (eventSourceName, framewo
'content-type': ['application/json']
}
})
const response = await serverlessExpressInstance.handler(event)
const response = await serverlessExpressInstance(event)
const expectedResponse = makeResponse({
eventSourceName,
body: JSON.stringify({ id: '2', name }),
Expand Down Expand Up @@ -360,7 +360,7 @@ describe.each(EACH_MATRIX)('%s:%s: integration tests', (eventSourceName, framewo
'content-type': ['application/json']
}
})
const response = await serverlessExpressInstance.handler(event)
const response = await serverlessExpressInstance(event)
const expectedResponse = makeResponse({
eventSourceName,
body: JSON.stringify({ id: '2', name }),
Expand All @@ -378,7 +378,7 @@ describe.each(EACH_MATRIX)('%s:%s: integration tests', (eventSourceName, framewo
})

test.skip('respondToEventSourceWithError', async () => {
const response = await serverlessExpressInstance.handler(null)
const response = await serverlessExpressInstance(null)
expect(response).toEqual({
statusCode: 500,
body: '',
Expand All @@ -397,7 +397,7 @@ describe.each(EACH_MATRIX)('%s:%s: integration tests', (eventSourceName, framewo
path: '/cookie',
httpMethod: 'GET'
})
const response = await serverlessExpressInstance.handler(event)
const response = await serverlessExpressInstance(event)

const expectedSetCookieHeaders = [
'Foo=bar; Path=/',
Expand Down Expand Up @@ -437,7 +437,7 @@ describe.each(EACH_MATRIX)('%s:%s: integration tests', (eventSourceName, framewo
debug: jest.fn()
}
serverlessExpressInstance = serverlessExpress({ app, log: customLogger })
await serverlessExpressInstance.handler(event)
await serverlessExpressInstance(event)

expect(customLogger.debug.mock.calls.length).toBe(6)

Expand All @@ -448,7 +448,7 @@ describe.each(EACH_MATRIX)('%s:%s: integration tests', (eventSourceName, framewo
// customLogger.debug = jest.fn()

// serverlessExpressInstance = serverlessExpress({ app, log: customLogger })
// await serverlessExpressInstance.handler(event)
// await serverlessExpressInstance(event)
// expect(customLogger.debug.mock.calls.length).toBe(0)
})

Expand Down
2 changes: 1 addition & 1 deletion examples/alb/src/lambda.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const serverlessExpress = require('@vendia/serverless-express')
const app = require('./app')

exports.handler = serverlessExpress({ app }).handler
exports.handler = serverlessExpress({ app })
2 changes: 1 addition & 1 deletion examples/basic-starter-api-gateway-v1/src/lambda.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ require('source-map-support/register')
const serverlessExpress = require('@vendia/serverless-express')
const app = require('./app')

exports.handler = serverlessExpress({ app }).handler
exports.handler = serverlessExpress({ app })
2 changes: 1 addition & 1 deletion examples/basic-starter-api-gateway-v2/src/lambda.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ require('source-map-support/register')
const serverlessExpress = require('@vendia/serverless-express')
const app = require('./app')

exports.handler = serverlessExpress({ app }).handler
exports.handler = serverlessExpress({ app })
2 changes: 1 addition & 1 deletion examples/basic-starter-hapi/src/lambda.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ require('source-map-support/register')
const serverlessExpress = require('@vendia/serverless-express')
const app = require('./app')

exports.handler = serverlessExpress({ app }).handler
exports.handler = serverlessExpress({ app })
2 changes: 1 addition & 1 deletion examples/basic-starter-koa/src/lambda.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ require('source-map-support/register')
const serverlessExpress = require('@vendia/serverless-express')
const app = require('./app')

exports.handler = serverlessExpress({ app }).handler
exports.handler = serverlessExpress({ app })
2 changes: 1 addition & 1 deletion examples/basic-starter-nestjs/src/lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async function bootstrap() {

await nestApp.init();

cachedServer = serverlessExpress({ app: expressApp }).handler;
cachedServer = serverlessExpress({ app: expressApp });
}

return cachedServer;
Expand Down
2 changes: 1 addition & 1 deletion examples/custom-mapper-dynamodb/src/lambda.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ exports.handler = serverlessExpress({
getRequest: mapDynamoDbEventToHttpRequest,
getResponse: mapResponseToDynamoDb
}
}).handler
})
2 changes: 1 addition & 1 deletion examples/lambda-edge/src/lambda.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const serverlessExpress = require('@vendia/serverless-express')
const app = require('./app')

exports.handler = serverlessExpress({ app }).handler
exports.handler = serverlessExpress({ app })
2 changes: 1 addition & 1 deletion examples/sails-example/lambda.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ const appPromise = require('./app');

exports.handler = async (event, context, callback) => {
const app = await appPromise;
return serverlessExpress({ app }).handler(event, context, callback);
return serverlessExpress({ app })(event, context, callback);
};
9 changes: 4 additions & 5 deletions src/configure.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,10 @@ function configure ({
callback
})
} = {}) {
return {
proxy: configureProxy,
handler: configureHandler,
log: configureLog
}
configureHandler.handler = configureHandler
configureHandler.proxy = configureProxy
configureHandler.log = configureLog
return configureHandler
}

module.exports = configure
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function proxy (configureOptions, event, context, resolutionMode, callback) {
...configureOptions,
resolutionMode
})
return se.handler(event, context, callback)
return se(event, context, callback)
}
module.exports.createServer = createServer
module.exports.proxy = proxy

0 comments on commit 1eb3100

Please sign in to comment.