@veloxts/router 0.6.77 → 0.6.79
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/CHANGELOG.md +18 -0
- package/dist/procedure/builder.d.ts +4 -2
- package/dist/procedure/builder.js +3 -1
- package/dist/rest/routes.d.ts +3 -3
- package/dist/router-utils.d.ts +13 -9
- package/dist/tokens.d.ts +1 -1
- package/dist/types.d.ts +3 -2
- package/package.json +3 -3
- package/dist/contracts.d.ts +0 -180
- package/dist/contracts.js +0 -100
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
# @veloxts/router
|
|
2
2
|
|
|
3
|
+
## 0.6.79
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- fix(router,client): preserve namespace literal types for proper type narrowing
|
|
8
|
+
- Updated dependencies
|
|
9
|
+
- @veloxts/core@0.6.79
|
|
10
|
+
- @veloxts/validation@0.6.79
|
|
11
|
+
|
|
12
|
+
## 0.6.78
|
|
13
|
+
|
|
14
|
+
### Patch Changes
|
|
15
|
+
|
|
16
|
+
- fix(ci): add Docker Hub login to avoid rate limits in tests, add rsc-auth template to smoke test matrix, Fix / types.d.ts reference error in template
|
|
17
|
+
- Updated dependencies
|
|
18
|
+
- @veloxts/core@0.6.78
|
|
19
|
+
- @veloxts/validation@0.6.78
|
|
20
|
+
|
|
3
21
|
## 0.6.77
|
|
4
22
|
|
|
5
23
|
### Patch Changes
|
|
@@ -79,11 +79,12 @@ export interface DefineProceduresOptions {
|
|
|
79
79
|
* In development mode, emits warnings for procedure names that don't follow
|
|
80
80
|
* naming conventions (which means they won't generate REST routes).
|
|
81
81
|
*
|
|
82
|
+
* @template TNamespace - The literal namespace string (inferred from argument)
|
|
82
83
|
* @template TProcedures - The record of named procedures
|
|
83
84
|
* @param namespace - Resource namespace (e.g., 'users', 'posts')
|
|
84
85
|
* @param procedures - Object containing named procedures
|
|
85
86
|
* @param options - Optional configuration for warnings
|
|
86
|
-
* @returns Procedure collection with preserved types
|
|
87
|
+
* @returns Procedure collection with preserved types including literal namespace
|
|
87
88
|
*
|
|
88
89
|
* @example
|
|
89
90
|
* ```typescript
|
|
@@ -102,6 +103,7 @@ export interface DefineProceduresOptions {
|
|
|
102
103
|
* });
|
|
103
104
|
*
|
|
104
105
|
* // Types are fully preserved:
|
|
106
|
+
* // userProcedures.namespace -> 'users' (literal type)
|
|
105
107
|
* // userProcedures.procedures.getUser.inputSchema -> { id: string }
|
|
106
108
|
* // userProcedures.procedures.createUser -> mutation type
|
|
107
109
|
* ```
|
|
@@ -136,7 +138,7 @@ export interface DefineProceduresOptions {
|
|
|
136
138
|
* });
|
|
137
139
|
* ```
|
|
138
140
|
*/
|
|
139
|
-
export declare function defineProcedures<TProcedures extends ProcedureDefinitions>(namespace:
|
|
141
|
+
export declare function defineProcedures<const TNamespace extends string, TProcedures extends ProcedureDefinitions>(namespace: TNamespace, procedures: TProcedures, options?: DefineProceduresOptions): ProcedureCollection<TNamespace, InferProcedures<TProcedures>>;
|
|
140
142
|
/**
|
|
141
143
|
* Short alias for defineProcedures
|
|
142
144
|
*
|
|
@@ -303,11 +303,12 @@ function createPrecompiledMiddlewareExecutor(middlewares, handler) {
|
|
|
303
303
|
* In development mode, emits warnings for procedure names that don't follow
|
|
304
304
|
* naming conventions (which means they won't generate REST routes).
|
|
305
305
|
*
|
|
306
|
+
* @template TNamespace - The literal namespace string (inferred from argument)
|
|
306
307
|
* @template TProcedures - The record of named procedures
|
|
307
308
|
* @param namespace - Resource namespace (e.g., 'users', 'posts')
|
|
308
309
|
* @param procedures - Object containing named procedures
|
|
309
310
|
* @param options - Optional configuration for warnings
|
|
310
|
-
* @returns Procedure collection with preserved types
|
|
311
|
+
* @returns Procedure collection with preserved types including literal namespace
|
|
311
312
|
*
|
|
312
313
|
* @example
|
|
313
314
|
* ```typescript
|
|
@@ -326,6 +327,7 @@ function createPrecompiledMiddlewareExecutor(middlewares, handler) {
|
|
|
326
327
|
* });
|
|
327
328
|
*
|
|
328
329
|
* // Types are fully preserved:
|
|
330
|
+
* // userProcedures.namespace -> 'users' (literal type)
|
|
329
331
|
* // userProcedures.procedures.getUser.inputSchema -> { id: string }
|
|
330
332
|
* // userProcedures.procedures.createUser -> mutation type
|
|
331
333
|
* ```
|
package/dist/rest/routes.d.ts
CHANGED
|
@@ -7,14 +7,14 @@
|
|
|
7
7
|
*
|
|
8
8
|
* @module rest/routes
|
|
9
9
|
*/
|
|
10
|
-
import type { ProcedureCollection, ProcedureRecord } from '../types.js';
|
|
10
|
+
import type { HttpMethod, ProcedureCollection, ProcedureRecord } from '../types.js';
|
|
11
11
|
/**
|
|
12
12
|
* A single route entry with method, path, and procedure kind
|
|
13
13
|
*
|
|
14
14
|
* Matches the RouteEntry interface in @veloxts/client
|
|
15
15
|
*/
|
|
16
16
|
export interface RouteEntry {
|
|
17
|
-
method:
|
|
17
|
+
method: HttpMethod;
|
|
18
18
|
path: string;
|
|
19
19
|
kind: 'query' | 'mutation';
|
|
20
20
|
}
|
|
@@ -87,7 +87,7 @@ export declare function extractRoutes(collections: ProcedureCollection[]): Route
|
|
|
87
87
|
* ```
|
|
88
88
|
*/
|
|
89
89
|
export type ExtractRoutesType<TRouter> = {
|
|
90
|
-
[K in keyof TRouter]: TRouter[K] extends ProcedureCollection<infer P> ? ExtractNamespaceRoutes<P> : never;
|
|
90
|
+
[K in keyof TRouter]: TRouter[K] extends ProcedureCollection<infer _N, infer P> ? ExtractNamespaceRoutes<P> : never;
|
|
91
91
|
};
|
|
92
92
|
/**
|
|
93
93
|
* Helper type to extract routes from a procedure record
|
package/dist/router-utils.d.ts
CHANGED
|
@@ -7,21 +7,25 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import type { ProcedureCollection, ProcedureRecord } from './types.js';
|
|
9
9
|
/**
|
|
10
|
-
* Extracts the namespace from a ProcedureCollection
|
|
10
|
+
* Extracts the namespace literal type from a ProcedureCollection
|
|
11
|
+
*
|
|
12
|
+
* With the updated ProcedureCollection<TNamespace, TProcedures> signature,
|
|
13
|
+
* TNamespace is now a literal type (e.g., 'users', 'auth') rather than just string.
|
|
11
14
|
*/
|
|
12
|
-
type ExtractNamespace<T> = T extends ProcedureCollection<infer _P>
|
|
13
|
-
readonly namespace: infer N;
|
|
14
|
-
} ? N extends string ? N : never : never;
|
|
15
|
+
type ExtractNamespace<T> = T extends ProcedureCollection<infer N, infer _P> ? N : never;
|
|
15
16
|
/**
|
|
16
17
|
* Creates a union of namespaces from an array of ProcedureCollections
|
|
17
18
|
*/
|
|
18
|
-
type CollectionNamespaces<T extends readonly ProcedureCollection[]> = {
|
|
19
|
+
type CollectionNamespaces<T extends readonly ProcedureCollection<string, ProcedureRecord>[]> = {
|
|
19
20
|
[K in keyof T]: ExtractNamespace<T[K]>;
|
|
20
21
|
}[number];
|
|
21
22
|
/**
|
|
22
23
|
* Maps namespaces to their corresponding ProcedureCollections
|
|
24
|
+
*
|
|
25
|
+
* The Extract utility type now works correctly because TNamespace is a literal type,
|
|
26
|
+
* allowing proper narrowing from `'users' | 'auth'` to the specific `'users'` collection.
|
|
23
27
|
*/
|
|
24
|
-
type RouterFromCollections<T extends readonly ProcedureCollection[]> = {
|
|
28
|
+
type RouterFromCollections<T extends readonly ProcedureCollection<string, ProcedureRecord>[]> = {
|
|
25
29
|
[K in CollectionNamespaces<T>]: Extract<T[number], {
|
|
26
30
|
namespace: K;
|
|
27
31
|
}>;
|
|
@@ -29,7 +33,7 @@ type RouterFromCollections<T extends readonly ProcedureCollection[]> = {
|
|
|
29
33
|
/**
|
|
30
34
|
* Result type from createRouter
|
|
31
35
|
*/
|
|
32
|
-
export interface RouterResult<T extends readonly ProcedureCollection[]> {
|
|
36
|
+
export interface RouterResult<T extends readonly ProcedureCollection<string, ProcedureRecord>[]> {
|
|
33
37
|
/** Array of procedure collections for routing */
|
|
34
38
|
readonly collections: T;
|
|
35
39
|
/** Object mapping namespaces to procedure collections */
|
|
@@ -67,7 +71,7 @@ export interface RouterResult<T extends readonly ProcedureCollection[]> {
|
|
|
67
71
|
* export const routes = extractRoutes(collections);
|
|
68
72
|
* ```
|
|
69
73
|
*/
|
|
70
|
-
export declare function createRouter<T extends ProcedureCollection<ProcedureRecord>[]>(...collections: T): RouterResult<T>;
|
|
74
|
+
export declare function createRouter<T extends ProcedureCollection<string, ProcedureRecord>[]>(...collections: T): RouterResult<T>;
|
|
71
75
|
/**
|
|
72
76
|
* Creates a router object from procedure collections.
|
|
73
77
|
*
|
|
@@ -87,5 +91,5 @@ export declare function createRouter<T extends ProcedureCollection<ProcedureReco
|
|
|
87
91
|
* export type AppRouter = typeof router;
|
|
88
92
|
* ```
|
|
89
93
|
*/
|
|
90
|
-
export declare function toRouter<T extends ProcedureCollection<ProcedureRecord>[]>(...collections: T): RouterFromCollections<T>;
|
|
94
|
+
export declare function toRouter<T extends ProcedureCollection<string, ProcedureRecord>[]>(...collections: T): RouterFromCollections<T>;
|
|
91
95
|
export {};
|
package/dist/tokens.d.ts
CHANGED
|
@@ -104,4 +104,4 @@ export declare const TRPC_PLUGIN_OPTIONS: import("@veloxts/core").SymbolToken<TR
|
|
|
104
104
|
*
|
|
105
105
|
* Array of procedure collections to register with the router.
|
|
106
106
|
*/
|
|
107
|
-
export declare const PROCEDURE_COLLECTIONS: import("@veloxts/core").SymbolToken<ProcedureCollection<import("./types.js").ProcedureRecord>[]>;
|
|
107
|
+
export declare const PROCEDURE_COLLECTIONS: import("@veloxts/core").SymbolToken<ProcedureCollection<string, import("./types.js").ProcedureRecord>[]>;
|
package/dist/types.d.ts
CHANGED
|
@@ -283,11 +283,12 @@ export type ProcedureRecord = Record<string, CompiledProcedure<any, any, any>>;
|
|
|
283
283
|
*
|
|
284
284
|
* Groups related procedures under a common namespace for routing.
|
|
285
285
|
*
|
|
286
|
+
* @template TNamespace - The literal namespace string (e.g., 'users', 'posts')
|
|
286
287
|
* @template TProcedures - The record of named procedures
|
|
287
288
|
*/
|
|
288
|
-
export interface ProcedureCollection<TProcedures extends ProcedureRecord = ProcedureRecord> {
|
|
289
|
+
export interface ProcedureCollection<TNamespace extends string = string, TProcedures extends ProcedureRecord = ProcedureRecord> {
|
|
289
290
|
/** Resource namespace (e.g., 'users', 'posts') */
|
|
290
|
-
readonly namespace:
|
|
291
|
+
readonly namespace: TNamespace;
|
|
291
292
|
/** Named procedures in this collection */
|
|
292
293
|
readonly procedures: TProcedures;
|
|
293
294
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@veloxts/router",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.79",
|
|
4
4
|
"description": "Procedure definitions with tRPC and REST routing for VeloxTS framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -40,8 +40,8 @@
|
|
|
40
40
|
"@trpc/server": "11.8.0",
|
|
41
41
|
"fastify": "5.6.2",
|
|
42
42
|
"zod-to-json-schema": "3.24.5",
|
|
43
|
-
"@veloxts/core": "0.6.
|
|
44
|
-
"@veloxts/validation": "0.6.
|
|
43
|
+
"@veloxts/core": "0.6.79",
|
|
44
|
+
"@veloxts/validation": "0.6.79"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
47
|
"@vitest/coverage-v8": "4.0.16",
|
package/dist/contracts.d.ts
DELETED
|
@@ -1,180 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Contract Utilities - Browser-Safe Type Definitions
|
|
3
|
-
*
|
|
4
|
-
* These utilities enable "Great DX" by allowing developers to define
|
|
5
|
-
* type contracts in a concise, type-safe manner that's compatible
|
|
6
|
-
* with browser bundling.
|
|
7
|
-
*
|
|
8
|
-
* The key insight is that Zod schemas are browser-safe, so we can
|
|
9
|
-
* use them to define contracts without pulling in server code.
|
|
10
|
-
*
|
|
11
|
-
* @example
|
|
12
|
-
* ```typescript
|
|
13
|
-
* // schemas/user.ts - Browser-safe Zod schemas
|
|
14
|
-
* export const GetUserInput = z.object({ id: z.string() });
|
|
15
|
-
* export const UserSchema = z.object({ id: z.string(), name: z.string() });
|
|
16
|
-
*
|
|
17
|
-
* // contracts.ts - Type-safe contract definition
|
|
18
|
-
* import { defineContract } from '@veloxts/router';
|
|
19
|
-
* import { GetUserInput, UserSchema } from './schemas/user.js';
|
|
20
|
-
*
|
|
21
|
-
* export const userContracts = defineContract({
|
|
22
|
-
* getUser: { input: GetUserInput, output: UserSchema },
|
|
23
|
-
* createUser: { input: CreateUserInput, output: UserSchema },
|
|
24
|
-
* });
|
|
25
|
-
*
|
|
26
|
-
* export type AppRouter = { users: typeof userContracts };
|
|
27
|
-
* ```
|
|
28
|
-
*
|
|
29
|
-
* @module contracts
|
|
30
|
-
*/
|
|
31
|
-
import type { ZodType, ZodTypeDef } from 'zod';
|
|
32
|
-
/**
|
|
33
|
-
* Represents a single procedure's input/output contract
|
|
34
|
-
*
|
|
35
|
-
* Both input and output are optional to support:
|
|
36
|
-
* - Procedures with no input (e.g., getHealth)
|
|
37
|
-
* - Procedures with inferred output (less common)
|
|
38
|
-
*/
|
|
39
|
-
export interface ContractEntry {
|
|
40
|
-
/** Input validation schema (optional for procedures with no input) */
|
|
41
|
-
input?: ZodType<unknown, ZodTypeDef, unknown>;
|
|
42
|
-
/** Output validation schema (optional for inferred output) */
|
|
43
|
-
output?: ZodType<unknown, ZodTypeDef, unknown>;
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* A collection of procedure contracts
|
|
47
|
-
*/
|
|
48
|
-
export type ContractDefinition = Record<string, ContractEntry>;
|
|
49
|
-
/**
|
|
50
|
-
* Infers the TypeScript type from a Zod schema
|
|
51
|
-
*
|
|
52
|
-
* @template T - The Zod schema type
|
|
53
|
-
*/
|
|
54
|
-
type InferZodType<T> = T extends ZodType<infer O, ZodTypeDef, unknown> ? O : undefined;
|
|
55
|
-
/**
|
|
56
|
-
* Transforms a contract entry into its inferred types
|
|
57
|
-
*
|
|
58
|
-
* @template T - The contract entry type
|
|
59
|
-
*/
|
|
60
|
-
type InferContractEntry<T extends ContractEntry> = {
|
|
61
|
-
input: T['input'] extends ZodType ? InferZodType<T['input']> : undefined;
|
|
62
|
-
output: T['output'] extends ZodType ? InferZodType<T['output']> : undefined;
|
|
63
|
-
};
|
|
64
|
-
/**
|
|
65
|
-
* Transforms a contract definition into inferred procedure types
|
|
66
|
-
*
|
|
67
|
-
* This is the core type utility that enables automatic type inference
|
|
68
|
-
* from Zod schemas to procedure contracts.
|
|
69
|
-
*
|
|
70
|
-
* @template T - The contract definition type
|
|
71
|
-
*
|
|
72
|
-
* @example
|
|
73
|
-
* ```typescript
|
|
74
|
-
* const contracts = defineContract({
|
|
75
|
-
* getUser: { input: GetUserInput, output: UserSchema },
|
|
76
|
-
* });
|
|
77
|
-
*
|
|
78
|
-
* // InferContract<typeof contracts> = {
|
|
79
|
-
* // getUser: { input: { id: string }; output: { id: string; name: string } }
|
|
80
|
-
* // }
|
|
81
|
-
* ```
|
|
82
|
-
*/
|
|
83
|
-
export type InferContract<T extends ContractDefinition> = {
|
|
84
|
-
[K in keyof T]: InferContractEntry<T[K]>;
|
|
85
|
-
};
|
|
86
|
-
/**
|
|
87
|
-
* Infers router types from a collection of contracts
|
|
88
|
-
*
|
|
89
|
-
* @template T - Record of namespace to contract definitions
|
|
90
|
-
*
|
|
91
|
-
* @example
|
|
92
|
-
* ```typescript
|
|
93
|
-
* export type AppRouter = InferRouterFromContracts<{
|
|
94
|
-
* users: typeof userContracts;
|
|
95
|
-
* auth: typeof authContracts;
|
|
96
|
-
* }>;
|
|
97
|
-
* ```
|
|
98
|
-
*/
|
|
99
|
-
export type InferRouterFromContracts<T extends Record<string, ContractDefinition>> = {
|
|
100
|
-
[K in keyof T]: InferContract<T[K]>;
|
|
101
|
-
};
|
|
102
|
-
/**
|
|
103
|
-
* Defines a type-safe procedure contract from Zod schemas
|
|
104
|
-
*
|
|
105
|
-
* This function provides compile-time validation and autocomplete while
|
|
106
|
-
* returning the contract definition for type inference. The runtime value
|
|
107
|
-
* is just passed through - the magic is all in the types.
|
|
108
|
-
*
|
|
109
|
-
* **Why use defineContract?**
|
|
110
|
-
* - Automatic type inference from Zod schemas
|
|
111
|
-
* - TypeScript validates schema existence at compile time
|
|
112
|
-
* - Autocomplete for schema imports
|
|
113
|
-
* - One-line per procedure (vs 5+ lines with manual types)
|
|
114
|
-
* - Browser-safe - only imports Zod schemas, not server code
|
|
115
|
-
*
|
|
116
|
-
* @template T - The contract definition type (inferred)
|
|
117
|
-
* @param contracts - Object mapping procedure names to their schemas
|
|
118
|
-
* @returns The same object, typed for inference
|
|
119
|
-
*
|
|
120
|
-
* @example
|
|
121
|
-
* ```typescript
|
|
122
|
-
* // Define contracts alongside your schemas
|
|
123
|
-
* import { GetUserInput, UserSchema, CreateUserInput } from './schemas/user.js';
|
|
124
|
-
*
|
|
125
|
-
* export const userContracts = defineContract({
|
|
126
|
-
* getUser: { input: GetUserInput, output: UserSchema },
|
|
127
|
-
* listUsers: { output: z.array(UserSchema) },
|
|
128
|
-
* createUser: { input: CreateUserInput, output: UserSchema },
|
|
129
|
-
* deleteUser: { input: z.object({ id: z.string() }), output: z.object({ success: z.boolean() }) },
|
|
130
|
-
* });
|
|
131
|
-
*
|
|
132
|
-
* // Use in AppRouter type
|
|
133
|
-
* export type AppRouter = {
|
|
134
|
-
* users: typeof userContracts;
|
|
135
|
-
* auth: typeof authContracts;
|
|
136
|
-
* };
|
|
137
|
-
* ```
|
|
138
|
-
*/
|
|
139
|
-
export declare function defineContract<T extends ContractDefinition>(contracts: T): T;
|
|
140
|
-
/**
|
|
141
|
-
* HTTP method type
|
|
142
|
-
*/
|
|
143
|
-
export type HttpMethodRoute = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
144
|
-
/**
|
|
145
|
-
* A single route entry
|
|
146
|
-
*/
|
|
147
|
-
export interface RouteDefinition {
|
|
148
|
-
method: HttpMethodRoute;
|
|
149
|
-
path: string;
|
|
150
|
-
}
|
|
151
|
-
/**
|
|
152
|
-
* Collection of route definitions for a namespace
|
|
153
|
-
*/
|
|
154
|
-
export type RoutesDefinition = Record<string, RouteDefinition>;
|
|
155
|
-
/**
|
|
156
|
-
* Defines route mappings for frontend client
|
|
157
|
-
*
|
|
158
|
-
* This helper provides type safety and autocomplete for route definitions,
|
|
159
|
-
* making it easier to maintain route mappings that match your procedures.
|
|
160
|
-
*
|
|
161
|
-
* @template T - The routes definition type (inferred)
|
|
162
|
-
* @param routes - Object mapping procedure names to their routes
|
|
163
|
-
* @returns The same object, typed for client consumption
|
|
164
|
-
*
|
|
165
|
-
* @example
|
|
166
|
-
* ```typescript
|
|
167
|
-
* export const userRoutes = defineRoutes({
|
|
168
|
-
* getUser: { method: 'GET', path: '/users/:id' },
|
|
169
|
-
* listUsers: { method: 'GET', path: '/users' },
|
|
170
|
-
* createUser: { method: 'POST', path: '/users' },
|
|
171
|
-
* });
|
|
172
|
-
*
|
|
173
|
-
* export const routes = {
|
|
174
|
-
* users: userRoutes,
|
|
175
|
-
* auth: authRoutes,
|
|
176
|
-
* } as const;
|
|
177
|
-
* ```
|
|
178
|
-
*/
|
|
179
|
-
export declare function defineRoutes<T extends RoutesDefinition>(routes: T): T;
|
|
180
|
-
export {};
|
package/dist/contracts.js
DELETED
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Contract Utilities - Browser-Safe Type Definitions
|
|
3
|
-
*
|
|
4
|
-
* These utilities enable "Great DX" by allowing developers to define
|
|
5
|
-
* type contracts in a concise, type-safe manner that's compatible
|
|
6
|
-
* with browser bundling.
|
|
7
|
-
*
|
|
8
|
-
* The key insight is that Zod schemas are browser-safe, so we can
|
|
9
|
-
* use them to define contracts without pulling in server code.
|
|
10
|
-
*
|
|
11
|
-
* @example
|
|
12
|
-
* ```typescript
|
|
13
|
-
* // schemas/user.ts - Browser-safe Zod schemas
|
|
14
|
-
* export const GetUserInput = z.object({ id: z.string() });
|
|
15
|
-
* export const UserSchema = z.object({ id: z.string(), name: z.string() });
|
|
16
|
-
*
|
|
17
|
-
* // contracts.ts - Type-safe contract definition
|
|
18
|
-
* import { defineContract } from '@veloxts/router';
|
|
19
|
-
* import { GetUserInput, UserSchema } from './schemas/user.js';
|
|
20
|
-
*
|
|
21
|
-
* export const userContracts = defineContract({
|
|
22
|
-
* getUser: { input: GetUserInput, output: UserSchema },
|
|
23
|
-
* createUser: { input: CreateUserInput, output: UserSchema },
|
|
24
|
-
* });
|
|
25
|
-
*
|
|
26
|
-
* export type AppRouter = { users: typeof userContracts };
|
|
27
|
-
* ```
|
|
28
|
-
*
|
|
29
|
-
* @module contracts
|
|
30
|
-
*/
|
|
31
|
-
// ============================================================================
|
|
32
|
-
// Contract Definition Helper
|
|
33
|
-
// ============================================================================
|
|
34
|
-
/**
|
|
35
|
-
* Defines a type-safe procedure contract from Zod schemas
|
|
36
|
-
*
|
|
37
|
-
* This function provides compile-time validation and autocomplete while
|
|
38
|
-
* returning the contract definition for type inference. The runtime value
|
|
39
|
-
* is just passed through - the magic is all in the types.
|
|
40
|
-
*
|
|
41
|
-
* **Why use defineContract?**
|
|
42
|
-
* - Automatic type inference from Zod schemas
|
|
43
|
-
* - TypeScript validates schema existence at compile time
|
|
44
|
-
* - Autocomplete for schema imports
|
|
45
|
-
* - One-line per procedure (vs 5+ lines with manual types)
|
|
46
|
-
* - Browser-safe - only imports Zod schemas, not server code
|
|
47
|
-
*
|
|
48
|
-
* @template T - The contract definition type (inferred)
|
|
49
|
-
* @param contracts - Object mapping procedure names to their schemas
|
|
50
|
-
* @returns The same object, typed for inference
|
|
51
|
-
*
|
|
52
|
-
* @example
|
|
53
|
-
* ```typescript
|
|
54
|
-
* // Define contracts alongside your schemas
|
|
55
|
-
* import { GetUserInput, UserSchema, CreateUserInput } from './schemas/user.js';
|
|
56
|
-
*
|
|
57
|
-
* export const userContracts = defineContract({
|
|
58
|
-
* getUser: { input: GetUserInput, output: UserSchema },
|
|
59
|
-
* listUsers: { output: z.array(UserSchema) },
|
|
60
|
-
* createUser: { input: CreateUserInput, output: UserSchema },
|
|
61
|
-
* deleteUser: { input: z.object({ id: z.string() }), output: z.object({ success: z.boolean() }) },
|
|
62
|
-
* });
|
|
63
|
-
*
|
|
64
|
-
* // Use in AppRouter type
|
|
65
|
-
* export type AppRouter = {
|
|
66
|
-
* users: typeof userContracts;
|
|
67
|
-
* auth: typeof authContracts;
|
|
68
|
-
* };
|
|
69
|
-
* ```
|
|
70
|
-
*/
|
|
71
|
-
export function defineContract(contracts) {
|
|
72
|
-
return contracts;
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* Defines route mappings for frontend client
|
|
76
|
-
*
|
|
77
|
-
* This helper provides type safety and autocomplete for route definitions,
|
|
78
|
-
* making it easier to maintain route mappings that match your procedures.
|
|
79
|
-
*
|
|
80
|
-
* @template T - The routes definition type (inferred)
|
|
81
|
-
* @param routes - Object mapping procedure names to their routes
|
|
82
|
-
* @returns The same object, typed for client consumption
|
|
83
|
-
*
|
|
84
|
-
* @example
|
|
85
|
-
* ```typescript
|
|
86
|
-
* export const userRoutes = defineRoutes({
|
|
87
|
-
* getUser: { method: 'GET', path: '/users/:id' },
|
|
88
|
-
* listUsers: { method: 'GET', path: '/users' },
|
|
89
|
-
* createUser: { method: 'POST', path: '/users' },
|
|
90
|
-
* });
|
|
91
|
-
*
|
|
92
|
-
* export const routes = {
|
|
93
|
-
* users: userRoutes,
|
|
94
|
-
* auth: authRoutes,
|
|
95
|
-
* } as const;
|
|
96
|
-
* ```
|
|
97
|
-
*/
|
|
98
|
-
export function defineRoutes(routes) {
|
|
99
|
-
return routes;
|
|
100
|
-
}
|