import {z} from 'zod' export type SubscriptionTopicStatus = 'OPTED_OUT' | 'OPTED_IN' | 'REQUIRED'; export type ChannelClassification = 'direct_message' | 'email' | 'push' | 'sms' | 'webhook' | 'inbox'; export type AllowedPreference = 'snooze' | 'channel_preferences'; export type WorkspacePreferenceTopicCreateRequest = {name: string, description?: (string | null) | undefined, default_status: SubscriptionTopicStatus, routing_options?: (Array | null) | undefined, allowed_preferences?: (Array | null) | undefined, include_unsubscribe_header?: (boolean | null) | undefined, topic_data?: ({[key: string]: unknown} | null) | undefined}; export type CreateApiPreferencesSectionsSectionIdTopicsArgs = {'Idempotency-Key'?: string | undefined, 'x-idempotency-expiration'?: string | undefined, section_id: string, body: WorkspacePreferenceTopicCreateRequest}; export type CreateApiPreferencesSectionsSectionIdTopicsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const subscriptionTopicStatus = z.enum(["OPTED_OUT", "OPTED_IN", "REQUIRED"]); export const channelClassification = z.enum(["direct_message", "email", "push", "sms", "webhook", "inbox"]); export const allowedPreference = z.enum(["snooze", "channel_preferences"]); export const workspacePreferenceTopicGetResponse = z.object({id: z.string(), name: z.string(), description: z.string().nullable().optional(), default_status: subscriptionTopicStatus, routing_options: z.array(channelClassification), allowed_preferences: z.array(allowedPreference), include_unsubscribe_header: z.boolean(), topic_data: z.record(z.string(), z.unknown()), created: z.string(), creator: z.string().nullable().optional(), updated: z.string(), 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 }; /** * Create Topic in Section * * POST /preferences/sections/{section_id}/topics */ export const createApiPreferencesSectionsSectionIdTopics = async (args: CreateApiPreferencesSectionsSectionIdTopicsArgs, options?: CreateApiPreferencesSectionsSectionIdTopicsOptions) => { 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 (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (args['x-idempotency-expiration'] != null) headers.set("x-idempotency-expiration", String(args['x-idempotency-expiration'])) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/preferences/sections/${encodeURIComponent(String(args.section_id))}/topics${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(workspacePreferenceTopicGetResponse, await res.json()) };