import {z} from 'zod' export type CategoryIndexSearchRequest = {search?: (string | null) | undefined, category?: (string | null) | undefined}; export type CreateApiV1CardsCategoryIndexSearchArgs = {body: CategoryIndexSearchRequest}; export type CreateApiV1CardsCategoryIndexSearchOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const categoryIndexLatest = z.object({value: z.number().nullable(), as_of: z.string(), card_count: z.number().int().nullable().optional(), cards_priced: z.number().int().nullable().optional(), calc_version: z.string().nullable().optional()}); export const categoryIndexChange = z.object({abs: z.number(), pct: z.number()}); export const categoryIndexItem = z.object({slug: z.string(), name: z.string(), category: z.string().nullable().optional(), description: z.string().nullable().optional(), image_url: z.string().nullable().optional(), base_date: z.string().nullable().optional(), latest: categoryIndexLatest.nullable(), changes: z.record(z.string(), categoryIndexChange.nullable())}); export const categoryIndexSearchResponse = z.object({count: z.number().int(), indexes: z.array(categoryIndexItem)}); 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 }; /** * Search Category Indexes * * POST /v1/cards/category-index-search */ export const createApiV1CardsCategoryIndexSearch = async (args: CreateApiV1CardsCategoryIndexSearchArgs, options?: CreateApiV1CardsCategoryIndexSearchOptions) => { const {baseUrl = "https://api.cardhedger.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/cards/category-index-search${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(categoryIndexSearchResponse, await res.json()) };