import {z} from 'zod' export type SupplierInquiryRequest = {supplierPartyId?: number | undefined, startTime?: string | undefined, endTime?: string | undefined, timeZone?: (string | null) | undefined}; export type CreateApiApiInquiryArgs = {body: SupplierInquiryRequest}; export type CreateApiApiInquiryOptions = {baseUrl: string, token?: string | undefined, fetch?: typeof fetch | undefined}; export const supplier = z.object({alias: z.string().nullable().optional(), name: z.string().nullable().optional(), id: z.number().int().optional()}); export const martSupplier = z.object({mart: z.lazy(() => mart.nullable().optional()), supplier: supplier.nullable().optional(), tableName: z.string().nullable().optional(), id: z.number().int().optional()}); export const mart: z.ZodType = z.object({alias: z.string().nullable().optional(), server: z.string().nullable().optional(), database: z.string().nullable().optional(), username: z.string().nullable().optional(), password: z.string().nullable().optional(), searchTypeId: z.number().int().optional(), audit: z.boolean().optional(), martAttributes: z.array(z.lazy(() => martAttribute)).nullable().optional(), martSuppliers: z.array(martSupplier).nullable().optional(), attributes: z.array(z.string()).nullable().optional(), suppliers: z.array(z.string()).nullable().optional(), id: z.number().int().optional()}); export const attributeType = z.literal(1); export const attribute = z.object({alias: z.string().nullable().optional(), type: attributeType.optional(), id: z.number().int().optional()}); export const martAttribute: z.ZodType = z.object({mart: mart.nullable().optional(), attribute: attribute.nullable().optional(), columnName: z.string().nullable().optional(), dataDictionaryItemId: z.number().int().optional(), id: z.number().int().optional()}); export const supplierInquiryMartAttribute = z.object({supplierInquiry: z.lazy(() => supplierInquiryRecord.nullable().optional()), martAttribute: martAttribute.nullable().optional(), id: z.number().int().optional()}); export const supplierInquiryRecord: z.ZodType = z.object({marketerPartyId: z.number().int().optional(), supplierPartyId: z.number().int().optional(), subCode: z.string().nullable().optional(), billingReference: z.string().nullable().optional(), timestamp: z.string().optional(), attributeRelationships: z.array(supplierInquiryMartAttribute).nullable().optional(), id: z.number().int().optional()}); export const createApiApiInquiryResponse = z.array(supplierInquiryRecord); 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 /api/inquiry */ export const createApiApiInquiry = async (args: CreateApiApiInquiryArgs, options: CreateApiApiInquiryOptions) => { const {baseUrl, fetch: fetchFn = fetch, token} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (token !== undefined) headers.set("X-Deluxe-Auth", token) const url = `${baseUrl.replace(/\/+$/, '')}/api/inquiry${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(createApiApiInquiryResponse, await res.json()) };