import {z} from 'zod' export type UserStatementsQueueExchangeRate = {currencyCode?: string | undefined, exchangeRate?: number | undefined}; export type UserStatementsQueueInputDTO = {statements?: Array | undefined, exchangeRates?: Array | undefined, notes?: string | undefined, statementRunId?: number | undefined, statementName?: string | undefined, date?: string | undefined, creationDate?: string | undefined, createdByEmail?: string | undefined, status?: number | undefined, filePath?: string | undefined, totalNetExchanged?: number | undefined, emailSent?: boolean | undefined, includedReportCount?: number | undefined, detachedReportCount?: number | undefined, detachedReportValue?: number | undefined}; export type CreateApiFinanceSalereportRunArgs = {body: UserStatementsQueueInputDTO}; export type CreateApiFinanceSalereportRunOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const userStatementsQueueDTO = z.object({statementRunId: z.number().int().optional(), statementName: z.string().optional(), date: z.string().optional(), creationDate: z.string().optional(), createdByEmail: z.string().optional(), notes: z.string().optional(), status: z.number().int().optional(), filePath: z.string().optional(), totalNetExchanged: z.number().optional(), emailSent: z.boolean().optional(), includedReportCount: z.number().int().optional(), detachedReportCount: z.number().int().optional(), detachedReportValue: z.number().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 }; /** * POST /finance/salereport/run */ export const createApiFinanceSalereportRun = async (args: CreateApiFinanceSalereportRunArgs, options?: CreateApiFinanceSalereportRunOptions) => { const {baseUrl = "http://api.revelator.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/finance/salereport/run${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(userStatementsQueueDTO, await res.json()) };