import {z} from 'zod' export type SubscriptionTopicStatus = 'OPTED_OUT' | 'OPTED_IN' | 'REQUIRED'; export type ChannelClassification = 'direct_message' | 'email' | 'push' | 'sms' | 'webhook' | 'inbox'; export type SubscriptionTopic = {status: SubscriptionTopicStatus, has_custom_routing?: (boolean | null) | undefined, custom_routing?: (Array | null) | undefined}; export type DefaultPreferences = {items?: (Array | null) | undefined}; export type UpdateApiTenantsTenantIdArgs = {tenant_id: string, body: {name: string, parent_tenant_id?: (string | null) | undefined, default_preferences?: DefaultPreferences | undefined, properties?: ({[key: string]: unknown} | null) | undefined, user_profile?: ({[key: string]: unknown} | null) | undefined, brand_id?: (string | null) | undefined}}; export type UpdateApiTenantsTenantIdOptions = {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 subscriptionTopic = z.object({status: subscriptionTopicStatus, has_custom_routing: z.boolean().nullable().optional(), custom_routing: z.array(channelClassification).nullable().optional()}); export const defaultPreferences = z.object({items: z.array(subscriptionTopic).nullable().optional()}); export const tenant = z.object({id: z.string(), name: z.string(), parent_tenant_id: z.string().nullable().optional(), default_preferences: defaultPreferences.optional(), properties: z.record(z.string(), z.unknown()).nullable().optional(), user_profile: z.record(z.string(), z.unknown()).nullable().optional(), brand_id: 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 or Replace a Tenant * * PUT /tenants/{tenant_id} */ export const updateApiTenantsTenantId = async (args: UpdateApiTenantsTenantIdArgs, options?: UpdateApiTenantsTenantIdOptions) => { 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(/\/+$/, '')}/tenants/${encodeURIComponent(String(args.tenant_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(tenant, await res.json()) };