import {z} from 'zod' export type GetApiApiBooksSearchArgs = {Query: string | null, PageIndex: number, PageSize: number}; export type GetApiApiBooksSearchOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const bookDto = z.object({id: z.string().optional(), created: z.string().optional(), nameFr: z.string().optional(), nameEn: z.string().optional(), descriptionFr: z.string().optional(), descriptionEn: z.string().optional(), isbn: z.string().optional(), author: z.string().optional(), editor: z.string().optional(), yearOfPublication: z.number().int().optional(), numberOfPages: z.number().int().optional(), membersOnly: z.boolean().optional(), cardImage: z.string().optional(), memberPrice: z.number().optional(), nonMemberPrice: z.number().nullable().optional(), memberProductId: z.string().optional(), nonMemberProductId: z.string().nullable().optional(), slug: z.string().optional()}); export const pagingInfo = z.object({totalResultsFound: z.number().int().optional(), pageCount: z.number().int().optional()}); export const pagedSearchResultOfBookDto = z.object({items: z.array(bookDto).optional(), pagingInfo: pagingInfo.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 /api/books/search */ export const getApiApiBooksSearch = async (args: GetApiApiBooksSearchArgs, options?: GetApiApiBooksSearchOptions) => { const {baseUrl = "https://api.rgcq.org", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.Query != null) query.append("Query", String(args.Query)) if (args.PageIndex != null) query.append("PageIndex", String(args.PageIndex)) if (args.PageSize != null) query.append("PageSize", String(args.PageSize)) const url = `${baseUrl.replace(/\/+$/, '')}/api/books/search${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(pagedSearchResultOfBookDto, await res.json()) };