UNPKG

nats-micro

Version:

NATS micro compatible extra-lightweight microservice library

81 lines 4 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.wrapMethodSafe = void 0; const zod_1 = require("zod"); // eslint-disable-next-line import/no-cycle const callHandler_js_1 = require("./callHandler.js"); const misc_js_1 = require("./misc.js"); const threadContext_js_1 = require("./threadContext.js"); const debug_js_1 = require("../debug.js"); function wrapMethodSafe(broker, callback, handlerInfo) { return (msg, subject) => __awaiter(this, void 0, void 0, function* () { var _a; try { const store = new Map(); threadContext_js_1.threadContext.enterWith(store); let input = msg.data; if (handlerInfo.methodConfig.request) { // eslint-disable-next-line @typescript-eslint/no-explicit-any if (handlerInfo.methodConfig.request._def.typeName === 'ZodVoid') // eslint-disable-next-line @typescript-eslint/no-explicit-any input = undefined; else try { input = handlerInfo.methodConfig.request.parse(input); } catch (err) { if (err instanceof zod_1.ZodError) { throw new Error(`Error parsing request fields: ${err.issues.map((i) => `${i.path.join('.')} (${i.message})`).join(', ')}`); } throw err; } } (0, threadContext_js_1.addContextHeadersToThreadContext)(msg.headers); debug_js_1.debug.ms.thread.debug(`Executing safe ${handlerInfo.method}(${JSON.stringify(msg.data)})`); const output = yield (0, callHandler_js_1.callHandler)(callback, input, subject, (_a = msg.headers) !== null && _a !== void 0 ? _a : [], handlerInfo); if ((typeof (output.data) !== 'symbol') && 'replyTo' in msg && msg.replyTo) { if (handlerInfo.methodConfig.response) { try { output.data = handlerInfo.methodConfig.response.parse(output.data); } catch (err) { if (err instanceof zod_1.ZodError) throw new Error(`Invalid response type: ${err.issues.map((i) => i.message).join(',')}`); else throw err; } } broker.send(msg.replyTo, output.data, { headers: output.headers, }); } } catch (err) { const error = (0, misc_js_1.errorToString)(err); debug_js_1.debug.error(error); if ('replyTo' in msg && msg.replyTo) { const headers = [ ...((typeof (err) === 'object') && err && ('status' in err) ? [['X-Error-Status', String(err.status)]] : []), ['X-Error-Message', error], ]; broker.send(msg.replyTo, 'ERROR', { headers, }); } // throw err; } }); } exports.wrapMethodSafe = wrapMethodSafe; //# sourceMappingURL=wrapMethodSafe.js.map