express-http-client
Version:
middleware style interceptor; fetch based 0 dependancy imperative light weight http handler.
2 lines (1 loc) • 3.7 kB
JavaScript
async function e(e,t){let s=0;return await new Promise(((r,o)=>{const n=()=>{r(e.request)},c=async i=>{if(i)return i instanceof Error?(console.error(i.message),void o(i)):(console.error(i),void o(new Error(i)));const a=t[s++];if(a)try{await a(e,c,n)}catch(e){o(e)}else r(e.request)};c()}))}async function t(e,t){let s=0;return await new Promise(((r,o)=>{const n=()=>{r(e.response)},c=async o=>{o&&(o instanceof Error?(console.error(o.message),r(new Response(o.message,{status:424}))):(console.error(o),r(new Response(o,{status:424}))));const i=t[s++];if(i)try{await i(e,c,n)}catch(e){r(new Response(e,{status:424}))}else r(e.response)};c()}))}class s{#e=[];#t=[];#s="";constructor(e="",t=[],s=[]){this.#s=e,this.#e=t,this.#t=s}async send(s,r){let o={request:new Request(this.#s+s,r),store:new Map};const n=await e(o,this.#e);if(n instanceof Error)return new Response(n.message,{status:424});let c;o.requestTime=Date.now();try{c=await fetch(n)}catch(e){c=new Response(e,{status:424})}return o.responseTime=Date.now(),o.response=c,await t(o,this.#t)}}class r{constructor(){this.requestInterceptors=[],this.responseInterceptors=[]}addRequestInterceptor(...e){return e.forEach((e=>{if("function"!=typeof e)throw new Error("Request interceptor must be a function");this.requestInterceptors.push(e)})),this}addResponseInterceptor(...e){return e.forEach((e=>{if("function"!=typeof e)throw new Error("Response interceptor must be a function");this.responseInterceptors.push(e)})),this}create(e=""){return new s(e,this.requestInterceptors,this.responseInterceptors)}}const o=function(){return new r},n=async function(...s){let r="",o={},n=[],c=[];if(0===s.length)throw new Error("URL is required");if(1===s.length)r=s[0];else if(2===s.length)r=s[0],"object"==typeof s[1]?o=s[1]:"function"==typeof s[1]?c.push(s[1]):"array"==typeof s[1]&&c.push(...s[1]);else if(3===s.length)r=s[0],"object"==typeof s[1]?o=s[1]:"function"==typeof s[1]?c.push(s[1]):"array"==typeof s[1]&&c.push(...s[1]),"function"==typeof s[2]?n.push(s[2]):"array"==typeof s[2]&&n.push(...s[2]);else{if(4!==s.length)throw new Error(`4 arguments required, but ${s.length} are supplied.`);r=s[0],o=s[1],n=s[2],c=s[3]}let i={request:new Request(r,o)};const a=await e(i,c);if(a instanceof Error)return new Response(a.message,{status:424});let u;try{u=await fetch(a)}catch(e){u=new Response(e,{status:424})}return i.response=u,await t(i,n)},c=function(){return async function(e,t,s){const{status:r}=e.response,{method:o,url:n}=e.request,c=e.responseTime-e.requestTime,i=new Date(e.responseTime).toISOString();if("undefined"!=typeof window&&void 0!==window.document){let e="color: green; font-weight: bold;";r>=400?e="color: red; font-weight: bold;":r>=300&&(e="color: orange; font-weight: bold;");let t="color: cyan; font-weight: bold;";"POST"===o&&(t="color: magenta; font-weight: bold;"),"DELETE"===o&&(t="color: red; font-weight: bold;");let s="color: green;";c>=1e3?s="color: red;":c>=100&&(s="color: orange;"),console.log(`[${i}] %c${r}%c %c${o.padEnd(7)}%c ${n.padEnd(30)} %c${c}ms`,e,"color: inherit;",t,"color: inherit;",s)}else{let e="[32m";r>=400?e="[31m":r>=300&&(e="[33m");let t="[36m";"POST"===o&&(t="[35m"),"DELETE"===o&&(t="[31m");const s="[0m",a=c<100?`[32m${c}ms[0m`:c<1e3?`[33m${c}ms[0m`:`[31m${c}ms[0m`;console.log(`[${i}] ${e}${r}${s} ${t}${o.padEnd(7)}${s} ${n.padEnd(30)} ${a}`)}t()}},i=function(e=!0,t={},s=()=>new Response("Resource Not Found",{status:404})){return(r,o)=>{const n=r.request.url,c=r.request.method;return e&&(t[n]&&t[n][c]?r.response=t[n][c](r.request):t[n]&&t[n].ALL?r.response=t[n].ALL(r.request):r.response=s(r.request)),o()}};export{o as httpClient,c as logger,i as mockResponse,n as request};