|
1 | 1 | import type { ValueOf } from 'type-fest' |
2 | 2 |
|
3 | | -import { ApiRouteResponse, ServerConfig } from '@kivotos/core' |
4 | | -import { ApiRouteHandlerPayload, ApiRouteSchema, ClientApiRouter } from '@kivotos/core' |
| 3 | +import { ApiRouter, ApiRouteResponse, ServerConfig } from '@kivotos/core' |
| 4 | +import { ApiRouteHandlerPayload, ApiRouteSchema } from '@kivotos/core' |
5 | 5 |
|
6 | 6 | import { withPathParams, withQueryParams } from './utils' |
7 | 7 |
|
@@ -53,42 +53,44 @@ export function createRestClient<TServerConfig extends ServerConfig<any, any, an |
53 | 53 | } |
54 | 54 | } |
55 | 55 |
|
56 | | -type ExtractClientApiRouterPath<TApiRouter extends ClientApiRouter> = ValueOf<{ |
57 | | - [TKey in keyof TApiRouter]: TApiRouter[TKey] extends { path: infer TPath extends string } |
| 56 | +type ExtractClientApiRouterPath<TApiRouter extends ApiRouter<any>> = ValueOf<{ |
| 57 | + [TKey in keyof TApiRouter]: TApiRouter[TKey]['schema'] extends { |
| 58 | + path: infer TPath extends string |
| 59 | + } |
58 | 60 | ? TPath |
59 | 61 | : never |
60 | 62 | }> |
61 | 63 |
|
62 | | -type RestResponse<TApiRouter extends ClientApiRouter, TPath extends string> = ValueOf<{ |
63 | | - [TKey in keyof TApiRouter]: TApiRouter[TKey] extends { path: TPath } |
64 | | - ? TApiRouter[TKey] extends infer TApiRouteSchema extends ApiRouteSchema |
| 64 | +type RestResponse<TApiRouter extends ApiRouter<any>, TPath extends string> = ValueOf<{ |
| 65 | + [TKey in keyof TApiRouter]: TApiRouter[TKey]['schema'] extends { path: TPath } |
| 66 | + ? TApiRouter[TKey]['schema'] extends infer TApiRouteSchema extends ApiRouteSchema |
65 | 67 | ? ApiRouteResponse<TApiRouteSchema['responses']> |
66 | 68 | : never |
67 | 69 | : never |
68 | 70 | }> |
69 | 71 |
|
70 | | -type RestPayload<TApiRouter extends ClientApiRouter, TPath extends string> = ValueOf<{ |
71 | | - [TKey in keyof TApiRouter]: TApiRouter[TKey] extends { path: TPath } |
72 | | - ? TApiRouter[TKey] extends infer TApiRouteSchema extends ApiRouteSchema |
| 72 | +type RestPayload<TApiRouter extends ApiRouter<any>, TPath extends string> = ValueOf<{ |
| 73 | + [TKey in keyof TApiRouter]: TApiRouter[TKey]['schema'] extends { path: TPath } |
| 74 | + ? TApiRouter[TKey]['schema'] extends infer TApiRouteSchema extends ApiRouteSchema |
73 | 75 | ? ApiRouteHandlerPayload<TApiRouteSchema> |
74 | 76 | : never |
75 | 77 | : never |
76 | 78 | }> |
77 | 79 |
|
78 | | -type RestMethod<TApiRouter extends ClientApiRouter> = < |
| 80 | +type RestMethod<TApiRouter extends ApiRouter<any>> = < |
79 | 81 | TPath extends ExtractClientApiRouterPath<TApiRouter>, |
80 | 82 | >( |
81 | 83 | path: TPath, |
82 | 84 | payload: RestPayload<TApiRouter, TPath> |
83 | 85 | ) => Promise<RestResponse<TApiRouter, TPath>> |
84 | 86 |
|
85 | | -type FilterByMethod<TApiRouter, TMethod extends string> = { |
86 | | - [TKey in keyof TApiRouter as TApiRouter[TKey] extends { method: TMethod } |
| 87 | +type FilterByMethod<TApiRouter extends ApiRouter<any>, TMethod extends string> = { |
| 88 | + [TKey in keyof TApiRouter as TApiRouter[TKey]['schema'] extends { method: TMethod } |
87 | 89 | ? TKey |
88 | 90 | : never]: TApiRouter[TKey] |
89 | 91 | } |
90 | 92 |
|
91 | | -export interface RestClient<TApiRouter extends ClientApiRouter> { |
| 93 | +export interface RestClient<TApiRouter extends ApiRouter<any>> { |
92 | 94 | GET: RestMethod<FilterByMethod<TApiRouter, 'GET'>> |
93 | 95 | POST: RestMethod<FilterByMethod<TApiRouter, 'POST'>> |
94 | 96 | PUT: RestMethod<FilterByMethod<TApiRouter, 'PUT'>> |
|
0 commit comments