glasswork 0.9.1 → 0.10.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.
Files changed (108) hide show
  1. package/dist/core/bootstrap.d.ts +2 -1
  2. package/dist/core/bootstrap.d.ts.map +1 -1
  3. package/dist/core/bootstrap.js +121 -44
  4. package/dist/core/bootstrap.js.map +1 -1
  5. package/dist/core/types.d.ts +53 -3
  6. package/dist/core/types.d.ts.map +1 -1
  7. package/dist/http/error-handler.d.ts +28 -3
  8. package/dist/http/error-handler.d.ts.map +1 -1
  9. package/dist/http/error-handler.js +97 -22
  10. package/dist/http/error-handler.js.map +1 -1
  11. package/dist/http/errors.d.ts +52 -22
  12. package/dist/http/errors.d.ts.map +1 -1
  13. package/dist/http/errors.js +62 -42
  14. package/dist/http/errors.js.map +1 -1
  15. package/dist/http/route-helpers.d.ts +13 -0
  16. package/dist/http/route-helpers.d.ts.map +1 -1
  17. package/dist/http/route-helpers.js +15 -3
  18. package/dist/http/route-helpers.js.map +1 -1
  19. package/dist/index.d.ts +5 -2
  20. package/dist/index.d.ts.map +1 -1
  21. package/dist/index.js +9 -0
  22. package/dist/index.js.map +1 -1
  23. package/dist/list-query/builder.d.ts +5 -0
  24. package/dist/list-query/builder.d.ts.map +1 -1
  25. package/dist/list-query/builder.js +34 -1
  26. package/dist/list-query/builder.js.map +1 -1
  27. package/dist/list-query/schema-helpers.d.ts +2 -2
  28. package/dist/list-query/schema-helpers.js +2 -2
  29. package/dist/list-query/schema-helpers.js.map +1 -1
  30. package/dist/middleware/rate-limit.d.ts.map +1 -1
  31. package/dist/middleware/rate-limit.js +28 -18
  32. package/dist/middleware/rate-limit.js.map +1 -1
  33. package/dist/observability/exception-tracking.d.ts +214 -0
  34. package/dist/observability/exception-tracking.d.ts.map +1 -0
  35. package/dist/observability/exception-tracking.js +238 -0
  36. package/dist/observability/exception-tracking.js.map +1 -0
  37. package/dist/observability/pino-logger.d.ts +108 -0
  38. package/dist/observability/pino-logger.d.ts.map +1 -0
  39. package/dist/observability/pino-logger.js +158 -0
  40. package/dist/observability/pino-logger.js.map +1 -0
  41. package/dist/observability/request-context.d.ts +105 -0
  42. package/dist/observability/request-context.d.ts.map +1 -0
  43. package/dist/observability/request-context.js +115 -0
  44. package/dist/observability/request-context.js.map +1 -0
  45. package/dist/openapi/openapi.d.ts.map +1 -1
  46. package/dist/openapi/openapi.js +3 -1
  47. package/dist/openapi/openapi.js.map +1 -1
  48. package/dist/utils/environment.d.ts +2 -1
  49. package/dist/utils/environment.d.ts.map +1 -1
  50. package/dist/utils/environment.js +11 -2
  51. package/dist/utils/environment.js.map +1 -1
  52. package/dist/utils/logger.d.ts +43 -9
  53. package/dist/utils/logger.d.ts.map +1 -1
  54. package/dist/utils/logger.js +60 -16
  55. package/dist/utils/logger.js.map +1 -1
  56. package/package.json +6 -1
  57. package/dist/authorization/ability-middleware.d.ts +0 -61
  58. package/dist/authorization/ability-middleware.d.ts.map +0 -1
  59. package/dist/authorization/ability-middleware.js +0 -74
  60. package/dist/authorization/ability-middleware.js.map +0 -1
  61. package/dist/authorization/index.d.ts +0 -46
  62. package/dist/authorization/index.d.ts.map +0 -1
  63. package/dist/authorization/index.js +0 -46
  64. package/dist/authorization/index.js.map +0 -1
  65. package/dist/authorization/route-helpers.d.ts +0 -106
  66. package/dist/authorization/route-helpers.d.ts.map +0 -1
  67. package/dist/authorization/route-helpers.js +0 -136
  68. package/dist/authorization/route-helpers.js.map +0 -1
  69. package/dist/authorization/service-proxy.d.ts +0 -59
  70. package/dist/authorization/service-proxy.d.ts.map +0 -1
  71. package/dist/authorization/service-proxy.js +0 -110
  72. package/dist/authorization/service-proxy.js.map +0 -1
  73. package/dist/authorization/types.d.ts +0 -58
  74. package/dist/authorization/types.d.ts.map +0 -1
  75. package/dist/authorization/types.js +0 -7
  76. package/dist/authorization/types.js.map +0 -1
  77. package/dist/hooks/authorization-hook.d.ts +0 -100
  78. package/dist/hooks/authorization-hook.d.ts.map +0 -1
  79. package/dist/hooks/authorization-hook.js +0 -93
  80. package/dist/hooks/authorization-hook.js.map +0 -1
  81. package/dist/hooks/index.d.ts +0 -54
  82. package/dist/hooks/index.d.ts.map +0 -1
  83. package/dist/hooks/index.js +0 -55
  84. package/dist/hooks/index.js.map +0 -1
  85. package/dist/hooks/service-hooks.d.ts +0 -82
  86. package/dist/hooks/service-hooks.d.ts.map +0 -1
  87. package/dist/hooks/service-hooks.js +0 -158
  88. package/dist/hooks/service-hooks.js.map +0 -1
  89. package/dist/hooks/types.d.ts +0 -151
  90. package/dist/hooks/types.d.ts.map +0 -1
  91. package/dist/hooks/types.js +0 -2
  92. package/dist/hooks/types.js.map +0 -1
  93. package/dist/openapi/hooks.d.ts +0 -14
  94. package/dist/openapi/hooks.d.ts.map +0 -1
  95. package/dist/openapi/hooks.js +0 -83
  96. package/dist/openapi/hooks.js.map +0 -1
  97. package/dist/openapi/openapi-transformer.d.ts +0 -13
  98. package/dist/openapi/openapi-transformer.d.ts.map +0 -1
  99. package/dist/openapi/openapi-transformer.js +0 -87
  100. package/dist/openapi/openapi-transformer.js.map +0 -1
  101. package/dist/utils/pagination.d.ts +0 -48
  102. package/dist/utils/pagination.d.ts.map +0 -1
  103. package/dist/utils/pagination.js +0 -42
  104. package/dist/utils/pagination.js.map +0 -1
  105. package/dist/utils/serialize-dates.d.ts +0 -27
  106. package/dist/utils/serialize-dates.d.ts.map +0 -1
  107. package/dist/utils/serialize-dates.js +0 -39
  108. package/dist/utils/serialize-dates.js.map +0 -1
