import {z} from 'zod' export type RefProgram = string; export type CreateApiSplitLotsAccountAccountIdentifierActionSearchArgs = {AccountIdentifier: string, page?: number | undefined, size?: number | undefined, body: {programs?: Array | undefined, createdDateFrom?: string | undefined, createdDateTo?: string | undefined, counterpartyName?: string | undefined}}; export type CreateApiSplitLotsAccountAccountIdentifierActionSearchOptions = {baseUrl?: string | undefined, bearerToken?: string | undefined, fetch?: typeof fetch | undefined}; export const refCurrency = z.string().min(3); export const price = z.object({currency: refCurrency, amount: z.number().gte(0)}); export const refProgram = z.string(); export const createApiSplitLotsAccountAccountIdentifierActionSearchResponse = z.object({page: z.number().int(), size: z.number().int(), totalPages: z.number().int(), totalElements: z.number().int(), content: z.array(z.object({identifier: z.string().optional(), counterpartyName: z.string().optional(), type: z.string().optional(), status: z.string().optional(), quantity: z.number().int().optional(), taxLotId: z.number().int().optional(), taxLotName: z.string().optional(), sourceTaxLotId: z.number().int().optional(), sourceTaxLotName: z.string().optional(), externalTransferIdentifier: z.string().optional(), createdDate: z.string().optional(), lastUpdatedDate: z.string().optional(), unitPrice: price.optional(), ein: z.string().optional(), program: refProgram.optional()})).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 }; /** * Search split lots * * POST /split-lots/account/{AccountIdentifier}/action/search */ export const createApiSplitLotsAccountAccountIdentifierActionSearch = async (args: CreateApiSplitLotsAccountAccountIdentifierActionSearchArgs, options?: CreateApiSplitLotsAccountAccountIdentifierActionSearchOptions) => { const {baseUrl = "https://sandbox.preprod.connect.xpansiv.com/app/api/v1", fetch: fetchFn = fetch, bearerToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.size != null) query.append("size", String(args.size)) if (bearerToken !== undefined) headers.set('authorization', `Bearer ${bearerToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/split-lots/account/${encodeURIComponent(String(args.AccountIdentifier))}/action/search${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(createApiSplitLotsAccountAccountIdentifierActionSearchResponse, await res.json()) };