import {z} from 'zod' export type GetApiContactsIdArgs = {id: string, 'expand[]'?: Array | undefined}; export type GetApiContactsIdOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, accountToken?: string | undefined, fetch?: typeof fetch | undefined}; export const id = z.number().int().gte(1); export const createdAt = z.string(); export const updatedAt = z.string(); export const customFields = z.object({}).nullable(); export const nullableId = z.number().int().gte(1).nullable(); export const contactV2 = z.object({id: id, created_at: createdAt.optional(), updated_at: updatedAt.optional(), name: z.string().nullable(), first_name: z.string().nullable(), middle_name: z.string().nullable(), group_id: z.number().int().nullable(), group_name: z.string().nullable(), last_name: z.string().nullable(), images_count: z.number().int(), documents_count: z.number().int(), comments_count: z.number().int(), email: z.string().nullable().optional(), technician: z.boolean().nullable().optional(), vehicle_operator: z.boolean().nullable().optional(), employee: z.boolean().nullable().optional(), birth_date: z.string().nullable().optional(), street_address: z.string().nullable().optional(), street_address_line_2: z.string().nullable().optional(), city: z.string().nullable().optional(), region: z.string().nullable().optional(), postal_code: z.string().nullable().optional(), country: z.string().nullable().optional(), employee_number: z.string().nullable().optional(), job_title: z.string().max(255).nullable().optional(), license_class: z.string().nullable().optional(), license_number: z.string().nullable().optional(), license_state: z.string().nullable().optional(), license_expiration: z.string().nullable().optional(), home_phone_number: z.string().nullable().optional(), mobile_phone_number: z.string().nullable().optional(), work_phone_number: z.string().nullable().optional(), other_phone_number: z.string().nullable().optional(), start_date: z.string().nullable().optional(), leave_date: z.string().nullable().optional(), hourly_labor_rate: z.number().nullable().optional(), custom_fields: customFields.optional(), default_image_url: z.string().nullable(), account_membership_id: nullableId.optional(), archived_at: z.string().nullable().optional(), user: z.object({id: nullableId.optional(), email: z.string().nullable().optional(), username: z.string().nullable().optional(), time_zone: z.string().nullable().optional(), user_type: z.enum(["admin", "regular", "owner"]).nullable().optional(), archived_at: z.string().nullable().optional(), role: z.object({id: nullableId.optional(), name: z.string().nullable().optional()}).optional(), vehicles_record_set: z.object({id: nullableId.optional(), name: z.string().nullable().optional()}).optional(), equipments_record_set: z.object({id: nullableId.optional(), name: z.string().nullable().optional()}).optional(), contacts_record_set: z.object({id: nullableId.optional(), name: z.string().nullable().optional()}).optional(), parts_record_set: z.object({id: nullableId.optional(), name: z.string().nullable().optional()}).optional(), inspection_forms_record_set: z.object({id: nullableId.optional(), name: z.string().nullable().optional()}).optional()}).nullable()}).nullable(); 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 }; /** * Retrieve Contact * * GET /contacts/{id} */ export const getApiContactsId = async (args: GetApiContactsIdArgs, options?: GetApiContactsIdOptions) => { const {baseUrl = "https://secure.fleetio.com/api", fetch: fetchFn = fetch, apiKey, accountToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args['expand[]'] ?? []) query.append("expand[]", String(item)) if (apiKey !== undefined) headers.set("Authorization", apiKey) if (accountToken !== undefined) headers.set("Account-Token", accountToken) const url = `${baseUrl.replace(/\/+$/, '')}/contacts/${encodeURIComponent(String(args.id))}${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(contactV2, await res.json()) };