glasswork 0.2.4 → 0.3.1

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.
Files changed (93) hide show
  1. package/dist/http/route-helpers.d.ts +4 -4
  2. package/dist/http/route-helpers.d.ts.map +1 -1
  3. package/dist/http/route-helpers.js +4 -1
  4. package/dist/http/route-helpers.js.map +1 -1
  5. package/dist/index.d.ts +3 -2
  6. package/dist/index.d.ts.map +1 -1
  7. package/dist/index.js +4 -1
  8. package/dist/index.js.map +1 -1
  9. package/dist/list-query/builder.d.ts +33 -0
  10. package/dist/list-query/builder.d.ts.map +1 -0
  11. package/dist/list-query/builder.js +104 -0
  12. package/dist/list-query/builder.js.map +1 -0
  13. package/dist/list-query/casl.d.ts +36 -0
  14. package/dist/list-query/casl.d.ts.map +1 -0
  15. package/dist/list-query/casl.js +42 -0
  16. package/dist/list-query/casl.js.map +1 -0
  17. package/dist/list-query/global-search.d.ts +20 -0
  18. package/dist/list-query/global-search.d.ts.map +1 -0
  19. package/dist/list-query/global-search.js +68 -0
  20. package/dist/list-query/global-search.js.map +1 -0
  21. package/dist/list-query/index.d.ts +6 -0
  22. package/dist/list-query/index.d.ts.map +1 -0
  23. package/dist/list-query/index.js +7 -0
  24. package/dist/list-query/index.js.map +1 -0
  25. package/dist/list-query/parser.d.ts +17 -0
  26. package/dist/list-query/parser.d.ts.map +1 -0
  27. package/dist/list-query/parser.js +162 -0
  28. package/dist/list-query/parser.js.map +1 -0
  29. package/dist/list-query/prisma-builder.d.ts +21 -0
  30. package/dist/list-query/prisma-builder.d.ts.map +1 -0
  31. package/dist/list-query/prisma-builder.js +224 -0
  32. package/dist/list-query/prisma-builder.js.map +1 -0
  33. package/dist/list-query/query-schema.d.ts +15 -0
  34. package/dist/list-query/query-schema.d.ts.map +1 -0
  35. package/dist/list-query/query-schema.js +16 -0
  36. package/dist/list-query/query-schema.js.map +1 -0
  37. package/dist/list-query/schema-helpers.d.ts +81 -0
  38. package/dist/list-query/schema-helpers.d.ts.map +1 -0
  39. package/dist/list-query/schema-helpers.js +85 -0
  40. package/dist/list-query/schema-helpers.js.map +1 -0
  41. package/dist/list-query/types.d.ts +66 -0
  42. package/dist/list-query/types.d.ts.map +1 -0
  43. package/dist/{hooks → list-query}/types.js.map +1 -1
  44. package/dist/list-query/validator.d.ts +35 -0
  45. package/dist/list-query/validator.d.ts.map +1 -0
  46. package/dist/list-query/validator.js +44 -0
  47. package/dist/list-query/validator.js.map +1 -0
  48. package/dist/openapi/defaults.d.ts.map +1 -1
  49. package/dist/openapi/defaults.js +12 -3
  50. package/dist/openapi/defaults.js.map +1 -1
  51. package/dist/openapi/hooks.d.ts.map +1 -1
  52. package/dist/openapi/hooks.js +7 -4
  53. package/dist/openapi/hooks.js.map +1 -1
  54. package/package.json +12 -1
  55. package/dist/authorization/ability-middleware.d.ts +0 -61
  56. package/dist/authorization/ability-middleware.d.ts.map +0 -1
  57. package/dist/authorization/ability-middleware.js +0 -74
  58. package/dist/authorization/ability-middleware.js.map +0 -1
  59. package/dist/authorization/index.d.ts +0 -46
  60. package/dist/authorization/index.d.ts.map +0 -1
  61. package/dist/authorization/index.js +0 -46
  62. package/dist/authorization/index.js.map +0 -1
  63. package/dist/authorization/route-helpers.d.ts +0 -106
  64. package/dist/authorization/route-helpers.d.ts.map +0 -1
  65. package/dist/authorization/route-helpers.js +0 -136
  66. package/dist/authorization/route-helpers.js.map +0 -1
  67. package/dist/authorization/service-proxy.d.ts +0 -59
  68. package/dist/authorization/service-proxy.d.ts.map +0 -1
  69. package/dist/authorization/service-proxy.js +0 -110
  70. package/dist/authorization/service-proxy.js.map +0 -1
  71. package/dist/authorization/types.d.ts +0 -58
  72. package/dist/authorization/types.d.ts.map +0 -1
  73. package/dist/authorization/types.js +0 -7
  74. package/dist/authorization/types.js.map +0 -1
  75. package/dist/hooks/authorization-hook.d.ts +0 -100
  76. package/dist/hooks/authorization-hook.d.ts.map +0 -1
  77. package/dist/hooks/authorization-hook.js +0 -93
  78. package/dist/hooks/authorization-hook.js.map +0 -1
  79. package/dist/hooks/index.d.ts +0 -54
  80. package/dist/hooks/index.d.ts.map +0 -1
  81. package/dist/hooks/index.js +0 -55
  82. package/dist/hooks/index.js.map +0 -1
  83. package/dist/hooks/service-hooks.d.ts +0 -82
  84. package/dist/hooks/service-hooks.d.ts.map +0 -1
  85. package/dist/hooks/service-hooks.js +0 -158
  86. package/dist/hooks/service-hooks.js.map +0 -1
  87. package/dist/hooks/types.d.ts +0 -151
  88. package/dist/hooks/types.d.ts.map +0 -1
  89. package/dist/utils/pagination.d.ts +0 -48
  90. package/dist/utils/pagination.d.ts.map +0 -1
  91. package/dist/utils/pagination.js +0 -42
  92. package/dist/utils/pagination.js.map +0 -1
  93. /package/dist/{hooks → list-query}/types.js +0 -0
