import {z} from 'zod' export type GetApiV2InvoicingAccountingSyncInvoicesIdConnectionsArgs = {id: string}; export type GetApiV2InvoicingAccountingSyncInvoicesIdConnectionsOptions = {baseUrl?: string | undefined, Oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const dateTime = z.string().min(20).max(64); export const connection = z.object({platform_name: z.string().min(1).max(64).optional(), last_sync_time: dateTime.optional(), last_sync_status: z.enum(["IN_PROGRESS", "SUCCESS", "FAILED"]).min(1).max(255).optional()}); export const connectionList = z.array(connection); export const connections = z.object({connections: connectionList.optional()}); export const connectionsList = z.array(connections); export const invoiceConnectionDetails = z.object({id: z.string().min(1).max(64).optional(), connection_status: connectionsList.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 }; /** * List Connection Status for an Invoice. * * GET /v2/invoicing/accounting-sync/invoices/{id}/connections */ export const getApiV2InvoicingAccountingSyncInvoicesIdConnections = async (args: GetApiV2InvoicingAccountingSyncInvoicesIdConnectionsArgs, options?: GetApiV2InvoicingAccountingSyncInvoicesIdConnectionsOptions) => { const {baseUrl = "https://api-m.paypal.com", fetch: fetchFn = fetch, Oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (Oauth2 !== undefined) headers.set('authorization', `Bearer ${Oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/invoicing/accounting-sync/invoices/${encodeURIComponent(String(args.id))}/connections${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(invoiceConnectionDetails, await res.json()) };