import {z} from 'zod' export type DID = unknown; export type CreateApiEntitiesEntityDIDRelationshipsArgs = {entityDID: DID, body: {from?: string | undefined, to: string, custodian?: string | undefined, proof?: ({status: 'pending' | 'rejected' | 'flagged' | 'verified', did: string, address: string, xpub?: string | undefined, chainSpecificData?: {[key: string]: unknown} | undefined, type: 'eip-191' | 'eip-712' | 'eip-1271' | 'bip-137' | 'bip-322' | 'tip-191' | 'ed25519' | 'xrp-ed25519' | 'xlm-ed25519' | 'cip-8' | 'siwe' | 'siwx', proof: string, attestation: string, wallet_provider?: (string | null) | undefined} | {status: 'pending' | 'rejected' | 'flagged' | 'verified', did: string, address: string, xpub?: string | undefined, chainSpecificData?: {[key: string]: unknown} | undefined, type: 'screenshot', url: string} | {status: 'pending' | 'rejected' | 'flagged' | 'verified', did: string, address: string, xpub?: string | undefined, chainSpecificData?: {[key: string]: unknown} | undefined, type: 'self-declaration', confirmed: boolean, attestation?: string | undefined} | {status: 'pending' | 'rejected' | 'flagged' | 'verified', did: string, address: string, xpub?: string | undefined, chainSpecificData?: {[key: string]: unknown} | undefined, type: 'microtransfer', proof?: string | undefined, chain?: string | undefined, destination?: string | undefined, amountSubunits?: number | undefined}) | undefined, asset?: string | undefined}}; export type CreateApiEntitiesEntityDIDRelationshipsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiEntitiesEntityDIDRelationshipsResponse = z.object({relationship: z.object({"@id": z.string(), "@type": z.string().optional(), from: z.string().min(1), to: z.string().min(1), custodian: z.string().min(3).max(255).nullable().optional(), status: z.enum(["UNCONFIRMED", "CONFIRMED", "PROVEN"]), memoTag: z.string().nullable().optional(), createdTime: z.string().optional(), updatedTime: z.string().optional(), proofId: z.string().optional()}).and(z.record(z.string(), z.unknown()))}); 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 }; /** * Create a new relationship * * POST /entities/{entityDID}/relationships */ export const createApiEntitiesEntityDIDRelationships = async (args: CreateApiEntitiesEntityDIDRelationshipsArgs, options?: CreateApiEntitiesEntityDIDRelationshipsOptions) => { const {baseUrl = "https://api.eu1.notabene.id", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/entities/${encodeURIComponent(String(args.entityDID))}/relationships${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(createApiEntitiesEntityDIDRelationshipsResponse, await res.json()) };