@@ -1,46 +0,0 @@
1
- /**
2
- * Authorization module for integrating CASL with Glasswork.
3
- *
4
- * This module provides transparent integration with CASL while maintaining framework principles.
5
- *
6
- * ## Recommended Approach: Service Hooks
7
- *
8
- * Configure authorization globally using the hooks system:
9
- *
10
- * ```typescript
11
- * import { authorizationHook, defineServiceMetadata, defineAuthorize } from 'glasswork';
12
- *
13
- * const authServiceMeta = defineServiceMetadata({
14
- * deleteSession: { authorize: defineAuthorize('delete', 'Session') }
15
- * });
16
- *
17
- * bootstrap(AppModule, {
18
- * serviceHooks: [authorizationHook],
19
- * serviceMetadata: { authService: authServiceMeta }
20
- * });
21
- *
22
- * // In routes - authorization happens automatically!
23
- * await services.authService.deleteSession(token);
24
- * ```
25
- *
26
- * ## Alternative: Explicit Authorization
27
- *
28
- * For fine-grained control, use explicit authorize() calls:
29
- *
30
- * ```typescript
31
- * router.delete('/sessions/:id', async (c) => {
32
- * authorize(c, 'delete', 'Session', { id: c.req.param('id') });
33
- * await sessionService.delete(c.req.param('id'));
34
- * });
35
- * ```
36
- *
37
- * @module authorization
38
- * @see https://casl.js.org/ for CASL documentation
39
- * @see HOOKS_SYSTEM.md for hooks documentation
40
- */
41
- export { ForbiddenError, subject } from '@casl/ability';
42
- // Ability middleware (attaches CASL abilities to context)
43
- export { getAbility, withAbility } from './ability-middleware.js';
44
- // Route helpers (explicit authorization - recommended for fine-grained control)
45
- export { accessibleWhere, authorize, can, cannot } from './route-helpers.js';
46
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/authorization/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AAIH,OAAO,EAAE,cAAc,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AACxD,0DAA0D;AAC1D,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAClE,gFAAgF;AAChF,OAAO,EAAE,eAAe,EAAE,SAAS,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC"}
@@ -1,106 +0,0 @@
1
- import type { Context } from 'hono';
2
- import type { BaseAbility } from './types.js';
3
- /**
4
- * Authorizes an action on a subject using the CASL ability from context.
5
- * Throws ForbiddenError if the action is not permitted.
6
- *
7
- * This is a thin wrapper around CASL's authorize functionality, exposing
8
- * the native CASL API while providing convenient access to the ability
9
- * stored in Hono context.
10
- *
11
- * @param context - Hono context containing the ability
12
- * @param action - CASL action (e.g., 'create', 'read', 'update', 'delete')
13
- * @param subjectType - Subject type string (e.g., 'User', 'Post')
14
- * @param subjectData - Optional subject instance data
15
- * @throws ForbiddenError from CASL if action is not permitted
16
- *
17
- * @example
18
- * ```typescript
19
- * router.delete('/sessions/:id', async (c) => {
20
- * const sessionId = c.req.param('id');
21
- * const user = c.get('user');
22
- *
23
- * // Explicit authorization - clear and obvious
24
- * authorize(c, 'delete', 'Session', { userId: user.id, sessionId });
25
- *
26
- * await sessionService.delete(sessionId);
27
- * return c.json({ success: true });
28
- * });
29
- * ```
30
- */
31
- export declare function authorize<T extends BaseAbility = BaseAbility>(context: Context, action: string, subjectType: string, subjectData?: object): void;
32
- /**
33
- * Checks if an action is permitted without throwing an error.
34
- * Returns boolean indicating if the action is allowed.
35
- *
36
- * @param context - Hono context containing the ability
37
- * @param action - CASL action
38
- * @param subjectType - Subject type string
39
- * @param subjectData - Optional subject instance data
40
- * @returns True if action is permitted, false otherwise
41
- *
42
- * @example
43
- * ```typescript
44
- * router.get('/users', async (c) => {
45
- * if (!can(c, 'read', 'User')) {
46
- * return c.json({ error: 'Forbidden' }, 403);
47
- * }
48
- *
49
- * const users = await userService.findAll();
50
- * return c.json(users);
51
- * });
52
- * ```
53
- */
54
- export declare function can<T extends BaseAbility = BaseAbility>(context: Context, action: string, subjectType: string, subjectData?: object): boolean;
55
- /**
56
- * Checks if an action is forbidden (opposite of can).
57
- *
58
- * @param context - Hono context containing the ability
59
- * @param action - CASL action
60
- * @param subjectType - Subject type string
61
- * @param subjectData - Optional subject instance data
62
- * @returns True if action is forbidden, false if permitted
63
- *
64
- * @example
65
- * ```typescript
66
- * router.post('/admin/users', async (c) => {
67
- * if (cannot(c, 'create', 'User')) {
68
- * return c.json({ error: 'Forbidden' }, 403);
69
- * }
70
- *
71
- * // ... create user
72
- * });
73
- * ```
74
- */
75
- export declare function cannot<T extends BaseAbility = BaseAbility>(context: Context, action: string, subjectType: string, subjectData?: object): boolean;
76
- /**
77
- * Creates a Prisma where clause using CASL's accessibleBy helper.
78
- * This is a transparent wrapper that exposes @casl/prisma functionality.
79
- *
80
- * Requires @casl/prisma to be installed as a peer dependency.
81
- *
82
- * @param context - Hono context containing the ability
83
- * @param subjectType - Prisma model name (e.g., 'User', 'Post')
84
- * @param additionalWhere - Optional additional where conditions to combine
85
- * @returns Prisma where clause that filters by ability permissions
86
- *
87
- * @example
88
- * ```typescript
89
- * import { accessibleBy } from '@casl/prisma';
90
- *
91
- * router.get('/posts', async (c) => {
92
- * const ability = getAbility(c);
93
- *
94
- * // Use CASL Prisma integration directly
95
- * const posts = await prisma.post.findMany({
96
- * where: accessibleBy(ability).Post
97
- * });
98
- *
99
- * return c.json(posts);
100
- * });
101
- * ```
102
- *
103
- * @see https://casl.js.org/v6/en/package/casl-prisma
104
- */
105
- export declare function accessibleWhere<T extends BaseAbility = BaseAbility>(context: Context, subjectType: string, additionalWhere?: object): object;
106
- //# sourceMappingURL=route-helpers.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"route-helpers.d.ts","sourceRoot":"","sources":["../../src/authorization/route-helpers.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAEpC,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE9C;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAgB,SAAS,CAAC,CAAC,SAAS,WAAW,GAAG,WAAW,EAC3D,OAAO,EAAE,OAAO,EAChB,MAAM,EAAE,MAAM,EACd,WAAW,EAAE,MAAM,EACnB,WAAW,CAAC,EAAE,MAAM,GACnB,IAAI,CAUN;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,GAAG,CAAC,CAAC,SAAS,WAAW,GAAG,WAAW,EACrD,OAAO,EAAE,OAAO,EAChB,MAAM,EAAE,MAAM,EACd,WAAW,EAAE,MAAM,EACnB,WAAW,CAAC,EAAE,MAAM,GACnB,OAAO,CAKT;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,MAAM,CAAC,CAAC,SAAS,WAAW,GAAG,WAAW,EACxD,OAAO,EAAE,OAAO,EAChB,MAAM,EAAE,MAAM,EACd,WAAW,EAAE,MAAM,EACnB,WAAW,CAAC,EAAE,MAAM,GACnB,OAAO,CAET;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAgB,eAAe,CAAC,CAAC,SAAS,WAAW,GAAG,WAAW,EACjE,OAAO,EAAE,OAAO,EAChB,WAAW,EAAE,MAAM,EACnB,eAAe,CAAC,EAAE,MAAM,GACvB,MAAM,CAuBR"}
@@ -1,136 +0,0 @@
1
- import { ForbiddenError, subject } from '@casl/ability';
2
- import { getAbility } from './ability-middleware.js';
3
- /**
4
- * Authorizes an action on a subject using the CASL ability from context.
5
- * Throws ForbiddenError if the action is not permitted.
6
- *
7
- * This is a thin wrapper around CASL's authorize functionality, exposing
8
- * the native CASL API while providing convenient access to the ability
9
- * stored in Hono context.
10
- *
11
- * @param context - Hono context containing the ability
12
- * @param action - CASL action (e.g., 'create', 'read', 'update', 'delete')
13
- * @param subjectType - Subject type string (e.g., 'User', 'Post')
14
- * @param subjectData - Optional subject instance data
15
- * @throws ForbiddenError from CASL if action is not permitted
16
- *
17
- * @example
18
- * ```typescript
19
- * router.delete('/sessions/:id', async (c) => {
20
- * const sessionId = c.req.param('id');
21
- * const user = c.get('user');
22
- *
23
- * // Explicit authorization - clear and obvious
24
- * authorize(c, 'delete', 'Session', { userId: user.id, sessionId });
25
- *
26
- * await sessionService.delete(sessionId);
27
- * return c.json({ success: true });
28
- * });
29
- * ```
30
- */
31
- export function authorize(context, action, subjectType, subjectData) {
32
- const ability = getAbility(context);
33
- // Use CASL's native ForbiddenError.from() pattern
34
- // This exposes CASL API directly - no wrapper
35
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
36
- ForbiddenError.from(ability).throwUnlessCan(action, subject(subjectType, subjectData || {}));
37
- }
38
- /**
39
- * Checks if an action is permitted without throwing an error.
40
- * Returns boolean indicating if the action is allowed.
41
- *
42
- * @param context - Hono context containing the ability
43
- * @param action - CASL action
44
- * @param subjectType - Subject type string
45
- * @param subjectData - Optional subject instance data
46
- * @returns True if action is permitted, false otherwise
47
- *
48
- * @example
49
- * ```typescript
50
- * router.get('/users', async (c) => {
51
- * if (!can(c, 'read', 'User')) {
52
- * return c.json({ error: 'Forbidden' }, 403);
53
- * }
54
- *
55
- * const users = await userService.findAll();
56
- * return c.json(users);
57
- * });
58
- * ```
59
- */
60
- export function can(context, action, subjectType, subjectData) {
61
- const ability = getAbility(context);
62
- // Use CASL's native can() method directly
63
- return ability.can(action, subject(subjectType, subjectData || {}));
64
- }
65
- /**
66
- * Checks if an action is forbidden (opposite of can).
67
- *
68
- * @param context - Hono context containing the ability
69
- * @param action - CASL action
70
- * @param subjectType - Subject type string
71
- * @param subjectData - Optional subject instance data
72
- * @returns True if action is forbidden, false if permitted
73
- *
74
- * @example
75
- * ```typescript
76
- * router.post('/admin/users', async (c) => {
77
- * if (cannot(c, 'create', 'User')) {
78
- * return c.json({ error: 'Forbidden' }, 403);
79
- * }
80
- *
81
- * // ... create user
82
- * });
83
- * ```
84
- */
85
- export function cannot(context, action, subjectType, subjectData) {
86
- return !can(context, action, subjectType, subjectData);
87
- }
88
- /**
89
- * Creates a Prisma where clause using CASL's accessibleBy helper.
90
- * This is a transparent wrapper that exposes @casl/prisma functionality.
91
- *
92
- * Requires @casl/prisma to be installed as a peer dependency.
93
- *
94
- * @param context - Hono context containing the ability
95
- * @param subjectType - Prisma model name (e.g., 'User', 'Post')
96
- * @param additionalWhere - Optional additional where conditions to combine
97
- * @returns Prisma where clause that filters by ability permissions
98
- *
99
- * @example
100
- * ```typescript
101
- * import { accessibleBy } from '@casl/prisma';
102
- *
103
- * router.get('/posts', async (c) => {
104
- * const ability = getAbility(c);
105
- *
106
- * // Use CASL Prisma integration directly
107
- * const posts = await prisma.post.findMany({
108
- * where: accessibleBy(ability).Post
109
- * });
110
- *
111
- * return c.json(posts);
112
- * });
113
- * ```
114
- *
115
- * @see https://casl.js.org/v6/en/package/casl-prisma
116
- */
117
- export function accessibleWhere(context, subjectType, additionalWhere) {
118
- const ability = getAbility(context);
119
- // Use CASL Prisma's accessibleBy directly
120
- // Import is done dynamically to avoid hard dependency
121
- try {
122
- // eslint-disable-next-line @typescript-eslint/no-var-requires
123
- const { accessibleBy } = require('@casl/prisma');
124
- const abilityWhere = accessibleBy(ability)[subjectType];
125
- if (additionalWhere) {
126
- return {
127
- AND: [abilityWhere, additionalWhere].filter(Boolean),
128
- };
129
- }
130
- return abilityWhere;
131
- }
132
- catch {
133
- throw new Error('@casl/prisma is required for accessibleWhere(). Install it with: npm install @casl/prisma');
134
- }
135
- }
136
- //# sourceMappingURL=route-helpers.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"route-helpers.js","sourceRoot":"","sources":["../../src/authorization/route-helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAExD,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAGrD;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,MAAM,UAAU,SAAS,CACvB,OAAgB,EAChB,MAAc,EACd,WAAmB,EACnB,WAAoB;IAEpB,MAAM,OAAO,GAAG,UAAU,CAAI,OAAO,CAAC,CAAC;IAEvC,kDAAkD;IAClD,8CAA8C;IAC9C,8DAA8D;IAC7D,cAAc,CAAC,IAAI,CAAC,OAAO,CAAS,CAAC,cAAc,CAClD,MAAM,EACN,OAAO,CAAC,WAAW,EAAE,WAAW,IAAI,EAAE,CAAC,CACxC,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,UAAU,GAAG,CACjB,OAAgB,EAChB,MAAc,EACd,WAAmB,EACnB,WAAoB;IAEpB,MAAM,OAAO,GAAG,UAAU,CAAI,OAAO,CAAC,CAAC;IAEvC,0CAA0C;IAC1C,OAAO,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,OAAO,CAAC,WAAW,EAAE,WAAW,IAAI,EAAE,CAAC,CAAC,CAAC;AACtE,CAAC;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,UAAU,MAAM,CACpB,OAAgB,EAChB,MAAc,EACd,WAAmB,EACnB,WAAoB;IAEpB,OAAO,CAAC,GAAG,CAAI,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,WAAW,CAAC,CAAC;AAC5D,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,MAAM,UAAU,eAAe,CAC7B,OAAgB,EAChB,WAAmB,EACnB,eAAwB;IAExB,MAAM,OAAO,GAAG,UAAU,CAAI,OAAO,CAAC,CAAC;IAEvC,0CAA0C;IAC1C,sDAAsD;IACtD,IAAI,CAAC;QACH,8DAA8D;QAC9D,MAAM,EAAE,YAAY,EAAE,GAAG,OAAO,CAAC,cAAc,CAAC,CAAC;QAEjD,MAAM,YAAY,GAAG,YAAY,CAAC,OAAO,CAAC,CAAC,WAAW,CAAC,CAAC;QAExD,IAAI,eAAe,EAAE,CAAC;YACpB,OAAO;gBACL,GAAG,EAAE,CAAC,YAAY,EAAE,eAAe,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC;aACrD,CAAC;QACJ,CAAC;QAED,OAAO,YAAY,CAAC;IACtB,CAAC;IAAC,MAAM,CAAC;QACP,MAAM,IAAI,KAAK,CACb,2FAA2F,CAC5F,CAAC;IACJ,CAAC;AACH,CAAC"}
@@ -1,59 +0,0 @@
1
- import type { AuthorizedServiceOptions, BaseAbility, ServiceAuthMetadata } from './types.js';
2
- /**
3
- * Creates a transparent proxy around a service that automatically
4
- * checks CASL permissions before executing methods.
5
- *
6
- * **⚠️ DEPRECATED: Use the hooks system instead!**
7
- *
8
- * This function is deprecated in favor of the more flexible hooks system.
9
- * Instead of manually wrapping services, configure authorization globally:
10
- *
11
- * ```typescript
12
- * // NEW APPROACH (Recommended):
13
- * import { authorizationHook, defineServiceMetadata, defineAuthorize } from 'glasswork';
14
- *
15
- * const authServiceMeta = defineServiceMetadata({
16
- * deleteSession: {
17
- * authorize: defineAuthorize('delete', 'Session')
18
- * }
19
- * });
20
- *
21
- * bootstrap(AppModule, {
22
- * serviceHooks: [authorizationHook],
23
- * serviceMetadata: { authService: authServiceMeta }
24
- * });
25
- *
26
- * // In routes - services are automatically authorized!
27
- * await services.authService.deleteSession(token);
28
- * ```
29
- *
30
- * @deprecated Use hooks system with authorizationHook instead
31
- * @see {@link https://github.com/rolandboon/glasswork/blob/main/HOOKS_SYSTEM.md}
32
- *
33
- * @param service - The service instance to wrap
34
- * @param options - Authorization configuration
35
- * @returns Proxied service with automatic authorization
36
- */
37
- export declare function withAuthorization<T extends object>(service: T, options: AuthorizedServiceOptions): T;
38
- /**
39
- * Helper to define service authorization metadata.
40
- *
41
- * **⚠️ DEPRECATED: Use `defineServiceMetadata` and `defineAuthorize` from hooks system instead!**
42
- *
43
- * @deprecated Use `defineServiceMetadata` with `defineAuthorize` from hooks module
44
- * @param metadata - Service authorization metadata
45
- * @returns The same metadata (identity function)
46
- */
47
- export declare function defineServiceAuth(metadata: ServiceAuthMetadata): ServiceAuthMetadata;
48
- /**
49
- * Creates a factory function that wraps services with authorization.
50
- *
51
- * **⚠️ DEPRECATED: Use hooks system configured in bootstrap instead!**
52
- *
53
- * @deprecated Configure authorization globally with serviceHooks in bootstrap
54
- * @param ability - The CASL ability to use
55
- * @param metadataMap - Map of service names to their authorization metadata
56
- * @returns Function that wraps services with authorization
57
- */
58
- export declare function createServiceWrapper<T extends BaseAbility = BaseAbility>(ability: T, metadataMap: Record<string, ServiceAuthMetadata>): <S extends object>(serviceName: string, service: S) => S;
59
- //# sourceMappingURL=service-proxy.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"service-proxy.d.ts","sourceRoot":"","sources":["../../src/authorization/service-proxy.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,wBAAwB,EAAE,WAAW,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAC;AAE7F;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,SAAS,MAAM,EAChD,OAAO,EAAE,CAAC,EACV,OAAO,EAAE,wBAAwB,GAChC,CAAC,CAmDH;AAED;;;;;;;;GAQG;AACH,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,mBAAmB,GAAG,mBAAmB,CAEpF;AAED;;;;;;;;;GASG;AACH,wBAAgB,oBAAoB,CAAC,CAAC,SAAS,WAAW,GAAG,WAAW,EACtE,OAAO,EAAE,CAAC,EACV,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,mBAAmB,CAAC,GAC/C,CAAC,CAAC,SAAS,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,KAAK,CAAC,CAW1D"}
@@ -1,110 +0,0 @@
1
- import { ForbiddenError, subject } from '@casl/ability';
2
- /**
3
- * Creates a transparent proxy around a service that automatically
4
- * checks CASL permissions before executing methods.
5
- *
6
- * **⚠️ DEPRECATED: Use the hooks system instead!**
7
- *
8
- * This function is deprecated in favor of the more flexible hooks system.
9
- * Instead of manually wrapping services, configure authorization globally:
10
- *
11
- * ```typescript
12
- * // NEW APPROACH (Recommended):
13
- * import { authorizationHook, defineServiceMetadata, defineAuthorize } from 'glasswork';
14
- *
15
- * const authServiceMeta = defineServiceMetadata({
16
- * deleteSession: {
17
- * authorize: defineAuthorize('delete', 'Session')
18
- * }
19
- * });
20
- *
21
- * bootstrap(AppModule, {
22
- * serviceHooks: [authorizationHook],
23
- * serviceMetadata: { authService: authServiceMeta }
24
- * });
25
- *
26
- * // In routes - services are automatically authorized!
27
- * await services.authService.deleteSession(token);
28
- * ```
29
- *
30
- * @deprecated Use hooks system with authorizationHook instead
31
- * @see {@link https://github.com/rolandboon/glasswork/blob/main/HOOKS_SYSTEM.md}
32
- *
33
- * @param service - The service instance to wrap
34
- * @param options - Authorization configuration
35
- * @returns Proxied service with automatic authorization
36
- */
37
- export function withAuthorization(service, options) {
38
- const { ability, metadata, onUnauthorized } = options;
39
- return new Proxy(service, {
40
- get(target, prop) {
41
- // Only intercept function calls
42
- if (typeof prop !== 'string') {
43
- return Reflect.get(target, prop);
44
- }
45
- const originalMethod = Reflect.get(target, prop);
46
- // If it's not a function, return as-is
47
- if (typeof originalMethod !== 'function') {
48
- return originalMethod;
49
- }
50
- // Check if this method has authorization metadata
51
- const authMeta = metadata[prop];
52
- // If no metadata, return original method (no authorization)
53
- if (!authMeta) {
54
- return originalMethod.bind(target);
55
- }
56
- // Return wrapped method with authorization
57
- return function (...args) {
58
- const { action, subject: subjectType, getSubject } = authMeta;
59
- // Get subject data if extractor provided
60
- const subjectData = getSubject ? getSubject(...args) : undefined;
61
- // Check authorization using CASL
62
- try {
63
- ForbiddenError.from(ability).throwUnlessCan(action, subject(subjectType, subjectData || {}));
64
- }
65
- catch (error) {
66
- if (onUnauthorized) {
67
- onUnauthorized(prop, args);
68
- throw error; // Still throw after custom handler
69
- }
70
- throw error;
71
- }
72
- // Authorization passed - execute original method
73
- return originalMethod.apply(target, args);
74
- };
75
- },
76
- });
77
- }
78
- /**
79
- * Helper to define service authorization metadata.
80
- *
81
- * **⚠️ DEPRECATED: Use `defineServiceMetadata` and `defineAuthorize` from hooks system instead!**
82
- *
83
- * @deprecated Use `defineServiceMetadata` with `defineAuthorize` from hooks module
84
- * @param metadata - Service authorization metadata
85
- * @returns The same metadata (identity function)
86
- */
87
- export function defineServiceAuth(metadata) {
88
- return metadata;
89
- }
90
- /**
91
- * Creates a factory function that wraps services with authorization.
92
- *
93
- * **⚠️ DEPRECATED: Use hooks system configured in bootstrap instead!**
94
- *
95
- * @deprecated Configure authorization globally with serviceHooks in bootstrap
96
- * @param ability - The CASL ability to use
97
- * @param metadataMap - Map of service names to their authorization metadata
98
- * @returns Function that wraps services with authorization
99
- */
100
- export function createServiceWrapper(ability, metadataMap) {
101
- return (serviceName, service) => {
102
- const metadata = metadataMap[serviceName];
103
- if (!metadata) {
104
- // No metadata, return unwrapped service
105
- return service;
106
- }
107
- return withAuthorization(service, { ability, metadata });
108
- };
109
- }
110
- //# sourceMappingURL=service-proxy.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"service-proxy.js","sourceRoot":"","sources":["../../src/authorization/service-proxy.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAGxD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,MAAM,UAAU,iBAAiB,CAC/B,OAAU,EACV,OAAiC;IAEjC,MAAM,EAAE,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAE,GAAG,OAAO,CAAC;IAEtD,OAAO,IAAI,KAAK,CAAC,OAAO,EAAE;QACxB,GAAG,CAAC,MAAM,EAAE,IAAqB;YAC/B,gCAAgC;YAChC,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;gBAC7B,OAAO,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;YACnC,CAAC;YAED,MAAM,cAAc,GAAG,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;YAEjD,uCAAuC;YACvC,IAAI,OAAO,cAAc,KAAK,UAAU,EAAE,CAAC;gBACzC,OAAO,cAAc,CAAC;YACxB,CAAC;YAED,kDAAkD;YAClD,MAAM,QAAQ,GAAG,QAAQ,CAAC,IAAI,CAAC,CAAC;YAEhC,4DAA4D;YAC5D,IAAI,CAAC,QAAQ,EAAE,CAAC;gBACd,OAAO,cAAc,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;YACrC,CAAC;YAED,2CAA2C;YAC3C,OAAO,UAAyB,GAAG,IAAe;gBAChD,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,GAAG,QAAQ,CAAC;gBAE9D,yCAAyC;gBACzC,MAAM,WAAW,GAAG,UAAU,CAAC,CAAC,CAAC,UAAU,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;gBAEjE,iCAAiC;gBACjC,IAAI,CAAC;oBACH,cAAc,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,cAAc,CACzC,MAAM,EACN,OAAO,CAAC,WAAW,EAAE,WAAW,IAAI,EAAE,CAAC,CACxC,CAAC;gBACJ,CAAC;gBAAC,OAAO,KAAK,EAAE,CAAC;oBACf,IAAI,cAAc,EAAE,CAAC;wBACnB,cAAc,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;wBAC3B,MAAM,KAAK,CAAC,CAAC,mCAAmC;oBAClD,CAAC;oBACD,MAAM,KAAK,CAAC;gBACd,CAAC;gBAED,iDAAiD;gBACjD,OAAO,cAAc,CAAC,KAAK,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;YAC5C,CAAC,CAAC;QACJ,CAAC;KACF,CAAC,CAAC;AACL,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,iBAAiB,CAAC,QAA6B;IAC7D,OAAO,QAAQ,CAAC;AAClB,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,oBAAoB,CAClC,OAAU,EACV,WAAgD;IAEhD,OAAO,CAAmB,WAAmB,EAAE,OAAU,EAAK,EAAE;QAC9D,MAAM,QAAQ,GAAG,WAAW,CAAC,WAAW,CAAC,CAAC;QAE1C,IAAI,CAAC,QAAQ,EAAE,CAAC;YACd,wCAAwC;YACxC,OAAO,OAAO,CAAC;QACjB,CAAC;QAED,OAAO,iBAAiB,CAAC,OAAO,EAAE,EAAE,OAAO,EAAE,QAAQ,EAAE,CAAC,CAAC;IAC3D,CAAC,CAAC;AACJ,CAAC"}
@@ -1,58 +0,0 @@
1
- /**
2
- * Authorization types for CASL integration.
3
- *
4
- * This file contains core types for ability configuration and middleware.
5
- */
6
- import type { PureAbility } from '@casl/ability';
7
- import type { Context } from 'hono';
8
- /**
9
- * Base CASL ability type that applications can extend.
10
- * This type is re-exported from @casl/ability to maintain transparency.
11
- *
12
- * @example
13
- * ```typescript
14
- * import { createPrismaAbility } from '@casl/prisma';
15
- * import { AbilityBuilder } from '@casl/ability';
16
- *
17
- * type AppAbility = PureAbility<[Action, Subject]>;
18
- *
19
- * function defineAbility() {
20
- * const { can, build } = new AbilityBuilder<AppAbility>(createPrismaAbility);
21
- * can('read', 'User');
22
- * return build();
23
- * }
24
- * ```
25
- */
26
- export type BaseAbility = PureAbility<[string, string | object]>;
27
- /**
28
- * Factory function that creates an ability based on user context.
29
- * This function receives the full Hono context, allowing access to
30
- * user data, request information, etc.
31
- *
32
- * @example
33
- * ```typescript
34
- * const abilityFactory: AbilityFactory<AppAbility> = (c) => {
35
- * const user = c.get('user');
36
- * if (user.role === 'admin') {
37
- * return new AdminAbility();
38
- * }
39
- * return new UserAbility(user);
40
- * };
41
- * ```
42
- */
43
- export type AbilityFactory<T extends BaseAbility = BaseAbility> = (context: Context) => T | Promise<T>;
44
- /**
45
- * Configuration for ability-based authorization in routes.
46
- */
47
- export interface AbilityConfig<T extends BaseAbility = BaseAbility> {
48
- /**
49
- * Factory function to create the ability from request context.
50
- */
51
- factory: AbilityFactory<T>;
52
- /**
53
- * Optional key to store the ability in context variables.
54
- * @default 'ability'
55
- */
56
- contextKey?: string;
57
- }
58
- //# sourceMappingURL=types.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/authorization/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AACjD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAEpC;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,MAAM,WAAW,GAAG,WAAW,CAAC,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,CAAC,CAAC;AAEjE;;;;;;;;;;;;;;;GAeG;AACH,MAAM,MAAM,cAAc,CAAC,CAAC,SAAS,WAAW,GAAG,WAAW,IAAI,CAChE,OAAO,EAAE,OAAO,KACb,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;AAEpB;;GAEG;AACH,MAAM,WAAW,aAAa,CAAC,CAAC,SAAS,WAAW,GAAG,WAAW;IAChE;;OAEG;IACH,OAAO,EAAE,cAAc,CAAC,CAAC,CAAC,CAAC;IAE3B;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB"}
@@ -1,7 +0,0 @@
1
- /**
2
- * Authorization types for CASL integration.
3
- *
4
- * This file contains core types for ability configuration and middleware.
5
- */
6
- export {};
7
- //# sourceMappingURL=types.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/authorization/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG"}
@@ -1,100 +0,0 @@
1
- import type { ServiceHook } from './types.js';
2
- /**
3
- * Authorization metadata for a service method.
4
- * Defines what CASL action/subject to check before the method executes.
5
- */
6
- export interface AuthorizationMetadata {
7
- /**
8
- * CASL action (e.g., 'create', 'read', 'update', 'delete')
9
- */
10
- action: string;
11
- /**
12
- * CASL subject type (e.g., 'User', 'Post', 'Session')
13
- */
14
- subject: string;
15
- /**
16
- * Optional function to extract subject data from method arguments.
17
- * Used for field-level and instance-level authorization.
18
- *
19
- * @param args - The arguments passed to the service method
20
- * @returns The subject data to check against CASL rules
21
- *
22
- * @example
23
- * ```typescript
24
- * getSubjectData: (userId, updates) => ({ id: userId, ...updates })
25
- * ```
26
- */
27
- getSubjectData?: (...args: unknown[]) => object | undefined;
28
- }
29
- /**
30
- * Service hook that performs CASL authorization before method execution.
31
- *
32
- * This hook checks for `authorize` metadata on service methods and calls
33
- * the CASL `authorize()` function before allowing the method to execute.
34
- *
35
- * **Usage:**
36
- * 1. Define authorization metadata for your service methods
37
- * 2. Include this hook when wrapping services
38
- * 3. Ensure ability middleware is configured in your routes
39
- *
40
- * @example
41
- * ```typescript
42
- * import { authorizationHook, defineServiceMetadata } from 'glasswork';
43
- *
44
- * // Define metadata
45
- * export const authServiceMetadata = defineServiceMetadata({
46
- * deleteSession: {
47
- * authorize: {
48
- * action: 'delete',
49
- * subject: 'Session'
50
- * }
51
- * },
52
- * updateUser: {
53
- * authorize: {
54
- * action: 'update',
55
- * subject: 'User',
56
- * getSubjectData: (userId, data) => ({ id: userId, ...data })
57
- * }
58
- * }
59
- * });
60
- *
61
- * // Use in routes
62
- * router.delete('/session', async (c) => {
63
- * const services = withServiceHooks(
64
- * { authService },
65
- * {
66
- * hooks: [authorizationHook],
67
- * metadata: authServiceMetadata
68
- * },
69
- * c
70
- * );
71
- *
72
- * // This automatically checks authorization before executing
73
- * await services.authService.deleteSession(token);
74
- * });
75
- * ```
76
- */
77
- export declare const authorizationHook: ServiceHook;
78
- /**
79
- * Helper to define authorization metadata for service methods.
80
- * Provides better type inference for the authorize property.
81
- *
82
- * @param action - CASL action
83
- * @param subject - CASL subject type
84
- * @param getSubjectData - Optional function to extract subject data
85
- * @returns Authorization metadata object
86
- *
87
- * @example
88
- * ```typescript
89
- * export const userServiceMetadata = defineServiceMetadata({
90
- * update: {
91
- * authorize: defineAuthorize('update', 'User', (id, data) => ({ id, ...data }))
92
- * },
93
- * delete: {
94
- * authorize: defineAuthorize('delete', 'User', (id) => ({ id }))
95
- * }
96
- * });
97
- * ```
98
- */
99
- export declare function defineAuthorize(action: string, subject: string, getSubjectData?: (...args: unknown[]) => object | undefined): AuthorizationMetadata;
100
- //# sourceMappingURL=authorization-hook.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"authorization-hook.d.ts","sourceRoot":"","sources":["../../src/hooks/authorization-hook.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE9C;;;GAGG;AACH,MAAM,WAAW,qBAAqB;IACpC;;OAEG;IACH,MAAM,EAAE,MAAM,CAAC;IAEf;;OAEG;IACH,OAAO,EAAE,MAAM,CAAC;IAEhB;;;;;;;;;;;OAWG;IACH,cAAc,CAAC,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,MAAM,GAAG,SAAS,CAAC;CAC7D;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+CG;AACH,eAAO,MAAM,iBAAiB,EAAE,WAkB/B,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,eAAe,CAC7B,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,MAAM,EACf,cAAc,CAAC,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,MAAM,GAAG,SAAS,GAC1D,qBAAqB,CAMvB"}