UNPKG

@kitmi/jacaranda

Version:

JavaScript application framework

75 lines (74 loc) 2.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "default", { enumerable: true, get: function() { return _default; } }); const _Feature = /*#__PURE__*/ _interop_require_default(require("../Feature")); const _nodehttp = /*#__PURE__*/ _interop_require_default(require("node:http")); const _utils = require("@kitmi/utils"); function _interop_require_default(obj) { return obj && obj.__esModule ? obj : { default: obj }; } const statusToError = { 400: 'invalid_request', 401: 'unauthenticated', 403: 'permission_denied', 404: 'resource_not_found' }; const unknownError = 'unknown_error'; function removeReservedMeta(info) { return _utils._.reduce(info, (result, value, key)=>{ if (key[0] === '$') return result; result[key] = value; return result; }, {}); } const _default = { /** * This feature is loaded at init stage * @member {string} */ stage: _Feature.default.SERVICE, /** * Load the feature * @param {App} app - The cli app module object * @param {object} settings - Settings of soal client * @property {boolean} [settings.debug] - Debug mode * @property {string} [settings.dataField="data"] - Data field name * @returns {Promise.<*>} */ load_: async function(app, settings, name) { const service = { wrapResult: (ctx, data = null, others)=>{ return { status: 'ok', ...ctx.resPayload, ...others, [settings?.dataField || 'data']: data }; }, wrapError: (ctx, error, others)=>{ const code = error.code || statusToError[ctx.status] || unknownError; return { status: 'error', ...ctx.resPayload, ...others, error: { code, message: settings?.debug || error.expose ? error.message : _nodehttp.default.STATUS_CODES[ctx.status], ...error.info ? { info: removeReservedMeta(error.info) } : null } }; } }; app.registerService(name, service); } }; //# sourceMappingURL=apiWrapper.js.map