import {z} from 'zod' export type RevAppComponentsLink = {rel?: string | undefined, href?: string | undefined}; export type RevAppModelsFieldValue = {field_id?: number | undefined, label?: string | undefined, value?: string | undefined, links?: Array | undefined}; export type RevAppModelsContactAddress = {line_1?: string | undefined, line_2?: string | undefined, city?: string | undefined, state_or_province?: string | undefined, postal_code?: string | undefined, postal_code_extension?: string | undefined, links?: Array | undefined}; export type RevAppModelsContact = {contact_id?: number | undefined, name: string, email?: string | undefined, company?: string | undefined, customer_id?: number | undefined, agent_id?: number | undefined, created_date?: string | undefined, title?: string | undefined, position?: string | undefined, fax?: string | undefined, phone?: string | undefined, comments?: string | undefined, mobile?: string | undefined, contact_type_id?: number | undefined, fields?: Array | undefined, address?: RevAppModelsContactAddress | undefined, links?: Array | undefined}; export type CreateApiV1ContactsArgs = {body: RevAppModelsContact}; export type CreateApiV1ContactsOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const revWebAPIModelsCreate = z.object({ok: z.boolean().optional(), id: z.number().int().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 contact * * POST /v1/Contacts */ export const createApiV1Contacts = async (args: CreateApiV1ContactsArgs, options?: CreateApiV1ContactsOptions) => { const {baseUrl = "https://restapi.rev.io", fetch: fetchFn = fetch, basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (basic !== undefined) headers.set('authorization', `Basic ${btoa(`${basic.username}:${basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/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(revWebAPIModelsCreate, await res.json()) };