@tstdl/base 0.93.2 → 0.93.4

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 (182) hide show
  1. package/ai/ai.service.d.ts +2 -1
  2. package/ai/ai.service.js +11 -3
  3. package/ai/types.d.ts +3 -1
  4. package/api/server/api-request-token.provider.d.ts +5 -3
  5. package/api/server/api-request-token.provider.js +12 -4
  6. package/api/server/gateway.d.ts +1 -9
  7. package/api/server/gateway.js +67 -36
  8. package/api/types.d.ts +5 -1
  9. package/application/application.d.ts +2 -0
  10. package/application/application.js +3 -1
  11. package/application/providers.d.ts +1 -1
  12. package/application/providers.js +1 -1
  13. package/audit/audit.model.d.ts +14 -9
  14. package/audit/audit.model.js +36 -27
  15. package/audit/auditor.d.ts +32 -23
  16. package/audit/auditor.js +84 -21
  17. package/audit/drizzle/{0000_tiny_the_captain.sql → 0000_bored_stick.sql} +5 -4
  18. package/audit/drizzle/meta/0000_snapshot.json +22 -15
  19. package/audit/drizzle/meta/_journal.json +2 -2
  20. package/audit/index.d.ts +3 -1
  21. package/audit/index.js +3 -1
  22. package/audit/module.d.ts +1 -1
  23. package/audit/module.js +1 -2
  24. package/audit/schemas.d.ts +2 -2
  25. package/audit/schemas.js +1 -1
  26. package/audit/types.d.ts +2 -1
  27. package/audit/types.js +2 -1
  28. package/authentication/models/authentication-credentials.model.js +1 -2
  29. package/authentication/models/authentication-session.model.d.ts +2 -2
  30. package/authentication/models/authentication-session.model.js +3 -5
  31. package/authentication/server/authentication-api-request-token.provider.d.ts +2 -2
  32. package/authentication/server/authentication-api-request-token.provider.js +8 -5
  33. package/authentication/server/authentication.api-controller.d.ts +8 -8
  34. package/authentication/server/authentication.api-controller.js +16 -16
  35. package/authentication/server/authentication.audit.d.ts +34 -0
  36. package/authentication/server/authentication.audit.js +1 -0
  37. package/authentication/server/authentication.service.d.ts +19 -10
  38. package/authentication/server/authentication.service.js +158 -43
  39. package/authentication/server/module.d.ts +1 -1
  40. package/authentication/server/schemas.d.ts +2 -3
  41. package/authentication/server/schemas.js +2 -3
  42. package/constants.d.ts +1 -0
  43. package/constants.js +1 -0
  44. package/document-management/api/document-management.api.d.ts +74 -74
  45. package/document-management/models/document-assignment-scope.model.d.ts +1 -2
  46. package/document-management/models/document-assignment-scope.model.js +4 -6
  47. package/document-management/models/document-assignment-task.model.d.ts +1 -2
  48. package/document-management/models/document-assignment-task.model.js +3 -5
  49. package/document-management/models/document-category.model.d.ts +1 -2
  50. package/document-management/models/document-category.model.js +3 -4
  51. package/document-management/models/document-collection-assignment.model.d.ts +1 -2
  52. package/document-management/models/document-collection-assignment.model.js +5 -7
  53. package/document-management/models/document-collection.model.d.ts +1 -2
  54. package/document-management/models/document-collection.model.js +3 -4
  55. package/document-management/models/document-management-table.d.ts +1 -1
  56. package/document-management/models/document-management-table.js +1 -1
  57. package/document-management/models/document-property-value.model.d.ts +1 -2
  58. package/document-management/models/document-property-value.model.js +5 -8
  59. package/document-management/models/document-property.model.d.ts +1 -2
  60. package/document-management/models/document-property.model.js +2 -3
  61. package/document-management/models/document-request-collection-assignment.model.d.ts +1 -2
  62. package/document-management/models/document-request-collection-assignment.model.js +4 -6
  63. package/document-management/models/document-request-template.d.ts +1 -2
  64. package/document-management/models/document-request-template.js +4 -6
  65. package/document-management/models/document-request.model.d.ts +1 -1
  66. package/document-management/models/document-request.model.js +4 -5
  67. package/document-management/models/document-requests-template.d.ts +1 -1
  68. package/document-management/models/document-requests-template.js +2 -3
  69. package/document-management/models/document-tag-assignment.model.d.ts +1 -2
  70. package/document-management/models/document-tag-assignment.model.js +4 -6
  71. package/document-management/models/document-tag.model.d.ts +1 -1
  72. package/document-management/models/document-tag.model.js +2 -3
  73. package/document-management/models/document-type-property.model.d.ts +1 -2
  74. package/document-management/models/document-type-property.model.js +4 -6
  75. package/document-management/models/document-type-validation.model.d.ts +1 -2
  76. package/document-management/models/document-type-validation.model.js +4 -6
  77. package/document-management/models/document-type.model.d.ts +1 -2
  78. package/document-management/models/document-type.model.js +3 -5
  79. package/document-management/models/document-validation-definition.model.d.ts +1 -2
  80. package/document-management/models/document-validation-definition.model.js +3 -4
  81. package/document-management/models/document-validation-execution-related-document.model.d.ts +1 -2
  82. package/document-management/models/document-validation-execution-related-document.model.js +4 -6
  83. package/document-management/models/document-validation-execution.model.d.ts +1 -2
  84. package/document-management/models/document-validation-execution.model.js +8 -9
  85. package/document-management/models/document-workflow.model.d.ts +1 -2
  86. package/document-management/models/document-workflow.model.js +5 -7
  87. package/document-management/models/document.model.d.ts +1 -2
  88. package/document-management/models/document.model.js +5 -7
  89. package/document-management/server/api/document-management.api.js +1 -1
  90. package/document-management/server/module.d.ts +1 -1
  91. package/document-management/server/module.js +1 -1
  92. package/document-management/server/schemas.d.ts +1 -1
  93. package/document-management/server/schemas.js +1 -1
  94. package/document-management/server/services/document-category-type.service.d.ts +2 -2
  95. package/document-management/server/services/document-category-type.service.js +1 -2
  96. package/document-management/server/services/document-collection.service.d.ts +1 -1
  97. package/document-management/server/services/document-collection.service.js +1 -2
  98. package/document-management/server/services/document-management-ai.service.js +3 -3
  99. package/document-management/server/services/document-management.service.js +6 -6
  100. package/document-management/server/services/document-property.service.d.ts +1 -1
  101. package/document-management/server/services/document-property.service.js +1 -2
  102. package/document-management/server/services/document-validation.service.js +2 -2
  103. package/document-management/server/services/document-workflow.service.d.ts +2 -2
  104. package/document-management/server/services/document-workflow.service.js +1 -2
  105. package/document-management/server/services/document.service.d.ts +1 -1
  106. package/document-management/server/services/document.service.js +1 -2
  107. package/document-management/server/services/singleton.js +1 -1
  108. package/document-management/server/validators/ai-validation-executor.d.ts +2 -2
  109. package/document-management/server/validators/ai-validation-executor.js +6 -6
  110. package/document-management/server/validators/single-document-validation-executor.d.ts +2 -1
  111. package/document-management/server/validators/single-document-validation-executor.js +3 -2
  112. package/document-management/service-models/document.service-model.d.ts +62 -62
  113. package/document-management/service-models/document.service-model.js +1 -1
  114. package/document-management/service-models/enriched/enriched-document-management-data.view.js +1 -1
  115. package/document-management/service-models/enriched/enriched-document.view.d.ts +1 -1
  116. package/examples/api/authentication.js +2 -2
  117. package/examples/api/basic-overview.js +2 -2
  118. package/examples/api/custom-authentication.js +2 -2
  119. package/examples/api/streaming.js +2 -2
  120. package/examples/browser/basic.js +2 -2
  121. package/examples/document-management/main.js +4 -2
  122. package/examples/http/client.js +2 -2
  123. package/examples/mail/basic.js +2 -2
  124. package/examples/pdf/basic.js +2 -2
  125. package/examples/template/basic.js +2 -2
  126. package/http/server/http-server-request.d.ts +3 -3
  127. package/key-value-store/postgres/key-value-store.service.js +1 -2
  128. package/key-value-store/postgres/models/key-value.model.d.ts +1 -2
  129. package/key-value-store/postgres/models/key-value.model.js +2 -4
  130. package/key-value-store/postgres/models/schemas.d.ts +1 -1
  131. package/key-value-store/postgres/models/schemas.js +1 -1
  132. package/lock/postgres/lock.js +1 -1
  133. package/lock/postgres/models/lock.model.d.ts +1 -2
  134. package/lock/postgres/models/lock.model.js +3 -5
  135. package/lock/postgres/models/schemas.d.ts +1 -1
  136. package/lock/postgres/models/schemas.js +1 -1
  137. package/lock/postgres/provider.js +1 -2
  138. package/mail/models/mail-log.model.d.ts +1 -1
  139. package/mail/models/mail-log.model.js +4 -5
  140. package/mail/models/schemas.d.ts +1 -1
  141. package/mail/models/schemas.js +1 -1
  142. package/openid-connect/oidc-state.model.d.ts +1 -1
  143. package/openid-connect/oidc-state.model.js +2 -3
  144. package/openid-connect/oidc.service.js +1 -1
  145. package/orm/data-types/bytea.js +1 -1
  146. package/orm/data-types/numeric-date.js +1 -1
  147. package/orm/decorators.d.ts +65 -72
  148. package/orm/decorators.js +42 -40
  149. package/orm/entity.d.ts +1 -1
  150. package/orm/entity.js +13 -13
  151. package/orm/index.d.ts +2 -1
  152. package/orm/index.js +2 -1
  153. package/orm/schemas/json.d.ts +1 -1
  154. package/orm/schemas/json.js +1 -1
  155. package/orm/schemas/numeric-date.d.ts +1 -1
  156. package/orm/schemas/numeric-date.js +1 -1
  157. package/orm/schemas/timestamp.d.ts +1 -1
  158. package/orm/schemas/timestamp.js +1 -1
  159. package/orm/schemas/uuid.d.ts +2 -2
  160. package/orm/schemas/uuid.js +1 -1
  161. package/orm/server/drizzle/index.js +1 -0
  162. package/orm/server/drizzle/schema-converter.d.ts +15 -0
  163. package/orm/server/drizzle/schema-converter.js +300 -0
  164. package/orm/server/repository.d.ts +1 -1
  165. package/orm/server/repository.js +12 -9
  166. package/orm/sqls.d.ts +1 -1
  167. package/orm/sqls.js +1 -1
  168. package/orm/types.d.ts +2 -6
  169. package/orm/types.js +1 -4
  170. package/package.json +12 -10
  171. package/queue/postgres/job.model.d.ts +3 -3
  172. package/queue/postgres/job.model.js +5 -6
  173. package/queue/postgres/queue.js +2 -2
  174. package/queue/postgres/schemas.d.ts +1 -1
  175. package/queue/postgres/schemas.js +1 -1
  176. package/supports.d.ts +1 -0
  177. package/supports.js +2 -1
  178. package/types/types.d.ts +12 -1
  179. package/utils/object/object.d.ts +3 -1
  180. package/utils/object/object.js +7 -1
  181. package/orm/server/drizzle/schema-converter.ts +0 -408
  182. /package/orm/server/drizzle/{index.ts → index.d.ts} +0 -0
