test-openapi
Version:
Automated client requests
107 lines (75 loc) • 2.2 kB
JavaScript
;require("core-js/modules/es.array.flat-map");require("core-js/modules/es.array.unscopables.flat-map");Object.defineProperty(exports,"__esModule",{value:true});exports.runHandlers=void 0;var _reduce=require("../utils/reduce.js");
var _handler=require("../errors/handler.js");
const runHandlers=function({
type,
plugins,
input,
context,
errorHandler,
stopFunc,
mergeReturn=defaultMergeReturn})
{
const contextA=getContext({context,plugins});
const handlers=getHandlers({
plugins,
type,
errorHandler,
context:contextA});
return(0,_reduce.reduceAsync)(handlers,runHandler,{
prevVal:input,
secondMapFunc:mergeReturn,
stopFunc});
};exports.runHandlers=runHandlers;
const getContext=function({context,plugins}){
const pluginNames=plugins.map(({name})=>name);
return{...context,pluginNames,_plugins:plugins};
};
const getHandlers=function({plugins,type,errorHandler,context}){
return plugins.
flatMap(plugin=>getPluginHandlers({plugin,type})).
map(handler=>wrapHandler({handler,errorHandler,context}));
};
const getPluginHandlers=function({plugin,plugin:{name},type}){
const handlers=plugin[type];
if(handlers===undefined){
return[];
}
const handlersA=Array.isArray(handlers)?handlers:[handlers];
const handlersB=handlersA.map(func=>({func,name}));
return handlersB;
};
const wrapHandler=function({
handler:{func,name},
errorHandler,
context})
{
const handlerA=callHandler.bind(null,{func,context});
const handlerB=(0,_handler.addErrorHandler)(
handlerA,
pluginErrorHandler.bind(null,name));
const handlerC=wrapErrorHandler({handler:handlerB,errorHandler});
return handlerC;
};
const callHandler=function({func,context},input){
return func(input,context);
};
const pluginErrorHandler=function(name,error){
if(error.module===undefined){
error.module=`plugin-${name}`;
}
throw error;
};
const wrapErrorHandler=function({handler,errorHandler}){
if(errorHandler===undefined){
return handler;
}
const handlerA=(0,_handler.addErrorHandler)(handler,errorHandler);
return handlerA;
};
const runHandler=function(input,handler){
return handler(input);
};
const defaultMergeReturn=function(input,newInput){
return{...input,...newInput};
};
//# sourceMappingURL=handlers.js.map