itty-router
Advanced tools
Comparing version 4.1.0-next.2 to 4.1.0-next.3
@@ -5,6 +5,6 @@ import { RouteHandler, RouterType } from './Router'; | ||
format?: Function | false; | ||
error?: Function | false; | ||
notFound?: RouteHandler | false; | ||
handleErrors?: Function | false; | ||
handleNotFound?: RouteHandler | false; | ||
cors?: CorsOptions | true; | ||
}; | ||
export declare const flow: (router: RouterType, options?: FlowOptions) => (...args: any[]) => Promise<any>; |
@@ -1,1 +0,1 @@ | ||
"use strict";const e=((e="text/plain; charset=utf-8",t)=>(o,r)=>{const{headers:s={},...n}=r||{};return"Response"===o?.constructor.name?o:new Response(t?t(o):o,{headers:{"content-type":e,...s},...n})})("application/json; charset=utf-8",JSON.stringify),t=e=>({400:"Bad Request",401:"Unauthorized",403:"Forbidden",404:"Not Found",500:"Internal Server Error"}[e]||"Unknown Error"),o=(o=500,r)=>{if(o instanceof Error){const{message:e,...s}=o;o=o.status||500,r={error:e||t(o),...s}}return r={status:o,..."object"==typeof r?r:{error:r||t(o)}},e(r,{status:o})},r=e=>{e.proxy=new Proxy(e.proxy||e,{get:(t,o)=>{let r;return void 0!==(r=t[o])?r.bind?.(e)||r:t?.params?.[o]}})};exports.flow=(t,s={})=>{const{format:n=e,error:c=o,cors:a,notFound:i=(()=>o(404))}=s;let l;return"function"==typeof i&&t.all("*",i),a&&(l=((e={})=>{const{origins:t=["*"],maxAge:o,methods:r=["GET"],headers:s={}}=e;let n;const c={"content-type":"application/json","Access-Control-Allow-Methods":r.join(", "),...s};return o&&(c["Access-Control-Max-Age"]=o),{corsify:e=>{if(!e)throw new Error("No fetch handler responded and no upstream to proxy to specified.");const{headers:t,status:o,body:r}=e;return[101,301,302,308].includes(o)||t.get("access-control-allow-origin")?e:new Response(r,{status:o,headers:{...Object.fromEntries(t),...c,...n,"content-type":t.get("content-type")}})},preflight:e=>{const o=[...new Set(["OPTIONS",...r])],s=e.headers.get("origin")||"";if(n=(t.includes(s)||t.includes("*"))&&{"Access-Control-Allow-Origin":s},"OPTIONS"===e.method){const t={...c,"Access-Control-Allow-Methods":o.join(", "),"Access-Control-Allow-Headers":e.headers.get("Access-Control-Request-Headers"),...n};return new Response(null,{headers:e.headers.get("Origin")&&e.headers.get("Access-Control-Request-Method")&&e.headers.get("Access-Control-Request-Headers")?t:{Allow:o.join(", ")}})}}}})(!0===a?void 0:a),t.routes.unshift(["ALL",/^(.*)?\/*$/,[l.preflight,r],"*"])),async(...e)=>{let o=t.handle(...e);return n&&(o=o.then(n)),c&&(o=o.catch(c)),a&&(o=o.then(l?.corsify)),o}}; | ||
"use strict";const e=((e="text/plain; charset=utf-8",t)=>(o,r)=>{const{headers:s={},...n}=r||{};return"Response"===o?.constructor.name?o:new Response(t?t(o):o,{headers:{"content-type":e,...s},...n})})("application/json; charset=utf-8",JSON.stringify),t=e=>({400:"Bad Request",401:"Unauthorized",403:"Forbidden",404:"Not Found",500:"Internal Server Error"}[e]||"Unknown Error"),o=(o=500,r)=>{if(o instanceof Error){const{message:e,...s}=o;o=o.status||500,r={error:e||t(o),...s}}return r={status:o,..."object"==typeof r?r:{error:r||t(o)}},e(r,{status:o})},r=e=>{e.proxy=new Proxy(e.proxy||e,{get:(t,o)=>{let r;return void 0!==(r=t[o])?r.bind?.(e)||r:t?.params?.[o]}})};exports.flow=(t,s={})=>{const{format:n=e,cors:c,handleErrors:a=o,handleNotFound:l=(()=>o(404))}=s;let i;return"function"==typeof l&&t.all("*",l),c&&(i=((e={})=>{const{origins:t=["*"],maxAge:o,methods:r=["GET"],headers:s={}}=e;let n;const c={"content-type":"application/json","Access-Control-Allow-Methods":r.join(", "),...s};return o&&(c["Access-Control-Max-Age"]=o),{corsify:e=>{if(!e)throw new Error("No fetch handler responded and no upstream to proxy to specified.");const{headers:t,status:o,body:r}=e;return[101,301,302,308].includes(o)||t.get("access-control-allow-origin")?e:new Response(r,{status:o,headers:{...Object.fromEntries(t),...c,...n,"content-type":t.get("content-type")}})},preflight:e=>{const o=[...new Set(["OPTIONS",...r])],s=e.headers.get("origin")||"";if(n=(t.includes(s)||t.includes("*"))&&{"Access-Control-Allow-Origin":s},"OPTIONS"===e.method){const t={...c,"Access-Control-Allow-Methods":o.join(", "),"Access-Control-Allow-Headers":e.headers.get("Access-Control-Request-Headers"),...n};return new Response(null,{headers:e.headers.get("Origin")&&e.headers.get("Access-Control-Request-Method")&&e.headers.get("Access-Control-Request-Headers")?t:{Allow:o.join(", ")}})}}}})(!0===c?void 0:c),t.routes.unshift(["ALL",/^(.*)?\/*$/,[i.preflight,r],"*"])),async(...e)=>{let o=t.handle(...e);return n&&(o=o.then(n)),a&&(o=o.catch(a)),c&&(o=o.then(i?.corsify)),o}}; |
@@ -1,1 +0,1 @@ | ||
"use strict";const e=(e={})=>{const{origins:t=["*"],maxAge:o,methods:r=["GET"],headers:s={}}=e;let n;const a={"content-type":"application/json","Access-Control-Allow-Methods":r.join(", "),...s};o&&(a["Access-Control-Max-Age"]=o);return{corsify:e=>{if(!e)throw new Error("No fetch handler responded and no upstream to proxy to specified.");const{headers:t,status:o,body:r}=e;return[101,301,302,308].includes(o)||t.get("access-control-allow-origin")?e:new Response(r,{status:o,headers:{...Object.fromEntries(t),...a,...n,"content-type":t.get("content-type")}})},preflight:e=>{const o=[...new Set(["OPTIONS",...r])],s=e.headers.get("origin")||"";if(n=(t.includes(s)||t.includes("*"))&&{"Access-Control-Allow-Origin":s},"OPTIONS"===e.method){const t={...a,"Access-Control-Allow-Methods":o.join(", "),"Access-Control-Allow-Headers":e.headers.get("Access-Control-Request-Headers"),...n};return new Response(null,{headers:e.headers.get("Origin")&&e.headers.get("Access-Control-Request-Method")&&e.headers.get("Access-Control-Request-Headers")?t:{Allow:o.join(", ")}})}}}},t=(e="text/plain; charset=utf-8",t)=>(o,r)=>{const{headers:s={},...n}=r||{};return"Response"===o?.constructor.name?o:new Response(t?t(o):o,{headers:{"content-type":e,...s},...n})},o=t("application/json; charset=utf-8",JSON.stringify),r=e=>({400:"Bad Request",401:"Unauthorized",403:"Forbidden",404:"Not Found",500:"Internal Server Error"}[e]||"Unknown Error"),s=(e=500,t)=>{if(e instanceof Error){const{message:o,...s}=e;e=e.status||500,t={error:o||r(e),...s}}return t={status:e,..."object"==typeof t?t:{error:t||r(e)}},o(t,{status:e})},n=e=>{e.proxy=new Proxy(e.proxy||e,{get:(t,o)=>{let r;return void 0!==(r=t[o])?r.bind?.(e)||r:t?.params?.[o]}})};class a extends Error{status;constructor(e=500,t){super("object"==typeof t?t.error:t),"object"==typeof t&&Object.assign(this,t),this.status=e}}const c=t("text/html"),p=t("image/jpeg"),i=t("image/png"),l=t("image/webp");exports.Router=({base:e="",routes:t=[]}={})=>({__proto__:new Proxy({},{get:(o,r,s,n)=>(o,...a)=>t.push([r.toUpperCase(),RegExp(`^${(n=(e+o).replace(/\/+(\/|$)/g,"$1")).replace(/(\/?\.?):(\w+)\+/g,"($1(?<$2>*))").replace(/(\/?\.?):(\w+)/g,"($1(?<$2>[^$1/]+?))").replace(/\./g,"\\.").replace(/(\/?)\*/g,"($1.*)?")}/*$`),a,n])&&s}),routes:t,async handle(e,...o){let r,s,n=new URL(e.url),a=e.query={__proto__:null};for(let[e,t]of n.searchParams)a[e]=void 0===a[e]?t:[a[e],t].flat();for(let[a,c,p,i]of t)if((a===e.method||"ALL"===a)&&(s=n.pathname.match(c))){e.params=s.groups||{},e.route=i;for(let t of p)if(void 0!==(r=await t(e.proxy||e,...o)))return r}}}),exports.StatusError=a,exports.createCors=e,exports.createResponse=t,exports.error=s,exports.flow=(t,r={})=>{const{format:a=o,error:c=s,cors:p,notFound:i=(()=>s(404))}=r;let l;return"function"==typeof i&&t.all("*",i),p&&(l=e(!0===p?void 0:p),t.routes.unshift(["ALL",/^(.*)?\/*$/,[l.preflight,n],"*"])),async(...e)=>{let o=t.handle(...e);return a&&(o=o.then(a)),c&&(o=o.catch(c)),p&&(o=o.then(l?.corsify)),o}},exports.html=c,exports.jpeg=p,exports.json=o,exports.png=i,exports.status=(e,t)=>new Response(null,{...t,status:e}),exports.text=(e,t)=>new Response(String(e),t),exports.webp=l,exports.withContent=async e=>{e.headers.get("content-type")?.includes("json")&&(e.content=await e.json())},exports.withCookies=e=>{e.cookies=(e.headers.get("Cookie")||"").split(/;\s*/).map((e=>e.split(/=(.+)/))).reduce(((e,[t,o])=>o?(e[t]=o,e):e),{})},exports.withParams=n; | ||
"use strict";const e=(e={})=>{const{origins:t=["*"],maxAge:o,methods:r=["GET"],headers:s={}}=e;let n;const a={"content-type":"application/json","Access-Control-Allow-Methods":r.join(", "),...s};o&&(a["Access-Control-Max-Age"]=o);return{corsify:e=>{if(!e)throw new Error("No fetch handler responded and no upstream to proxy to specified.");const{headers:t,status:o,body:r}=e;return[101,301,302,308].includes(o)||t.get("access-control-allow-origin")?e:new Response(r,{status:o,headers:{...Object.fromEntries(t),...a,...n,"content-type":t.get("content-type")}})},preflight:e=>{const o=[...new Set(["OPTIONS",...r])],s=e.headers.get("origin")||"";if(n=(t.includes(s)||t.includes("*"))&&{"Access-Control-Allow-Origin":s},"OPTIONS"===e.method){const t={...a,"Access-Control-Allow-Methods":o.join(", "),"Access-Control-Allow-Headers":e.headers.get("Access-Control-Request-Headers"),...n};return new Response(null,{headers:e.headers.get("Origin")&&e.headers.get("Access-Control-Request-Method")&&e.headers.get("Access-Control-Request-Headers")?t:{Allow:o.join(", ")}})}}}},t=(e="text/plain; charset=utf-8",t)=>(o,r)=>{const{headers:s={},...n}=r||{};return"Response"===o?.constructor.name?o:new Response(t?t(o):o,{headers:{"content-type":e,...s},...n})},o=t("application/json; charset=utf-8",JSON.stringify),r=e=>({400:"Bad Request",401:"Unauthorized",403:"Forbidden",404:"Not Found",500:"Internal Server Error"}[e]||"Unknown Error"),s=(e=500,t)=>{if(e instanceof Error){const{message:o,...s}=e;e=e.status||500,t={error:o||r(e),...s}}return t={status:e,..."object"==typeof t?t:{error:t||r(e)}},o(t,{status:e})},n=e=>{e.proxy=new Proxy(e.proxy||e,{get:(t,o)=>{let r;return void 0!==(r=t[o])?r.bind?.(e)||r:t?.params?.[o]}})};class a extends Error{status;constructor(e=500,t){super("object"==typeof t?t.error:t),"object"==typeof t&&Object.assign(this,t),this.status=e}}const c=t("text/html"),p=t("image/jpeg"),l=t("image/png"),i=t("image/webp");exports.Router=({base:e="",routes:t=[]}={})=>({__proto__:new Proxy({},{get:(o,r,s,n)=>(o,...a)=>t.push([r.toUpperCase(),RegExp(`^${(n=(e+o).replace(/\/+(\/|$)/g,"$1")).replace(/(\/?\.?):(\w+)\+/g,"($1(?<$2>*))").replace(/(\/?\.?):(\w+)/g,"($1(?<$2>[^$1/]+?))").replace(/\./g,"\\.").replace(/(\/?)\*/g,"($1.*)?")}/*$`),a,n])&&s}),routes:t,async handle(e,...o){let r,s,n=new URL(e.url),a=e.query={__proto__:null};for(let[e,t]of n.searchParams)a[e]=void 0===a[e]?t:[a[e],t].flat();for(let[a,c,p,l]of t)if((a===e.method||"ALL"===a)&&(s=n.pathname.match(c))){e.params=s.groups||{},e.route=l;for(let t of p)if(void 0!==(r=await t(e.proxy||e,...o)))return r}}}),exports.StatusError=a,exports.createCors=e,exports.createResponse=t,exports.error=s,exports.flow=(t,r={})=>{const{format:a=o,cors:c,handleErrors:p=s,handleNotFound:l=(()=>s(404))}=r;let i;return"function"==typeof l&&t.all("*",l),c&&(i=e(!0===c?void 0:c),t.routes.unshift(["ALL",/^(.*)?\/*$/,[i.preflight,n],"*"])),async(...e)=>{let o=t.handle(...e);return a&&(o=o.then(a)),p&&(o=o.catch(p)),c&&(o=o.then(i?.corsify)),o}},exports.html=c,exports.jpeg=p,exports.json=o,exports.png=l,exports.status=(e,t)=>new Response(null,{...t,status:e}),exports.text=(e,t)=>new Response(String(e),t),exports.webp=i,exports.withContent=async e=>{e.headers.get("content-type")?.includes("json")&&(e.content=await e.json())},exports.withCookies=e=>{e.cookies=(e.headers.get("Cookie")||"").split(/;\s*/).map((e=>e.split(/=(.+)/))).reduce(((e,[t,o])=>o?(e[t]=o,e):e),{})},exports.withParams=n; |
{ | ||
"name": "itty-router", | ||
"version": "4.1.0-next.2", | ||
"version": "4.1.0-next.3", | ||
"description": "A tiny, zero-dependency router, designed to make beautiful APIs in any environment.", | ||
@@ -5,0 +5,0 @@ "main": "./index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
38923