glasswork 0.9.2 → 0.11.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/core/bootstrap.d.ts +2 -1
- package/dist/core/bootstrap.d.ts.map +1 -1
- package/dist/core/bootstrap.js +121 -44
- package/dist/core/bootstrap.js.map +1 -1
- package/dist/core/types.d.ts +53 -3
- package/dist/core/types.d.ts.map +1 -1
- package/dist/email/compiler/code-generator.d.ts +11 -0
- package/dist/email/compiler/code-generator.d.ts.map +1 -0
- package/dist/email/compiler/code-generator.js +319 -0
- package/dist/email/compiler/code-generator.js.map +1 -0
- package/dist/email/compiler/compile-templates.d.ts +68 -0
- package/dist/email/compiler/compile-templates.d.ts.map +1 -0
- package/dist/email/compiler/compile-templates.js +156 -0
- package/dist/email/compiler/compile-templates.js.map +1 -0
- package/dist/email/compiler/compiler.d.ts +30 -0
- package/dist/email/compiler/compiler.d.ts.map +1 -0
- package/dist/email/compiler/compiler.js +510 -0
- package/dist/email/compiler/compiler.js.map +1 -0
- package/dist/email/compiler/index.d.ts +4 -0
- package/dist/email/compiler/index.d.ts.map +1 -0
- package/dist/email/compiler/index.js +3 -0
- package/dist/email/compiler/index.js.map +1 -0
- package/dist/email/compiler/tokenizer.d.ts +7 -0
- package/dist/email/compiler/tokenizer.d.ts.map +1 -0
- package/dist/email/compiler/tokenizer.js +154 -0
- package/dist/email/compiler/tokenizer.js.map +1 -0
- package/dist/email/compiler/type-extractor.d.ts +11 -0
- package/dist/email/compiler/type-extractor.d.ts.map +1 -0
- package/dist/email/compiler/type-extractor.js +296 -0
- package/dist/email/compiler/type-extractor.js.map +1 -0
- package/dist/email/compiler/types.d.ts +108 -0
- package/dist/email/compiler/types.d.ts.map +1 -0
- package/dist/email/compiler/types.js.map +1 -0
- package/dist/email/config.d.ts +89 -0
- package/dist/email/config.d.ts.map +1 -0
- package/dist/email/config.js +55 -0
- package/dist/email/config.js.map +1 -0
- package/dist/email/email-service.d.ts +54 -0
- package/dist/email/email-service.d.ts.map +1 -0
- package/dist/email/email-service.js +94 -0
- package/dist/email/email-service.js.map +1 -0
- package/dist/email/index.d.ts +12 -0
- package/dist/email/index.d.ts.map +1 -0
- package/dist/email/index.js +23 -0
- package/dist/email/index.js.map +1 -0
- package/dist/email/template-registry.d.ts +83 -0
- package/dist/email/template-registry.d.ts.map +1 -0
- package/dist/email/template-registry.js +85 -0
- package/dist/email/template-registry.js.map +1 -0
- package/dist/email/templated-email-service.d.ts +59 -0
- package/dist/email/templated-email-service.d.ts.map +1 -0
- package/dist/email/templated-email-service.js +63 -0
- package/dist/email/templated-email-service.js.map +1 -0
- package/dist/email/transports/index.d.ts +3 -0
- package/dist/email/transports/index.d.ts.map +1 -0
- package/dist/email/transports/index.js +3 -0
- package/dist/email/transports/index.js.map +1 -0
- package/dist/email/transports/mock-transport.d.ts +74 -0
- package/dist/email/transports/mock-transport.d.ts.map +1 -0
- package/dist/email/transports/mock-transport.js +102 -0
- package/dist/email/transports/mock-transport.js.map +1 -0
- package/dist/email/transports/ses-transport.d.ts +65 -0
- package/dist/email/transports/ses-transport.d.ts.map +1 -0
- package/dist/email/transports/ses-transport.js +290 -0
- package/dist/email/transports/ses-transport.js.map +1 -0
- package/dist/email/types.d.ts +128 -0
- package/dist/email/types.d.ts.map +1 -0
- package/dist/email/types.js +2 -0
- package/dist/{hooks → email}/types.js.map +1 -1
- package/dist/email/webhooks/handler-factory.d.ts +41 -0
- package/dist/email/webhooks/handler-factory.d.ts.map +1 -0
- package/dist/email/webhooks/handler-factory.js +136 -0
- package/dist/email/webhooks/handler-factory.js.map +1 -0
- package/dist/email/webhooks/index.d.ts +6 -0
- package/dist/email/webhooks/index.d.ts.map +1 -0
- package/dist/email/webhooks/index.js +9 -0
- package/dist/email/webhooks/index.js.map +1 -0
- package/dist/email/webhooks/notification-parser.d.ts +45 -0
- package/dist/email/webhooks/notification-parser.d.ts.map +1 -0
- package/dist/email/webhooks/notification-parser.js +150 -0
- package/dist/email/webhooks/notification-parser.js.map +1 -0
- package/dist/email/webhooks/signature-verification.d.ts +38 -0
- package/dist/email/webhooks/signature-verification.d.ts.map +1 -0
- package/dist/email/webhooks/signature-verification.js +179 -0
- package/dist/email/webhooks/signature-verification.js.map +1 -0
- package/dist/email/webhooks/subscription-handler.d.ts +37 -0
- package/dist/email/webhooks/subscription-handler.d.ts.map +1 -0
- package/dist/email/webhooks/subscription-handler.js +99 -0
- package/dist/email/webhooks/subscription-handler.js.map +1 -0
- package/dist/email/webhooks/types.d.ts +187 -0
- package/dist/email/webhooks/types.d.ts.map +1 -0
- package/dist/email/webhooks/types.js +5 -0
- package/dist/email/webhooks/types.js.map +1 -0
- package/dist/http/error-handler.d.ts +28 -3
- package/dist/http/error-handler.d.ts.map +1 -1
- package/dist/http/error-handler.js +97 -22
- package/dist/http/error-handler.js.map +1 -1
- package/dist/http/errors.d.ts +52 -22
- package/dist/http/errors.d.ts.map +1 -1
- package/dist/http/errors.js +62 -42
- package/dist/http/errors.js.map +1 -1
- package/dist/http/route-helpers.d.ts +13 -0
- package/dist/http/route-helpers.d.ts.map +1 -1
- package/dist/http/route-helpers.js +15 -3
- package/dist/http/route-helpers.js.map +1 -1
- package/dist/index.d.ts +14 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +31 -0
- package/dist/index.js.map +1 -1
- package/dist/list-query/builder.d.ts +5 -0
- package/dist/list-query/builder.d.ts.map +1 -1
- package/dist/list-query/builder.js +34 -1
- package/dist/list-query/builder.js.map +1 -1
- package/dist/middleware/rate-limit.d.ts.map +1 -1
- package/dist/middleware/rate-limit.js +28 -18
- package/dist/middleware/rate-limit.js.map +1 -1
- package/dist/observability/exception-tracking.d.ts +214 -0
- package/dist/observability/exception-tracking.d.ts.map +1 -0
- package/dist/observability/exception-tracking.js +238 -0
- package/dist/observability/exception-tracking.js.map +1 -0
- package/dist/observability/pino-logger.d.ts +108 -0
- package/dist/observability/pino-logger.d.ts.map +1 -0
- package/dist/observability/pino-logger.js +158 -0
- package/dist/observability/pino-logger.js.map +1 -0
- package/dist/observability/request-context.d.ts +105 -0
- package/dist/observability/request-context.d.ts.map +1 -0
- package/dist/observability/request-context.js +115 -0
- package/dist/observability/request-context.js.map +1 -0
- package/dist/openapi/openapi.d.ts.map +1 -1
- package/dist/openapi/openapi.js +3 -1
- package/dist/openapi/openapi.js.map +1 -1
- package/dist/utils/environment.d.ts +2 -1
- package/dist/utils/environment.d.ts.map +1 -1
- package/dist/utils/environment.js +11 -2
- package/dist/utils/environment.js.map +1 -1
- package/dist/utils/logger.d.ts +43 -9
- package/dist/utils/logger.d.ts.map +1 -1
- package/dist/utils/logger.js +60 -16
- package/dist/utils/logger.js.map +1 -1
- package/package.json +15 -2
- package/dist/authorization/ability-middleware.d.ts +0 -61
- package/dist/authorization/ability-middleware.d.ts.map +0 -1
- package/dist/authorization/ability-middleware.js +0 -74
- package/dist/authorization/ability-middleware.js.map +0 -1
- package/dist/authorization/index.d.ts +0 -46
- package/dist/authorization/index.d.ts.map +0 -1
- package/dist/authorization/index.js +0 -46
- package/dist/authorization/index.js.map +0 -1
- package/dist/authorization/route-helpers.d.ts +0 -106
- package/dist/authorization/route-helpers.d.ts.map +0 -1
- package/dist/authorization/route-helpers.js +0 -136
- package/dist/authorization/route-helpers.js.map +0 -1
- package/dist/authorization/service-proxy.d.ts +0 -59
- package/dist/authorization/service-proxy.d.ts.map +0 -1
- package/dist/authorization/service-proxy.js +0 -110
- package/dist/authorization/service-proxy.js.map +0 -1
- package/dist/authorization/types.d.ts +0 -58
- package/dist/authorization/types.d.ts.map +0 -1
- package/dist/authorization/types.js +0 -7
- package/dist/authorization/types.js.map +0 -1
- package/dist/hooks/authorization-hook.d.ts +0 -100
- package/dist/hooks/authorization-hook.d.ts.map +0 -1
- package/dist/hooks/authorization-hook.js +0 -93
- package/dist/hooks/authorization-hook.js.map +0 -1
- package/dist/hooks/index.d.ts +0 -54
- package/dist/hooks/index.d.ts.map +0 -1
- package/dist/hooks/index.js +0 -55
- package/dist/hooks/index.js.map +0 -1
- package/dist/hooks/service-hooks.d.ts +0 -82
- package/dist/hooks/service-hooks.d.ts.map +0 -1
- package/dist/hooks/service-hooks.js +0 -158
- package/dist/hooks/service-hooks.js.map +0 -1
- package/dist/hooks/types.d.ts +0 -151
- package/dist/hooks/types.d.ts.map +0 -1
- package/dist/openapi/hooks.d.ts +0 -14
- package/dist/openapi/hooks.d.ts.map +0 -1
- package/dist/openapi/hooks.js +0 -83
- package/dist/openapi/hooks.js.map +0 -1
- package/dist/openapi/openapi-transformer.d.ts +0 -13
- package/dist/openapi/openapi-transformer.d.ts.map +0 -1
- package/dist/openapi/openapi-transformer.js +0 -87
- package/dist/openapi/openapi-transformer.js.map +0 -1
- package/dist/utils/pagination.d.ts +0 -48
- package/dist/utils/pagination.d.ts.map +0 -1
- package/dist/utils/pagination.js +0 -42
- package/dist/utils/pagination.js.map +0 -1
- package/dist/utils/serialize-dates.d.ts +0 -27
- package/dist/utils/serialize-dates.d.ts.map +0 -1
- package/dist/utils/serialize-dates.js +0 -39
- package/dist/utils/serialize-dates.js.map +0 -1
- /package/dist/{hooks → email/compiler}/types.js +0 -0
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
import { authorize } from '../authorization/route-helpers.js';
|
|
2
|
-
/**
|
|
3
|
-
* Service hook that performs CASL authorization before method execution.
|
|
4
|
-
*
|
|
5
|
-
* This hook checks for `authorize` metadata on service methods and calls
|
|
6
|
-
* the CASL `authorize()` function before allowing the method to execute.
|
|
7
|
-
*
|
|
8
|
-
* **Usage:**
|
|
9
|
-
* 1. Define authorization metadata for your service methods
|
|
10
|
-
* 2. Include this hook when wrapping services
|
|
11
|
-
* 3. Ensure ability middleware is configured in your routes
|
|
12
|
-
*
|
|
13
|
-
* @example
|
|
14
|
-
* ```typescript
|
|
15
|
-
* import { authorizationHook, defineServiceMetadata } from 'glasswork';
|
|
16
|
-
*
|
|
17
|
-
* // Define metadata
|
|
18
|
-
* export const authServiceMetadata = defineServiceMetadata({
|
|
19
|
-
* deleteSession: {
|
|
20
|
-
* authorize: {
|
|
21
|
-
* action: 'delete',
|
|
22
|
-
* subject: 'Session'
|
|
23
|
-
* }
|
|
24
|
-
* },
|
|
25
|
-
* updateUser: {
|
|
26
|
-
* authorize: {
|
|
27
|
-
* action: 'update',
|
|
28
|
-
* subject: 'User',
|
|
29
|
-
* getSubjectData: (userId, data) => ({ id: userId, ...data })
|
|
30
|
-
* }
|
|
31
|
-
* }
|
|
32
|
-
* });
|
|
33
|
-
*
|
|
34
|
-
* // Use in routes
|
|
35
|
-
* router.delete('/session', async (c) => {
|
|
36
|
-
* const services = withServiceHooks(
|
|
37
|
-
* { authService },
|
|
38
|
-
* {
|
|
39
|
-
* hooks: [authorizationHook],
|
|
40
|
-
* metadata: authServiceMetadata
|
|
41
|
-
* },
|
|
42
|
-
* c
|
|
43
|
-
* );
|
|
44
|
-
*
|
|
45
|
-
* // This automatically checks authorization before executing
|
|
46
|
-
* await services.authService.deleteSession(token);
|
|
47
|
-
* });
|
|
48
|
-
* ```
|
|
49
|
-
*/
|
|
50
|
-
export const authorizationHook = {
|
|
51
|
-
before: async (context, args) => {
|
|
52
|
-
// Check if this method has authorization metadata
|
|
53
|
-
const metadata = context.metadata;
|
|
54
|
-
if (!metadata?.authorize) {
|
|
55
|
-
// No authorization required for this method
|
|
56
|
-
return;
|
|
57
|
-
}
|
|
58
|
-
const { action, subject, getSubjectData } = metadata.authorize;
|
|
59
|
-
// Extract subject data if function provided
|
|
60
|
-
const subjectData = getSubjectData ? getSubjectData(...args) : undefined;
|
|
61
|
-
// Call CASL authorize - this will throw ForbiddenError if not allowed
|
|
62
|
-
authorize(context.honoContext, action, subject, subjectData);
|
|
63
|
-
},
|
|
64
|
-
};
|
|
65
|
-
/**
|
|
66
|
-
* Helper to define authorization metadata for service methods.
|
|
67
|
-
* Provides better type inference for the authorize property.
|
|
68
|
-
*
|
|
69
|
-
* @param action - CASL action
|
|
70
|
-
* @param subject - CASL subject type
|
|
71
|
-
* @param getSubjectData - Optional function to extract subject data
|
|
72
|
-
* @returns Authorization metadata object
|
|
73
|
-
*
|
|
74
|
-
* @example
|
|
75
|
-
* ```typescript
|
|
76
|
-
* export const userServiceMetadata = defineServiceMetadata({
|
|
77
|
-
* update: {
|
|
78
|
-
* authorize: defineAuthorize('update', 'User', (id, data) => ({ id, ...data }))
|
|
79
|
-
* },
|
|
80
|
-
* delete: {
|
|
81
|
-
* authorize: defineAuthorize('delete', 'User', (id) => ({ id }))
|
|
82
|
-
* }
|
|
83
|
-
* });
|
|
84
|
-
* ```
|
|
85
|
-
*/
|
|
86
|
-
export function defineAuthorize(action, subject, getSubjectData) {
|
|
87
|
-
return {
|
|
88
|
-
action,
|
|
89
|
-
subject,
|
|
90
|
-
getSubjectData,
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
|
-
//# sourceMappingURL=authorization-hook.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"authorization-hook.js","sourceRoot":"","sources":["../../src/hooks/authorization-hook.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,mCAAmC,CAAC;AAiC9D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+CG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAgB;IAC5C,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE;QAC9B,kDAAkD;QAClD,MAAM,QAAQ,GAAG,OAAO,CAAC,QAAiD,CAAC;QAE3E,IAAI,CAAC,QAAQ,EAAE,SAAS,EAAE,CAAC;YACzB,4CAA4C;YAC5C,OAAO;QACT,CAAC;QAED,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,cAAc,EAAE,GAAG,QAAQ,CAAC,SAAS,CAAC;QAE/D,4CAA4C;QAC5C,MAAM,WAAW,GAAG,cAAc,CAAC,CAAC,CAAC,cAAc,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;QAEzE,sEAAsE;QACtE,SAAS,CAAC,OAAO,CAAC,WAAW,EAAE,MAAM,EAAE,OAAO,EAAE,WAAW,CAAC,CAAC;IAC/D,CAAC;CACF,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,UAAU,eAAe,CAC7B,MAAc,EACd,OAAe,EACf,cAA2D;IAE3D,OAAO;QACL,MAAM;QACN,OAAO;QACP,cAAc;KACf,CAAC;AACJ,CAAC"}
|
package/dist/hooks/index.d.ts
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
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
|
-
export { type AuthorizationMetadata, authorizationHook, defineAuthorize, } from './authorization-hook.js';
|
|
52
|
-
export { defineServiceMetadata, withServiceHooks, withServiceHooksMany, } from './service-hooks.js';
|
|
53
|
-
export type { ServiceAfterHook, ServiceBeforeHook, ServiceErrorHook, ServiceHook, ServiceHookContext, ServiceHooksConfig, ServiceMethodMetadata, ServicesRegistry, } from './types.js';
|
|
54
|
-
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/hooks/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiDG;AAGH,OAAO,EACL,KAAK,qBAAqB,EAC1B,iBAAiB,EACjB,eAAe,GAChB,MAAM,yBAAyB,CAAC;AAGjC,OAAO,EACL,qBAAqB,EACrB,gBAAgB,EAChB,oBAAoB,GACrB,MAAM,oBAAoB,CAAC;AAE5B,YAAY,EACV,gBAAgB,EAChB,iBAAiB,EACjB,gBAAgB,EAChB,WAAW,EACX,kBAAkB,EAClB,kBAAkB,EAClB,qBAAqB,EACrB,gBAAgB,GACjB,MAAM,YAAY,CAAC"}
|
package/dist/hooks/index.js
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
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
|
package/dist/hooks/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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"}
|
|
@@ -1,82 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
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"}
|
|
@@ -1,158 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"service-hooks.js","sourceRoot":"","sources":["../../src/hooks/service-hooks.ts"],"names":[],"mappings":"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"}
|
package/dist/hooks/types.d.ts
DELETED
|
@@ -1,151 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
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"}
|
package/dist/openapi/hooks.d.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import type { OpenAPIResponseHook } from '../core/types.js';
|
|
2
|
-
/**
|
|
3
|
-
* Create a hook that adds CORS headers to responses
|
|
4
|
-
*/
|
|
5
|
-
export declare function createCorsHeadersHook(enabled: boolean): OpenAPIResponseHook;
|
|
6
|
-
/**
|
|
7
|
-
* Create a hook that adds rate limiting headers to responses
|
|
8
|
-
*/
|
|
9
|
-
export declare function createRateLimitHeadersHook(enabled: boolean): OpenAPIResponseHook;
|
|
10
|
-
/**
|
|
11
|
-
* Create a hook that adds pagination headers to 200 responses
|
|
12
|
-
*/
|
|
13
|
-
export declare function createPaginationHeadersHook(): OpenAPIResponseHook;
|
|
14
|
-
//# sourceMappingURL=hooks.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../../src/openapi/hooks.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AAE5D;;GAEG;AACH,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,OAAO,GAAG,mBAAmB,CAc3E;AAED;;GAEG;AACH,wBAAgB,0BAA0B,CAAC,OAAO,EAAE,OAAO,GAAG,mBAAmB,CAgChF;AAED;;GAEG;AACH,wBAAgB,2BAA2B,IAAI,mBAAmB,CA4BjE"}
|