import {z} from 'zod' export type FilterOriginRequestStatus = 'Requested' | 'Delayed' | 'Rejected' | 'Canceled' | 'Answered'; export type GetApiPublicV2EudrSuppliersOriginRequestsArgs = {page?: number | undefined, size?: number | undefined, customerId?: number | undefined, reference?: string | undefined, status?: FilterOriginRequestStatus | undefined}; export type GetApiPublicV2EudrSuppliersOriginRequestsOptions = {baseUrl?: string | undefined, TokenAuthentication?: string | undefined, fetch?: typeof fetch | undefined}; export const filterOriginRequestStatus = z.enum(["Requested", "Delayed", "Rejected", "Canceled", "Answered"]); export const publicProductReferenceType = z.enum(["PurchaseOrder", "ShipmentNumber", "Other"]); export const publicOriginRequestReference = z.object({reference: z.string().nullable().optional(), referenceType: publicProductReferenceType}); export const publicProductType = z.enum(["Inbound", "Outbound"]); export const publicMinimalProduct = z.object({id: z.number().int(), customerId: z.number().int(), supplierId: z.number().int().nullable().optional(), reference: z.string().nullable().optional(), type: publicProductType, name: z.string()}); export const publicGeometrySchema = z.object({type: z.string(), coordinates: z.array(z.array(z.array(z.number())))}); export const publicFeature = z.object({geometry: publicGeometrySchema, properties: z.record(z.string(), z.object({}))}); export const publicFeatureCollection = z.object({features: z.array(publicFeature)}); export const publicSupplierOrigin = z.object({id: z.number().int(), name: z.string().nullable().optional(), active: z.boolean(), producedAtStart: z.string().nullable().optional(), producedAtEnd: z.string().nullable().optional(), producerName: z.string().nullable().optional(), countryName: z.string(), plot: publicFeatureCollection, plotAreaHa: z.number(), weight: z.number().nullable().optional(), createdAt: z.string(), updatedAt: z.string()}); export const publicSupplierOriginRequest = z.object({id: z.number().int(), status: filterOriginRequestStatus, createdAt: z.string(), updatedAt: z.string(), closedAt: z.string().nullable().optional(), endDate: z.string().nullable().optional(), requestedWeight: z.number().nullable().optional(), providedWeight: z.number().nullable().optional(), references: publicOriginRequestReference, product: publicMinimalProduct, origins: publicSupplierOrigin, supplierDDSReferences: z.array(z.string())}); export const getApiPublicV2EudrSuppliersOriginRequestsResponse = z.object({content: z.array(publicSupplierOriginRequest).optional(), size: z.number().int().optional(), number: z.number().int().optional(), totalElements: z.number().int().optional(), totalPages: z.number().int().optional(), numberOfElements: z.number().int().optional(), first: z.boolean().optional(), last: z.boolean().optional(), empty: z.boolean().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 }; /** * Fetch supplier origin requests * * GET /public/v2/eudr/suppliers/origin-requests */ export const getApiPublicV2EudrSuppliersOriginRequests = async (args: GetApiPublicV2EudrSuppliersOriginRequestsArgs, options?: GetApiPublicV2EudrSuppliersOriginRequestsOptions) => { const {baseUrl = "https://api.prewave.com", fetch: fetchFn = fetch, TokenAuthentication} = options ?? {} const query = new URLSearchParams() const headers = new Headers() 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 (args.customerId != null) query.append("customerId", String(args.customerId)) if (args.reference != null) query.append("reference", String(args.reference)) if (args.status != null) query.append("status", String(args.status)) if (TokenAuthentication !== undefined) headers.set("X-Auth-Token", TokenAuthentication) const url = `${baseUrl.replace(/\/+$/, '')}/public/v2/eudr/suppliers/origin-requests${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(getApiPublicV2EudrSuppliersOriginRequestsResponse, await res.json()) };