import {z} from 'zod' export type PartnerToken = string; export type OrganizationToken = string; export type CustomFields = Record | null; export type Id = number; export type CreateApiContactsArgs = {'Partner-Token'?: PartnerToken | undefined, 'Organization-Token'?: OrganizationToken | undefined, body: {first_name: string, middle_name?: string | undefined, last_name?: string | undefined, birth_date?: string | undefined, group_hierarchy?: (string | null) | undefined, email?: string | undefined, mobile_phone_number?: string | undefined, home_phone_number?: string | undefined, work_phone_number?: string | undefined, other_phone_number?: string | undefined, street_address?: string | undefined, street_address_line_2?: string | undefined, city?: string | undefined, region?: string | undefined, postal_code?: string | undefined, country?: string | undefined, employee_number?: string | undefined, job_title?: string | undefined, start_date?: string | undefined, leave_date?: string | undefined, vehicle_operator?: boolean | undefined, license_number?: string | undefined, license_class?: string | undefined, license_state?: string | undefined, hourly_labor_rate?: number | undefined, custom_fields?: CustomFields | undefined, group_id?: Id | undefined, license_expiration?: string | undefined, employee?: boolean | undefined, technician?: boolean | undefined, user_access?: boolean | undefined, account_membership_attributes?: ({user_type?: ('admin' | 'regular') | undefined, role_id?: number | undefined, vehicles_record_set_id?: number | undefined, contacts_record_set_id?: number | undefined, equipments_record_set_id?: number | undefined, inspection_forms_record_set_id?: number | undefined, parts_record_set_id?: number | undefined, role_name?: string | undefined, vehicles_record_set_name?: string | undefined, contacts_record_set_name?: string | undefined, equipments_record_set_name?: string | undefined, inspection_forms_record_set_name?: string | undefined, parts_record_set_name?: string | undefined, admin_role_attributes?: ({administration_access?: ('full' | 'some' | 'none') | undefined, manage_account_settings?: boolean | undefined, manage_subscription?: boolean | undefined, export_account_data?: boolean | undefined, manage_groups?: boolean | undefined, manage_custom_object_templates?: boolean | undefined, manage_workflows?: boolean | undefined, manage_webhooks?: boolean | undefined, manage_integrations?: boolean | undefined, manage_integration_links?: boolean | undefined, manage_saml_connectors?: boolean | undefined, manage_security_settings?: boolean | undefined, user_management_access?: ('full' | 'some' | 'none') | undefined, manage_users?: boolean | undefined}) | undefined, user_attributes?: ({email?: string | undefined, username?: string | undefined, password?: string | undefined}) | undefined}) | undefined, default_image_attributes?: ({file_url?: string | undefined, default?: boolean | undefined}) | undefined}}; export type CreateApiContactsOptions = {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 }; /** * Create Contact * * POST /contacts */ export const createApiContacts = async (args: CreateApiContactsArgs, options?: CreateApiContactsOptions) => { const {baseUrl = "https://secure.fleetio.com/api", fetch: fetchFn = fetch, apiKey, accountToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Partner-Token'] != null) headers.set("Partner-Token", String(args['Partner-Token'])) if (args['Organization-Token'] != null) headers.set("Organization-Token", String(args['Organization-Token'])) if (apiKey !== undefined) headers.set("Authorization", apiKey) if (accountToken !== undefined) headers.set("Account-Token", accountToken) const url = `${baseUrl.replace(/\/+$/, '')}/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(contactV2, await res.json()) };