Client SDK

The @tablecraft/client package provides a type-safe frontend client for TableCraft APIs. It works seamlessly with React (via hooks) or any JavaScript environment (vanilla JS, Vue, Svelte, etc).

Installation

# Using bun
bun add @tablecraft/client

# Using npm
npm install @tablecraft/client

# Using pnpm
pnpm add @tablecraft/client

# Using yarn
yarn add @tablecraft/client

Quick Start

1. Create a Client

import { createClient } from '@tablecraft/client';

const tc = createClient({
  baseUrl: '/api/engine', // Your TableCraft API endpoint
});

2. Query a Table

3. Fetch Metadata


API Reference

createClient(options)

Creates a new TableCraft client instance.

Options

Option
Type
Required
Description

baseUrl

string

Yes

Base URL of your TableCraft API (e.g., /api/engine)

fetch

typeof fetch

No

Custom fetch function for testing or custom environments

axios

AxiosInstance

No

Axios instance. Takes precedence over fetch if provided

headers

Record<string, string> or () => Record<string, string>

No

Default headers for all requests (e.g., auth tokens)

Examples

Basic Usage:

With Static Headers:

With Dynamic Headers (async):

With Custom Fetch (for testing):

With Axios Instance:


tc.table<T>(name)

Returns a typed client for a specific table.


table.query(params?)

Fetches data from the table with optional parameters.

Query Parameters

Parameter
Type
Description

page

number

Page number (1-based)

pageSize

number

Rows per page

cursor

string

Cursor for cursor-based pagination

sort

string | string[]

Sort fields. Prefix with - for descending

filters

Record<string, unknown>

Filter conditions

search

string

Global search term

select

string[]

Fields to select

distinct

boolean

Return distinct rows only

includeDeleted

boolean

Include soft-deleted rows

Filter Syntax

Simple Equality:

With Operators:

Available Operators:

Operator
Description

eq

Equal (default)

neq

Not equal

gt

Greater than

gte

Greater than or equal

lt

Less than

lte

Less than or equal

between

Between two values

in

In a list of values

notIn

Not in a list

contains

Contains substring

startsWith

Starts with

endsWith

Ends with

Sorting

Selecting Fields

Return Type


table.meta()

Fetches table metadata (columns, filters, capabilities).

Return Type

Column Metadata


table.count(params?)

Returns the total count matching the query (without fetching data).


table.export(format, params?)

Exports data as CSV or JSON string.


table.buildUrl(params?)

Builds the URL without making a request. Useful for debugging or custom fetch.


React Hooks

Import React hooks from the /react subpath:

useTableQuery(client, initialParams?)

A React hook for fetching and managing table data with automatic request cancellation.

Return Values

Property
Type
Description

data

T[]

Array of rows

meta

QueryMeta

Pagination metadata

aggregations

Record<string, number>

Aggregation results (if configured)

loading

boolean

Loading state

error

Error | null

Error if request failed

params

QueryParams

Current query parameters

setParams

(params: QueryParams) => void

Set all params at once

setPage

(page: number) => void

Change page

setPageSize

(size: number) => void

Change page size (resets to page 1)

setSort

(sort: string | string[]) => void

Change sort (resets to page 1)

setFilter

(field: string, value: unknown) => void

Set a filter (resets to page 1)

removeFilter

(field: string) => void

Remove a filter

setSearch

(search: string) => void

Set search term (resets to page 1)

refresh

() => void

Re-fetch current data


useTableMeta(client)

A React hook for fetching table metadata.

Return Values

Property
Type
Description

metadata

TableMetadata | null

Table metadata

loading

boolean

Loading state

error

Error | null

Error if request failed


TypeScript Integration

Type-Safe Rows

With Generated Types

Use @tablecraft/codegen to generate types automatically:


Error Handling

The client throws errors with additional properties:

Common Error Codes

Code
HTTP Status
Description

VALIDATION_ERROR

400

Invalid input format

FIELD_ERROR

400

Unknown or hidden field

ACCESS_DENIED

403

Insufficient permissions

NOT_FOUND

404

Table or resource not found

QUERY_ERROR

500

Database query failed


Framework Support

The @tablecraft/client package is framework-agnostic for core functionality, with React hooks built-in:

Feature
React
Vue
Svelte
Vanilla JS

createClient()

table.query()

table.meta()

table.count()

table.export()

useTableQuery() hook

useTableMeta() hook

What's Supported

  • Core client works everywhere (makes HTTP requests)

  • React hooks are built-in (@tablecraft/client/react)

What's NOT Included (Yet)

  • No Vue composables (like useTableQuery)

  • No Svelte stores

💡 Want Vue or Svelte support? We're seeking contributors! See Contributing section below.


Non-React Usage

The client works in any JavaScript environment:

Vanilla JS

Vue 3

Svelte


Best Practices

1. Create a Shared Client Instance

2. Use Type Assertions for Better DX

3. Combine with DataTable Component

4. Cache Metadata for Performance


Contributing

We're actively seeking contributors to expand framework support!

Help Wanted

Framework
Status
What's Needed

Vue 3

🤝 Seeking Contributor

useTableQuery() composable, useTableMeta() composable

Svelte

🤝 Seeking Contributor

createTableStore() store, createMetaStore() store

How to Contribute

  1. Fork the repository

  2. Create a new branch: git checkout -b feature/vue-support

  3. Add your implementation in packages/client/src/vue.ts or packages/client/src/svelte.ts

  4. Add tests in packages/client/test/

  5. Submit a pull request

Vue 3 Composable Example (Expected API)

Svelte Store Example (Expected API)

Interested? Open an issue on GitHubarrow-up-right to discuss!


FAQ

Does using axios increase my bundle size?

No! Axios is an optional peer dependency. If you don't use it, it won't be installed or bundled.

Your Choice
Bundle Size Impact

Use fetch (default)

0 bytes - no axios code included

Use axios

~13KB - only if you explicitly install axios

The axios adapter avoids a static import of the axios library (so axios itself won't be bundled if you don't use it), but the createAxiosFetchAdapter function will still be included in consumer bundles.

When should I use axios vs fetch?

Use fetch (default) when
Use axios when

You want minimal dependencies

You already have axios in your project

Simple API calls

You need request/response interceptors

Fastest bundle size

You use axios features (timeouts, retries, etc.)

Modern browsers/server

You have complex auth/token refresh logic

Can I use other HTTP libraries?

Currently only fetch (native) and axios are supported. For other libraries, you can create a custom adapter by wrapping them to match the fetch API:

Last updated

Was this helpful?