Skip to content

Commit

Permalink
feat: access route data with $route
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien-R44 committed May 11, 2024
1 parent 31961e6 commit 2a46f4c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
9 changes: 8 additions & 1 deletion packages/client/src/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export class RouteHelper<Routes extends GeneratedRoutes> {
/**
* Generate methods like `$get`, `$post`, `$put`, etc
*/
return route.method.reduce((result, method) => {
const result = route.method.reduce((result, method) => {
result[`$${method.toLowerCase()}`] = (body: any, options: TuyauQueryOptions = {}) => {
const path = this.#buildUrl(route.path, params as any)

Expand All @@ -172,6 +172,13 @@ export class RouteHelper<Routes extends GeneratedRoutes> {

return result
}, {} as any)

result.method = route.method
result.name = route.name
result.path = route.path
result.params = route.params

return result
}

/**
Expand Down
5 changes: 5 additions & 0 deletions packages/client/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ export type RouteReturnType<T extends GeneratedRoutes, K extends T[number]['name
? (body?: Req, options?: TuyauQueryOptions) => ResponseOrUnwrap<res>
: (body: Req, options?: TuyauQueryOptions) => ResponseOrUnwrap<res>
: (body?: unknown, options?: TuyauQueryOptions) => Promise<unknown>
} & {
params: RouteByName<T, K>['params']
method: RouteByName<T, K>['method']
name: RouteByName<T, K>['name']
path: RouteByName<T, K>['path']
}

/**
Expand Down
10 changes: 10 additions & 0 deletions packages/client/tests/route.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ test.group('Route Helpers', () => {
await tuyau.$route('posts_comments.edit', { id: 2, postId: 1 }).$get().unwrap()
})

test('$route get route data', async ({ assert }) => {
const tuyau = createTuyau({ api, baseUrl: 'http://localhost:3333' })

const route = tuyau.$route('posts_comments.edit', { id: 2, postId: 1 })
assert.deepEqual(route.method, ['GET', 'HEAD'])
assert.deepEqual(route.name, 'posts_comments.edit')
assert.deepEqual(route.path, '/posts/:post_id/comments/:id/edit')
assert.deepEqual(route.params, ['post_id', 'id'])
})

test('$current', async ({ assert }) => {
const tuyau = createTuyau({ api, baseUrl: 'http://localhost:3333' })

Expand Down

0 comments on commit 2a46f4c

Please sign in to comment.