Registry / storage / fetch-on-rest

fetch-on-rest

JSON →
library2.0.4jsnpmunverified

A lightweight RESTful API wrapper around window.fetch for browser and React Native environments. Version 2.0.4 focuses on simplicity: no Babel or ES2015 dependencies, making it smoother for React Native. It automatically handles JSON headers and responses, and offers methods like get, post, put, patch, del, and rawGet. The library uses URI.js for robust URL parsing. It is best suited for simple REST API interactions where manual fetch configuration is unwanted.

npm install fetch-on-rest
INSTALL
IMPORT
SIG · FETCH-ON-REST
F
fetch-on-rest
storagejavascriptv2.0.4
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.

Rest
import Rest from 'fetch-on-rest'
const Rest = require('fetch-on-rest').Rest
The module exports a single constructor as default. CommonJS require works directly as shown in README.
Rest
const Rest = require('fetch-on-rest')
const { Rest } = require('fetch-on-rest')
Default export is the constructor; named import 'Rest' does not exist.
Rest
new Rest('/api')
Rest('/api')
Rest must be instantiated with 'new'; calling it without new will not work.

Shows basic initialization, GET, POST, and DELETE (del) requests with query params and JSON body.

import Rest from 'fetch-on-rest'; const api = new Rest('/api/v2'); async function run() { // GET request with query params const users = await api.get('users', { name: 'foo' }); console.log(users); // POST request with body const newPost = await api.post('posts', { title: 'foo', content: 'bar' }); console.log(newPost); // DELETE request (renamed from delete to del in v2) await api.del('users/1'); } run().catch(console.error);
Debug
Known issues
breakingMethod `delete` renamed to `del` in version 2. Using `.delete` will throw an error.
fix
Replace all `.delete` calls with `.del`.
affects: >=2.0.0
deprecatedThe `addOptions` function must mutate the `defaultOptions` object and not return a new object. Returning a new object will have no effect.
fix
Ensure `addOptions` modifies `defaultOptions` directly without returning.
affects: >=1.0.0
gotchaThe library does not validate HTTP status codes; it always resolves the promise. Non-2xx responses are treated as success unless you manually check.
fix
Check response status inside the then handler or use a custom wrapper.
affects: >=1.0.0
gotcha`rawGet` returns a string, not JSON. Using `.then()` on `rawGet` expecting JSON will cause an error.
fix
Only use `rawGet` when you expect a text response.
affects: >=1.0.0
gotchaThe `basePath` is concatenated with segments exactly. If `basePath` does not end with a slash, e.g. '/api', then `.get('users')` will call '/apiusers'. Use trailing slashes or empty base path as needed.
fix
Set `useTrailingSlashes` to true or ensure `basePath` ends with '/'.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: Rest is not a constructor
Importing the module incorrectly or calling it without 'new'.
fix
Use `const Rest = require('fetch-on-rest');` then `new Rest();`
api.delete is not a function
The method was renamed to `del` in version 2.
fix
Replace `api.delete(...)` with `api.del(...)`.
Uncaught SyntaxError: Unexpected token u in JSON at position 0
Using `rawGet` and treating its response as JSON.
fix
Use `.get` instead of `.rawGet` for JSON responses, or parse manually.
404 Not Found when using GET /users
`basePath` missing trailing slash, causing malformed URL (e.g., `/apiusers`).
fix
Set `basePath` to end with '/' or enable `useTrailingSlashes`.
Upgrade
Version history
2.0.4latest on npm
Audit
Dependencies
urijsrequiredUsed for URL parsing, segment handling, and query building
Agent activity
18 hits · last 30 days
node
14
Amazon
1
OpenAI (training)
1
Resources
fetch-on-rest — npm install fetch-on-rest · libregistry