import {z} from 'zod' export type GetApiRestMphGroupIdArgs = {id: string}; export type GetApiRestMphGroupIdOptions = {baseUrl?: string | undefined, apiKeyScheme?: string | undefined, fetch?: typeof fetch | undefined}; export const mphRuleInfo = z.object({step: z.string().optional(), question: z.string().optional(), reason: z.string().optional()}); export const mphGroup = z.object({id: z.string().optional(), name: z.string().optional(), site_inclusions: z.string().optional(), site_exclusions: z.string().optional(), histology_inclusions: z.string().optional(), histology_exclusions: z.string().optional(), behavior_inclusions: z.string().optional(), year_inclusions: z.string().optional(), rules: z.array(mphRuleInfo).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 }; /** * Returns the group information for a given group ID * * GET /rest/mph/group/{id} */ export const getApiRestMphGroupId = async (args: GetApiRestMphGroupIdArgs, options?: GetApiRestMphGroupIdOptions) => { 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/mph/group/${encodeURIComponent(String(args.id))}${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(mphGroup, await res.json()) };