import {z} from 'zod' export type UUID = string; export type CreateApiV0WebhookConfigurationsWebhookConfigurationIdRotateSecretArgs = {webhookConfigurationId: UUID}; export type CreateApiV0WebhookConfigurationsWebhookConfigurationIdRotateSecretOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const uUID = z.string(); export const httpsUrl = z.string(); export const nonNegativeInt = z.number().int().gte(0).lte(9007199254740991); export const createApiV0WebhookConfigurationsWebhookConfigurationIdRotateSecretResponse = z.object({id: uUID, distributorId: uUID, url: httpsUrl, topics: z.array(z.enum(["account.internal.created", "deposit-order.created", "deposit-order.funded", "deposit-order.executed", "deposit-order.canceled", "deposit-order.retargeted", "withdrawal-order.pending", "withdrawal-order.executed", "withdrawal-order.canceled", "transfer-order.created", "transfer-order.executed", "swap-order.created", "swap-order.executed", "swap-order.canceled", "investor.created", "investor.updated", "investor.activated", "investor.made-compliant", "investor.made-non-compliant", "investor.archived", "bank-account.created", "bank-account.validated", "bank-account.invalidated", "bank-account.pending-validation", "bank-account.archived", "investor-invitation.accepted", "yield.credited", "subscription-order.created", "subscription-order.executed", "subscription-order.canceled", "subscription-order.wire-validated", "redemption-order.created", "redemption-order.executed", "redemption-order.canceled"])), signingSecret: z.string(), previousSigningSecret: z.string().nullable(), secretRotatedAt: z.string().nullable(), status: z.enum(["active", "disabled", "circuit-broken"]), consecutiveFailures: nonNegativeInt, createdAt: z.string(), updatedAt: z.string()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Rotate the signing secret * * POST /v0/webhook-configurations/{webhookConfigurationId}/rotate-secret */ export const createApiV0WebhookConfigurationsWebhookConfigurationIdRotateSecret = async (args: CreateApiV0WebhookConfigurationsWebhookConfigurationIdRotateSecretArgs, options?: CreateApiV0WebhookConfigurationsWebhookConfigurationIdRotateSecretOptions) => { const {baseUrl = "https://distributor-api.spiko.io", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v0/webhook-configurations/${encodeURIComponent(String(args.webhookConfigurationId))}/rotate-secret${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiV0WebhookConfigurationsWebhookConfigurationIdRotateSecretResponse, await res.json()) };