post-get-service
Version:
simple way to create node http in memory server
36 lines (27 loc) • 917 B
JavaScript
// @ts-check
const fastify = require('fastify');
const cors = require('@fastify/cors');
function require_module_from_string(src, filename = '') {
const Module = module.constructor;
// @ts-ignore
const mod = new Module();
mod._compile(src, filename);
return mod.exports;
}
const { set_authorization_middleware } = require('./authorization');
const { get_routing_model } = require('../routing_templates');
module.exports = function (model) {
const { debug } = model;
const app = fastify({ logger: Boolean(debug) });
app.register(cors);
const router_plugin = require_module_from_string(get_routing_model(model));
// if fake server model contains authorization property
// next execution will register a fastify preHandler hook
// that mirrors koa-bearer-token behavior
// @ts-ignore
set_authorization_middleware(app, model);
app.register(router_plugin);
return {
app,
};
};