/* * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT. */ import { NovuCore } from "../core.js"; import { encodeJSON, encodeSimple } from "../lib/encodings.js"; import { matchStatusCode } from "../lib/http.js"; import * as M from "../lib/matchers.js"; import { compactMap } from "../lib/primitives.js"; import { safeParse } from "../lib/schemas.js"; import { RequestOptions } from "../lib/sdks.js"; import { extractSecurity, resolveGlobalSecurity } from "../lib/security.js"; import { pathToFunc } from "../lib/url.js"; import * as components from "../models/components/index.js"; import { ConnectionError, InvalidRequestError, RequestAbortedError, RequestTimeoutError, UnexpectedClientError, } from "../models/errors/httpclienterrors.js"; import * as errors from "../models/errors/index.js"; import { NovuError } from "../models/errors/novuerror.js"; import { ResponseValidationError } from "../models/errors/responsevalidationerror.js"; import { SDKValidationError } from "../models/errors/sdkvalidationerror.js"; import * as operations from "../models/operations/index.js"; import { APICall, APIPromise } from "../types/async.js"; import { Result } from "../types/fp.js"; /** * Trigger event * * @remarks * * Trigger event is the main (and only) way to send notifications to subscribers. The trigger identifier is used to match the particular workflow associated with it. Maximum number of recipients can be 100. Additional information can be passed according the body interface below. * To prevent duplicate triggers, you can optionally pass a **transactionId** in the request body. If the same **transactionId** is used again, the trigger will be ignored. The retention period depends on your billing tier. */ export function trigger( client: NovuCore, triggerEventRequestDto: components.TriggerEventRequestDto, idempotencyKey?: string | undefined, options?: RequestOptions, ): APIPromise< Result< operations.EventsControllerTriggerResponse, | errors.PayloadValidationExceptionDto | errors.ErrorDto | errors.ValidationErrorDto | NovuError | ResponseValidationError | ConnectionError | RequestAbortedError | RequestTimeoutError | InvalidRequestError | UnexpectedClientError | SDKValidationError > > { return new APIPromise($do( client, triggerEventRequestDto, idempotencyKey, options, )); } async function $do( client: NovuCore, triggerEventRequestDto: components.TriggerEventRequestDto, idempotencyKey?: string | undefined, options?: RequestOptions, ): Promise< [ Result< operations.EventsControllerTriggerResponse, | errors.PayloadValidationExceptionDto | errors.ErrorDto | errors.ValidationErrorDto | NovuError | ResponseValidationError | ConnectionError | RequestAbortedError | RequestTimeoutError | InvalidRequestError | UnexpectedClientError | SDKValidationError >, APICall, ] > { const input: operations.EventsControllerTriggerRequest = { triggerEventRequestDto: triggerEventRequestDto, idempotencyKey: idempotencyKey, }; const parsed = safeParse( input, (value) => operations.EventsControllerTriggerRequest$outboundSchema.parse(value), "Input validation failed", ); if (!parsed.ok) { return [parsed, { status: "invalid" }]; } const payload = parsed.value; const body = encodeJSON("body", payload.TriggerEventRequestDto, { explode: true, }); const path = pathToFunc("/v1/events/trigger")(); const headers = new Headers(compactMap({ "Content-Type": "application/json", Accept: "application/json", "idempotency-key": encodeSimple( "idempotency-key", payload["idempotency-key"], { explode: false, charEncoding: "none" }, ), })); const secConfig = await extractSecurity(client._options.secretKey); const securityInput = secConfig == null ? {} : { secretKey: secConfig }; const requestSecurity = resolveGlobalSecurity(securityInput); const context = { options: client._options, baseURL: options?.serverURL ?? client._baseURL ?? "", operationID: "EventsController_trigger", oAuth2Scopes: null, resolvedSecurity: requestSecurity, securitySource: client._options.secretKey, retryConfig: options?.retries || client._options.retryConfig || { strategy: "backoff", backoff: { initialInterval: 1000, maxInterval: 30000, exponent: 1.5, maxElapsedTime: 3600000, }, retryConnectionErrors: true, } || { strategy: "none" }, retryCodes: options?.retryCodes || ["408", "409", "429", "5XX"], }; const requestRes = client._createRequest(context, { security: requestSecurity, method: "POST", baseURL: options?.serverURL, path: path, headers: headers, body: body, userAgent: client._options.userAgent, timeoutMs: options?.timeoutMs || client._options.timeoutMs || -1, }, options); if (!requestRes.ok) { return [requestRes, { status: "invalid" }]; } const req = requestRes.value; const doResult = await client._do(req, { context, isErrorStatusCode: (statusCode: number) => matchStatusCode({ status: statusCode } as Response, ["4XX", "5XX"]), retryConfig: context.retryConfig, retryCodes: context.retryCodes, }); if (!doResult.ok) { return [doResult, { status: "request-error", request: req }]; } const response = doResult.value; const responseFields = { HttpMeta: { Response: response, Request: req }, }; const [result] = await M.match< operations.EventsControllerTriggerResponse, | errors.PayloadValidationExceptionDto | errors.ErrorDto | errors.ValidationErrorDto | NovuError | ResponseValidationError | ConnectionError | RequestAbortedError | RequestTimeoutError | InvalidRequestError | UnexpectedClientError | SDKValidationError >( M.json(201, operations.EventsControllerTriggerResponse$inboundSchema, { hdrs: true, key: "Result", }), M.jsonErr(400, errors.PayloadValidationExceptionDto$inboundSchema, { hdrs: true, }), M.jsonErr(414, errors.ErrorDto$inboundSchema), M.jsonErr( [401, 403, 404, 405, 409, 413, 415], errors.ErrorDto$inboundSchema, { hdrs: true }, ), M.jsonErr(422, errors.ValidationErrorDto$inboundSchema, { hdrs: true }), M.fail(429), M.jsonErr(500, errors.ErrorDto$inboundSchema, { hdrs: true }), M.fail(503), M.fail("4XX"), M.fail("5XX"), )(response, req, { extraFields: responseFields }); if (!result.ok) { return [result, { status: "complete", request: req, response }]; } return [result, { status: "complete", request: req, response }]; }