import {z} from 'zod' export type ApiKeywordMatch = {id?: string | undefined, name?: string | undefined, keyword?: string | undefined, category?: (Array<'GENERAL' | 'SOLID_TUMOR' | 'HEMATO' | 'NON_NEOPLASTIC' | 'SEERRX' | 'SEER_TRAINING' | 'LYMPH_NODES' | 'STAGING'>) | undefined, start?: number | undefined, end?: number | undefined}; export type Rx = {type?: ('DRUG' | 'REGIMEN') | undefined, alternate_name?: Array | undefined, primary_site?: Array | undefined, histology?: string | undefined, remarks?: string | undefined, evs_id?: string | undefined, abbreviation?: Array | undefined, category?: Array | undefined, subcategory?: Array | undefined, nsc_number?: Array | undefined, do_not_code?: ('YES' | 'NO' | 'SEE_REMARKS') | undefined, drugs?: Array | undefined, radiation?: string | undefined, id?: string | undefined, name?: string | undefined, version?: string | undefined, hidden?: boolean | undefined, status?: string | undefined, assigned_to?: string | undefined, first_published?: string | undefined, last_modified?: string | undefined, fingerprint?: string | undefined, note?: string | undefined, field_notes?: {[key: string]: string} | undefined, score?: number | undefined, glossary?: Array | undefined}; export type CreateApiRestRxVersionCreateArgs = {version: string, body: Rx}; export type CreateApiRestRxVersionCreateOptions = {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 rx = z.object({type: z.enum(["DRUG", "REGIMEN"]).optional(), alternate_name: z.array(z.string()).optional(), primary_site: z.array(z.string()).optional(), histology: z.string().optional(), remarks: z.string().optional(), evs_id: z.string().optional(), abbreviation: z.array(z.string()).optional(), category: z.array(z.string()).optional(), subcategory: z.array(z.string()).optional(), nsc_number: z.array(z.string()).optional(), do_not_code: z.enum(["YES", "NO", "SEE_REMARKS"]).optional(), drugs: z.array(z.string()).optional(), radiation: z.string().optional(), 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 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 }; /** * Save a new Rx entry * * POST /rest/rx/{version}/create */ export const createApiRestRxVersionCreate = async (args: CreateApiRestRxVersionCreateArgs, options?: CreateApiRestRxVersionCreateOptions) => { const {baseUrl = "https://api.seer.cancer.gov", fetch: fetchFn = fetch, apiKeyScheme} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKeyScheme !== undefined) headers.set("X-SEERAPI-Key", apiKeyScheme) const url = `${baseUrl.replace(/\/+$/, '')}/rest/rx/${encodeURIComponent(String(args.version))}/create${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(rx, await res.json()) };