import {z} from 'zod' export type GetApiRestV1StandardNameArgs = {name: string, format?: ('json' | 'md' | 'csv' | 'oscal') | undefined, section?: string | undefined, subsection?: string | undefined, sectionID?: string | undefined, page?: number | undefined, items_per_page?: number | undefined, version?: string | undefined, hyperlink?: string | undefined, include_only?: Array | undefined}; export type GetApiRestV1StandardNameOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const nodeListResponseSchema = z.object({total_pages: z.number().int().nullable().optional(), page: z.number().int().nullable().optional(), standards: z.array(z.record(z.string(), z.unknown())).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 }; /** * Get standard nodes by name * * GET /rest/v1/standard/{name} */ export const getApiRestV1StandardName = async (args: GetApiRestV1StandardNameArgs, options: GetApiRestV1StandardNameOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.format != null) query.append("format", String(args.format)) if (args.section != null) query.append("section", String(args.section)) if (args.subsection != null) query.append("subsection", String(args.subsection)) if (args.sectionID != null) query.append("sectionID", String(args.sectionID)) if (args.page != null) query.append("page", String(args.page)) if (args.items_per_page != null) query.append("items_per_page", String(args.items_per_page)) if (args.version != null) query.append("version", String(args.version)) if (args.hyperlink != null) query.append("hyperlink", String(args.hyperlink)) for (const item of args.include_only ?? []) query.append("include_only", String(item)) const url = `${baseUrl.replace(/\/+$/, '')}/rest/v1/standard/${encodeURIComponent(String(args.name))}${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(nodeListResponseSchema, await res.json()) };