@veloxts/router 0.3.4 → 0.3.5
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 +39 -4
- package/dist/procedure/builder.js.map +1 -1
- package/dist/procedure/types.d.ts +36 -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 +92 -15
- package/dist/rest/adapter.js.map +1 -1
- package/dist/rest/index.d.ts +2 -2
- package/dist/rest/index.d.ts.map +1 -1
- package/dist/rest/index.js +3 -3
- package/dist/rest/index.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 +37 -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/errors.d.ts
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error classes for @veloxts/router
|
|
3
|
+
*
|
|
4
|
+
* Provides specialized error types for the routing system, including
|
|
5
|
+
* guard authorization failures.
|
|
6
|
+
*
|
|
7
|
+
* @module errors
|
|
8
|
+
*/
|
|
9
|
+
import { VeloxError } from '@veloxts/core';
|
|
10
|
+
/**
|
|
11
|
+
* Error codes specific to the router package
|
|
12
|
+
*/
|
|
13
|
+
export type RouterErrorCode = 'GUARD_FAILED' | 'UNAUTHORIZED' | 'FORBIDDEN';
|
|
14
|
+
/**
|
|
15
|
+
* Guard error response type for API responses
|
|
16
|
+
*/
|
|
17
|
+
export interface GuardErrorResponse {
|
|
18
|
+
error: 'GuardError';
|
|
19
|
+
message: string;
|
|
20
|
+
statusCode: number;
|
|
21
|
+
code: 'GUARD_FAILED';
|
|
22
|
+
guardName: string;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Error thrown when a guard check fails
|
|
26
|
+
*
|
|
27
|
+
* Used to signal authorization failures in a type-safe way without
|
|
28
|
+
* mutating error objects. Integrates with the VeloxTS error system
|
|
29
|
+
* and provides proper HTTP status codes.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```typescript
|
|
33
|
+
* // In guard execution
|
|
34
|
+
* if (!passed) {
|
|
35
|
+
* throw new GuardError('authenticated', 'Authentication required', 401);
|
|
36
|
+
* }
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```typescript
|
|
41
|
+
* // In error handler
|
|
42
|
+
* if (isGuardError(error)) {
|
|
43
|
+
* reply.status(error.statusCode).send({
|
|
44
|
+
* error: 'GuardError',
|
|
45
|
+
* message: error.message,
|
|
46
|
+
* guardName: error.guardName,
|
|
47
|
+
* });
|
|
48
|
+
* }
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export declare class GuardError extends VeloxError<'GUARD_FAILED'> {
|
|
52
|
+
/**
|
|
53
|
+
* Name of the guard that failed
|
|
54
|
+
*/
|
|
55
|
+
readonly guardName: string;
|
|
56
|
+
/**
|
|
57
|
+
* Creates a new GuardError instance
|
|
58
|
+
*
|
|
59
|
+
* @param guardName - Name of the guard that failed (for debugging)
|
|
60
|
+
* @param message - Human-readable error message
|
|
61
|
+
* @param statusCode - HTTP status code (default: 403 Forbidden)
|
|
62
|
+
*/
|
|
63
|
+
constructor(guardName: string, message: string, statusCode?: number);
|
|
64
|
+
/**
|
|
65
|
+
* Converts guard error to JSON format for API responses
|
|
66
|
+
*
|
|
67
|
+
* @returns Guard error response with guard name
|
|
68
|
+
*/
|
|
69
|
+
toJSON(): GuardErrorResponse;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Type guard to check if an error is a GuardError
|
|
73
|
+
*
|
|
74
|
+
* @param error - Error to check
|
|
75
|
+
* @returns true if error is a GuardError instance
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```typescript
|
|
79
|
+
* try {
|
|
80
|
+
* await executeProcedure(proc, input, ctx);
|
|
81
|
+
* } catch (error) {
|
|
82
|
+
* if (isGuardError(error)) {
|
|
83
|
+
* console.log(`Guard "${error.guardName}" failed:`, error.message);
|
|
84
|
+
* }
|
|
85
|
+
* }
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
88
|
+
export declare function isGuardError(error: unknown): error is GuardError;
|
|
89
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAM3C;;GAEG;AACH,MAAM,MAAM,eAAe,GAAG,cAAc,GAAG,cAAc,GAAG,WAAW,CAAC;AAM5E;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,KAAK,EAAE,YAAY,CAAC;IACpB,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,EAAE,MAAM,CAAC;IACnB,IAAI,EAAE,cAAc,CAAC;IACrB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,qBAAa,UAAW,SAAQ,UAAU,CAAC,cAAc,CAAC;IACxD;;OAEG;IACH,SAAgB,SAAS,EAAE,MAAM,CAAC;IAElC;;;;;;OAMG;gBACS,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,UAAU,GAAE,MAAY;IAWxE;;;;OAIG;IACM,MAAM,IAAI,kBAAkB;CAStC;AAMD;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,UAAU,CAEhE"}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error classes for @veloxts/router
|
|
3
|
+
*
|
|
4
|
+
* Provides specialized error types for the routing system, including
|
|
5
|
+
* guard authorization failures.
|
|
6
|
+
*
|
|
7
|
+
* @module errors
|
|
8
|
+
*/
|
|
9
|
+
import { VeloxError } from '@veloxts/core';
|
|
10
|
+
/**
|
|
11
|
+
* Error thrown when a guard check fails
|
|
12
|
+
*
|
|
13
|
+
* Used to signal authorization failures in a type-safe way without
|
|
14
|
+
* mutating error objects. Integrates with the VeloxTS error system
|
|
15
|
+
* and provides proper HTTP status codes.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* // In guard execution
|
|
20
|
+
* if (!passed) {
|
|
21
|
+
* throw new GuardError('authenticated', 'Authentication required', 401);
|
|
22
|
+
* }
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```typescript
|
|
27
|
+
* // In error handler
|
|
28
|
+
* if (isGuardError(error)) {
|
|
29
|
+
* reply.status(error.statusCode).send({
|
|
30
|
+
* error: 'GuardError',
|
|
31
|
+
* message: error.message,
|
|
32
|
+
* guardName: error.guardName,
|
|
33
|
+
* });
|
|
34
|
+
* }
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export class GuardError extends VeloxError {
|
|
38
|
+
/**
|
|
39
|
+
* Name of the guard that failed
|
|
40
|
+
*/
|
|
41
|
+
guardName;
|
|
42
|
+
/**
|
|
43
|
+
* Creates a new GuardError instance
|
|
44
|
+
*
|
|
45
|
+
* @param guardName - Name of the guard that failed (for debugging)
|
|
46
|
+
* @param message - Human-readable error message
|
|
47
|
+
* @param statusCode - HTTP status code (default: 403 Forbidden)
|
|
48
|
+
*/
|
|
49
|
+
constructor(guardName, message, statusCode = 403) {
|
|
50
|
+
super(message, statusCode, 'GUARD_FAILED');
|
|
51
|
+
this.name = 'GuardError';
|
|
52
|
+
this.guardName = guardName;
|
|
53
|
+
// Maintains proper stack trace for where error was thrown (V8 only)
|
|
54
|
+
if (Error.captureStackTrace) {
|
|
55
|
+
Error.captureStackTrace(this, GuardError);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Converts guard error to JSON format for API responses
|
|
60
|
+
*
|
|
61
|
+
* @returns Guard error response with guard name
|
|
62
|
+
*/
|
|
63
|
+
toJSON() {
|
|
64
|
+
return {
|
|
65
|
+
error: 'GuardError',
|
|
66
|
+
message: this.message,
|
|
67
|
+
statusCode: this.statusCode,
|
|
68
|
+
code: 'GUARD_FAILED',
|
|
69
|
+
guardName: this.guardName,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
// ============================================================================
|
|
74
|
+
// Type Guards
|
|
75
|
+
// ============================================================================
|
|
76
|
+
/**
|
|
77
|
+
* Type guard to check if an error is a GuardError
|
|
78
|
+
*
|
|
79
|
+
* @param error - Error to check
|
|
80
|
+
* @returns true if error is a GuardError instance
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```typescript
|
|
84
|
+
* try {
|
|
85
|
+
* await executeProcedure(proc, input, ctx);
|
|
86
|
+
* } catch (error) {
|
|
87
|
+
* if (isGuardError(error)) {
|
|
88
|
+
* console.log(`Guard "${error.guardName}" failed:`, error.message);
|
|
89
|
+
* }
|
|
90
|
+
* }
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
93
|
+
export function isGuardError(error) {
|
|
94
|
+
return error instanceof GuardError;
|
|
95
|
+
}
|
|
96
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AA0B3C;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,OAAO,UAAW,SAAQ,UAA0B;IACxD;;OAEG;IACa,SAAS,CAAS;IAElC;;;;;;OAMG;IACH,YAAY,SAAiB,EAAE,OAAe,EAAE,aAAqB,GAAG;QACtE,KAAK,CAAC,OAAO,EAAE,UAAU,EAAE,cAAc,CAAC,CAAC;QAC3C,IAAI,CAAC,IAAI,GAAG,YAAY,CAAC;QACzB,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAE3B,oEAAoE;QACpE,IAAI,KAAK,CAAC,iBAAiB,EAAE,CAAC;YAC5B,KAAK,CAAC,iBAAiB,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC;QAC5C,CAAC;IACH,CAAC;IAED;;;;OAIG;IACM,MAAM;QACb,OAAO;YACL,KAAK,EAAE,YAAY;YACnB,OAAO,EAAE,IAAI,CAAC,OAAO;YACrB,UAAU,EAAE,IAAI,CAAC,UAAU;YAC3B,IAAI,EAAE,cAAc;YACpB,SAAS,EAAE,IAAI,CAAC,SAAS;SAC1B,CAAC;IACJ,CAAC;CACF;AAED,+EAA+E;AAC/E,cAAc;AACd,+EAA+E;AAE/E;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,YAAY,CAAC,KAAc;IACzC,OAAO,KAAK,YAAY,UAAU,CAAC;AACrC,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -39,12 +39,14 @@
|
|
|
39
39
|
*/
|
|
40
40
|
/** Router package version */
|
|
41
41
|
export declare const ROUTER_VERSION: string;
|
|
42
|
-
export type { CompiledProcedure, ContextExtensions, ContextFactory, ExtendedContext, HttpMethod, InferProcedureContext, InferProcedureInput, InferProcedureOutput, InferProcedureTypes, MiddlewareArgs, MiddlewareFunction, MiddlewareNext, MiddlewareResult, ProcedureCollection, ProcedureHandler, ProcedureHandlerArgs, ProcedureRecord, ProcedureType, RestRouteOverride, } from './types.js';
|
|
42
|
+
export type { CompiledProcedure, ContextExtensions, ContextFactory, ExtendedContext, GuardLike, HttpMethod, InferProcedureContext, InferProcedureInput, InferProcedureOutput, InferProcedureTypes, MiddlewareArgs, MiddlewareFunction, MiddlewareNext, MiddlewareResult, ProcedureCollection, ProcedureHandler, ProcedureHandlerArgs, ProcedureRecord, ProcedureType, RestRouteOverride, } from './types.js';
|
|
43
43
|
export { PROCEDURE_METHOD_MAP, } from './types.js';
|
|
44
|
+
export type { GuardErrorResponse, RouterErrorCode } from './errors.js';
|
|
45
|
+
export { GuardError, isGuardError } from './errors.js';
|
|
44
46
|
export { defineProcedures, executeProcedure, isCompiledProcedure, isProcedureCollection, procedure, } from './procedure/builder.js';
|
|
45
47
|
export type { BuilderRuntimeState, InferProcedures, InferSchemaOutput, ProcedureBuilder, ProcedureBuilderState, ProcedureDefinitions, ValidSchema, } from './procedure/types.js';
|
|
46
48
|
export type { RestAdapterOptions, RestMapping, RestRoute } from './rest/index.js';
|
|
47
|
-
export {
|
|
49
|
+
export { buildRestPath, createRoutesRegistrar, followsNamingConvention, generateRestRoutes, getRouteSummary, inferResourceName, parseNamingConvention, registerRestRoutes, rest, } from './rest/index.js';
|
|
48
50
|
export type { AnyRouter, InferAppRouter, TRPCInstance, TRPCPluginOptions, } from './trpc/index.js';
|
|
49
51
|
export { buildTRPCRouter, createAppRouter, createTRPC, createTRPCContextFactory, registerTRPCPlugin, veloxErrorToTRPCError, } from './trpc/index.js';
|
|
50
52
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AAQH,6BAA6B;AAC7B,eAAO,MAAM,cAAc,EA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,YAAY,EAEV,SAAS,EACT,cAAc,EACd,YAAY,EACZ,iBAAiB,GAClB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAEL,eAAe,EACf,eAAe,EACf,UAAU,EACV,wBAAwB,EACxB,kBAAkB,EAClB,qBAAqB,GACtB,MAAM,iBAAiB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -46,6 +46,7 @@ export const ROUTER_VERSION = packageJson.version ?? '0.0.0-unknown';
|
|
|
46
46
|
export {
|
|
47
47
|
// Constants
|
|
48
48
|
PROCEDURE_METHOD_MAP, } from './types.js';
|
|
49
|
+
export { GuardError, isGuardError } from './errors.js';
|
|
49
50
|
// ============================================================================
|
|
50
51
|
// Procedure Builder
|
|
51
52
|
// ============================================================================
|
|
@@ -53,12 +54,12 @@ export {
|
|
|
53
54
|
// Builder functions
|
|
54
55
|
defineProcedures, executeProcedure, isCompiledProcedure, isProcedureCollection, procedure, } from './procedure/builder.js';
|
|
55
56
|
export {
|
|
56
|
-
// Succinct API (preferred)
|
|
57
|
-
rest,
|
|
58
|
-
// Legacy API (deprecated)
|
|
59
|
-
createRoutesRegistrar,
|
|
60
57
|
// Internal utilities
|
|
61
|
-
buildRestPath,
|
|
58
|
+
buildRestPath,
|
|
59
|
+
// Legacy API (deprecated)
|
|
60
|
+
createRoutesRegistrar, followsNamingConvention, generateRestRoutes, getRouteSummary, inferResourceName, parseNamingConvention, registerRestRoutes,
|
|
61
|
+
// Succinct API (preferred)
|
|
62
|
+
rest, } from './rest/index.js';
|
|
62
63
|
export {
|
|
63
64
|
// tRPC utilities
|
|
64
65
|
buildTRPCRouter, createAppRouter, createTRPC, createTRPCContextFactory, registerTRPCPlugin, veloxErrorToTRPCError, } from './trpc/index.js';
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;G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AAiB;AACjB,eAAe,EACf,eAAe,EACf,UAAU,EACV,wBAAwB,EACxB,kBAAkB,EAClB,qBAAqB,GACtB,MAAM,iBAAiB,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"builder.d.ts","sourceRoot":"","sources":["../../src/procedure/builder.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"builder.d.ts","sourceRoot":"","sources":["../../src/procedure/builder.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAGjD,OAAO,KAAK,EACV,iBAAiB,EAGjB,mBAAmB,EAGpB,MAAM,aAAa,CAAC;AACrB,OAAO,KAAK,EAEV,eAAe,EAEf,gBAAgB,EAChB,oBAAoB,EAErB,MAAM,YAAY,CAAC;AAMpB;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,SAAS,IAAI,gBAAgB,CAAC,OAAO,EAAE,OAAO,EAAE,WAAW,CAAC,CAQ3E;AA8MD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,wBAAgB,gBAAgB,CAAC,WAAW,SAAS,oBAAoB,EACvE,SAAS,EAAE,MAAM,EACjB,UAAU,EAAE,WAAW,GACtB,mBAAmB,CAAC,eAAe,CAAC,WAAW,CAAC,CAAC,CAKnD;AAMD;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAsB,gBAAgB,CAAC,MAAM,EAAE,OAAO,EAAE,QAAQ,SAAS,WAAW,EAClF,SAAS,EAAE,iBAAiB,CAAC,MAAM,EAAE,OAAO,EAAE,QAAQ,CAAC,EACvD,QAAQ,EAAE,OAAO,EACjB,GAAG,EAAE,QAAQ,GACZ,OAAO,CAAC,OAAO,CAAC,CAyDlB;AAkDD;;GAEG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,iBAAiB,CAa9E;AAED;;GAEG;AACH,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,mBAAmB,CAYlF"}
|
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
*
|
|
8
8
|
* @module procedure/builder
|
|
9
9
|
*/
|
|
10
|
+
import { GuardError } from '../errors.js';
|
|
10
11
|
// ============================================================================
|
|
11
12
|
// Builder Factory
|
|
12
13
|
// ============================================================================
|
|
@@ -36,6 +37,7 @@ export function procedure() {
|
|
|
36
37
|
inputSchema: undefined,
|
|
37
38
|
outputSchema: undefined,
|
|
38
39
|
middlewares: [],
|
|
40
|
+
guards: [],
|
|
39
41
|
restOverride: undefined,
|
|
40
42
|
});
|
|
41
43
|
}
|
|
@@ -85,6 +87,18 @@ function createBuilder(state) {
|
|
|
85
87
|
middlewares: [...state.middlewares, typedMiddleware],
|
|
86
88
|
});
|
|
87
89
|
},
|
|
90
|
+
/**
|
|
91
|
+
* Adds an authorization guard
|
|
92
|
+
*
|
|
93
|
+
* Accepts guards with partial context types (contravariant).
|
|
94
|
+
* Guards typed for `{ auth?: AuthContext }` work with full `BaseContext`.
|
|
95
|
+
*/
|
|
96
|
+
guard(guardDef) {
|
|
97
|
+
return createBuilder({
|
|
98
|
+
...state,
|
|
99
|
+
guards: [...state.guards, guardDef],
|
|
100
|
+
});
|
|
101
|
+
},
|
|
88
102
|
/**
|
|
89
103
|
* Sets REST route override
|
|
90
104
|
*/
|
|
@@ -131,6 +145,7 @@ function compileProcedure(type, handler, state) {
|
|
|
131
145
|
inputSchema: state.inputSchema,
|
|
132
146
|
outputSchema: state.outputSchema,
|
|
133
147
|
middlewares: typedMiddlewares,
|
|
148
|
+
guards: state.guards,
|
|
134
149
|
restOverride: state.restOverride,
|
|
135
150
|
// Store pre-compiled executor for performance
|
|
136
151
|
_precompiledExecutor: precompiledExecutor,
|
|
@@ -252,11 +267,30 @@ export function defineProcedures(namespace, procedures) {
|
|
|
252
267
|
* ```
|
|
253
268
|
*/
|
|
254
269
|
export async function executeProcedure(procedure, rawInput, ctx) {
|
|
255
|
-
// Step 1:
|
|
270
|
+
// Step 1: Execute guards if any
|
|
271
|
+
if (procedure.guards.length > 0) {
|
|
272
|
+
// Defensive check: ensure request and reply exist in context
|
|
273
|
+
// These are required for guard execution but may be missing in test contexts
|
|
274
|
+
const request = ctx.request;
|
|
275
|
+
const reply = ctx.reply;
|
|
276
|
+
if (!request || !reply) {
|
|
277
|
+
throw new GuardError('context', 'Guard execution requires request and reply in context. ' +
|
|
278
|
+
'Ensure the procedure is being called from a valid HTTP context.', 500);
|
|
279
|
+
}
|
|
280
|
+
for (const guard of procedure.guards) {
|
|
281
|
+
const passed = await guard.check(ctx, request, reply);
|
|
282
|
+
if (!passed) {
|
|
283
|
+
const statusCode = guard.statusCode ?? 403;
|
|
284
|
+
const message = guard.message ?? `Guard "${guard.name}" check failed`;
|
|
285
|
+
throw new GuardError(guard.name, message, statusCode);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
// Step 2: Validate input if schema provided
|
|
256
290
|
const input = procedure.inputSchema
|
|
257
291
|
? procedure.inputSchema.parse(rawInput)
|
|
258
292
|
: rawInput;
|
|
259
|
-
// Step
|
|
293
|
+
// Step 3: Execute handler (with or without middleware)
|
|
260
294
|
let result;
|
|
261
295
|
if (procedure._precompiledExecutor) {
|
|
262
296
|
// PERFORMANCE: Use pre-compiled middleware chain executor
|
|
@@ -270,7 +304,7 @@ export async function executeProcedure(procedure, rawInput, ctx) {
|
|
|
270
304
|
// Fallback: Build middleware chain dynamically (should not normally happen)
|
|
271
305
|
result = await executeMiddlewareChainFallback(procedure.middlewares, input, ctx, async () => procedure.handler({ input, ctx }));
|
|
272
306
|
}
|
|
273
|
-
// Step
|
|
307
|
+
// Step 4: Validate output if schema provided
|
|
274
308
|
if (procedure.outputSchema) {
|
|
275
309
|
return procedure.outputSchema.parse(result);
|
|
276
310
|
}
|
|
@@ -324,7 +358,8 @@ export function isCompiledProcedure(value) {
|
|
|
324
358
|
const obj = value;
|
|
325
359
|
return ((obj.type === 'query' || obj.type === 'mutation') &&
|
|
326
360
|
typeof obj.handler === 'function' &&
|
|
327
|
-
Array.isArray(obj.middlewares)
|
|
361
|
+
Array.isArray(obj.middlewares) &&
|
|
362
|
+
Array.isArray(obj.guards));
|
|
328
363
|
}
|
|
329
364
|
/**
|
|
330
365
|
* Type guard to check if a value is a procedure collection
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"builder.js","sourceRoot":"","sources":["../../src/procedure/builder.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;
|
|
1
|
+
{"version":3,"file":"builder.js","sourceRoot":"","sources":["../../src/procedure/builder.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAIH,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAkB1C,+EAA+E;AAC/E,kBAAkB;AAClB,+EAA+E;AAE/E;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,UAAU,SAAS;IACvB,OAAO,aAAa,CAAC;QACnB,WAAW,EAAE,SAAS;QACtB,YAAY,EAAE,SAAS;QACvB,WAAW,EAAE,EAAE;QACf,MAAM,EAAE,EAAE;QACV,YAAY,EAAE,SAAS;KACxB,CAAC,CAAC;AACL,CAAC;AAED,+EAA+E;AAC/E,kCAAkC;AAClC,+EAA+E;AAE/E;;;;;;;GAOG;AACH,SAAS,aAAa,CACpB,KAA0B;IAE1B,OAAO;QACL;;WAEG;QACH,KAAK,CACH,MAAe;YAEf,+CAA+C;YAC/C,uDAAuD;YACvD,OAAO,aAAa,CAAgD;gBAClE,GAAG,KAAK;gBACR,WAAW,EAAE,MAAM;aACpB,CAAC,CAAC;QACL,CAAC;QAED;;WAEG;QACH,MAAM,CACJ,MAAe;YAEf,gDAAgD;YAChD,OAAO,aAAa,CAA+C;gBACjE,GAAG,KAA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AAC,WAAW,CAAC;QAC9B,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,CAC1B,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,qBAAqB,CAAC,KAAc;IAClD,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;QAChD,OAAO,KAAK,CAAC;IACf,CAAC;IAED,MAAM,GAAG,GAAG,KAAgC,CAAC;IAE7C,OAAO,CACL,OAAO,GAAG,CAAC,SAAS,KAAK,QAAQ;QACjC,OAAO,GAAG,CAAC,UAAU,KAAK,QAAQ;QAClC,GAAG,CAAC,UAAU,KAAK,IAAI,CACxB,CAAC;AACJ,CAAC"}
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
*/
|
|
12
12
|
import type { BaseContext } from '@veloxts/core';
|
|
13
13
|
import type { ZodType, ZodTypeDef } from 'zod';
|
|
14
|
-
import type { CompiledProcedure, MiddlewareFunction, ProcedureHandler, RestRouteOverride } from '../types.js';
|
|
14
|
+
import type { CompiledProcedure, GuardLike, MiddlewareFunction, ProcedureHandler, RestRouteOverride } from '../types.js';
|
|
15
15
|
/**
|
|
16
16
|
* Internal state type that accumulates type information through the builder chain
|
|
17
17
|
*
|
|
@@ -137,6 +137,39 @@ export interface ProcedureBuilder<TInput = unknown, TOutput = unknown, TContext
|
|
|
137
137
|
* ```
|
|
138
138
|
*/
|
|
139
139
|
use<TNewContext extends BaseContext = TContext>(middleware: MiddlewareFunction<TInput, TContext, TNewContext, TOutput>): ProcedureBuilder<TInput, TOutput, TNewContext>;
|
|
140
|
+
/**
|
|
141
|
+
* Adds an authorization guard to the procedure
|
|
142
|
+
*
|
|
143
|
+
* Guards are executed before the handler and middleware chain.
|
|
144
|
+
* If the guard check fails, a 401/403 error is returned.
|
|
145
|
+
*
|
|
146
|
+
* Guards are compatible with @veloxts/auth guards (authenticated, hasRole, etc.)
|
|
147
|
+
* but can be any object implementing the GuardLike interface.
|
|
148
|
+
*
|
|
149
|
+
* The guard type parameter accepts guards that require a subset of the current
|
|
150
|
+
* context (contravariant). This allows guards typed for `{ auth?: AuthContext }`
|
|
151
|
+
* to work with the full `BaseContext` since BaseContext extends that shape.
|
|
152
|
+
*
|
|
153
|
+
* @template TGuardContext - The context type required by the guard (must be a subset of TContext)
|
|
154
|
+
* @param guard - Guard definition to apply
|
|
155
|
+
* @returns Same builder (no type changes)
|
|
156
|
+
*
|
|
157
|
+
* @example
|
|
158
|
+
* ```typescript
|
|
159
|
+
* import { authenticated, hasRole } from '@veloxts/auth';
|
|
160
|
+
*
|
|
161
|
+
* // Require authentication
|
|
162
|
+
* procedure()
|
|
163
|
+
* .guard(authenticated)
|
|
164
|
+
* .query(async ({ ctx }) => { ... });
|
|
165
|
+
*
|
|
166
|
+
* // Require admin role
|
|
167
|
+
* procedure()
|
|
168
|
+
* .guard(hasRole('admin'))
|
|
169
|
+
* .mutation(async ({ input, ctx }) => { ... });
|
|
170
|
+
* ```
|
|
171
|
+
*/
|
|
172
|
+
guard<TGuardContext extends Partial<TContext>>(guard: GuardLike<TGuardContext>): ProcedureBuilder<TInput, TOutput, TContext>;
|
|
140
173
|
/**
|
|
141
174
|
* Configures REST route override
|
|
142
175
|
*
|
|
@@ -205,6 +238,8 @@ export interface BuilderRuntimeState {
|
|
|
205
238
|
outputSchema?: ValidSchema;
|
|
206
239
|
/** Middleware chain */
|
|
207
240
|
middlewares: MiddlewareFunction<unknown, BaseContext, BaseContext, unknown>[];
|
|
241
|
+
/** Guards to execute before handler */
|
|
242
|
+
guards: GuardLike<unknown>[];
|
|
208
243
|
/** REST route override */
|
|
209
244
|
restOverride?: RestRouteOverride;
|
|
210
245
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/procedure/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,eAAe,CAA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AE,gBAAgB,CAAC,MAAM,EAAE,OAAO,EAAE,QAAQ,CAAC,GACnD,iBAAiB,CAAC,MAAM,EAAE,OAAO,EAAE,QAAQ,CAAC,CAAC;CACjD;AAMD;;;;;GAKG;AACH,MAAM,WAAW,mBAAmB;IAClC,8BAA8B;IAC9B,WAAW,CAAC,EAAE,WAAW,CAAC;IAC1B,+BAA+B;IAC/B,YAAY,CAAC,EAAE,WAAW,CAAC;IAC3B,uBAAuB;IACvB,WAAW,EAAE,kBAAkB,CAAC,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,OAAO,CAAC,EAAE,CAAC;IAC9E,uCAAuC;IACvC,MAAM,EAAE,SAAS,CAAC,OAAO,CAAC,EAAE,CAAC;IAC7B,0BAA0B;IAC1B,YAAY,CAAC,EAAE,iBAAiB,CAAC;CAClC;AAMD;;;;;;;;;;;;GAYG;AAEH,MAAM,MAAM,oBAAoB,GAAG,MAAM,CAAC,MAAM,EAAE,iBAAiB,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC;AAEpF;;;;;GAKG;AACH,MAAM,MAAM,eAAe,CAAC,CAAC,SAAS,oBAAoB,IAAI;KAC3D,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;CACrB,CAAC"}
|
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;;;;;;;;GAQ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AgC,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
|
@@ -148,7 +148,6 @@ function gatherInput(request, route) {
|
|
|
148
148
|
case 'PATCH':
|
|
149
149
|
// PUT/PATCH: params (for :id) + body (for data)
|
|
150
150
|
return { ...params, ...body };
|
|
151
|
-
case 'POST':
|
|
152
151
|
default:
|
|
153
152
|
// POST: body only (no ID in params for creates)
|
|
154
153
|
return request.body;
|
|
@@ -158,7 +157,8 @@ function gatherInput(request, route) {
|
|
|
158
157
|
* Extract context from Fastify request
|
|
159
158
|
*
|
|
160
159
|
* The context is decorated onto the request by @veloxts/core's onRequest hook.
|
|
161
|
-
* This function expects the context to already be present
|
|
160
|
+
* This function expects the context to already be present and also merges
|
|
161
|
+
* any auth properties added by @veloxts/auth's preHandler hook.
|
|
162
162
|
*/
|
|
163
163
|
function getContextFromRequest(request) {
|
|
164
164
|
// Type assertion is safe because @veloxts/core decorates this in onRequest hook
|
|
@@ -168,7 +168,17 @@ function getContextFromRequest(request) {
|
|
|
168
168
|
if (!requestWithContext.context) {
|
|
169
169
|
throw new ConfigurationError('Request context not found. Ensure VeloxApp is started before registering routes.');
|
|
170
170
|
}
|
|
171
|
-
|
|
171
|
+
// Merge auth properties from request if @veloxts/auth has added them
|
|
172
|
+
// This allows guards to access ctx.auth and ctx.user
|
|
173
|
+
const context = requestWithContext.context;
|
|
174
|
+
if (requestWithContext.auth !== undefined || requestWithContext.user !== undefined) {
|
|
175
|
+
return {
|
|
176
|
+
...context,
|
|
177
|
+
auth: requestWithContext.auth,
|
|
178
|
+
user: requestWithContext.user,
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
return context;
|
|
172
182
|
}
|
|
173
183
|
// ============================================================================
|
|
174
184
|
// Route Registration
|
|
@@ -209,11 +219,13 @@ function getContextFromRequest(request) {
|
|
|
209
219
|
* ```
|
|
210
220
|
*/
|
|
211
221
|
export function registerRestRoutes(server, collections, options = {}) {
|
|
212
|
-
const { prefix = '/api' } = options;
|
|
222
|
+
const { prefix = '/api', _prefixHandledByFastify = false } = options;
|
|
213
223
|
for (const collection of collections) {
|
|
214
224
|
const routes = generateRestRoutes(collection);
|
|
215
225
|
for (const route of routes) {
|
|
216
|
-
|
|
226
|
+
// When used with server.register(), Fastify handles the prefix automatically.
|
|
227
|
+
// When used in legacy mode, we prepend the prefix manually.
|
|
228
|
+
const fullPath = _prefixHandledByFastify ? route.path : `${prefix}${route.path}`;
|
|
217
229
|
const handler = createRouteHandler(route);
|
|
218
230
|
// Register route based on method
|
|
219
231
|
switch (route.method) {
|
|
@@ -294,18 +306,19 @@ export function createRoutesRegistrar(collections, options = {}) {
|
|
|
294
306
|
};
|
|
295
307
|
}
|
|
296
308
|
/**
|
|
297
|
-
* Creates a
|
|
309
|
+
* Creates a Fastify plugin for REST endpoints from procedure collections.
|
|
298
310
|
*
|
|
299
311
|
* This is the preferred way to register procedure collections as REST routes.
|
|
300
|
-
*
|
|
312
|
+
* The function returns a dual-mode plugin that works with both modern Fastify
|
|
313
|
+
* `register()` pattern and the legacy `app.routes()` pattern.
|
|
301
314
|
*
|
|
302
315
|
* @param collections - Procedure collections to register as REST routes
|
|
303
|
-
* @param
|
|
304
|
-
* @returns A
|
|
316
|
+
* @param defaultOptions - Default REST adapter options (can be overridden at registration)
|
|
317
|
+
* @returns A Fastify plugin that can be used with `server.register()` or called directly
|
|
305
318
|
*
|
|
306
|
-
* @example
|
|
319
|
+
* @example Modern API (recommended)
|
|
307
320
|
* ```typescript
|
|
308
|
-
* import
|
|
321
|
+
* import Fastify from 'fastify';
|
|
309
322
|
* import { rest, defineProcedures, procedure } from '@veloxts/router';
|
|
310
323
|
*
|
|
311
324
|
* const users = defineProcedures('users', {
|
|
@@ -315,17 +328,81 @@ export function createRoutesRegistrar(collections, options = {}) {
|
|
|
315
328
|
* .mutation(async ({ input }) => ({ id: '1', ...input })),
|
|
316
329
|
* });
|
|
317
330
|
*
|
|
331
|
+
* const server = Fastify();
|
|
332
|
+
*
|
|
333
|
+
* // Register with Fastify's built-in prefix handling
|
|
334
|
+
* await server.register(rest([users]), { prefix: '/api' });
|
|
335
|
+
*
|
|
336
|
+
* // Generates:
|
|
337
|
+
* // GET /api/users -> listUsers
|
|
338
|
+
* // POST /api/users -> createUser
|
|
339
|
+
* ```
|
|
340
|
+
*
|
|
341
|
+
* @example Legacy API (backward compatible)
|
|
342
|
+
* ```typescript
|
|
343
|
+
* import { veloxApp } from '@veloxts/core';
|
|
344
|
+
* import { rest, defineProcedures, procedure } from '@veloxts/router';
|
|
345
|
+
*
|
|
318
346
|
* const app = await veloxApp();
|
|
319
347
|
*
|
|
320
|
-
* //
|
|
348
|
+
* // Legacy registration with manual prefix
|
|
321
349
|
* app.routes(rest([users], { prefix: '/api' }));
|
|
322
350
|
*
|
|
323
351
|
* await app.start();
|
|
324
352
|
* ```
|
|
353
|
+
*
|
|
354
|
+
* @example Merging options
|
|
355
|
+
* ```typescript
|
|
356
|
+
* // Options passed to rest() are defaults
|
|
357
|
+
* const plugin = rest([users], { onError: customHandler });
|
|
358
|
+
*
|
|
359
|
+
* // Options passed to register() override/merge with defaults
|
|
360
|
+
* await server.register(plugin, { prefix: '/v1' });
|
|
361
|
+
* ```
|
|
325
362
|
*/
|
|
326
|
-
export function rest(collections,
|
|
327
|
-
|
|
328
|
-
|
|
363
|
+
export function rest(collections, defaultOptions = {}) {
|
|
364
|
+
/**
|
|
365
|
+
* The async plugin function for Fastify's register() method.
|
|
366
|
+
* When used with register(), Fastify handles the prefix automatically.
|
|
367
|
+
*/
|
|
368
|
+
const plugin = async (instance, opts) => {
|
|
369
|
+
// Merge default options with options passed to register()
|
|
370
|
+
// Options from register() take precedence
|
|
371
|
+
const mergedOptions = {
|
|
372
|
+
...defaultOptions,
|
|
373
|
+
...opts,
|
|
374
|
+
// When used via register(), Fastify handles the prefix automatically
|
|
375
|
+
// We signal this to registerRestRoutes so it doesn't double-prefix
|
|
376
|
+
_prefixHandledByFastify: true,
|
|
377
|
+
};
|
|
378
|
+
registerRestRoutes(instance, collections, mergedOptions);
|
|
379
|
+
};
|
|
380
|
+
/**
|
|
381
|
+
* Create a callable wrapper that supports both patterns:
|
|
382
|
+
* 1. As a Fastify plugin: server.register(rest([...]), opts)
|
|
383
|
+
* 2. As a direct call: rest([...])(server) - legacy pattern
|
|
384
|
+
*
|
|
385
|
+
* The trick is that FastifyPluginAsync expects (instance, opts) => Promise<void>
|
|
386
|
+
* but the legacy pattern expects (server) => void.
|
|
387
|
+
*
|
|
388
|
+
* We detect which mode based on the second argument:
|
|
389
|
+
* - If second arg is undefined/missing: legacy callable mode
|
|
390
|
+
* - If second arg is present: Fastify plugin mode (called by register())
|
|
391
|
+
*/
|
|
392
|
+
const dualModePlugin = (instance, opts) => {
|
|
393
|
+
// If opts is provided, we're being called by Fastify's register()
|
|
394
|
+
// In this case, return the promise from the async plugin
|
|
395
|
+
if (opts !== undefined) {
|
|
396
|
+
return plugin(instance, opts);
|
|
397
|
+
}
|
|
398
|
+
// Legacy mode: called directly as rest([...])(server)
|
|
399
|
+
// Use default options and apply prefix manually
|
|
400
|
+
registerRestRoutes(instance, collections, defaultOptions);
|
|
329
401
|
};
|
|
402
|
+
// Cast to RestPlugin - TypeScript needs help understanding this dual nature
|
|
403
|
+
// The function satisfies both signatures:
|
|
404
|
+
// - (instance, opts) => Promise<void> for Fastify register
|
|
405
|
+
// - (server) => void for legacy callable
|
|
406
|
+
return dualModePlugin;
|
|
330
407
|
}
|
|
331
408
|
//# 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,aAAa,CAAC;
|
|
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,aAAa,CAAC;AAqDnE,+EAA+E;AAC/E,mBAAmB;AACnB,+EAA+E;AAE/E;;;;;;;;;;GAUG;AACH,MAAM,UAAU,kBAAkB,CAAC,UAA+B;IAChE,MAAM,MAAM,GAAgB,EAAE,CAAC;IAE/B,KAAK,MAA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ACH,MAAM,UAAU,IAAI,CAClB,WAAkC,EAClC,iBAAqC,EAAE;IAEvC;;;OAGG;IACH,MAAM,MAAM,GAA2C,KAAK,EAC1D,QAAyB,EACzB,IAAwB,EACT,EAAE;QACjB,0DAA0D;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
9
|
export { 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,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,
|
|
11
|
+
generateRestRoutes, getRouteSummary, registerRestRoutes,
|
|
12
|
+
// Succinct API (preferred)
|
|
13
|
+
rest, } from './adapter.js';
|
|
14
14
|
export { 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,aAAa,EACb,uBAAuB,EACvB,iBAAiB,EACjB,qBAAqB,GACtB,MAAM,aAAa,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,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAE/C;;;;;GAKG;AACH,MAAM,MAAM,SAAS,GAAG,aAAa,CAAC;
|
|
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,cAA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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,
|
package/dist/trpc/adapter.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"adapter.js","sourceRoot":"","sources":["../../src/trpc/adapter.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAGH,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"adapter.js","sourceRoot":"","sources":["../../src/trpc/adapter.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAGH,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAYnD,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAG5C,+EAA+E;AAC/E,sBAAsB;AACtB,+EAA+E;AAE/E,iDAAiD;AACjD,2EAA2E;AAC3E,MAAM,QAAQ,GAAG,QAAQ,CAAC,OAAO,EAAe,CAAC,MAAM,EAAE,CAAC;AAU1D;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,UAAU;IACxB,uFAAuF;IACvF,mFAAmF;IACnF,OAAO,QAAQ,CAAC,OAAO,EAAe,CAAC,MAAM,EAAE,CAAC;AAClD,CAAC;AAED,+EAA+E;AAC/E,kBAAkB;AAClB,+EAA+E;AAE/E;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,eAAe,CAC7B,CAA4B,EAC5B,UAA+B;IAE/B,8CAA8C;IAC9C,MAAM,YAAY,GAA0D,EAAE,CAAC;IAE/E,KAAK,MAAM,CAAC,IAAI,EAAE,SAAS,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,UAAU,CAAC,UAAU,CAAC,EAAE,CAAC;QACtE,YAAY,CAAC,IA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AU,CAAC,IAAI,WAAW,CAAC,CAAC,CAAC,CAAC,WAAW,CAAC;IAElG,4DAA4D;IAC5D,IAAI,YAAY,CAAC,KAAK,CAAC,EAAE,CAAC;QACxB,OAAO,IAAI,SAAS,CAAC;YACnB,IAAI,EAAE,QAAQ;YACd,OAAO,EAAE,KAAK,CAAC,OAAO;YACtB,KAAK,EAAE;gBACL,IAAI,EAAE,KAAK,CAAC,IAAI;gBAChB,SAAS,EAAE,KAAK,CAAC,SAAS;aAC3B;SACF,CAAC,CAAC;IACL,CAAC;IAED,OAAO,IAAI,SAAS,CAAC;QACnB,IAAI,EAAE,QAAQ;QACd,OAAO,EAAE,KAAK,CAAC,OAAO;QACtB,KAAK,EAAE,KAAK,CAAC,IAAI;KAClB,CAAC,CAAC;AACL,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,gBAAgB,CAAC,KAAgB;IAC/C,OAAO,OAAO,KAAK,CAAC,KAAK,KAAK,QAAQ,CAAC;AACzC,CAAC;AAgBD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,CAAC,KAAK,UAAU,kBAAkB,CACtC,MAAuB,EACvB,OAA0B;IAE1B,mEAAmE;IACnE,MAAM,EAAE,iBAAiB,EAAE,GAAG,MAAM,MAAM,CAAC,+BAA+B,CAAC,CAAC;IAE5E,MAAM,MAAM,CAAC,QAAQ,CAAC,iBAAiB,EAAE;QACvC,MAAM,EAAE,OAAO,CAAC,MAAM,IAAI,OAAO;QACjC,WAAW,EAAE;YACX,MAAM,EAAE,OAAO,CAAC,MAAM;YACtB,aAAa,EAAE,wBAAwB,EAAE;SAC1C;KACF,CAAC,CAAC;AACL,CAAC"}
|
package/dist/types.d.ts
CHANGED
|
@@ -17,8 +17,7 @@ export type ProcedureType = 'query' | 'mutation';
|
|
|
17
17
|
/**
|
|
18
18
|
* HTTP methods supported by the REST adapter
|
|
19
19
|
*
|
|
20
|
-
*
|
|
21
|
-
* v1.1+: Full REST (GET, POST, PUT, PATCH, DELETE)
|
|
20
|
+
* Full REST support: GET, POST, PUT, PATCH, DELETE
|
|
22
21
|
*/
|
|
23
22
|
export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
24
23
|
/**
|
|
@@ -28,8 +27,9 @@ export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
|
28
27
|
* - getUser -> GET
|
|
29
28
|
* - listUsers -> GET
|
|
30
29
|
* - createUser -> POST
|
|
31
|
-
* - updateUser -> PUT
|
|
32
|
-
* -
|
|
30
|
+
* - updateUser -> PUT
|
|
31
|
+
* - patchUser -> PATCH
|
|
32
|
+
* - deleteUser -> DELETE
|
|
33
33
|
*/
|
|
34
34
|
export declare const PROCEDURE_METHOD_MAP: Record<string, HttpMethod>;
|
|
35
35
|
/**
|
|
@@ -67,6 +67,37 @@ export interface ProcedureHandlerArgs<TInput, TContext extends BaseContext = Bas
|
|
|
67
67
|
* @template TContext - The context type
|
|
68
68
|
*/
|
|
69
69
|
export type ProcedureHandler<TInput, TOutput, TContext extends BaseContext = BaseContext> = (args: ProcedureHandlerArgs<TInput, TContext>) => TOutput | Promise<TOutput>;
|
|
70
|
+
/**
|
|
71
|
+
* Guard check function type
|
|
72
|
+
*
|
|
73
|
+
* The function receives the context, request, and reply objects.
|
|
74
|
+
* Request and reply are typed as `any` to enable compatibility with more
|
|
75
|
+
* specific types (like FastifyRequest/FastifyReply from @veloxts/auth).
|
|
76
|
+
*
|
|
77
|
+
* This design allows guards defined with specific Fastify types to be
|
|
78
|
+
* used interchangeably with guards that use generic types.
|
|
79
|
+
*
|
|
80
|
+
* @template TContext - The context type the guard operates on
|
|
81
|
+
*/
|
|
82
|
+
export type GuardCheckFunction<TContext = unknown> = (ctx: TContext, request: any, reply: any) => boolean | Promise<boolean>;
|
|
83
|
+
/**
|
|
84
|
+
* Guard-like type for authorization checks
|
|
85
|
+
*
|
|
86
|
+
* This interface is compatible with @veloxts/auth guards but doesn't create
|
|
87
|
+
* a hard dependency. Any object matching this shape can be used as a guard.
|
|
88
|
+
*
|
|
89
|
+
* @template TContext - The context type the guard operates on
|
|
90
|
+
*/
|
|
91
|
+
export interface GuardLike<TContext = unknown> {
|
|
92
|
+
/** Guard name for error messages */
|
|
93
|
+
name: string;
|
|
94
|
+
/** Guard check function - returns true if access is allowed */
|
|
95
|
+
check: GuardCheckFunction<TContext>;
|
|
96
|
+
/** Custom error message (optional) */
|
|
97
|
+
message?: string;
|
|
98
|
+
/** HTTP status code on failure (default: 403) */
|
|
99
|
+
statusCode?: number;
|
|
100
|
+
}
|
|
70
101
|
/**
|
|
71
102
|
* Result returned by the next() function in middleware
|
|
72
103
|
*
|
|
@@ -184,6 +215,8 @@ export interface CompiledProcedure<TInput = unknown, TOutput = unknown, TContext
|
|
|
184
215
|
};
|
|
185
216
|
/** Middleware chain to execute before handler */
|
|
186
217
|
readonly middlewares: ReadonlyArray<MiddlewareFunction<TInput, TContext, TContext, TOutput>>;
|
|
218
|
+
/** Guards to execute before handler (checked before middleware) */
|
|
219
|
+
readonly guards: ReadonlyArray<GuardLike<TContext>>;
|
|
187
220
|
/** REST route override (if specified) */
|
|
188
221
|
readonly restOverride?: RestRouteOverride;
|
|
189
222
|
/**
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAMjD;;;;;GAKG;AACH,MAAM,MAAM,aAAa,GAAG,OAAO,GAAG,UAAU,CAAC;AAEjD
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAMjD;;;;;GAKG;AACH,MAAM,MAAM,aAAa,GAAG,OAAO,G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AC,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;KAClD;CACF,CAAC"}
|
package/dist/types.js
CHANGED
|
@@ -13,8 +13,9 @@
|
|
|
13
13
|
* - getUser -> GET
|
|
14
14
|
* - listUsers -> GET
|
|
15
15
|
* - createUser -> POST
|
|
16
|
-
* - updateUser -> PUT
|
|
17
|
-
* -
|
|
16
|
+
* - updateUser -> PUT
|
|
17
|
+
* - patchUser -> PATCH
|
|
18
|
+
* - deleteUser -> DELETE
|
|
18
19
|
*/
|
|
19
20
|
export const PROCEDURE_METHOD_MAP = {
|
|
20
21
|
get: 'GET',
|
package/dist/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAuBH;;;;;;;;;;GAUG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAA+B;IAC9D,GAAG,EAAE,KAAK;IACV,IAAI,EAAE,KAAK;IACX,IAAI,EAAE,KAAK;IACX,MAAM,EAAE,MAAM;IACd,GAAG,EAAE,MAAM;IACX,MAAM,EAAE,KAAK;IACb,IAAI,EAAE,KAAK;IACX,KAAK,EAAE,OAAO;IACd,MAAM,EAAE,QAAQ;IAChB,MAAM,EAAE,QAAQ;CACR,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@veloxts/router",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.5",
|
|
4
4
|
"description": "Procedure definitions with tRPC and REST routing for VeloxTS framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -11,11 +11,24 @@
|
|
|
11
11
|
"import": "./dist/index.js"
|
|
12
12
|
}
|
|
13
13
|
},
|
|
14
|
+
"tsd": {
|
|
15
|
+
"directory": "src/__type-tests__",
|
|
16
|
+
"compilerOptions": {
|
|
17
|
+
"paths": {
|
|
18
|
+
"@veloxts/core": [
|
|
19
|
+
"../core/dist"
|
|
20
|
+
],
|
|
21
|
+
"@veloxts/router": [
|
|
22
|
+
"dist"
|
|
23
|
+
]
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
},
|
|
14
27
|
"dependencies": {
|
|
15
28
|
"@trpc/server": "11.7.2",
|
|
16
29
|
"fastify": "5.6.2",
|
|
17
|
-
"@veloxts/core": "0.3.
|
|
18
|
-
"@veloxts/validation": "0.3.
|
|
30
|
+
"@veloxts/core": "0.3.5",
|
|
31
|
+
"@veloxts/validation": "0.3.5"
|
|
19
32
|
},
|
|
20
33
|
"devDependencies": {
|
|
21
34
|
"@vitest/coverage-v8": "4.0.15",
|
|
@@ -60,6 +73,7 @@
|
|
|
60
73
|
"build": "tsc",
|
|
61
74
|
"dev": "tsc --watch",
|
|
62
75
|
"type-check": "tsc --noEmit",
|
|
76
|
+
"test:types": "tsd",
|
|
63
77
|
"clean": "rm -rf dist tsconfig.tsbuildinfo",
|
|
64
78
|
"test": "vitest run",
|
|
65
79
|
"test:watch": "vitest",
|