import {z} from 'zod' export type CustomFields = Record | null; export type PatchApiVendorsIdArgs = {id: string, body: {name?: string | undefined, notes?: (string | null) | undefined, city?: string | undefined, contact_email?: string | undefined, contact_name?: string | undefined, contact_phone?: string | undefined, country?: string | undefined, external_id?: string | undefined, phone?: string | undefined, postal_code?: string | undefined, region?: string | undefined, street_address?: string | undefined, street_address_line_2?: string | undefined, website?: string | undefined, fuel?: boolean | undefined, service?: boolean | undefined, parts?: boolean | undefined, vehicle?: boolean | undefined, custom_fields?: CustomFields | undefined}}; export type PatchApiVendorsIdOptions = {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 labels = z.array(z.object({id: id.optional(), name: z.string().optional()})); export const customFields = z.object({}).nullable(); export const vendor = z.object({id: id, created_at: createdAt.optional(), updated_at: updatedAt.optional(), name: z.string(), phone: z.string().nullable(), street_address: z.string().nullable(), street_address_line_2: z.string().nullable(), city: z.string().nullable(), region: z.string().nullable(), postal_code: z.string().nullable(), country: z.string().nullable(), website: z.string().nullable(), notes: z.string().nullable(), fuel: z.boolean().nullable(), service: z.boolean().nullable(), parts: z.boolean().nullable(), vehicle: z.boolean().nullable(), contact_name: z.string().nullable(), contact_email: z.string().nullable(), contact_phone: z.string().nullable(), latitude: z.number().nullable(), longitude: z.number().nullable(), external_id: z.string().nullable(), archived_at: z.string().nullable(), labels: labels, custom_fields: customFields}).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 }; /** * Update Vendor * * PATCH /vendors/{id} */ export const patchApiVendorsId = async (args: PatchApiVendorsIdArgs, options?: PatchApiVendorsIdOptions) => { 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 (apiKey !== undefined) headers.set("Authorization", apiKey) if (accountToken !== undefined) headers.set("Account-Token", accountToken) const url = `${baseUrl.replace(/\/+$/, '')}/vendors/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(vendor, await res.json()) };