@@ -36,7 +36,7 @@ export declare class AiServiceOptions {
36
36
  };
37
37
  /**
38
38
  * The default model to use for generation requests.
39
- * @default 'gemini-2.5-flash-lite'
39
+ * @default 'small'
40
40
  */
41
41
  defaultModel?: AiModel;
42
42
  }
@@ -133,6 +133,7 @@ export declare class AiService implements Resolvable<AiServiceArgument> {
133
133
  private convertContent;
134
134
  private convertFunctions;
135
135
  private convertGoogleContent;
136
+ private mapModel;
136
137
  }
137
138
  /**
138
139
  * Merges an array of streaming generation results into a single, consolidated result.
package/ai/ai.service.js CHANGED
@@ -7,6 +7,7 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
7
7
  var _a;
8
8
  var AiService_1;
9
9
  import { FinishReason, FunctionCallingConfigMode as GoogleFunctionCallingMode, GoogleGenAI } from '@google/genai';
10
+ import { match } from 'ts-pattern';
10
11
  import { CancellationSignal } from '../cancellation/index.js';
11
12
  import { NotSupportedError } from '../errors/not-supported.error.js';
12
13
  import { Singleton } from '../injector/decorators.js';
@@ -39,7 +40,7 @@ export class AiServiceOptions {
39
40
  vertex;
40
41
  /**
41
42
  * The default model to use for generation requests.
42
- * @default 'gemini-2.5-flash-lite'
43
+ * @default 'small'
43
44
  */
