import type { SinonSandbox } from 'sinon'; import type { QueryParameter } from '../core/HttpServer/types/QueryParameter.js'; import type { SupportedHttpMethod } from '../core/HttpServer/types/SupportedHttpMethod.js'; import type { Service } from '../core/Service/Service.impl.js'; import type { Complete } from '../core/types/Complete.js'; import type { ContentType } from '../core/types/ContentType.js'; import type { CommandAfterGuardHook } from '../core/types/commandType/CommandAfterGuardHook.js'; import type { CommandBeforeGuardHook } from '../core/types/commandType/CommandBeforeGuardHook.js'; import type { CommandDefinition } from '../core/types/commandType/CommandDefinition.js'; import type { CommandDefinitionMetadataBase } from '../core/types/commandType/CommandDefinitionMetadataBase.js'; import type { CommandFunction } from '../core/types/commandType/CommandFunction.js'; import type { CommandTransformInputHook } from '../core/types/commandType/CommandTransformInputHook.js'; import type { CommandTransformOutputHook } from '../core/types/commandType/CommandTransformOutputHook.js'; import type { GetMessageParamsType } from '../core/types/GetMessageParamsType.js'; import type { GetMessagePayloadType } from '../core/types/GetMessagePayloadType.js'; import { StatusCode } from '../core/types/StatusCode.enum.js'; import type { ScheduleOptions } from '../core/types/schedule/index.js'; import type { NonEmptyString } from '../helper/types/NonEmptyString.js'; import type { Infer, InferIn, Schema } from '../schema/index.js'; import type { CommandDefinitionBuilderTypes } from './CommandDefinitionBuilderTypes.js'; export type HttpExposureOptions = { /** Execute HTTP requests synchronously or enqueue asynchronously when queue access is declared. */ mode?: 'sync' | 'async'; }; /** * Command definition builder is a helper to create and define a command for a service. * It helps to set all needed information like schemas and hooks. * With these information, the types are automatically set and extended. * * A working schema definition needs at least a command name, a short description and the function implementation. * * @group Command */ export declare class CommandDefinitionBuilder { private commandName; private commandDescription; private eventName?; private httpMetadata?; private inputSchema?; private inputContentType; private inputContentEncoding; private outputSchema?; private outputContentType; private outputContentEncoding; private parameterSchema?; private queryParameter; private tags; private deprecated; private summary?; private errorStatusCodes; private isSecure; private operationId?; private durable; private autoacknowledge; private schedules; private invokes; private streamInvokes; private emitList; private hooks; /** * Declare a queue this command handler may enqueue through its typed queue proxy. * * @example * ```ts * command.canEnqueue('billing.monthlyClosing', monthlyPayloadSchema) * ``` */ canEnqueue(queueName: QueueName, payloadSchema?: Payload, parameterSchema?: Parameter): CommandDefinitionBuilder>>; private queueInvokes; private fn?; constructor(commandName: Exclude, commandDescription: string, eventName?: Exclude | undefined, deprecated?: boolean); /** * Define a command which can be invoked by the current command * @param serviceName * @param serviceVersion * @param serviceTarget * @param outputSchema * @param payloadSchema * @param parameterSchema * @returns */ canInvoke(serviceName: SName, serviceVersion: Version, serviceTarget: Fname, outputSchema?: Output, payloadSchema?: Payload, parameterSchema?: Parameter): CommandDefinitionBuilder, parameter: InferIn) => Promise>>>>, C['StreamInvokes'], C['EmitList'], C['QueueInvokes']>>; /** * Declare a stream this command handler may consume through its typed stream proxy. * * @example * ```ts * command.canConsumeStream( * 'reports', * '1', * 'generateReport', * reportChunkSchema, * reportRequestSchema, * ) * ``` */ canConsumeStream(serviceName: SName, serviceVersion: Version, serviceTarget: Fname, chunkSchema?: Chunk, payloadSchema?: Payload, parameterSchema?: Parameter, finalSchema?: Final, validateChunk?: boolean, validateFinal?: boolean): CommandDefinitionBuilder, parameter: InferIn) => Promise<{ sessionId: string; cancel(reason?: string): Promise; [Symbol.asyncIterator](): AsyncIterator<{ payload: { chunk?: Infer; final?: Infer; }; }>; }>>>>, C['EmitList'], C['QueueInvokes']>>; /** * Define which custom events the command can emit. * * @param eventName The custom event name * @param schema the payload schema * @returns */ canEmit(eventName: EventName, schema: T): CommandDefinitionBuilder>>>; setSuccessEventName(eventName: NonEmptyString): this; /** * Add a schema for input payload validation. * Types for payload of message and function payload input are generated from given schema. * @param inputSchema The schema validation for input payload * @param inputContentType optional the content type of payload * @param inputContentEncoding optional the content encoding * @returns CommandDefinitionBuilder */ addPayloadSchema(inputSchema: PayloadSchema, inputContentType?: ContentType, inputContentEncoding?: string): CommandDefinitionBuilder>; /** * Add a schema for output parameter validation. * Types for parameter of message and function parameter output are generated from given schema. * @param parameterSchema The schema validation for output parameter * @returns CommandDefinitionBuilder */ addParameterSchema(parameterSchema: ParamsSchema): CommandDefinitionBuilder>; /** * Add a schema for output payload validation. * Types for payload of message and function payload output are generated from given schema. * @param outputSchema The schema validation for output payload * @param outputContentType optional the content type of payload * @param outputContentEncoding optional the content encoding * @returns CommandDefinitionBuilder */ addOutputSchema(outputSchema: OutputSchema, outputContentType?: ContentType, outputContentEncoding?: string): CommandDefinitionBuilder>; /** * Mark this endpoint/command as deprecated * @returns CommandDefinitionBuilder */ markAsDeprecated(): this; /** * Define query parameters if you expose the function as http endpoint. * Query parameters are add to openApi definition. * Query parameters are add to input parameters. * * @example * ```ts * .addQueryParameters( * { * required: false, * name: 'search', * }, * { * required: false, * name: 'limit', * }, * ) * ``` * * @param queryParams Add one or more query parameter definitions * @returns CommandDefinitionBuilder */ addQueryParameters(...queryParams: QueryParameter>[]): this; /** * Add tags for openApi definition for given function. * It is recommended to use some enum for tags to avoid typo issues. * * @example * ```ts * addTags('User','Public') * ``` * * @param tags List of tag strings * @returns CommandDefinitionBuilder */ addOpenApiTags(...tags: string[]): this; /** * If a function can return other status codes, than the default ones, you should add them to openApi definition. * By default, 200, 204, 400, 401 and 500 can be autogenerated in most cases. * Special cases or different status codes should be added with this function. * * @example * ```ts * addErrorStatusCodes(StatusCode.PaymentRequired, StatusCode.Conflict) * ``` * * @param codes List of status codes * @returns CommandDefinitionBuilder */ addOpenApiErrorStatusCodes(...codes: StatusCode[]): this; /** * Set a transform input hook which will encode or transform the input payload and parameters. * Will be executed as first step before input validation, before guard and the function itself. * This will change the type of input message payload and input message parameter. * @param transformInputSchema Input payload validation schema * @param transformParameterSchema Input parameter validation schema * @param transformFunction Transform input function * @param inputContentType optional the content type of payload * @param inputContentEncoding optional the content encoding * @returns CommandDefinitionBuilder */ setTransformInput(transformInputSchema: TransformInputPayloadSchema, transformParameterSchema: TransformInputParamsSchema, transformFunction: CommandTransformInputHook, InferIn, Infer, Infer, InferIn, InferIn, C['Resources']>, inputContentType?: ContentType, inputContentEncoding?: string): CommandDefinitionBuilder>; /** * Return the transform input function * @returns the input transform function if defined */ getTransformInputFunction(): CommandTransformInputHook, InferIn, Infer, Infer, InferIn, InferIn, C["Resources"]> | undefined; /** * Set a transform output hook which will encode or transform the response payload. * Will be executed at very last step after function execution, output validation and after guard hooks. * This will change the type of output message payload. * @param transformOutputSchema The output validation schema * @param transformFunction Transform output function * @param outputContentType optional the content type of payload * @param outputContentEncoding optional the content encoding * @returns CommandDefinitionBuilder */ setTransformOutput(transformOutputSchema: TransformOutputSchema, transformFunction: CommandTransformOutputHook, GetMessageParamsType, Infer, Infer, InferIn, C['Resources']>, outputContentType?: ContentType, outputContentEncoding?: string): CommandDefinitionBuilder>; /** * Return the transform output function * @returns the transform output function if defined */ getTransformOutputFunction(): CommandTransformOutputHook, GetMessageParamsType, Infer, Infer, InferIn, C["Resources"]> | undefined; /** * Set one or more before guard hook(s). * If there are multiple before guard hooks, they are executed in parallel * @param beforeGuards Object of key = name of guard, value = function * @returns CommandDefinitionBuilder */ setBeforeGuardHooks(beforeGuards: Record, GetMessageParamsType, Infer, Infer, C['Resources'], C['Invokes'], C['StreamInvokes'], C['EmitList'], C['QueueInvokes']>>): this; /** * Get the before guard hook for this command. * * @param name The name of the before guard to retrieve. * @returns The before guard hook, or undefined if not found. */ getBeforeGuardHook(name: keyof typeof this.hooks.beforeGuard): CommandBeforeGuardHook, GetMessageParamsType, Infer, Infer, C['Resources'], C['Invokes'], C['StreamInvokes'], C['EmitList'], C['QueueInvokes']>; /** * Set one or more after guard hook(s). * If there are multiple after guard hooks, they are executed in parallel * @param afterGuards Object of key = name of guard, value = function * @returns CommandDefinitionBuilder */ setAfterGuardHooks(afterGuards: Record, GetMessageParamsType, Infer, Infer, Infer, C['Resources'], C['Invokes'], C['StreamInvokes'], C['EmitList'], C['QueueInvokes']>>): this; /** * Returns the after guard hook corresponding to the provided name. * @param name - The name of the hook. * @return The after guard hook, or undefined if not found. */ getAfterGuardHook(name: keyof typeof this.hooks.afterGuard): CommandAfterGuardHook, GetMessageParamsType, Infer, Infer, Infer, C['Resources'], C['Invokes'], C['StreamInvokes'], C['EmitList'], C['QueueInvokes']>; /** * Mark the function to be exposed as http endpoint. * * Api url prefix and service version are prepended automatically * * For exposing a url like: `/api/V1/user/login` simply provide `user/login`as path * * @param method Http method POST, PUT, PATCH, GET, DELETE * @param path The url path * @param contentTypeRequest input content type defaults to application/json * @param contentEncodingRequest input content encoding defaults to utf-8 * @param contentTypeResponse input content type defaults to application/json * @param contentEncodingResponse input content encoding defaults to utf-8 * @returns CommandDefinitionBuilder * * @example * ```ts * command * .exposeAsHttpEndpoint('POST', 'orders') * .setOpenApiSummary('Create an order') * ``` */ exposeAsHttpEndpoint(method: SupportedHttpMethod, path: string, contentTypeRequest?: ContentType, contentEncodingRequest?: string, contentTypeResponse?: ContentType, contentEncodingResponse?: string, options?: HttpExposureOptions): this; /** * enable or disable security for this endpoint * @param enabled Defaults to true if not set means "enable security" * @returns CommandDefinitionBuilder */ enableHttpSecurity(enabled?: boolean): this; /** * enable or disable security for this endpoint * @param disabled Defaults to true if not set meaning "disable security" * @returns CommandDefinitionBuilder * @deprecated Use makeEndpointPublic() instead. */ disableHttpSecurity(disabled?: boolean): this; /** * Mark the endpoint to be public available. * No security check like bearer token or basic auth is required to access the endpoint. * @returns CommandDefinitionBuilder */ makeEndpointPublic(): this; /** * Set the function summary text used for example in openApi documentation * * @example * ```ts * setSummary('Some function summary') * ``` * * @param summary Summary text * @returns CommandDefinitionBuilder */ setOpenApiSummary(summary: string): this; /** * Set the operationId for openApi documentation * @param operationId * @returns CommandDefinitionBuilder */ setOpenApiOperationId(operationId: string): this; private extendWithHttpMetadata; /** * Instruct the event bridge message broker to autoacknowledge commands as soon as they arrive. * This means, a message will not be resent, if the command execution fails unexpected. * * If set to false, the command message will be resent from message broker to eventbridge, if: * - the underlaying message broker supports it * - if the command execution fails unexpected * - if sending of command response failed * * @param acknowledge Enable (true) and disable (false) * @returns CommandDefinition */ adviceAutoacknowledgeMessages(acknowledge?: boolean): this; /** * Mark this command as a short, idempotent schedule target. * * @example * ```ts * command.markSchedulable({ * name: 'refresh-cache', * expression: { kind: 'interval', everyMs: 300_000 }, * }) * ``` */ markSchedulable(options: ScheduleOptions & { name: string; description?: string; }): this; /** * Creates and returns the CommandDefinition used as input for the service. * @returns CommandDefinition */ getDefinition(): Promise, GetMessageParamsType, Infer, Infer, Infer, Infer, InferIn, Infer, InferIn, C["Resources"], C["Invokes"], C["StreamInvokes"], C["EmitList"], CommandDefinitionMetadataBase, C["QueueInvokes"]>>>; /** * Required: Set the function implementation. * The types should be automatically set as soon as schemas previously defined. * As the function will be a a function of a service class you need to implement as function declaration. * Arrow functions do not have access to the `this` scope. * * @example * ```ts * async function (context, payload, parameter) { * * return `the result output payload` * } * ``` * @param fn the function implementation * @returns CommandDefinitionBuilder */ setCommandFunction(fn: CommandFunction, GetMessageParamsType, Infer, Infer, InferIn, C['Resources'], C['Invokes'], C['StreamInvokes'], C['EmitList'], C['QueueInvokes']>): this; /** * Get the function implementation including input and output validation. * Also, before hooks are triggered during execution. * Before guards can be optional overwritten by optional input parameter. * * * @param input Overwrite beforeGuards * @returns the function */ getCommandFunction(input?: { beforeGuards?: Partial; }): CommandFunction, GetMessageParamsType, InferIn, InferIn, InferIn, C['Resources'], C['Invokes'], C['StreamInvokes'], C['EmitList'], C['QueueInvokes']>; /** * Get the function implementation without input and output validation. * No hooks are triggered during execution. * * @returns the function */ getCommandFunctionPlain(): CommandFunction, GetMessageParamsType, Infer, Infer, InferIn, C['Resources'], C['Invokes'], C['StreamInvokes'], C['EmitList'], C['QueueInvokes']>; /** * Returns a mocked transform function context, which can be used in unit tests. * * @param input * @returns a mocked transform function context */ getCommandTransformContextMock(input: { payload: GetMessagePayloadType; parameter: GetMessageParamsType; resources?: Partial; sandbox?: SinonSandbox; }): { mock: { logger: import("../index.js").Logger; metrics: import("../index.js").PuristaMetricContext; wrapInSpan: (name: string, opts: import("@opentelemetry/api").SpanOptions, fn: (span: import("@opentelemetry/api").Span) => Promise, context?: import("@opentelemetry/api").Context) => Promise; startActiveSpan: (name: string, opts: import("@opentelemetry/api").SpanOptions, context: import("@opentelemetry/api").Context | undefined, fn: (span: import("@opentelemetry/api").Span) => Promise) => Promise; secrets: { getSecret: import("../index.js").SecretGetterFunction; setSecret: import("../index.js").SecretSetterFunction; removeSecret: import("../index.js").SecretDeleteFunction; }; configs: { getConfig: import("../index.js").ConfigGetterFunction; setConfig: import("../index.js").ConfigSetterFunction; removeConfig: import("../index.js").ConfigDeleteFunction; }; states: { getState: import("../index.js").StateGetterFunction; setState: import("../index.js").StateSetterFunction; removeState: import("../index.js").StateDeleteFunction; }; queue: import("../index.js").QueueContext; message: Readonly<{ id: import("../index.js").EBMessageId; timestamp: number; contentType: ContentType; contentEncoding: string; traceId?: import("../index.js").TraceId; principalId?: import("../index.js").PrincipalId; tenantId?: import("../index.js").TenantId; eventName?: string; otp?: string; sender: import("../index.js").EBMessageSenderAddress; messageType: import("../index.js").EBMessageType.Command; correlationId: import("../index.js").CorrelationId; receiver: import("../index.js").EBMessageAddress; payload: { parameter: GetMessageParamsType; payload: GetMessagePayloadType; }; }>; resources: C["Resources"]; }; stubs: { logger: { info: import("sinon").SinonStub; error: import("sinon").SinonStub; warn: import("sinon").SinonStub; debug: import("sinon").SinonStub; trace: import("sinon").SinonStub; fatal: import("sinon").SinonStub; }; wrapInSpan: import("sinon").SinonStub; startActiveSpan: import("sinon").SinonStub; getSecret: import("sinon").SinonStub; setSecret: import("sinon").SinonStub; removeSecret: import("sinon").SinonStub; getConfig: import("sinon").SinonStub; setConfig: import("sinon").SinonStub; removeConfig: import("sinon").SinonStub; getState: import("sinon").SinonStub; setState: import("sinon").SinonStub; removeState: import("sinon").SinonStub; enqueue: import("sinon").SinonStub; scheduleAt: import("sinon").SinonStub; resources: Partial; }; }; } //# sourceMappingURL=CommandDefinitionBuilder.impl.d.ts.map