import {z} from 'zod' export type GetApiGetJSONArgs = {query: string}; export type GetApiGetJSONOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const query = z.object({queryString: z.string().optional(), class: z.string().optional()}); export const queryRequest = z.object({query: query.optional(), criteria: z.string().optional()}); export const fieldsDocTypeRef = z.object({"@name": z.string(), "*body": z.string().optional(), "@xlink:type": z.string().optional(), "@xlink:href": z.string().optional()}); export const classDocTypeRef = z.object({"@name": z.string(), "@recordNumber": z.string(), field: z.array(fieldsDocTypeRef)}); export const xlink4 = z.object({"@number": z.string(), "@xlink:type": z.string(), "@xlink:href": z.string(), "*body": z.string()}); export const xlink2 = z.object({"@count": z.string(), page: xlink4}); export const xlink5 = z.object({ErrorMessage: z.string()}); export const xlink3 = z.object({Error: z.array(xlink5)}); export const xlink1 = z.object({recordCounter: z.array(z.string()).optional(), class: z.array(classDocTypeRef).optional(), pages: xlink2.optional(), start: z.string().optional(), end: z.string().optional(), Errors: xlink3.optional()}); export const xlink = z.object({queryRequest: queryRequest.optional(), queryResponse: xlink1}); export const getXMLDoc = z.object({"@encoding": z.string(), "xlink:httpQuery": xlink}); export const getJSONGETResponse = z.object({getXMLDocnew: getXMLDoc}); 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 /GetJSON */ export const getApiGetJSON = async (args: GetApiGetJSONArgs, options?: GetApiGetJSONOptions) => { const {baseUrl = "https://cadsrapi.cancer.gov/caDSR5.0/rest/getJSON", 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)) const url = `${baseUrl.replace(/\/+$/, '')}/GetJSON${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(getJSONGETResponse, await res.json()) };