import {z} from 'zod' export type GetApiRestHcpcsCodeCodeArgs = {code: string}; export type GetApiRestHcpcsCodeCodeOptions = {baseUrl?: string | undefined, apiKeyScheme?: string | undefined, fetch?: typeof fetch | undefined}; export const hcpcsProcedure = z.object({hcpcs_code: z.string().optional(), generic_name: z.string().optional(), description: z.string().optional(), brand_names: z.array(z.string()).optional(), strength: z.string().optional(), fda_approval_year: z.string().optional(), fda_discontinuation_year: z.string().optional(), cms_approval_date: z.string().optional(), cms_discontinuation_date: z.string().optional(), categories: z.array(z.enum(["HORMONAL_THERAPY", "ANCILLARY", "CHEMOTHERAPY", "IMMUNOTHERAPY", "RADIOPHARMACEUTICAL", "HEME_TRANSPLANT_ENDOCRINE_PROCEDURE"])).optional(), major_drug_class: z.string().optional(), minor_drug_class: z.string().optional(), oral: z.boolean().optional(), date_added: z.string().optional(), date_modified: z.string().optional(), score: z.number().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 single procedure * * GET /rest/hcpcs/code/{code} */ export const getApiRestHcpcsCodeCode = async (args: GetApiRestHcpcsCodeCodeArgs, options?: GetApiRestHcpcsCodeCodeOptions) => { 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 (apiKeyScheme !== undefined) headers.set("X-SEERAPI-Key", apiKeyScheme) const url = `${baseUrl.replace(/\/+$/, '')}/rest/hcpcs/code/${encodeURIComponent(String(args.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(hcpcsProcedure, await res.json()) };