JSData HTTP Adapter

raw JSON →
3.0.1 verified Thu Apr 23 auth: no javascript abandoned

This package provides an HTTP (XHR) adapter for the js-data ORM/ODM library, specifically designed for use in browser environments. Its primary function is to enable js-data to interact with RESTful APIs over HTTP, abstracting away direct XMLHttpRequest calls. The current stable version is 3.0.1, released in 2017. The project appears to be largely abandoned, with no significant updates or active development since then, implying a very slow, if any, future release cadence. It differentiates itself by tightly integrating with the js-data ecosystem, allowing developers to leverage js-data's data modeling, caching, and relationship management features while performing standard CRUD operations via HTTP requests. It utilizes XMLHttpRequest internally for its operations, and while Axios is mentioned as a development dependency, the core request mechanism relies on older browser APIs. Users should be acutely aware of its lack of recent maintenance when considering it for new projects, especially regarding security and modern browser compatibility.

error TypeError: HttpAdapter is not a constructor
cause Attempting to instantiate `HttpAdapter` after importing it incorrectly as a default export (e.g., `import HttpAdapter from 'js-data-http'`) or as the entire module in CommonJS (`const HttpAdapter = require('js-data-http')`).
fix
Use named imports: import { HttpAdapter } from 'js-data-http'; for ESM or const { HttpAdapter } = require('js-data-http'); for CommonJS.
error Error: No default adapter is specified or no adapter is specified for resource "users"
cause The `js-data` store was initialized, and resources were defined, but no HTTP adapter was registered or set as the default, preventing HTTP operations.
fix
Ensure you call store.registerAdapter('http', httpAdapter, { default: true }); after creating your HttpAdapter instance and before defining resources or performing queries.
breaking Starting from v3.0.0-beta.3, the package changed its export mechanism from a default export to a named export. This requires updating import statements.
fix Change `import HttpAdapter from 'js-data-http'` to `import { HttpAdapter } from 'js-data-http'` for ESM, and `const HttpAdapter = require('js-data-http')` to `const { HttpAdapter } = require('js-data-http')` for CommonJS.
breaking Version 3.0.0-rc.1 and later of `js-data-http` strictly depends on `js-data` version 3.0.0-rc.4 or greater. Incompatible `js-data` versions will lead to runtime errors.
fix Ensure your `js-data` package is at version `3.0.0-rc.4` or higher (preferably a stable v3.x release) to match the adapter's requirements.
gotcha The `js-data-http` project appears to be abandoned, with its last major release (v3.0.1) in 2017. This means it no longer receives updates, security patches, or bug fixes, making it potentially vulnerable or incompatible with modern browser environments and JavaScript language features.
fix Consider migrating to a more actively maintained data layer or HTTP client library. If continuing to use, thoroughly audit the codebase for security vulnerabilities and prepare for manual maintenance.
npm install js-data-http
yarn add js-data-http
pnpm add js-data-http

Demonstrates initializing a DataStore, registering the HttpAdapter, defining a resource, and performing a basic `findAll` operation to an API endpoint.

import { DataStore } from 'js-data';
import { HttpAdapter } from 'js-data-http';

// Initialize DataStore
const store = new DataStore();

// Instantiate the HTTP Adapter with a base path
// Use environment variable for API_URL, falling back to a placeholder
const httpAdapter = new HttpAdapter({
  basePath: process.env.API_URL ?? 'https://api.example.com/v1',
  // Optionally configure default headers for all requests
  // defaultHeaders: {
  //   'X-API-KEY': 'your-api-key'
  // },
  // Optional: configure Axios instance if you have custom setup
  // axios: myPreconfiguredAxiosInstance
});

// Register the HTTP adapter with the store, making it the default
store.registerAdapter('http', httpAdapter, { default: true });

// Define a resource, which will use the default 'http' adapter
const User = store.defineResource({
  name: 'users',
  // idAttribute: 'id', // Default is 'id'
});

async function fetchAndLogUsers() {
  try {
    // Perform a findAll operation, fetching users from basePath/users
    const users = await User.findAll({}, {
      // Optional: request-specific configuration
      params: { limit: 5 }, // Adds ?limit=5 to the URL
      headers: { 'X-Request-ID': 'unique-id-123' }
    });
    console.log('Successfully fetched users:', users);
  } catch (error) {
    console.error('Error fetching users:', error);
    // Log detailed error from the adapter if available
    if (error.response) {
      console.error('API Response Error:', error.response.status, error.response.data);
    }
  }
}

fetchAndLogUsers();