import {z} from 'zod' export type CreateApiV2ContactPhoneNumbersContactIdArgs = {contactId: string, 'Idempotency-Key': string, body: {phone_number: string, label?: string | undefined, ranking?: number | undefined, country_code?: string | undefined, phone_number_sanitized?: string | undefined}}; export type CreateApiV2ContactPhoneNumbersContactIdOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV2ContactPhoneNumbersContactIdResponse = z.object({data: z.object({phoneNumber: z.object({id: z.string(), contact_id: z.string(), user_id: z.string().optional(), workspace_id: z.string().nullable().optional(), phone_number: z.string(), label: z.string().nullable().optional(), ranking: z.number().int().gte(-9007199254740991).lte(9007199254740991), country_code: z.string().nullable().optional(), phone_number_sanitized: z.string().nullable().optional(), created_at: z.union([z.string(), z.unknown()]), updated_at: z.union([z.string(), z.unknown()])})})}); 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 contact phone number (v2) * * POST /v2/contact-phone-numbers/{contactId} */ export const createApiV2ContactPhoneNumbersContactId = async (args: CreateApiV2ContactPhoneNumbersContactIdArgs, options?: CreateApiV2ContactPhoneNumbersContactIdOptions) => { const {baseUrl = "https://api.prod.getdex.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 (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/contact-phone-numbers/${encodeURIComponent(String(args.contactId))}${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(createApiV2ContactPhoneNumbersContactIdResponse, await res.json()) };