import {z} from 'zod' export type GetApiV4KyaLabelsArgs = {chain?: string | undefined, address: string}; export type GetApiV4KyaLabelsOptions = {baseUrl: string, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const entityLocalized = z.object({en: z.string().optional(), zh: z.string().optional()}); export const entityEntity = z.object({country: entityLocalized.optional(), description: entityLocalized.optional(), entity_id: z.string().optional(), entity_name: entityLocalized.optional(), link: z.string().optional(), other_link: z.string().optional()}); export const labelLabels = z.object({address: z.string().optional(), category: z.string().optional(), chain: z.string().optional(), label: z.string().optional(), source: z.string().optional(), sub_category: z.string().optional(), ts: z.number().int().optional()}); export const getApiV4KyaLabelsResponse = z.object({code: z.number().int().optional(), data: z.object({address: z.string().optional(), chain: z.string().optional(), entities: z.array(entityEntity).optional(), entity: entityEntity.optional(), entity_id: z.string().optional(), entity_ids: z.array(z.string()).optional(), labels: z.array(labelLabels).optional()}).nullable().optional(), message: 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 }; /** * Get labels for a blockchain address * * GET /v4/kya/labels * * Also accepts auth: ApiSecretAuth */ export const getApiV4KyaLabels = async (args: GetApiV4KyaLabelsArgs, options: GetApiV4KyaLabelsOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.chain != null) query.append("chain", String(args.chain)) if (args.address != null) query.append("address", String(args.address)) if (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v4/kya/labels${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(getApiV4KyaLabelsResponse, await res.json()) };