import {z} from 'zod' export type ContactGroup = {pk?: number | undefined, url?: string | undefined, created_at?: string | undefined, modified_at?: string | undefined, name: string, sms_list?: Array | undefined, email_list?: Array | undefined, phonecall_list?: Array | undefined, integrations?: Array | undefined, push_notification_profiles?: Array | undefined, oncall_schedule?: string | undefined}; export type CreateApiApiV1ContactsArgs = {body: ContactGroup}; export type CreateApiApiV1ContactsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const contactGroup = z.object({pk: z.number().int().optional(), url: z.string().optional(), created_at: z.string().optional(), modified_at: z.string().optional(), name: z.string().max(100), sms_list: z.array(z.string()).optional(), email_list: z.array(z.string()).optional(), phonecall_list: z.array(z.string()).optional(), integrations: z.array(z.string()).optional(), push_notification_profiles: z.array(z.string()).optional(), oncall_schedule: z.string().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 a new contact * * POST /api/v1/contacts/ */ export const createApiApiV1Contacts = async (args: CreateApiApiV1ContactsArgs, options: CreateApiApiV1ContactsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/contacts/${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(contactGroup, await res.json()) };