Skip to content

Commit

Permalink
fix(typed-router): partial route definitions
Browse files Browse the repository at this point in the history
The provided route definitions type might not implement all
methods. For this we can wrap the type in a Partial utility. To handle
the possibly undefined types that arise from this change we'll
need to wrap each type in a NonNullable utility as well.
  • Loading branch information
believer committed Jun 27, 2023
1 parent e80ea46 commit 10b0ffd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/strong-clouds-decide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sebspark/typed-router': patch
---

Make route definitions type a partial since the definitions might not implement all methods.
17 changes: 10 additions & 7 deletions packages/typed-router/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ type MethodDefinition = {
>
}

export type RouteDefinitions = {
export type RouteDefinitions = Partial<{
get: MethodDefinition
post: MethodDefinition
put: MethodDefinition
patch: MethodDefinition
delete: MethodDefinition
}
}>

const abstractedHandler = <R extends MethodDefinition>(
router: Router,
Expand Down Expand Up @@ -97,11 +97,14 @@ export const TypedRouter = <R extends RouteDefinitions>() => {

return {
expressRouter,
get: abstractedHandler<R['get']>(expressRouter, 'GET'),
post: abstractedHandler<R['post']>(expressRouter, 'POST'),
put: abstractedHandler<R['put']>(expressRouter, 'PUT'),
patch: abstractedHandler<R['patch']>(expressRouter, 'PATCH'),
delete: abstractedHandler<R['delete']>(expressRouter, 'DELETE'),
get: abstractedHandler<NonNullable<R['get']>>(expressRouter, 'GET'),
post: abstractedHandler<NonNullable<R['post']>>(expressRouter, 'POST'),
put: abstractedHandler<NonNullable<R['put']>>(expressRouter, 'PUT'),
patch: abstractedHandler<NonNullable<R['patch']>>(expressRouter, 'PATCH'),
delete: abstractedHandler<NonNullable<R['delete']>>(
expressRouter,
'DELETE'
),
}
}

Expand Down

0 comments on commit 10b0ffd

Please sign in to comment.