import {z} from 'zod' export type GetApiMarketPagesByPathArgs = {path: string}; export type GetApiMarketPagesByPathOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const filterGroupOptionDto = z.object({label: z.string(), value: z.string(), metadata: z.object({}).optional(), count: z.number().optional()}); export const filterGroupDto = z.object({name: z.string().optional(), slug: z.string().optional(), allowMultiple: z.boolean().optional(), presentation: z.string().optional(), options: z.array(filterGroupOptionDto).optional(), expanded: z.boolean().optional(), tabs: z.object({}).optional()}); export const breadcrumbItemDto = z.object({name: z.string(), slug: z.string(), path: z.string()}); export const marketPageByPathResponseDto = z.object({id: z.string(), name: z.string(), slug: z.string(), description: z.string().nullable().optional(), fullPath: z.string(), baseFilter: z.object({}), filterGroups: z.array(filterGroupDto), metadata: z.object({}), breadcrumb: z.array(breadcrumbItemDto), totalCount: z.number().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 market page by path * * GET /market-pages/by-path */ export const getApiMarketPagesByPath = async (args: GetApiMarketPagesByPathArgs, options?: GetApiMarketPagesByPathOptions) => { const {baseUrl = "https://api.limitless.exchange", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.path != null) query.append("path", String(args.path)) const url = `${baseUrl.replace(/\/+$/, '')}/market-pages/by-path${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(marketPageByPathResponseDto, await res.json()) };