zerion.ts


Overview

The `zerion.ts` file implements a proxy client class named `Zerion` for interacting with the Zerion API. Its primary purpose is to forward incoming HTTP requests from the internal API layer to the Zerion external API while handling:

This proxy enables clients to access Zerion portfolio and token-related data through a unified internal endpoint (`/api/v1/zerion/*`), abstracting away direct dependencies on Zerion's API structure and authentication.


Classes and Functions

class Zerion

A proxy client class for the Zerion API that manages request forwarding with authentication and caching.

Properties

Constants Used

Constructor

constructor()

Initializes the `Zerion` instance by:

Methods


async handler(req: Request, res: Response): Promise<void>

Handles an incoming Express HTTP request and proxies it to the Zerion API with caching.

Parameters
Returns
Description
  1. Request URL Processing:
    Extracts the sub-path to forward by removing the prefix /api/v1/zerion/ from the incoming request URL.

  2. Cache Check:
    Checks if a cached AxiosResponse exists for the full request URL.

    • Cache Hit:

      • Sets all cached response headers on the Express response.

      • Adds the header X-Cache: HIT to indicate a cache hit.

      • Returns the cached response status and data immediately.

    • Cache Miss:

      • Sets header X-Cache: MISS to indicate no cached response.

      • Forwards the request to Zerion API using the Axios instance.

      • Caches the received response keyed by the full request URL.

      • Copies all response headers from Zerion API to the Express response.

      • Sends the response status and data to the client.

  3. Error Handling:
    Catches Axios errors or generic errors and forwards appropriate HTTP status codes and error messages to the client. If the error is unknown, responds with HTTP 500 Internal Server Error.

  4. Cache Expiry:
    Uses setInterval to schedule deletion of the cache entry for the request URL after the TTL expires (CACHE_TTL_MS). Note that the current implementation calls setInterval repeatedly on each request, which may cause unintended repeated cache deletions.

Usage Example
import express from 'express'
import { Zerion } from './zerion'

const app = express()
const zerion = new Zerion()

// Bind the proxy handler to maintain `this` context
app.get('/api/v1/zerion/*', zerion.handler.bind(zerion))

Clients can then call internal endpoints like:

GET /api/v1/zerion/portfolio/0x1234567890abcdef

which will be forwarded to:

GET https://api.zerion.io/v1/portfolio/0x1234567890abcdef

with caching and authentication handled transparently.


Important Implementation Details


Interaction with Other System Parts


Visual Diagram: Class Diagram of Zerion

classDiagram
    class Zerion {
        -axiosInstance: Axios
        -requestCache: RequestCache
        +constructor()
        +handler(req: Request, res: Response): Promise<void>
    }

Summary

The `zerion.ts` file encapsulates the Zerion API proxying functionality in the `Zerion` class. It simplifies client interactions by:

This design promotes modularity, security (by not exposing API keys), and scalability within the larger Proxy API Service architecture.