import {z} from 'zod' export type ConnectorType = string; export type ResourceKind = 'OBJECT' | 'TABULAR' | 'DOCUMENT' | 'CLAIM_SET' | 'GRAPH' | 'EVENT_STREAM' | 'FILE' | 'SECRET' | 'CONFIGURATION' | 'CREDENTIAL' | 'PRESENTATION' | 'CUSTOM'; export type GetApiSemanticBindingsArgs = {connectorType?: ConnectorType | undefined, resourceKind?: ResourceKind | undefined, page?: number | undefined, size?: number | undefined}; export type GetApiSemanticBindingsOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const connectorInstanceId = z.string(); export const resourceDescriptorId = z.string(); export const fieldDescriptorId = z.string(); export const semanticReference = z.object({referenceType: z.string(), referenceId: z.string(), iri: z.string().optional(), version: z.string().optional()}); export const stringMap = z.record(z.string(), z.string()); export const semanticBindingId = z.string(); export const semanticConnectorBinding = z.object({connectorInstanceId: connectorInstanceId.optional(), resourceDescriptorId: resourceDescriptorId, fieldDescriptorId: fieldDescriptorId.optional(), bindingKind: z.string(), semanticReference: semanticReference.optional(), validationMode: z.enum(["NONE", "WARN", "ENFORCE"]).optional(), metadata: stringMap.optional(), semanticBindingId: semanticBindingId, createdAt: z.string(), updatedAt: z.string()}); export const page = z.object({limit: z.number().int().gte(0), offset: z.number().int().gte(0), page: z.number().int().gte(0), size: z.number().int().gte(0), total: z.number().int().gte(0), totalPages: z.number().int().gte(0), hasMore: z.boolean()}); export const semanticConnectorBindingPage = z.object({data: z.array(semanticConnectorBinding), pagination: page}); 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 }; /** * List semantic connector bindings * * GET /semantic/bindings */ export const getApiSemanticBindings = async (args: GetApiSemanticBindingsArgs, options?: GetApiSemanticBindingsOptions) => { const {baseUrl = "https://api.example.com/api/connector/v1", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.connectorType != null) query.append("connectorType", String(args.connectorType)) if (args.resourceKind != null) query.append("resourceKind", String(args.resourceKind)) if (args.page != null) query.append("page", String(args.page)) if (args.size != null) query.append("size", String(args.size)) if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/semantic/bindings${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(semanticConnectorBindingPage, await res.json()) };