import {z} from 'zod' export type JTLDataContractsKeysKundeKey = string; export type JTLDataContractsKeysVerkaufAuftragKey = string; export type JTLDataContractsKeysRetoureStatusKey = string; export type GetApiV2ReturnsArgs = {'x-tenant-id': string, returnNumber?: string | undefined, customerId?: JTLDataContractsKeysKundeKey | undefined, IsExisting?: boolean | undefined, salesOrderId?: JTLDataContractsKeysVerkaufAuftragKey | undefined, returnStateId?: JTLDataContractsKeysRetoureStatusKey | undefined, pageNumber?: number | undefined, pageSize?: number | undefined, 'x-runas'?: string | undefined, 'x-companyid'?: string | undefined}; export type GetApiV2ReturnsOptions = {baseUrl?: string | undefined, oauth2AuthorizationCode?: string | undefined, fetch?: typeof fetch | undefined}; export const jTLDataContractsKeysRetoureKey = z.string(); export const jTLDataContractsKeysKundeKey = z.string(); export const jTLWawiRestContractsModelsV1ReturnReturnOrigin = z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3), z.literal(4), z.literal(5), z.literal(6), z.literal(7)]); export const jTLDataContractsKeysFirmaKey = z.string(); export const jTLDataContractsKeysVerkaufAuftragKey = z.string(); export const jTLDataContractsKeysRetoureStatusKey = z.string(); export const jTLDataContractsKeysWarenlagerKey = z.string(); export const jTLWawiRestContractsModelsV1ReturnReturn = z.object({id: jTLDataContractsKeysRetoureKey.optional(), number: z.string().optional(), returnDate: z.string().optional(), customerId: jTLDataContractsKeysKundeKey.optional(), externalNumber: z.string().optional(), origin: jTLWawiRestContractsModelsV1ReturnReturnOrigin.optional(), companyId: jTLDataContractsKeysFirmaKey.optional(), salesOrderId: jTLDataContractsKeysVerkaufAuftragKey.optional(), stateId: jTLDataContractsKeysRetoureStatusKey.optional(), warehouseId: jTLDataContractsKeysWarenlagerKey.optional(), externalComment: z.string().optional(), internalComment: z.string().optional(), contact: z.string().optional(), transmitToSalesChannel: z.boolean().optional()}); export const jTLWawiRestContractsModelsPagedListOfJTLWawiRestContractsModelsV1ReturnReturn = z.object({totalItems: z.number().int().optional(), pageNumber: z.number().int().optional(), pageSize: z.number().int().optional(), items: z.array(jTLWawiRestContractsModelsV1ReturnReturn).optional(), totalPages: z.number().int().optional(), hasPreviousPage: z.boolean().optional(), hasNextPage: z.boolean().optional(), nextPageNumber: z.number().int().optional(), previousPageNumber: z.number().int().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 }; /** * Query Returns * * GET /v2/returns * * Also accepts auth: oauth2-client-credentials */ export const getApiV2Returns = async (args: GetApiV2ReturnsArgs, options?: GetApiV2ReturnsOptions) => { const {baseUrl = "https://scx-sbx.api.jtl-software.com", fetch: fetchFn = fetch, oauth2AuthorizationCode} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['x-tenant-id'] != null) headers.set("x-tenant-id", String(args['x-tenant-id'])) if (args.returnNumber != null) query.append("returnNumber", String(args.returnNumber)) if (args.customerId != null) query.append("customerId", String(args.customerId)) if (args.IsExisting != null) query.append("IsExisting", String(args.IsExisting)) if (args.salesOrderId != null) query.append("salesOrderId", String(args.salesOrderId)) if (args.returnStateId != null) query.append("returnStateId", String(args.returnStateId)) if (args.pageNumber != null) query.append("pageNumber", String(args.pageNumber)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args['x-runas'] != null) headers.set("x-runas", String(args['x-runas'])) if (args['x-companyid'] != null) headers.set("x-companyid", String(args['x-companyid'])) if (oauth2AuthorizationCode !== undefined) headers.set('authorization', `Bearer ${oauth2AuthorizationCode}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/returns${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(jTLWawiRestContractsModelsPagedListOfJTLWawiRestContractsModelsV1ReturnReturn, await res.json()) };