import {z} from 'zod' export type SellerId = string; export type GetApiV1ChannelOfferStockUpdatesArgs = {updatedAfter: string, sellerId: SellerId}; export type GetApiV1ChannelOfferStockUpdatesOptions = {baseUrl?: string | undefined, ChannelAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const sellerId = z.string(); export const offerId = z.number().int().gte(1); export const channelOfferId = z.string().min(1).max(100); export const stock = z.object({sellerId: sellerId, offerId: offerId, channelOfferId: channelOfferId, quantity: z.number().int().gte(0), updatedAt: z.string()}); export const stockList = z.object({stockUpdateList: z.array(stock), lastUpdatedAt: z.string()}); 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 }; /** * Stock Updates (by Seller) * * GET /v1/channel/offer/stock-updates */ export const getApiV1ChannelOfferStockUpdates = async (args: GetApiV1ChannelOfferStockUpdatesArgs, options?: GetApiV1ChannelOfferStockUpdatesOptions) => { const {baseUrl = "https://scx-sbx.api.jtl-software.com", fetch: fetchFn = fetch, ChannelAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.updatedAfter != null) query.append("updatedAfter", String(args.updatedAfter)) if (args.sellerId != null) query.append("sellerId", String(args.sellerId)) if (ChannelAuth !== undefined) headers.set('authorization', `Bearer ${ChannelAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/channel/offer/stock-updates${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(stockList, await res.json()) };