import {z} from 'zod' export type ConnectorInstanceId = string; export type ResourceDescriptorId = string; export type DataFlowRole = 'SOURCE' | 'DESTINATION'; export type AccessProtocol = 'HTTP' | 'HTTPS' | 'JDBC' | 'ODBC' | 'SFTP' | 'FILE' | 'S3' | 'AZURE_BLOB' | 'GCS' | 'KAFKA' | 'AMQP' | 'MQTT' | 'OIDC' | 'DIDCOMM' | 'VAULT' | 'INTERNAL' | 'CUSTOM'; export type StringMap = {[key: string]: string}; export type AttachConnectorResourceRequest = {resourceDescriptorId: ResourceDescriptorId, role: DataFlowRole, externalResourceName?: string | undefined, accessProtocol?: AccessProtocol | undefined, endpointId?: string | undefined, metadata?: StringMap | undefined}; export type CreateApiConnectorsConnectorInstanceIdResourcesArgs = {connectorInstanceId: ConnectorInstanceId, body: AttachConnectorResourceRequest}; export type CreateApiConnectorsConnectorInstanceIdResourcesOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const resourceDescriptorId = z.string(); export const dataFlowRole = z.enum(["SOURCE", "DESTINATION"]); export const accessProtocol = z.enum(["HTTP", "HTTPS", "JDBC", "ODBC", "SFTP", "FILE", "S3", "AZURE_BLOB", "GCS", "KAFKA", "AMQP", "MQTT", "OIDC", "DIDCOMM", "VAULT", "INTERNAL", "CUSTOM"]); export const stringMap = z.record(z.string(), z.string()); export const connectorResourceId = z.string(); export const connectorInstanceId = z.string(); export const connectorResource = z.object({resourceDescriptorId: resourceDescriptorId, role: dataFlowRole, externalResourceName: z.string().optional(), accessProtocol: accessProtocol.optional(), endpointId: z.string().optional(), metadata: stringMap.optional(), connectorResourceId: connectorResourceId, connectorInstanceId: connectorInstanceId}); 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 }; /** * Attach a resource descriptor to a connector * * POST /connectors/{connectorInstanceId}/resources */ export const createApiConnectorsConnectorInstanceIdResources = async (args: CreateApiConnectorsConnectorInstanceIdResourcesArgs, options?: CreateApiConnectorsConnectorInstanceIdResourcesOptions) => { const {baseUrl = "https://api.example.com/api/connector/v1", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/connectors/${encodeURIComponent(String(args.connectorInstanceId))}/resources${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(connectorResource, await res.json()) };