itty-router 4.0.22 → 4.0.24

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/README.md CHANGED
@@ -1,10 +1,9 @@
1
1
  <p align="center">
2
2
  <a href="https://itty.dev/itty-router">
3
- <img src="https://github.com/kwhitley/itty-router/assets/865416/7751dac0-2dc8-4754-8b39-d08fc2144b4f" alt="Itty Router" />
3
+ <img src="https://github.com/kwhitley/itty-router/assets/865416/15a90b05-344d-4135-a3f8-52a4d117250e" alt="Itty Router" />
4
4
  </a>
5
5
  <p>
6
-
7
- ---
6
+
8
7
  <h2 align="center"><a href="https://itty.dev/itty-router">v4.x Documentation @ itty.dev</a>
9
8
  <br /></h2>
10
9
 
package/createCors.d.ts CHANGED
@@ -1,10 +1,10 @@
1
1
  import { IRequest } from './Router';
2
- export interface CorsOptions {
3
- origins?: string[];
2
+ export type CorsOptions = {
3
+ origins?: string[] | ((origin: string) => boolean);
4
4
  maxAge?: number;
5
5
  methods?: string[];
6
6
  headers?: any;
7
- }
7
+ };
8
8
  export declare const createCors: (options?: CorsOptions) => {
9
9
  corsify: (response: Response) => Response;
10
10
  preflight: (r: IRequest) => Response | undefined;
package/createCors.js CHANGED
@@ -1 +1 @@
1
- "use strict";exports.createCors=(e={})=>{const{origins:s=["*"],maxAge:o,methods:t=["GET"],headers:n={}}=e;let r;const c={"content-type":"application/json","Access-Control-Allow-Methods":t.join(", "),...n};o&&(c["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:s,status:o,body:t}=e;return[101,301,302,308].includes(o)||s.get("access-control-allow-origin")?e:new Response(t,{status:o,headers:{...Object.fromEntries(s),...c,...r,"content-type":s.get("content-type")}})},preflight:e=>{const o=[...new Set(["OPTIONS",...t])],n=e.headers.get("origin")||"";if(r=(s.includes(n)||s.includes("*"))&&{"Access-Control-Allow-Origin":n},"OPTIONS"===e.method){const s={...c,"Access-Control-Allow-Methods":o.join(", "),"Access-Control-Allow-Headers":e.headers.get("Access-Control-Request-Headers"),...r};return new Response(null,{headers:e.headers.get("Origin")&&e.headers.get("Access-Control-Request-Method")&&e.headers.get("Access-Control-Request-Headers")?s:{Allow:o.join(", ")}})}}}};
1
+ "use strict";exports.createCors=(e={})=>{const{origins:s=["*"],maxAge:o,methods:t=["GET"],headers:n={}}=e;let r;const c="function"==typeof s?s:e=>s.includes(e)||s.includes("*"),l={"content-type":"application/json","Access-Control-Allow-Methods":t.join(", "),...n};o&&(l["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:s,status:o,body:t}=e;return[101,301,302,308].includes(o)||s.get("access-control-allow-origin")?e:new Response(t,{status:o,headers:{...Object.fromEntries(s),...l,...r,"content-type":s.get("content-type")}})},preflight:e=>{const s=[...new Set(["OPTIONS",...t])],o=e.headers.get("origin")||"";if(r=c(o)&&{"Access-Control-Allow-Origin":o},"OPTIONS"===e.method){const o={...l,"Access-Control-Allow-Methods":s.join(", "),"Access-Control-Allow-Headers":e.headers.get("Access-Control-Request-Headers"),...r};return new Response(null,{headers:e.headers.get("Origin")&&e.headers.get("Access-Control-Request-Method")&&e.headers.get("Access-Control-Request-Headers")?o:{Allow:s.join(", ")}})}}}};
package/createCors.mjs CHANGED
@@ -1 +1 @@
1
- const e=(e={})=>{const{origins:o=["*"],maxAge:s,methods:t=["GET"],headers:n={}}=e;let r;const c={"content-type":"application/json","Access-Control-Allow-Methods":t.join(", "),...n};s&&(c["Access-Control-Max-Age"]=s);return{corsify:e=>{if(!e)throw new Error("No fetch handler responded and no upstream to proxy to specified.");const{headers:o,status:s,body:t}=e;return[101,301,302,308].includes(s)||o.get("access-control-allow-origin")?e:new Response(t,{status:s,headers:{...Object.fromEntries(o),...c,...r,"content-type":o.get("content-type")}})},preflight:e=>{const s=[...new Set(["OPTIONS",...t])],n=e.headers.get("origin")||"";if(r=(o.includes(n)||o.includes("*"))&&{"Access-Control-Allow-Origin":n},"OPTIONS"===e.method){const o={...c,"Access-Control-Allow-Methods":s.join(", "),"Access-Control-Allow-Headers":e.headers.get("Access-Control-Request-Headers"),...r};return new Response(null,{headers:e.headers.get("Origin")&&e.headers.get("Access-Control-Request-Method")&&e.headers.get("Access-Control-Request-Headers")?o:{Allow:s.join(", ")}})}}}};export{e as createCors};
1
+ const e=(e={})=>{const{origins:o=["*"],maxAge:s,methods:t=["GET"],headers:n={}}=e;let r;const c="function"==typeof o?o:e=>o.includes(e)||o.includes("*"),l={"content-type":"application/json","Access-Control-Allow-Methods":t.join(", "),...n};s&&(l["Access-Control-Max-Age"]=s);return{corsify:e=>{if(!e)throw new Error("No fetch handler responded and no upstream to proxy to specified.");const{headers:o,status:s,body:t}=e;return[101,301,302,308].includes(s)||o.get("access-control-allow-origin")?e:new Response(t,{status:s,headers:{...Object.fromEntries(o),...l,...r,"content-type":o.get("content-type")}})},preflight:e=>{const o=[...new Set(["OPTIONS",...t])],s=e.headers.get("origin")||"";if(r=c(s)&&{"Access-Control-Allow-Origin":s},"OPTIONS"===e.method){const s={...l,"Access-Control-Allow-Methods":o.join(", "),"Access-Control-Allow-Headers":e.headers.get("Access-Control-Request-Headers"),...r};return new Response(null,{headers:e.headers.get("Origin")&&e.headers.get("Access-Control-Request-Method")&&e.headers.get("Access-Control-Request-Headers")?s:{Allow:o.join(", ")}})}}}};export{e as createCors};
package/createResponse.js CHANGED
@@ -1 +1 @@
1
- "use strict";exports.createResponse=(e="text/plain; charset=utf-8",t)=>(s,n)=>{const{headers:r={},...o}=n||{};return"Response"===s?.constructor.name?s:new Response(t?t(s):s,{headers:{"content-type":e,...r},...o})};
1
+ "use strict";exports.createResponse=(e="text/plain; charset=utf-8",s)=>(t,{headers:n={},...o}={})=>void 0===t||"Response"===t?.constructor.name?t:new Response(s?s(t):t,{headers:{"content-type":e,...n},...o});
@@ -1 +1 @@
1
- const e=(e="text/plain; charset=utf-8",t)=>(n,s)=>{const{headers:o={},...r}=s||{};return"Response"===n?.constructor.name?n:new Response(t?t(n):n,{headers:{"content-type":e,...o},...r})};export{e as createResponse};
1
+ const e=(e="text/plain; charset=utf-8",t)=>(n,{headers:s={},...o}={})=>void 0===n||"Response"===n?.constructor.name?n:new Response(t?t(n):n,{headers:{"content-type":e,...s},...o});export{e as createResponse};
package/error.js CHANGED
@@ -1 +1 @@
1
- "use strict";const r=((r="text/plain; charset=utf-8",e)=>(t,s)=>{const{headers:n={},...o}=s||{};return"Response"===t?.constructor.name?t:new Response(e?e(t):t,{headers:{"content-type":r,...n},...o})})("application/json; charset=utf-8",JSON.stringify),e=r=>({400:"Bad Request",401:"Unauthorized",403:"Forbidden",404:"Not Found",500:"Internal Server Error"}[r]||"Unknown Error");exports.error=(t=500,s)=>{if(t instanceof Error){const{message:r,...n}=t;t=t.status||500,s={error:r||e(t),...n}}return s={status:t,..."object"==typeof s?s:{error:s||e(t)}},r(s,{status:t})};
1
+ "use strict";const r=((r="text/plain; charset=utf-8",e)=>(t,{headers:s={},...o}={})=>void 0===t||"Response"===t?.constructor.name?t:new Response(e?e(t):t,{headers:{"content-type":r,...s},...o}))("application/json; charset=utf-8",JSON.stringify),e=r=>({400:"Bad Request",401:"Unauthorized",403:"Forbidden",404:"Not Found",500:"Internal Server Error"}[r]||"Unknown Error");exports.error=(t=500,s)=>{if(t instanceof Error){const{message:r,...o}=t;t=t.status||500,s={error:r||e(t),...o}}return s={status:t,..."object"==typeof s?s:{error:s||e(t)}},r(s,{status:t})};
package/error.mjs CHANGED
@@ -1 +1 @@
1
- const e=((e="text/plain; charset=utf-8",r)=>(t,n)=>{const{headers:o={},...s}=n||{};return"Response"===t?.constructor.name?t:new Response(r?r(t):t,{headers:{"content-type":e,...o},...s})})("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"),t=(t=500,n)=>{if(t instanceof Error){const{message:e,...o}=t;t=t.status||500,n={error:e||r(t),...o}}return n={status:t,..."object"==typeof n?n:{error:n||r(t)}},e(n,{status:t})};export{t as error};
1
+ const e=((e="text/plain; charset=utf-8",r)=>(t,{headers:n={},...o}={})=>void 0===t||"Response"===t?.constructor.name?t:new Response(r?r(t):t,{headers:{"content-type":e,...n},...o}))("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"),t=(t=500,n)=>{if(t instanceof Error){const{message:e,...o}=t;t=t.status||500,n={error:e||r(t),...o}}return n={status:t,..."object"==typeof n?n:{error:n||r(t)}},e(n,{status:t})};export{t as error};
package/html.js CHANGED
@@ -1 +1 @@
1
- "use strict";const t=((t="text/plain; charset=utf-8",e)=>(s,n)=>{const{headers:r={},...o}=n||{};return"Response"===s?.constructor.name?s:new Response(e?e(s):s,{headers:{"content-type":t,...r},...o})})("text/html");exports.html=t;
1
+ "use strict";const e=((e="text/plain; charset=utf-8",t)=>(s,{headers:n={},...o}={})=>void 0===s||"Response"===s?.constructor.name?s:new Response(t?t(s):s,{headers:{"content-type":e,...n},...o}))("text/html");exports.html=e;
package/html.mjs CHANGED
@@ -1 +1 @@
1
- const e=((e="text/plain; charset=utf-8",t)=>(n,s)=>{const{headers:o={},...r}=s||{};return"Response"===n?.constructor.name?n:new Response(t?t(n):n,{headers:{"content-type":e,...o},...r})})("text/html");export{e as html};
1
+ const e=((e="text/plain; charset=utf-8",t)=>(n,{headers:s={},...o}={})=>void 0===n||"Response"===n?.constructor.name?n:new Response(t?t(n):n,{headers:{"content-type":e,...s},...o}))("text/html");export{e as html};
package/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";class e 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 t=(e="text/plain; charset=utf-8",t)=>(s,o)=>{const{headers:r={},...n}=o||{};return"Response"===s?.constructor.name?s:new Response(t?t(s):s,{headers:{"content-type":e,...r},...n})},s=t("application/json; charset=utf-8",JSON.stringify),o=e=>({400:"Bad Request",401:"Unauthorized",403:"Forbidden",404:"Not Found",500:"Internal Server Error"}[e]||"Unknown Error"),r=t("text/plain; charset=utf-8",String),n=t("text/html"),a=t("image/jpeg"),c=t("image/png"),p=t("image/webp");exports.Router=({base:e="",routes:t=[]}={})=>({__proto__:new Proxy({},{get:(s,o,r,n)=>(s,...a)=>t.push([o.toUpperCase(),RegExp(`^${(n=(e+s).replace(/\/+(\/|$)/g,"$1")).replace(/(\/?\.?):(\w+)\+/g,"($1(?<$2>*))").replace(/(\/?\.?):(\w+)/g,"($1(?<$2>[^$1/]+?))").replace(/\./g,"\\.").replace(/(\/?)\*/g,"($1.*)?")}/*$`),a,n])&&r}),routes:t,async handle(e,...s){let o,r,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)&&(r=n.pathname.match(c))){e.params=r.groups||{},e.route=i;for(let t of p)if(void 0!==(o=await t(e.proxy||e,...s)))return o}}}),exports.StatusError=e,exports.createCors=(e={})=>{const{origins:t=["*"],maxAge:s,methods:o=["GET"],headers:r={}}=e;let n;const a={"content-type":"application/json","Access-Control-Allow-Methods":o.join(", "),...r};s&&(a["Access-Control-Max-Age"]=s);return{corsify:e=>{if(!e)throw new Error("No fetch handler responded and no upstream to proxy to specified.");const{headers:t,status:s,body:o}=e;return[101,301,302,308].includes(s)||t.get("access-control-allow-origin")?e:new Response(o,{status:s,headers:{...Object.fromEntries(t),...a,...n,"content-type":t.get("content-type")}})},preflight:e=>{const s=[...new Set(["OPTIONS",...o])],r=e.headers.get("origin")||"";if(n=(t.includes(r)||t.includes("*"))&&{"Access-Control-Allow-Origin":r},"OPTIONS"===e.method){const t={...a,"Access-Control-Allow-Methods":s.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:s.join(", ")}})}}}},exports.createResponse=t,exports.error=(e=500,t)=>{if(e instanceof Error){const{message:s,...r}=e;e=e.status||500,t={error:s||o(e),...r}}return t={status:e,..."object"==typeof t?t:{error:t||o(e)}},s(t,{status:e})},exports.html=n,exports.jpeg=a,exports.json=s,exports.png=c,exports.status=(e,t)=>new Response(null,{...t,status:e}),exports.text=r,exports.webp=p,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,s])=>s?(e[t]=s,e):e),{})},exports.withParams=e=>{e.proxy=new Proxy(e.proxy||e,{get:(t,s)=>{let o;return void 0!==(o=t[s])?o.bind?.(e)||o:t?.params?.[s]}})};
1
+ "use strict";class e 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 t=(e="text/plain; charset=utf-8",t)=>(o,{headers:s={},...r}={})=>void 0===o||"Response"===o?.constructor.name?o:new Response(t?t(o):o,{headers:{"content-type":e,...s},...r}),o=t("application/json; charset=utf-8",JSON.stringify),s=e=>({400:"Bad Request",401:"Unauthorized",403:"Forbidden",404:"Not Found",500:"Internal Server Error"}[e]||"Unknown Error"),r=t("text/plain; charset=utf-8",String),n=t("text/html"),a=t("image/jpeg"),c=t("image/png"),p=t("image/webp");exports.Router=({base:e="",routes:t=[]}={})=>({__proto__:new Proxy({},{get:(o,s,r,n)=>(o,...a)=>t.push([s.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])&&r}),routes:t,async handle(e,...o){let s,r,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)&&(r=n.pathname.match(c))){e.params=r.groups||{},e.route=i;for(let t of p)if(void 0!==(s=await t(e.proxy||e,...o)))return s}}}),exports.StatusError=e,exports.createCors=(e={})=>{const{origins:t=["*"],maxAge:o,methods:s=["GET"],headers:r={}}=e;let n;const a="function"==typeof t?t:e=>t.includes(e)||t.includes("*"),c={"content-type":"application/json","Access-Control-Allow-Methods":s.join(", "),...r};o&&(c["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:s}=e;return[101,301,302,308].includes(o)||t.get("access-control-allow-origin")?e:new Response(s,{status:o,headers:{...Object.fromEntries(t),...c,...n,"content-type":t.get("content-type")}})},preflight:e=>{const t=[...new Set(["OPTIONS",...s])],o=e.headers.get("origin")||"";if(n=a(o)&&{"Access-Control-Allow-Origin":o},"OPTIONS"===e.method){const o={...c,"Access-Control-Allow-Methods":t.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")?o:{Allow:t.join(", ")}})}}}},exports.createResponse=t,exports.error=(e=500,t)=>{if(e instanceof Error){const{message:o,...r}=e;e=e.status||500,t={error:o||s(e),...r}}return t={status:e,..."object"==typeof t?t:{error:t||s(e)}},o(t,{status:e})},exports.html=n,exports.jpeg=a,exports.json=o,exports.png=c,exports.status=(e,t)=>new Response(null,{...t,status:e}),exports.text=r,exports.webp=p,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=e=>{e.proxy=new Proxy(e.proxy||e,{get:(t,o)=>{let s;return void 0!==(s=t[o])?s.bind?.(e)||s:t?.params?.[o]}})};
package/index.mjs CHANGED
@@ -1 +1 @@
1
- const e=({base:e="",routes:t=[]}={})=>({__proto__:new Proxy({},{get:(o,s,r,n)=>(o,...a)=>t.push([s.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])&&r}),routes:t,async handle(e,...o){let s,r,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)&&(r=n.pathname.match(c))){e.params=r.groups||{},e.route=i;for(let t of l)if(void 0!==(s=await t(e.proxy||e,...o)))return s}}});class t 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 o=(e="text/plain; charset=utf-8",t)=>(o,s)=>{const{headers:r={},...n}=s||{};return"Response"===o?.constructor.name?o:new Response(t?t(o):o,{headers:{"content-type":e,...r},...n})},s=o("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"),n=(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)}},s(t,{status:e})},a=(e,t)=>new Response(null,{...t,status:e}),c=o("text/plain; charset=utf-8",String),l=o("text/html"),i=o("image/jpeg"),p=o("image/png"),d=o("image/webp"),u=async e=>{e.headers.get("content-type")?.includes("json")&&(e.content=await e.json())},g=e=>{e.cookies=(e.headers.get("Cookie")||"").split(/;\s*/).map((e=>e.split(/=(.+)/))).reduce(((e,[t,o])=>o?(e[t]=o,e):e),{})},h=e=>{e.proxy=new Proxy(e.proxy||e,{get:(t,o)=>{let s;return void 0!==(s=t[o])?s.bind?.(e)||s:t?.params?.[o]}})},f=(e={})=>{const{origins:t=["*"],maxAge:o,methods:s=["GET"],headers:r={}}=e;let n;const a={"content-type":"application/json","Access-Control-Allow-Methods":s.join(", "),...r};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:s}=e;return[101,301,302,308].includes(o)||t.get("access-control-allow-origin")?e:new Response(s,{status:o,headers:{...Object.fromEntries(t),...a,...n,"content-type":t.get("content-type")}})},preflight:e=>{const o=[...new Set(["OPTIONS",...s])],r=e.headers.get("origin")||"";if(n=(t.includes(r)||t.includes("*"))&&{"Access-Control-Allow-Origin":r},"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(", ")}})}}}};export{e as Router,t as StatusError,f as createCors,o as createResponse,n as error,l as html,i as jpeg,s as json,p as png,a as status,c as text,d as webp,u as withContent,g as withCookies,h as withParams};
1
+ const e=({base:e="",routes:t=[]}={})=>({__proto__:new Proxy({},{get:(o,s,r,n)=>(o,...a)=>t.push([s.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])&&r}),routes:t,async handle(e,...o){let s,r,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,i,l]of t)if((a===e.method||"ALL"===a)&&(r=n.pathname.match(c))){e.params=r.groups||{},e.route=l;for(let t of i)if(void 0!==(s=await t(e.proxy||e,...o)))return s}}});class t 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 o=(e="text/plain; charset=utf-8",t)=>(o,{headers:s={},...r}={})=>void 0===o||"Response"===o?.constructor.name?o:new Response(t?t(o):o,{headers:{"content-type":e,...s},...r}),s=o("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"),n=(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)}},s(t,{status:e})},a=(e,t)=>new Response(null,{...t,status:e}),c=o("text/plain; charset=utf-8",String),i=o("text/html"),l=o("image/jpeg"),p=o("image/png"),d=o("image/webp"),u=async e=>{e.headers.get("content-type")?.includes("json")&&(e.content=await e.json())},g=e=>{e.cookies=(e.headers.get("Cookie")||"").split(/;\s*/).map((e=>e.split(/=(.+)/))).reduce(((e,[t,o])=>o?(e[t]=o,e):e),{})},h=e=>{e.proxy=new Proxy(e.proxy||e,{get:(t,o)=>{let s;return void 0!==(s=t[o])?s.bind?.(e)||s:t?.params?.[o]}})},f=(e={})=>{const{origins:t=["*"],maxAge:o,methods:s=["GET"],headers:r={}}=e;let n;const a="function"==typeof t?t:e=>t.includes(e)||t.includes("*"),c={"content-type":"application/json","Access-Control-Allow-Methods":s.join(", "),...r};o&&(c["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:s}=e;return[101,301,302,308].includes(o)||t.get("access-control-allow-origin")?e:new Response(s,{status:o,headers:{...Object.fromEntries(t),...c,...n,"content-type":t.get("content-type")}})},preflight:e=>{const t=[...new Set(["OPTIONS",...s])],o=e.headers.get("origin")||"";if(n=a(o)&&{"Access-Control-Allow-Origin":o},"OPTIONS"===e.method){const o={...c,"Access-Control-Allow-Methods":t.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")?o:{Allow:t.join(", ")}})}}}};export{e as Router,t as StatusError,f as createCors,o as createResponse,n as error,i as html,l as jpeg,s as json,p as png,a as status,c as text,d as webp,u as withContent,g as withCookies,h as withParams};
package/jpeg.js CHANGED
@@ -1 +1 @@
1
- "use strict";const e=((e="text/plain; charset=utf-8",t)=>(s,n)=>{const{headers:r={},...o}=n||{};return"Response"===s?.constructor.name?s:new Response(t?t(s):s,{headers:{"content-type":e,...r},...o})})("image/jpeg");exports.jpeg=e;
1
+ "use strict";const e=((e="text/plain; charset=utf-8",t)=>(s,{headers:n={},...o}={})=>void 0===s||"Response"===s?.constructor.name?s:new Response(t?t(s):s,{headers:{"content-type":e,...n},...o}))("image/jpeg");exports.jpeg=e;
package/jpeg.mjs CHANGED
@@ -1 +1 @@
1
- const e=((e="text/plain; charset=utf-8",t)=>(n,s)=>{const{headers:o={},...r}=s||{};return"Response"===n?.constructor.name?n:new Response(t?t(n):n,{headers:{"content-type":e,...o},...r})})("image/jpeg");export{e as jpeg};
1
+ const e=((e="text/plain; charset=utf-8",t)=>(n,{headers:s={},...o}={})=>void 0===n||"Response"===n?.constructor.name?n:new Response(t?t(n):n,{headers:{"content-type":e,...s},...o}))("image/jpeg");export{e as jpeg};
package/json.js CHANGED
@@ -1 +1 @@
1
- "use strict";const t=((t="text/plain; charset=utf-8",e)=>(s,n)=>{const{headers:o={},...r}=n||{};return"Response"===s?.constructor.name?s:new Response(e?e(s):s,{headers:{"content-type":t,...o},...r})})("application/json; charset=utf-8",JSON.stringify);exports.json=t;
1
+ "use strict";const e=((e="text/plain; charset=utf-8",t)=>(s,{headers:n={},...o}={})=>void 0===s||"Response"===s?.constructor.name?s:new Response(t?t(s):s,{headers:{"content-type":e,...n},...o}))("application/json; charset=utf-8",JSON.stringify);exports.json=e;
package/json.mjs CHANGED
@@ -1 +1 @@
1
- const e=((e="text/plain; charset=utf-8",t)=>(n,s)=>{const{headers:o={},...r}=s||{};return"Response"===n?.constructor.name?n:new Response(t?t(n):n,{headers:{"content-type":e,...o},...r})})("application/json; charset=utf-8",JSON.stringify);export{e as json};
1
+ const e=((e="text/plain; charset=utf-8",t)=>(n,{headers:s={},...o}={})=>void 0===n||"Response"===n?.constructor.name?n:new Response(t?t(n):n,{headers:{"content-type":e,...s},...o}))("application/json; charset=utf-8",JSON.stringify);export{e as json};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "itty-router",
3
- "version": "4.0.22",
3
+ "version": "4.0.24",
4
4
  "description": "A tiny, zero-dependency router, designed to make beautiful APIs in any environment.",
