diff --git a/packages/client/src/route.ts b/packages/client/src/route.ts index 2a4dcc4..5ae28d6 100644 --- a/packages/client/src/route.ts +++ b/packages/client/src/route.ts @@ -157,7 +157,7 @@ export class RouteHelper { /** * 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) @@ -172,6 +172,13 @@ export class RouteHelper { return result }, {} as any) + + result.method = route.method + result.name = route.name + result.path = route.path + result.params = route.params + + return result } /** diff --git a/packages/client/src/types.ts b/packages/client/src/types.ts index d3b7a4e..4f0658c 100644 --- a/packages/client/src/types.ts +++ b/packages/client/src/types.ts @@ -154,6 +154,11 @@ export type RouteReturnType ResponseOrUnwrap : (body: Req, options?: TuyauQueryOptions) => ResponseOrUnwrap : (body?: unknown, options?: TuyauQueryOptions) => Promise +} & { + params: RouteByName['params'] + method: RouteByName['method'] + name: RouteByName['name'] + path: RouteByName['path'] } /** diff --git a/packages/client/tests/route.spec.ts b/packages/client/tests/route.spec.ts index b6d67be..6e43bd6 100644 --- a/packages/client/tests/route.spec.ts +++ b/packages/client/tests/route.spec.ts @@ -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' })