itty-router
Advanced tools
Comparing version 4.0.23 to 4.0.24-next.0
import { IRequest } from './Router'; | ||
export interface CorsOptions { | ||
origins?: string[]; | ||
export type CorsOptions = { | ||
origins?: string[] | ((origin: string) => boolean); | ||
maxAge?: number; | ||
methods?: string[]; | ||
headers?: any; | ||
} | ||
}; | ||
export declare const createCors: (options?: CorsOptions) => { | ||
@@ -9,0 +9,0 @@ corsify: (response: Response) => Response; |
@@ -1,1 +0,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(", ")}})}}}}; | ||
"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(", ")}})}}}}; |
@@ -1,1 +0,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})}; | ||
"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 +0,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})}; | ||
"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})}; |
@@ -1,1 +0,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; | ||
"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; |
@@ -1,1 +0,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]}})}; | ||
"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,{headers:o={},...r}={})=>void 0===s||"Response"===s?.constructor.name?s:new Response(t?t(s):s,{headers:{"content-type":e,...o},...r}),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"),p=t("image/png"),c=t("image/webp");exports.Router=({base:e="",routes:t=[]}={})=>({__proto__:new Proxy({},{get:(s,o,r,n)=>(s,...a)=>(a=a.map((e=>e.handle?e.base?e.handle:(t,...o)=>(t.url=new URL(t.url),t.url.pathname=t.url.pathname.replace(s.replace(/\/\*$/,""),""),e.handle(t,...o)):e)),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,base:e,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,p,c,l]of t)if((a===e.method||"ALL"===a)&&(r=n.pathname.match(p))){e.params=r.groups||{},e.route=l;for(let t of c)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="function"==typeof t?t:e=>t.includes(e)||t.includes("*"),p={"content-type":"application/json","Access-Control-Allow-Methods":o.join(", "),...r};s&&(p["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),...p,...n,"content-type":t.get("content-type")}})},preflight:e=>{const t=[...new Set(["OPTIONS",...o])],s=e.headers.get("origin")||"";if(n=a(s)&&{"Access-Control-Allow-Origin":s},"OPTIONS"===e.method){const s={...p,"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")?s:{Allow:t.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=p,exports.status=(e,t)=>new Response(null,{...t,status:e}),exports.text=r,exports.webp=c,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)=>void 0!==t[s]?t[s].bind?.(e)||t[s]:t?.params?.[s]})}; |
@@ -1,1 +0,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; | ||
"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; |
@@ -1,1 +0,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; | ||
"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; |
{ | ||
"name": "itty-router", | ||
"version": "4.0.23", | ||
"version": "4.0.24-next.0", | ||
"description": "A tiny, zero-dependency router, designed to make beautiful APIs in any environment.", | ||
@@ -146,4 +146,2 @@ "main": "./index.js", | ||
"@typescript-eslint/parser": "^5.60.0", | ||
"@vitejs/plugin-vue": "^4.2.3", | ||
"@vitest/coverage-c8": "^0.32.2", | ||
"@vitest/coverage-v8": "^0.32.2", | ||
@@ -150,0 +148,0 @@ "@whatwg-node/server": "^0.8.12", |
@@ -1,1 +0,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; | ||
"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; |
@@ -27,5 +27,5 @@ export type GenericTraps = { | ||
(request: I, ...args: A): any; | ||
}; | ||
} & MayBeRouter; | ||
export type RouteEntry = [string, RegExp, RouteHandler[], string]; | ||
export type Route = <RequestType = IRequest, Args extends any[] = any[], RT = RouterType>(path: string, ...handlers: RouteHandler<RequestType, Args>[]) => RT; | ||
export type Route = <RequestType = IRequest, Args extends any[] = any[], RT = RouterType>(path: string, ...handlers: RouteHandlerOrRouter<RequestType, Args>[]) => RT; | ||
export type UniversalRoute<RequestType = IRequest, Args extends any[] = any[]> = (path: string, ...handlers: RouteHandler<RequestType, Args>[]) => RouterType<UniversalRoute<RequestType, Args>, Args>; | ||
@@ -36,2 +36,6 @@ type Equal<X, Y> = (<T>() => T extends X ? 1 : 2) extends (<T>() => T extends Y ? 1 : 2) ? true : false; | ||
}; | ||
type MayBeRouter = { | ||
handle?: any; | ||
base?: string; | ||
}; | ||
export type RouterType<R = Route, Args extends any[] = any[]> = { | ||
@@ -41,2 +45,3 @@ __proto__: RouterType<R>; | ||
handle: <A extends any[] = Args>(request: RequestLike, ...extra: Equal<R, Args> extends true ? A : Args) => Promise<any>; | ||
base: string; | ||
all: R; | ||
@@ -51,3 +56,4 @@ delete: R; | ||
} & CustomRoutes<R>; | ||
type RouteHandlerOrRouter<RequestType = IRequest, Args extends any[] = any[]> = RouteHandler<RequestType, Args> | RouterType; | ||
export declare const Router: <RequestType = IRequest, Args extends any[] = any[], RouteType = Equal<RequestType, IRequest> extends true ? Route : UniversalRoute<RequestType, Args>>({ base, routes }?: RouterOptions) => RouterType<RouteType, Args>; | ||
export {}; |
@@ -1,1 +0,1 @@ | ||
"use strict";exports.Router=({base:e="",routes:r=[]}={})=>({__proto__:new Proxy({},{get:(o,t,a,p)=>(o,...l)=>r.push([t.toUpperCase(),RegExp(`^${(p=(e+o).replace(/\/+(\/|$)/g,"$1")).replace(/(\/?\.?):(\w+)\+/g,"($1(?<$2>*))").replace(/(\/?\.?):(\w+)/g,"($1(?<$2>[^$1/]+?))").replace(/\./g,"\\.").replace(/(\/?)\*/g,"($1.*)?")}/*$`),l,p])&&a}),routes:r,async handle(e,...o){let t,a,p=new URL(e.url),l=e.query={__proto__:null};for(let[e,r]of p.searchParams)l[e]=void 0===l[e]?r:[l[e],r].flat();for(let[l,s,u,$]of r)if((l===e.method||"ALL"===l)&&(a=p.pathname.match(s))){e.params=a.groups||{},e.route=$;for(let r of u)if(void 0!==(t=await r(e.proxy||e,...o)))return t}}}); | ||
"use strict";exports.Router=({base:e="",routes:r=[]}={})=>({__proto__:new Proxy({},{get:(a,l,t,o)=>(a,...p)=>(p=p.map((e=>e.handle?e.base?e.handle:(r,...l)=>(r.url=new URL(r.url),r.url.pathname=r.url.pathname.replace(a.replace(/\/\*$/,""),""),e.handle(r,...l)):e)),r.push([l.toUpperCase?.(),RegExp(`^${(o=(e+a).replace(/\/+(\/|$)/g,"$1")).replace(/(\/?\.?):(\w+)\+/g,"($1(?<$2>*))").replace(/(\/?\.?):(\w+)/g,"($1(?<$2>[^$1/]+?))").replace(/\./g,"\\.").replace(/(\/?)\*/g,"($1.*)?")}/*$`),p,o]),t)}),routes:r,base:e,async handle(e,...a){let l,t,o=new URL(e.url),p=e.query={__proto__:null};for(let[e,r]of o.searchParams)p[e]=void 0===p[e]?r:[p[e],r].flat();for(let[p,s,u,n]of r)if((p===e.method||"ALL"===p)&&(t=o.pathname.match(s))){e.params=t.groups||{},e.route=n;for(let r of u)if(void 0!==(l=await r(e.proxy||e,...a)))return l}}}); |
@@ -1,1 +0,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; | ||
"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; |
@@ -1,1 +0,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; | ||
"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; |
@@ -1,1 +0,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}); | ||
"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}); |
@@ -1,2 +0,5 @@ | ||
import { IRequest } from './Router'; | ||
import { IRequest, IRequestStrict } from './Router'; | ||
export type HasContent<ContentType> = { | ||
content: ContentType; | ||
} & IRequestStrict; | ||
export declare const withContent: (request: IRequest) => Promise<void>; |
@@ -1,1 +0,1 @@ | ||
"use strict";exports.withParams=r=>{r.proxy=new Proxy(r.proxy||r,{get:(t,e)=>{let o;return void 0!==(o=t[e])?o.bind?.(r)||o:t?.params?.[e]}})}; | ||
"use strict";exports.withParams=r=>{r.proxy=new Proxy(r.proxy||r,{get:(o,s)=>void 0!==o[s]?o[s].bind?.(r)||o[s]:o?.params?.[s]})}; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
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
37884
34
202
1