UNPKG

as-procedure

Version:

easily create procedures within a pit-of-success

35 lines 1.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.withExpectOutkey = void 0; const helpful_errors_1 = require("helpful-errors"); const HasName_1 = require("../domain/HasName"); const withExpectOutkey = (procedure) => { const wrapped = (...args) => { // define an error which has access to the original call.stack, so we can trace the stack to the root caller, since promises loose their chain const errorFromOriginalCall = new helpful_errors_1.HelpfulError('expect.outkey: call', { procedure: (0, HasName_1.getName)(procedure), input: args[0], }); // invoke the request const promise = procedure(...args); // add the expect wrapper const expect = async (key) => { const output = await promise; const outputOfKey = output[key]; if (!outputOfKey) throw new helpful_errors_1.HelpfulError('expect.outkey: breach: key is not present in output', { cause: errorFromOriginalCall, procedure: (0, HasName_1.getName)(procedure), key, output, }); const assured = { [key]: outputOfKey }; return assured; }; const extended = Object.assign(promise, { expect }); return extended; }; return wrapped; }; exports.withExpectOutkey = withExpectOutkey; //# sourceMappingURL=withExpectOutkey.js.map