import {z} from 'zod' export type Address = {phoneNumber?: string | undefined, line1?: string | undefined, line2?: string | undefined, postalCode?: string | undefined, city?: string | undefined, state?: string | undefined, country?: string | undefined}; export type CreateInvoiceGroupRequest = {name: string, email: string, dailyInvoiceConsolidation: boolean, accountIds: Array, netTermDays?: number | undefined, address: Address}; export type CreateApiInvoiceGroupsArgs = {body: CreateInvoiceGroupRequest}; export type CreateApiInvoiceGroupsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const address = z.object({phoneNumber: z.string().optional(), line1: z.string().optional(), line2: z.string().optional(), postalCode: z.string().optional(), city: z.string().optional(), state: z.string().optional(), country: z.string().optional()}); export const invoiceGroups = z.object({id: z.string(), name: z.string(), email: z.string().max(320), dailyInvoiceConsolidation: z.boolean(), netTermDays: z.number().int().optional(), invoiceCurrency: z.string(), billingAddress: address, accountsCount: z.number().int().gte(0), createdAt: z.string(), updatedAt: 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 }; /** * Create an invoice group * * POST /invoice_groups */ export const createApiInvoiceGroups = async (args: CreateApiInvoiceGroupsArgs, options?: CreateApiInvoiceGroupsOptions) => { const {baseUrl = "https://api.togai.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(/\/+$/, '')}/invoice_groups${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(invoiceGroups, await res.json()) };