import {z} from 'zod' export type GetApiApiOffersPriceListArgs = {'x-site-context': string, limit?: number | undefined, offset?: number | undefined, sortBy?: ('updatedAt' | 'name' | 'priceListId') | undefined, sortOrder?: ('asc' | 'desc') | undefined, filter?: 'not_expired' | undefined, isDefault?: boolean | undefined}; export type GetApiApiOffersPriceListOptions = {baseUrl?: string | undefined, AuthorizationToken?: string | undefined, fetch?: typeof fetch | undefined}; export const genericListQueryResponse = z.object({limit: z.number().optional(), count: z.number().optional(), offset: z.number().optional()}); export const priceList = z.object({_id: z.string().optional(), priceListId: z.number().optional(), name: z.string().optional(), isDefault: z.boolean().optional(), currency: z.string().optional(), startDate: z.string().nullable().optional(), endDate: z.string().nullable().optional(), deleted: z.boolean().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional()}); export const priceListResponse = z.object({query: genericListQueryResponse.optional(), data: z.array(priceList).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 }; /** * Get all price lists * * GET /api-offers/price-list */ export const getApiApiOffersPriceList = async (args: GetApiApiOffersPriceListArgs, options?: GetApiApiOffersPriceListOptions) => { const {baseUrl = "https://live.copilot.fabric.inc", fetch: fetchFn = fetch, AuthorizationToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['x-site-context'] != null) headers.set("x-site-context", String(args['x-site-context'])) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.sortBy != null) query.append("sortBy", String(args.sortBy)) if (args.sortOrder != null) query.append("sortOrder", String(args.sortOrder)) if (args.filter != null) query.append("filter", String(args.filter)) if (args.isDefault != null) query.append("isDefault", String(args.isDefault)) if (AuthorizationToken !== undefined) headers.set('authorization', `Bearer ${AuthorizationToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/api-offers/price-list${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(priceListResponse, await res.json()) };