Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/route-pattern/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { RoutePattern } from './lib/route-pattern.ts'
export { RoutePattern,urlpat } from './lib/route-pattern.ts'
export { createRouter } from './lib/router.ts'
export type { RoutePatternOptions, RouteMatch } from './lib/route-pattern.ts'
export { MissingParamError, createHrefBuilder } from './lib/href.ts'
export type { HrefBuilder, HrefBuilderOptions } from './lib/href.ts'
Expand Down
29 changes: 29 additions & 0 deletions packages/route-pattern/src/lib/route-pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,32 @@ function matchSearch(search: string, constraints: SearchConstraints): boolean {

return true
}
/**
// Anative URLPattern for pathname-only patterns (variables, wildcards)
Copy link
Preview

Copilot AI Oct 4, 2025

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'.

Suggested change
// Anative URLPattern for pathname-only patterns (variables, wildcards)
// A native URLPattern for pathname-only patterns (variables, wildcards)

Copilot uses AI. Check for mistakes.

//eturns null if not supported or pattern uses enums or optionals or origin/search....
**/
Comment on lines +226 to +228
Copy link
Preview

Copilot AI Oct 4, 2025

Choose a reason for hiding this comment

The 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
// Anative URLPattern for pathname-only patterns (variables, wildcards)
//eturns null if not supported or pattern uses enums or optionals or origin/search....
**/
* A native URLPattern for pathname-only patterns (variables, wildcards).
* Returns null if not supported or if the pattern uses enums, optionals, origin, or search.
*/

Copilot uses AI. Check for mistakes.



Comment on lines +226 to +230
Copy link
Preview

Copilot AI Oct 4, 2025

Choose a reason for hiding this comment

The 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
// Anative URLPattern for pathname-only patterns (variables, wildcards)
//eturns null if not supported or pattern uses enums or optionals or origin/search....
**/
* Converts pathname-only RoutePattern definitions to native URLPattern.
* Returns null if URLPattern is unavailable or if the pattern includes origin, search, enums, or optionals.
*
* @param input - A string or RoutePattern to convert.
* @returns A URLPattern instance, or null if constraints are not met.
*/

Copilot uses AI. Check for mistakes.

export function urlpat(input:string|RoutePattern):URLPattern|null{
let Ctor =(globalThis as any).URLPattern
Comment on lines +231 to +232
Copy link
Preview

Copilot AI Oct 4, 2025

Choose a reason for hiding this comment

The 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
export function urlpat(input:string|RoutePattern):URLPattern|null{
let Ctor =(globalThis as any).URLPattern
type NativeURLPattern = typeof globalThis extends { URLPattern: infer U } ? U : never;
export function urlpat(input: string | RoutePattern): NativeURLPattern | null {
let Ctor = (globalThis as any).URLPattern

Copilot uses AI. Check for mistakes.

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;
}
30 changes: 30 additions & 0 deletions packages/route-pattern/src/lib/router.ts
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>}
Copy link
Preview

Copilot AI Oct 4, 2025

Choose a reason for hiding this comment

The 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
type Route ={method:Method; pattern:RoutePattern<any>; up?:URLPattern; handler:Handler<any>}
type URLPatternLike = { test(input: string | URL, baseURL?: string): boolean }
type Route ={method:Method; pattern:RoutePattern<any>; up?:URLPatternLike; handler:Handler<any>}

Copilot uses AI. Check for mistakes.


export function createRouter(){
const routes:Route[]=[]
const api={
get<P extends string>(pattern:P,handler:Handler<P>){add('GET',pattern,handler);return api},
Copy link
Preview

Copilot AI Oct 4, 2025

Choose a reason for hiding this comment

The 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
get<P extends string>(pattern:P,handler:Handler<P>){add('GET',pattern,handler);return api},
get<P extends string>(pattern:P,handler:Handler<P>){add('GET',pattern,handler);return api},
post<P extends string>(pattern:P,handler:Handler<P>){add('POST',pattern,handler);return api},
put<P extends string>(pattern:P,handler:Handler<P>){add('PUT',pattern,handler);return api},
delete<P extends string>(pattern:P,handler:Handler<P>){add('DELETE',pattern,handler);return api},
patch<P extends string>(pattern:P,handler:Handler<P>){add('PATCH',pattern,handler);return api},
head<P extends string>(pattern:P,handler:Handler<P>){add('HEAD',pattern,handler);return api},
options<P extends string>(pattern:P,handler:Handler<P>){add('OPTIONS',pattern,handler);return api},

Copilot uses AI. Check for mistakes.

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
}