import {z} from 'zod' export type GetApiReferencesRfCodeArgs = {rf_code: string, with_nested?: boolean | undefined, create_parquet?: boolean | undefined}; export type GetApiReferencesRfCodeOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const reference = z.object({rf_code: z.string().optional(), rf_label: z.string().optional(), short_name: z.string().optional(), full_citation: z.string().optional(), reference_type: z.string().optional(), title: z.string().optional(), publication_date: z.string().optional(), total_pages: z.number().int().optional(), publisher: z.string().optional(), publication_place: z.string().optional(), degree: z.string().optional(), isbn: z.string().optional(), url: z.string().optional(), doi: z.string().optional(), journal: z.string().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 reference * * GET /references/{rf_code} */ export const getApiReferencesRfCode = async (args: GetApiReferencesRfCodeArgs, options?: GetApiReferencesRfCodeOptions) => { const {baseUrl = "https://api.vegbank.org", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.with_nested != null) query.append("with_nested", String(args.with_nested)) if (args.create_parquet != null) query.append("create_parquet", String(args.create_parquet)) const url = `${baseUrl.replace(/\/+$/, '')}/references/${encodeURIComponent(String(args.rf_code))}${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(reference, await res.json()) };