glasswork 0.8.0 → 0.8.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 (49) hide show
  1. package/dist/openapi/openapi-transformer.d.ts +5 -0
  2. package/dist/openapi/openapi-transformer.d.ts.map +1 -1
  3. package/dist/openapi/openapi-transformer.js +5 -0
  4. package/dist/openapi/openapi-transformer.js.map +1 -1
  5. package/dist/utils/serialize-prisma-types.d.ts +1 -1
  6. package/dist/utils/serialize-prisma-types.d.ts.map +1 -1
  7. package/dist/utils/serialize-prisma-types.js +12 -10
  8. package/dist/utils/serialize-prisma-types.js.map +1 -1
  9. package/package.json +2 -2
  10. package/dist/authorization/ability-middleware.d.ts +0 -61
  11. package/dist/authorization/ability-middleware.d.ts.map +0 -1
  12. package/dist/authorization/ability-middleware.js +0 -74
  13. package/dist/authorization/ability-middleware.js.map +0 -1
  14. package/dist/authorization/index.d.ts +0 -46
  15. package/dist/authorization/index.d.ts.map +0 -1
  16. package/dist/authorization/index.js +0 -46
  17. package/dist/authorization/index.js.map +0 -1
  18. package/dist/authorization/route-helpers.d.ts +0 -106
  19. package/dist/authorization/route-helpers.d.ts.map +0 -1
  20. package/dist/authorization/route-helpers.js +0 -136
  21. package/dist/authorization/route-helpers.js.map +0 -1
  22. package/dist/authorization/service-proxy.d.ts +0 -59
  23. package/dist/authorization/service-proxy.d.ts.map +0 -1
  24. package/dist/authorization/service-proxy.js +0 -110
  25. package/dist/authorization/service-proxy.js.map +0 -1
  26. package/dist/authorization/types.d.ts +0 -58
  27. package/dist/authorization/types.d.ts.map +0 -1
  28. package/dist/authorization/types.js +0 -7
  29. package/dist/authorization/types.js.map +0 -1
  30. package/dist/hooks/authorization-hook.d.ts +0 -100
  31. package/dist/hooks/authorization-hook.d.ts.map +0 -1
  32. package/dist/hooks/authorization-hook.js +0 -93
  33. package/dist/hooks/authorization-hook.js.map +0 -1
  34. package/dist/hooks/index.d.ts +0 -54
  35. package/dist/hooks/index.d.ts.map +0 -1
  36. package/dist/hooks/index.js +0 -55
  37. package/dist/hooks/index.js.map +0 -1
  38. package/dist/hooks/service-hooks.d.ts +0 -82
  39. package/dist/hooks/service-hooks.d.ts.map +0 -1
  40. package/dist/hooks/service-hooks.js +0 -158
  41. package/dist/hooks/service-hooks.js.map +0 -1
  42. package/dist/hooks/types.d.ts +0 -151
  43. package/dist/hooks/types.d.ts.map +0 -1
  44. package/dist/hooks/types.js +0 -2
  45. package/dist/hooks/types.js.map +0 -1
  46. package/dist/utils/pagination.d.ts +0 -48
  47. package/dist/utils/pagination.d.ts.map +0 -1
  48. package/dist/utils/pagination.js +0 -42
  49. package/dist/utils/pagination.js.map +0 -1
@@ -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,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;YACzB,IAAI,IAAI,CAAC,KAAK,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E,CAAC;gBACxC,OAAO,aAAa,CAAC;YACvB,CAAC;YAED,+BAA+B;YAC/B,MAAM,cAAc,GAAsC,QAAQ,CAAC,IAAI,CAAC,CAAC;YAEzE,qCAAqC;YACrC,OAAO,KAAK,WAA0B,GAAG,IAAe;gBACtD,MAAM,WAAW,GAAuB;oBACtC,WAAW;oBACX,WAAW,EAAE,mBAAmB;oBAChC,UAAU,EAAE,IAAI;oBAChB,QAAQ,EAAE,cAAc;iBACzB,CAAC;gBAEF,8GAA8G;gBAC9G,OAAO,gBAAgB,CAAC,WAAW,EAAE,KAAK,EAAE,aAAoB,EAAE,MAAM,EAAE,IAAI,CAAC,CAAC;YAClF,CAAC,CAAC;QACJ,CAAC;KACF,CAAC,CAAC;AACL,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,UAAU,oBAAoB,CAClC,QAAW,EACX,MAA4F,EAC5F,WAAoB;IAEpB,MAAM,MAAM,GAAG,EAAO,CAAC;IAEvB,KAAK,MAAM,CAAC,WAAW,EAAE,OAAO,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAC;QAC9D,MAAM,aAAa,GAAG,OAAO,MAAM,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"}
@@ -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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,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,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=types.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/hooks/types.ts"],"names":[],"mappings":""}
@@ -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"}