itty-router 4.1.0-next.2 → 4.1.0-next.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/flow.d.ts +2 -2
- package/flow.js +1 -1
- package/flow.mjs +1 -1
- package/index.js +1 -1
- package/index.mjs +1 -1
- package/package.json +1 -1
package/flow.d.ts
CHANGED
|
@@ -2,8 +2,8 @@ import { RouteHandler, RouterType } from './Router';
|
|
|
2
2
|
import { CorsOptions } from './createCors';
|
|
3
3
|
export type FlowOptions = {
|
|
4
4
|
format?: Function | false;
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
handleErrors?: Function | false;
|
|
6
|
+
handleNotFound?: RouteHandler | false;
|
|
7
7
|
cors?: CorsOptions | true;
|
|
8
8
|
};
|
|
9
9
|
export declare const flow: (router: RouterType, options?: FlowOptions) => (...args: any[]) => Promise<any>;
|
package/flow.js
CHANGED
|
@@ -1 +1 @@
|
|
|
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,
|
|
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,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}};
|
package/flow.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
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]}})},s=(t,s={})=>{const{format:n=e,
|
|
1
|
+
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]}})},s=(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}};export{s as flow};
|
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
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"),
|
|
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"),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;
|
package/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
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]}})},a=(t,r={})=>{const{format:a=o,
|
|
1
|
+
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]}})},a=(t,r={})=>{const{format:a=o,cors:c,handleErrors:l=s,handleNotFound:i=(()=>s(404))}=r;let p;return"function"==typeof i&&t.all("*",i),c&&(p=e(!0===c?void 0:c),t.routes.unshift(["ALL",/^(.*)?\/*$/,[p.preflight,n],"*"])),async(...e)=>{let o=t.handle(...e);return a&&(o=o.then(a)),l&&(o=o.catch(l)),c&&(o=o.then(p?.corsify)),o}},c=({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,l,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 l)if(void 0!==(r=await t(e.proxy||e,...o)))return r}}});class l 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 i=(e,t)=>new Response(null,{...t,status:e}),p=(e,t)=>new Response(String(e),t),d=t("text/html"),u=t("image/jpeg"),h=t("image/png"),g=t("image/webp"),f=async e=>{e.headers.get("content-type")?.includes("json")&&(e.content=await e.json())},w=e=>{e.cookies=(e.headers.get("Cookie")||"").split(/;\s*/).map((e=>e.split(/=(.+)/))).reduce(((e,[t,o])=>o?(e[t]=o,e):e),{})};export{c as Router,l as StatusError,e as createCors,t as createResponse,s as error,a as flow,d as html,u as jpeg,o as json,h as png,i as status,p as text,g as webp,f as withContent,w as withCookies,n as withParams};
|
package/package.json
CHANGED