import {z} from 'zod' export type GetApiApiV2SalesChannelsArgs = {page?: {number: string, size: string} | undefined, filter?: (Array<{key: 'id' | 'name' | 'moduleName', op: 'equals' | 'notEquals' | 'in' | 'notIn' | 'contains' | 'notContains' | 'startsWith' | 'endsWith', value: string | Array}>) | undefined}; export type GetApiApiV2SalesChannelsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiV2SalesChannelsResponse = z.object({data: z.array(z.object({id: z.string().optional(), uuid: z.string(), project: z.object({id: z.string().optional()}).optional(), moduleName: z.enum(["shopify", "amazon", "shopware6", "woocommerce"]).optional(), name: z.string().optional(), stockExport: z.boolean().optional(), productExport: z.boolean().optional(), active: z.boolean().optional(), preventDeletion: z.boolean().optional(), hideFromOverview: z.boolean().optional(), isMigratedToConnect: z.boolean().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional()})), extra: z.object({totalCount: z.number().int(), page: z.object({number: z.number().int(), size: z.number().int()})})}); 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 }; /** * List sales channels V2 * * GET /api/v2/salesChannels */ export const getApiApiV2SalesChannels = async (args: GetApiApiV2SalesChannelsArgs, options?: GetApiApiV2SalesChannelsOptions) => { 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 (args.page != null) query.append("page", JSON.stringify(args.page)) for (const item of args.filter ?? []) query.append("filter", String(item)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/salesChannels${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(getApiApiV2SalesChannelsResponse, await res.json()) };