import {z} from 'zod' export type DID = unknown; export type AssetIdentifier = string; export type GetApiEntitiesEntityDIDRelationshipsArgs = {entityDID: DID, from?: string | undefined, to?: string | undefined, custodian?: string | undefined, asset?: Array | undefined}; export type GetApiEntitiesEntityDIDRelationshipsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const ownershipProof = z.union([z.object({type: z.enum(["eip-191", "eip-712", "bip-137"]), proof: z.string(), attestation: z.string(), address: z.string().optional(), status: z.string().optional()}), z.object({type: z.literal("screenshot"), url: z.string()}), z.object({type: z.literal("self-declaration"), did: z.string().optional(), address: z.string().optional(), attestation: z.string(), confirmed: z.boolean().optional(), status: z.string().optional()}), z.object({type: z.literal("microtransfer"), did: z.string().optional(), address: z.string().optional(), proof: z.string(), chain: z.string().optional(), destination: z.string().optional(), amountSubunits: z.string().optional(), status: z.string().optional()}), z.object({type: z.string()}).and(z.record(z.string(), z.unknown()))]); export const relationshipWithProofs = 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(), proofs: z.array(ownershipProof)}).and(z.record(z.string(), z.unknown())); export const relationshipList = z.object({relationships: z.array(relationshipWithProofs)}).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 }; /** * List relationships for an entity * * GET /entities/{entityDID}/relationships */ export const getApiEntitiesEntityDIDRelationships = async (args: GetApiEntitiesEntityDIDRelationshipsArgs, options?: GetApiEntitiesEntityDIDRelationshipsOptions) => { const {baseUrl = "https://api.eu1.notabene.id", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.custodian != null) query.append("custodian", String(args.custodian)) for (const item of args.asset ?? []) query.append("asset", String(item)) 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: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(relationshipList, await res.json()) };