import {z} from 'zod' export type CommonDID = string; export type BlockchainAddressQuerySource = 'ADDRESS_GRAPH'; export type QuerySourceType = 'INTERNAL' | 'EXTERNAL'; export type PaginationPage = number; export type PaginationPerPage = number; export type GetApiV1AddressesArgs = {vasp_did: CommonDID, source: BlockchainAddressQuerySource, source_type?: QuerySourceType | undefined, page?: PaginationPage | undefined, per_page?: PaginationPerPage | undefined}; export type GetApiV1AddressesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const paginationPage = z.number().int(); export const paginationPerPage = z.number().int(); export const blockchainAddressQuerySource = z.literal("ADDRESS_GRAPH"); export const blockchainAddress = z.string(); export const address = z.object({address: blockchainAddress.optional(), owner_vasp_did: z.string().optional(), customer_did: z.string().optional(), customer_ref: z.string().optional()}); export const addressesResponse = z.object({pagination: z.object({page: paginationPage.optional(), per_page: paginationPerPage.optional(), total: z.number().int().optional()}).optional(), metadata: z.object({source: blockchainAddressQuerySource.optional()}).optional(), data: z.array(address).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 }; /** * Get addresses registered * * GET /v1/addresses */ export const getApiV1Addresses = async (args: GetApiV1AddressesArgs, options?: GetApiV1AddressesOptions) => { 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.vasp_did != null) query.append("vasp_did", String(args.vasp_did)) if (args.source != null) query.append("source", String(args.source)) if (args.source_type != null) query.append("source_type", String(args.source_type)) if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/addresses${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(addressesResponse, await res.json()) };