import {z} from 'zod' export type GetApiConceptschemesSchemeIdCCIdArgs = {scheme_id: string, c_id: string}; export type GetApiConceptschemesSchemeIdCCIdOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const label = z.object({type: z.string(), language: z.string(), label: z.string()}); export const note = z.object({note: z.string().optional(), type: z.string().optional(), language: z.string().optional(), markup: z.string().nullable().optional()}); export const source = z.object({citation: z.string().optional(), markup: z.string().nullable().optional()}); export const matchList = z.array(z.string()); export const matches = z.object({broad: matchList.optional(), close: matchList.optional(), exact: matchList.optional(), narrow: matchList.optional(), related: matchList.optional()}); export const conceptDetail = z.object({id: z.string().optional(), type: z.literal("concept"), labels: z.array(label), notes: z.array(note).optional(), sources: z.array(source).optional(), member_of: z.array(z.object({id: z.string().optional()})).optional(), related: z.array(z.object({id: z.string().optional()})).optional(), broader: z.array(z.object({id: z.string().optional()})).optional(), narrower: z.array(z.object({id: z.string().optional()})).optional(), matches: matches.optional(), subordinate_arrays: z.array(z.object({id: z.string().optional()})).optional(), label: z.string().optional(), uri: z.string().optional(), concept_scheme: z.object({uri: z.string().optional(), labels: z.array(label).optional()}).optional()}); export const collectionDetail = z.object({id: z.string().optional(), type: z.literal("collection"), labels: z.array(label), notes: z.array(note).optional(), sources: z.array(source).optional(), member_of: z.array(z.object({id: z.string().optional()})).optional(), members: z.array(z.object({id: z.string().optional()})).optional(), superordinates: z.array(z.object({id: z.string().optional()})).optional(), infer_concept_relations: z.boolean().optional(), label: z.string().optional(), uri: z.string().optional()}); export const thingDetail = z.union([conceptDetail, collectionDetail]); 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 information about a concept or collection. * * GET /conceptschemes/{scheme_id}/c/{c_id} */ export const getApiConceptschemesSchemeIdCCId = async (args: GetApiConceptschemesSchemeIdCCIdArgs, options: GetApiConceptschemesSchemeIdCCIdOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/conceptschemes/${encodeURIComponent(String(args.scheme_id))}/c/${encodeURIComponent(String(args.c_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(thingDetail, await res.json()) };