Skip to content

Commit

Permalink
feat: add backwards compatibility for most of v3 usage
Browse files Browse the repository at this point in the history
  • Loading branch information
brett-vendia committed Feb 8, 2021
1 parent 4c5178c commit 184f63e
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 2 deletions.
37 changes: 35 additions & 2 deletions __tests__/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ describe.each(EACH_MATRIX)('%s:%s: integration tests', (eventSourceName, framewo
app.engine('.ejs', ejs)
app.set('views', path.join(jestHelpersPath, 'views'))
router.get('/', (req, res) => {
const currentInvoke = serverlessExpress.getCurrentInvoke()
const eventPath = currentInvoke.event.path || currentInvoke.event.rawPath || currentInvoke.event.Records[0].cf.request.uri
const { event } = serverlessExpress.getCurrentInvoke()
const eventPath = event.path || event.rawPath || event.Records[0].cf.request.uri
res.render('index', {
path: eventPath
})
Expand Down Expand Up @@ -451,4 +451,37 @@ describe.each(EACH_MATRIX)('%s:%s: integration tests', (eventSourceName, framewo
// await serverlessExpressInstance.handler(event)
// expect(customLogger.debug.mock.calls.length).toBe(0)
})

test('legacy/deprecated usage', async () => {
const serverlessExpressMiddleware = require('../src/middleware')
app = express()
router = express.Router()
app.use('/', router)
router.use(serverlessExpressMiddleware.eventContext())
router.get('/users', (req, res) => {
const { event } = req.apiGateway
const eventPath = event.path || event.rawPath || event.Records[0].cf.request.uri
res.json({
path: eventPath
})
})
const event = makeEvent({
eventSourceName,
path: '/users',
httpMethod: 'GET'
})
const binaryMimeTypes = []
const server = serverlessExpress.createServer(app, null, binaryMimeTypes)
const response = await serverlessExpress.proxy(server, event)
const expectedResponse = makeResponse({
eventSourceName,
body: JSON.stringify({ path: '/users' }),
multiValueHeaders: {
'content-length': ['17'],
etag: ['W/"11-eM8YArY+qNwdvTL2ppeAaFc4Oq8"']
},
statusCode: 200
})
expect(response).toEqual(expectedResponse)
})
})
29 changes: 29 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,32 @@ const { getCurrentInvoke } = require('./current-invoke')
module.exports = configure
module.exports.getCurrentInvoke = getCurrentInvoke
module.exports.configure = configure

// Legacy/deprecated:

function createServer (app, serverListenCallback, binaryMimeTypes) {
console.warn('You\'re using the deprecated createServer method that will be removed in the next major version. See https://github.com/vendia/serverless-express/blob/mainline/UPGRADE.md to upgrade.')

if (serverListenCallback) {
throw new Error('serverListenCallback is no longer supported.')
}

const configureOptions = {
app,
binarySettings: {
contentTypes: binaryMimeTypes
}
}

return configureOptions
}

function proxy (configureOptions, event, context, resolutionMode, callback) {
const se = configure({
...configureOptions,
resolutionMode
})
return se.handler(event, context, callback)
}
module.exports.createServer = createServer
module.exports.proxy = proxy
21 changes: 21 additions & 0 deletions src/middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const { getCurrentInvoke } = require('./current-invoke')

module.exports.eventContext = options => function apiGatewayEventParser (req, res, next) {
options = options || {}
const reqPropKey = options.reqPropKey || 'apiGateway'
const deleteHeaders = options.deleteHeaders === undefined ? true : options.deleteHeaders

const currentInvoke = getCurrentInvoke()

req[reqPropKey] = currentInvoke

if (!deleteHeaders) {
const clonedEventWithoutBody = JSON.parse(JSON.stringify(currentInvoke.event))
delete clonedEventWithoutBody.body

req.headers['x-apigateway-event'] = encodeURIComponent(JSON.stringify(clonedEventWithoutBody))
req.headers['x-apigateway-context'] = encodeURIComponent(JSON.stringify(currentInvoke.context))
}

next()
}

0 comments on commit 184f63e

Please sign in to comment.