as-procedure
Version:
easily create procedures within a pit-of-success
35 lines • 1.49 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.withExpectOutkey = void 0;
const error_fns_1 = require("@ehmpathy/error-fns");
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 error_fns_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 error_fns_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