@usirin/spellbook
Version:
Type-safe API surfaces that work across process boundaries
28 lines (27 loc) • 1.33 kB
JavaScript
import * as __WEBPACK_EXTERNAL_MODULE__validate_standard_schema_js_0f6ad7de__ from "./validate-standard-schema.js";
function createSpell(spec) {
const spell = async (parameters, context)=>{
const [validatedParams, validatedContext] = await Promise.all([
(0, __WEBPACK_EXTERNAL_MODULE__validate_standard_schema_js_0f6ad7de__.standardValidate)(spec.parameters, parameters),
(0, __WEBPACK_EXTERNAL_MODULE__validate_standard_schema_js_0f6ad7de__.standardValidate)(spec.context, context)
]);
const result = await spec.execute(validatedParams, validatedContext);
const validatedResult = await (0, __WEBPACK_EXTERNAL_MODULE__validate_standard_schema_js_0f6ad7de__.standardValidate)(spec.result, result);
return validatedResult;
};
spell._spec = spec;
spell._tag = "spell";
return spell;
}
function createSpellbook(spells, context) {
const wrappedSpells = {};
for(const spellName in spells){
const originalSpell = spells[spellName];
const wrappedSpell = async (parameters)=>originalSpell(parameters, context);
wrappedSpell._spec = originalSpell._spec;
wrappedSpell._tag = originalSpell._tag;
wrappedSpells[spellName] = wrappedSpell;
}
return wrappedSpells;
}
export { createSpell, createSpellbook };