@whook/example
Version:
A basic Whook server
87 lines (71 loc) • 3.22 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _knifecycle = require("knifecycle");
var _getOpenAPI = require("../handlers/getOpenAPI");
var _getPing = require("@whook/whook/dist/handlers/getPing");
var _getDelay = require("../handlers/getDelay");
var _getDiagnostic = require("../handlers/getDiagnostic");
var _getTime = require("../handlers/getTime");
var _putEcho = require("../handlers/putEcho");
var _cors = require("@whook/cors");
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
var _default = (0, _knifecycle.name)('API', (0, _knifecycle.service)(initAPI, "aPI", ["DEBUG_NODE_ENVS", "NODE_ENV", "CONFIG", "HOST", "PORT", "BASE_PATH", "API_VERSION", "log"])); // The API service is where you put your handlers
// altogether to form the final API
exports.default = _default;
async function initAPI({
DEBUG_NODE_ENVS,
NODE_ENV,
CONFIG,
HOST,
PORT,
BASE_PATH,
API_VERSION,
log
}) {
log('debug', '🦄 - Initializing the API service!');
const debugging = DEBUG_NODE_ENVS.includes(NODE_ENV);
const API = {
openapi: '3.0.2',
info: {
version: API_VERSION,
title: CONFIG.name,
description: CONFIG.description
},
servers: [{
url: `http://${CONFIG.host || HOST}${debugging ? `:${PORT}` : ''}${BASE_PATH}`
}],
components: {
securitySchemes: _objectSpread({
bearerAuth: {
description: 'Bearer authentication with a user API token',
type: 'http',
scheme: 'bearer'
}
}, debugging ? {
fakeAuth: {
description: 'A fake authentication for development purpose.',
type: 'apiKey',
in: 'header',
name: 'Authorization'
}
} : {})
},
paths: [_getOpenAPI.definition, _getPing.definition, _getDelay.definition, _getDiagnostic.definition, _getTime.definition, _putEcho.definition].map(definition => debugging && definition.operation.security ? _objectSpread({}, definition, {
operation: _objectSpread({}, definition.operation, {
security: [...definition.operation.security, {
fakeAuth: ['admin']
}]
})
}) : definition).reduce((paths, definition) => _objectSpread({}, paths, {
[definition.path]: _objectSpread({}, paths[definition.path] || {}, {
[definition.method]: definition.operation
})
}), {})
}; // You can apply transformations to your API like
// here for CORS support (OPTIONS method handling)
return (0, _cors.augmentAPIWithCORS)(API);
}