import {z} from 'zod' export type ResourceDescriptorId = string; export type ResourceKind = 'OBJECT' | 'TABULAR' | 'DOCUMENT' | 'CLAIM_SET' | 'GRAPH' | 'EVENT_STREAM' | 'FILE' | 'SECRET' | 'CONFIGURATION' | 'CREDENTIAL' | 'PRESENTATION' | 'CUSTOM'; export type RepresentationKind = 'JSON' | 'JSON_LD' | 'XML' | 'CSV' | 'PARQUET' | 'AVRO' | 'RDF' | 'JWT' | 'SD_JWT' | 'CBOR' | 'BINARY' | 'TEXT' | 'CUSTOM'; export type ShapeKind = 'SCHEMA' | 'OPENAPI_SCHEMA' | 'JSON_SCHEMA' | 'RDF_SHAPE' | 'SQL_TABLE' | 'CSV_HEADER' | 'CLAIMS_SCHEMA' | 'FREEFORM'; export type ContractKind = 'OPENAPI' | 'JSON_SCHEMA' | 'SQL_SCHEMA' | 'RDF_SCHEMA' | 'CSV_PROFILE' | 'OIDC_DISCOVERY' | 'VAULT_POLICY' | 'CUSTOM'; export type StringMap = {[key: string]: string}; export type ContractRef = {contractKind: ContractKind, uri?: string | undefined, version?: string | undefined, contentHash?: string | undefined, metadata?: StringMap | undefined}; export type ResourceDescriptorUpdateRequest = {displayName?: string | undefined, description?: string | undefined, resourceKind?: ResourceKind | undefined, representationKind?: RepresentationKind | undefined, shapeKind?: ShapeKind | undefined, contract?: ContractRef | undefined, metadata?: StringMap | undefined}; export type PatchApiResourceDescriptorsResourceDescriptorIdArgs = {resourceDescriptorId: ResourceDescriptorId, body: ResourceDescriptorUpdateRequest}; export type PatchApiResourceDescriptorsResourceDescriptorIdOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const resourceKind = z.enum(["OBJECT", "TABULAR", "DOCUMENT", "CLAIM_SET", "GRAPH", "EVENT_STREAM", "FILE", "SECRET", "CONFIGURATION", "CREDENTIAL", "PRESENTATION", "CUSTOM"]); export const representationKind = z.enum(["JSON", "JSON_LD", "XML", "CSV", "PARQUET", "AVRO", "RDF", "JWT", "SD_JWT", "CBOR", "BINARY", "TEXT", "CUSTOM"]); export const shapeKind = z.enum(["SCHEMA", "OPENAPI_SCHEMA", "JSON_SCHEMA", "RDF_SHAPE", "SQL_TABLE", "CSV_HEADER", "CLAIMS_SCHEMA", "FREEFORM"]); export const contractKind = z.enum(["OPENAPI", "JSON_SCHEMA", "SQL_SCHEMA", "RDF_SCHEMA", "CSV_PROFILE", "OIDC_DISCOVERY", "VAULT_POLICY", "CUSTOM"]); export const stringMap = z.record(z.string(), z.string()); export const contractRef = z.object({contractKind: contractKind, uri: z.string().optional(), version: z.string().optional(), contentHash: z.string().optional(), metadata: stringMap.optional()}); export const retentionDeleteAction = z.enum(["DELETE", "ANONYMIZE", "PSEUDONYMIZE", "TOMBSTONE", "REVIEW"]); export const retentionSpec = z.object({purpose: z.string().optional(), legalBasis: z.string().optional(), retentionPeriod: z.string().optional(), deleteAction: retentionDeleteAction.optional(), metadata: stringMap.optional()}); export const fieldDescriptorId = z.string(); export const resourceDescriptorId = z.string(); export const resourceDescriptor = z.object({displayName: z.string(), description: z.string().optional(), resourceKind: resourceKind, representationKind: representationKind, shapeKind: shapeKind.optional(), contract: contractRef.optional(), fields: z.array(z.object({fieldPath: z.string(), displayName: z.string().optional(), valueType: z.string(), required: z.boolean().optional(), multiValued: z.boolean().optional(), semanticAttributeId: z.string().optional(), sensitivity: z.string().optional(), retention: retentionSpec.optional(), metadata: stringMap.optional(), fieldDescriptorId: fieldDescriptorId})).optional(), metadata: stringMap.optional(), resourceDescriptorId: resourceDescriptorId, createdAt: z.string(), updatedAt: z.string()}); 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 }; /** * Update a resource descriptor * * PATCH /resource/descriptors/{resourceDescriptorId} */ export const patchApiResourceDescriptorsResourceDescriptorId = async (args: PatchApiResourceDescriptorsResourceDescriptorIdArgs, options?: PatchApiResourceDescriptorsResourceDescriptorIdOptions) => { 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(/\/+$/, '')}/resource/descriptors/${encodeURIComponent(String(args.resourceDescriptorId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(resourceDescriptor, await res.json()) };