@cerebral/http (HTTP Provider for Cerebral 2)

raw JSON →
4.0.2 verified Sat Apr 25 auth: no javascript deprecated

HTTP provider for Cerebral 2 (cerebral ^4.0.1), current version 4.0.2. Stable but unmaintained since 2020; the Cerebral ecosystem moved to v5/v6 with breaking changes. Provides factories (httpGet, httpPost, etc.) for use in signals and actions, supporting CORS, file uploads, progress, and abort. Exposes HttpProviderError for error handling. Last updated 2020; recommend migrating to cerebral v6 for continued support.

error Cannot find module '@cerebral/http/operators'
cause The package does not ship operators in the main entry; some bundlers may resolve incorrectly.
fix
Ensure your bundler supports package.json exports or import directly from '@cerebral/http' (main module) and use actions instead.
error TypeError: http.get is not a function
cause HttpProvider not added to controller's providers array.
fix
Add providers: { http: HttpProvider({...}) } in your Module definition.
error Uncaught (in promise) HttpProviderError: Failed to fetch
cause CORS issue or network error; request failed.
fix
Check CORS headers on server, enable withCredentials if needed, or verify URL.
breaking Not compatible with cerebral v5/v6; only works with cerebral ^4.0.1.
fix Use @cerebral/http from cerebral v5+ or migrate to cerebral v6's built-in HTTP provider.
deprecated Package last released in 2020; no longer maintained.
fix Consider using the official cerebral v6 HTTP provider or a custom fetch-based solution.
gotcha Operator factories (httpGet, etc.) must be imported from '@cerebral/http/operators', not '@cerebral/http'.
fix Use `import { httpGet } from '@cerebral/http/operators'`.
gotcha HttpProviderError is a named export, not default; importing as default will break.
fix Use `import { HttpProviderError } from '@cerebral/http'`.
npm install cerebral-http-bugfix-1210
yarn add cerebral-http-bugfix-1210
pnpm add cerebral-http-bugfix-1210

Shows setup of HttpProvider with baseUrl, a signal that does a GET request, and updates state.

import { Controller, Module } from 'cerebral';
import HttpProvider from '@cerebral/http';

const http = HttpProvider({
  baseUrl: 'https://api.example.com',
  headers: { 'Content-Type': 'application/json' },
  withCredentials: false,
});

const app = Module({
  providers: { http },
  state: { items: [] },
  signals: {
    fetchItems: [
      function getItems({ http, state }) {
        return http.get('/items')
          .then(response => ({ result: response.result }));
      },
      ({ state, props }) => { state.set('items', props.result); },
    ],
  },
});

const controller = Controller(app);
// Use with UI framework (React etc.)