@everestate/serverless-router
Advanced tools
Comparing version 0.0.2 to 0.1.0
@@ -48,5 +48,11 @@ const ServerlessRouter = require('../ServerlessRouter'); | ||
const router = subj(); | ||
router.weekday.saturday(() => ({ saturday: true })); | ||
router.weekday.saturday((...args) => ({ saturday: true, args })); | ||
router.mismatch(() => ({ mismatched: true })); | ||
expect(router.dispatch(new Date('2014-08-16'))).toEqual({ saturday: true }); | ||
expect(router.dispatch(new Date('2014-08-16'))).toEqual({ | ||
saturday: true, | ||
args: [ | ||
{}, | ||
new Date('2014-08-16'), | ||
], | ||
}); | ||
expect(router.dispatch(new Date('2017-09-05'))).toEqual({ mismatched: true }); | ||
@@ -53,0 +59,0 @@ }); |
@@ -40,3 +40,3 @@ class ServerlessRouter { | ||
if (route) { | ||
return route.callback.apply(null, [...args, route.context || {}]); | ||
return route.callback.apply(null, [route.context || {}, ...args]); | ||
} | ||
@@ -43,0 +43,0 @@ } |
{ | ||
"name": "@everestate/serverless-router", | ||
"version": "0.0.2", | ||
"version": "0.1.0", | ||
"description": "Fast, minimalist, pluggable, universal router.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -25,29 +25,28 @@ # @everestate/serverless-router | ||
const router = new ServerlessRouter([ServerlessRouterWebPlugin]); | ||
cosnt userService = require('../services/userService'); | ||
router.web | ||
.get('/users/:userId/appointments', (event, context, callback) => { | ||
const appointments = findAppointmentsByUserId(event.pathParameters.userId); | ||
return callback(null, { | ||
statusCode: '200', | ||
body: JSON.stringify({ appointments }), | ||
}); | ||
}) | ||
.post('/users/:userId/appointments', (event, context, callback) => { | ||
const { userId } = event.pathParameters; | ||
const appointment = createAppointment({ ...event.body, userId }); | ||
return callback(null, { | ||
statusCode: '201', | ||
body: JSON.stringify({ appointment }), | ||
}); | ||
}) | ||
.delete('/users/:userId/appointments/:appointmentId', (event, context, callback) => { | ||
const { appointmentId } = event.pathParameters; | ||
deleteAppointmentById(appointmentId); | ||
return callback(null, { | ||
statusCode: '204', | ||
body: '', | ||
}); | ||
function dispatch(event) { | ||
const router = new ServerlessRouter([ServerlessRouterWebPlugin]); | ||
router.web | ||
.get('/users/:id', () => | ||
userService.getUserById(event.pathParameters.id)) // returns promise | ||
.delete('/users/:id', () => | ||
userService.deleteUserById(event.pathParameters.id)); // returns promise | ||
router.mismatch(() => { | ||
const { path, httpMethod } = event; | ||
return Promise.reject(new Error(`Unknown route: ${httpMethod} ${path}`)); | ||
}); | ||
router.dispatch(event, context, callback); | ||
return router.dispatch(event); | ||
} | ||
function myLambdaHandler(event, context, callback) { | ||
return dispatch(event) | ||
.then(result => | ||
callback(null, { statusCode: result.code, body: JSON.stringify({ payload: result.payload }) })) | ||
.catch(error => | ||
callback(null, { statusCode: '500', body: JSON.stringify({ message: error.message }) })); | ||
} | ||
``` | ||
@@ -54,0 +53,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
310
138384
18
81