import {z} from 'zod' export type GetApiAuthoringV1ReferencesOutgoingClassificationIdArgs = {id: string, classification: string, filterType?: ('include' | 'exclude') | undefined, depth?: number | undefined, classifications?: Array | undefined, statuses?: (Array<'draft' | 'ready' | 'retired'>) | undefined, include?: Array | undefined, fl?: Array | undefined}; export type GetApiAuthoringV1ReferencesOutgoingClassificationIdOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const getApiAuthoringV1ReferencesOutgoingClassificationIdResponse = z.object({result: z.object({status: z.enum(["ok", "partial"]).optional(), depth: z.number().int().optional()}).optional(), next: z.object({roots: z.array(z.string()).optional(), depth: z.number().int().optional(), fl: z.array(z.string()).optional(), metadata: z.boolean().optional(), filters: z.object({classifications: z.array(z.string()).optional(), statuses: z.array(z.enum(["draft", "ready", "retired"])).optional(), filterType: z.enum(["include", "exclude"]).optional()}).optional()}).optional(), root: z.string().optional(), roots: z.array(z.string()).optional(), references: z.record(z.string(), z.object({id: z.string().optional(), classification: z.string().optional(), metadata: z.object({}).optional(), included: z.array(z.string()).optional(), excluded: z.array(z.string()).optional()})).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 }; /** * Retrieve the outgoing references for an item with the provided ID. * * GET /authoring/v1/references/outgoing/{classification}/{id} */ export const getApiAuthoringV1ReferencesOutgoingClassificationId = async (args: GetApiAuthoringV1ReferencesOutgoingClassificationIdArgs, options: GetApiAuthoringV1ReferencesOutgoingClassificationIdOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.filterType != null) query.append("filterType", String(args.filterType)) if (args.depth != null) query.append("depth", String(args.depth)) if (args.classifications != null) query.append("classifications", args.classifications.join(',')) if (args.statuses != null) query.append("statuses", args.statuses.join(',')) if (args.include != null) query.append("include", args.include.join(',')) if (args.fl != null) query.append("fl", args.fl.join(',')) const url = `${baseUrl.replace(/\/+$/, '')}/authoring/v1/references/outgoing/${encodeURIComponent(String(args.classification))}/${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(getApiAuthoringV1ReferencesOutgoingClassificationIdResponse, await res.json()) };