@tstdl/base 0.92.145 → 0.92.148
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/ai/ai-file.service.d.ts +29 -1
- package/ai/ai-file.service.js +66 -23
- package/ai/ai-session.d.ts +28 -1
- package/ai/ai-session.js +27 -0
- package/ai/ai.service.d.ts +89 -5
- package/ai/ai.service.js +130 -27
- package/ai/functions.d.ts +7 -1
- package/ai/functions.js +7 -1
- package/ai/module.d.ts +8 -0
- package/ai/module.js +4 -0
- package/ai/types.d.ts +115 -2
- package/ai/types.js +16 -0
- package/api/client/client.d.ts +3 -1
- package/api/client/client.js +5 -2
- package/api/default-error-handlers.d.ts +1 -1
- package/api/index.d.ts +1 -9
- package/api/index.js +1 -9
- package/api/response.d.ts +1 -1
- package/api/server/api-controller.d.ts +1 -1
- package/api/server/error-handler.d.ts +1 -1
- package/api/server/gateway.d.ts +1 -5
- package/api/server/gateway.js +0 -4
- package/api/server/middlewares/catch-error.middleware.d.ts +1 -1
- package/api/server/module.d.ts +1 -1
- package/api/types.d.ts +1 -1
- package/api/utils.d.ts +1 -0
- package/api/utils.js +1 -0
- package/application/application.d.ts +1 -1
- package/authentication/authentication.api.d.ts +36 -1
- package/authentication/authentication.api.js +28 -0
- package/authentication/client/api.client.d.ts +14 -1
- package/authentication/client/api.client.js +13 -0
- package/authentication/client/authentication.service.d.ts +104 -1
- package/authentication/client/authentication.service.js +103 -0
- package/authentication/client/http-client.middleware.d.ts +5 -0
- package/authentication/client/http-client.middleware.js +6 -2
- package/authentication/client/module.d.ts +20 -1
- package/authentication/client/module.js +6 -1
- package/authentication/client/tokens.d.ts +6 -0
- package/authentication/client/tokens.js +6 -0
- package/authentication/models/authentication-credentials.model.d.ts +6 -0
- package/authentication/models/authentication-credentials.model.js +6 -0
- package/authentication/models/authentication-session.model.d.ts +6 -0
- package/authentication/models/authentication-session.model.js +6 -0
- package/authentication/models/init-secret-reset-data.model.d.ts +10 -1
- package/authentication/models/init-secret-reset-data.model.js +10 -1
- package/authentication/models/token-payload-base.model.d.ts +24 -4
- package/authentication/models/token-payload-base.model.js +24 -4
- package/authentication/models/token.model.d.ts +33 -2
- package/authentication/server/authentication-ancillary.service.d.ts +27 -4
- package/authentication/server/authentication-ancillary.service.js +7 -0
- package/authentication/server/authentication-api-request-token.provider.d.ts +3 -0
- package/authentication/server/authentication-api-request-token.provider.js +3 -0
- package/authentication/server/authentication-secret-requirements.validator.d.ts +37 -0
- package/authentication/server/authentication-secret-requirements.validator.js +22 -0
- package/authentication/server/authentication.api-controller.d.ts +66 -1
- package/authentication/server/authentication.api-controller.js +65 -0
- package/authentication/server/authentication.service.d.ts +191 -11
- package/authentication/server/authentication.service.js +157 -8
- package/authentication/server/helper.d.ts +44 -5
- package/authentication/server/helper.js +43 -4
- package/authentication/server/module.d.ts +23 -1
- package/authentication/server/module.js +23 -1
- package/browser/browser-context-controller.d.ts +1 -1
- package/browser/browser-controller.d.ts +1 -1
- package/browser/browser-controller.js +1 -1
- package/browser/element-controller.d.ts +1 -1
- package/browser/locator-controller.d.ts +1 -1
- package/context/context.d.ts +1 -1
- package/data-structures/context-data-map.d.ts +1 -1
- package/database/mongo/mongo-base.repository.d.ts +1 -1
- package/database/mongo/types.d.ts +1 -1
- package/database/query.d.ts +1 -1
- package/document-management/api/document-management.api.d.ts +13 -0
- package/document-management/api/document-management.api.js +8 -0
- package/document-management/models/document-management-table.d.ts +1 -1
- package/document-management/models/document-validation-definition.model.d.ts +1 -1
- package/document-management/server/schemas.d.ts +1 -1
- package/document-management/server/services/document-collection.service.d.ts +1 -1
- package/document-management/server/services/document-management-observation.service.d.ts +1 -1
- package/document-management/server/services/document-management.service.d.ts +1 -1
- package/document-management/server/services/document-property.service.d.ts +3 -3
- package/document-management/server/services/document-request.service.d.ts +1 -1
- package/document-management/server/services/document-validation.service.d.ts +1 -1
- package/document-management/server/services/document-workflow.service.d.ts +1 -1
- package/document-management/server/services/document.service.d.ts +1 -1
- package/document-management/server/services/singleton.d.ts +1 -1
- package/document-management/service-models/document-management.view-model.d.ts +1 -1
- package/document-management/service-models/enriched/enriched-document-assignment.view.d.ts +1 -1
- package/document-management/service-models/enriched/enriched-document-category.view.d.ts +1 -1
- package/document-management/service-models/enriched/enriched-document-collection.view.d.ts +1 -1
- package/document-management/service-models/enriched/enriched-document-request.view.d.ts +1 -1
- package/document-management/service-models/enriched/enriched-document-type.view.d.ts +1 -1
- package/document-management/service-models/enriched/enriched-document.view.d.ts +1 -1
- package/document-management/service-models/enriched/enriched-requests-template-data.model.d.ts +1 -1
- package/dom/file-select-dialog.d.ts +1 -1
- package/enumeration/enumeration.d.ts +1 -1
- package/errors/custom.error.d.ts +3 -0
- package/errors/custom.error.js +0 -1
- package/errors/errors.localization.d.ts +1 -1
- package/errors/not-supported.error.d.ts +1 -1
- package/{formats.js → formats/formats.js} +3 -3
- package/formats/index.d.ts +1 -0
- package/formats/index.js +1 -0
- package/http/client/http-client-request.d.ts +1 -1
- package/http/client/http-client-request.js +1 -1
- package/http/client/http-client-response.d.ts +1 -1
- package/http/client/http-client.d.ts +1 -1
- package/http/client/module.d.ts +1 -1
- package/http/http-body.d.ts +1 -1
- package/http/http-value-map.d.ts +1 -1
- package/http/http.error.d.ts +1 -1
- package/http/index.d.ts +1 -0
- package/http/index.js +1 -0
- package/http/server/http-server-request.d.ts +1 -1
- package/http/server/http-server-response.d.ts +1 -1
- package/http/tokens.d.ts +5 -0
- package/http/tokens.js +5 -0
- package/http/types.d.ts +1 -1
- package/http/utils.d.ts +1 -1
- package/injector/decorators.d.ts +1 -1
- package/injector/index.d.ts +1 -1
- package/injector/index.js +1 -1
- package/injector/injector.d.ts +10 -1
- package/injector/injector.js +6 -0
- package/injector/interfaces.d.ts +1 -1
- package/injector/provider.d.ts +1 -1
- package/injector/resolution.d.ts +10 -5
- package/injector/resolve-chain.d.ts +2 -2
- package/injector/resolve-chain.js +1 -1
- package/injector/resolve.error.js +1 -1
- package/injector/token.d.ts +8 -1
- package/injector/token.js +7 -0
- package/injector/types.d.ts +1 -1
- package/key-value-store/key-value-store.provider.d.ts +2 -2
- package/key-value-store/key-value.store.d.ts +2 -2
- package/key-value-store/mongo/mongo-key-value-store.provider.d.ts +1 -1
- package/key-value-store/mongo/mongo-key-value.store.d.ts +1 -1
- package/key-value-store/postgres/key-value-store.service.d.ts +1 -1
- package/mail/mail.service.d.ts +1 -1
- package/mail/models/mail-data.model.d.ts +1 -1
- package/mail/models/mail-template.model.d.ts +1 -1
- package/mail/module.d.ts +1 -1
- package/module/index.d.ts +0 -1
- package/module/index.js +0 -1
- package/module/module-base.d.ts +1 -1
- package/module/module-metric-reporter.js +1 -1
- package/module/module.d.ts +1 -1
- package/module/modules/function.module.js +1 -1
- package/object-storage/object.d.ts +1 -1
- package/openid-connect/mongo-oidc-state.repository.d.ts +1 -1
- package/openid-connect/oidc.service.d.ts +1 -1
- package/orm/decorators.d.ts +2 -2
- package/orm/entity.d.ts +1 -1
- package/orm/index.d.ts +3 -3
- package/orm/index.js +3 -3
- package/orm/query.d.ts +1 -1
- package/orm/repository.types.d.ts +1 -1
- package/orm/schemas/json.d.ts +1 -1
- package/orm/server/database-schema.d.ts +1 -1
- package/orm/server/drizzle/schema-converter.d.ts +1 -1
- package/orm/server/repository.d.ts +1 -1
- package/orm/server/transaction.d.ts +1 -1
- package/orm/server/transactional.d.ts +3 -3
- package/orm/server/types.d.ts +1 -1
- package/orm/types.d.ts +1 -1
- package/package.json +23 -20
- package/queue/mongo/job.d.ts +1 -1
- package/queue/mongo/queue.js +31 -31
- package/queue/postgres/job.model.d.ts +1 -1
- package/queue/postgres/queue.d.ts +1 -1
- package/queue/postgres/queue.provider.d.ts +1 -1
- package/queue/provider.d.ts +1 -1
- package/reflection/decorators.d.ts +1 -1
- package/reflection/registry.d.ts +1 -1
- package/reflection/types.d.ts +1 -1
- package/reflection/utils.d.ts +1 -1
- package/rpc/model.d.ts +1 -1
- package/rxjs-utils/retry-backoff.js +2 -2
- package/schema/converters/openapi-converter.d.ts +1 -1
- package/schema/decorators/schema.d.ts +1 -1
- package/schema/decorators/utils.d.ts +1 -1
- package/schema/schema.d.ts +1 -1
- package/schema/schema.error.d.ts +1 -1
- package/schema/schemas/array.d.ts +1 -1
- package/schema/schemas/enumeration.d.ts +1 -1
- package/schema/schemas/function.d.ts +1 -1
- package/schema/schemas/instance.d.ts +1 -1
- package/schema/schemas/nullable.d.ts +1 -1
- package/schema/schemas/number.d.ts +1 -1
- package/schema/schemas/object.d.ts +1 -1
- package/schema/schemas/one-or-many.d.ts +1 -1
- package/schema/schemas/optional.d.ts +1 -1
- package/schema/schemas/simple.d.ts +1 -1
- package/search-index/elastic/model/index-mapping.d.ts +1 -1
- package/search-index/elastic/search-index.js +3 -4
- package/search-index/memory/memory-search-index.d.ts +1 -1
- package/search-index/memory/memory-search-index.js +1 -1
- package/serializer/handlers/binary.d.ts +1 -1
- package/serializer/serializable.d.ts +1 -1
- package/serializer/types.d.ts +1 -1
- package/templates/module.d.ts +1 -1
- package/templates/renderers/handlebars.template-renderer.d.ts +1 -1
- package/templates/renderers/jsx.template-renderer.d.ts +1 -1
- package/templates/renderers/mjml.template-renderer.d.ts +1 -1
- package/templates/renderers/string.template-renderer.d.ts +1 -1
- package/templates/resolvers/file.template-resolver.d.ts +1 -1
- package/templates/resolvers/jsx.template-resolver.d.ts +1 -1
- package/templates/resolvers/string.template-resolver.d.ts +1 -1
- package/templates/template.model.d.ts +1 -1
- package/templates/template.renderer.d.ts +1 -1
- package/templates/template.service.d.ts +1 -1
- package/text/dynamic-text.model.d.ts +1 -1
- package/text/localization.service.d.ts +1 -1
- package/types/geo-json.d.ts +1 -1
- package/types/index.d.ts +2 -0
- package/types/tagged.d.ts +1 -1
- package/{types.d.ts → types/types.d.ts} +2 -1
- package/utils/async-hook/async-hook.d.ts +109 -0
- package/utils/async-hook/async-hook.js +77 -3
- package/utils/backoff.d.ts +125 -43
- package/utils/backoff.js +140 -65
- package/utils/base64.d.ts +1 -1
- package/utils/base64.js +1 -2
- package/utils/binary.d.ts +1 -1
- package/utils/comparison.d.ts +5 -5
- package/utils/comparison.js +5 -3
- package/utils/cryptography.d.ts +1 -1
- package/utils/encoding.d.ts +1 -1
- package/utils/enum.d.ts +1 -1
- package/utils/equals.d.ts +1 -1
- package/utils/format-error.d.ts +1 -1
- package/utils/function/class.d.ts +1 -1
- package/utils/function/memoize.d.ts +1 -1
- package/utils/helpers.d.ts +1 -1
- package/utils/helpers.js +2 -2
- package/utils/jwt.d.ts +3 -3
- package/utils/merge.d.ts +1 -1
- package/utils/middleware.js +3 -3
- package/utils/object/decycle.d.ts +1 -1
- package/utils/object/forward-ref.d.ts +1 -1
- package/utils/object/lazy-property.d.ts +1 -1
- package/utils/object/object.d.ts +1 -1
- package/utils/object/property-name.d.ts +1 -1
- package/utils/patch-worker.d.ts +1 -1
- package/utils/reactive-value-to-signal.d.ts +1 -1
- package/utils/reflection.d.ts +1 -1
- package/utils/repl.d.ts +1 -1
- package/utils/singleton.d.ts +1 -1
- package/utils/stream/size-limited-stream.d.ts +1 -1
- package/utils/type/extends.d.ts +1 -1
- package/utils/type-guards.d.ts +1 -1
- package/utils/url-builder.d.ts +1 -1
- package/utils/z-base32.d.ts +1 -1
- package/module/utils.d.ts +0 -4
- package/module/utils.js +0 -21
- /package/{formats.d.ts → formats/formats.d.ts} +0 -0
- /package/{types.js → types/types.js} +0 -0
- /package/{web-types.d.ts → types/web-types.d.ts} +0 -0
- /package/{web-types.js → types/web-types.js} +0 -0
package/serializer/types.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { AbstractConstructor, JsonPrimitive, Nested, Record } from '../types.js';
|
|
1
|
+
import type { AbstractConstructor, JsonPrimitive, Nested, Record } from '../types/index.js';
|
|
2
2
|
declare const serializedSymbol: unique symbol;
|
|
3
3
|
declare const stringSerializedSymbol: unique symbol;
|
|
4
4
|
declare const decycledSymbol: unique symbol;
|
package/templates/module.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Type } from '../types.js';
|
|
1
|
+
import type { Type } from '../types/index.js';
|
|
2
2
|
import { TemplateProvider } from './template.provider.js';
|
|
3
3
|
import { TemplateRenderer } from './template.renderer.js';
|
|
4
4
|
import { TemplateResolver } from './template.resolver.js';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ObjectLiteral, Record } from '../../types.js';
|
|
1
|
+
import type { ObjectLiteral, Record } from '../../types/index.js';
|
|
2
2
|
import type * as Handlebars from 'handlebars';
|
|
3
3
|
import { TemplateResolverProvider } from '../template-resolver.provider.js';
|
|
4
4
|
import type { TemplateField } from '../template.model.js';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Record } from '../../types.js';
|
|
1
|
+
import type { Record } from '../../types/index.js';
|
|
2
2
|
import type { JsxTemplate } from '../resolvers/jsx.template-resolver.js';
|
|
3
3
|
import type { TemplateRenderObject, TemplateRenderResult } from '../template.renderer.js';
|
|
4
4
|
import { TemplateRenderer } from '../template.renderer.js';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Record } from '../../types.js';
|
|
1
|
+
import type { Record } from '../../types/index.js';
|
|
2
2
|
import type { MJMLParsingOptions } from 'mjml-core';
|
|
3
3
|
import { TemplateRendererProvider } from '../template-renderer.provider.js';
|
|
4
4
|
import type { TemplateRenderObject, TemplateRenderResult } from '../template.renderer.js';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Record } from '../../types.js';
|
|
1
|
+
import type { Record } from '../../types/index.js';
|
|
2
2
|
import type { StringTemplate } from '../resolvers/string.template-resolver.js';
|
|
3
3
|
import type { TemplateRenderObject, TemplateRenderResult } from '../template.renderer.js';
|
|
4
4
|
import { TemplateRenderer } from '../template.renderer.js';
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type Resolvable, resolveArgumentType } from '../../injector/interfaces.js';
|
|
2
|
-
import type { TypedOmit } from '../../types.js';
|
|
2
|
+
import type { TypedOmit } from '../../types/index.js';
|
|
3
3
|
import { TemplateField } from '../template.model.js';
|
|
4
4
|
import type { TemplateRenderer, TemplateRendererOptions, TemplateRendererString } from '../template.renderer.js';
|
|
5
5
|
import { TemplateResolver } from '../template.resolver.js';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { PartialProperty, Record, TypedOmit } from '../../types.js';
|
|
1
|
+
import type { PartialProperty, Record, TypedOmit } from '../../types/index.js';
|
|
2
2
|
import type { ComponentClass, FunctionComponent } from 'preact';
|
|
3
3
|
import { type Template, TemplateField } from '../template.model.js';
|
|
4
4
|
import type { TemplateRenderer, TemplateRendererOptions, TemplateRendererString } from '../template.renderer.js';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Record, TypedOmit } from '../../types.js';
|
|
1
|
+
import type { Record, TypedOmit } from '../../types/index.js';
|
|
2
2
|
import { TemplateField } from '../template.model.js';
|
|
3
3
|
import type { TemplateRenderer, TemplateRendererOptions, TemplateRendererString } from '../template.renderer.js';
|
|
4
4
|
import { TemplateResolver } from '../template.resolver.js';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { PickBy, Record, SimplifyObject } from '../types.js';
|
|
1
|
+
import type { PickBy, Record, SimplifyObject } from '../types/index.js';
|
|
2
2
|
export type TemplateContext<T extends Template> = Parameters<NonNullable<T[typeof templateContext]>>[0];
|
|
3
3
|
declare const templateContext: unique symbol;
|
|
4
4
|
export declare class TemplateField<Resolver extends string = string, Renderer extends string = string, Options = any, Context extends Record = any> {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { SchemaTestable } from '../schema/index.js';
|
|
2
|
-
import type { Record } from '../types.js';
|
|
2
|
+
import type { Record } from '../types/index.js';
|
|
3
3
|
export type TemplateRenderResult = string;
|
|
4
4
|
export type TemplateRenderObject<Renderer extends string = string, Options = any, V = unknown> = {
|
|
5
5
|
renderer: Renderer;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Record } from '../types.js';
|
|
1
|
+
import type { Record } from '../types/index.js';
|
|
2
2
|
import { TemplateRendererProvider } from './template-renderer.provider.js';
|
|
3
3
|
import { TemplateResolverProvider } from './template-resolver.provider.js';
|
|
4
4
|
import type { Template, TemplateContext } from './template.model.js';
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type Observable } from 'rxjs';
|
|
2
2
|
import { type Signal } from '../signals/api.js';
|
|
3
|
-
import type { PickBy, ReactiveValue, ReplaceKey } from '../types.js';
|
|
3
|
+
import type { PickBy, ReactiveValue, ReplaceKey } from '../types/index.js';
|
|
4
4
|
import type { LocalizableText } from './localizable-text.model.js';
|
|
5
5
|
import { LocalizationService } from './localization.service.js';
|
|
6
6
|
export type DynamicText = ReactiveValue<LocalizableText>;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { Observable } from 'rxjs';
|
|
2
2
|
import { Logger } from '../logger/index.js';
|
|
3
3
|
import type { Signal } from '../signals/api.js';
|
|
4
|
-
import type { Enumeration, EnumerationValue, Record } from '../types.js';
|
|
4
|
+
import type { Enumeration, EnumerationValue, Record } from '../types/index.js';
|
|
5
5
|
import type { PropertyName } from '../utils/object/property-name.js';
|
|
6
6
|
export type Language = {
|
|
7
7
|
code: string;
|
package/types/geo-json.d.ts
CHANGED
package/types/index.d.ts
CHANGED
package/types/tagged.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Observable } from 'rxjs';
|
|
2
2
|
import type { CamelCase, Except, IsEqual, LiteralUnion } from 'type-fest';
|
|
3
|
-
import type { Signal } from '
|
|
3
|
+
import type { Signal } from '../signals/api.js';
|
|
4
4
|
export type ObjectLiteral = {};
|
|
5
5
|
export type Function<P extends any[] = any[], R = any> = (...params: P) => R;
|
|
6
6
|
export type PrimitiveTypeMap = {
|
|
@@ -149,6 +149,7 @@ export type PickBy<T, V> = Pick<T, {
|
|
|
149
149
|
export type NonNullable<T> = T extends null ? never : T;
|
|
150
150
|
export type NonUndefinable<T> = T extends undefined ? never : T;
|
|
151
151
|
export type NonNullOrUndefinable<T> = T extends null | undefined ? never : T;
|
|
152
|
+
export type NoUnion<T> = [T] extends [boolean] ? boolean : [T] extends [UnionToIntersection<T>] ? T : never;
|
|
152
153
|
/**
|
|
153
154
|
* Makes optional properties required and removes null and undefined
|
|
154
155
|
*/
|
|
@@ -1,9 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents the public interface for an asynchronous hook.
|
|
3
|
+
*
|
|
4
|
+
* @template T The type of the value that the hook is triggered with.
|
|
5
|
+
* @template C The type of the optional context object passed to the hook's trigger and handlers. Defaults to `never`.
|
|
6
|
+
* @template R The return type of an individual handler. Defaults to `unknown`.
|
|
7
|
+
*/
|
|
1
8
|
export type AsyncHook<T, C = never, R = unknown> = {
|
|
9
|
+
/**
|
|
10
|
+
* Registers a handler function to be called when the hook is triggered.
|
|
11
|
+
* @param handler The async handler function to register.
|
|
12
|
+
* @returns A registration object with an `unregister` method to remove the handler.
|
|
13
|
+
*/
|
|
2
14
|
register: (handler: AsyncHookHandler<T, C, R>) => AsyncHookHandlerRegistration;
|
|
15
|
+
/**
|
|
16
|
+
* Triggers the hook, executing all registered handlers in sequence.
|
|
17
|
+
*
|
|
18
|
+
* The signature of this function is conditional:
|
|
19
|
+
* - If the context type `C` is `never` (the default), it accepts only the `value` argument.
|
|
20
|
+
* - If `C` is any other type, it requires both `value` and `context` arguments.
|
|
21
|
+
*
|
|
22
|
+
* @param value The value to pass to all handlers.
|
|
23
|
+
* @param context The context object to pass to all handlers (only required if `C` is not `never`).
|
|
24
|
+
* @returns A promise that resolves to an array of results from all handlers.
|
|
25
|
+
*/
|
|
3
26
|
trigger: [C] extends [never] ? ((value: T) => Promise<R[]>) : ((value: T, context: C) => Promise<R[]>);
|
|
4
27
|
};
|
|
28
|
+
/**
|
|
29
|
+
* Defines the signature for a handler function that can be registered with an `AsyncHook`.
|
|
30
|
+
* The function can be synchronous (returning `R`) or asynchronous (returning `Promise<R>`).
|
|
31
|
+
*
|
|
32
|
+
* The signature is conditional based on the context type `C`:
|
|
33
|
+
* - If `C` is `never`, the handler receives only the `value` argument.
|
|
34
|
+
* - If `C` is any other type, the handler receives both `value` and `context`.
|
|
35
|
+
*
|
|
36
|
+
* @template T The type of the value passed to the handler.
|
|
37
|
+
* @template C The type of the optional context object.
|
|
38
|
+
* @template R The expected return type of the handler.
|
|
39
|
+
*/
|
|
5
40
|
export type AsyncHookHandler<T, C, R> = [C] extends [never] ? ((value: T) => R | Promise<R>) : ((value: T, context: C) => R | Promise<R>);
|
|
41
|
+
/**
|
|
42
|
+
* Represents the object returned when a handler is registered,
|
|
43
|
+
* allowing for its subsequent unregistration.
|
|
44
|
+
*/
|
|
6
45
|
export type AsyncHookHandlerRegistration = {
|
|
46
|
+
/**
|
|
47
|
+
* Unregisters the handler, preventing it from being called in future triggers.
|
|
48
|
+
*/
|
|
7
49
|
unregister: () => void;
|
|
8
50
|
};
|
|
51
|
+
/**
|
|
52
|
+
* Creates a new asynchronous hook.
|
|
53
|
+
*
|
|
54
|
+
* An async hook is a system that allows you to register multiple "handler" functions
|
|
55
|
+
* that will be executed in sequence when a "trigger" event occurs. This is useful
|
|
56
|
+
* for creating extensible, plugin-like architectures. Handlers can be synchronous
|
|
57
|
+
* or asynchronous.
|
|
58
|
+
*
|
|
59
|
+
* @template T The type of the primary value that the hook is triggered with.
|
|
60
|
+
* @template C The type of the optional context object passed to the hook's trigger and handlers. Defaults to `never`.
|
|
61
|
+
* @template R The return type of an individual handler. The `trigger` method will resolve with an array of these values (`R[]`). Defaults to `unknown`.
|
|
62
|
+
* @returns {AsyncHook<T, C, R>} An object with `register` and `trigger` methods.
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```ts
|
|
66
|
+
* // Simple hook without context
|
|
67
|
+
* async function runSimpleExample() {
|
|
68
|
+
* const onTaskStart = asyncHook<string>();
|
|
69
|
+
*
|
|
70
|
+
* onTaskStart.register(taskName => {
|
|
71
|
+
* console.log(`[Logger] Task started: ${taskName}`);
|
|
72
|
+
* });
|
|
73
|
+
*
|
|
74
|
+
* const registration = onTaskStart.register(async taskName => {
|
|
75
|
+
* await new Promise(resolve => setTimeout(resolve, 50));
|
|
76
|
+
* console.log(`[Notifier] Notifying that task started: ${taskName}`);
|
|
77
|
+
* });
|
|
78
|
+
*
|
|
79
|
+
* await onTaskStart.trigger('Process Data');
|
|
80
|
+
* // [Logger] Task started: Process Data
|
|
81
|
+
* // [Notifier] Notifying that task started: Process Data
|
|
82
|
+
*
|
|
83
|
+
* registration.unregister();
|
|
84
|
+
* console.log('Notifier unregistered.');
|
|
85
|
+
*
|
|
86
|
+
* await onTaskStart.trigger('Finalize Report');
|
|
87
|
+
* // [Logger] Task started: Finalize Report
|
|
88
|
+
* }
|
|
89
|
+
* ```
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```ts
|
|
93
|
+
* // Hook with a context object
|
|
94
|
+
* async function runContextExample() {
|
|
95
|
+
* type TaskContext = { userId: number; transactionId: string };
|
|
96
|
+
*
|
|
97
|
+
* const onTaskComplete = asyncHook<string, TaskContext, boolean>();
|
|
98
|
+
*
|
|
99
|
+
* onTaskComplete.register((taskName, context) => {
|
|
100
|
+
* console.log(`[Audit] Task '${taskName}' completed by user ${context.userId}.`);
|
|
101
|
+
* return true; // Audit successful
|
|
102
|
+
* });
|
|
103
|
+
*
|
|
104
|
+
* onTaskComplete.register(async (taskName, context) => {
|
|
105
|
+
* console.log(`[DB] Logging completion of '${taskName}' for transaction ${context.transactionId}.`);
|
|
106
|
+
* return true; // DB update successful
|
|
107
|
+
* });
|
|
108
|
+
*
|
|
109
|
+
* const results = await onTaskComplete.trigger(
|
|
110
|
+
* 'SubmitOrder',
|
|
111
|
+
* { userId: 123, transactionId: 'abc-456' }
|
|
112
|
+
* );
|
|
113
|
+
*
|
|
114
|
+
* console.log('Handler results:', results); // [true, true]
|
|
115
|
+
* }
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
9
118
|
export declare function asyncHook<T, C = never, R = unknown>(): AsyncHook<T, C, R>;
|
|
@@ -1,3 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a new asynchronous hook.
|
|
3
|
+
*
|
|
4
|
+
* An async hook is a system that allows you to register multiple "handler" functions
|
|
5
|
+
* that will be executed in sequence when a "trigger" event occurs. This is useful
|
|
6
|
+
* for creating extensible, plugin-like architectures. Handlers can be synchronous
|
|
7
|
+
* or asynchronous.
|
|
8
|
+
*
|
|
9
|
+
* @template T The type of the primary value that the hook is triggered with.
|
|
10
|
+
* @template C The type of the optional context object passed to the hook's trigger and handlers. Defaults to `never`.
|
|
11
|
+
* @template R The return type of an individual handler. The `trigger` method will resolve with an array of these values (`R[]`). Defaults to `unknown`.
|
|
12
|
+
* @returns {AsyncHook<T, C, R>} An object with `register` and `trigger` methods.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* // Simple hook without context
|
|
17
|
+
* async function runSimpleExample() {
|
|
18
|
+
* const onTaskStart = asyncHook<string>();
|
|
19
|
+
*
|
|
20
|
+
* onTaskStart.register(taskName => {
|
|
21
|
+
* console.log(`[Logger] Task started: ${taskName}`);
|
|
22
|
+
* });
|
|
23
|
+
*
|
|
24
|
+
* const registration = onTaskStart.register(async taskName => {
|
|
25
|
+
* await new Promise(resolve => setTimeout(resolve, 50));
|
|
26
|
+
* console.log(`[Notifier] Notifying that task started: ${taskName}`);
|
|
27
|
+
* });
|
|
28
|
+
*
|
|
29
|
+
* await onTaskStart.trigger('Process Data');
|
|
30
|
+
* // [Logger] Task started: Process Data
|
|
31
|
+
* // [Notifier] Notifying that task started: Process Data
|
|
32
|
+
*
|
|
33
|
+
* registration.unregister();
|
|
34
|
+
* console.log('Notifier unregistered.');
|
|
35
|
+
*
|
|
36
|
+
* await onTaskStart.trigger('Finalize Report');
|
|
37
|
+
* // [Logger] Task started: Finalize Report
|
|
38
|
+
* }
|
|
39
|
+
* ```
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```ts
|
|
43
|
+
* // Hook with a context object
|
|
44
|
+
* async function runContextExample() {
|
|
45
|
+
* type TaskContext = { userId: number; transactionId: string };
|
|
46
|
+
*
|
|
47
|
+
* const onTaskComplete = asyncHook<string, TaskContext, boolean>();
|
|
48
|
+
*
|
|
49
|
+
* onTaskComplete.register((taskName, context) => {
|
|
50
|
+
* console.log(`[Audit] Task '${taskName}' completed by user ${context.userId}.`);
|
|
51
|
+
* return true; // Audit successful
|
|
52
|
+
* });
|
|
53
|
+
*
|
|
54
|
+
* onTaskComplete.register(async (taskName, context) => {
|
|
55
|
+
* console.log(`[DB] Logging completion of '${taskName}' for transaction ${context.transactionId}.`);
|
|
56
|
+
* return true; // DB update successful
|
|
57
|
+
* });
|
|
58
|
+
*
|
|
59
|
+
* const results = await onTaskComplete.trigger(
|
|
60
|
+
* 'SubmitOrder',
|
|
61
|
+
* { userId: 123, transactionId: 'abc-456' }
|
|
62
|
+
* );
|
|
63
|
+
*
|
|
64
|
+
* console.log('Handler results:', results); // [true, true]
|
|
65
|
+
* }
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
1
68
|
export function asyncHook() {
|
|
2
69
|
const handlers = [];
|
|
3
70
|
return {
|
|
@@ -5,13 +72,20 @@ export function asyncHook() {
|
|
|
5
72
|
handlers.push(handler);
|
|
6
73
|
return {
|
|
7
74
|
unregister() {
|
|
8
|
-
handlers.
|
|
75
|
+
const index = handlers.indexOf(handler);
|
|
76
|
+
if (index > -1) {
|
|
77
|
+
handlers.splice(index, 1);
|
|
78
|
+
}
|
|
9
79
|
},
|
|
10
80
|
};
|
|
11
81
|
},
|
|
82
|
+
// The implementation uses a single function body, but the public type signature
|
|
83
|
+
// is conditional, ensuring type safety for callers.
|
|
12
84
|
trigger: async (value, context) => {
|
|
13
|
-
|
|
14
|
-
|
|
85
|
+
const returnValues = [];
|
|
86
|
+
// Create a snapshot of handlers in case one of them unregisters another during its execution.
|
|
87
|
+
for (const handler of [...handlers]) {
|
|
88
|
+
// The non-null assertion `context!` is safe due to the conditional public type of `trigger`.
|
|
15
89
|
const returnValue = await handler(value, context);
|
|
16
90
|
returnValues.push(returnValue);
|
|
17
91
|
}
|
package/utils/backoff.d.ts
CHANGED
|
@@ -1,77 +1,159 @@
|
|
|
1
1
|
import type { CancellationSignal } from '../cancellation/token.js';
|
|
2
|
-
import { CancellationToken } from '../cancellation/token.js';
|
|
3
2
|
export type BackoffStrategy = 'linear' | 'exponential';
|
|
3
|
+
/**
|
|
4
|
+
* Configuration for the backoff behavior.
|
|
5
|
+
*/
|
|
4
6
|
export type BackoffOptions = {
|
|
5
7
|
/**
|
|
6
|
-
*
|
|
8
|
+
* The strategy to use for increasing the delay.
|
|
9
|
+
* - `linear`: Adds the `increase` value to the delay in each step.
|
|
10
|
+
* - `exponential`: Multiplies the delay by the `increase` value in each step.
|
|
11
|
+
* @default 'exponential'
|
|
7
12
|
*/
|
|
8
|
-
strategy
|
|
13
|
+
strategy?: BackoffStrategy;
|
|
9
14
|
/**
|
|
10
|
-
* delay
|
|
15
|
+
* The initial delay in milliseconds. Must be non-negative.
|
|
16
|
+
* @default 1000
|
|
11
17
|
*/
|
|
12
|
-
initialDelay
|
|
18
|
+
initialDelay?: number;
|
|
13
19
|
/**
|
|
14
|
-
*
|
|
20
|
+
* The value to increase the delay with.
|
|
21
|
+
* - For `linear` strategy, this is the number of milliseconds to add.
|
|
22
|
+
* - For `exponential` strategy, this is the multiplication factor.
|
|
23
|
+
* Must be non-negative. For exponential, should be > 1 to ensure growth.
|
|
24
|
+
* @default 2
|
|
15
25
|
*/
|
|
16
|
-
increase
|
|
26
|
+
increase?: number;
|
|
17
27
|
/**
|
|
18
|
-
*
|
|
28
|
+
* The maximum delay in milliseconds. The backoff delay will not exceed this value.
|
|
29
|
+
* @default 30000
|
|
19
30
|
*/
|
|
20
31
|
maximumDelay?: number;
|
|
32
|
+
/**
|
|
33
|
+
* A factor to randomize the delay, e.g., 0.1 for 10% jitter.
|
|
34
|
+
* This helps prevent the "thundering herd" problem in distributed systems.
|
|
35
|
+
* The actual delay will be `delay ± delay * jitter`.
|
|
36
|
+
* Must be a value between 0 and 1.
|
|
37
|
+
* @default 0.15
|
|
38
|
+
*/
|
|
39
|
+
jitter?: number;
|
|
40
|
+
};
|
|
41
|
+
export type AutoBackoffLoopOptions = BackoffOptions & {
|
|
42
|
+
cancellationSignal?: CancellationSignal;
|
|
43
|
+
errorHandler?: (error: Error) => void;
|
|
44
|
+
};
|
|
45
|
+
export type BackoffLoopOptions = BackoffOptions & {
|
|
46
|
+
cancellationSignal?: CancellationSignal;
|
|
47
|
+
};
|
|
48
|
+
export type BackoffGeneratorOptions = BackoffOptions & {
|
|
49
|
+
/**
|
|
50
|
+
* An optional signal to terminate the generator.
|
|
51
|
+
* The generator can also be terminated by breaking the `for-await-of` loop.
|
|
52
|
+
*/
|
|
53
|
+
cancellationSignal?: CancellationSignal;
|
|
21
54
|
};
|
|
55
|
+
/**
|
|
56
|
+
* Provides controls for the `backoffLoop`.
|
|
57
|
+
*/
|
|
22
58
|
export type BackoffLoopController = {
|
|
23
59
|
/**
|
|
24
|
-
* backoff before next iteration
|
|
60
|
+
* Schedules a backoff delay before the next iteration of the loop.
|
|
61
|
+
* If this is not called, the backoff delay is reset for the next attempt.
|
|
25
62
|
*/
|
|
26
63
|
backoff: () => void;
|
|
27
64
|
/**
|
|
28
|
-
*
|
|
65
|
+
* Immediately breaks out of the loop.
|
|
29
66
|
*/
|
|
30
67
|
break: () => void;
|
|
31
68
|
};
|
|
32
|
-
export type BackoffLoopFunction = (controller: BackoffLoopController) =>
|
|
69
|
+
export type BackoffLoopFunction = (controller: BackoffLoopController, cancellationSignal: CancellationSignal) => unknown;
|
|
70
|
+
/**
|
|
71
|
+
* A function yielded by `backoffGenerator` to control the next iteration.
|
|
72
|
+
*/
|
|
73
|
+
export type BackoffGeneratorCallback = (options?: {
|
|
74
|
+
/**
|
|
75
|
+
* An optional signal that, when set, cancels the current backoff delay and proceeds
|
|
76
|
+
* to the next iteration immediately. This is useful for "continue early" scenarios.
|
|
77
|
+
*/
|
|
78
|
+
continueToken?: CancellationSignal;
|
|
79
|
+
}) => void;
|
|
80
|
+
/**
|
|
81
|
+
* Default options for a robust backoff strategy.
|
|
82
|
+
*/
|
|
83
|
+
export declare const DEFAULT_BACKOFF_OPTIONS: {
|
|
84
|
+
readonly strategy: "exponential";
|
|
85
|
+
readonly initialDelay: 1000;
|
|
86
|
+
readonly increase: 2;
|
|
87
|
+
readonly maximumDelay: 30000;
|
|
88
|
+
readonly jitter: 0.15;
|
|
89
|
+
};
|
|
33
90
|
/**
|
|
34
|
-
*
|
|
91
|
+
* A helper class to manage the state of a backoff strategy.
|
|
35
92
|
*/
|
|
36
|
-
export type BackoffGeneratorYield = (continueToken?: CancellationToken) => void;
|
|
37
93
|
export declare class BackoffHelper {
|
|
38
|
-
private readonly
|
|
39
|
-
private
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
94
|
+
private readonly options;
|
|
95
|
+
private currentDelay;
|
|
96
|
+
/**
|
|
97
|
+
* Creates a new BackoffHelper.
|
|
98
|
+
* @param options Partial backoff options, which will be merged with sane defaults.
|
|
99
|
+
*/
|
|
100
|
+
constructor(options?: BackoffOptions);
|
|
101
|
+
/**
|
|
102
|
+
* Resets the current delay to the initial delay.
|
|
103
|
+
*/
|
|
44
104
|
reset(): void;
|
|
45
|
-
|
|
105
|
+
/**
|
|
106
|
+
* Calculates and returns the next backoff delay based on the configured strategy.
|
|
107
|
+
* This also updates the internal state for the subsequent call.
|
|
108
|
+
* @returns The next delay in milliseconds.
|
|
109
|
+
*/
|
|
110
|
+
getNextDelay(): number;
|
|
46
111
|
}
|
|
47
112
|
/**
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
113
|
+
* Runs a function in a loop, automatically backing off on errors.
|
|
114
|
+
*
|
|
115
|
+
* This function is a convenient wrapper that catches any errors from `loopFunction`,
|
|
116
|
+
* triggers a backoff, and continues the loop.
|
|
117
|
+
*
|
|
118
|
+
* @param loopFunction The asynchronous function to execute in each iteration.
|
|
119
|
+
* @param options Additional options for backoff configuration, cancellation, and error handling.
|
|
52
120
|
*/
|
|
53
|
-
export declare function autoBackoffLoop(
|
|
54
|
-
cancellationSignal?: CancellationSignal;
|
|
55
|
-
errorHandler?: (error: Error) => void;
|
|
56
|
-
}): Promise<void>;
|
|
121
|
+
export declare function autoBackoffLoop(loopFunction: BackoffLoopFunction, options?: AutoBackoffLoopOptions): Promise<void>;
|
|
57
122
|
/**
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
123
|
+
* Runs a function in a loop, allowing manual control over backoff and breaking.
|
|
124
|
+
* The loop continues until it is explicitly broken via the controller or the cancellation signal is set.
|
|
125
|
+
* If `controller.backoff()` is not called in an iteration, the delay is reset for the next backoff.
|
|
126
|
+
*
|
|
127
|
+
* @param loopFunction The function to execute, receiving a controller to manage the loop.
|
|
128
|
+
* @param options Additional options for backoff configuration and cancellation.
|
|
62
129
|
*/
|
|
63
|
-
export declare function backoffLoop(
|
|
64
|
-
cancellationSignal?: CancellationSignal;
|
|
65
|
-
}): Promise<void>;
|
|
130
|
+
export declare function backoffLoop(loopFunction: BackoffLoopFunction, options?: BackoffLoopOptions): Promise<void>;
|
|
66
131
|
/**
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
132
|
+
* Creates an async generator that yields a callback to trigger a backoff.
|
|
133
|
+
* This is useful for `for-await-of` loops where you need fine-grained control.
|
|
134
|
+
* The generator terminates when the `cancellationSignal` is set or the loop is terminated by break.
|
|
135
|
+
*
|
|
136
|
+
* @param options Options for backoff configuration and an optional cancellation signal.
|
|
70
137
|
* @example
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
138
|
+
* ```ts
|
|
139
|
+
* const token = new CancellationToken();
|
|
140
|
+
*
|
|
141
|
+
* // with a cancellation signal
|
|
142
|
+
* for await (const backoff of backoffGenerator({ cancellationSignal: token.signal })) {
|
|
143
|
+
* const success = await doSomeWork();
|
|
144
|
+
* if (!success) {
|
|
145
|
+
* backoff(); // Schedule a backoff before the next iteration.
|
|
146
|
+
* }
|
|
147
|
+
* }
|
|
148
|
+
*
|
|
149
|
+
* // without a cancellation signal (loop is broken manually)
|
|
150
|
+
* for await (const backoff of backoffGenerator()) {
|
|
151
|
+
* const success = await doSomeWork();
|
|
152
|
+
* if (success) {
|
|
153
|
+
* break; // Exit the loop
|
|
154
|
+
* }
|
|
155
|
+
* backoff();
|
|
75
156
|
* }
|
|
157
|
+
* ```
|
|
76
158
|
*/
|
|
77
|
-
export declare function backoffGenerator(options
|
|
159
|
+
export declare function backoffGenerator(options?: BackoffGeneratorOptions): AsyncIterableIterator<BackoffGeneratorCallback>;
|