import {z} from 'zod' export type RouteExposureDescriptorId = string; export type StringMap = {[key: string]: string}; export type PartyRef = {partyId?: string | undefined, externalId?: string | undefined, partyType?: string | undefined, displayName?: string | undefined, metadata?: StringMap | undefined}; export type RetentionDeleteAction = 'DELETE' | 'ANONYMIZE' | 'PSEUDONYMIZE' | 'TOMBSTONE' | 'REVIEW'; export type RetentionSpec = {purpose?: string | undefined, legalBasis?: string | undefined, retentionPeriod?: string | undefined, deleteAction?: RetentionDeleteAction | undefined, metadata?: StringMap | undefined}; export type ConnectorIntegrityReference = {originalPayloadHash?: string | undefined, canonicalPayloadHash?: string | undefined, transformationHash?: string | undefined, evidenceRef?: string | undefined, vaultRef?: string | undefined, signatureRef?: string | undefined, sealRef?: string | undefined, timestampRef?: string | undefined, sourceAuthenticityVerdict?: string | undefined, outputManifestHash?: string | undefined}; export type ConnectorGovernanceEnvelope = {classification?: string | undefined, sensitivity?: string | undefined, specialCategory?: boolean | undefined, legalBasis?: string | undefined, purpose?: string | undefined, b2bUsagePurpose?: string | undefined, frameworkAgreementRef?: string | undefined, contractReference?: string | undefined, minimizationStatus?: ('NOT_EVALUATED' | 'MINIMIZED' | 'REDACTED' | 'FULL_DATASET' | 'EXCESSIVE') | undefined, retentionProfileId?: string | undefined, retention?: RetentionSpec | undefined, crossBorderRestriction?: ('NONE' | 'SAME_COUNTRY' | 'EEA_ONLY' | 'APPROVED_COUNTRIES' | 'CONTRACT_REQUIRED' | 'BLOCKED') | undefined, encryptionRequirement?: ('NONE' | 'IN_TRANSIT' | 'AT_REST' | 'AT_REST_AND_IN_TRANSIT' | 'CUSTOMER_MANAGED_KEY') | undefined, dpiaRequired?: boolean | undefined, integrity?: ConnectorIntegrityReference | undefined, policyDecisionId?: string | undefined, relianceVerdict?: ('NOT_EVALUATED' | 'RELIABLE' | 'CONDITIONAL' | 'NOT_RELIABLE') | undefined, auditCategory?: ('PROTOCOL_ISSUANCE' | 'PROTOCOL_VERIFICATION' | 'FORM' | 'PORTAL' | 'WORKFLOW' | 'DATASPACE' | 'INDUSTRIAL' | 'EXTERNAL_EXCHANGE' | 'VAULT' | 'SYSTEM_CATALOG') | undefined}; export type ConnectorCursor = {value: string, metadata?: StringMap | undefined}; export type RouteExposureExternalReadRequest = {authMethod: string, callerParty: PartyRef, canonicalFields: {[key: string]: unknown}, governance?: ConnectorGovernanceEnvelope | undefined, cursor?: ConnectorCursor | undefined}; export type CreateApiRouteExposuresExposureDescriptorIdReadArgs = {exposureDescriptorId: RouteExposureDescriptorId, body: RouteExposureExternalReadRequest}; export type CreateApiRouteExposuresExposureDescriptorIdReadOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const connectorRunId = z.string(); export const routeExposureDescriptorId = z.string(); export const logicalConnectionBindingId = z.string(); export const connectorExportedField = z.object({fieldPath: z.string(), value: z.record(z.string(), z.unknown()).nullable().optional(), redacted: z.boolean().optional(), minimized: z.boolean().optional(), integrityHash: z.string()}); export const connectorExportManifest = z.object({manifestId: z.string(), destinationIdentity: z.string(), selectedFields: z.array(connectorExportedField), mappingVersion: z.string(), mappingProvenance: z.string().optional(), policyDecisionId: z.string(), retentionProfileId: z.string(), agreementRef: z.record(z.string(), z.unknown()).optional(), participantIdentity: z.record(z.string(), z.unknown()).optional(), transferProcess: z.record(z.string(), z.unknown()).optional(), obligations: z.array(z.record(z.string(), z.unknown())).optional(), manifestHash: z.string()}); export const stringMap = z.record(z.string(), z.string()); export const connectorCursor = z.object({value: z.string(), metadata: stringMap.optional()}); export const connectorExternalReadResponse = z.object({state: z.string(), runId: connectorRunId.optional(), exposureDescriptorId: routeExposureDescriptorId, logicalConnectionBindingId: logicalConnectionBindingId, policyDecisionId: z.string(), selectedFieldPaths: z.array(z.string()), manifest: connectorExportManifest, cursor: connectorCursor.optional(), sourceSurface: z.string().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 }; /** * Execute an externally initiated route exposure read * * POST /route/exposures/{exposureDescriptorId}/read */ export const createApiRouteExposuresExposureDescriptorIdRead = async (args: CreateApiRouteExposuresExposureDescriptorIdReadArgs, options?: CreateApiRouteExposuresExposureDescriptorIdReadOptions) => { 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(/\/+$/, '')}/route/exposures/${encodeURIComponent(String(args.exposureDescriptorId))}/read${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(connectorExternalReadResponse, await res.json()) };