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.
- 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/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 +5 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -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/list-query/schema-helpers.d.ts +2 -2
- package/dist/list-query/schema-helpers.js +2 -2
- package/dist/list-query/schema-helpers.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 +6 -1
- 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/hooks/types.js +0 -2
- package/dist/hooks/types.js.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
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
|
+
/**
|
|
3
|
+
* AsyncLocalStorage instance for request context.
|
|
4
|
+
* @internal
|
|
5
|
+
*/
|
|
6
|
+
export const requestContextStorage = new AsyncLocalStorage();
|
|
7
|
+
/**
|
|
8
|
+
* Get the current request context.
|
|
9
|
+
* Returns undefined if called outside a request lifecycle.
|
|
10
|
+
*
|
|
11
|
+
* @returns Current request context or undefined
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* import { getRequestContext } from 'glasswork';
|
|
16
|
+
*
|
|
17
|
+
* function someUtility() {
|
|
18
|
+
* const ctx = getRequestContext();
|
|
19
|
+
* if (ctx) {
|
|
20
|
+
* console.log('Request ID:', ctx.requestId);
|
|
21
|
+
* }
|
|
22
|
+
* }
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export function getRequestContext() {
|
|
26
|
+
return requestContextStorage.getStore();
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Get the current request ID.
|
|
30
|
+
* Returns undefined if called outside a request lifecycle.
|
|
31
|
+
*
|
|
32
|
+
* This is a convenience function for the common case of just needing the request ID.
|
|
33
|
+
*
|
|
34
|
+
* @returns Current request ID or undefined
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```typescript
|
|
38
|
+
* import { getRequestId } from 'glasswork';
|
|
39
|
+
*
|
|
40
|
+
* class UserService {
|
|
41
|
+
* async create(data: CreateUserDto) {
|
|
42
|
+
* const requestId = getRequestId();
|
|
43
|
+
* this.logger.info({ requestId, email: data.email }, 'Creating user');
|
|
44
|
+
* // ...
|
|
45
|
+
* }
|
|
46
|
+
* }
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export function getRequestId() {
|
|
50
|
+
return requestContextStorage.getStore()?.requestId;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Set user information in the current request context.
|
|
54
|
+
* Useful for correlating logs and errors with users after authentication.
|
|
55
|
+
*
|
|
56
|
+
* @param userId - User identifier
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```typescript
|
|
60
|
+
* import { setRequestUser } from 'glasswork';
|
|
61
|
+
*
|
|
62
|
+
* // In auth middleware
|
|
63
|
+
* const user = await validateToken(token);
|
|
64
|
+
* setRequestUser(user.id);
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
export function setRequestUser(userId) {
|
|
68
|
+
const store = requestContextStorage.getStore();
|
|
69
|
+
if (store) {
|
|
70
|
+
store.userId = userId;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Set custom context data for the current request.
|
|
75
|
+
* Useful for adding business-specific context to logs.
|
|
76
|
+
*
|
|
77
|
+
* @param key - Context key
|
|
78
|
+
* @param value - Context value
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```typescript
|
|
82
|
+
* import { setRequestContext } from 'glasswork';
|
|
83
|
+
*
|
|
84
|
+
* // Add tenant ID for multi-tenant apps
|
|
85
|
+
* setRequestContext('tenantId', tenant.id);
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
88
|
+
export function setRequestContextValue(key, value) {
|
|
89
|
+
const store = requestContextStorage.getStore();
|
|
90
|
+
if (store) {
|
|
91
|
+
store.custom[key] = value;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Middleware that initializes AsyncLocalStorage request context.
|
|
96
|
+
* Applied automatically by bootstrap when using the built-in logger.
|
|
97
|
+
*
|
|
98
|
+
* @returns Hono middleware handler
|
|
99
|
+
* @internal
|
|
100
|
+
*/
|
|
101
|
+
export function createRequestContextMiddleware() {
|
|
102
|
+
return async (c, next) => {
|
|
103
|
+
const requestId = c.get('requestId') || crypto.randomUUID();
|
|
104
|
+
const context = {
|
|
105
|
+
requestId,
|
|
106
|
+
method: c.req.method,
|
|
107
|
+
path: c.req.path,
|
|
108
|
+
custom: {},
|
|
109
|
+
};
|
|
110
|
+
await requestContextStorage.run(context, async () => {
|
|
111
|
+
await next();
|
|
112
|
+
});
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
//# sourceMappingURL=request-context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request-context.js","sourceRoot":"","sources":["../../src/observability/request-context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAoBrD;;;GAGG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG,IAAI,iBAAiB,EAAkB,CAAC;AAE7E;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,iBAAiB;IAC/B,OAAO,qBAAqB,CAAC,QAAQ,EAAE,CAAC;AAC1C,CAAC;AAED;;;;;;;;;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/dist/openapi/openapi.js
CHANGED
|
@@ -36,7 +36,9 @@ export function configureOpenAPI(options) {
|
|
|
36
36
|
if (!openapi.enabled) {
|
|
37
37
|
return {};
|
|
38
38
|
}
|
|
39
|
-
|
|
39
|
+
// Use 'error' level so errors are always logged (even in test mode)
|
|
40
|
+
// This ensures OpenAPI spec writing errors are visible
|
|
41
|
+
const logger = createLogger('Glasswork:OpenAPI', 'error');
|
|
40
42
|
const shouldServeSpecs = openapi.serveSpecs ?? environment === 'development';
|
|
41
43
|
const shouldServeUI = openapi.serveUI ?? environment === 'development';
|
|
42
44
|
// Filter components based on enabled features (only include headers that are actually used)
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openapi.js","sourceRoot":"","sources":["../../src/openapi/openapi.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC7C,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAE7C,OAAO,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AAQnD,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AACnD,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,wBAAwB,EAAE,MAAM,eAAe,CAAC;AAEzD,iFAAiF;AACjF,MAAM,sBAAsB,GAAG,IAAI,CAAC;AAgCpC;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,UAAU,gBAAgB,CAAC,OAAgC;IAC/D,MAAM,EAAE,GAAG,EAAE,WAAW,EAAE,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,GAAG,OAAO,CAAC;IAErE,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,CAAC;QACrB,OAAO,EAAE,CAAC;IACZ,CAAC;IAED,MAAM,MAAM,GAAG,YAAY,CAAC,mBAAmB,EAAE,
|
|
1
|
+
{"version":3,"file":"openapi.js","sourceRoot":"","sources":["../../src/openapi/openapi.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CAAC;IAED,mCAAmC;IACnC,OAAO,IAAI,CAAC;AACd,CAAC"}
|
|
@@ -11,7 +11,8 @@ export declare function isLambda(): boolean;
|
|
|
11
11
|
*/
|
|
12
12
|
export declare function isProduction(): boolean;
|
|
13
13
|
/**
|
|
14
|
-
* Check if running in test environment
|
|
14
|
+
* Check if running in test environment.
|
|
15
|
+
* Detects both NODE_ENV=test and test runners (Vitest, Jest).
|
|
15
16
|
*/
|
|
16
17
|
export declare function isTest(): boolean;
|
|
17
18
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"environment.d.ts","sourceRoot":"","sources":["../../src/utils/environment.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;GAEG;AACH,wBAAgB,QAAQ,IAAI,OAAO,CAElC;AAED;;;GAGG;AACH,wBAAgB,YAAY,IAAI,OAAO,CAEtC;AAED
|
|
1
|
+
{"version":3,"file":"environment.d.ts","sourceRoot":"","sources":["../../src/utils/environment.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;GAEG;AACH,wBAAgB,QAAQ,IAAI,OAAO,CAElC;AAED;;;GAGG;AACH,wBAAgB,YAAY,IAAI,OAAO,CAEtC;AAED;;;GAGG;AACH,wBAAgB,MAAM,IAAI,OAAO,CAUhC;AAED;;GAEG;AACH,wBAAgB,aAAa,IAAI,OAAO,CAEvC"}
|
|
@@ -15,10 +15,19 @@ export function isProduction() {
|
|
|
15
15
|
return process.env.NODE_ENV === 'production' || isLambda();
|
|
16
16
|
}
|
|
17
17
|
/**
|
|
18
|
-
* Check if running in test environment
|
|
18
|
+
* Check if running in test environment.
|
|
19
|
+
* Detects both NODE_ENV=test and test runners (Vitest, Jest).
|
|
19
20
|
*/
|
|
20
21
|
export function isTest() {
|
|
21
|
-
|
|
22
|
+
// Check NODE_ENV first
|
|
23
|
+
if (process.env.NODE_ENV === 'test') {
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
// Check for test runners (Vitest sets VITEST, Jest sets JEST_WORKER_ID)
|
|
27
|
+
if (process.env.VITEST === 'true' || process.env.JEST_WORKER_ID) {
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
return false;
|
|
22
31
|
}
|
|
23
32
|
/**
|
|
24
33
|
* Check if running in development environment
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"environment.js","sourceRoot":"","sources":["../../src/utils/environment.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;GAEG;AACH,MAAM,UAAU,QAAQ;IACtB,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,wBAAwB,IAAI,OAAO,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAC;AAClF,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,YAAY;IAC1B,OAAO,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY,IAAI,QAAQ,EAAE,CAAC;AAC7D,CAAC;AAED
|
|
1
|
+
{"version":3,"file":"environment.js","sourceRoot":"","sources":["../../src/utils/environment.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;GAEG;AACH,MAAM,UAAU,QAAQ;IACtB,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,wBAAwB,IAAI,OAAO,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAC;AAClF,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,YAAY;IAC1B,OAAO,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY,IAAI,QAAQ,EAAE,CAAC;AAC7D,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,MAAM;IACpB,uBAAuB;IACvB,IAAI,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,MAAM,EAAE,CAAC;QACpC,OAAO,IAAI,CAAC;IACd,CAAC;IACD,wEAAwE;IACxE,IAAI,OAAO,CAAC,GAAG,CAAC,MAAM,KAAK,MAAM,IAAI,OAAO,CAAC,GAAG,CAAC,cAAc,EAAE,CAAC;QAChE,OAAO,IAAI,CAAC;IACd,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,aAAa;IAC3B,OAAO,CAAC,YAAY,EAAE,IAAI,CAAC,MAAM,EAAE,CAAC;AACtC,CAAC"}
|
package/dist/utils/logger.d.ts
CHANGED
|
@@ -1,30 +1,64 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Log levels matching Pino's level system.
|
|
3
|
+
* Lower numbers = more verbose, higher numbers = less verbose.
|
|
4
|
+
*/
|
|
5
|
+
export type LogLevel = 'silent' | 'error' | 'warn' | 'info' | 'debug';
|
|
6
|
+
/**
|
|
7
|
+
* Get the default log level based on environment.
|
|
8
|
+
* - test: silent (no logs)
|
|
9
|
+
* - development/production: info
|
|
10
|
+
*/
|
|
11
|
+
export declare function getDefaultLogLevel(): LogLevel;
|
|
12
|
+
/**
|
|
13
|
+
* Logger interface for framework and application logging.
|
|
14
|
+
*
|
|
15
|
+
* This interface is compatible with popular logging libraries like Pino.
|
|
16
|
+
* The optional `child()` method enables request-scoped logging with automatic context binding.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```typescript
|
|
20
|
+
* // Console-based logger
|
|
21
|
+
* const logger = createLogger('MyService');
|
|
22
|
+
* logger.info('Hello', { userId: '123' });
|
|
23
|
+
*
|
|
24
|
+
* // Pino logger (recommended for Lambda)
|
|
25
|
+
* import pino from 'pino';
|
|
26
|
+
* const logger = pino({ level: 'info' });
|
|
27
|
+
* ```
|
|
3
28
|
*/
|
|
4
29
|
export interface Logger {
|
|
5
30
|
debug(message: string, ...meta: unknown[]): void;
|
|
6
31
|
info(message: string, ...meta: unknown[]): void;
|
|
7
32
|
warn(message: string, ...meta: unknown[]): void;
|
|
8
33
|
error(message: string, ...meta: unknown[]): void;
|
|
34
|
+
/**
|
|
35
|
+
* Create a child logger with bound context (Pino-compatible).
|
|
36
|
+
* @param bindings - Context to bind to all log messages
|
|
37
|
+
*/
|
|
38
|
+
child?(bindings: Record<string, unknown>): Logger;
|
|
9
39
|
}
|
|
10
|
-
/**
|
|
11
|
-
* Default console logger (used by framework internally)
|
|
12
|
-
*/
|
|
13
40
|
export declare const defaultLogger: Logger;
|
|
14
41
|
/**
|
|
15
|
-
* Create a logger with a prefix for namespacing
|
|
42
|
+
* Create a logger with a prefix for namespacing.
|
|
43
|
+
*
|
|
44
|
+
* This is a simple console-based logger suitable for development.
|
|
45
|
+
* For production Lambda deployments, use Pino with `createContextAwarePinoLogger`.
|
|
16
46
|
*
|
|
17
47
|
* @param prefix - Prefix to add to all log messages
|
|
18
|
-
* @param
|
|
48
|
+
* @param level - Log level (default: 'silent' in test, 'info' otherwise)
|
|
19
49
|
* @returns Logger instance
|
|
20
50
|
*
|
|
21
51
|
* @example
|
|
22
52
|
* ```typescript
|
|
23
|
-
* const logger = createLogger('
|
|
24
|
-
* logger.info('
|
|
53
|
+
* const logger = createLogger('UserService');
|
|
54
|
+
* logger.info('Creating user'); // [UserService] Creating user
|
|
55
|
+
*
|
|
56
|
+
* // Silent logger for tests
|
|
57
|
+
* const testLogger = createLogger('TestService', 'silent');
|
|
58
|
+
* testLogger.info('This will not log'); // No output
|
|
25
59
|
* ```
|
|
26
60
|
*/
|
|
27
|
-
export declare function createLogger(prefix: string,
|
|
61
|
+
export declare function createLogger(prefix: string, level?: LogLevel): Logger;
|
|
28
62
|
/**
|
|
29
63
|
* Create a plain logger middleware for Lambda (strips ANSI color codes).
|
|
30
64
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../src/utils/logger.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../src/utils/logger.ts"],"names":[],"mappings":"AAGA;;;GAGG;AACH,MAAM,MAAM,QAAQ,GAAG,QAAQ,GAAG,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC;AActE;;;;GAIG;AACH,wBAAgB,kBAAkB,IAAI,QAAQ,CAE7C;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,WAAW,MAAM;IACrB,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,IAAI,CAAC;IACjD,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,IAAI,CAAC;IAChD,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,IAAI,CAAC;IAChD,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,IAAI,CAAC;IAEjD;;;OAGG;IACH,KAAK,CAAC,CAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC;CACnD;AAQD,eAAO,MAAM,aAAa,EAAE,MAUxB,CAAC;AAEL;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,QAAQ,GAAG,MAAM,CA4BrE;AAED;;;;;;;GAOG;AACH,wBAAgB,iBAAiB,qCAQhC"}
|
package/dist/utils/logger.js
CHANGED
|
@@ -1,36 +1,80 @@
|
|
|
1
1
|
import { logger as honoLogger } from 'hono/logger';
|
|
2
|
+
import { isTest } from './environment.js';
|
|
2
3
|
/**
|
|
3
|
-
*
|
|
4
|
+
* Log level numeric values (matching Pino).
|
|
5
|
+
* Used for level comparison.
|
|
4
6
|
*/
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
warn:
|
|
9
|
-
|
|
7
|
+
const LOG_LEVELS = {
|
|
8
|
+
silent: 60,
|
|
9
|
+
error: 50,
|
|
10
|
+
warn: 40,
|
|
11
|
+
info: 30,
|
|
12
|
+
debug: 20,
|
|
10
13
|
};
|
|
11
14
|
/**
|
|
12
|
-
*
|
|
15
|
+
* Get the default log level based on environment.
|
|
16
|
+
* - test: silent (no logs)
|
|
17
|
+
* - development/production: info
|
|
18
|
+
*/
|
|
19
|
+
export function getDefaultLogLevel() {
|
|
20
|
+
return isTest() ? 'silent' : 'info';
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Default console logger (used by framework internally).
|
|
24
|
+
* Respects LOG_LEVEL environment variable or defaults to info.
|
|
25
|
+
*/
|
|
26
|
+
const defaultLevel = process.env.LOG_LEVEL || getDefaultLogLevel();
|
|
27
|
+
export const defaultLogger = (() => {
|
|
28
|
+
const level = LOG_LEVELS[defaultLevel];
|
|
29
|
+
const noop = () => { };
|
|
30
|
+
return {
|
|
31
|
+
debug: level <= LOG_LEVELS.debug ? console.debug.bind(console) : noop,
|
|
32
|
+
info: level <= LOG_LEVELS.info ? console.log.bind(console) : noop,
|
|
33
|
+
warn: level <= LOG_LEVELS.warn ? console.warn.bind(console) : noop,
|
|
34
|
+
error: level <= LOG_LEVELS.error ? console.error.bind(console) : noop,
|
|
35
|
+
};
|
|
36
|
+
})();
|
|
37
|
+
/**
|
|
38
|
+
* Create a logger with a prefix for namespacing.
|
|
39
|
+
*
|
|
40
|
+
* This is a simple console-based logger suitable for development.
|
|
41
|
+
* For production Lambda deployments, use Pino with `createContextAwarePinoLogger`.
|
|
13
42
|
*
|
|
14
43
|
* @param prefix - Prefix to add to all log messages
|
|
15
|
-
* @param
|
|
44
|
+
* @param level - Log level (default: 'silent' in test, 'info' otherwise)
|
|
16
45
|
* @returns Logger instance
|
|
17
46
|
*
|
|
18
47
|
* @example
|
|
19
48
|
* ```typescript
|
|
20
|
-
* const logger = createLogger('
|
|
21
|
-
* logger.info('
|
|
49
|
+
* const logger = createLogger('UserService');
|
|
50
|
+
* logger.info('Creating user'); // [UserService] Creating user
|
|
51
|
+
*
|
|
52
|
+
* // Silent logger for tests
|
|
53
|
+
* const testLogger = createLogger('TestService', 'silent');
|
|
54
|
+
* testLogger.info('This will not log'); // No output
|
|
22
55
|
* ```
|
|
23
56
|
*/
|
|
24
|
-
export function createLogger(prefix,
|
|
25
|
-
|
|
57
|
+
export function createLogger(prefix, level) {
|
|
58
|
+
const logLevel = level ?? getDefaultLogLevel();
|
|
59
|
+
const levelValue = LOG_LEVELS[logLevel];
|
|
60
|
+
// Silent logger - all methods are no-ops
|
|
61
|
+
if (logLevel === 'silent') {
|
|
26
62
|
const noop = () => { };
|
|
27
63
|
return { debug: noop, info: noop, warn: noop, error: noop };
|
|
28
64
|
}
|
|
29
65
|
return {
|
|
30
|
-
debug:
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
66
|
+
debug: levelValue <= LOG_LEVELS.debug
|
|
67
|
+
? (msg, ...meta) => console.debug(`[${prefix}] ${msg}`, ...meta)
|
|
68
|
+
: () => { },
|
|
69
|
+
info: levelValue <= LOG_LEVELS.info
|
|
70
|
+
? (msg, ...meta) => console.log(`[${prefix}] ${msg}`, ...meta)
|
|
71
|
+
: () => { },
|
|
72
|
+
warn: levelValue <= LOG_LEVELS.warn
|
|
73
|
+
? (msg, ...meta) => console.warn(`[${prefix}] ${msg}`, ...meta)
|
|
74
|
+
: () => { },
|
|
75
|
+
error: levelValue <= LOG_LEVELS.error
|
|
76
|
+
? (msg, ...meta) => console.error(`[${prefix}] ${msg}`, ...meta)
|
|
77
|
+
: () => { },
|
|
34
78
|
};
|
|
35
79
|
}
|
|
36
80
|
/**
|
package/dist/utils/logger.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"logger.js","sourceRoot":"","sources":["../../src/utils/logger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,UAAU,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"logger.js","sourceRoot":"","sources":["../../src/utils/logger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,UAAU,EAAE,MAAM,aAAa,CAAC;AACnD,O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I,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,MAAM,KAAK,GAAG,EAAE,EAAE,GAAG,IAAI,CAAC;YAChE,CAAC,CAAC,GAAG,EAAE,GAAE,CAAC;KACf,CAAC;AACJ,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,iBAAiB;IAC/B,6FAA6F;IAC7F,MAAM,SAAS,GAAG,CAAC,KAAa,EAAU,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,mBAAmB,EAAE,EAAE,CAAC,CAAC;IAEpF,OAAO,UAAU,CAAC,CAAC,GAAW,EAAE,GAAG,IAAc,EAAE,EAAE;QACnD,MAAM,OAAO,GAAG,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QACzC,OAAO,CAAC,GAAG,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,CAAC;IAClC,CAAC,CAAC,CAAC;AACL,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "glasswork",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"description": "A transparent, Lambda-optimized web framework for building OpenAPI-compliant REST APIs.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -61,6 +61,7 @@
|
|
|
61
61
|
"access": "public"
|
|
62
62
|
},
|
|
63
63
|
"peerDependencies": {
|
|
64
|
+
"@aws-sdk/client-cloudwatch": "^3.900.0",
|
|
64
65
|
"@aws-sdk/client-dynamodb": "^3.900.0",
|
|
65
66
|
"@aws-sdk/client-ssm": "^3.900.0",
|
|
66
67
|
"@aws-sdk/lib-dynamodb": "^3.900.0",
|
|
@@ -74,6 +75,9 @@
|
|
|
74
75
|
"valibot": "^1.0.0"
|
|
75
76
|
},
|
|
76
77
|
"peerDependenciesMeta": {
|
|
78
|
+
"@aws-sdk/client-cloudwatch": {
|
|
79
|
+
"optional": true
|
|
80
|
+
},
|
|
77
81
|
"@aws-sdk/client-dynamodb": {
|
|
78
82
|
"optional": true
|
|
79
83
|
},
|
|
@@ -97,6 +101,7 @@
|
|
|
97
101
|
}
|
|
98
102
|
},
|
|
99
103
|
"devDependencies": {
|
|
104
|
+
"@aws-sdk/client-cloudwatch": "^3.932.0",
|
|
100
105
|
"@aws-sdk/client-dynamodb": "^3.932.0",
|
|
101
106
|
"@aws-sdk/client-ssm": "^3.932.0",
|
|
102
107
|
"@aws-sdk/lib-dynamodb": "^3.932.0",
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import type { Context, MiddlewareHandler } from 'hono';
|
|
2
|
-
import type { AbilityConfig, BaseAbility } from './types.js';
|
|
3
|
-
/**
|
|
4
|
-
* Creates Hono middleware that attaches a CASL ability to the request context.
|
|
5
|
-
*
|
|
6
|
-
* This middleware is transparent - it exposes the CASL ability directly in the
|
|
7
|
-
* Hono context, making it accessible to route handlers. All CASL features
|
|
8
|
-
* remain available.
|
|
9
|
-
*
|
|
10
|
-
* @param config - Ability configuration with factory function
|
|
11
|
-
* @returns Hono middleware that creates and attaches the ability
|
|
12
|
-
*
|
|
13
|
-
* @example
|
|
14
|
-
* ```typescript
|
|
15
|
-
* import { createPrismaAbility } from '@casl/prisma';
|
|
16
|
-
* import { AbilityBuilder } from '@casl/ability';
|
|
17
|
-
*
|
|
18
|
-
* const abilityMiddleware = withAbility({
|
|
19
|
-
* factory: (c) => {
|
|
20
|
-
* const user = c.get('user');
|
|
21
|
-
* const { can, build } = new AbilityBuilder(createPrismaAbility);
|
|
22
|
-
*
|
|
23
|
-
* if (user.role === 'admin') {
|
|
24
|
-
* can('manage', 'all');
|
|
25
|
-
* } else {
|
|
26
|
-
* can('read', 'User', { id: user.id });
|
|
27
|
-
* }
|
|
28
|
-
*
|
|
29
|
-
* return build();
|
|
30
|
-
* }
|
|
31
|
-
* });
|
|
32
|
-
*
|
|
33
|
-
* app.use('*', abilityMiddleware);
|
|
34
|
-
* ```
|
|
35
|
-
*/
|
|
36
|
-
export declare function withAbility<T extends BaseAbility = BaseAbility>(config: AbilityConfig<T>): MiddlewareHandler;
|
|
37
|
-
/**
|
|
38
|
-
* Helper to retrieve the ability from Hono context.
|
|
39
|
-
* This provides type-safe access to the CASL ability stored by `withAbility`.
|
|
40
|
-
*
|
|
41
|
-
* @param context - Hono context
|
|
42
|
-
* @param contextKey - The key used to store the ability (default: 'ability')
|
|
43
|
-
* @returns The CASL ability instance
|
|
44
|
-
* @throws Error if ability is not found in context
|
|
45
|
-
*
|
|
46
|
-
* @example
|
|
47
|
-
* ```typescript
|
|
48
|
-
* router.get('/users', async (c) => {
|
|
49
|
-
* const ability = getAbility(c);
|
|
50
|
-
*
|
|
51
|
-
* // Use CASL ability directly
|
|
52
|
-
* if (ability.can('read', 'User')) {
|
|
53
|
-
* return c.json(users);
|
|
54
|
-
* }
|
|
55
|
-
*
|
|
56
|
-
* return c.json({ error: 'Forbidden' }, 403);
|
|
57
|
-
* });
|
|
58
|
-
* ```
|
|
59
|
-
*/
|
|
60
|
-
export declare function getAbility<T extends BaseAbility = BaseAbility>(context: Context, contextKey?: string): T;
|
|
61
|
-
//# sourceMappingURL=ability-middleware.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ability-middleware.d.ts","sourceRoot":"","sources":["../../src/authorization/ability-middleware.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,iBAAiB,EAAE,MAAM,MAAM,CAAC;AACvD,OAAO,KAAK,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE7D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,wBAAgB,WAAW,CAAC,CAAC,SAAS,WAAW,GAAG,WAAW,EAC7D,MAAM,EAAE,aAAa,CAAC,CAAC,CAAC,GACvB,iBAAiB,CAYnB;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,UAAU,CAAC,CAAC,SAAS,WAAW,GAAG,WAAW,EAC5D,OAAO,EAAE,OAAO,EAChB,UAAU,SAAY,GACrB,CAAC,CAUH"}
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Creates Hono middleware that attaches a CASL ability to the request context.
|
|
3
|
-
*
|
|
4
|
-
* This middleware is transparent - it exposes the CASL ability directly in the
|
|
5
|
-
* Hono context, making it accessible to route handlers. All CASL features
|
|
6
|
-
* remain available.
|
|
7
|
-
*
|
|
8
|
-
* @param config - Ability configuration with factory function
|
|
9
|
-
* @returns Hono middleware that creates and attaches the ability
|
|
10
|
-
*
|
|
11
|
-
* @example
|
|
12
|
-
* ```typescript
|
|
13
|
-
* import { createPrismaAbility } from '@casl/prisma';
|
|
14
|
-
* import { AbilityBuilder } from '@casl/ability';
|
|
15
|
-
*
|
|
16
|
-
* const abilityMiddleware = withAbility({
|
|
17
|
-
* factory: (c) => {
|
|
18
|
-
* const user = c.get('user');
|
|
19
|
-
* const { can, build } = new AbilityBuilder(createPrismaAbility);
|
|
20
|
-
*
|
|
21
|
-
* if (user.role === 'admin') {
|
|
22
|
-
* can('manage', 'all');
|
|
23
|
-
* } else {
|
|
24
|
-
* can('read', 'User', { id: user.id });
|
|
25
|
-
* }
|
|
26
|
-
*
|
|
27
|
-
* return build();
|
|
28
|
-
* }
|
|
29
|
-
* });
|
|
30
|
-
*
|
|
31
|
-
* app.use('*', abilityMiddleware);
|
|
32
|
-
* ```
|
|
33
|
-
*/
|
|
34
|
-
export function withAbility(config) {
|
|
35
|
-
const contextKey = config.contextKey || 'ability';
|
|
36
|
-
return async (c, next) => {
|
|
37
|
-
// Create ability using factory
|
|
38
|
-
const ability = await config.factory(c);
|
|
39
|
-
// Store in Hono context
|
|
40
|
-
c.set(contextKey, ability);
|
|
41
|
-
await next();
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* Helper to retrieve the ability from Hono context.
|
|
46
|
-
* This provides type-safe access to the CASL ability stored by `withAbility`.
|
|
47
|
-
*
|
|
48
|
-
* @param context - Hono context
|
|
49
|
-
* @param contextKey - The key used to store the ability (default: 'ability')
|
|
50
|
-
* @returns The CASL ability instance
|
|
51
|
-
* @throws Error if ability is not found in context
|
|
52
|
-
*
|
|
53
|
-
* @example
|
|
54
|
-
* ```typescript
|
|
55
|
-
* router.get('/users', async (c) => {
|
|
56
|
-
* const ability = getAbility(c);
|
|
57
|
-
*
|
|
58
|
-
* // Use CASL ability directly
|
|
59
|
-
* if (ability.can('read', 'User')) {
|
|
60
|
-
* return c.json(users);
|
|
61
|
-
* }
|
|
62
|
-
*
|
|
63
|
-
* return c.json({ error: 'Forbidden' }, 403);
|
|
64
|
-
* });
|
|
65
|
-
* ```
|
|
66
|
-
*/
|
|
67
|
-
export function getAbility(context, contextKey = 'ability') {
|
|
68
|
-
const ability = context.get(contextKey);
|
|
69
|
-
if (!ability) {
|
|
70
|
-
throw new Error(`Ability not found in context. Did you forget to use withAbility() middleware?`);
|
|
71
|
-
}
|
|
72
|
-
return ability;
|
|
73
|
-
}
|
|
74
|
-
//# sourceMappingURL=ability-middleware.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ability-middleware.js","sourceRoot":"","sources":["../../src/authorization/ability-middleware.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,MAAM,UAAU,WAAW,CACzB,MAAwB;IAExB,MAAM,UAAU,GAAG,MAAM,CAAC,UAAU,IAAI,SAAS,CAAC;IAElD,OAAO,KAAK,EAAE,CAAU,EAAE,IAAI,EAAE,EAAE;QAChC,+BAA+B;QAC/B,MAAM,OAAO,GAAG,MAAM,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;QAExC,wBAAwB;QACxB,CAAC,CAAC,GAAG,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;QAE3B,MAAM,IAAI,EAAE,CAAC;IACf,CAAC,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,UAAU,UAAU,CACxB,OAAgB,EAChB,UAAU,GAAG,SAAS;IAEtB,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;IAExC,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,MAAM,IAAI,KAAK,CACb,+EAA+E,CAChF,CAAC;IACJ,CAAC;IAED,OAAO,OAAY,CAAC;AACtB,CAAC"}
|
|
@@ -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 type { PureAbility } from '@casl/ability';
|
|
42
|
-
export { ForbiddenError, subject } from '@casl/ability';
|
|
43
|
-
export { getAbility, withAbility } from './ability-middleware.js';
|
|
44
|
-
export { accessibleWhere, authorize, can, cannot } from './route-helpers.js';
|
|
45
|
-
export type { AbilityConfig, AbilityFactory, BaseAbility } from './types.js';
|
|
46
|
-
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/authorization/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AAGH,YAAY,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AACjD,OAAO,EAAE,cAAc,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAExD,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAElE,OAAO,EAAE,eAAe,EAAE,SAAS,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAE7E,YAAY,EAAE,aAAa,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC"}
|