import {z} from 'zod' export type GetApiQuoteSearchArgs = {category?: unknown | undefined, author?: unknown | undefined, minlength?: number | undefined, maxlength?: number | undefined, query?: unknown | undefined, private?: boolean | undefined, language?: unknown | undefined, limit?: number | undefined, sfw?: boolean | undefined}; export type GetApiQuoteSearchOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const quote = z.object({author: z.string().optional(), quote: z.string(), tags: z.array(z.string()).optional(), id: z.string(), image: z.string().optional(), length: z.number().int().optional()}); export const quoteResponse = z.object({success: z.string().optional(), contents: z.object({quotes: z.array(quote).optional()}).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 }; /** * Search for a `Quote` in They Said So platform. Optional `category` , `author`, `minlength`, `maxlength` params determines the filters applied while searching for the quote. * * GET /quote/search */ export const getApiQuoteSearch = async (args: GetApiQuoteSearchArgs, options?: GetApiQuoteSearchOptions) => { const {baseUrl = "https://quotes.rest", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.category != null) query.append("category", String(args.category)) if (args.author != null) query.append("author", String(args.author)) if (args.minlength != null) query.append("minlength", String(args.minlength)) if (args.maxlength != null) query.append("maxlength", String(args.maxlength)) if (args.query != null) query.append("query", String(args.query)) if (args.private != null) query.append("private", String(args.private)) if (args.language != null) query.append("language", String(args.language)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.sfw != null) query.append("sfw", String(args.sfw)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/quote/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(quoteResponse, await res.json()) };