serverless-offline
Version:
Emulate AWS λ and API Gateway locally when developing your Serverless project
64 lines (54 loc) • 2.1 kB
JavaScript
;
const sinon = require('sinon');
const functionHelper = require('../../src/functionHelper');
const Offline = require('../../src/index');
const ServiceBuilder = require('./ServerlessBuilder');
function createHandler(handlers) {
return (funOptions) => handlers[funOptions.handlerPath.split('/')[1]][funOptions.handlerName];
}
module.exports = class OffLineBuilder {
constructor(serviceBuilder, options) {
this.serviceBuilder = serviceBuilder || new ServiceBuilder();
this.handlers = {};
// Avoid already wrapped exception when offline is instanciated many times
// Problem if test are instanciated serveral times
// FIXME, we could refactor index to have an handlerFactory and just instanciate offline with a factory test stub
if (functionHelper.createHandler.restore) {
functionHelper.createHandler.restore();
}
this.options = options || {};
}
addFunctionConfig(functionName, functionConfig, handler) {
this.serviceBuilder.addFunction(functionName, functionConfig);
const funOptions = functionHelper.getFunctionOptions(functionConfig, functionName, '.');
const handlerPath = funOptions.handlerPath.split('/')[1];
this.handlers[handlerPath] = this.constructor.getFunctionIndex(funOptions.handlerName, handler);
return this;
}
addFunctionHTTP(functionName, http, handler) {
return this.addFunctionConfig(functionName, {
handler: `handler.${functionName}`,
events: [{
http,
}],
}, handler);
}
static getFunctionIndex(handlerName, handler) {
const functionIndex = {};
functionIndex[handlerName] = handler;
return functionIndex;
}
toObject() {
const offline = new Offline(this.serviceBuilder.toObject(), this.options);
sinon.stub(functionHelper, 'createHandler', createHandler(this.handlers));
sinon.stub(offline, 'printBlankLine');
this.server = offline._buildServer();
Object.assign(this.server, {
restore: this.restore,
});
return this.server;
}
static restore() {
functionHelper.createHandler.restore();
}
};