import {z} from 'zod' export type CoreSortOrder = 'ASC' | 'DESC'; export type CoreAccountAddressScope = 'Internal' | 'External'; export type GetApiV1DomainsDomainIdAccountsAccountIdAddressesArgs = {domainId: string, accountId: string, limit?: number | undefined, startingAfter?: string | undefined, sortBy?: ('id' | 'ledgerId' | 'address' | 'createdAt' | 'scope') | undefined, sortOrder?: CoreSortOrder | undefined, ledgerId?: Array | undefined, lastOnly?: boolean | undefined, scope?: Array | undefined}; export type GetApiV1DomainsDomainIdAccountsAccountIdAddressesOptions = {baseUrl: string, httpAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const coreAccountAddressData = z.object({id: z.string(), ledgerId: z.string().min(1).max(100), domainId: z.string().optional(), accountId: z.string().optional(), address: z.string().min(1).max(512), keyPath: z.string().min(1).optional(), createdAt: z.string(), scope: z.enum(["Internal", "External"])}); export const coreAccountAddress = z.object({data: coreAccountAddressData, id: z.string(), ledgerId: z.string().min(1).max(100), address: z.string().min(1).max(512), keyPath: z.string().min(1).optional(), signature: z.string().optional(), signingKey: z.string().optional(), createdAt: z.string(), scope: z.enum(["Internal", "External"])}); export const coreAddressesCollection = z.object({items: z.array(coreAccountAddress), count: z.number().int(), currentStartingAfter: z.string().optional(), nextStartingAfter: 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 }; /** * Retrieve all generated addresses * * GET /v1/domains/{domainId}/accounts/{accountId}/addresses */ export const getApiV1DomainsDomainIdAccountsAccountIdAddresses = async (args: GetApiV1DomainsDomainIdAccountsAccountIdAddressesArgs, options: GetApiV1DomainsDomainIdAccountsAccountIdAddressesOptions) => { const {baseUrl, fetch: fetchFn = fetch, httpAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.startingAfter != null) query.append("startingAfter", String(args.startingAfter)) if (args.sortBy != null) query.append("sortBy", String(args.sortBy)) if (args.sortOrder != null) query.append("sortOrder", String(args.sortOrder)) for (const item of args.ledgerId ?? []) query.append("ledgerId", String(item)) if (args.lastOnly != null) query.append("lastOnly", String(args.lastOnly)) for (const item of args.scope ?? []) query.append("scope", String(item)) if (httpAuth !== undefined) headers.set('authorization', `Bearer ${httpAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/domains/${encodeURIComponent(String(args.domainId))}/accounts/${encodeURIComponent(String(args.accountId))}/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(coreAddressesCollection, await res.json()) };