import {z} from 'zod' export type GetApiV1ChannelOptions = {baseUrl?: string | undefined, ChannelAuth?: 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 channelBasedData = z.object({group: z.string().nullable().optional(), currency: z.string().optional(), 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.optional(), platform: platform.optional(), marketplaceChannelList: marketplaceChannelList.optional(), stockSyncGroup: stockSyncGroup.optional()}); export const salesChannelData = channelBasedData; export const channelStatus = z.object({isActive: z.boolean().optional(), channel: salesChannelData.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 }; /** * Status * * GET /v1/channel */ export const getApiV1Channel = async (options?: GetApiV1ChannelOptions) => { 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 (ChannelAuth !== undefined) headers.set('authorization', `Bearer ${ChannelAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/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(channelStatus, await res.json()) };