digitaltwin-core 0.14.3 → 1.0.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 (159) hide show
  1. package/README.md +218 -1
  2. package/dist/auth/apisix_parser.d.ts +56 -56
  3. package/dist/auth/apisix_parser.d.ts.map +1 -1
  4. package/dist/auth/apisix_parser.js +72 -86
  5. package/dist/auth/apisix_parser.js.map +1 -1
  6. package/dist/auth/auth_provider.d.ts +118 -0
  7. package/dist/auth/auth_provider.d.ts.map +1 -0
  8. package/dist/auth/auth_provider.js +8 -0
  9. package/dist/auth/auth_provider.js.map +1 -0
  10. package/dist/auth/auth_provider_factory.d.ts +91 -0
  11. package/dist/auth/auth_provider_factory.d.ts.map +1 -0
  12. package/dist/auth/auth_provider_factory.js +146 -0
  13. package/dist/auth/auth_provider_factory.js.map +1 -0
  14. package/dist/auth/index.d.ts +4 -1
  15. package/dist/auth/index.d.ts.map +1 -1
  16. package/dist/auth/index.js +3 -0
  17. package/dist/auth/index.js.map +1 -1
  18. package/dist/auth/providers/gateway_auth_provider.d.ts +78 -0
  19. package/dist/auth/providers/gateway_auth_provider.d.ts.map +1 -0
  20. package/dist/auth/providers/gateway_auth_provider.js +109 -0
  21. package/dist/auth/providers/gateway_auth_provider.js.map +1 -0
  22. package/dist/auth/providers/index.d.ts +4 -0
  23. package/dist/auth/providers/index.d.ts.map +1 -0
  24. package/dist/auth/providers/index.js +4 -0
  25. package/dist/auth/providers/index.js.map +1 -0
  26. package/dist/auth/providers/jwt_auth_provider.d.ts +91 -0
  27. package/dist/auth/providers/jwt_auth_provider.d.ts.map +1 -0
  28. package/dist/auth/providers/jwt_auth_provider.js +204 -0
  29. package/dist/auth/providers/jwt_auth_provider.js.map +1 -0
  30. package/dist/auth/providers/no_auth_provider.d.ts +61 -0
  31. package/dist/auth/providers/no_auth_provider.d.ts.map +1 -0
  32. package/dist/auth/providers/no_auth_provider.js +76 -0
  33. package/dist/auth/providers/no_auth_provider.js.map +1 -0
  34. package/dist/auth/types.d.ts +5 -3
  35. package/dist/auth/types.d.ts.map +1 -1
  36. package/dist/components/assets_manager.d.ts +1 -1
  37. package/dist/components/assets_manager.d.ts.map +1 -1
  38. package/dist/components/assets_manager.js +54 -48
  39. package/dist/components/assets_manager.js.map +1 -1
  40. package/dist/components/collector.d.ts.map +1 -1
  41. package/dist/components/collector.js +30 -18
  42. package/dist/components/collector.js.map +1 -1
  43. package/dist/components/custom_table_manager.d.ts.map +1 -1
  44. package/dist/components/custom_table_manager.js +36 -65
  45. package/dist/components/custom_table_manager.js.map +1 -1
  46. package/dist/components/global_assets_handler.d.ts +4 -2
  47. package/dist/components/global_assets_handler.d.ts.map +1 -1
  48. package/dist/components/global_assets_handler.js.map +1 -1
  49. package/dist/components/harvester.d.ts.map +1 -1
  50. package/dist/components/harvester.js +46 -33
  51. package/dist/components/harvester.js.map +1 -1
  52. package/dist/components/interfaces.d.ts +3 -2
  53. package/dist/components/interfaces.d.ts.map +1 -1
  54. package/dist/components/map_manager.d.ts.map +1 -1
  55. package/dist/components/map_manager.js.map +1 -1
  56. package/dist/components/tileset_manager.d.ts +2 -1
  57. package/dist/components/tileset_manager.d.ts.map +1 -1
  58. package/dist/components/tileset_manager.js +20 -15
  59. package/dist/components/tileset_manager.js.map +1 -1
  60. package/dist/database/adapters/knex_database_adapter.d.ts +6 -1
  61. package/dist/database/adapters/knex_database_adapter.d.ts.map +1 -1
  62. package/dist/database/adapters/knex_database_adapter.js +118 -36
  63. package/dist/database/adapters/knex_database_adapter.js.map +1 -1
  64. package/dist/database/database_adapter.d.ts +13 -1
  65. package/dist/database/database_adapter.d.ts.map +1 -1
  66. package/dist/database/database_adapter.js.map +1 -1
  67. package/dist/engine/component_types.d.ts +95 -0
  68. package/dist/engine/component_types.d.ts.map +1 -0
  69. package/dist/engine/component_types.js +93 -0
  70. package/dist/engine/component_types.js.map +1 -0
  71. package/dist/engine/digital_twin_engine.d.ts +121 -6
  72. package/dist/engine/digital_twin_engine.d.ts.map +1 -1
  73. package/dist/engine/digital_twin_engine.js +402 -74
  74. package/dist/engine/digital_twin_engine.js.map +1 -1
  75. package/dist/engine/endpoints.d.ts.map +1 -1
  76. package/dist/engine/endpoints.js +35 -3
  77. package/dist/engine/endpoints.js.map +1 -1
  78. package/dist/engine/error_handler.d.ts +20 -0
  79. package/dist/engine/error_handler.d.ts.map +1 -0
  80. package/dist/engine/error_handler.js +69 -0
  81. package/dist/engine/error_handler.js.map +1 -0
  82. package/dist/engine/events.d.ts +1 -1
  83. package/dist/engine/events.d.ts.map +1 -1
  84. package/dist/engine/events.js.map +1 -1
  85. package/dist/engine/health.d.ts +112 -0
  86. package/dist/engine/health.d.ts.map +1 -0
  87. package/dist/engine/health.js +190 -0
  88. package/dist/engine/health.js.map +1 -0
  89. package/dist/engine/initializer.d.ts.map +1 -1
  90. package/dist/engine/initializer.js +6 -4
  91. package/dist/engine/initializer.js.map +1 -1
  92. package/dist/engine/scheduler.d.ts.map +1 -1
  93. package/dist/engine/scheduler.js +17 -9
  94. package/dist/engine/scheduler.js.map +1 -1
  95. package/dist/engine/upload_processor.d.ts.map +1 -1
  96. package/dist/engine/upload_processor.js +24 -12
  97. package/dist/engine/upload_processor.js.map +1 -1
  98. package/dist/errors/index.d.ts +94 -0
  99. package/dist/errors/index.d.ts.map +1 -0
  100. package/dist/errors/index.js +149 -0
  101. package/dist/errors/index.js.map +1 -0
  102. package/dist/index.d.ts +9 -0
  103. package/dist/index.d.ts.map +1 -1
  104. package/dist/index.js +13 -0
  105. package/dist/index.js.map +1 -1
  106. package/dist/loader/component_loader.d.ts +128 -0
  107. package/dist/loader/component_loader.d.ts.map +1 -0
  108. package/dist/loader/component_loader.js +330 -0
  109. package/dist/loader/component_loader.js.map +1 -0
  110. package/dist/loader/index.d.ts +19 -0
  111. package/dist/loader/index.d.ts.map +1 -0
  112. package/dist/loader/index.js +19 -0
  113. package/dist/loader/index.js.map +1 -0
  114. package/dist/storage/adapters/local_storage_service.d.ts +6 -0
  115. package/dist/storage/adapters/local_storage_service.d.ts.map +1 -1
  116. package/dist/storage/adapters/local_storage_service.js +26 -4
  117. package/dist/storage/adapters/local_storage_service.js.map +1 -1
  118. package/dist/storage/adapters/ovh_storage_service.d.ts.map +1 -1
  119. package/dist/storage/adapters/ovh_storage_service.js +5 -6
  120. package/dist/storage/adapters/ovh_storage_service.js.map +1 -1
  121. package/dist/storage/storage_factory.d.ts.map +1 -1
  122. package/dist/storage/storage_factory.js +4 -1
  123. package/dist/storage/storage_factory.js.map +1 -1
  124. package/dist/storage/storage_service.d.ts.map +1 -1
  125. package/dist/storage/storage_service.js +6 -2
  126. package/dist/storage/storage_service.js.map +1 -1
  127. package/dist/types/http.d.ts +156 -0
  128. package/dist/types/http.d.ts.map +1 -0
  129. package/dist/types/http.js +8 -0
  130. package/dist/types/http.js.map +1 -0
  131. package/dist/utils/graceful_shutdown.d.ts +44 -0
  132. package/dist/utils/graceful_shutdown.d.ts.map +1 -0
  133. package/dist/utils/graceful_shutdown.js +79 -0
  134. package/dist/utils/graceful_shutdown.js.map +1 -0
  135. package/dist/utils/http_responses.d.ts +20 -0
  136. package/dist/utils/http_responses.d.ts.map +1 -1
  137. package/dist/utils/http_responses.js +28 -2
  138. package/dist/utils/http_responses.js.map +1 -1
  139. package/dist/utils/logger.d.ts +8 -8
  140. package/dist/utils/logger.d.ts.map +1 -1
  141. package/dist/utils/logger.js +8 -8
  142. package/dist/utils/logger.js.map +1 -1
  143. package/dist/utils/safe_async.d.ts +50 -0
  144. package/dist/utils/safe_async.d.ts.map +1 -0
  145. package/dist/utils/safe_async.js +90 -0
  146. package/dist/utils/safe_async.js.map +1 -0
  147. package/dist/validation/index.d.ts +3 -0
  148. package/dist/validation/index.d.ts.map +1 -0
  149. package/dist/validation/index.js +7 -0
  150. package/dist/validation/index.js.map +1 -0
  151. package/dist/validation/schemas.d.ts +273 -0
  152. package/dist/validation/schemas.d.ts.map +1 -0
  153. package/dist/validation/schemas.js +82 -0
  154. package/dist/validation/schemas.js.map +1 -0
  155. package/dist/validation/validate.d.ts +49 -0
  156. package/dist/validation/validate.d.ts.map +1 -0
  157. package/dist/validation/validate.js +110 -0
  158. package/dist/validation/validate.js.map +1 -0
  159. package/package.json +14 -8
