import {z} from 'zod' export type Contact = {cellPhone?: (string | null) | undefined, companyName?: (string | null) | undefined, displayName?: (string | null) | undefined, email?: (string | null) | undefined, firstName?: (string | null) | undefined, function?: (string | null) | undefined, id?: number | undefined, internalId?: (string | null) | undefined, invertedDisplayName?: (string | null) | undefined, lastName?: (string | null) | undefined, phone?: (string | null) | undefined, service?: (string | null) | undefined, title?: (string | null) | undefined}; export type CreateApiV1ContactsArgs = {body: Contact}; export type CreateApiV1ContactsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const contact = z.object({cellPhone: z.string().nullable().optional(), companyName: z.string().nullable().optional(), displayName: z.string().nullable().optional(), email: z.string().nullable().optional(), firstName: z.string().nullable().optional(), function: z.string().nullable().optional(), id: z.number().int().optional(), internalId: z.string().nullable().optional(), invertedDisplayName: z.string().nullable().optional(), lastName: z.string().nullable().optional(), phone: z.string().nullable().optional(), service: z.string().nullable().optional(), title: 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 a new Contact * * POST /v1/contacts */ export const createApiV1Contacts = async (args: CreateApiV1ContactsArgs, options?: CreateApiV1ContactsOptions) => { const {baseUrl = "https://api.altoviz.com", 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(/\/+$/, '')}/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(contact, await res.json()) };