JSData HTTP Adapter
raw JSON →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.
Common errors
error TypeError: HttpAdapter is not a constructor ↓
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" ↓
store.registerAdapter('http', httpAdapter, { default: true }); after creating your HttpAdapter instance and before defining resources or performing queries. Warnings
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. ↓
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. ↓
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. ↓
Install
npm install js-data-http yarn add js-data-http pnpm add js-data-http Imports
- HttpAdapter wrong
import HttpAdapter from 'js-data-http';correctimport { HttpAdapter } from 'js-data-http'; - HttpAdapter wrong
const HttpAdapter = require('js-data-http');correctconst { HttpAdapter } = require('js-data-http'); - HttpAdapter
const adapter = new window.JSDataHttp.HttpAdapter();
Quickstart
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();