package/README.md CHANGED
@@ -12,6 +12,7 @@ Digital Twin Core is a minimalist TypeScript framework used to collect and proce
12
12
  - **Storage adapters** – currently local filesystem and OVH Object Storage via S3 API.
13
13
  - **Database adapter** – implemented with [Knex](https://knexjs.org/) to index metadata.
14
14
  - **Engine** – orchestrates components, schedules jobs with BullMQ and exposes endpoints via Express.
15
+ - **Authentication** – pluggable authentication system supporting API gateway headers, JWT tokens, or no-auth mode.
15
16
 
16
17
  ## Installation
17
18
 
@@ -65,6 +66,70 @@ const engine = new DigitalTwinEngine({ storage, database });
65
66
  engine.start();
66
67
  ```
67
68
 
69
+ ## Developer Experience
70
+
71
+ ### Auto-Discovery
72
+
73
+ The framework provides automatic component discovery to reduce boilerplate:
74
+
75
+ ```typescript
76
+ import { DigitalTwinEngine, loadComponents } from 'digitaltwin-core'
77
+
78
+ // Before: manual imports for each component
79
+ // import { WeatherCollector } from './components/weather_collector.js'
80
+ // import { SensorCollector } from './components/sensor_collector.js'
81
+ // ...
82
+
83
+ // After: automatic discovery
84
+ const result = await loadComponents('./dist/components')
85
+
86
+ const engine = new DigitalTwinEngine({ storage, database })
87
+ engine.registerComponents(result)
88
+ await engine.start()
89
+ ```
90
+
91
+ The loader discovers components based on file naming conventions:
92
+ - `*_collector.js` → Collectors
93
+ - `*_harvester.js` → Harvesters
94
+ - `*_handler.js` → Handlers
95
+ - `*_assets_manager.js` → Assets Managers
96
+ - `*_custom_table.js` → Custom Table Managers
97
+
98
+ ### Loading Options
99
+
100
+ ```typescript
101
+ const result = await loadComponents('./dist/components', {
102
+ recursive: true, // Scan subdirectories (default)
103
+ verbose: true, // Log discovered components
104
+ extensions: ['.js'], // File extensions
105
+ exclude: ['*.test.*'] // Exclusion patterns
106
+ })
107
+
108
+ // Result includes metadata
109
+ console.log(result.summary)
110
+ // { total: 5, collectors: 2, harvesters: 1, handlers: 2, errors: 0 }
111
+
112
+ // Check for loading errors
113
+ if (result.errors.length > 0) {
114
+ console.warn('Some components failed to load:', result.errors)
115
+ }
116
+ ```
117
+
118
+ ### Type Guards
119
+
120
+ Runtime type detection utilities:
121
+
122
+ ```typescript
123
+ import { isCollector, isHandler, detectComponentType } from 'digitaltwin-core'
124
+
125
+ if (isCollector(component)) {
126
+ // TypeScript narrowing works here
127
+ await component.collect()
128
+ }
129
+
130
+ const type = detectComponentType(component) // 'collector' | 'handler' | ...
131
+ ```
132
+
68
133
  ## Components
69
134
 
70
135
  ### Collectors
@@ -326,6 +391,153 @@ class WMSLayersManager extends CustomTableManager {
326
391
  - `update(id, data)` - Update existing record
327
392
  - `delete(id)` - Delete record
328
393
 
394
+ ## Request Validation
395
+
396
+ The framework includes VineJS integration for type-safe request validation:
397
+
398
+ ```typescript
399
+ import { validate, AssetUploadSchema } from 'digitaltwin-core'
400
+
401
+ // In your component
402
+ async handleUpload(req: any) {
403
+ const data = await validate(AssetUploadSchema, req.body)
404
+ // data is now typed and validated
405
+ }
406
+ ```
407
+
408
+ Validation errors return HTTP 422 with detailed error messages:
409
+
410
+ ```json
411
+ {
412
+ "error": "Validation failed",
413
+ "details": [
414
+ { "field": "description", "message": "The description field must be a string" }
415
+ ]
416
+ }
417
+ ```
418
+
419
+ ## Error Handling
420
+
421
+ The framework provides custom error classes for structured error handling:
422
+
423
+ ```typescript
424
+ import { CollectorError, ValidationError, StorageError } from 'digitaltwin-core'
425
+
426
+ // Errors include context
427
+ throw new CollectorError('Failed to fetch data', 'weather-collector', originalError)
428
+
429
+ // Validation errors return 422
430
+ throw new ValidationError('Invalid input', details)
431
+ ```
432
+
433
+ All component errors are caught and logged with context (component name, stack trace). Non-critical operations use `safeAsync` to log errors without crashing:
434
+
435
+ ```typescript
436
+ import { safeAsync } from 'digitaltwin-core'
437
+
438
+ // Won't throw, just logs on failure
439
+ await safeAsync(() => cleanup(), 'cleanup temporary files', logger)
440
+ ```
441
+
442
+ ## Production Features
443
+
444
+ ### Graceful Shutdown
445
+
446
+ The engine supports graceful shutdown with configurable timeout:
447
+
448
+ ```typescript
449
+ const engine = new DigitalTwinEngine({ database, storage })
450
+
451
+ // Configure shutdown timeout (default: 30s)
452
+ engine.setShutdownTimeout(60000)
453
+
454
+ // Check if shutting down
455
+ if (engine.isShuttingDown()) {
456
+ // Don't accept new work
457
+ }
458
+
459
+ // Graceful stop
460
+ await engine.stop()
461
+ ```
462
+
463
+ ### Health Checks
464
+
465
+ Register custom health checks for monitoring:
466
+
467
+ ```typescript
468
+ engine.registerHealthCheck('external-api', async () => {
469
+ const response = await fetch('https://api.example.com/health')
470
+ return { status: response.ok ? 'up' : 'down' }
471
+ })
472
+
473
+ // Built-in checks: database, redis (if configured)
474
+ const names = engine.getHealthCheckNames() // ['database', 'redis', 'external-api']
475
+
476
+ // Remove check
477
+ engine.removeHealthCheck('external-api')
478
+ ```
479
+
480
+ ### OpenAPI Specification
481
+
482
+ Generate OpenAPI 3.0 specs from your components:
483
+
484
+ ```typescript
485
+ import { OpenAPIGenerator } from 'digitaltwin-core'
486
+
487
+ const spec = OpenAPIGenerator.generate({
488
+ info: { title: 'My API', version: '1.0.0' },
489
+ components: [collector, assetsManager, handler]
490
+ })
491
+
492
+ // Output as JSON or YAML
493
+ const json = OpenAPIGenerator.toJSON(spec)
494
+ const yaml = OpenAPIGenerator.toYAML(spec)
495
+ ```
496
+
497
+ ## Server Configuration
498
+
499
+ ### HTTP Compression
500
+
501
+ HTTP compression is **disabled by default** because API gateways (Apache APISIX, Kong, Nginx, etc.) typically handle compression at the gateway level.
502
+
503
+ For standalone deployments without a gateway, enable compression via environment variable:
504
+
505
+ ```bash
506
+ export DIGITALTWIN_ENABLE_COMPRESSION=true
507
+ ```
508
+
509
+ When enabled, the server uses gzip compression for JSON responses larger than 1KB, reducing bandwidth by 60-80%.
510
+
511
+ ## Authentication
512
+
513
+ The framework supports multiple authentication modes:
514
+
515
+ - **Gateway** (default): Uses headers from API gateways (Apache APISIX, KrakenD)
516
+ - **JWT**: Direct JWT token validation
517
+ - **None**: Disabled for development/testing
518
+
519
+ ### Gateway Mode (Default)
520
+
521
+ No configuration needed. The framework reads `x-user-id` and `x-user-roles` headers set by your API gateway.
522
+
523
+ ### JWT Mode
524
+
525
+ ```bash
526
+ export AUTH_MODE=jwt
527
+ export JWT_SECRET=your-secret-key
528
+ # Or for RSA: JWT_PUBLIC_KEY or JWT_PUBLIC_KEY_FILE
529
+ ```
530
+
531
+ ### Disable Authentication
532
+
533
+ ```bash
534
+ export DIGITALTWIN_DISABLE_AUTH=true
535
+ # Or
536
+ export AUTH_MODE=none
537
+ ```
538
+
539
+ For detailed configuration options, see [src/auth/README.md](src/auth/README.md).
540
+
329
541
  ## Project Scaffolding
330
542
 
331
543
  Use [create-digitaltwin](https://github.com/CePseudoBE/create-digitaltwin) to quickly bootstrap new projects:
@@ -348,11 +560,16 @@ node dt make:harvester DataProcessor --source weather-collector
348
560
  ## Folder structure
349
561
 
350
562
  - `src/` – framework sources
563
+ - `auth/` – authentication providers and user management
351
564
  - `components/` – base classes for collectors, harvesters, handlers and assets manager
352
- - `engine/` – orchestration logic
565
+ - `engine/` – orchestration logic (includes component type guards)
566
+ - `loader/` – component auto-discovery utilities
353
567
  - `storage/` – storage service abstractions and adapters
354
568
  - `database/` – metadata database adapter
355
569
  - `env/` – environment configuration helper
570
+ - `errors/` – custom error classes
571
+ - `validation/` – request validation with VineJS
572
+ - `utils/` – utility functions (graceful shutdown, HTTP responses, etc.)
356
573
  - `tests/` – unit tests
357
574
 
358
575
  ---
@@ -1,16 +1,23 @@
1
1
  import type { AuthenticatedUser } from './types.js';
2
+ import type { AuthProvider } from './auth_provider.js';
3
+ /**
4
+ * Headers type that accepts both Express IncomingHttpHeaders and Record<string, string>
5
+ */
6
+ export type HeadersLike = Record<string, string | string[] | undefined>;
2
7
  /**
3
8
  * Parses authentication information from Apache APISIX headers set after Keycloak authentication.
4
9
  *
5
- * This class handles the parsing of authentication headers forwarded by Apache APISIX
6
- * after successful Keycloak authentication. APISIX acts as a gateway that:
7
- * 1. Validates JWT tokens with Keycloak
8
- * 2. Extracts user information from the token
9
- * 3. Forwards user data as HTTP headers to downstream services
10
+ * This class provides a static API for backward compatibility while internally using
11
+ * the AuthProvider system. It automatically handles:
12
+ * - Gateway mode (x-user-id, x-user-roles headers)
13
+ * - JWT mode (Authorization: Bearer token)
14
+ * - No-auth mode (DIGITALTWIN_DISABLE_AUTH=true)
10
15
  *
11
- * Authentication can be disabled via environment variables for development/testing:
12
- * - Set DIGITALTWIN_DISABLE_AUTH=true to bypass authentication checks
13
- * - Set DIGITALTWIN_ANONYMOUS_USER_ID=custom-id to use a custom anonymous user ID
16
+ * For new code, consider using AuthProviderFactory directly:
17
+ * ```typescript
18
+ * const authProvider = AuthProviderFactory.fromEnv()
19
+ * const user = authProvider.parseRequest(req)
20
+ * ```
14
21
  *
15
22
  * @example
16
23
  * ```typescript
@@ -24,18 +31,39 @@ import type { AuthenticatedUser } from './types.js';
24
31
  * ```
25
32
  */
26
33
  export declare class ApisixAuthParser {
34
+ private static _provider;
35
+ /**
36
+ * Get the authentication provider instance.
37
+ * Creates it on first use based on environment configuration.
38
+ */
39
+ private static getProvider;
40
+ /**
41
+ * Reset the provider instance (useful for testing).
42
+ * @internal
43
+ */
44
+ static _resetProvider(): void;
45
+ /**
46
+ * Set a custom provider (useful for testing).
47
+ * @internal
48
+ */
49
+ static _setProvider(provider: AuthProvider): void;
50
+ /**
51
+ * Create a request-like object from headers for the AuthProvider.
52
+ * Normalizes headers by taking only the first value for array headers.
53
+ */
54
+ private static toAuthRequest;
27
55
  /**
28
- * Extracts user information from APISIX headers.
56
+ * Extracts user information from authentication headers.
29
57
  *
30
- * Parses the authentication headers forwarded by APISIX:
31
- * - `x-user-id`: Keycloak user UUID (required)
32
- * - `x-user-roles`: Comma-separated list of user roles (optional)
58
+ * Parses the authentication headers (gateway mode) or JWT token (jwt mode):
59
+ * - Gateway: `x-user-id` and `x-user-roles` headers
60
+ * - JWT: `Authorization: Bearer <token>` header
33
61
  *
34
62
  * When authentication is disabled (DIGITALTWIN_DISABLE_AUTH=true),
35
- * returns a default anonymous user instead of requiring headers.
63
+ * returns a default anonymous user.
36
64
  *
37
- * @param headers - HTTP request headers from APISIX
38
- * @returns Parsed user authentication data, or null if x-user-id is missing and auth is enabled
65
+ * @param headers - HTTP request headers
66
+ * @returns Parsed user authentication data, or null if not authenticated
39
67
  *
40
68
  * @example
41
69
  * ```typescript
@@ -46,48 +74,35 @@ export declare class ApisixAuthParser {
46
74
  *
47
75
  * const authUser = ApisixAuthParser.parseAuthHeaders(headers)
48
76
  * // Returns: { id: '6e06a527...', roles: ['default-roles-master', 'offline_access'] }
49
- *
50
- * // With DIGITALTWIN_DISABLE_AUTH=true
51
- * const authUser = ApisixAuthParser.parseAuthHeaders({})
52
- * // Returns: { id: 'anonymous', roles: ['anonymous'] }
53
77
  * ```
54
78
  */
55
- static parseAuthHeaders(headers: Record<string, string>): AuthenticatedUser | null;
79
+ static parseAuthHeaders(headers: HeadersLike): AuthenticatedUser | null;
56
80
  /**
57
- * Checks if a request has valid authentication headers.
81
+ * Checks if a request has valid authentication.
58
82
  *
59
83
  * Performs a quick validation to determine if the request contains
60
- * the minimum required authentication information (x-user-id header).
61
- * Use this for early authentication checks before parsing.
84
+ * valid authentication credentials (gateway headers or JWT token).
62
85
  *
63
- * When authentication is disabled (DIGITALTWIN_DISABLE_AUTH=true),
64
- * this always returns true to allow all requests through.
86
+ * When authentication is disabled, this always returns true.
65
87
  *
66
88
  * @param headers - HTTP request headers
67
- * @returns true if x-user-id header is present or auth is disabled, false otherwise
89
+ * @returns true if authentication is valid or disabled, false otherwise
68
90
  *
69
91
  * @example
70
92
  * ```typescript
71
- * // Early authentication check in handler
72
93
  * if (!ApisixAuthParser.hasValidAuth(req.headers)) {
73
94
  * return { status: 401, content: 'Authentication required' }
74
95
  * }
75
- *
76
- * // Now safe to proceed with parsing
77
- * const authUser = ApisixAuthParser.parseAuthHeaders(req.headers)
78
96
  * ```
79
97
  */
80
- static hasValidAuth(headers: Record<string, string>): boolean;
98
+ static hasValidAuth(headers: HeadersLike): boolean;
81
99
  /**
82
100
  * Extracts just the user ID from headers.
83
101
  *
84
- * Convenience method for cases where you only need the user ID
85
- * without parsing the full authentication context.
86
- *
87
- * When authentication is disabled, returns the configured anonymous user ID.
102
+ * Convenience method for cases where you only need the user ID.
88
103
  *
89
104
  * @param headers - HTTP request headers
90
- * @returns Keycloak user ID, anonymous user ID if auth disabled, or null if not present
105
+ * @returns User ID, or null if not authenticated
91
106
  *
92
107
  * @example
93
108
  * ```typescript
@@ -97,17 +112,12 @@ export declare class ApisixAuthParser {
97
112
  * }
98
113
  * ```
99
114
  */
100
- static getUserId(headers: Record<string, string>): string | null;
115
+ static getUserId(headers: HeadersLike): string | null;
101
116
  /**
102
117
  * Extracts just the user roles from headers.
103
118
  *
104
- * Convenience method for cases where you only need the user roles
105
- * without parsing the full authentication context.
106
- *
107
- * When authentication is disabled, returns the anonymous user roles.
108
- *
109
119
  * @param headers - HTTP request headers
110
- * @returns Array of role names, anonymous roles if auth disabled, empty array if no roles header present
120
+ * @returns Array of role names, empty array if not authenticated
111
121
  *
112
122
  * @example
113
123
  * ```typescript
@@ -117,30 +127,20 @@ export declare class ApisixAuthParser {
117
127
  * }
118
128
  * ```
119
129
  */
120
- static getUserRoles(headers: Record<string, string>): string[];
130
+ static getUserRoles(headers: HeadersLike): string[];
121
131
  /**
122
132
  * Checks if a user has the admin role.
123
133
  *
124
- * Determines if the authenticated user has administrative privileges by checking
125
- * if their roles include the configured admin role name (default: "admin").
126
- *
127
- * The admin role name can be configured via DIGITALTWIN_ADMIN_ROLE_NAME environment variable.
128
- *
129
134
  * @param headers - HTTP request headers
130
135
  * @returns true if user has admin role, false otherwise
131
136
  *
132
137
  * @example
133
138
  * ```typescript
134
139
  * if (ApisixAuthParser.isAdmin(req.headers)) {
135
- * // User has full administrative access
136
- * // Can view all assets including private assets owned by others
137
- * console.log('Admin user detected')
140
+ * // Admin-only logic
138
141
  * }
139
- *
140
- * // With custom admin role name (DIGITALTWIN_ADMIN_ROLE_NAME=administrator)
141
- * const isAdmin = ApisixAuthParser.isAdmin(req.headers)
142
142
  * ```
143
143
  */
144
- static isAdmin(headers: Record<string, string>): boolean;
144
+ static isAdmin(headers: HeadersLike): boolean;
145
145
  }
146
146
  //# sourceMappingURL=apisix_parser.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"apisix_parser.d.ts","sourceRoot":"","sources":["../../src/auth/apisix_parser.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAGnD;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,qBAAa,gBAAgB;IACzB;;;;;;;;;;;;;;;;;;;;;;;;;;;OA2BG;IACH,MAAM,CAAC,gBAAgB,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,iBAAiB,GAAG,IAAI;IAqBlF;;;;;;;;;;;;;;;;;;;;;;;OAuBG;IACH,MAAM,CAAC,YAAY,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,OAAO;IAS7D;;;;;;;;;;;;;;;;;;OAkBG;IACH,MAAM,CAAC,SAAS,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,MAAM,GAAG,IAAI;IAShE;;;;;;;;;;;;;;;;;;OAkBG;IACH,MAAM,CAAC,YAAY,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,MAAM,EAAE;IAU9D;;;;;;;;;;;;;;;;;;;;;;OAsBG;IACH,MAAM,CAAC,OAAO,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,OAAO;CAK3D"}
1
+ {"version":3,"file":"apisix_parser.d.ts","sourceRoot":"","sources":["../../src/auth/apisix_parser.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AACnD,OAAO,KAAK,EAAE,YAAY,EAAe,MAAM,oBAAoB,CAAA;AAGnE;;GAEG;AACH,MAAM,MAAM,WAAW,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,SAAS,CAAC,CAAA;AAEvE;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,qBAAa,gBAAgB;IACzB,OAAO,CAAC,MAAM,CAAC,SAAS,CAA4B;IAEpD;;;OAGG;IACH,OAAO,CAAC,MAAM,CAAC,WAAW;IAO1B;;;OAGG;IACH,MAAM,CAAC,cAAc,IAAI,IAAI;IAI7B;;;OAGG;IACH,MAAM,CAAC,YAAY,CAAC,QAAQ,EAAE,YAAY,GAAG,IAAI;IAIjD;;;OAGG;IACH,OAAO,CAAC,MAAM,CAAC,aAAa;IAY5B;;;;;;;;;;;;;;;;;;;;;;;OAuBG;IACH,MAAM,CAAC,gBAAgB,CAAC,OAAO,EAAE,WAAW,GAAG,iBAAiB,GAAG,IAAI;IAIvE;;;;;;;;;;;;;;;;;OAiBG;IACH,MAAM,CAAC,YAAY,CAAC,OAAO,EAAE,WAAW,GAAG,OAAO;IAIlD;;;;;;;;;;;;;;;OAeG;IACH,MAAM,CAAC,SAAS,CAAC,OAAO,EAAE,WAAW,GAAG,MAAM,GAAG,IAAI;IAIrD;;;;;;;;;;;;;OAaG;IACH,MAAM,CAAC,YAAY,CAAC,OAAO,EAAE,WAAW,GAAG,MAAM,EAAE;IAInD;;;;;;;;;;;;OAYG;IACH,MAAM,CAAC,OAAO,CAAC,OAAO,EAAE,WAAW,GAAG,OAAO;CAGhD"}