import {z} from 'zod' export type VendorRequest = {name: string, address1?: string | undefined, address2?: string | undefined, city?: string | undefined, state?: string | undefined, postal_code?: string | undefined, country_id?: string | undefined, phone?: string | undefined, email?: string | undefined, website?: string | undefined, custom_value1?: string | undefined, custom_value2?: string | undefined, custom_value3?: string | undefined, custom_value4?: string | undefined, is_deleted?: boolean | undefined, archived_at?: number | undefined, contacts?: (Array<{first_name?: string | undefined, last_name?: string | undefined, email?: string | undefined, phone?: string | undefined, send_email?: boolean | undefined}>) | undefined}; export type CreateApiApiV1VendorsArgs = {'X-API-TOKEN': string, 'X-Requested-With': string, include?: string | undefined, body: VendorRequest}; export type CreateApiApiV1VendorsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const vendorContact = z.object({id: z.string().optional(), user_id: z.string().optional(), vendor_id: z.string().optional(), first_name: z.string().optional(), last_name: z.string().optional(), contact_key: z.string().optional(), confirmation_code: z.string().optional(), phone: z.string().optional(), custom_value1: z.string().optional(), custom_value2: z.string().optional(), custom_value3: z.string().optional(), custom_value4: z.string().optional(), email: z.string().optional(), email_verified_at: z.number().optional(), password: z.string().optional(), is_primary: z.boolean().optional(), created_at: z.number().optional(), updated_at: z.number().optional(), deleted_at: z.number().optional(), send_email: z.boolean().optional(), cc_only: z.boolean().optional(), can_sign: z.boolean().optional()}); export const vendor = z.object({id: z.string().optional(), user_id: z.string().optional(), assigned_user_id: z.string().optional(), contacts: z.array(vendorContact).optional(), name: z.string().optional(), classification: z.string().optional(), website: z.string().optional(), private_notes: z.string().optional(), industry_id: z.string().optional(), size_id: z.string().optional(), address1: z.string().optional(), address2: z.string().optional(), city: z.string().optional(), state: z.string().optional(), postal_code: z.string().optional(), phone: z.string().optional(), country_id: z.string().optional(), currency_id: z.string().optional(), custom_value1: z.string().optional(), custom_value2: z.string().optional(), custom_value3: z.string().optional(), custom_value4: z.string().optional(), vat_number: z.string().optional(), id_number: z.string().optional(), number: z.string().optional(), is_deleted: z.boolean().optional(), language_id: z.string().optional(), vendor_hash: z.string().optional(), transaction_name: z.string().optional(), last_login: z.number().optional(), created_at: z.number().optional(), updated_at: z.number().optional(), display_name: z.string().optional(), is_tax_exempt: z.boolean().optional(), public_notes: z.string().optional(), routing_id: z.string().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 vendor * * POST /api/v1/vendors */ export const createApiApiV1Vendors = async (args: CreateApiApiV1VendorsArgs, options?: CreateApiApiV1VendorsOptions) => { const {baseUrl = "https://demo.invoiceninja.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-API-TOKEN'] != null) headers.set("X-API-TOKEN", String(args['X-API-TOKEN'])) if (args['X-Requested-With'] != null) headers.set("X-Requested-With", String(args['X-Requested-With'])) if (args.include != null) query.append("include", String(args.include)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/vendors${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(vendor, await res.json()) };