Registry / devops / mobx-rest

mobx-rest

JSON →
library11.0.0jsnpmunverified

REST conventions for MobX, providing Model and Collection classes with built-in CRUD actions (fetch, save, destroy, etc.) to minimize boilerplate for API interactions. Current stable version is 11.0.0, released sporadically. Key differentiators: integrates with MobX observable state management, supports automatic synchronization with server via RESTful endpoints, and includes ready-to-use React example. Ships TypeScript definitions.

npm install mobx-rest
INSTALL
IMPORT
SIG · MOBX-REST
M
mobx-rest
devopsjavascriptv11.0.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Model
import { Model } from 'mobx-rest'
const Model = require('mobx-rest').Model
ESM-only since v11; CommonJS require may still work but is not recommended.
Collection
import { Collection } from 'mobx-rest'
const { Collection } = require('mobx-rest');
Use named import for both Model and Collection.
apiClient
import { apiClient } from 'mobx-rest'
import apiClient from 'mobx-rest';
apiClient is a named export, not default.

Defines Task model and Collection, fetches tasks, creates, updates, and deletes a task via REST.

import { observable, configure } from 'mobx'; import { Model, Collection, apiClient } from 'mobx-rest'; configure({ enforceActions: 'never' }); // optional, for simplicity class Task extends Model { urlRoot() { return '/tasks' } primaryKey() { return 'id' } } class TaskCollection extends Collection { url() { return '/tasks' } model = Task } const tasks = new TaskCollection(); tasks.fetch(); // GET /tasks const task = new Task({ title: 'Learn mobx-rest', done: false }); tasks.add(task); task.save(); // POST /tasks task.set({ done: true }); task.save(); // PUT /tasks/1 task.destroy(); // DELETE /tasks/1
Debug
Known issues
breakingVersion 11.0.0 drops support for older MobX versions; requires MobX >=6.3.2.
fix
Upgrade mobx to ^6.3.2 or later.
affects: >=11.0.0
breakingESM-only exports; CommonJS require may not work as expected in some bundlers.
fix
Use ES import syntax: import { Model } from 'mobx-rest'.
affects: >=11.0.0
deprecatedThe `defaultAttributes` property is still present but may be removed in future versions.
fix
Initialize attributes directly in constructor instead.
affects: >=11.0.0
gotchaIf you do not set a collection on a model, you must implement urlRoot() or url()
fix
Provide urlRoot() method or assign model to a collection.
affects: >=1.0.0
gotchaMobX strict mode (enforceActions) may cause errors when modifying observable attributes without action.
fix
Wrap modifications in action() or configure MobX with enforceActions: 'never'.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: Cannot read property 'Model' of undefined
Using CommonJS require on ESM-only export
fix
Change to import { Model } from 'mobx-rest'
Error: [MobX] Since strict-mode is enabled, changing observed observable values outside actions is not allowed.
Modifying observable attributes without wrapping in action().
fix
Wrap model.set() calls in runInAction() or configure MobX with enforceActions: 'never'.
404 Not Found during fetch
Model or Collection url not matching server routes.
fix
Ensure urlRoot() or url() returns correct endpoint.
mobx-rest requires mobx 6.3.2 or higher
Using an older version of mobx (<6.3.2).
fix
npm install mobx@^6.3.2
Upgrade
Version history
11.0.0latest on npm
Audit
Dependencies
mobxrequiredPeer dependency; mobx-rest relies on MobX for observable state and reactions.
Agent activity
2 hits · last 30 days
node
2
Resources
mobx-rest — npm install mobx-rest · libregistry