import {z} from 'zod' export type AccountCode = string; export type CountryCode = string; export type Address = {line1: string, line2?: (string | null) | undefined, city: string, state?: (string | null) | undefined, zip_code: string, country: CountryCode}; export type ExternalReference = {type: string, id: string, url?: string | undefined}; export type FieldAssignments = Array<{field_id: string, field_value_id: string}>; export type VendorRequest = {name: string, account_code?: AccountCode | undefined, address?: Address | undefined, email?: string | undefined, payment_terms?: number | undefined, external_references?: Array | undefined, ten_ninety_nine_eligible?: boolean | undefined, tax_id?: string | undefined, fields?: FieldAssignments | undefined}; export type CreateApiVendorsArgs = {body: VendorRequest}; export type CreateApiVendorsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const accountCode = z.string(); export const countryCode = z.string(); export const address = z.object({line1: z.string(), line2: z.string().nullable().optional(), city: z.string(), state: z.string().nullable().optional(), zip_code: z.string(), country: countryCode}); export const externalReference = z.object({type: z.string(), id: z.string(), url: z.string().optional()}); export const fieldAssignments = z.array(z.object({field_id: z.string(), field_value_id: z.string()})); export const vendorResponse = z.object({name: z.string(), account_code: accountCode.optional(), address: address.optional(), email: z.string().optional(), payment_terms: z.number().int().gte(0).lte(180).optional(), external_references: z.array(externalReference).optional(), ten_ninety_nine_eligible: z.boolean().optional(), tax_id: z.string().optional(), fields: fieldAssignments.optional(), id: z.string(), updated_at: z.string()}); 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 }; /** * Creates a vendor * * POST /vendors */ export const createApiVendors = async (args: CreateApiVendorsArgs, options?: CreateApiVendorsOptions) => { const {baseUrl = "https://api.rillet.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 (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/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(vendorResponse, await res.json()) };