zrx.ts

Overview

`zrx.ts` implements a proxy client class `Zrx` for the 0x API, a decentralized exchange liquidity aggregator API. This file provides a middleware-compatible handler designed to forward incoming HTTP requests from the application's Express server to the appropriate 0x API endpoint, transparently relaying responses back to the client.

The primary purpose of this proxy is to:

This proxy allows clients to access multi-chain 0x liquidity and swap quote services through a unified internal endpoint, abstracting complexities of multiple base URLs and API versions.


Class: Zrx

Description

The `Zrx` class encapsulates the logic to forward HTTP GET requests to the 0x API, handling:

This class is intended to be instantiated once and its [.handler()](/projects/291/69247) method used as an Express route handler.


Properties

Property

Type

Description

`axiosInstance`

`Axios`

An Axios HTTP client instance configured with the 0x API key header for authentication.


Constructor

constructor()

Methods

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

import express from 'express';
import { Zrx } from './zrx';

const app = express();
const zrxProxy = new Zrx();

app.get('/api/v1/zrx/*', zrxProxy.handler.bind(zrxProxy));

app.listen(3000, () => {
  console.log('Server running on port 3000');
});

In this example, all requests to `/api/v1/zrx/*` are proxied through the `Zrx` class handler.


Important Implementation Details


Interaction With Other Parts of the System


Mermaid Class Diagram

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

Summary

The `zrx.ts` file defines a single class `Zrx` that acts as an authenticated, multi-chain proxy client for the 0x API. It dynamically routes requests based on blockchain networks, forwards headers and responses transparently, and integrates seamlessly with the Express-based Proxy API Service. This design enables secure, version-aware, and simplified access to 0x liquidity and swap services across multiple chains.


If you have any questions or need further examples on using the `Zrx` proxy, feel free to ask!