5
5
  "main": "./index.js",
6
6
  "module": "./index.mjs",
package/png.js CHANGED
@@ -1 +1 @@
1
- "use strict";const e=((e="text/plain; charset=utf-8",t)=>(n,s)=>{const{headers:r={},...o}=s||{};return"Response"===n?.constructor.name?n:new Response(t?t(n):n,{headers:{"content-type":e,...r},...o})})("image/png");exports.png=e;
1
+ "use strict";const e=((e="text/plain; charset=utf-8",t)=>(s,{headers:n={},...o}={})=>void 0===s||"Response"===s?.constructor.name?s:new Response(t?t(s):s,{headers:{"content-type":e,...n},...o}))("image/png");exports.png=e;
package/png.mjs CHANGED
@@ -1 +1 @@
1
- const e=((e="text/plain; charset=utf-8",t)=>(n,s)=>{const{headers:o={},...r}=s||{};return"Response"===n?.constructor.name?n:new Response(t?t(n):n,{headers:{"content-type":e,...o},...r})})("image/png");export{e as png};
1
+ const e=((e="text/plain; charset=utf-8",t)=>(n,{headers:s={},...o}={})=>void 0===n||"Response"===n?.constructor.name?n:new Response(t?t(n):n,{headers:{"content-type":e,...s},...o}))("image/png");export{e as png};
package/text.js CHANGED
@@ -1 +1 @@
1
- "use strict";const t=((t="text/plain; charset=utf-8",e)=>(s,n)=>{const{headers:r={},...c}=n||{};return"Response"===s?.constructor.name?s:new Response(e?e(s):s,{headers:{"content-type":t,...r},...c})})("text/plain; charset=utf-8",String);exports.text=t;
1
+ "use strict";const t=((t="text/plain; charset=utf-8",e)=>(s,{headers:n={},...r}={})=>void 0===s||"Response"===s?.constructor.name?s:new Response(e?e(s):s,{headers:{"content-type":t,...n},...r}))("text/plain; charset=utf-8",String);exports.text=t;
package/text.mjs CHANGED
@@ -1 +1 @@
1
- const e=((e="text/plain; charset=utf-8",t)=>(n,s)=>{const{headers:r={},...o}=s||{};return"Response"===n?.constructor.name?n:new Response(t?t(n):n,{headers:{"content-type":e,...r},...o})})("text/plain; charset=utf-8",String);export{e as text};
1
+ const e=((e="text/plain; charset=utf-8",t)=>(n,{headers:s={},...o}={})=>void 0===n||"Response"===n?.constructor.name?n:new Response(t?t(n):n,{headers:{"content-type":e,...s},...o}))("text/plain; charset=utf-8",String);export{e as text};
package/webp.js CHANGED
@@ -1 +1 @@
1
- "use strict";const e=((e="text/plain; charset=utf-8",t)=>(s,n)=>{const{headers:r={},...o}=n||{};return"Response"===s?.constructor.name?s:new Response(t?t(s):s,{headers:{"content-type":e,...r},...o})})("image/webp");exports.webp=e;
1
+ "use strict";const e=((e="text/plain; charset=utf-8",t)=>(s,{headers:n={},...o}={})=>void 0===s||"Response"===s?.constructor.name?s:new Response(t?t(s):s,{headers:{"content-type":e,...n},...o}))("image/webp");exports.webp=e;
package/webp.mjs CHANGED
@@ -1 +1 @@
1
- const e=((e="text/plain; charset=utf-8",t)=>(n,s)=>{const{headers:o={},...r}=s||{};return"Response"===n?.constructor.name?n:new Response(t?t(n):n,{headers:{"content-type":e,...o},...r})})("image/webp");export{e as webp};
1
+ const e=((e="text/plain; charset=utf-8",t)=>(n,{headers:s={},...o}={})=>void 0===n||"Response"===n?.constructor.name?n:new Response(t?t(n):n,{headers:{"content-type":e,...s},...o}))("image/webp");export{e as webp};
package/websocket.js CHANGED
@@ -1 +1 @@
1
- "use strict";exports.websocket=(e,t={})=>((e="text/plain; charset=utf-8",t)=>(s,n)=>{const{headers:o={},...r}=n||{};return"Response"===s?.constructor.name?s:new Response(t?t(s):s,{headers:{"content-type":e,...o},...r})})()(null,{status:101,webSocket:e,...t});
1
+ "use strict";exports.websocket=(e,t={})=>((e="text/plain; charset=utf-8",t)=>(s,{headers:n={},...o}={})=>void 0===s||"Response"===s?.constructor.name?s:new Response(t?t(s):s,{headers:{"content-type":e,...n},...o}))()(null,{status:101,webSocket:e,...t});
package/websocket.mjs CHANGED
@@ -1 +1 @@
1
- const e=(e,t={})=>((e="text/plain; charset=utf-8",t)=>(n,s)=>{const{headers:o={},...r}=s||{};return"Response"===n?.constructor.name?n:new Response(t?t(n):n,{headers:{"content-type":e,...o},...r})})()(null,{status:101,webSocket:e,...t});export{e as websocket};
1
+ const e=(e,t={})=>((e="text/plain; charset=utf-8",t)=>(s,{headers:n={},...o}={})=>void 0===s||"Response"===s?.constructor.name?s:new Response(t?t(s):s,{headers:{"content-type":e,...n},...o}))()(null,{status:101,webSocket:e,...t});export{e as websocket};
package/withContent.d.ts CHANGED
@@ -1,2 +1,5 @@
1
- import { IRequest } from './Router';
1
+ import { IRequest, IRequestStrict } from './Router';
2
+ export type HasContent<ContentType> = {
3
+ content: ContentType;
4
+ } & IRequestStrict;
2
5
  export declare const withContent: (request: IRequest) => Promise<void>;