import {z} from 'zod' export type GetApiTenantsArgs = {parent_tenant_id?: (string | null) | undefined, limit?: (number | null) | undefined, cursor?: (string | null) | undefined}; export type GetApiTenantsOptions = {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 tenantListResponse = z.object({cursor: z.string().nullable().optional(), has_more: z.boolean(), items: z.array(tenant), next_url: z.string().nullable().optional(), url: z.string(), type: z.literal("list")}); 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 }; /** * Get a List of Tenants * * GET /tenants */ export const getApiTenants = async (args: GetApiTenantsArgs, options?: GetApiTenantsOptions) => { const {baseUrl = "https://api.courier.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.parent_tenant_id != null) query.append("parent_tenant_id", String(args.parent_tenant_id)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/tenants${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(tenantListResponse, await res.json()) };