glasswork 0.7.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/authorization/ability-middleware.d.ts +61 -0
- package/dist/authorization/ability-middleware.d.ts.map +1 -0
- package/dist/authorization/ability-middleware.js +74 -0
- package/dist/authorization/ability-middleware.js.map +1 -0
- package/dist/authorization/index.d.ts +46 -0
- package/dist/authorization/index.d.ts.map +1 -0
- package/dist/authorization/index.js +46 -0
- package/dist/authorization/index.js.map +1 -0
- package/dist/authorization/route-helpers.d.ts +106 -0
- package/dist/authorization/route-helpers.d.ts.map +1 -0
- package/dist/authorization/route-helpers.js +136 -0
- package/dist/authorization/route-helpers.js.map +1 -0
- package/dist/authorization/service-proxy.d.ts +59 -0
- package/dist/authorization/service-proxy.d.ts.map +1 -0
- package/dist/authorization/service-proxy.js +110 -0
- package/dist/authorization/service-proxy.js.map +1 -0
- package/dist/authorization/types.d.ts +58 -0
- package/dist/authorization/types.d.ts.map +1 -0
- package/dist/authorization/types.js +7 -0
- package/dist/authorization/types.js.map +1 -0
- package/dist/core/bootstrap.d.ts +1 -1
- package/dist/core/bootstrap.d.ts.map +1 -1
- package/dist/core/bootstrap.js +96 -4
- package/dist/core/bootstrap.js.map +1 -1
- package/dist/core/types.d.ts +71 -0
- package/dist/core/types.d.ts.map +1 -1
- package/dist/hooks/authorization-hook.d.ts +100 -0
- package/dist/hooks/authorization-hook.d.ts.map +1 -0
- package/dist/hooks/authorization-hook.js +93 -0
- package/dist/hooks/authorization-hook.js.map +1 -0
- package/dist/hooks/index.d.ts +54 -0
- package/dist/hooks/index.d.ts.map +1 -0
- package/dist/hooks/index.js +55 -0
- package/dist/hooks/index.js.map +1 -0
- package/dist/hooks/service-hooks.d.ts +82 -0
- package/dist/hooks/service-hooks.d.ts.map +1 -0
- package/dist/hooks/service-hooks.js +158 -0
- package/dist/hooks/service-hooks.js.map +1 -0
- package/dist/hooks/types.d.ts +151 -0
- package/dist/hooks/types.d.ts.map +1 -0
- package/dist/hooks/types.js +2 -0
- package/dist/hooks/types.js.map +1 -0
- package/dist/http/route-helpers.d.ts +22 -0
- package/dist/http/route-helpers.d.ts.map +1 -1
- package/dist/http/route-helpers.js +21 -11
- package/dist/http/route-helpers.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/list-query/casl.d.ts +9 -8
- package/dist/list-query/casl.d.ts.map +1 -1
- package/dist/list-query/casl.js +29 -11
- package/dist/list-query/casl.js.map +1 -1
- package/dist/openapi/defaults.js +3 -3
- package/dist/openapi/openapi-processors.js +1 -1
- package/dist/openapi/openapi-processors.js.map +1 -1
- package/dist/openapi/openapi-transformer.d.ts +0 -5
- package/dist/openapi/openapi-transformer.d.ts.map +1 -1
- package/dist/openapi/openapi-transformer.js +0 -5
- package/dist/openapi/openapi-transformer.js.map +1 -1
- package/dist/utils/pagination.d.ts +48 -0
- package/dist/utils/pagination.d.ts.map +1 -0
- package/dist/utils/pagination.js +42 -0
- package/dist/utils/pagination.js.map +1 -0
- package/dist/utils/serialize-prisma-types.d.ts.map +1 -1
- package/dist/utils/serialize-prisma-types.js +1 -2
- package/dist/utils/serialize-prisma-types.js.map +1 -1
- package/package.json +5 -4
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Service hooks system for wrapping service method calls with before/after/error hooks.
|
|
3
|
+
*
|
|
4
|
+
* This module provides a flexible hook system that can be used for:
|
|
5
|
+
* - Authorization (using CASL)
|
|
6
|
+
* - Audit logging
|
|
7
|
+
* - Metrics and tracing
|
|
8
|
+
* - Error handling
|
|
9
|
+
* - Retry logic
|
|
10
|
+
* - Timeouts
|
|
11
|
+
* - Response transformation
|
|
12
|
+
*
|
|
13
|
+
* Hooks are transparent - they wrap service methods without modifying the service
|
|
14
|
+
* implementation, keeping business logic clean and framework-agnostic.
|
|
15
|
+
*
|
|
16
|
+
* @module hooks
|
|
17
|
+
*
|
|
18
|
+
* @example Basic Usage
|
|
19
|
+
* ```typescript
|
|
20
|
+
* import { authorizationHook, defineServiceMetadata } from 'glasswork';
|
|
21
|
+
*
|
|
22
|
+
* // Define metadata for service methods
|
|
23
|
+
* export const authServiceMetadata = defineServiceMetadata({
|
|
24
|
+
* deleteSession: {
|
|
25
|
+
* authorize: {
|
|
26
|
+
* action: 'delete',
|
|
27
|
+
* subject: 'Session'
|
|
28
|
+
* }
|
|
29
|
+
* }
|
|
30
|
+
* });
|
|
31
|
+
*
|
|
32
|
+
* // Configure hooks globally in bootstrap
|
|
33
|
+
* bootstrap({
|
|
34
|
+
* serviceHooks: [authorizationHook],
|
|
35
|
+
* serviceMetadata: {
|
|
36
|
+
* authService: authServiceMetadata
|
|
37
|
+
* }
|
|
38
|
+
* });
|
|
39
|
+
*
|
|
40
|
+
* // In routes, service calls are automatically wrapped
|
|
41
|
+
* export const authRoutes = createRoutes((router, { authService }) => {
|
|
42
|
+
* router.delete('/session', ...route({
|
|
43
|
+
* handler: async ({ context }) => {
|
|
44
|
+
* // This automatically checks authorization before executing!
|
|
45
|
+
* await authService.deleteSession(token);
|
|
46
|
+
* }
|
|
47
|
+
* }));
|
|
48
|
+
* });
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
// Built-in hooks
|
|
52
|
+
export { authorizationHook, defineAuthorize, } from './authorization-hook.js';
|
|
53
|
+
// Hook system
|
|
54
|
+
export { defineServiceMetadata, withServiceHooks, withServiceHooksMany, } from './service-hooks.js';
|
|
55
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/hooks/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiDG;AAEH,iBAAiB;AACjB,OAAO,EAEL,iBAAiB,EACjB,eAAe,GAChB,MAAM,yBAAyB,CAAC;AAEjC,cAAc;AACd,OAAO,EACL,qBAAqB,EACrB,gBAAgB,EAChB,oBAAoB,GACrB,MAAM,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import type { Context } from 'hono';
|
|
2
|
+
import type { ServiceHooksConfig, ServiceMethodMetadata } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Wraps a service with hooks that run before/after/on-error for method calls.
|
|
5
|
+
* This creates a transparent proxy that intercepts method calls and runs hooks
|
|
6
|
+
* while preserving all service functionality.
|
|
7
|
+
*
|
|
8
|
+
* **Note:** Hooks only run for method calls, not property access.
|
|
9
|
+
*
|
|
10
|
+
* @param service - The service instance to wrap
|
|
11
|
+
* @param config - Hook configuration
|
|
12
|
+
* @param honoContext - The current Hono request context
|
|
13
|
+
* @returns Proxied service with hooks
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* const authHook: ServiceHook = {
|
|
18
|
+
* before: async (context) => {
|
|
19
|
+
* if (context.metadata?.authorize) {
|
|
20
|
+
* authorize(context.honoContext, ...);
|
|
21
|
+
* }
|
|
22
|
+
* }
|
|
23
|
+
* };
|
|
24
|
+
*
|
|
25
|
+
* const wrappedService = withServiceHooks(authService, {
|
|
26
|
+
* hooks: [authHook],
|
|
27
|
+
* metadata: {
|
|
28
|
+
* deleteSession: {
|
|
29
|
+
* authorize: { action: 'delete', subject: 'Session' }
|
|
30
|
+
* }
|
|
31
|
+
* }
|
|
32
|
+
* }, honoContext);
|
|
33
|
+
*
|
|
34
|
+
* // Hooks automatically run when calling methods
|
|
35
|
+
* await wrappedService.deleteSession(token);
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export declare function withServiceHooks<T extends object>(service: T, config: ServiceHooksConfig, honoContext: Context): T;
|
|
39
|
+
/**
|
|
40
|
+
* Wraps multiple services with the same hooks configuration.
|
|
41
|
+
* Useful when you want to apply the same hooks to all services in a container.
|
|
42
|
+
*
|
|
43
|
+
* @param services - Object containing service instances
|
|
44
|
+
* @param config - Hook configuration (can be a function for per-service config)
|
|
45
|
+
* @param honoContext - The current Hono request context
|
|
46
|
+
* @returns Object with all services wrapped with hooks
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```typescript
|
|
50
|
+
* const wrappedServices = withServiceHooksMany(
|
|
51
|
+
* { authService, userService },
|
|
52
|
+
* (serviceName) => ({
|
|
53
|
+
* hooks: [authHook, auditHook],
|
|
54
|
+
* metadata: serviceMetadataRegistry[serviceName],
|
|
55
|
+
* serviceName,
|
|
56
|
+
* }),
|
|
57
|
+
* honoContext
|
|
58
|
+
* );
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
export declare function withServiceHooksMany<T extends Record<string, object>>(services: T, config: ServiceHooksConfig | ((serviceName: string, service: unknown) => ServiceHooksConfig), honoContext: Context): T;
|
|
62
|
+
/**
|
|
63
|
+
* Helper to define service method metadata with better type inference.
|
|
64
|
+
* This is just an identity function that helps TypeScript understand the structure.
|
|
65
|
+
*
|
|
66
|
+
* @param metadata - Service method metadata mapping
|
|
67
|
+
* @returns The same metadata (identity function)
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```typescript
|
|
71
|
+
* export const authServiceMetadata = defineServiceMetadata({
|
|
72
|
+
* deleteSession: {
|
|
73
|
+
* authorize: { action: 'delete', subject: 'Session' }
|
|
74
|
+
* },
|
|
75
|
+
* findById: {
|
|
76
|
+
* authorize: { action: 'read', subject: 'User' }
|
|
77
|
+
* }
|
|
78
|
+
* });
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
export declare function defineServiceMetadata(metadata: Record<string, ServiceMethodMetadata>): Record<string, ServiceMethodMetadata>;
|
|
82
|
+
//# sourceMappingURL=service-hooks.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"service-hooks.d.ts","sourceRoot":"","sources":["../../src/hooks/service-hooks.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AACpC,OAAO,KAAK,EAGV,kBAAkB,EAClB,qBAAqB,EACtB,MAAM,YAAY,CAAC;AAkDpB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,SAAS,MAAM,EAC/C,OAAO,EAAE,CAAC,EACV,MAAM,EAAE,kBAAkB,EAC1B,WAAW,EAAE,OAAO,GACnB,CAAC,CAqCH;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,oBAAoB,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EACnE,QAAQ,EAAE,CAAC,EACX,MAAM,EAAE,kBAAkB,GAAG,CAAC,CAAC,WAAW,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,KAAK,kBAAkB,CAAC,EAC5F,WAAW,EAAE,OAAO,GACnB,CAAC,CAcH;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,qBAAqB,CACnC,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,qBAAqB,CAAC,GAC9C,MAAM,CAAC,MAAM,EAAE,qBAAqB,CAAC,CAEvC"}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Execute a method with hooks (before/after/error).
|
|
3
|
+
* Extracted as a helper to reduce complexity.
|
|
4
|
+
*/
|
|
5
|
+
async function executeWithHooks(hookContext, hooks,
|
|
6
|
+
// biome-ignore lint/suspicious/noExplicitAny: Function type requires any
|
|
7
|
+
originalMethod, target, args) {
|
|
8
|
+
try {
|
|
9
|
+
// Run all before hooks
|
|
10
|
+
for (const hook of hooks) {
|
|
11
|
+
if (hook.before) {
|
|
12
|
+
await hook.before(hookContext, args);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
// Execute original method
|
|
16
|
+
let result = await originalMethod.apply(target, args);
|
|
17
|
+
// Run all after hooks (in order)
|
|
18
|
+
for (const hook of hooks) {
|
|
19
|
+
if (hook.after) {
|
|
20
|
+
const hookResult = await hook.after(hookContext, args, result);
|
|
21
|
+
// If hook returns a value, use it as the new result
|
|
22
|
+
if (hookResult !== undefined) {
|
|
23
|
+
result = hookResult;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return result;
|
|
28
|
+
}
|
|
29
|
+
catch (error) {
|
|
30
|
+
// Run all error hooks
|
|
31
|
+
for (const hook of hooks) {
|
|
32
|
+
if (hook.onError) {
|
|
33
|
+
await hook.onError(hookContext, args, error);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
// Re-throw the error after hooks
|
|
37
|
+
throw error;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Wraps a service with hooks that run before/after/on-error for method calls.
|
|
42
|
+
* This creates a transparent proxy that intercepts method calls and runs hooks
|
|
43
|
+
* while preserving all service functionality.
|
|
44
|
+
*
|
|
45
|
+
* **Note:** Hooks only run for method calls, not property access.
|
|
46
|
+
*
|
|
47
|
+
* @param service - The service instance to wrap
|
|
48
|
+
* @param config - Hook configuration
|
|
49
|
+
* @param honoContext - The current Hono request context
|
|
50
|
+
* @returns Proxied service with hooks
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```typescript
|
|
54
|
+
* const authHook: ServiceHook = {
|
|
55
|
+
* before: async (context) => {
|
|
56
|
+
* if (context.metadata?.authorize) {
|
|
57
|
+
* authorize(context.honoContext, ...);
|
|
58
|
+
* }
|
|
59
|
+
* }
|
|
60
|
+
* };
|
|
61
|
+
*
|
|
62
|
+
* const wrappedService = withServiceHooks(authService, {
|
|
63
|
+
* hooks: [authHook],
|
|
64
|
+
* metadata: {
|
|
65
|
+
* deleteSession: {
|
|
66
|
+
* authorize: { action: 'delete', subject: 'Session' }
|
|
67
|
+
* }
|
|
68
|
+
* }
|
|
69
|
+
* }, honoContext);
|
|
70
|
+
*
|
|
71
|
+
* // Hooks automatically run when calling methods
|
|
72
|
+
* await wrappedService.deleteSession(token);
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
export function withServiceHooks(service, config, honoContext) {
|
|
76
|
+
const { hooks, metadata = {}, serviceName } = config;
|
|
77
|
+
// Try to determine service name
|
|
78
|
+
const inferredServiceName = serviceName || service.constructor?.name || 'UnknownService';
|
|
79
|
+
return new Proxy(service, {
|
|
80
|
+
get(target, prop) {
|
|
81
|
+
// Only intercept string properties (method names)
|
|
82
|
+
if (typeof prop !== 'string') {
|
|
83
|
+
return Reflect.get(target, prop);
|
|
84
|
+
}
|
|
85
|
+
const originalValue = Reflect.get(target, prop);
|
|
86
|
+
// If it's not a function, return as-is
|
|
87
|
+
if (typeof originalValue !== 'function') {
|
|
88
|
+
return originalValue;
|
|
89
|
+
}
|
|
90
|
+
// Get metadata for this method
|
|
91
|
+
const methodMetadata = metadata[prop];
|
|
92
|
+
// Return wrapped function with hooks
|
|
93
|
+
return async function (...args) {
|
|
94
|
+
const hookContext = {
|
|
95
|
+
honoContext,
|
|
96
|
+
serviceName: inferredServiceName,
|
|
97
|
+
methodName: prop,
|
|
98
|
+
metadata: methodMetadata,
|
|
99
|
+
};
|
|
100
|
+
// biome-ignore lint/suspicious/noExplicitAny: originalValue is guaranteed to be a function by the check above
|
|
101
|
+
return executeWithHooks(hookContext, hooks, originalValue, target, args);
|
|
102
|
+
};
|
|
103
|
+
},
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Wraps multiple services with the same hooks configuration.
|
|
108
|
+
* Useful when you want to apply the same hooks to all services in a container.
|
|
109
|
+
*
|
|
110
|
+
* @param services - Object containing service instances
|
|
111
|
+
* @param config - Hook configuration (can be a function for per-service config)
|
|
112
|
+
* @param honoContext - The current Hono request context
|
|
113
|
+
* @returns Object with all services wrapped with hooks
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* ```typescript
|
|
117
|
+
* const wrappedServices = withServiceHooksMany(
|
|
118
|
+
* { authService, userService },
|
|
119
|
+
* (serviceName) => ({
|
|
120
|
+
* hooks: [authHook, auditHook],
|
|
121
|
+
* metadata: serviceMetadataRegistry[serviceName],
|
|
122
|
+
* serviceName,
|
|
123
|
+
* }),
|
|
124
|
+
* honoContext
|
|
125
|
+
* );
|
|
126
|
+
* ```
|
|
127
|
+
*/
|
|
128
|
+
export function withServiceHooksMany(services, config, honoContext) {
|
|
129
|
+
const result = {};
|
|
130
|
+
for (const [serviceName, service] of Object.entries(services)) {
|
|
131
|
+
const serviceConfig = typeof config === 'function' ? config(serviceName, service) : config;
|
|
132
|
+
result[serviceName] = withServiceHooks(service, { ...serviceConfig, serviceName }, honoContext);
|
|
133
|
+
}
|
|
134
|
+
return result;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Helper to define service method metadata with better type inference.
|
|
138
|
+
* This is just an identity function that helps TypeScript understand the structure.
|
|
139
|
+
*
|
|
140
|
+
* @param metadata - Service method metadata mapping
|
|
141
|
+
* @returns The same metadata (identity function)
|
|
142
|
+
*
|
|
143
|
+
* @example
|
|
144
|
+
* ```typescript
|
|
145
|
+
* export const authServiceMetadata = defineServiceMetadata({
|
|
146
|
+
* deleteSession: {
|
|
147
|
+
* authorize: { action: 'delete', subject: 'Session' }
|
|
148
|
+
* },
|
|
149
|
+
* findById: {
|
|
150
|
+
* authorize: { action: 'read', subject: 'User' }
|
|
151
|
+
* }
|
|
152
|
+
* });
|
|
153
|
+
* ```
|
|
154
|
+
*/
|
|
155
|
+
export function defineServiceMetadata(metadata) {
|
|
156
|
+
return metadata;
|
|
157
|
+
}
|
|
158
|
+
//# sourceMappingURL=service-hooks.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"service-hooks.js","sourceRoot":"","sources":["../../src/hooks/service-hooks.ts"],"names":[],"mappings":"AAQA;;;GAGG;AACH,KAAK,UAAU,gBAAgB,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,KAAK,UAAU,CAAC,CAAC,CAAC,MAAM,CAAC,WAAW,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC;QAE3F,MAAM,CAAC,WAAsB,CAAC,GAAG,gBAAgB,CAC/C,OAAO,EACP,EAAE,GAAG,aAAa,EAAE,WAAW,EAAE,EACjC,WAAW,CACE,CAAC;IAClB,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,qBAAqB,CACnC,QAA+C;IAE/C,OAAO,QAAQ,CAAC;AAClB,CAAC"}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import type { Context } from 'hono';
|
|
2
|
+
/**
|
|
3
|
+
* Context passed to service hooks containing the current Hono request context
|
|
4
|
+
* and any additional metadata about the service call.
|
|
5
|
+
*/
|
|
6
|
+
export interface ServiceHookContext {
|
|
7
|
+
/**
|
|
8
|
+
* The Hono request context, providing access to request data,
|
|
9
|
+
* response helpers, and context variables (like user, ability, etc.)
|
|
10
|
+
*/
|
|
11
|
+
honoContext: Context;
|
|
12
|
+
/**
|
|
13
|
+
* The name of the service being called (e.g., 'authService', 'userService')
|
|
14
|
+
*/
|
|
15
|
+
serviceName: string;
|
|
16
|
+
/**
|
|
17
|
+
* The name of the method being called (e.g., 'deleteSession', 'findById')
|
|
18
|
+
*/
|
|
19
|
+
methodName: string;
|
|
20
|
+
/**
|
|
21
|
+
* Optional metadata associated with this service method.
|
|
22
|
+
* Can be used to pass configuration like authorization rules, audit info, etc.
|
|
23
|
+
*/
|
|
24
|
+
metadata?: unknown;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Hook function that runs before a service method is executed.
|
|
28
|
+
* Can be used for authorization, validation, logging, etc.
|
|
29
|
+
*
|
|
30
|
+
* @param context - Hook context with Hono context and service info
|
|
31
|
+
* @param args - Arguments being passed to the service method
|
|
32
|
+
* @throws Error to prevent the service method from executing
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```typescript
|
|
36
|
+
* const authorizationHook: ServiceBeforeHook = async (context, args) => {
|
|
37
|
+
* const { honoContext, metadata } = context;
|
|
38
|
+
* if (metadata?.authorize) {
|
|
39
|
+
* authorize(honoContext, metadata.authorize.action, metadata.authorize.subject);
|
|
40
|
+
* }
|
|
41
|
+
* };
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export type ServiceBeforeHook = (context: ServiceHookContext, args: unknown[]) => Promise<void> | void;
|
|
45
|
+
/**
|
|
46
|
+
* Hook function that runs after a service method successfully executes.
|
|
47
|
+
* Can be used for audit logging, metrics, response transformation, etc.
|
|
48
|
+
*
|
|
49
|
+
* @param context - Hook context with Hono context and service info
|
|
50
|
+
* @param args - Arguments that were passed to the service method
|
|
51
|
+
* @param result - The result returned by the service method
|
|
52
|
+
* @returns Optionally return a transformed result
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```typescript
|
|
56
|
+
* const auditHook: ServiceAfterHook = async (context, args, result) => {
|
|
57
|
+
* await auditLog.create({
|
|
58
|
+
* service: context.serviceName,
|
|
59
|
+
* method: context.methodName,
|
|
60
|
+
* args,
|
|
61
|
+
* timestamp: new Date(),
|
|
62
|
+
* });
|
|
63
|
+
* };
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
export type ServiceAfterHook = (context: ServiceHookContext, args: unknown[], result: unknown) => Promise<unknown | undefined> | unknown | undefined;
|
|
67
|
+
/**
|
|
68
|
+
* Hook function that runs when a service method throws an error.
|
|
69
|
+
* Can be used for error logging, error transformation, retries, etc.
|
|
70
|
+
*
|
|
71
|
+
* @param context - Hook context with Hono context and service info
|
|
72
|
+
* @param args - Arguments that were passed to the service method
|
|
73
|
+
* @param error - The error that was thrown
|
|
74
|
+
* @throws Error to propagate or throw a different error
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```typescript
|
|
78
|
+
* const errorLoggingHook: ServiceErrorHook = async (context, args, error) => {
|
|
79
|
+
* logger.error('Service error', {
|
|
80
|
+
* service: context.serviceName,
|
|
81
|
+
* method: context.methodName,
|
|
82
|
+
* error,
|
|
83
|
+
* });
|
|
84
|
+
* // Re-throw to propagate
|
|
85
|
+
* throw error;
|
|
86
|
+
* };
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
export type ServiceErrorHook = (context: ServiceHookContext, args: unknown[], error: unknown) => undefined | never | Promise<undefined | never>;
|
|
90
|
+
/**
|
|
91
|
+
* A service hook that can define before, after, and error handlers.
|
|
92
|
+
*/
|
|
93
|
+
export interface ServiceHook {
|
|
94
|
+
/**
|
|
95
|
+
* Optional hook that runs before the service method executes.
|
|
96
|
+
*/
|
|
97
|
+
before?: ServiceBeforeHook;
|
|
98
|
+
/**
|
|
99
|
+
* Optional hook that runs after the service method successfully executes.
|
|
100
|
+
*/
|
|
101
|
+
after?: ServiceAfterHook;
|
|
102
|
+
/**
|
|
103
|
+
* Optional hook that runs when the service method throws an error.
|
|
104
|
+
*/
|
|
105
|
+
onError?: ServiceErrorHook;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Metadata that can be attached to service methods to configure hook behavior.
|
|
109
|
+
*/
|
|
110
|
+
export type ServiceMethodMetadata = Record<string, unknown>;
|
|
111
|
+
/**
|
|
112
|
+
* Configuration for applying hooks to a service.
|
|
113
|
+
*/
|
|
114
|
+
export interface ServiceHooksConfig {
|
|
115
|
+
/**
|
|
116
|
+
* Array of hooks to apply to the service.
|
|
117
|
+
* Hooks run in the order they're defined.
|
|
118
|
+
*/
|
|
119
|
+
hooks: ServiceHook[];
|
|
120
|
+
/**
|
|
121
|
+
* Optional metadata for service methods.
|
|
122
|
+
* Keys are method names, values are metadata objects.
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
* ```typescript
|
|
126
|
+
* {
|
|
127
|
+
* deleteSession: {
|
|
128
|
+
* authorize: { action: 'delete', subject: 'Session' }
|
|
129
|
+
* },
|
|
130
|
+
* findById: {
|
|
131
|
+
* authorize: { action: 'read', subject: 'User' },
|
|
132
|
+
* cache: { ttl: 300 }
|
|
133
|
+
* }
|
|
134
|
+
* }
|
|
135
|
+
* ```
|
|
136
|
+
*/
|
|
137
|
+
metadata?: Record<string, ServiceMethodMetadata>;
|
|
138
|
+
/**
|
|
139
|
+
* Optional service name for better debugging.
|
|
140
|
+
* If not provided, will try to infer from the service constructor name.
|
|
141
|
+
*/
|
|
142
|
+
serviceName?: string;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Services registry that maps service names to their wrapped instances.
|
|
146
|
+
* This is used internally by the route context to provide hooked services.
|
|
147
|
+
*/
|
|
148
|
+
export type ServicesRegistry<T = unknown> = {
|
|
149
|
+
[K in keyof T]: T[K];
|
|
150
|
+
};
|
|
151
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/hooks/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAEpC;;;GAGG;AACH,MAAM,WAAW,kBAAkB;IACjC;;;OAGG;IACH,WAAW,EAAE,OAAO,CAAC;IAErB;;OAEG;IACH,WAAW,EAAE,MAAM,CAAC;IAEpB;;OAEG;IACH,UAAU,EAAE,MAAM,CAAC;IAEnB;;;OAGG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,MAAM,iBAAiB,GAAG,CAC9B,OAAO,EAAE,kBAAkB,EAC3B,IAAI,EAAE,OAAO,EAAE,KACZ,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;AAE1B;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,MAAM,gBAAgB,GAAG,CAC7B,OAAO,EAAE,kBAAkB,EAC3B,IAAI,EAAE,OAAO,EAAE,EACf,MAAM,EAAE,OAAO,KACZ,OAAO,CAAC,OAAO,GAAG,SAAS,CAAC,GAAG,OAAO,GAAG,SAAS,CAAC;AAExD;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,MAAM,gBAAgB,GAAG,CAC7B,OAAO,EAAE,kBAAkB,EAC3B,IAAI,EAAE,OAAO,EAAE,EACf,KAAK,EAAE,OAAO,KACX,SAAS,GAAG,KAAK,GAAG,OAAO,CAAC,SAAS,GAAG,KAAK,CAAC,CAAC;AAEpD;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B;;OAEG;IACH,MAAM,CAAC,EAAE,iBAAiB,CAAC;IAE3B;;OAEG;IACH,KAAK,CAAC,EAAE,gBAAgB,CAAC;IAEzB;;OAEG;IACH,OAAO,CAAC,EAAE,gBAAgB,CAAC;CAC5B;AAED;;GAEG;AACH,MAAM,MAAM,qBAAqB,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAE5D;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC;;;OAGG;IACH,KAAK,EAAE,WAAW,EAAE,CAAC;IAErB;;;;;;;;;;;;;;;;OAgBG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,qBAAqB,CAAC,CAAC;IAEjD;;;OAGG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED;;;GAGG;AACH,MAAM,MAAM,gBAAgB,CAAC,CAAC,GAAG,OAAO,IAAI;KACzC,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;CACrB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/hooks/types.ts"],"names":[],"mappings":""}
|
|
@@ -126,6 +126,22 @@ export interface RouteOpenAPIOptions extends Partial<Omit<DescribeRouteOptions,
|
|
|
126
126
|
* ```
|
|
127
127
|
*/
|
|
128
128
|
responseHeaders?: string[] | Record<string, string[]>;
|
|
129
|
+
/**
|
|
130
|
+
* Exclude this route from the OpenAPI specification.
|
|
131
|
+
* Useful for internal or hidden endpoints.
|
|
132
|
+
*/
|
|
133
|
+
exclude?: boolean;
|
|
134
|
+
/**
|
|
135
|
+
* Mark this route as deprecated.
|
|
136
|
+
*/
|
|
137
|
+
deprecated?: boolean;
|
|
138
|
+
/**
|
|
139
|
+
* External documentation for this route.
|
|
140
|
+
*/
|
|
141
|
+
docs?: {
|
|
142
|
+
description?: string;
|
|
143
|
+
url: string;
|
|
144
|
+
};
|
|
129
145
|
}
|
|
130
146
|
/**
|
|
131
147
|
* Route configuration with automatic type inference from Valibot schemas.
|
|
@@ -166,6 +182,12 @@ export interface RouteConfig<TBody extends ValibotSchema | undefined = undefined
|
|
|
166
182
|
*/
|
|
167
183
|
public?: TPublic;
|
|
168
184
|
body?: TBody;
|
|
185
|
+
/**
|
|
186
|
+
* Content type for request body validation.
|
|
187
|
+
* - 'json': application/json (default)
|
|
188
|
+
* - 'form': application/x-www-form-urlencoded or multipart/form-data
|
|
189
|
+
*/
|
|
190
|
+
bodyType?: 'json' | 'form';
|
|
169
191
|
query?: ValibotSchema;
|
|
170
192
|
params?: ValibotSchema;
|
|
171
193
|
responses?: TResponses;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"route-helpers.d.ts","sourceRoot":"","sources":["../../src/http/route-helpers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,iBAAiB,EAAE,MAAM,MAAM,CAAC;AAE7D,OAAO,EACL,KAAK,oBAAoB,EAI1B,MAAM,cAAc,CAAC;AACtB,OAAO,KAAK,EAAE,SAAS,EAAE,UAAU,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAEnF,OAAO,KAAK,EAGV,wBAAwB,EACxB,qBAAqB,EACtB,MAAM,kBAAkB,CAAC;AAI1B,OAAO,EACL,KAAK,iBAAiB,EAEtB,KAAK,mBAAmB,EAEzB,M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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8DG;AACH,wBAAgB,KAAK,CACnB,KAAK,SAAS,aAAa,GAAG,SAAS,GAAG,SAAS,EACnD,UAAU,SAAS,OAAO,CACxB,MAAM,CAAC,MAAM,OAAO,mBAAmB,EAAE,aAAa,GAAG,SAAS,CAAC,CACpE,GAAG,MAAM,CAAC,KAAK,EAAE,KAAK,CAAC,EACxB,OAAO,SAAS,OAAO,GAAG,KAAK,EAC/B,YAAY,SAAS,OAAO,GAAG,KAAK,EAEpC,MAAM,EAAE,IAAI,EACZ,MAAM,EAAE,WAAW,CAAC,KAAK,EAAE,UAAU,EAAE,OAAO,EAAE,YAAY,CAAC,GAC5D,iBAAiB,EAAE,CAmDrB"}
|
|
@@ -105,7 +105,7 @@ function hasPaginationFields(schema) {
|
|
|
105
105
|
* Create a validation middleware for request validation.
|
|
106
106
|
* Returns 422 Unprocessable Entity on validation failure (instead of Hono's default 400).
|
|
107
107
|
*
|
|
108
|
-
* @param type - The validation target ('json' for body, 'query', or 'param')
|
|
108
|
+
* @param type - The validation target ('json' or 'form' for body, 'query', or 'param')
|
|
109
109
|
* @param schema - The Valibot schema to validate against
|
|
110
110
|
*/
|
|
111
111
|
function createValidationMiddleware(type, schema) {
|
|
@@ -185,14 +185,16 @@ export function route(router, config) {
|
|
|
185
185
|
// Detect pagination from query schema
|
|
186
186
|
const hasPagination = hasPaginationFields(config.query);
|
|
187
187
|
// Add OpenAPI metadata
|
|
188
|
-
|
|
188
|
+
if (!config.openapi?.exclude) {
|
|
189
|
+
middlewares.push(buildOpenAPIMiddleware(config, openAPIContext, hasPagination));
|
|
190
|
+
}
|
|
189
191
|
// Add custom middleware (e.g., auth) before validation
|
|
190
192
|
if (config.middleware) {
|
|
191
193
|
middlewares.push(...config.middleware);
|
|
192
194
|
}
|
|
193
195
|
// Add validation middleware (returns 422 instead of Hono's default 400)
|
|
194
196
|
if (config.body) {
|
|
195
|
-
middlewares.push(createValidationMiddleware('json', config.body));
|
|
197
|
+
middlewares.push(createValidationMiddleware(config.bodyType || 'json', config.body));
|
|
196
198
|
}
|
|
197
199
|
if (config.query) {
|
|
198
200
|
middlewares.push(createValidationMiddleware('query', config.query));
|
|
@@ -202,9 +204,9 @@ export function route(router, config) {
|
|
|
202
204
|
}
|
|
203
205
|
// Add handler wrapper
|
|
204
206
|
middlewares.push(async (c) => {
|
|
205
|
-
const routeContext = buildRouteContext(c);
|
|
207
|
+
const routeContext = buildRouteContext(c, config);
|
|
206
208
|
const result = await config.handler(routeContext);
|
|
207
|
-
return handleResponse(result, config.responses, config.serialization, config.summary, c);
|
|
209
|
+
return handleResponse(result, config.responses, config.serialization, config.strictTypes, config.summary, c);
|
|
208
210
|
});
|
|
209
211
|
return middlewares;
|
|
210
212
|
}
|
|
@@ -237,7 +239,7 @@ function serializeResponseData(data, serializationConfig) {
|
|
|
237
239
|
* @param context - Hono context
|
|
238
240
|
* @returns Response object or undefined if handled
|
|
239
241
|
*/
|
|
240
|
-
async function handleResponse(result, responses, serializationConfig, routeSummary, context) {
|
|
242
|
+
async function handleResponse(result, responses, serializationConfig, strictTypes, routeSummary, context) {
|
|
241
243
|
// If handler returned a Response, return it directly (for custom responses like text)
|
|
242
244
|
if (result instanceof Response) {
|
|
243
245
|
return result;
|
|
@@ -250,7 +252,14 @@ async function handleResponse(result, responses, serializationConfig, routeSumma
|
|
|
250
252
|
// Serialize Prisma types and custom transformers
|
|
251
253
|
let serializedResult;
|
|
252
254
|
try {
|
|
253
|
-
|
|
255
|
+
// Only serialize if strictTypes is disabled (default)
|
|
256
|
+
// If strictTypes is true, we expect the handler to return exact types
|
|
257
|
+
if (!strictTypes) {
|
|
258
|
+
serializedResult = serializeResponseData(result, serializationConfig);
|
|
259
|
+
}
|
|
260
|
+
else {
|
|
261
|
+
serializedResult = result;
|
|
262
|
+
}
|
|
254
263
|
}
|
|
255
264
|
catch (error) {
|
|
256
265
|
// Handle serialization errors (circular references, max depth exceeded)
|
|
@@ -327,13 +336,14 @@ function buildOpenAPIMiddleware(config, openAPIContext, hasPagination) {
|
|
|
327
336
|
const security = config.public
|
|
328
337
|
? []
|
|
329
338
|
: openAPIContext.securitySchemes.map((scheme) => ({ [scheme]: [] }));
|
|
330
|
-
// Extract responseHeaders from openapi config to avoid passing it to hono-openapi
|
|
331
|
-
const { responseHeaders: _responseHeaders, ...restOpenapi } = config.openapi ?? {};
|
|
339
|
+
// Extract responseHeaders and exclude from openapi config to avoid passing it to hono-openapi
|
|
340
|
+
const { responseHeaders: _responseHeaders, exclude: _exclude, docs, ...restOpenapi } = config.openapi ?? {};
|
|
332
341
|
const openApiConfig = {
|
|
333
342
|
operationId: config.operationId || config.summary,
|
|
334
343
|
tags: config.tags,
|
|
335
344
|
summary: config.summary,
|
|
336
345
|
description: config.description,
|
|
346
|
+
externalDocs: docs,
|
|
337
347
|
responses: responses,
|
|
338
348
|
security,
|
|
339
349
|
...restOpenapi,
|
|
@@ -410,7 +420,7 @@ function buildOpenAPIResponses(config, openAPIContext, hasPagination) {
|
|
|
410
420
|
}
|
|
411
421
|
return responses;
|
|
412
422
|
}
|
|
413
|
-
function buildRouteContext(c) {
|
|
423
|
+
function buildRouteContext(c, config) {
|
|
414
424
|
const req = c.req;
|
|
415
425
|
// Spread all context variables (from ContextVariableMap)
|
|
416
426
|
// This includes session, ability, role, and any other middleware-added variables
|
|
@@ -418,7 +428,7 @@ function buildRouteContext(c) {
|
|
|
418
428
|
// The type will be narrowed at the call site based on the route's public setting
|
|
419
429
|
return {
|
|
420
430
|
...c.var,
|
|
421
|
-
body: req.valid('json'),
|
|
431
|
+
body: req.valid(config.bodyType || 'json'),
|
|
422
432
|
query: req.valid('query'),
|
|
423
433
|
params: req.valid('param'),
|
|
424
434
|
services: c.get('services') || {},
|