@datasketch/bases-client
v3.1.1
Published
A TypeScript client library for interacting with the Bases API service. This library provides a simple and type-safe interface for managing tables, records, fields, and views.
Downloads
73
Keywords
Readme
@datasketch/bases-client
A TypeScript client library for interacting with the Bases API service. This library provides a simple and type-safe interface for managing tables, records, fields, and views.
Installation
npm install @datasketch/bases-clientQuick Start
import BasesClient from '@datasketch/bases-client';
// Initialize the client with your token
const client = new BasesClient({
token: 'your-api-token',
tbl: 'your-table-id' // Optional: can be set later
});
// If table wasn't set in constructor, set it before making requests
await client.setTable('your-table-id');
// Fetch all records
const { data, fields } = await client.getRecords();
console.log(data); // Array of records
console.log(fields); // Array of field definitionsConfiguration
The BasesClient constructor accepts a Config object:
type Config = {
token: string; // Required: Your API authentication token
tbl?: string; // Optional: Table ID (can be set later with setTable())
};API Reference
Constructor
new BasesClient(config: Config)Creates a new BasesClient instance. If a table ID is provided in the config, authentication will happen automatically on the first request.
Methods
setTable(tbl: string): Promise<void>
Sets the active table for subsequent operations. If the table changes, the authentication token will be invalidated and a new one will be requested.
await client.setTable('new-table-id');getRecords<T>(props?: getRecordsProps): Promise<{ data: T[], fields: Field[] }>
Fetches all records from the current table.
Parameters:
props.mapValues(optional, default:true): Iftrue, maps field IDs to their labels in the returned data.
Returns: An object containing:
data: Array of records of typeTfields: Array of field definitions
Example:
// With field mapping (default)
const { data, fields } = await client.getRecords<MyRecordType>();
// data will have keys as field labels
// Without field mapping
const { data, fields } = await client.getRecords<MyRecordType>({ mapValues: false });
// data will have keys as field IDsgetRecord<T>(props: getRecordProps): Promise<{ data: T | null, fields: Field[] | null }>
Fetches a single record by ID.
Parameters:
props.id(required): The ID of the record to fetchprops.mapValues(optional, default:true): Iftrue, maps field IDs to their labels
Returns: An object containing:
data: The record of typeT, ornullif not foundfields: Array of field definitions, ornullif record not found
Example:
const { data, fields } = await client.getRecord<MyRecordType>({
id: 'record-id-123'
});getFields<T>(): Promise<Field[]>
Fetches all field definitions for the current table.
Returns: Array of field objects with the following structure:
type Field = {
id: string;
label: string;
type: string;
tbl: string;
fld___id: string;
};Example:
const fields = await client.getFields();
console.log(fields); // Array of field definitionsinsertRecord<T>(data: T): Promise<{ success: boolean }>
Inserts a single record into the table.
Parameters:
data: The record data to insert (typeT)
Returns: An object with a success boolean
Example:
const result = await client.insertRecord({
name: 'John Doe',
email: '[email protected]'
});
console.log(result.success); // true if successfulinsertRecords<T>(data: T[]): Promise<{ success: boolean }>
Bulk inserts multiple records into the table.
Parameters:
data: Array of records to insert (typeT[])
Returns: An object with a success boolean
Example:
const result = await client.insertRecords([
{ name: 'John Doe', email: '[email protected]' },
{ name: 'Jane Smith', email: '[email protected]' }
]);updateRecord<T>(id: string, data: T): Promise<{ success: boolean }>
Updates a single record by ID.
Parameters:
id: The ID of the record to updatedata: The updated record data (typeT)
Returns: An object with a success boolean
Example:
const result = await client.updateRecord('record-id-123', {
name: 'Updated Name',
email: '[email protected]'
});updateRecords<T>(data: T[]): Promise<{ success: boolean }>
Bulk updates multiple records.
Parameters:
data: Array of records to update (typeT[]). Each record should include anidfield.
Returns: An object with a success boolean
Example:
const result = await client.updateRecords([
{ id: 'record-1', name: 'Updated Name 1' },
{ id: 'record-2', name: 'Updated Name 2' }
]);deleteRecord(id: string): Promise<{ success: boolean }>
Deletes a single record by ID.
Parameters:
id: The ID of the record to delete
Returns: An object with a success boolean
Example:
const result = await client.deleteRecord('record-id-123');createView(id: string, table: string, type: string, config: object): Promise<{ success: boolean }>
Creates a new view for a table.
Parameters:
id: The view IDtable: The table IDtype: The view typeconfig: View configuration object
Returns: An object with a success boolean
Example:
const result = await client.createView(
'view-id-123',
'table-id-456',
'chart',
{ xAxis: 'date', yAxis: 'value' }
);updateView(id: string, config: object): Promise<{ success: boolean }>
Updates an existing view's configuration.
Parameters:
id: The view IDconfig: Updated view configuration object
Returns: An object with a success boolean
Example:
const result = await client.updateView('view-id-123', {
xAxis: 'new-date',
yAxis: 'new-value'
});deleteView(id: string): Promise<{ success: boolean }>
Deletes a view by ID.
Parameters:
id: The view ID
Returns: An object with a success boolean
Example:
const result = await client.deleteView('view-id-123');getView(id: string): Promise<{ success: boolean }>
Fetches a view by ID.
Parameters:
id: The view ID
Returns: An object with a success boolean
Example:
const result = await client.getView('view-id-123');Error Handling
The library throws two types of errors:
AuthenticationError
Thrown when authentication fails (invalid credentials, missing token, etc.).
import { AuthenticationError } from '@datasketch/bases-client';
try {
await client.getRecords();
} catch (error) {
if (error instanceof AuthenticationError) {
console.error('Authentication failed:', error.message);
}
}BasesClientError
Thrown for general API errors (HTTP errors, invalid responses, etc.).
import { BasesClientError } from '@datasketch/bases-client';
try {
await client.getRecords();
} catch (error) {
if (error instanceof BasesClientError) {
console.error('API error:', error.message);
}
}Field Mapping
By default, the getRecords() and getRecord() methods map field IDs to their human-readable labels. This means:
- With
mapValues: true(default): Record keys use field labels (e.g.,"Full Name","Email Address") - With
mapValues: false: Record keys use field IDs (e.g.,"fld123","fld456")
// Field labels as keys
const { data } = await client.getRecords({ mapValues: true });
// data[0] = { "Full Name": "John", "Email": "[email protected]" }
// Field IDs as keys
const { data } = await client.getRecords({ mapValues: false });
// data[0] = { "fld123": "John", "fld456": "[email protected]" }TypeScript Support
This library is written in TypeScript and provides full type definitions. You can use generic types to ensure type safety:
interface User {
id: string;
name: string;
email: string;
}
const client = new BasesClient({ token: 'your-token' });
await client.setTable('users-table');
// Type-safe record fetching
const { data } = await client.getRecords<User>();
// data is typed as User[]
// Type-safe record operations
await client.insertRecord<User>({
id: '123',
name: 'John Doe',
email: '[email protected]'
});Authentication
The client automatically handles authentication using Bearer token authentication. When you make your first request (or when the table changes), the client will:
- Send a POST request to
/auth/generatewith your credentials - Receive an authentication token
- Use this token in the
Authorizationheader for subsequent requests
The token is cached and reused until the table changes or the client is reinitialized.
License
ISC
