REST & Axios

If your backend is a standard REST API, or if your project relies heavily on axios for request interceptors (like automatically attaching auth tokens or handling 401 refreshes), TableCraft makes it incredibly easy to connect your data.


1. "Hello World" (Static Data Adapter)

To understand how simple an adapter can be, here is one that returns static, hardcoded data. It ignores pagination and filtering entirely but satisfies the contract.

import type { DataAdapter } from '@tablecraft/table';

interface User { id: number; name: string; }

const myStaticAdapter: DataAdapter<User> = {
  async query(params) {
    const data = [
      { id: 1, name: "Alice" },
      { id: 2, name: "Bob" }
    ];
    
    return {
      data,
      meta: {
        total: data.length,
        page: params.page,
        pageSize: params.pageSize,
        totalPages: 1
      }
    };
  }
};

2. Example: Native fetch Adapter

TableCraft provides a built-in helper called createRestAdapter. You just need to supply the queryFn that maps the QueryParams to your URL string.

This is a robust example of connecting TableCraft to a generic REST API using the native fetch API.


3. Example: axios Adapter

Many enterprise applications use axios because it allows you to configure global instances with interceptors. You can easily wrap your existing Axios setup in a DataAdapter.

First, you might have an Axios instance set up in your project:

Then, you create an adapter that uses that instance:

This pattern completely decouples your <DataTable> component from knowing how the data is fetched. The component just renders, and the Axios adapter handles all the heavy lifting and interceptor logic.

Last updated

Was this helpful?