-
Notifications
You must be signed in to change notification settings - Fork 2.7k
feat(route-pattern): add createRouter
& urlpat
#10756
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -222,3 +222,32 @@ function matchSearch(search: string, constraints: SearchConstraints): boolean { | |||||||||||||||||||
|
||||||||||||||||||||
return true | ||||||||||||||||||||
} | ||||||||||||||||||||
/** | ||||||||||||||||||||
// Anative URLPattern for pathname-only patterns (variables, wildcards) | ||||||||||||||||||||
//eturns null if not supported or pattern uses enums or optionals or origin/search.... | ||||||||||||||||||||
**/ | ||||||||||||||||||||
Comment on lines
+226
to
+228
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct 'eturns' to 'Returns' and remove the stray slashes for consistency with the doc style.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||||||
|
||||||||||||||||||||
|
||||||||||||||||||||
Comment on lines
+226
to
+230
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The docblock uses line comments inside a block and contains typos, making it harder to read. Please convert to proper JSDoc and clarify constraints, e.g.: /** Converts pathname-only RoutePattern definitions to native URLPattern. Returns null if URLPattern is unavailable or if the pattern includes origin, search, enums, or optionals. */
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||||||
export function urlpat(input:string|RoutePattern):URLPattern|null{ | ||||||||||||||||||||
let Ctor =(globalThis as any).URLPattern | ||||||||||||||||||||
Comment on lines
+231
to
+232
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using the URLPattern type in the signature introduces a compile-time dependency on lib.dom, which may break consumers compiling for non-DOM targets. Consider a conditional or local alias to avoid requiring DOM types, e.g.: type NativeURLPattern = typeof globalThis extends { URLPattern: infer U } ? U : never and export function urlpat(...): NativeURLPattern | null.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||||||
if (!Ctor) return null; | ||||||||||||||||||||
let src=typeof input==='string' ? input:input.source | ||||||||||||||||||||
let {protocol,hostname,port,pathname,search}=parse(src) | ||||||||||||||||||||
if(protocol||hostname||port|| search) return null | ||||||||||||||||||||
if(!pathname) return new Ctor({pathname: '/'}) | ||||||||||||||||||||
let path=token(pathname) | ||||||||||||||||||||
if (path==null) return null | ||||||||||||||||||||
if(!path.startsWith('/')) path='/'+path | ||||||||||||||||||||
return new Ctor({pathname: path}) | ||||||||||||||||||||
} | ||||||||||||||||||||
function token(tokens: TokenList): string|null{ | ||||||||||||||||||||
let o='' | ||||||||||||||||||||
for (let i=0;i<tokens.length;i++ ){ | ||||||||||||||||||||
let t=tokens[i] | ||||||||||||||||||||
if (t.type==='text') o+=t.value | ||||||||||||||||||||
else if(t.type==='variable') o+=`:${t.name}` | ||||||||||||||||||||
else if (t.type==='wildcard') o+=t.name?`*${t.name}` : '*'; | ||||||||||||||||||||
else return null; | ||||||||||||||||||||
} | ||||||||||||||||||||
return o; | ||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,30 @@ | ||||||||||||||||||
import {RoutePattern,urlpat} from './route-pattern.ts' | ||||||||||||||||||
import type {Params} from './params.ts' | ||||||||||||||||||
export type Method='GET'|'POST'|'PUT'|'DELETE'|'PATCH'|'HEAD'|'OPTIONS'|'*' | ||||||||||||||||||
export type Handler<P extends string>=(req:Request,params:Params<P>)=>Response | Promise<Response> | ||||||||||||||||||
type Route ={method:Method; pattern:RoutePattern<any>; up?:URLPattern; handler:Handler<any>} | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same DOM typing concern as urlpat: up?: URLPattern will require lib.dom. Prefer a local alias (e.g., type NativeURLPattern = ReturnType extends infer R ? Exclude<R, null> : never) or a minimal structural type (e.g., { test(input: string | URL, baseURL?: string): boolean }) to keep the package environment-agnostic.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||||
|
||||||||||||||||||
export function createRouter(){ | ||||||||||||||||||
const routes:Route[]=[] | ||||||||||||||||||
const api={ | ||||||||||||||||||
get<P extends string>(pattern:P,handler:Handler<P>){add('GET',pattern,handler);return api}, | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Method union includes POST/PUT/DELETE/PATCH/etc., but the public API only exposes get and all. Consider adding per-verb helpers (post, put, delete, patch, head, options) or a generic on(method: Method, ...) to avoid surprising consumers.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||||
all<P extends string>(pattern:P,handler:Handler<P>){add('*',pattern,handler);return api}, | ||||||||||||||||||
async handle(req:Request):Promise<Response|undefined>{ | ||||||||||||||||||
const url=new URL(req.url) | ||||||||||||||||||
const method=(req.method||'GET').toUpperCase() | ||||||||||||||||||
for(const r of routes){ | ||||||||||||||||||
if(r.method!=='*'&&r.method!==method) continue | ||||||||||||||||||
if(r.up&&!r.up.test(url)) continue | ||||||||||||||||||
const m=r.pattern.match(url) | ||||||||||||||||||
if(m) return r.handler(req,m.params as any) | ||||||||||||||||||
} | ||||||||||||||||||
return undefined | ||||||||||||||||||
} | ||||||||||||||||||
} | ||||||||||||||||||
function add(method:Method, src:string, handler:Handler<any>) { | ||||||||||||||||||
const rp=new RoutePattern(src) | ||||||||||||||||||
const up=urlpat(rp)||undefined | ||||||||||||||||||
routes.push({method,pattern:rp,up,handler}) | ||||||||||||||||||
} | ||||||||||||||||||
return api | ||||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct 'Anative' to 'A native'.
Copilot uses AI. Check for mistakes.