@veloxts/router 0.3.4 → 0.3.6
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/errors.d.ts +89 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +96 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +4 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -5
- package/dist/index.js.map +1 -1
- package/dist/procedure/builder.d.ts.map +1 -1
- package/dist/procedure/builder.js +118 -4
- package/dist/procedure/builder.js.map +1 -1
- package/dist/procedure/types.d.ts +67 -1
- package/dist/procedure/types.d.ts.map +1 -1
- package/dist/rest/adapter.d.ts +74 -11
- package/dist/rest/adapter.d.ts.map +1 -1
- package/dist/rest/adapter.js +124 -22
- package/dist/rest/adapter.js.map +1 -1
- package/dist/rest/index.d.ts +3 -3
- package/dist/rest/index.d.ts.map +1 -1
- package/dist/rest/index.js +4 -4
- package/dist/rest/index.js.map +1 -1
- package/dist/rest/naming.d.ts +26 -1
- package/dist/rest/naming.d.ts.map +1 -1
- package/dist/rest/naming.js +36 -0
- package/dist/rest/naming.js.map +1 -1
- package/dist/trpc/adapter.d.ts +1 -0
- package/dist/trpc/adapter.d.ts.map +1 -1
- package/dist/trpc/adapter.js +13 -0
- package/dist/trpc/adapter.js.map +1 -1
- package/dist/types.d.ts +79 -4
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +3 -2
- package/dist/types.js.map +1 -1
- package/package.json +17 -3
package/dist/rest/adapter.d.ts
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
*
|
|
8
8
|
* @module rest/adapter
|
|
9
9
|
*/
|
|
10
|
-
import type { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify';
|
|
10
|
+
import type { FastifyInstance, FastifyPluginAsync, FastifyReply, FastifyRequest } from 'fastify';
|
|
11
11
|
import type { CompiledProcedure, HttpMethod, ProcedureCollection } from '../types.js';
|
|
12
12
|
/**
|
|
13
13
|
* REST route definition generated from a procedure
|
|
@@ -24,13 +24,35 @@ export interface RestRoute {
|
|
|
24
24
|
}
|
|
25
25
|
/**
|
|
26
26
|
* Options for REST route registration
|
|
27
|
+
*
|
|
28
|
+
* When using `server.register(rest(...), options)`, the `prefix` option is
|
|
29
|
+
* handled by Fastify's built-in prefix mechanism. When using the legacy
|
|
30
|
+
* `rest(...)(server)` pattern, the prefix is applied manually.
|
|
27
31
|
*/
|
|
28
32
|
export interface RestAdapterOptions {
|
|
29
|
-
/**
|
|
33
|
+
/**
|
|
34
|
+
* API prefix for routes.
|
|
35
|
+
*
|
|
36
|
+
* - With `server.register()`: Use Fastify's built-in prefix option instead
|
|
37
|
+
* - With legacy `rest(...)(server)`: Prefix is applied manually (default: '/api')
|
|
38
|
+
*
|
|
39
|
+
* @default '/api' (legacy mode only)
|
|
40
|
+
*/
|
|
30
41
|
prefix?: string;
|
|
31
42
|
/** Custom error handler */
|
|
32
43
|
onError?: (error: unknown, request: FastifyRequest, reply: FastifyReply) => void;
|
|
33
44
|
}
|
|
45
|
+
/**
|
|
46
|
+
* Internal options used during route registration
|
|
47
|
+
* @internal
|
|
48
|
+
*/
|
|
49
|
+
interface InternalRegistrationOptions extends RestAdapterOptions {
|
|
50
|
+
/**
|
|
51
|
+
* When true, the prefix is handled by Fastify's register() mechanism
|
|
52
|
+
* and should not be manually prepended to routes.
|
|
53
|
+
*/
|
|
54
|
+
_prefixHandledByFastify?: boolean;
|
|
55
|
+
}
|
|
34
56
|
/**
|
|
35
57
|
* Generate REST routes from a procedure collection
|
|
36
58
|
*
|
|
@@ -78,7 +100,7 @@ export declare function generateRestRoutes(collection: ProcedureCollection): Res
|
|
|
78
100
|
* // POST /api/users -> createUser
|
|
79
101
|
* ```
|
|
80
102
|
*/
|
|
81
|
-
export declare function registerRestRoutes(server: FastifyInstance, collections: ProcedureCollection[], options?:
|
|
103
|
+
export declare function registerRestRoutes(server: FastifyInstance, collections: ProcedureCollection[], options?: InternalRegistrationOptions): void;
|
|
82
104
|
/**
|
|
83
105
|
* Get a summary of routes that would be generated from collections
|
|
84
106
|
*
|
|
@@ -124,18 +146,34 @@ export declare function getRouteSummary(collections: ProcedureCollection[], pref
|
|
|
124
146
|
*/
|
|
125
147
|
export declare function createRoutesRegistrar(collections: ProcedureCollection[], options?: RestAdapterOptions): (server: FastifyInstance) => void;
|
|
126
148
|
/**
|
|
127
|
-
*
|
|
149
|
+
* Legacy callable signature for backward compatibility.
|
|
150
|
+
* @deprecated Use `server.register(rest([...]), { prefix: '/api' })` instead.
|
|
151
|
+
*/
|
|
152
|
+
type LegacyRestCallable = (server: FastifyInstance) => void;
|
|
153
|
+
/**
|
|
154
|
+
* A callable Fastify plugin type that can be used both as:
|
|
155
|
+
* - A Fastify plugin: `server.register(rest([...]), { prefix: '/api' })`
|
|
156
|
+
* - A direct callable: `rest([...])(server)` or `app.routes(rest([...]))`
|
|
157
|
+
*
|
|
158
|
+
* This type represents a function that is both:
|
|
159
|
+
* 1. A FastifyPluginAsync (for server.register())
|
|
160
|
+
* 2. A callable that returns void (for legacy app.routes() usage)
|
|
161
|
+
*/
|
|
162
|
+
export type RestPlugin = FastifyPluginAsync<RestAdapterOptions> & LegacyRestCallable;
|
|
163
|
+
/**
|
|
164
|
+
* Creates a Fastify plugin for REST endpoints from procedure collections.
|
|
128
165
|
*
|
|
129
166
|
* This is the preferred way to register procedure collections as REST routes.
|
|
130
|
-
*
|
|
167
|
+
* The function returns a dual-mode plugin that works with both modern Fastify
|
|
168
|
+
* `register()` pattern and the legacy `app.routes()` pattern.
|
|
131
169
|
*
|
|
132
170
|
* @param collections - Procedure collections to register as REST routes
|
|
133
|
-
* @param
|
|
134
|
-
* @returns A
|
|
171
|
+
* @param defaultOptions - Default REST adapter options (can be overridden at registration)
|
|
172
|
+
* @returns A Fastify plugin that can be used with `server.register()` or called directly
|
|
135
173
|
*
|
|
136
|
-
* @example
|
|
174
|
+
* @example Modern API (recommended)
|
|
137
175
|
* ```typescript
|
|
138
|
-
* import
|
|
176
|
+
* import Fastify from 'fastify';
|
|
139
177
|
* import { rest, defineProcedures, procedure } from '@veloxts/router';
|
|
140
178
|
*
|
|
141
179
|
* const users = defineProcedures('users', {
|
|
@@ -145,13 +183,38 @@ export declare function createRoutesRegistrar(collections: ProcedureCollection[]
|
|
|
145
183
|
* .mutation(async ({ input }) => ({ id: '1', ...input })),
|
|
146
184
|
* });
|
|
147
185
|
*
|
|
186
|
+
* const server = Fastify();
|
|
187
|
+
*
|
|
188
|
+
* // Register with Fastify's built-in prefix handling
|
|
189
|
+
* await server.register(rest([users]), { prefix: '/api' });
|
|
190
|
+
*
|
|
191
|
+
* // Generates:
|
|
192
|
+
* // GET /api/users -> listUsers
|
|
193
|
+
* // POST /api/users -> createUser
|
|
194
|
+
* ```
|
|
195
|
+
*
|
|
196
|
+
* @example Legacy API (backward compatible)
|
|
197
|
+
* ```typescript
|
|
198
|
+
* import { veloxApp } from '@veloxts/core';
|
|
199
|
+
* import { rest, defineProcedures, procedure } from '@veloxts/router';
|
|
200
|
+
*
|
|
148
201
|
* const app = await veloxApp();
|
|
149
202
|
*
|
|
150
|
-
* //
|
|
203
|
+
* // Legacy registration with manual prefix
|
|
151
204
|
* app.routes(rest([users], { prefix: '/api' }));
|
|
152
205
|
*
|
|
153
206
|
* await app.start();
|
|
154
207
|
* ```
|
|
208
|
+
*
|
|
209
|
+
* @example Merging options
|
|
210
|
+
* ```typescript
|
|
211
|
+
* // Options passed to rest() are defaults
|
|
212
|
+
* const plugin = rest([users], { onError: customHandler });
|
|
213
|
+
*
|
|
214
|
+
* // Options passed to register() override/merge with defaults
|
|
215
|
+
* await server.register(plugin, { prefix: '/v1' });
|
|
216
|
+
* ```
|
|
155
217
|
*/
|
|
156
|
-
export declare function rest(collections: ProcedureCollection[],
|
|
218
|
+
export declare function rest(collections: ProcedureCollection[], defaultOptions?: RestAdapterOptions): RestPlugin;
|
|
219
|
+
export {};
|
|
157
220
|
//# sourceMappingURL=adapter.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../../src/rest/adapter.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAGH,OAAO,KAAK,EAAE,eAAe,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../../src/rest/adapter.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAGH,OAAO,KAAK,EAAE,eAAe,EAAE,kBAAkB,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAGjG,OAAO,KAAK,EAAE,iBAAiB,EAAE,UAAU,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAOtF;;GAEG;AACH,MAAM,WAAW,SAAS;IACxB,kBAAkB;IAClB,QAAQ,CAAC,MAAM,EAAE,UAAU,CAAC;IAC5B,2CAA2C;IAC3C,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,mCAAmC;IACnC,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;IAC/B,6BAA6B;IAC7B,QAAQ,CAAC,SAAS,EAAE,iBAAiB,CAAC;CACvC;AAED;;;;;;GAMG;AACH,MAAM,WAAW,kBAAkB;IACjC;;;;;;;OAOG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,2BAA2B;IAC3B,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,cAAc,EAAE,KAAK,EAAE,YAAY,KAAK,IAAI,CAAC;CAClF;AAED;;;GAGG;AACH,UAAU,2BAA4B,SAAQ,kBAAkB;IAC9D;;;OAGG;IACH,uBAAuB,CAAC,EAAE,OAAO,CAAC;CACnC;AAMD;;;;;;;;;;GAUG;AACH,wBAAgB,kBAAkB,CAAC,UAAU,EAAE,mBAAmB,GAAG,SAAS,EAAE,CAW/E;AA0ND;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,wBAAgB,kBAAkB,CAChC,MAAM,EAAE,eAAe,EACvB,WAAW,EAAE,mBAAmB,EAAE,EAClC,OAAO,GAAE,2BAAgC,GACxC,IAAI,CAgCN;AAED;;;;;;;;GAQG;AACH,wBAAgB,eAAe,CAC7B,WAAW,EAAE,mBAAmB,EAAE,EAClC,MAAM,SAAS,GACd,KAAK,CAAC;IAAE,MAAM,EAAE,UAAU,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,CAAC,CAsBnF;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAgB,qBAAqB,CACnC,WAAW,EAAE,mBAAmB,EAAE,EAClC,OAAO,GAAE,kBAAuB,GAC/B,CAAC,MAAM,EAAE,eAAe,KAAK,IAAI,CAInC;AAED;;;GAGG;AACH,KAAK,kBAAkB,GAAG,CAAC,MAAM,EAAE,eAAe,KAAK,IAAI,CAAC;AAE5D;;;;;;;;GAQG;AACH,MAAM,MAAM,UAAU,GAAG,kBAAkB,CAAC,kBAAkB,CAAC,GAAG,kBAAkB,CAAC;AAErF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsDG;AACH,wBAAgB,IAAI,CAClB,WAAW,EAAE,mBAAmB,EAAE,EAClC,cAAc,GAAE,kBAAuB,GACtC,UAAU,CAsDZ"}
|
package/dist/rest/adapter.js
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
*/
|
|
10
10
|
import { ConfigurationError } from '@veloxts/core';
|
|
11
11
|
import { executeProcedure } from '../procedure/builder.js';
|
|
12
|
-
import { buildRestPath, parseNamingConvention } from './naming.js';
|
|
12
|
+
import { buildNestedRestPath, buildRestPath, parseNamingConvention } from './naming.js';
|
|
13
13
|
// ============================================================================
|
|
14
14
|
// Route Generation
|
|
15
15
|
// ============================================================================
|
|
@@ -37,6 +37,9 @@ export function generateRestRoutes(collection) {
|
|
|
37
37
|
/**
|
|
38
38
|
* Generate a REST route for a single procedure
|
|
39
39
|
*
|
|
40
|
+
* Handles both flat routes (e.g., /users/:id) and nested routes
|
|
41
|
+
* (e.g., /posts/:postId/comments/:id) when a parent resource is configured.
|
|
42
|
+
*
|
|
40
43
|
* @internal
|
|
41
44
|
*/
|
|
42
45
|
function generateRouteForProcedure(name, procedure, namespace) {
|
|
@@ -45,6 +48,8 @@ function generateRouteForProcedure(name, procedure, namespace) {
|
|
|
45
48
|
const override = procedure.restOverride;
|
|
46
49
|
// Must have both method and path for override
|
|
47
50
|
if (override.method && override.path) {
|
|
51
|
+
// For manual overrides with full path, use as-is
|
|
52
|
+
// The user is responsible for including parent segments if needed
|
|
48
53
|
return {
|
|
49
54
|
method: override.method,
|
|
50
55
|
path: override.path,
|
|
@@ -55,9 +60,14 @@ function generateRouteForProcedure(name, procedure, namespace) {
|
|
|
55
60
|
// Partial override - try to fill in missing parts from convention
|
|
56
61
|
const convention = parseNamingConvention(name, procedure.type);
|
|
57
62
|
if (convention) {
|
|
63
|
+
// Build path based on whether there's a parent resource
|
|
64
|
+
const path = override.path ??
|
|
65
|
+
(procedure.parentResource
|
|
66
|
+
? buildNestedRestPath(procedure.parentResource, namespace, convention)
|
|
67
|
+
: buildRestPath(namespace, convention));
|
|
58
68
|
return {
|
|
59
69
|
method: override.method ?? convention.method,
|
|
60
|
-
path
|
|
70
|
+
path,
|
|
61
71
|
procedureName: name,
|
|
62
72
|
procedure,
|
|
63
73
|
};
|
|
@@ -68,9 +78,13 @@ function generateRouteForProcedure(name, procedure, namespace) {
|
|
|
68
78
|
// Try to infer from naming convention
|
|
69
79
|
const mapping = parseNamingConvention(name, procedure.type);
|
|
70
80
|
if (mapping) {
|
|
81
|
+
// Build path based on whether there's a parent resource
|
|
82
|
+
const path = procedure.parentResource
|
|
83
|
+
? buildNestedRestPath(procedure.parentResource, namespace, mapping)
|
|
84
|
+
: buildRestPath(namespace, mapping);
|
|
71
85
|
return {
|
|
72
86
|
method: mapping.method,
|
|
73
|
-
path
|
|
87
|
+
path,
|
|
74
88
|
procedureName: name,
|
|
75
89
|
procedure,
|
|
76
90
|
};
|
|
@@ -129,28 +143,38 @@ function isPlainObject(value) {
|
|
|
129
143
|
* Gather input data from the request based on HTTP method
|
|
130
144
|
*
|
|
131
145
|
* - GET: Merge params and query
|
|
132
|
-
* - POST: Use body
|
|
146
|
+
* - POST: Use body, but merge params for nested routes (parent ID in URL)
|
|
133
147
|
* - PUT/PATCH: Merge params (for ID) and body (for data)
|
|
134
148
|
* - DELETE: Merge params and query (no body per REST conventions)
|
|
149
|
+
*
|
|
150
|
+
* For nested routes (e.g., /posts/:postId/comments), the parent param
|
|
151
|
+
* is extracted from the URL and merged with the body/query as appropriate.
|
|
135
152
|
*/
|
|
136
153
|
function gatherInput(request, route) {
|
|
137
154
|
const params = isPlainObject(request.params) ? request.params : {};
|
|
138
155
|
const query = isPlainObject(request.query) ? request.query : {};
|
|
139
156
|
const body = isPlainObject(request.body) ? request.body : {};
|
|
157
|
+
// Check if this is a nested route (has parent resource)
|
|
158
|
+
const hasParentResource = route.procedure.parentResource !== undefined;
|
|
140
159
|
switch (route.method) {
|
|
141
160
|
case 'GET':
|
|
142
|
-
// GET: params (for :id) + query (for filters/pagination)
|
|
161
|
+
// GET: params (for :id and parent params) + query (for filters/pagination)
|
|
143
162
|
return { ...params, ...query };
|
|
144
163
|
case 'DELETE':
|
|
145
|
-
// DELETE: params (for :id) + query (for options), no body per REST conventions
|
|
164
|
+
// DELETE: params (for :id and parent params) + query (for options), no body per REST conventions
|
|
146
165
|
return { ...params, ...query };
|
|
147
166
|
case 'PUT':
|
|
148
167
|
case 'PATCH':
|
|
149
|
-
// PUT/PATCH: params (for :id) + body (for data)
|
|
168
|
+
// PUT/PATCH: params (for :id and parent params) + body (for data)
|
|
150
169
|
return { ...params, ...body };
|
|
151
170
|
case 'POST':
|
|
171
|
+
// POST: For nested routes, merge params (for parent ID) with body
|
|
172
|
+
// For flat routes, use body only (no ID in params for creates)
|
|
173
|
+
if (hasParentResource) {
|
|
174
|
+
return { ...params, ...body };
|
|
175
|
+
}
|
|
176
|
+
return request.body;
|
|
152
177
|
default:
|
|
153
|
-
// POST: body only (no ID in params for creates)
|
|
154
178
|
return request.body;
|
|
155
179
|
}
|
|
156
180
|
}
|
|
@@ -158,7 +182,8 @@ function gatherInput(request, route) {
|
|
|
158
182
|
* Extract context from Fastify request
|
|
159
183
|
*
|
|
160
184
|
* The context is decorated onto the request by @veloxts/core's onRequest hook.
|
|
161
|
-
* This function expects the context to already be present
|
|
185
|
+
* This function expects the context to already be present and also merges
|
|
186
|
+
* any auth properties added by @veloxts/auth's preHandler hook.
|
|
162
187
|
*/
|
|
163
188
|
function getContextFromRequest(request) {
|
|
164
189
|
// Type assertion is safe because @veloxts/core decorates this in onRequest hook
|
|
@@ -168,7 +193,17 @@ function getContextFromRequest(request) {
|
|
|
168
193
|
if (!requestWithContext.context) {
|
|
169
194
|
throw new ConfigurationError('Request context not found. Ensure VeloxApp is started before registering routes.');
|
|
170
195
|
}
|
|
171
|
-
|
|
196
|
+
// Merge auth properties from request if @veloxts/auth has added them
|
|
197
|
+
// This allows guards to access ctx.auth and ctx.user
|
|
198
|
+
const context = requestWithContext.context;
|
|
199
|
+
if (requestWithContext.auth !== undefined || requestWithContext.user !== undefined) {
|
|
200
|
+
return {
|
|
201
|
+
...context,
|
|
202
|
+
auth: requestWithContext.auth,
|
|
203
|
+
user: requestWithContext.user,
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
return context;
|
|
172
207
|
}
|
|
173
208
|
// ============================================================================
|
|
174
209
|
// Route Registration
|
|
@@ -209,11 +244,13 @@ function getContextFromRequest(request) {
|
|
|
209
244
|
* ```
|
|
210
245
|
*/
|
|
211
246
|
export function registerRestRoutes(server, collections, options = {}) {
|
|
212
|
-
const { prefix = '/api' } = options;
|
|
247
|
+
const { prefix = '/api', _prefixHandledByFastify = false } = options;
|
|
213
248
|
for (const collection of collections) {
|
|
214
249
|
const routes = generateRestRoutes(collection);
|
|
215
250
|
for (const route of routes) {
|
|
216
|
-
|
|
251
|
+
// When used with server.register(), Fastify handles the prefix automatically.
|
|
252
|
+
// When used in legacy mode, we prepend the prefix manually.
|
|
253
|
+
const fullPath = _prefixHandledByFastify ? route.path : `${prefix}${route.path}`;
|
|
217
254
|
const handler = createRouteHandler(route);
|
|
218
255
|
// Register route based on method
|
|
219
256
|
switch (route.method) {
|
|
@@ -294,18 +331,19 @@ export function createRoutesRegistrar(collections, options = {}) {
|
|
|
294
331
|
};
|
|
295
332
|
}
|
|
296
333
|
/**
|
|
297
|
-
* Creates a
|
|
334
|
+
* Creates a Fastify plugin for REST endpoints from procedure collections.
|
|
298
335
|
*
|
|
299
336
|
* This is the preferred way to register procedure collections as REST routes.
|
|
300
|
-
*
|
|
337
|
+
* The function returns a dual-mode plugin that works with both modern Fastify
|
|
338
|
+
* `register()` pattern and the legacy `app.routes()` pattern.
|
|
301
339
|
*
|
|
302
340
|
* @param collections - Procedure collections to register as REST routes
|
|
303
|
-
* @param
|
|
304
|
-
* @returns A
|
|
341
|
+
* @param defaultOptions - Default REST adapter options (can be overridden at registration)
|
|
342
|
+
* @returns A Fastify plugin that can be used with `server.register()` or called directly
|
|
305
343
|
*
|
|
306
|
-
* @example
|
|
344
|
+
* @example Modern API (recommended)
|
|
307
345
|
* ```typescript
|
|
308
|
-
* import
|
|
346
|
+
* import Fastify from 'fastify';
|
|
309
347
|
* import { rest, defineProcedures, procedure } from '@veloxts/router';
|
|
310
348
|
*
|
|
311
349
|
* const users = defineProcedures('users', {
|
|
@@ -315,17 +353,81 @@ export function createRoutesRegistrar(collections, options = {}) {
|
|
|
315
353
|
* .mutation(async ({ input }) => ({ id: '1', ...input })),
|
|
316
354
|
* });
|
|
317
355
|
*
|
|
356
|
+
* const server = Fastify();
|
|
357
|
+
*
|
|
358
|
+
* // Register with Fastify's built-in prefix handling
|
|
359
|
+
* await server.register(rest([users]), { prefix: '/api' });
|
|
360
|
+
*
|
|
361
|
+
* // Generates:
|
|
362
|
+
* // GET /api/users -> listUsers
|
|
363
|
+
* // POST /api/users -> createUser
|
|
364
|
+
* ```
|
|
365
|
+
*
|
|
366
|
+
* @example Legacy API (backward compatible)
|
|
367
|
+
* ```typescript
|
|
368
|
+
* import { veloxApp } from '@veloxts/core';
|
|
369
|
+
* import { rest, defineProcedures, procedure } from '@veloxts/router';
|
|
370
|
+
*
|
|
318
371
|
* const app = await veloxApp();
|
|
319
372
|
*
|
|
320
|
-
* //
|
|
373
|
+
* // Legacy registration with manual prefix
|
|
321
374
|
* app.routes(rest([users], { prefix: '/api' }));
|
|
322
375
|
*
|
|
323
376
|
* await app.start();
|
|
324
377
|
* ```
|
|
378
|
+
*
|
|
379
|
+
* @example Merging options
|
|
380
|
+
* ```typescript
|
|
381
|
+
* // Options passed to rest() are defaults
|
|
382
|
+
* const plugin = rest([users], { onError: customHandler });
|
|
383
|
+
*
|
|
384
|
+
* // Options passed to register() override/merge with defaults
|
|
385
|
+
* await server.register(plugin, { prefix: '/v1' });
|
|
386
|
+
* ```
|
|
325
387
|
*/
|
|
326
|
-
export function rest(collections,
|
|
327
|
-
|
|
328
|
-
|
|
388
|
+
export function rest(collections, defaultOptions = {}) {
|
|
389
|
+
/**
|
|
390
|
+
* The async plugin function for Fastify's register() method.
|
|
391
|
+
* When used with register(), Fastify handles the prefix automatically.
|
|
392
|
+
*/
|
|
393
|
+
const plugin = async (instance, opts) => {
|
|
394
|
+
// Merge default options with options passed to register()
|
|
395
|
+
// Options from register() take precedence
|
|
396
|
+
const mergedOptions = {
|
|
397
|
+
...defaultOptions,
|
|
398
|
+
...opts,
|
|
399
|
+
// When used via register(), Fastify handles the prefix automatically
|
|
400
|
+
// We signal this to registerRestRoutes so it doesn't double-prefix
|
|
401
|
+
_prefixHandledByFastify: true,
|
|
402
|
+
};
|
|
403
|
+
registerRestRoutes(instance, collections, mergedOptions);
|
|
404
|
+
};
|
|
405
|
+
/**
|
|
406
|
+
* Create a callable wrapper that supports both patterns:
|
|
407
|
+
* 1. As a Fastify plugin: server.register(rest([...]), opts)
|
|
408
|
+
* 2. As a direct call: rest([...])(server) - legacy pattern
|
|
409
|
+
*
|
|
410
|
+
* The trick is that FastifyPluginAsync expects (instance, opts) => Promise<void>
|
|
411
|
+
* but the legacy pattern expects (server) => void.
|
|
412
|
+
*
|
|
413
|
+
* We detect which mode based on the second argument:
|
|
414
|
+
* - If second arg is undefined/missing: legacy callable mode
|
|
415
|
+
* - If second arg is present: Fastify plugin mode (called by register())
|
|
416
|
+
*/
|
|
417
|
+
const dualModePlugin = (instance, opts) => {
|
|
418
|
+
// If opts is provided, we're being called by Fastify's register()
|
|
419
|
+
// In this case, return the promise from the async plugin
|
|
420
|
+
if (opts !== undefined) {
|
|
421
|
+
return plugin(instance, opts);
|
|
422
|
+
}
|
|
423
|
+
// Legacy mode: called directly as rest([...])(server)
|
|
424
|
+
// Use default options and apply prefix manually
|
|
425
|
+
registerRestRoutes(instance, collections, defaultOptions);
|
|
329
426
|
};
|
|
427
|
+
// Cast to RestPlugin - TypeScript needs help understanding this dual nature
|
|
428
|
+
// The function satisfies both signatures:
|
|
429
|
+
// - (instance, opts) => Promise<void> for Fastify register
|
|
430
|
+
// - (server) => void for legacy callable
|
|
431
|
+
return dualModePlugin;
|
|
330
432
|
}
|
|
331
433
|
//# sourceMappingURL=adapter.js.map
|
package/dist/rest/adapter.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"adapter.js","sourceRoot":"","sources":["../../src/rest/adapter.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAoB,kBAAkB,EAAE,MAAM,eAAe,CAAC;AAGrE,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAE3D,OAAO,EAAE,aAAa,EAAE,qBAAqB,EAAE,MAAM,aAA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A0D;QAC1D,0CAA0C;QAC1C,MAAM,aAAa,GAAgC;YACjD,GAAG,cAAc;YACjB,GAAG,IAAI;YACP,qEAAqE;YACrE,mEAAmE;YACnE,uBAAuB,EAAE,IAAI;SAC9B,CAAC;QAEF,kBAAkB,CAAC,QAAQ,EAAE,WAAW,EAAE,aAAa,CAAC,CAAC;IAC3D,CAAC,CAAC;IAEF;;;;;;;;;;;OAWG;IACH,MAAM,cAAc,GAAG,CACrB,QAAyB,EACzB,IAAyB,EACH,EAAE;QACxB,kEAAkE;QAClE,yDAAyD;QACzD,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;YACvB,OAAO,MAAM,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC;QAChC,CAAC;QAED,sDAAsD;QACtD,gDAAgD;QAChD,kBAAkB,CAAC,QAAQ,EAAE,WAAW,EAAE,cAAc,CAAC,CAAC;IAC5D,CAAC,CAAC;IAEF,4EAA4E;IAC5E,0CAA0C;IAC1C,2DAA2D;IAC3D,yCAAyC;IACzC,OAAO,cAA4B,CAAC;AACtC,CAAC"}
|
package/dist/rest/index.d.ts
CHANGED
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
*
|
|
4
4
|
* @module rest
|
|
5
5
|
*/
|
|
6
|
-
export type { RestAdapterOptions, RestRoute } from './adapter.js';
|
|
7
|
-
export {
|
|
6
|
+
export type { RestAdapterOptions, RestPlugin, RestRoute } from './adapter.js';
|
|
7
|
+
export { createRoutesRegistrar, generateRestRoutes, getRouteSummary, registerRestRoutes, rest, } from './adapter.js';
|
|
8
8
|
export type { RestMapping } from './naming.js';
|
|
9
|
-
export { buildRestPath, followsNamingConvention, inferResourceName, parseNamingConvention, } from './naming.js';
|
|
9
|
+
export { buildNestedRestPath, buildRestPath, followsNamingConvention, inferResourceName, parseNamingConvention, } from './naming.js';
|
|
10
10
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/rest/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/rest/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,YAAY,EAAE,kBAAkB,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/rest/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,YAAY,EAAE,kBAAkB,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAE9E,OAAO,EAEL,qBAAqB,EAErB,kBAAkB,EAClB,eAAe,EACf,kBAAkB,EAElB,IAAI,GACL,MAAM,cAAc,CAAC;AAEtB,YAAY,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC/C,OAAO,EACL,mBAAmB,EACnB,aAAa,EACb,uBAAuB,EACvB,iBAAiB,EACjB,qBAAqB,GACtB,MAAM,aAAa,CAAC"}
|
package/dist/rest/index.js
CHANGED
|
@@ -5,11 +5,11 @@
|
|
|
5
5
|
*/
|
|
6
6
|
// REST adapter - public API
|
|
7
7
|
export {
|
|
8
|
-
// Succinct API (preferred)
|
|
9
|
-
rest,
|
|
10
8
|
// Legacy API (deprecated)
|
|
11
9
|
createRoutesRegistrar,
|
|
12
10
|
// Internal utilities
|
|
13
|
-
generateRestRoutes, getRouteSummary, registerRestRoutes,
|
|
14
|
-
|
|
11
|
+
generateRestRoutes, getRouteSummary, registerRestRoutes,
|
|
12
|
+
// Succinct API (preferred)
|
|
13
|
+
rest, } from './adapter.js';
|
|
14
|
+
export { buildNestedRestPath, buildRestPath, followsNamingConvention, inferResourceName, parseNamingConvention, } from './naming.js';
|
|
15
15
|
//# sourceMappingURL=index.js.map
|
package/dist/rest/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/rest/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,4BAA4B;AAC5B,OAAO;AACL,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/rest/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,4BAA4B;AAC5B,OAAO;AACL,0BAA0B;AAC1B,qBAAqB;AACrB,qBAAqB;AACrB,kBAAkB,EAClB,eAAe,EACf,kBAAkB;AAClB,2BAA2B;AAC3B,IAAI,GACL,MAAM,cAAc,CAAC;AAGtB,OAAO,EACL,mBAAmB,EACnB,aAAa,EACb,uBAAuB,EACvB,iBAAiB,EACjB,qBAAqB,GACtB,MAAM,aAAa,CAAC"}
|
package/dist/rest/naming.d.ts
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
*
|
|
7
7
|
* @module rest/naming
|
|
8
8
|
*/
|
|
9
|
-
import type { HttpMethod, ProcedureType } from '../types.js';
|
|
9
|
+
import type { HttpMethod, ParentResourceConfig, ProcedureType } from '../types.js';
|
|
10
10
|
/**
|
|
11
11
|
* Result of parsing a procedure name into REST route info
|
|
12
12
|
*/
|
|
@@ -58,6 +58,31 @@ export declare function parseNamingConvention(name: string, type: ProcedureType)
|
|
|
58
58
|
* ```
|
|
59
59
|
*/
|
|
60
60
|
export declare function buildRestPath(namespace: string, mapping: RestMapping): string;
|
|
61
|
+
/**
|
|
62
|
+
* Build a nested REST path with parent resource prefix
|
|
63
|
+
*
|
|
64
|
+
* Creates paths like `/posts/:postId/comments/:id` for nested resources.
|
|
65
|
+
*
|
|
66
|
+
* @param parentResource - Parent resource configuration
|
|
67
|
+
* @param childNamespace - Child resource namespace (e.g., 'comments')
|
|
68
|
+
* @param mapping - REST mapping from parseNamingConvention
|
|
69
|
+
* @returns Full nested path
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```typescript
|
|
73
|
+
* const parent = { namespace: 'posts', paramName: 'postId' };
|
|
74
|
+
*
|
|
75
|
+
* buildNestedRestPath(parent, 'comments', { method: 'GET', path: '/:id', hasIdParam: true })
|
|
76
|
+
* // Returns: '/posts/:postId/comments/:id'
|
|
77
|
+
*
|
|
78
|
+
* buildNestedRestPath(parent, 'comments', { method: 'GET', path: '/', hasIdParam: false })
|
|
79
|
+
* // Returns: '/posts/:postId/comments'
|
|
80
|
+
*
|
|
81
|
+
* buildNestedRestPath(parent, 'comments', { method: 'POST', path: '/', hasIdParam: false })
|
|
82
|
+
* // Returns: '/posts/:postId/comments'
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
export declare function buildNestedRestPath(parentResource: ParentResourceConfig, childNamespace: string, mapping: RestMapping): string;
|
|
61
86
|
/**
|
|
62
87
|
* Infer the resource name from a procedure name
|
|
63
88
|
*
|
|
@@ -1 +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;
|
|
1
|
+
{"version":3,"file":"naming.d.ts","sourceRoot":"","sources":["../../src/rest/naming.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAE,oBAAoB,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAMnF;;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;AAgHD;;;;;;;;;;;;;;;;;;;;;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;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,mBAAmB,CACjC,cAAc,EAAE,oBAAoB,EACpC,cAAc,EAAE,MAAM,EACtB,OAAO,EAAE,WAAW,GACnB,MAAM,CAcR;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"}
|
package/dist/rest/naming.js
CHANGED
|
@@ -165,6 +165,42 @@ export function buildRestPath(namespace, mapping) {
|
|
|
165
165
|
// Otherwise append the path (e.g., '/:id')
|
|
166
166
|
return `${basePath}${mapping.path}`;
|
|
167
167
|
}
|
|
168
|
+
/**
|
|
169
|
+
* Build a nested REST path with parent resource prefix
|
|
170
|
+
*
|
|
171
|
+
* Creates paths like `/posts/:postId/comments/:id` for nested resources.
|
|
172
|
+
*
|
|
173
|
+
* @param parentResource - Parent resource configuration
|
|
174
|
+
* @param childNamespace - Child resource namespace (e.g., 'comments')
|
|
175
|
+
* @param mapping - REST mapping from parseNamingConvention
|
|
176
|
+
* @returns Full nested path
|
|
177
|
+
*
|
|
178
|
+
* @example
|
|
179
|
+
* ```typescript
|
|
180
|
+
* const parent = { namespace: 'posts', paramName: 'postId' };
|
|
181
|
+
*
|
|
182
|
+
* buildNestedRestPath(parent, 'comments', { method: 'GET', path: '/:id', hasIdParam: true })
|
|
183
|
+
* // Returns: '/posts/:postId/comments/:id'
|
|
184
|
+
*
|
|
185
|
+
* buildNestedRestPath(parent, 'comments', { method: 'GET', path: '/', hasIdParam: false })
|
|
186
|
+
* // Returns: '/posts/:postId/comments'
|
|
187
|
+
*
|
|
188
|
+
* buildNestedRestPath(parent, 'comments', { method: 'POST', path: '/', hasIdParam: false })
|
|
189
|
+
* // Returns: '/posts/:postId/comments'
|
|
190
|
+
* ```
|
|
191
|
+
*/
|
|
192
|
+
export function buildNestedRestPath(parentResource, childNamespace, mapping) {
|
|
193
|
+
// Build parent path segment: /{parentNamespace}/:{parentParamName}
|
|
194
|
+
const parentPath = `/${parentResource.namespace}/:${parentResource.paramName}`;
|
|
195
|
+
// Build child path segment
|
|
196
|
+
const childBasePath = `/${childNamespace}`;
|
|
197
|
+
// If mapping path is just '/', don't add extra suffix
|
|
198
|
+
if (mapping.path === '/') {
|
|
199
|
+
return `${parentPath}${childBasePath}`;
|
|
200
|
+
}
|
|
201
|
+
// Otherwise append the path (e.g., '/:id')
|
|
202
|
+
return `${parentPath}${childBasePath}${mapping.path}`;
|
|
203
|
+
}
|
|
168
204
|
/**
|
|
169
205
|
* Infer the resource name from a procedure name
|
|
170
206
|
*
|
package/dist/rest/naming.js.map
CHANGED
|
@@ -1 +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;;;;;;;;;;;;;;GAcG;AACH,MAAM,eAAe,GAA6B;IAChD,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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B,OAAO,GAAG,UAAU,GAAG,aAAa,EAAE,CAAC;IACzC,CAAC;IAED,2CAA2C;IAC3C,OAAO,GAAG,UAAU,GAAG,aAAa,GAAG,OAAO,CAAC,IAAI,EAAE,CAAC;AACxD,CAAC;AAED;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,iBAAiB,CAAC,IAAY;IAC5C,KAAK,MAAM,OAAO,IAAI,eAAe,EAAE,CAAC;QACtC,MAAM,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,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"}
|
package/dist/trpc/adapter.d.ts
CHANGED
|
@@ -130,6 +130,7 @@ export declare function createTRPCContextFactory(): ({ req }: {
|
|
|
130
130
|
* Convert a VeloxTS error to a tRPC error
|
|
131
131
|
*
|
|
132
132
|
* Maps VeloxTS error codes to appropriate tRPC error codes.
|
|
133
|
+
* Handles GuardError specifically to preserve guard metadata.
|
|
133
134
|
*
|
|
134
135
|
* @param error - Error to convert
|
|
135
136
|
* @param defaultCode - Default tRPC code if mapping not found
|
|
@@ -1 +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,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CAAA;KAAE,CAAA;CAAE,KAAG,WAAW,CAWlE;AAMD;;;;;;;;;GASG;AACH,wBAAgB,qBAAqB,CACnC,KAAK,EAAE,KAAK,GAAG;IAAE,UAAU,CAAC,EAAE,MAAM,CAAC;IAAC,IAAI,CAAC,EAAE,MAAM,CAAA;CAAE,EACrD,WAAW,GAAE,SAAS,CAAC,MAAM,CAA2B,GACvD,SAAS,CAiCX;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,SAAS,GAAG,KAAK,IAAI,SAAS,GAAG;IAAE,KAAK,EAAE,MAAM,CAAA;CAAE,CAEzF;AAMD;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC,oDAAoD;IACpD,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,+CAA+C;IAC/C,MAAM,EAAE,SAAS,CAAC;CACnB;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAsB,kBAAkB,CACtC,MAAM,EAAE,eAAe,EACvB,OAAO,EAAE,iBAAiB,GACzB,OAAO,CAAC,IAAI,CAAC,CAWf"}
|
package/dist/trpc/adapter.js
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
* @module trpc/adapter
|
|
8
8
|
*/
|
|
9
9
|
import { initTRPC, TRPCError } from '@trpc/server';
|
|
10
|
+
import { isGuardError } from '../errors.js';
|
|
10
11
|
// ============================================================================
|
|
11
12
|
// tRPC Initialization
|
|
12
13
|
// ============================================================================
|
|
@@ -245,6 +246,7 @@ export function createTRPCContextFactory() {
|
|
|
245
246
|
* Convert a VeloxTS error to a tRPC error
|
|
246
247
|
*
|
|
247
248
|
* Maps VeloxTS error codes to appropriate tRPC error codes.
|
|
249
|
+
* Handles GuardError specifically to preserve guard metadata.
|
|
248
250
|
*
|
|
249
251
|
* @param error - Error to convert
|
|
250
252
|
* @param defaultCode - Default tRPC code if mapping not found
|
|
@@ -264,6 +266,17 @@ export function veloxErrorToTRPCError(error, defaultCode = 'INTERNAL_SERVER_ERRO
|
|
|
264
266
|
500: 'INTERNAL_SERVER_ERROR',
|
|
265
267
|
};
|
|
266
268
|
const trpcCode = error.statusCode ? (statusToTRPC[error.statusCode] ?? defaultCode) : defaultCode;
|
|
269
|
+
// Handle GuardError specifically to preserve guard metadata
|
|
270
|
+
if (isGuardError(error)) {
|
|
271
|
+
return new TRPCError({
|
|
272
|
+
code: trpcCode,
|
|
273
|
+
message: error.message,
|
|
274
|
+
cause: {
|
|
275
|
+
code: error.code,
|
|
276
|
+
guardName: error.guardName,
|
|
277
|
+
},
|
|
278
|
+
});
|
|
279
|
+
}
|
|
267
280
|
return new TRPCError({
|
|
268
281
|
code: trpcCode,
|
|
269
282
|
message: error.message,
|