@tupik/client 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +44 -12
- package/dist/index.d.ts +44 -12
- package/package.json +2 -2
package/dist/index.d.mts
CHANGED
|
@@ -203,10 +203,16 @@ declare function createWebSocket(url: string, options?: WebSocketOptions): WebSo
|
|
|
203
203
|
*/
|
|
204
204
|
declare function createRpcProxy(client: TupikClient, prefix?: string, method?: HTTPMethod): unknown;
|
|
205
205
|
|
|
206
|
-
/**
|
|
206
|
+
/**
|
|
207
|
+
* Route type union carried by a `TupikApp` (its phantom `~routes` member).
|
|
208
|
+
* Read by indexed access rather than `infer`: inferring from the optional
|
|
209
|
+
* member re-instantiates every route of the union (a second O(N) walk).
|
|
210
|
+
*/
|
|
207
211
|
type AppRoutes<TApp> = TApp extends {
|
|
208
|
-
readonly '~routes'?:
|
|
209
|
-
} ?
|
|
212
|
+
readonly '~routes'?: unknown;
|
|
213
|
+
} ? DefinedRoutes<TApp['~routes']> : never;
|
|
214
|
+
/** `unknown` stays `unknown` (a type without `~routes`), as the `infer` form gave. */
|
|
215
|
+
type DefinedRoutes<TRoutes> = unknown extends TRoutes ? TRoutes : NonNullable<TRoutes>;
|
|
210
216
|
/** Standard Schema input type (`~standard.types.input`), falling back to the output type. */
|
|
211
217
|
type InferInput<TSchema extends StandardSchemaV1> = TSchema extends {
|
|
212
218
|
readonly '~standard': {
|
|
@@ -241,6 +247,18 @@ type MethodRoutes<TRoutes, TMethod extends HTTPMethod> = {
|
|
|
241
247
|
readonly path: infer TPath extends string;
|
|
242
248
|
} ? TPath : never]: TRoute;
|
|
243
249
|
};
|
|
250
|
+
/**
|
|
251
|
+
* Same map as {@link MethodRoutes}, built from a union already filtered by
|
|
252
|
+
* method (`Extract<TRoutes, { readonly method: M }>`). Used by the typed client
|
|
253
|
+
* for type cost: each typed call re-instantiates its map's type arguments, so a
|
|
254
|
+
* map over one method's routes (no `MethodRoutes` alias carrying the whole
|
|
255
|
+
* union) keeps that per-call walk to the routes of the method being called.
|
|
256
|
+
*/
|
|
257
|
+
type PathRoutes<TRoutes> = {
|
|
258
|
+
[TRoute in TRoutes as TRoute extends {
|
|
259
|
+
readonly path: infer TPath extends string;
|
|
260
|
+
} ? TPath : never]: TRoute;
|
|
261
|
+
};
|
|
244
262
|
/** `'/users/:id/*rest'` → `{ id: string; rest: string }`. */
|
|
245
263
|
type PathParams<TPath extends string> = TPath extends `${string}:${infer TName}/${infer TRest}` ? {
|
|
246
264
|
[K in TName]: string;
|
|
@@ -305,20 +323,34 @@ type TypedMethod<TRoutes> = <TPath extends keyof TRoutes & string>(path: TPath,
|
|
|
305
323
|
* accumulates: `const app = createApp().register(users)`.
|
|
306
324
|
*/
|
|
307
325
|
interface TypedClient<TRoutes> {
|
|
308
|
-
get: TypedMethod<
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
326
|
+
get: TypedMethod<PathRoutes<Extract<TRoutes, {
|
|
327
|
+
readonly method: 'GET';
|
|
328
|
+
}>>>;
|
|
329
|
+
post: TypedMethod<PathRoutes<Extract<TRoutes, {
|
|
330
|
+
readonly method: 'POST';
|
|
331
|
+
}>>>;
|
|
332
|
+
put: TypedMethod<PathRoutes<Extract<TRoutes, {
|
|
333
|
+
readonly method: 'PUT';
|
|
334
|
+
}>>>;
|
|
335
|
+
patch: TypedMethod<PathRoutes<Extract<TRoutes, {
|
|
336
|
+
readonly method: 'PATCH';
|
|
337
|
+
}>>>;
|
|
338
|
+
delete: TypedMethod<PathRoutes<Extract<TRoutes, {
|
|
339
|
+
readonly method: 'DELETE';
|
|
340
|
+
}>>>;
|
|
341
|
+
head: TypedMethod<PathRoutes<Extract<TRoutes, {
|
|
342
|
+
readonly method: 'HEAD';
|
|
343
|
+
}>>>;
|
|
344
|
+
options: TypedMethod<PathRoutes<Extract<TRoutes, {
|
|
345
|
+
readonly method: 'OPTIONS';
|
|
346
|
+
}>>>;
|
|
315
347
|
/** Untyped escape hatch, same as `TupikClient.request`. */
|
|
316
348
|
request<TData = unknown>(method: HTTPMethod, path: string, options?: RequestOptions): Promise<ClientResult<TData>>;
|
|
317
349
|
}
|
|
318
350
|
/**
|
|
319
351
|
* Creates a client typed end-to-end from `typeof app`. Paths, request options
|
|
320
|
-
* and response data are resolved through
|
|
321
|
-
*
|
|
352
|
+
* and response data are resolved through one path-keyed map per method (the
|
|
353
|
+
* {@link MethodRoutes} map), so each call costs a key lookup rather than a scan of every route.
|
|
322
354
|
*/
|
|
323
355
|
declare function createTypedClient<TApp>(options: ClientOptions): TypedClient<AppRoutes<TApp>>;
|
|
324
356
|
|
package/dist/index.d.ts
CHANGED
|
@@ -203,10 +203,16 @@ declare function createWebSocket(url: string, options?: WebSocketOptions): WebSo
|
|
|
203
203
|
*/
|
|
204
204
|
declare function createRpcProxy(client: TupikClient, prefix?: string, method?: HTTPMethod): unknown;
|
|
205
205
|
|
|
206
|
-
/**
|
|
206
|
+
/**
|
|
207
|
+
* Route type union carried by a `TupikApp` (its phantom `~routes` member).
|
|
208
|
+
* Read by indexed access rather than `infer`: inferring from the optional
|
|
209
|
+
* member re-instantiates every route of the union (a second O(N) walk).
|
|
210
|
+
*/
|
|
207
211
|
type AppRoutes<TApp> = TApp extends {
|
|
208
|
-
readonly '~routes'?:
|
|
209
|
-
} ?
|
|
212
|
+
readonly '~routes'?: unknown;
|
|
213
|
+
} ? DefinedRoutes<TApp['~routes']> : never;
|
|
214
|
+
/** `unknown` stays `unknown` (a type without `~routes`), as the `infer` form gave. */
|
|
215
|
+
type DefinedRoutes<TRoutes> = unknown extends TRoutes ? TRoutes : NonNullable<TRoutes>;
|
|
210
216
|
/** Standard Schema input type (`~standard.types.input`), falling back to the output type. */
|
|
211
217
|
type InferInput<TSchema extends StandardSchemaV1> = TSchema extends {
|
|
212
218
|
readonly '~standard': {
|
|
@@ -241,6 +247,18 @@ type MethodRoutes<TRoutes, TMethod extends HTTPMethod> = {
|
|
|
241
247
|
readonly path: infer TPath extends string;
|
|
242
248
|
} ? TPath : never]: TRoute;
|
|
243
249
|
};
|
|
250
|
+
/**
|
|
251
|
+
* Same map as {@link MethodRoutes}, built from a union already filtered by
|
|
252
|
+
* method (`Extract<TRoutes, { readonly method: M }>`). Used by the typed client
|
|
253
|
+
* for type cost: each typed call re-instantiates its map's type arguments, so a
|
|
254
|
+
* map over one method's routes (no `MethodRoutes` alias carrying the whole
|
|
255
|
+
* union) keeps that per-call walk to the routes of the method being called.
|
|
256
|
+
*/
|
|
257
|
+
type PathRoutes<TRoutes> = {
|
|
258
|
+
[TRoute in TRoutes as TRoute extends {
|
|
259
|
+
readonly path: infer TPath extends string;
|
|
260
|
+
} ? TPath : never]: TRoute;
|
|
261
|
+
};
|
|
244
262
|
/** `'/users/:id/*rest'` → `{ id: string; rest: string }`. */
|
|
245
263
|
type PathParams<TPath extends string> = TPath extends `${string}:${infer TName}/${infer TRest}` ? {
|
|
246
264
|
[K in TName]: string;
|
|
@@ -305,20 +323,34 @@ type TypedMethod<TRoutes> = <TPath extends keyof TRoutes & string>(path: TPath,
|
|
|
305
323
|
* accumulates: `const app = createApp().register(users)`.
|
|
306
324
|
*/
|
|
307
325
|
interface TypedClient<TRoutes> {
|
|
308
|
-
get: TypedMethod<
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
326
|
+
get: TypedMethod<PathRoutes<Extract<TRoutes, {
|
|
327
|
+
readonly method: 'GET';
|
|
328
|
+
}>>>;
|
|
329
|
+
post: TypedMethod<PathRoutes<Extract<TRoutes, {
|
|
330
|
+
readonly method: 'POST';
|
|
331
|
+
}>>>;
|
|
332
|
+
put: TypedMethod<PathRoutes<Extract<TRoutes, {
|
|
333
|
+
readonly method: 'PUT';
|
|
334
|
+
}>>>;
|
|
335
|
+
patch: TypedMethod<PathRoutes<Extract<TRoutes, {
|
|
336
|
+
readonly method: 'PATCH';
|
|
337
|
+
}>>>;
|
|
338
|
+
delete: TypedMethod<PathRoutes<Extract<TRoutes, {
|
|
339
|
+
readonly method: 'DELETE';
|
|
340
|
+
}>>>;
|
|
341
|
+
head: TypedMethod<PathRoutes<Extract<TRoutes, {
|
|
342
|
+
readonly method: 'HEAD';
|
|
343
|
+
}>>>;
|
|
344
|
+
options: TypedMethod<PathRoutes<Extract<TRoutes, {
|
|
345
|
+
readonly method: 'OPTIONS';
|
|
346
|
+
}>>>;
|
|
315
347
|
/** Untyped escape hatch, same as `TupikClient.request`. */
|
|
316
348
|
request<TData = unknown>(method: HTTPMethod, path: string, options?: RequestOptions): Promise<ClientResult<TData>>;
|
|
317
349
|
}
|
|
318
350
|
/**
|
|
319
351
|
* Creates a client typed end-to-end from `typeof app`. Paths, request options
|
|
320
|
-
* and response data are resolved through
|
|
321
|
-
*
|
|
352
|
+
* and response data are resolved through one path-keyed map per method (the
|
|
353
|
+
* {@link MethodRoutes} map), so each call costs a key lookup rather than a scan of every route.
|
|
322
354
|
*/
|
|
323
355
|
declare function createTypedClient<TApp>(options: ClientOptions): TypedClient<AppRoutes<TApp>>;
|
|
324
356
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tupik/client",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Type-safe RPC client for Tupik apps with SSE and WebSocket helpers, inferred from typeof app.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"keywords": [
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
"node": ">=22.12"
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@tupik/core": "^1.
|
|
42
|
+
"@tupik/core": "^1.2.0"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
45
|
"typescript": "^5.6.0",
|