import {z} from 'zod' export type GetApiApiV0ComponentsComponentIdSchematicConnectionsArgs = {component_id: string, vc_id: string}; export type GetApiApiV0ComponentsComponentIdSchematicConnectionsOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const fromToEntry = z.object({line_number: z.string().nullable(), source: z.string(), target: z.string(), source_type: z.string(), target_type: z.string(), source_drawing: z.string().nullable(), target_drawing: z.string().nullable(), directed: z.boolean(), segment_ids: z.array(z.string()), source_node: z.string(), target_node: z.string()}); export const connectedItemEntry = z.object({line_number: z.string().nullable(), source: z.string(), target: z.string(), source_type: z.string(), target_type: z.string(), line_type: z.string(), directed: z.boolean(), segment_ids: z.array(z.string()), source_node: z.string(), target_node: z.string()}); export const schematicConnectionsResponse = z.object({streams: z.array(fromToEntry), connections: z.array(connectedItemEntry)}); 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 the drawing's From-To and connected-items tables * * GET /api/v0/components/{component_id}/schematic/connections */ export const getApiApiV0ComponentsComponentIdSchematicConnections = async (args: GetApiApiV0ComponentsComponentIdSchematicConnectionsArgs, options: GetApiApiV0ComponentsComponentIdSchematicConnectionsOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.vc_id != null) query.append("vc_id", String(args.vc_id)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v0/components/${encodeURIComponent(String(args.component_id))}/schematic/connections${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(schematicConnectionsResponse, await res.json()) };