44
45
  defaultModel;
45
46
  }
@@ -80,7 +81,7 @@ let AiService = AiService_1 = class AiService {
80
81
  /**
81
82
  * The default AI model to use for requests if not specified otherwise.
82
83
  */
83
- defaultModel = this.#options.defaultModel ?? 'gemini-2.5-flash-lite';
84
+ defaultModel = this.#options.defaultModel ?? 'gemini-flash-lite-latest';
84
85
  /**
85
86
  * Creates a new {@link AiSession} for managing conversational history.
86
87
  */
@@ -202,7 +203,7 @@ let AiService = AiService_1 = class AiService {
202
203
  thinkingBudget: request.generationOptions?.thinkingBudget,
203
204
  },
204
205
  };
205
- const model = request.model ?? this.defaultModel;
206
+ const model = this.mapModel(request.model);
206
207
  const maxModelTokens = await this.getModelOutputTokenLimit(model);
207
208
  const maxTotalOutputTokens = request.generationOptions?.maxOutputTokens ?? maxModelTokens;
208
209
  const inputContent = this.convertContents(request.contents);
@@ -427,6 +428,13 @@ let AiService = AiService_1 = class AiService {
427
428
  .filter(isNotNull),
428
429
  };
429
430
  }
431
+ mapModel(model) {
432
+ return match(model)
433
+ .with('small', () => 'gemini-flash-lite-latest')
434
+ .with('medium', () => 'gemini-flash-latest')
435
+ .with('large', () => 'gemini-2.5-pro')
436
+ .otherwise(() => model ?? this.defaultModel);
437
+ }
430
438
  };