@@ -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
@@ -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":"AAQA;;;GAGG;AACH,KAAK,UAAU,gBAAgB,CAC7B,WAA+B,EAC/B,KAAoB;AACpB,yEAAyE;AACzE,cAAuC,EACvC,MAAe,EACf,IAAe;IAEf,IAAI,CAAC;QACH,uBAAuB;QACvB,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;YACzB,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC;gBAChB,MAAM,IAAI,CAAC,MAAM,CAAC,WAAW,EAAE,IAAI,CAAC,CAAC;YACvC,CAAC;QACH,CAAC;QAED,0BAA0B;QAC1B,IAAI,MAAM,GAAG,MAAM,cAAc,CAAC,KAAK,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;QAEtD,iCAAiC;QACjC,KAAK,MAA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- /**
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"}
@@ -1,48 +0,0 @@
1
- import type { Context } from 'hono';
2
- /**
3
- * Pagination input parameters
4
- */
5
- export interface PaginationInput {
6
- page: number;
7
- limit: number;
8
- }
9
- /**
10
- * Paginated output structure
11
- */
12
- export interface PaginatedOutput<T> {
13
- data: T[];
14
- total: number;
15
- page: number;
16
- limit: number;
17
- }
18
- /**
19
- * Execute a paginated query and set response headers.
20
- *
21
- * This helper extracts pagination parameters from the query string,
22
- * executes the provided function, and sets the appropriate response headers.
23
- *
24
- * @template T - The type of items in the result
25
- * @param c - Hono context
26
- * @param fn - Function that executes the paginated query
27
- * @returns Array of items (headers are set as side effect)
28
- *
29
- * @example
30
- * ```typescript
31
- * router.get('/users', route({
32
- * paginate: true,
33
- * handler: async ({ context }) => {
34
- * return await paginate(context, (p) => userService.getUsers(p));
35
- * }
36
- * }));
37
- * ```
38
- */
39
- export declare function paginate<T>(c: Context, fn: (p: PaginationInput) => Promise<PaginatedOutput<T>>): Promise<T[]>;
40
- /**
41
- * Extract pagination parameters from Hono context.
42
- *
43
- * @param c - Hono context
44
- * @param defaultLimit - Default limit if not provided (default: 100)
45
- * @returns Pagination input with page and limit
46
- */
47
- export declare function paginationInput(c: Context, defaultLimit?: number): PaginationInput;
48
- //# sourceMappingURL=pagination.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"pagination.d.ts","sourceRoot":"","sources":["../../src/utils/pagination.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAEpC;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;CACf;AAED;;GAEG;AACH,MAAM,WAAW,eAAe,CAAC,CAAC;IAChC,IAAI,EAAE,CAAC,EAAE,CAAC;IACV,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;CACf;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAsB,QAAQ,CAAC,CAAC,EAC9B,CAAC,EAAE,OAAO,EACV,EAAE,EAAE,CAAC,CAAC,EAAE,eAAe,KAAK,OAAO,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC,GACtD,OAAO,CAAC,CAAC,EAAE,CAAC,CASd;AAED;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,CAAC,EAAE,OAAO,EAAE,YAAY,SAAM,GAAG,eAAe,CAI/E"}
@@ -1,42 +0,0 @@
1
- /**
2
- * Execute a paginated query and set response headers.
3
- *
4
- * This helper extracts pagination parameters from the query string,
5
- * executes the provided function, and sets the appropriate response headers.
6
- *
7
- * @template T - The type of items in the result
8
- * @param c - Hono context
9
- * @param fn - Function that executes the paginated query
10
- * @returns Array of items (headers are set as side effect)
11
- *
12
- * @example
13
- * ```typescript
14
- * router.get('/users', route({
15
- * paginate: true,
16
- * handler: async ({ context }) => {
17
- * return await paginate(context, (p) => userService.getUsers(p));
18
- * }
19
- * }));
20
- * ```
21
- */
22
- export async function paginate(c, fn) {
23
- const pagination = paginationInput(c);
24
- const result = await fn(pagination);
25
- c.header('X-Total-Count', result.total.toString());
26
- c.header('X-Page', result.page.toString());
27
- c.header('X-Limit', result.limit.toString());
28
- return result.data;
29
- }
30
- /**
31
- * Extract pagination parameters from Hono context.
32
- *
33
- * @param c - Hono context
34
- * @param defaultLimit - Default limit if not provided (default: 100)
35
- * @returns Pagination input with page and limit
36
- */
37
- export function paginationInput(c, defaultLimit = 100) {
38
- const page = Number.parseInt(c.req.query('page') ?? '1', 10);
39
- const limit = Number.parseInt(c.req.query('limit') ?? defaultLimit.toString(), 10);
40
- return { page, limit };
41
- }
42
- //# sourceMappingURL=pagination.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"pagination.js","sourceRoot":"","sources":["../../src/utils/pagination.ts"],"names":[],"mappings":"AAoBA;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,CAAC,KAAK,UAAU,QAAQ,CAC5B,CAAU,EACV,EAAuD;IAEvD,MAAM,UAAU,GAAG,eAAe,CAAC,CAAC,CAAC,CAAC;IACtC,MAAM,MAAM,GAAG,MAAM,EAAE,CAAC,UAAU,CAAC,CAAC;IAEpC,CAAC,CAAC,MAAM,CAAC,eAAe,EAAE,MAAM,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAC,CAAC;IACnD,CAAC,CAAC,MAAM,CAAC,QAAQ,EAAE,MAAM,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC,CAAC;IAC3C,CAAC,CAAC,MAAM,CAAC,SAAS,EAAE,MAAM,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAC,CAAC;IAE7C,OAAO,MAAM,CAAC,IAAI,CAAC;AACrB,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,eAAe,CAAC,CAAU,EAAE,YAAY,GAAG,GAAG;IAC5D,MAAM,IAAI,GAAG,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,GAAG,EAAE,EAAE,CAAC,CAAC;IAC7D,MAAM,KAAK,GAAG,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,YAAY,CAAC,QAAQ,EAAE,EAAE,EAAE,CAAC,CAAC;IACnF,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC;AACzB,CAAC"}
File without changes