import {z} from 'zod' export type JurisdictionInput = {id: string}; export type OrderCreateWithProduct = {company: {id: string}, product: {id: string}, jurisdictions: Array, linked_to_order_id?: (string | null) | undefined, mark_ordered?: boolean | undefined}; export type OrderCreateWithPreconfiguredProduct = {company: {id: string}, preconfigured_product: {id: string}, mark_ordered?: boolean | undefined}; export type CreateApiOrdersArgs = {body: OrderCreateWithProduct | OrderCreateWithPreconfiguredProduct}; export type CreateApiOrdersOptions = {baseUrl?: string | undefined, Oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const jurisdiction = z.object({id: z.string(), name: z.string()}); export const businessStructure = z.object({id: z.string(), name: z.string()}); export const address = z.object({address_line_1: z.string(), address_line_2: z.string().nullable().optional(), locality: z.string(), postal_code: z.string(), administrative_area: jurisdiction.optional(), country: jurisdiction.optional()}); export const irsTaxClassification = z.object({id: z.string(), name: z.string()}); export const accountingMethod = z.object({id: z.string(), name: z.string()}); export const user = z.object({id: z.string().optional(), first_name: z.string().optional(), middle_name: z.string().nullable().optional(), last_name: z.string().optional(), full_name: z.string().optional(), email: z.string().optional()}); export const company = z.object({id: z.string(), legal_name: z.string(), formation_date: z.string().nullable().optional(), fiscal_year_end: z.string().nullable().optional(), phone: z.string().nullable().optional(), toll_free_phone: z.string().nullable().optional(), email: z.string().nullable().optional(), fax: z.string().nullable().optional(), no_fax: z.boolean().optional(), website: z.string().nullable().optional(), no_website: z.boolean().optional(), mission: z.string().nullable().optional(), purpose_statement: z.string().nullable().optional(), primary_product_or_service: z.string().nullable().optional(), ein: z.string().nullable().optional(), domicile: jurisdiction.optional(), business_structure: businessStructure.optional(), principal_address: address.optional(), mailing_address: address.optional(), tax_classification: irsTaxClassification.optional(), accounting_method: accountingMethod.optional(), signers: z.array(user).optional()}); export const product = z.object({id: z.string(), name: z.string()}); export const order = z.object({id: z.string().optional(), linked_to_order_id: z.string().nullable().optional(), company: company, product: product, jurisdictions: z.array(jurisdiction)}); export const createApiOrdersResponse = z.object({data: order.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 a new order * * POST /orders */ export const createApiOrders = async (args: CreateApiOrdersArgs, options?: CreateApiOrdersOptions) => { const {baseUrl = "https://www.harborcompliance.com/api/v1", fetch: fetchFn = fetch, Oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Oauth2 !== undefined) headers.set('authorization', `Bearer ${Oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/orders${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(createApiOrdersResponse, await res.json()) };