import {z} from 'zod' export type GetApiV1PublicChannelArgs = {offset?: number | undefined, limit?: number | undefined, marketplace?: string | undefined, visibility?: ('JTL' | 'THIRDPARTY' | 'ONBOARDING' | 'RESTRICTED') | undefined}; export type GetApiV1PublicChannelOptions = {baseUrl?: string | undefined, SellerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const channelFeatureList = z.object({invoiceDocumentTransfer: z.enum(["not-supported", "from-channel", "from-seller"]).optional(), priceUpdatesSupported: z.boolean().optional(), quantityPriceSupported: z.boolean().optional(), remainingQuanitySupported: z.boolean().optional(), variationsSupported: z.boolean().optional(), returnTrackingRequired: z.boolean().optional(), allowCombineOrders: z.boolean().optional(), stockUpdateWithoutEventSupported: z.boolean().optional()}); export const minimumClientsVersionRequired = z.object({type: z.string(), version: z.string()}); export const isPlatform = z.boolean(); export const platform = z.string(); export const marketplaceChannelList = z.array(z.unknown()); export const stockSyncGroup = z.string().nullable(); export const channel = z.string(); export const salesChannel = z.object({group: z.string().nullable().optional(), currency: z.string(), marketplaceList: z.array(z.string()), supportedLanguages: z.array(z.string()).optional(), displayName: z.string(), website: z.string(), pricing: z.string().nullable().optional(), channelType: z.enum(["MARKETPLACE", "OTHER"]).optional(), supportContact: z.string(), vendor: z.string(), signUpUrl: z.string(), updateUrl: z.string().optional(), logo: z.string().nullable().optional(), description: z.string().nullable().optional(), appId: z.string().nullable().optional(), featureList: channelFeatureList, minimumClientsVersionRequired: z.array(minimumClientsVersionRequired).nullable().optional(), isPlatform: isPlatform, platform: platform.optional(), marketplaceChannelList: marketplaceChannelList.optional(), stockSyncGroup: stockSyncGroup.optional(), visibility: z.enum(["JTL", "THIRDPARTY", "ONBOARDING", "RESTRICTED"]), channel: channel, statistics: z.object({rating: z.number().int().optional(), avgOfferListingDelaySeconds: z.number().int().optional(), avgOrderDelaySeconds: z.number().int().optional()}).optional()}); export const salesChannelList = z.array(salesChannel); export const channelList = z.object({limit: z.number().int().optional(), offset: z.number().int().optional(), total: z.number().int().optional(), channelList: salesChannelList}); 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 }; /** * Channel List * * GET /v1/public/channel */ export const getApiV1PublicChannel = async (args: GetApiV1PublicChannelArgs, options?: GetApiV1PublicChannelOptions) => { const {baseUrl = "https://scx-sbx.api.jtl-software.com", fetch: fetchFn = fetch, SellerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.marketplace != null) query.append("marketplace", String(args.marketplace)) if (args.visibility != null) query.append("visibility", String(args.visibility)) if (SellerAuth !== undefined) headers.set('authorization', `Bearer ${SellerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/public/channel${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(channelList, await res.json()) };