UNPKG

viem

Version:

TypeScript Interface for Ethereum

39 lines 1.39 kB
import { http as http_, } from '../../clients/transports/http.js'; import * as Storage_ from '../Storage.js'; /** * Creates an HTTP transport with support for Zone authentication tokens. * * Reads the authorization token from Storage and injects the * `X-Authorization-Token` header on every request. Batching is disabled * by default because zone tokens are account-scoped. * * @example * ```ts * import { createPublicClient } from 'viem' * import { http, zone } from 'viem/tempo/zones' // or zoneModerato * * const client = createPublicClient({ * chain: zone(6), * transport: http(), * }) * ``` */ export function http(url, config = {}) { const { storage: storage_, onFetchRequest, ...rest } = config; const storage = storage_ ?? Storage_.defaultStorage(); return (config) => http_(url, { ...rest, async onFetchRequest(request, init) { const next = (await onFetchRequest?.(request, init)) ?? init; const headers = new Headers(next.headers); const chainId = config.chain?.id; if (chainId) { const token = (await storage.getItem(`auth:token:${chainId}`)) ?? null; if (token) headers.set('X-Authorization-Token', token); } return { ...next, headers }; }, })(config); } //# sourceMappingURL=transport.js.map