-
Notifications
You must be signed in to change notification settings - Fork 1
Open
Description
Apparently the docs are really not detailed enough so they could use an update.
Also, if using serveless-offline I needed to expose the reset and use it in the prod handler. I have this code in main handlers.js
const lambda = require('lambda-chain');
const routes = require('./lib/routes');
const { FileEventHandler } = require('./lib/events/file');
const { bodyParser } = require('./lib/utils');
// TODO: Special for serverless-offline, it tries to destroy the node cache
// but the handler factory is a single instance, cached or not.
// Ref: https://goo.gl/54NkoK
lambda.reset();
// Register route handlers
lambda.register(routes);
// Register file event handler
lambda.register({ file: FileEventHandler }, {
before: bodyParser.parseRequest(),
after: bodyParser.toJson,
});
module.exports = lambda.exports();So apparently this link has the problem.
Big thing to document is how the event handler stuff works such as this example node 6 compatible implementation.
const _ = require('lodash');
const { HttpEventHandler } = require('lambda-chain');
const TransformFactory = require('./transform-factory');
const { respond } = require('../../utils');
// Implements the file handler logic for the file http route / event
class FileEventHandler extends HttpEventHandler {
constructor(event, context) {
super(event, context);
this._supportedActions = ['transform'];
}
_isSupported(action) {
return _.includes(this._supportedActions, action);
}
_getTransformer(filename, contentType, options) {
if (filename) return TransformFactory.fromFilename(filename, options);
if (contentType) return TransformFactory.fromContentType(contentType, options);
throw new Error('filename or contentType not provided');
}
post() {
const { action, options } = this.event.body;
return this._isSupported(action) ? this[action](JSON.parse(options || null)) : this.throwUnimplemented(action);
}
// Transform some content in the body
transform(options) {
// Ensure whatever is coming back is a promise for consistency accross transformations
return new Promise((resolve) => {
const { file, filename, contentType } = this.event.body;
if (!file) throw new Error('No `file` property to transform');
const transform = this._getTransformer(filename, contentType, options);
resolve(transform.process(file));
}).then(result => respond(200, {
file: {
data: result,
},
}))
.catch(err => respond(422, { message: err.message }));
}
}
module.exports = { FileEventHandler };Metadata
Metadata
Assignees
Labels
No labels