431
439
  AiService = AiService_1 = __decorate([
432
440
  Singleton()
package/ai/types.d.ts CHANGED
@@ -113,8 +113,10 @@ export type FunctionCallingMode = 'auto' | 'force' | 'none';
113
113
  export type FinishReason = 'stop' | 'maxTokens' | 'unknown';
114
114
  /**
115
115
  * The specific AI model to use for a request.
116
+ *
117
+ * Aliases `small`, `medium`, and `large` map to models that change over time.
116
118
  */
117
- export type AiModel = LiteralUnion<'gemini-2.5-pro' | 'gemini-2.5-flash' | 'gemini-2.5-flash-lite', string>;
119
+ export type AiModel = LiteralUnion<'small' | 'medium' | 'large' | 'gemini-flash-latest' | 'gemini-flash-lite-latest' | 'gemini-2.5-pro' | 'gemini-2.5-flash' | 'gemini-2.5-flash-lite', string>;
118
120
  /**
119
121
  * Options to control the generation process.
120
122
  */
@@ -1,10 +1,12 @@
1
+ import type { Token } from '../../authentication/index.js';
1
2
  import type { ApiRequestData } from '../types.js';
2
3
  export declare abstract class ApiRequestTokenProvider {
3
- abstract getToken<T>(requestData: ApiRequestData): T | Promise<T>;
4
+ getToken<T extends Token>(data: ApiRequestData): Promise<T>;
5
+ abstract tryGetToken<T extends Token>(requestData: ApiRequestData): T | null | Promise<T | null>;
4
6
  }
5
7
  export declare class NoopApiRequestTokenProvider extends ApiRequestTokenProvider {
6
- getToken<T>(): T;
8
+ tryGetToken<T extends Token>(_requestData: ApiRequestData): T | null | Promise<T | null>;
7
9
  }
8
10
  export declare class MockApiRequestTokenProvider extends ApiRequestTokenProvider {
9
- getToken<T>(): T;
11
+ tryGetToken<T extends Token>(_requestData: ApiRequestData): T | null | Promise<T | null>;
10
12
  }
@@ -5,11 +5,19 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
5
5
  return c > 3 && r && Object.defineProperty(target, key, r), r;
6
6
  };
7
7
  import { Singleton } from '../../injector/decorators.js';
8
+ import { isNull } from '../../utils/type-guards.js';
8
9
  export class ApiRequestTokenProvider {
10
+ async getToken(data) {
11
+ const token = await this.tryGetToken(data);
12
+ if (isNull(token)) {
13
+ throw new Error('No token available for request');
14
+ }
15
+ return token;
16
+ }
9
17
  }
10
18
  let NoopApiRequestTokenProvider = class NoopApiRequestTokenProvider extends ApiRequestTokenProvider {
11
- getToken() {
12
- throw new Error('No RequestTokenProvider registered');
19
+ tryGetToken(_requestData) {
20
+ throw new Error('No RequestInfoProvider registered');
13
21
  }
14
22
  };
15
23
  NoopApiRequestTokenProvider = __decorate([
@@ -17,11 +25,11 @@ NoopApiRequestTokenProvider = __decorate([
17
25
  ], NoopApiRequestTokenProvider);
18
26
  export { NoopApiRequestTokenProvider };
19
27
  let MockApiRequestTokenProvider = class MockApiRequestTokenProvider extends ApiRequestTokenProvider {
20
- getToken() {
28
+ tryGetToken(_requestData) {
21
29
  return {};
22
30
  }
23
31
  };
24
32
  MockApiRequestTokenProvider = __decorate([
25
- Singleton({ alias: ApiRequestTokenProvider })
33
+ Singleton()
26
34
  ], MockApiRequestTokenProvider);
27
35
  export { MockApiRequestTokenProvider };
@@ -50,15 +50,7 @@ export type ApiMetadata = {
50
50
  patternResult: URLPatternResult;
51
51
  };
52
52
  export declare class ApiGateway implements Resolvable<ApiGatewayOptions> {
53
- private readonly requestTokenProvider;
54
- private readonly logger;
55
- private readonly prefix;
56
- private readonly apis;
57
- private readonly middlewares;
58
- private readonly supressedErrors;
59
- private readonly catchErrorMiddleware;
60
- private readonly options;
61
- private composedMiddleware;
53
+ #private;
62
54
  readonly [resolveArgumentType]: ApiGatewayOptions;
63
55
  constructor();
64
56
  addMiddleware(middleware: ApiGatewayMiddleware): void;
@@ -7,13 +7,18 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
7
7
  var __metadata = (this && this.__metadata) || function (k, v) {
8
8
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
9
  };
10
+ var _a;
10
11
  var ApiGateway_1;
12
+ import { match } from 'ts-pattern';
11
13
  import 'urlpattern-polyfill';
14
+ import { Auditor } from '../../audit/auditor.js';
15
+ import { ActorType } from '../../audit/types.js';
16
+ import { NIL_UUID } from '../../constants.js';
12
17
  import { BadRequestError } from '../../errors/bad-request.error.js';
13
18
  import { NotFoundError } from '../../errors/not-found.error.js';
14
19
  import { NotImplementedError } from '../../errors/not-implemented.error.js';
15
20
  import { HttpServerResponse } from '../../http/server/index.js';
16
- import { Singleton, inject, injectArgument, resolveArgumentType } from '../../injector/index.js';
21
+ import { inject, injectArgument, resolveArgumentType, Singleton } from '../../injector/index.js';
17
22
  import { Logger } from '../../logger/index.js';
18
23
  import { Schema } from '../../schema/index.js';
19
24
  import { DataStreamSource } from '../../sse/data-stream-source.js';
@@ -23,7 +28,7 @@ import { toArray } from '../../utils/array/array.js';
23
28
  import { composeAsyncMiddleware } from '../../utils/middleware.js';
24
29
  import { mapObjectValues } from '../../utils/object/object.js';
25
30
  import { deferThrow } from '../../utils/throw.js';
26
- import { isArray, isBlob, isDefined, isNull, isNullOrUndefined, isObject, isReadableStream, isUint8Array, isUndefined } from '../../utils/type-guards.js';
31
+ import { isArray, isBlob, isDefined, isNotNull, isNotNullOrUndefined, isNull, isNullOrUndefined, isObject, isReadableStream, isUint8Array, isUndefined } from '../../utils/type-guards.js';
27
32
  import { mebibyte } from '../../utils/units.js';
28
33
  import { normalizedApiDefinitionEndpointsEntries } from '../types.js';
29
34
  import { getFullApiEndpointResource } from '../utils.js';
@@ -51,42 +56,43 @@ export class ApiGatewayOptions {
51
56
  defaultMaxBytes;
52
57
  }
53
58
  let ApiGateway = ApiGateway_1 = class ApiGateway {
54
- requestTokenProvider = inject(ApiRequestTokenProvider);
55
- logger = inject(Logger, ApiGateway_1.name);
56
- prefix;
57
- apis;
58
- middlewares;
59
- supressedErrors;
60
- catchErrorMiddleware;
61
- options = injectArgument(this, { optional: true }) ?? {};
62
- composedMiddleware;
59
+ #requestTokenProvider = inject(ApiRequestTokenProvider);
60
+ #auditor = inject(Auditor, 'Api');
61
+ #logger = inject(Logger, ApiGateway_1.name);
62
+ #prefix;
63
+ #apis;
64
+ #middlewares;
65
+ #supressedErrors;
66
+ #catchErrorMiddleware;
67
+ #options = injectArgument(this, { optional: true }) ?? {};
68
+ #composedMiddleware;
63
69
  constructor() {
64
- this.prefix = isNull(this.options.prefix) ? null : (this.options.prefix ?? 'api');
65
- this.apis = new Map();
66
- this.middlewares = this.options.middlewares ?? [];
67
- this.supressedErrors = new Set(this.options.supressedErrors);
68
- this.catchErrorMiddleware = getCatchErrorMiddleware(this.supressedErrors, this.logger);
70
+ this.#prefix = isNull(this.#options.prefix) ? null : (this.#options.prefix ?? 'api');
71
+ this.#apis = new Map();
72
+ this.#middlewares = this.#options.middlewares ?? [];
73
+ this.#supressedErrors = new Set(this.#options.supressedErrors);
74
+ this.#catchErrorMiddleware = getCatchErrorMiddleware(this.#supressedErrors, this.#logger);
69
75
  this.updateMiddleware();
70
76
  }
71
77
  addMiddleware(middleware) {
72
- this.middlewares.push(middleware);
78
+ this.#middlewares.push(middleware);
73
79
  this.updateMiddleware();
74
80
  }
75
81
  supressErrors(...errorTypes) {
76
82
  for (const type of errorTypes) {
77
- this.supressedErrors.add(type);
83
+ this.#supressedErrors.add(type);
78
84
  }
79
85
  }
80
86
  registerApi(definition, implementation) {
81
87
  for (const [name, endpointDefinition] of normalizedApiDefinitionEndpointsEntries(definition.endpoints)) {
82
88
  const versionArray = isUndefined(endpointDefinition.version) ? [1] : toArray(endpointDefinition.version);
83
89
  for (const version of versionArray) {
84
- const resource = getFullApiEndpointResource({ api: definition, endpoint: endpointDefinition, defaultPrefix: this.prefix, explicitVersion: version });
90
+ const resource = getFullApiEndpointResource({ api: definition, endpoint: endpointDefinition, defaultPrefix: this.#prefix, explicitVersion: version });
85
91
  const methods = isArray(endpointDefinition.method) ? endpointDefinition.method : [endpointDefinition.method ?? 'GET'];
86
92
  if (methods.length == 0) {
87
93
  throw new Error(`No method provided for resource ${resource}.`);
88
94
  }
89
- let resourceApis = this.apis.get(resource);
95
+ let resourceApis = this.#apis.get(resource);
90
96
  if (isUndefined(resourceApis)) {
91
97
  resourceApis = {
92
98
  resource,
@@ -103,7 +109,7 @@ let ApiGateway = ApiGateway_1 = class ApiGateway {
103
109
  }),
104
110
  endpoints: new Map(),
105
111
  };
106
- this.apis.set(resource, resourceApis);
112
+ this.#apis.set(resource, resourceApis);
107
113
  }
108
114
  const endpointImplementation = implementation[name]?.bind(implementation) ?? deferThrow(() => new NotImplementedError(`Endpoint ${name} for resource ${resource} not implemented.`));
109
115
  for (const method of methods) {
@@ -119,13 +125,13 @@ let ApiGateway = ApiGateway_1 = class ApiGateway {
119
125
  const { api, patternResult } = this.getApiMetadata(request.url);
120
126
  const endpoint = api.endpoints.get(request.method);
121
127
  const context = { api, resourcePatternResult: patternResult, endpoint, request, response };
122
- await this.composedMiddleware(context);
128
+ await this.#composedMiddleware(context);
123
129
  responded = true;
124
130
  await respond(context.response);
125
131
  }
126
132
  catch (error) {
127
133
  try {
128
- handleApiError(error, response, this.supressedErrors, this.logger);
134
+ handleApiError(error, response, this.#supressedErrors, this.#logger);
129
135
  if (responded) {
130
136
  await close();
131
137
  }
@@ -134,7 +140,7 @@ let ApiGateway = ApiGateway_1 = class ApiGateway {
134
140
  await respond(response);
135
141
  }
136
142
  catch (errorRespondError) {
137
- this.logger.error(errorRespondError);
143
+ this.#logger.error(errorRespondError);
138
144
  await close();
139
145
  }
140
146
  }
@@ -145,7 +151,7 @@ let ApiGateway = ApiGateway_1 = class ApiGateway {
145
151
  getApiMetadata(resource) {
146
152
  const urlWithoutPort = new URL(resource);
147
153
  urlWithoutPort.port = '';
148
- for (const api of this.apis.values()) {
154
+ for (const api of this.#apis.values()) {
149
155
  const result = api.pattern.exec(urlWithoutPort);
150
156
  if (isNullOrUndefined(result)) {
151
157
  continue;
@@ -155,11 +161,11 @@ let ApiGateway = ApiGateway_1 = class ApiGateway {
155
161
  throw new NotFoundError(`Resource ${resource.pathname} not available.`);
156
162
  }
157
163
  updateMiddleware() {
158
- const middlewares = [responseTimeMiddleware, contentTypeMiddleware, this.catchErrorMiddleware, corsMiddleware(this.options.cors), allowedMethodsMiddleware, ...this.middlewares, async (context, next) => this.endpointMiddleware(context, next)];
159
- this.composedMiddleware = composeAsyncMiddleware(middlewares);
164
+ const middlewares = [responseTimeMiddleware, contentTypeMiddleware, this.#catchErrorMiddleware, corsMiddleware(this.#options.cors), allowedMethodsMiddleware, ...this.#middlewares, async (context, next) => await this.endpointMiddleware(context, next)];
165
+ this.#composedMiddleware = composeAsyncMiddleware(middlewares);
160
166
  }
161
167
  async endpointMiddleware(context, next) {
162
- const readBodyOptions = { maxBytes: context.endpoint.definition.maxBytes ?? this.options.defaultMaxBytes ?? defaultMaxBytes };
168
+ const readBodyOptions = { maxBytes: context.endpoint.definition.maxBytes ?? this.#options.defaultMaxBytes ?? defaultMaxBytes };
163
169
  const body = isDefined(context.endpoint.definition.body)
164
170
  ? await getBody(context.request, readBodyOptions, context.endpoint.definition.body)
165
171
  : undefined;
@@ -174,24 +180,49 @@ let ApiGateway = ApiGateway_1 = class ApiGateway {
174
180
  const validatedParameters = isDefined(context.endpoint.definition.parameters)
175
181
  ? Schema.parse(context.endpoint.definition.parameters, parameters)
176
182
  : parameters;
183
+ const requestTokenProvider = this.#requestTokenProvider;
184
+ const auditor = this.#auditor;
177
185
  const requestContext = {
178
186
  parameters: validatedParameters,
179
187
  body,
180
188
  request: context.request,
181
- getToken: async () => this.requestTokenProvider.getToken(requestContext),
189
+ async tryGetToken() {
190
+ return await requestTokenProvider.tryGetToken(requestContext);
191
+ },
192
+ async getToken() {
193
+ return await requestTokenProvider.getToken(requestContext);
194
+ },
195
+ async getAuditor() {
196
+ const token = await this.tryGetToken();
197
+ return auditor.fork(context.api.resource)
198
+ .withCorrelation()
199
+ .with({
200
+ actorType: isNotNull(token) ? ActorType.User : ActorType.Anonymous,
201
+ actor: token?.payload.subject ?? NIL_UUID,
202
+ impersonatorType: isNotNullOrUndefined(token?.payload.impersonator) ? ActorType.User : null,
203
+ impersonator: token?.payload.impersonator ?? null,
204
+ network: {
205
+ path: context.request.url.pathname,
206
+ ipAddress: context.request.ip,
207
+ userAgent: context.request.headers.tryGetSingle('User-Agent') ?? null,
208
+ sessionId: token?.payload.sessionId ?? null,
209
+ },
210
+ });
211
+ },
182
212
  };
183
213
  const result = await context.endpoint.implementation(requestContext);
184
214
  if (result instanceof HttpServerResponse) {
185
215
  context.response.update(result);
186
216
  }
187
217
  else {
188
- context.response.body = isUint8Array(result) ? { buffer: result }
189
- : isBlob(result) ? { stream: result.stream() }
190
- : isReadableStream(result) ? { stream: result }
191
- : (result instanceof ServerSentEventsSource) ? { events: result }
192
- : (context.endpoint.definition.result == DataStream) ? { events: DataStreamSource.fromIterable(result).eventSource }
193
- : (context.endpoint.definition.result == String) ? { text: result }
194
- : { json: result };
218
+ context.response.body = match(result)
219
+ .when(isUint8Array, (buffer) => ({ buffer }))
220
+ .when(isBlob, (value) => ({ stream: value.stream() }))
221
+ .when((isReadableStream), (stream) => ({ stream }))
222
+ .when((value) => value instanceof ServerSentEventsSource, (events) => ({ events }))
223
+ .when(() => (context.endpoint.definition.result == DataStream), (value) => ({ events: DataStreamSource.fromIterable(value).eventSource }))
224
+ .when(() => (context.endpoint.definition.result == String), (text) => ({ text: text }))
225
+ .otherwise((json) => ({ json }));
195
226
  }
196
227
  await next();
197
228
  }
package/api/types.d.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  import type { Observable } from 'rxjs';
2
+ import type { Auditor } from '../audit/index.js';
3
+ import type { Token } from '../authentication/index.js';
2
4
  import type { HttpServerRequest, HttpServerResponse } from '../http/server/index.js';
3
5
  import type { HttpMethod } from '../http/types.js';
4
6
  import type { SchemaOutput, SchemaTestable } from '../schema/index.js';
@@ -106,7 +108,9 @@ export type ApiRequestData<T extends ApiDefinition = ApiDefinition, K extends Ap
106
108
  request: HttpServerRequest;
107
109
  };
108
110
  export type ApiRequestContext<T extends ApiDefinition = ApiDefinition, K extends ApiEndpointKeys<T> = ApiEndpointKeys<T>> = ApiRequestData<T, K> & {
109
- getToken<Token>(): Promise<Token>;
111
+ tryGetToken<T extends Token>(): Promise<T | null>;
112
+ getToken<T extends Token>(): Promise<T>;
113
+ getAuditor(): Promise<Auditor>;
110
114
  };
111
115
  export type ApiEndpointServerImplementation<T extends ApiDefinition = ApiDefinition, K extends ApiEndpointKeys<T> = ApiEndpointKeys<T>> = (context: ApiRequestContext<T, K>) => ApiServerResult<T, K> | Promise<ApiServerResult<T, K>>;
112
116
  export type ApiEndpointClientImplementation<T extends ApiDefinition = ApiDefinition, K extends ApiEndpointKeys<T> = ApiEndpointKeys<T>> = ApiClientBody<T, K> extends never ? ApiParameters<T, K> extends never ? () => Promise<ApiClientResult<T, K>> : (parameters: ApiParameters<T, K>) => Promise<ApiClientResult<T, K>> : (parameters: ApiParameters<T, K> extends never ? undefined | Record<never, never> : ApiParameters<T, K>, body: ApiClientBody<T, K>) => Promise<ApiClientResult<T, K>>;
@@ -7,6 +7,8 @@ export type DestructorFn = () => void | Promise<void>;
7
7
  export declare const APPLICATION_MODULE: import("../injector/token.js").InjectionToken<Module, never>;
8
8
  export declare const APPLICATION_INITIALIZER: import("../injector/token.js").InjectionToken<InitializerFn, never>;
9
9
  export declare const APPLICATION_DESTRUCTOR: import("../injector/token.js").InjectionToken<DestructorFn, never>;
10
+ export declare const APPLICATION_INSTANCE_ID: import("../injector/token.js").InjectionToken<string, never>;
11
+ export declare const APPLICATION_NAME: import("../injector/token.js").InjectionToken<string, never>;
10
12
  export declare class Application {
11
13
  #private;
12
14
  static create(name: string, providers?: OneOrMany<ProvidersItem>[]): Application;
@@ -19,7 +19,8 @@ import { isDefined, isFunction } from '../utils/type-guards.js';
19
19
  export const APPLICATION_MODULE = injectionToken('ApplicationModule');
20
20
  export const APPLICATION_INITIALIZER = injectionToken('ApplicationInitializer');
21
21
  export const APPLICATION_DESTRUCTOR = injectionToken('ApplicationDestructor');
22
- const APPLICATION_NAME = injectionToken('ApplicationName');
22
+ export const APPLICATION_INSTANCE_ID = injectionToken('ApplicationInstanceId');
23
+ export const APPLICATION_NAME = injectionToken('ApplicationName');
23
24
  let Application = Application_1 = class Application {
24
25
  #name = inject(APPLICATION_NAME);
25
26
  #injector = inject(Injector);
@@ -29,6 +30,7 @@ let Application = Application_1 = class Application {
29
30
  #shutdownToken = inject(CancellationSignal, undefined, { optional: true })?.createChild() ?? new CancellationToken();
30
31
  static create(name, providers = []) {
31
32
  const injector = new Injector(`${name}Injector`);
33
+ injector.register(APPLICATION_INSTANCE_ID, { useValue: crypto.randomUUID() });
32
34
  injector.register(APPLICATION_NAME, { useValue: name });
33
35
  for (const providersItem of providers.flat()) {
34
36
  injector.register(providersItem.provide, providersItem, { multi: providersItem.multi });
@@ -3,7 +3,7 @@ import { type ProvidersItem } from '../injector/index.js';
3
3
  import { Module, type FunctionModuleFunction } from '../module/index.js';
4
4
  import type { OneOrMany, Type } from '../types/index.js';
5
5
  import { type DestructorFn, type InitializerFn } from './application.js';
6
- export declare function provideModules(...functionsAndModules: OneOrMany<FunctionModuleFunction | Type<Module>>[]): ProvidersItem[];
6
+ export declare function provideModule(...functionsAndModules: OneOrMany<FunctionModuleFunction | Type<Module>>[]): ProvidersItem[];
7
7
  export declare function provideInitializer(fn: InitializerFn): ProvidersItem;
8
8
  export declare function provideDestructor(fn: DestructorFn): ProvidersItem;
9
9
  export declare function provideShutdownSignal(signal: CancellationSignal): ProvidersItem;
@@ -7,7 +7,7 @@ import { typeExtends } from '../utils/type/index.js';
7
7
  import { Application, APPLICATION_DESTRUCTOR, APPLICATION_INITIALIZER, APPLICATION_MODULE } from './application.js';
8
8
  const quitSignals = ['SIGTERM', 'SIGINT', 'SIGHUP', 'SIGBREAK'];
9
9
  const quitEvents = ['uncaughtException', 'unhandledRejection', 'rejectionHandled'];
10
- export function provideModules(...functionsAndModules) {
10
+ export function provideModule(...functionsAndModules) {
11
11
  return functionsAndModules.flat().map((fnOrModule) => {
12
12
  if (typeExtends(fnOrModule, Module)) {
13
13
  return { provide: APPLICATION_MODULE, useValue: fnOrModule, multi: true };
@@ -1,10 +1,15 @@
1
- import { Embedded, EntityWithoutMetadata, Json, Timestamp, Uuid } from '../orm/index.js';
1
+ import { type Embedded, EntityWithoutMetadata, type Json, type Timestamp, type Uuid } from '../orm/index.js';
2
2
  import type { UndefinableJsonObject } from '../types/index.js';
3
3
  import { ActorType, AuditOutcome, AuditSeverity } from './types.js';
4
4
  /**
5
5
  * Represents the network details of the request that triggered the audit event.
6
6
  */
7
7
  export declare class RequestDetails {
8
+ /**
9
+ * The path of the request.
10
+ * @example '/api/v1/users/xyz'
11
+ */
12
+ path: string | null;
8
13
  /**
9
14
  * The IP address of the client.
10
15
  * @example '192.168.1.100'
@@ -70,31 +75,31 @@ export declare class AuditEvent<Details extends UndefinableJsonObject = Undefina
70
75
  * The severity level of the event.
71
76
  */
72
77
  severity: AuditSeverity;
73
- /**
74
- * The unique identifier of the actor who performed the action.
75
- */
76
- actorId: Uuid;
77
78
  /**
78
79
  * The type of the actor.
79
80
  */
80
81
  actorType: ActorType;
81
82
  /**
82
- * The unique identifier of the user who is impersonating the actor, if applicable.
83
+ * The (if possible unique) identifier of the actor who performed the action.
83
84
  */
84
- impersonatorId: Uuid | null;
85
+ actor: string | null;
85
86
  /**
86
87
  * The type of the impersonator, if applicable.
87
88
  */
88
89
  impersonatorType: ActorType | null;
89
90
  /**
90
- * The unique identifier of the primary resource or entity that was the target of the action.
91
+ * The (if possible unique) identifier of the user who is impersonating the actor, if applicable.
91
92
  */
92
- targetId: Uuid;
93
+ impersonator: string | null;
93
94
  /**
94
95
  * The type of the target entity.
95
96
  * @example 'User'
96
97
  */
97
98
  targetType: string;
99
+ /**
100
+ * The unique identifier of the primary resource or entity that was the target of the action.
101
+ */
102
+ targetId: Uuid;
98
103
  /**
99
104
  * Network-related details for the request that triggered the event.
100
105
  */
@@ -7,13 +7,18 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
7
7
  var __metadata = (this && this.__metadata) || function (k, v) {
8
8
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
9
  };
10
- import { Embedded, EntityWithoutMetadata, Json, Table, Timestamp, Uuid } from '../orm/index.js';
10
+ import { EmbeddedProperty, EntityWithoutMetadata, JsonProperty, Table, TimestampProperty, UuidProperty } from '../orm/index.js';
11
11
  import { Enumeration, StringProperty } from '../schema/index.js';
12
12
  import { ActorType, AuditOutcome, AuditSeverity } from './types.js';
13
13
  /**
14
14
  * Represents the network details of the request that triggered the audit event.
15
15
  */
16
16
  export class RequestDetails {
17
+ /**
18
+ * The path of the request.
19
+ * @example '/api/v1/users/xyz'
20
+ */
21
+ path;
17
22
  /**
18
23
  * The IP address of the client.
19
24
  * @example '192.168.1.100'
@@ -29,6 +34,10 @@ export class RequestDetails {
29
34
  */
30
35
  sessionId;
31
36
  }
37
+ __decorate([
38
+ StringProperty(),
39
+ __metadata("design:type", Object)
40
+ ], RequestDetails.prototype, "path", void 0);
32
41
  __decorate([
33
42
  StringProperty({ nullable: true }),
34
43
  __metadata("design:type", Object)
@@ -38,7 +47,7 @@ __decorate([
38
47
  __metadata("design:type", Object)
39
48
  ], RequestDetails.prototype, "userAgent", void 0);
40
49
  __decorate([
41
- Uuid({ nullable: true }),
50
+ UuidProperty({ nullable: true }),
42
51
  __metadata("design:type", Object)
43
52
  ], RequestDetails.prototype, "sessionId", void 0);
44
53
  /**
@@ -55,11 +64,11 @@ export class ChangeDetails {
55
64
  after;
56
65
  }
57
66
  __decorate([
58
- Json({ nullable: true }),
67
+ JsonProperty({ nullable: true }),
59
68
  __metadata("design:type", Object)
60
69
  ], ChangeDetails.prototype, "before", void 0);
61
70
  __decorate([
62
- Json({ nullable: true }),
71
+ JsonProperty({ nullable: true }),
63
72
  __metadata("design:type", Object)
64
73
  ], ChangeDetails.prototype, "after", void 0);
65
74
  /**
@@ -99,31 +108,31 @@ let AuditEvent = class AuditEvent extends EntityWithoutMetadata {
99
108
  * The severity level of the event.
100
109
  */
101
110
  severity;
102
- /**
103
- * The unique identifier of the actor who performed the action.
104
- */
105
- actorId;
106
111
  /**
107
112
  * The type of the actor.
108
113
  */
109
114
  actorType;
110
115
  /**
111
- * The unique identifier of the user who is impersonating the actor, if applicable.
116
+ * The (if possible unique) identifier of the actor who performed the action.
112
117
  */
113
- impersonatorId;
118
+ actor;
114
119
  /**
115
120
  * The type of the impersonator, if applicable.
116
121
  */
117
122
  impersonatorType;
118
123
  /**
119
- * The unique identifier of the primary resource or entity that was the target of the action.
124
+ * The (if possible unique) identifier of the user who is impersonating the actor, if applicable.
120
125
  */
121
- targetId;
126
+ impersonator;
122
127
  /**
123
128
  * The type of the target entity.
124
129
  * @example 'User'
125
130
  */
126
131
  targetType;
132
+ /**
133
+ * The unique identifier of the primary resource or entity that was the target of the action.
134
+ */
135
+ targetId;
127
136
  /**
128
137
  * Network-related details for the request that triggered the event.
129
138
  */
@@ -138,15 +147,15 @@ let AuditEvent = class AuditEvent extends EntityWithoutMetadata {
138
147
  details;
139
148
  };
140
149
  __decorate([
141
- Timestamp(),
150
+ TimestampProperty(),
142
151
  __metadata("design:type", Number)
143
152
  ], AuditEvent.prototype, "timestamp", void 0);
144
153
  __decorate([
145
- Uuid({ nullable: true }),
154
+ UuidProperty({ nullable: true }),
146
155
  __metadata("design:type", Object)
147
156
  ], AuditEvent.prototype, "tenantId", void 0);
148
157
  __decorate([
149
- Uuid({ nullable: true }),
158
+ UuidProperty({ nullable: true }),
150
159
  __metadata("design:type", Object)
151
160
  ], AuditEvent.prototype, "correlationId", void 0);
152
161
  __decorate([
@@ -165,40 +174,40 @@ __decorate([
165
174
  Enumeration(AuditSeverity),
166
175
  __metadata("design:type", String)
167
176
  ], AuditEvent.prototype, "severity", void 0);
168
- __decorate([
169
- Uuid(),
170
- __metadata("design:type", String)
171
- ], AuditEvent.prototype, "actorId", void 0);
172
177
  __decorate([
173
178
  Enumeration(ActorType),
174
179
  __metadata("design:type", String)
175
180
  ], AuditEvent.prototype, "actorType", void 0);
176
181
  __decorate([
177
- Uuid({ nullable: true }),
182
+ StringProperty(),
178
183
  __metadata("design:type", Object)
179
- ], AuditEvent.prototype, "impersonatorId", void 0);
184
+ ], AuditEvent.prototype, "actor", void 0);
180
185
  __decorate([
181
186
  Enumeration(ActorType, { nullable: true }),
182
187
  __metadata("design:type", Object)
183
188
  ], AuditEvent.prototype, "impersonatorType", void 0);
184
189
  __decorate([
185
- Uuid(),
186
- __metadata("design:type", String)
187
- ], AuditEvent.prototype, "targetId", void 0);
190
+ StringProperty({ nullable: true }),
191
+ __metadata("design:type", Object)
192
+ ], AuditEvent.prototype, "impersonator", void 0);
188
193
  __decorate([
189
194
  StringProperty(),
190
195
  __metadata("design:type", String)
191
196
  ], AuditEvent.prototype, "targetType", void 0);
192
197
  __decorate([
193
- Embedded(RequestDetails),
198
+ UuidProperty(),
199
+ __metadata("design:type", String)
200
+ ], AuditEvent.prototype, "targetId", void 0);
201
+ __decorate([
202
+ EmbeddedProperty(RequestDetails),
194
203
  __metadata("design:type", Object)
195
204
  ], AuditEvent.prototype, "network", void 0);
196
205
  __decorate([
197
- Embedded(ChangeDetails),
206
+ EmbeddedProperty(ChangeDetails),
198
207
  __metadata("design:type", Object)
199
208
  ], AuditEvent.prototype, "changes", void 0);
200
209
  __decorate([
201
- Json({ nullable: true }),
210
+ JsonProperty({ nullable: true }),
202
211
  __metadata("design:type", Object)
203
212
  ], AuditEvent.prototype, "details", void 0);
204
213
  AuditEvent = __decorate([