import {z} from 'zod' export type MessageRoutingMethod = 'all' | 'single'; export type MessageRoutingChannel = string | MessageRouting; export type MessageRouting = {method: MessageRoutingMethod, channels: Array}; export type RoutingMethod = 'all' | 'single'; export type Timeouts = {provider?: (number | null) | undefined, channel?: (number | null) | undefined}; export type UTM = {source?: (string | null) | undefined, medium?: (string | null) | undefined, campaign?: (string | null) | undefined, term?: (string | null) | undefined, content?: (string | null) | undefined}; export type ChannelMetadata = {utm?: UTM | undefined}; export type Channel = {brand_id?: (string | null) | undefined, providers?: (Array | null) | undefined, routing_method?: RoutingMethod | undefined, if?: (string | null) | undefined, timeouts?: Timeouts | undefined, override?: ({[key: string]: unknown} | null) | undefined, metadata?: ChannelMetadata | undefined}; export type MessageChannels = {[key: string]: Channel}; export type Metadata = {utm?: UTM | undefined}; export type MessageProvidersType = {override?: ({[key: string]: unknown} | null) | undefined, if?: (string | null) | undefined, timeouts?: (number | null) | undefined, metadata?: Metadata | undefined}; export type MessageProviders = {[key: string]: MessageProvidersType}; export type RoutingStrategyReplaceRequest = {name: string, description?: (string | null) | undefined, tags?: (Array | null) | undefined, routing: MessageRouting, channels?: MessageChannels | undefined, providers?: MessageProviders | undefined}; export type UpdateApiRoutingStrategiesIdArgs = {id: string, body: RoutingStrategyReplaceRequest}; export type UpdateApiRoutingStrategiesIdOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const messageRoutingMethod = z.enum(["all", "single"]); export const messageRoutingChannel = z.union([z.string(), z.lazy(() => messageRouting)]); export const messageRouting: z.ZodType = z.object({method: messageRoutingMethod, channels: z.array(messageRoutingChannel)}); export const routingMethod = z.enum(["all", "single"]); export const timeouts = z.object({provider: z.number().int().nullable().optional(), channel: z.number().int().nullable().optional()}); export const uTM = z.object({source: z.string().nullable().optional(), medium: z.string().nullable().optional(), campaign: z.string().nullable().optional(), term: z.string().nullable().optional(), content: z.string().nullable().optional()}); export const channelMetadata = z.object({utm: uTM.optional()}); export const channel = z.object({brand_id: z.string().nullable().optional(), providers: z.array(z.string()).nullable().optional(), routing_method: routingMethod.optional(), if: z.string().nullable().optional(), timeouts: timeouts.optional(), override: z.record(z.string(), z.unknown()).nullable().optional(), metadata: channelMetadata.optional()}); export const messageChannels = z.record(z.string(), channel); export const metadata = z.object({utm: uTM.optional()}); export const messageProvidersType = z.object({override: z.record(z.string(), z.unknown()).nullable().optional(), if: z.string().nullable().optional(), timeouts: z.number().int().nullable().optional(), metadata: metadata.optional()}); export const messageProviders = z.record(z.string(), messageProvidersType); export const routingStrategyGetResponse = z.object({id: z.string(), name: z.string(), description: z.string().nullable().optional(), tags: z.array(z.string()).nullable().optional(), routing: messageRouting, channels: messageChannels, providers: messageProviders, created: z.number().int(), creator: z.string(), updated: z.number().int().nullable().optional(), updater: z.string().nullable().optional()}); 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 }; /** * Replace Routing Strategy * * PUT /routing-strategies/{id} */ export const updateApiRoutingStrategiesId = async (args: UpdateApiRoutingStrategiesIdArgs, options?: UpdateApiRoutingStrategiesIdOptions) => { const {baseUrl = "https://api.courier.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/routing-strategies/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(routingStrategyGetResponse, await res.json()) };