@veloxts/router 0.2.0 → 0.2.2
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.ts +49 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +55 -0
- package/dist/index.js.map +1 -0
- package/dist/procedure/builder.d.ts +106 -0
- package/dist/procedure/builder.d.ts.map +1 -0
- package/dist/procedure/builder.js +341 -0
- package/dist/procedure/builder.js.map +1 -0
- package/dist/procedure/types.d.ts +234 -0
- package/dist/procedure/types.d.ts.map +1 -0
- package/dist/procedure/types.js +13 -0
- package/dist/procedure/types.js.map +1 -0
- package/dist/rest/adapter.d.ts +124 -0
- package/dist/rest/adapter.d.ts.map +1 -0
- package/dist/rest/adapter.js +270 -0
- package/dist/rest/adapter.js.map +1 -0
- package/dist/rest/index.d.ts +10 -0
- package/dist/rest/index.d.ts.map +1 -0
- package/dist/rest/index.js +9 -0
- package/dist/rest/index.js.map +1 -0
- package/dist/rest/naming.d.ts +83 -0
- package/dist/rest/naming.d.ts.map +1 -0
- package/dist/rest/naming.js +164 -0
- package/dist/rest/naming.js.map +1 -0
- package/dist/trpc/adapter.d.ts +182 -0
- package/dist/trpc/adapter.d.ts.map +1 -0
- package/dist/trpc/adapter.js +313 -0
- package/dist/trpc/adapter.js.map +1 -0
- package/dist/trpc/index.d.ts +8 -0
- package/dist/trpc/index.d.ts.map +1 -0
- package/dist/trpc/index.js +7 -0
- package/dist/trpc/index.js.map +1 -0
- package/dist/types.d.ts +240 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +31 -0
- package/dist/types.js.map +1 -0
- package/package.json +3 -3
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* REST adapter exports
|
|
3
|
+
*
|
|
4
|
+
* @module rest
|
|
5
|
+
*/
|
|
6
|
+
// REST adapter - public API
|
|
7
|
+
export { createRoutesRegistrar, generateRestRoutes, getRouteSummary, registerRestRoutes, } from './adapter.js';
|
|
8
|
+
export { buildRestPath, followsNamingConvention, inferResourceName, parseNamingConvention, } from './naming.js';
|
|
9
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/rest/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,4BAA4B;AAC5B,OAAO,EACL,qBAAqB,EACrB,kBAAkB,EAClB,eAAe,EACf,kBAAkB,GACnB,MAAM,cAAc,CAAC;AAGtB,OAAO,EACL,aAAa,EACb,uBAAuB,EACvB,iBAAiB,EACjB,qBAAqB,GACtB,MAAM,aAAa,CAAC"}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* REST naming convention parser
|
|
3
|
+
*
|
|
4
|
+
* Parses procedure names to infer HTTP methods and paths following
|
|
5
|
+
* convention-over-configuration principles.
|
|
6
|
+
*
|
|
7
|
+
* @module rest/naming
|
|
8
|
+
*/
|
|
9
|
+
import type { HttpMethod, ProcedureType } from '../types.js';
|
|
10
|
+
/**
|
|
11
|
+
* Result of parsing a procedure name into REST route info
|
|
12
|
+
*/
|
|
13
|
+
export interface RestMapping {
|
|
14
|
+
/** HTTP method inferred from naming convention */
|
|
15
|
+
readonly method: HttpMethod;
|
|
16
|
+
/** Path pattern (e.g., '/', '/:id') */
|
|
17
|
+
readonly path: string;
|
|
18
|
+
/** Whether the path includes an :id parameter */
|
|
19
|
+
readonly hasIdParam: boolean;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Parse a procedure name into REST mapping using naming conventions
|
|
23
|
+
*
|
|
24
|
+
* @param name - Procedure name (e.g., 'getUser', 'listUsers', 'createUser')
|
|
25
|
+
* @param type - Procedure type ('query' or 'mutation')
|
|
26
|
+
* @returns REST mapping if convention matches, undefined otherwise
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* parseNamingConvention('getUser', 'query')
|
|
31
|
+
* // Returns: { method: 'GET', path: '/:id', hasIdParam: true }
|
|
32
|
+
*
|
|
33
|
+
* parseNamingConvention('listUsers', 'query')
|
|
34
|
+
* // Returns: { method: 'GET', path: '/', hasIdParam: false }
|
|
35
|
+
*
|
|
36
|
+
* parseNamingConvention('createUser', 'mutation')
|
|
37
|
+
* // Returns: { method: 'POST', path: '/', hasIdParam: false }
|
|
38
|
+
*
|
|
39
|
+
* parseNamingConvention('doSomething', 'mutation')
|
|
40
|
+
* // Returns: undefined (no convention matches)
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
export declare function parseNamingConvention(name: string, type: ProcedureType): RestMapping | undefined;
|
|
44
|
+
/**
|
|
45
|
+
* Build the full REST path from namespace and mapping
|
|
46
|
+
*
|
|
47
|
+
* @param namespace - Resource namespace (e.g., 'users')
|
|
48
|
+
* @param mapping - REST mapping from parseNamingConvention
|
|
49
|
+
* @returns Full path (e.g., '/users/:id', '/users')
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```typescript
|
|
53
|
+
* buildRestPath('users', { method: 'GET', path: '/:id', hasIdParam: true })
|
|
54
|
+
* // Returns: '/users/:id'
|
|
55
|
+
*
|
|
56
|
+
* buildRestPath('users', { method: 'GET', path: '/', hasIdParam: false })
|
|
57
|
+
* // Returns: '/users'
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
export declare function buildRestPath(namespace: string, mapping: RestMapping): string;
|
|
61
|
+
/**
|
|
62
|
+
* Infer the resource name from a procedure name
|
|
63
|
+
*
|
|
64
|
+
* @param name - Procedure name (e.g., 'getUser', 'listUsers')
|
|
65
|
+
* @returns Resource name or undefined if cannot be inferred
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```typescript
|
|
69
|
+
* inferResourceName('getUser') // 'User'
|
|
70
|
+
* inferResourceName('listUsers') // 'Users'
|
|
71
|
+
* inferResourceName('doSomething') // undefined
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
export declare function inferResourceName(name: string): string | undefined;
|
|
75
|
+
/**
|
|
76
|
+
* Check if a procedure name follows any known naming convention
|
|
77
|
+
*
|
|
78
|
+
* @param name - Procedure name to check
|
|
79
|
+
* @param type - Procedure type
|
|
80
|
+
* @returns true if the name follows a convention
|
|
81
|
+
*/
|
|
82
|
+
export declare function followsNamingConvention(name: string, type: ProcedureType): boolean;
|
|
83
|
+
//# sourceMappingURL=naming.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"naming.d.ts","sourceRoot":"","sources":["../../src/rest/naming.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAM7D;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,kDAAkD;IAClD,QAAQ,CAAC,MAAM,EAAE,UAAU,CAAC;IAC5B,uCAAuC;IACvC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,iDAAiD;IACjD,QAAQ,CAAC,UAAU,EAAE,OAAO,CAAC;CAC9B;AA4ED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,aAAa,GAAG,WAAW,GAAG,SAAS,CAoBhG;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,aAAa,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,WAAW,GAAG,MAAM,CAU7E;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAQlE;AAED;;;;;;GAMG;AACH,wBAAgB,uBAAuB,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,aAAa,GAAG,OAAO,CAElF"}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* REST naming convention parser
|
|
3
|
+
*
|
|
4
|
+
* Parses procedure names to infer HTTP methods and paths following
|
|
5
|
+
* convention-over-configuration principles.
|
|
6
|
+
*
|
|
7
|
+
* @module rest/naming
|
|
8
|
+
*/
|
|
9
|
+
// ============================================================================
|
|
10
|
+
// Naming Patterns
|
|
11
|
+
// ============================================================================
|
|
12
|
+
/**
|
|
13
|
+
* MVP (v0.1.0) naming patterns - GET and POST only
|
|
14
|
+
*
|
|
15
|
+
* Pattern matching is done by prefix:
|
|
16
|
+
* - get<Resource> -> GET /:id (single resource)
|
|
17
|
+
* - list<Resources> -> GET / (collection)
|
|
18
|
+
* - find<Resource> -> GET / (search/filter)
|
|
19
|
+
* - create<Resource> -> POST / (create new)
|
|
20
|
+
* - add<Resource> -> POST / (alias for create)
|
|
21
|
+
*
|
|
22
|
+
* v1.1+ will add:
|
|
23
|
+
* - update<Resource> -> PUT /:id
|
|
24
|
+
* - delete<Resource> -> DELETE /:id
|
|
25
|
+
*/
|
|
26
|
+
const MVP_NAMING_PATTERNS = [
|
|
27
|
+
// GET with ID - single resource retrieval
|
|
28
|
+
{
|
|
29
|
+
pattern: /^get([A-Z][a-zA-Z]*)$/,
|
|
30
|
+
method: 'GET',
|
|
31
|
+
hasIdParam: true,
|
|
32
|
+
procedureType: 'query',
|
|
33
|
+
},
|
|
34
|
+
// GET without ID - list/collection
|
|
35
|
+
{
|
|
36
|
+
pattern: /^list([A-Z][a-zA-Z]*)$/,
|
|
37
|
+
method: 'GET',
|
|
38
|
+
hasIdParam: false,
|
|
39
|
+
procedureType: 'query',
|
|
40
|
+
},
|
|
41
|
+
// GET without ID - search/find
|
|
42
|
+
{
|
|
43
|
+
pattern: /^find([A-Z][a-zA-Z]*)$/,
|
|
44
|
+
method: 'GET',
|
|
45
|
+
hasIdParam: false,
|
|
46
|
+
procedureType: 'query',
|
|
47
|
+
},
|
|
48
|
+
// POST - create resource
|
|
49
|
+
{
|
|
50
|
+
pattern: /^create([A-Z][a-zA-Z]*)$/,
|
|
51
|
+
method: 'POST',
|
|
52
|
+
hasIdParam: false,
|
|
53
|
+
procedureType: 'mutation',
|
|
54
|
+
},
|
|
55
|
+
// POST - add resource (alias)
|
|
56
|
+
{
|
|
57
|
+
pattern: /^add([A-Z][a-zA-Z]*)$/,
|
|
58
|
+
method: 'POST',
|
|
59
|
+
hasIdParam: false,
|
|
60
|
+
procedureType: 'mutation',
|
|
61
|
+
},
|
|
62
|
+
];
|
|
63
|
+
// ============================================================================
|
|
64
|
+
// Parsing Functions
|
|
65
|
+
// ============================================================================
|
|
66
|
+
/**
|
|
67
|
+
* Parse a procedure name into REST mapping using naming conventions
|
|
68
|
+
*
|
|
69
|
+
* @param name - Procedure name (e.g., 'getUser', 'listUsers', 'createUser')
|
|
70
|
+
* @param type - Procedure type ('query' or 'mutation')
|
|
71
|
+
* @returns REST mapping if convention matches, undefined otherwise
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```typescript
|
|
75
|
+
* parseNamingConvention('getUser', 'query')
|
|
76
|
+
* // Returns: { method: 'GET', path: '/:id', hasIdParam: true }
|
|
77
|
+
*
|
|
78
|
+
* parseNamingConvention('listUsers', 'query')
|
|
79
|
+
* // Returns: { method: 'GET', path: '/', hasIdParam: false }
|
|
80
|
+
*
|
|
81
|
+
* parseNamingConvention('createUser', 'mutation')
|
|
82
|
+
* // Returns: { method: 'POST', path: '/', hasIdParam: false }
|
|
83
|
+
*
|
|
84
|
+
* parseNamingConvention('doSomething', 'mutation')
|
|
85
|
+
* // Returns: undefined (no convention matches)
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
88
|
+
export function parseNamingConvention(name, type) {
|
|
89
|
+
for (const pattern of MVP_NAMING_PATTERNS) {
|
|
90
|
+
// Check if procedure type matches
|
|
91
|
+
if (pattern.procedureType !== type) {
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
// Check if name matches pattern
|
|
95
|
+
const match = pattern.pattern.exec(name);
|
|
96
|
+
if (match) {
|
|
97
|
+
return {
|
|
98
|
+
method: pattern.method,
|
|
99
|
+
path: pattern.hasIdParam ? '/:id' : '/',
|
|
100
|
+
hasIdParam: pattern.hasIdParam,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
// No convention matched
|
|
105
|
+
return undefined;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Build the full REST path from namespace and mapping
|
|
109
|
+
*
|
|
110
|
+
* @param namespace - Resource namespace (e.g., 'users')
|
|
111
|
+
* @param mapping - REST mapping from parseNamingConvention
|
|
112
|
+
* @returns Full path (e.g., '/users/:id', '/users')
|
|
113
|
+
*
|
|
114
|
+
* @example
|
|
115
|
+
* ```typescript
|
|
116
|
+
* buildRestPath('users', { method: 'GET', path: '/:id', hasIdParam: true })
|
|
117
|
+
* // Returns: '/users/:id'
|
|
118
|
+
*
|
|
119
|
+
* buildRestPath('users', { method: 'GET', path: '/', hasIdParam: false })
|
|
120
|
+
* // Returns: '/users'
|
|
121
|
+
* ```
|
|
122
|
+
*/
|
|
123
|
+
export function buildRestPath(namespace, mapping) {
|
|
124
|
+
const basePath = `/${namespace}`;
|
|
125
|
+
// If path is just '/', return the base path without trailing slash
|
|
126
|
+
if (mapping.path === '/') {
|
|
127
|
+
return basePath;
|
|
128
|
+
}
|
|
129
|
+
// Otherwise append the path (e.g., '/:id')
|
|
130
|
+
return `${basePath}${mapping.path}`;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Infer the resource name from a procedure name
|
|
134
|
+
*
|
|
135
|
+
* @param name - Procedure name (e.g., 'getUser', 'listUsers')
|
|
136
|
+
* @returns Resource name or undefined if cannot be inferred
|
|
137
|
+
*
|
|
138
|
+
* @example
|
|
139
|
+
* ```typescript
|
|
140
|
+
* inferResourceName('getUser') // 'User'
|
|
141
|
+
* inferResourceName('listUsers') // 'Users'
|
|
142
|
+
* inferResourceName('doSomething') // undefined
|
|
143
|
+
* ```
|
|
144
|
+
*/
|
|
145
|
+
export function inferResourceName(name) {
|
|
146
|
+
for (const pattern of MVP_NAMING_PATTERNS) {
|
|
147
|
+
const match = pattern.pattern.exec(name);
|
|
148
|
+
if (match) {
|
|
149
|
+
return match[1];
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return undefined;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Check if a procedure name follows any known naming convention
|
|
156
|
+
*
|
|
157
|
+
* @param name - Procedure name to check
|
|
158
|
+
* @param type - Procedure type
|
|
159
|
+
* @returns true if the name follows a convention
|
|
160
|
+
*/
|
|
161
|
+
export function followsNamingConvention(name, type) {
|
|
162
|
+
return parseNamingConvention(name, type) !== undefined;
|
|
163
|
+
}
|
|
164
|
+
//# sourceMappingURL=naming.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"naming.js","sourceRoot":"","sources":["../../src/rest/naming.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAkCH,+EAA+E;AAC/E,kBAAkB;AAClB,+EAA+E;AAE/E;;;;;;;;;;;;;GAaG;AACH,MAAM,mBAAmB,GAA6B;IACpD,0CAA0C;IAC1C;QACE,OAAO,EAAE,uBAAuB;QAChC,MAAM,EAAE,KAAK;QACb,UAAU,EAAE,IAAI;QAChB,aAAa,EAAE,OAAO;KACvB;IACD,mCAAmC;IACnC;QACE,OAAO,EAAE,wBAAwB;QACjC,MAAM,EAAE,KAAK;QACb,UAAU,EAAE,KAAK;QACjB,aAAa,EAAE,OAAO;KACvB;IACD,+BAA+B;IAC/B;QACE,OAAO,EAAE,wBAAwB;QACjC,MAAM,EAAE,KAAK;QACb,UAAU,EAAE,KAAK;QACjB,aAAa,EAAE,OAAO;KACvB;IACD,yBAAyB;IACzB;QACE,OAAO,EAAE,0BAA0B;QACnC,MAAM,EAAE,MAAM;QACd,UAAU,EAAE,KAAK;QACjB,aAAa,EAAE,UAAU;KAC1B;IACD,8BAA8B;IAC9B;QACE,OAAO,EAAE,uBAAuB;QAChC,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IAAI,CAAC,CAAC;QACzC,IAAI,KAAK,EAAE,CAAC;YACV,OAAO,KAAK,CAAC,CAAC,CAAC,CAAC;QAClB,CAAC;IACH,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,uBAAuB,CAAC,IAAY,EAAE,IAAmB;IACvE,OAAO,qBAAqB,CAAC,IAAI,EAAE,IAAI,CAAC,KAAK,SAAS,CAAC;AACzD,CAAC"}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* tRPC adapter for procedure collections
|
|
3
|
+
*
|
|
4
|
+
* Converts VeloxTS procedure definitions into tRPC routers, enabling type-safe
|
|
5
|
+
* API calls between frontend and backend.
|
|
6
|
+
*
|
|
7
|
+
* @module trpc/adapter
|
|
8
|
+
*/
|
|
9
|
+
import type { AnyRouter as TRPCAnyRouter } from '@trpc/server';
|
|
10
|
+
import { TRPCError } from '@trpc/server';
|
|
11
|
+
import type { BaseContext } from '@veloxts/core';
|
|
12
|
+
import type { FastifyInstance } from 'fastify';
|
|
13
|
+
/**
|
|
14
|
+
* Re-exported AnyRouter type from tRPC
|
|
15
|
+
*
|
|
16
|
+
* This allows consumers to use AnyRouter without directly importing @trpc/server,
|
|
17
|
+
* which helps avoid TypeScript compilation memory issues with tRPC v11.7+.
|
|
18
|
+
*/
|
|
19
|
+
export type AnyRouter = TRPCAnyRouter;
|
|
20
|
+
import type { ProcedureCollection } from '../types.js';
|
|
21
|
+
declare const baseTRPC: import("@trpc/server").TRPCRootObject<BaseContext, object, import("@trpc/server").TRPCRuntimeConfigOptions<BaseContext, object>, {
|
|
22
|
+
ctx: BaseContext;
|
|
23
|
+
meta: object;
|
|
24
|
+
errorShape: import("@trpc/server").TRPCDefaultErrorShape;
|
|
25
|
+
transformer: false;
|
|
26
|
+
}>;
|
|
27
|
+
/**
|
|
28
|
+
* Type for a created tRPC instance
|
|
29
|
+
*
|
|
30
|
+
* Using typeof on a concrete instance avoids the "cannot be named" error
|
|
31
|
+
* that occurs with generic type inference in tRPC v11.7+
|
|
32
|
+
*/
|
|
33
|
+
export type TRPCInstance<_TContext extends BaseContext = BaseContext> = typeof baseTRPC;
|
|
34
|
+
/**
|
|
35
|
+
* Create a tRPC instance with VeloxTS context
|
|
36
|
+
*
|
|
37
|
+
* This initializes tRPC with the BaseContext type, allowing procedures
|
|
38
|
+
* to access request context and plugin-provided features.
|
|
39
|
+
*
|
|
40
|
+
* @returns tRPC instance with context
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```typescript
|
|
44
|
+
* const t = createTRPC();
|
|
45
|
+
*
|
|
46
|
+
* const router = t.router({
|
|
47
|
+
* hello: t.procedure.query(() => 'Hello World'),
|
|
48
|
+
* });
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export declare function createTRPC(): TRPCInstance;
|
|
52
|
+
/**
|
|
53
|
+
* Build a tRPC router from a single procedure collection
|
|
54
|
+
*
|
|
55
|
+
* Converts all procedures in a collection to tRPC procedures,
|
|
56
|
+
* preserving type information for client inference.
|
|
57
|
+
*
|
|
58
|
+
* @param t - tRPC instance
|
|
59
|
+
* @param collection - Procedure collection to convert
|
|
60
|
+
* @returns tRPC router
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```typescript
|
|
64
|
+
* const t = createTRPC();
|
|
65
|
+
* const userRouter = buildTRPCRouter(t, userProcedures);
|
|
66
|
+
*
|
|
67
|
+
* // Router has typed procedures:
|
|
68
|
+
* // userRouter.getUser({ id: '123' })
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
export declare function buildTRPCRouter(t: TRPCInstance<BaseContext>, collection: ProcedureCollection): AnyRouter;
|
|
72
|
+
/**
|
|
73
|
+
* Create a namespaced app router from multiple procedure collections
|
|
74
|
+
*
|
|
75
|
+
* Each collection becomes a nested router under its namespace.
|
|
76
|
+
*
|
|
77
|
+
* @param t - tRPC instance
|
|
78
|
+
* @param collections - Array of procedure collections
|
|
79
|
+
* @returns Merged app router
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```typescript
|
|
83
|
+
* const t = createTRPC();
|
|
84
|
+
* const appRouter = createAppRouter(t, [
|
|
85
|
+
* userProcedures, // namespace: 'users'
|
|
86
|
+
* postProcedures, // namespace: 'posts'
|
|
87
|
+
* ]);
|
|
88
|
+
*
|
|
89
|
+
* // Usage:
|
|
90
|
+
* // appRouter.users.getUser({ id: '123' })
|
|
91
|
+
* // appRouter.posts.listPosts({ page: 1 })
|
|
92
|
+
*
|
|
93
|
+
* // Export type for client
|
|
94
|
+
* export type AppRouter = typeof appRouter;
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
export declare function createAppRouter(t: TRPCInstance<BaseContext>, collections: ProcedureCollection[]): AnyRouter;
|
|
98
|
+
/**
|
|
99
|
+
* Helper type to infer the AppRouter type
|
|
100
|
+
*/
|
|
101
|
+
export type InferAppRouter = AnyRouter;
|
|
102
|
+
/**
|
|
103
|
+
* Create a tRPC context factory for Fastify
|
|
104
|
+
*
|
|
105
|
+
* This factory creates the context for each tRPC request,
|
|
106
|
+
* pulling from the Fastify request's decorated context.
|
|
107
|
+
*
|
|
108
|
+
* @template TContext - Context type
|
|
109
|
+
* @returns Context factory function
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* ```typescript
|
|
113
|
+
* import { fastifyTRPCPlugin } from '@trpc/server/adapters/fastify';
|
|
114
|
+
*
|
|
115
|
+
* await server.register(fastifyTRPCPlugin, {
|
|
116
|
+
* prefix: '/trpc',
|
|
117
|
+
* trpcOptions: {
|
|
118
|
+
* router: appRouter,
|
|
119
|
+
* createContext: createTRPCContextFactory(),
|
|
120
|
+
* },
|
|
121
|
+
* });
|
|
122
|
+
* ```
|
|
123
|
+
*/
|
|
124
|
+
export declare function createTRPCContextFactory(): ({ req }: {
|
|
125
|
+
req: {
|
|
126
|
+
context?: BaseContext;
|
|
127
|
+
};
|
|
128
|
+
}) => BaseContext;
|
|
129
|
+
/**
|
|
130
|
+
* Convert a VeloxTS error to a tRPC error
|
|
131
|
+
*
|
|
132
|
+
* Maps VeloxTS error codes to appropriate tRPC error codes.
|
|
133
|
+
*
|
|
134
|
+
* @param error - Error to convert
|
|
135
|
+
* @param defaultCode - Default tRPC code if mapping not found
|
|
136
|
+
* @returns TRPCError
|
|
137
|
+
*/
|
|
138
|
+
export declare function veloxErrorToTRPCError(error: Error & {
|
|
139
|
+
statusCode?: number;
|
|
140
|
+
code?: string;
|
|
141
|
+
}, defaultCode?: TRPCError['code']): TRPCError;
|
|
142
|
+
/**
|
|
143
|
+
* Type guard for tRPC errors with VeloxTS cause
|
|
144
|
+
*
|
|
145
|
+
* Use this when handling errors that may have been converted from VeloxTS errors
|
|
146
|
+
* using veloxErrorToTRPCError().
|
|
147
|
+
*/
|
|
148
|
+
export declare function isVeloxTRPCError(error: TRPCError): error is TRPCError & {
|
|
149
|
+
cause: string;
|
|
150
|
+
};
|
|
151
|
+
/**
|
|
152
|
+
* Options for tRPC plugin registration
|
|
153
|
+
*/
|
|
154
|
+
export interface TRPCPluginOptions {
|
|
155
|
+
/** URL prefix for tRPC routes (default: '/trpc') */
|
|
156
|
+
prefix?: string;
|
|
157
|
+
/** tRPC router created with createAppRouter */
|
|
158
|
+
router: AnyRouter;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Register tRPC plugin with Fastify server
|
|
162
|
+
*
|
|
163
|
+
* This is a convenience wrapper around fastifyTRPCPlugin that handles
|
|
164
|
+
* the context factory automatically.
|
|
165
|
+
*
|
|
166
|
+
* @param server - Fastify server instance
|
|
167
|
+
* @param options - tRPC plugin options
|
|
168
|
+
*
|
|
169
|
+
* @example
|
|
170
|
+
* ```typescript
|
|
171
|
+
* const app = await createVeloxApp({ port: 3210 });
|
|
172
|
+
* const appRouter = createAppRouter(t, [userProcedures]);
|
|
173
|
+
*
|
|
174
|
+
* await registerTRPCPlugin(app.server, {
|
|
175
|
+
* prefix: '/trpc',
|
|
176
|
+
* router: appRouter,
|
|
177
|
+
* });
|
|
178
|
+
* ```
|
|
179
|
+
*/
|
|
180
|
+
export declare function registerTRPCPlugin(server: FastifyInstance, options: TRPCPluginOptions): Promise<void>;
|
|
181
|
+
export {};
|
|
182
|
+
//# sourceMappingURL=adapter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../../src/trpc/adapter.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,SAAS,IAAI,aAAa,EAAE,MAAM,cAAc,CAAC;AAC/D,OAAO,EAAY,SAAS,EAAE,MAAM,cAAc,CAAC;AACnD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AACjD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAE/C;;;;;GAKG;AACH,MAAM,MAAM,SAAS,GAAG,aAAa,CAAC;AAEtC,OAAO,KAAK,EAAqB,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAQ1E,QAAA,MAAM,QAAQ;;;;;EAA2C,CAAC;AAE1D;;;;;GAKG;AACH,MAAM,MAAM,YAAY,CAAC,SAAS,SAAS,WAAW,GAAG,WAAW,IAAI,OAAO,QAAQ,CAAC;AAExF;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,UAAU,IAAI,YAAY,CAIzC;AAMD;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,eAAe,CAC7B,CAAC,EAAE,YAAY,CAAC,WAAW,CAAC,EAC5B,UAAU,EAAE,mBAAmB,GAC9B,SAAS,CAWX;AA2ID;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,eAAe,CAC7B,CAAC,EAAE,YAAY,CAAC,WAAW,CAAC,EAC5B,WAAW,EAAE,mBAAmB,EAAE,GACjC,SAAS,CAQX;AAED;;GAEG;AACH,MAAM,MA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