import {z} from 'zod' export type GetApiApiV1ReturnsIdArgs = {id: string}; export type GetApiApiV1ReturnsIdOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiV1ReturnsIdResponse = z.object({data: z.object({id: z.string(), date: z.string().nullable(), status: z.enum(["unknown", "open", "canceled", "completed", "sent", "created", "announced", "released", "checked", "done"]).optional(), progress: z.enum(["announced", "received", "checked", "done"]).optional(), documentNumber: z.string().nullable().optional(), bodyOutroduction: z.string().optional(), internalComment: z.string().optional(), shippingMethod: z.object({id: z.number().int().optional(), name: z.string().optional()}).optional(), project: z.object({id: z.number().int().optional(), name: z.string().optional()}).optional(), salesOrder: z.object({id: z.string()}).nullable(), positions: z.array(z.object({id: z.string(), quantity: z.number(), salesOrderPosition: z.object({id: z.string()}).nullable(), product: z.object({id: z.string(), number: z.string(), name: z.string()}), returnReason: z.object({designation: z.string().nullable()})}))}).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 }; /** * View return * * GET /api/v1/returns/{id} */ export const getApiApiV1ReturnsId = async (args: GetApiApiV1ReturnsIdArgs, options?: GetApiApiV1ReturnsIdOptions) => { const {baseUrl = "https://xentral.xentral.biz", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/returns/${encodeURIComponent(String(args.id))}${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(getApiApiV1ReturnsIdResponse, await res.json()) };