import {z} from 'zod' export type GetApiRestRxVersionArgs = {version: string, q?: string | undefined, status?: Array | undefined, assigned_to?: string | undefined, type?: ('DRUG' | 'REGIMEN') | undefined, modified_from?: string | undefined, modified_to?: string | undefined, published_from?: string | undefined, published_to?: string | undefined, been_published?: boolean | undefined, hidden?: boolean | undefined, mode?: ('AND' | 'OR') | undefined, count?: number | undefined, offset?: number | undefined, order?: ('name' | '-name' | 'status' | '-status' | 'assigned_to' | '-assigned_to' | 'last_modified' | '-last_modified' | 'type' | '-type' | 'site_category' | '-site_category' | 'icdO3_morphology' | '-icdO3_morphology') | undefined, output_type?: ('MIN' | 'PARTIAL' | 'FULL') | undefined, glossary?: boolean | undefined, category?: Array | undefined, do_not_code?: ('YES' | 'NO' | 'SEE_REMARKS') | undefined}; export type GetApiRestRxVersionOptions = {baseUrl?: string | undefined, apiKeyScheme?: string | undefined, fetch?: typeof fetch | undefined}; export const apiKeywordMatch = z.object({id: z.string().optional(), name: z.string().optional(), keyword: z.string().optional(), category: z.array(z.enum(["GENERAL", "SOLID_TUMOR", "HEMATO", "NON_NEOPLASTIC", "SEERRX", "SEER_TRAINING", "LYMPH_NODES", "STAGING"])).optional(), start: z.number().int().optional(), end: z.number().int().optional()}); export const publishableEntity = z.object({id: z.string().optional(), name: z.string().optional(), version: z.string().optional(), hidden: z.boolean().optional(), status: z.string().optional(), assigned_to: z.string().optional(), first_published: z.string().optional(), last_modified: z.string().optional(), fingerprint: z.string().optional(), note: z.string().optional(), field_notes: z.record(z.string(), z.string()).optional(), score: z.number().optional(), glossary: z.array(apiKeywordMatch).optional()}); export const publishableSearchResults = z.object({terms: z.array(z.string()).optional(), total: z.number().int().optional(), count: z.number().int().optional(), offset: z.number().int().optional(), max_score: z.number().optional(), order: z.string().optional(), results: z.array(publishableEntity).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 }; /** * Return a list of Rx entries * * GET /rest/rx/{version} */ export const getApiRestRxVersion = async (args: GetApiRestRxVersionArgs, options?: GetApiRestRxVersionOptions) => { const {baseUrl = "https://api.seer.cancer.gov", fetch: fetchFn = fetch, apiKeyScheme} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.q != null) query.append("q", String(args.q)) for (const item of args.status ?? []) query.append("status", String(item)) if (args.assigned_to != null) query.append("assigned_to", String(args.assigned_to)) if (args.type != null) query.append("type", String(args.type)) if (args.modified_from != null) query.append("modified_from", String(args.modified_from)) if (args.modified_to != null) query.append("modified_to", String(args.modified_to)) if (args.published_from != null) query.append("published_from", String(args.published_from)) if (args.published_to != null) query.append("published_to", String(args.published_to)) if (args.been_published != null) query.append("been_published", String(args.been_published)) if (args.hidden != null) query.append("hidden", String(args.hidden)) if (args.mode != null) query.append("mode", String(args.mode)) if (args.count != null) query.append("count", String(args.count)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.order != null) query.append("order", String(args.order)) if (args.output_type != null) query.append("output_type", String(args.output_type)) if (args.glossary != null) query.append("glossary", String(args.glossary)) for (const item of args.category ?? []) query.append("category", String(item)) if (args.do_not_code != null) query.append("do_not_code", String(args.do_not_code)) if (apiKeyScheme !== undefined) headers.set("X-SEERAPI-Key", apiKeyScheme) const url = `${baseUrl.replace(/\/+$/, '')}/rest/rx/${encodeURIComponent(String(args.version))}${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(publishableSearchResults, await res.json()) };