REST Facade Client
rest-facade is a Node.js module designed to simplify the consumption of REST API endpoints by providing a higher-level abstraction over HTTP requests. As of version 1.16.4, it abstracts common CRUD operations (GET, POST, PUT, PATCH, DELETE) and supports dynamic URL parameters using a colon notation (e.g., `:id`). The library offers both Promise-based and callback-based interfaces for asynchronous operations, catering to different coding styles. It differentiates itself by providing a concise client abstraction for specific API paths, aiming to reduce boilerplate when interacting with structured RESTful services. The package's release cadence is effectively dormant, with the last significant update being several years ago.
Common errors
-
ReferenceError: require is not defined
cause Attempting to use `require()` in an ES Module context (`.mjs` file or `"type": "module"` in `package.json`).fixConvert your project to CommonJS by setting `"type": "commonjs"` in `package.json`, or use an actively maintained ESM-compatible HTTP client. -
Cannot find module 'superagent-proxy'
cause The peer dependency `superagent-proxy` is required for proxy functionality but was not installed.fixInstall the peer dependency: `npm install superagent-proxy`. -
TypeError: rest_facade_1.Client is not a constructor
cause Incorrect import syntax for an old CommonJS module in an ES Module project, or incorrectly trying to destructure `Client` from `require('rest-facade')` without it being a direct named export.fixFor CommonJS, use `const rest = require('rest-facade'); const client = new rest.Client(...);`. If in an ESM project, consider `import * as rest from 'rest-facade'; const client = new rest.Client(...);` though full compatibility with older CJS modules may vary. It's recommended to migrate to an ESM-first client. -
(node:xyz) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'length' of undefined
cause Lack of error handling on Promise-based API calls. The `.then()` block assumes success without handling potential rejections.fixAlways chain a `.catch()` method to Promise calls, e.g., `UserVideos.getAll({ userId: 4 }).then(...).catch(error => console.error(error));`
Warnings
- breaking This package appears to be unmaintained, with its last commit over six years ago (as of April 2026) and last publish three years ago. Users should exercise caution, as it may not receive critical updates, security patches, or compatibility fixes for newer Node.js versions or dependencies. Consider alternatives for long-term projects.
- gotcha The package declares `superagent-proxy` as a peer dependency. This means users *must* explicitly install `superagent-proxy` alongside `rest-facade` for proxy functionality to work. Forgetting to install it will lead to runtime errors if proxy options are configured.
- gotcha This library is primarily designed for CommonJS (CJS) environments and is not officially documented or tested for use with ES Modules (ESM). Attempting to use `import` statements directly might lead to runtime errors or require specific transpilation configurations.
- gotcha While `rest-facade` supports Promises, it also offers a callback-based API. Mixing Promises and callbacks, or relying solely on callbacks, can lead to complex error handling and 'callback hell' in modern async JavaScript.
- gotcha The library uses a colon notation (e.g., `:id`) for dynamic URL parameters. While convenient, complex parameter values (e.g., containing slashes or special characters that are part of the URL structure) might be incorrectly parsed or require careful encoding outside the library.
Install
-
npm install rest-facade -
yarn add rest-facade -
pnpm add rest-facade
Imports
- Client
import { Client } from 'rest-facade';const rest = require('rest-facade'); const client = new rest.Client(...); - rest
import rest from 'rest-facade';
const rest = require('rest-facade');
Quickstart
const rest = require('rest-facade');
// Create a client for a specific API endpoint with dynamic parameters
const Users = new rest.Client('http://domain.com/users/:id', {
headers: {
Authorization: 'Bearer my-auth-token'
},
errorFormatter: {
name: 'error.title',
message: 'error.text'
}
});
const newUser = { firstName: 'Jane', lastName: 'Doe' };
// Example: Create a new user
Users.create(newUser)
.then(function (user) {
console.log('User created:', user);
// Example: Get the created user by ID
return Users.get({ id: user.id });
})
.then(function (retrievedUser) {
console.log('User retrieved:', retrievedUser);
// Example: Update the user
return Users.update({ id: retrievedUser.id }, { firstName: 'Janet' });
})
.then(function () {
console.log('User updated successfully.');
// Example: Delete the user
return Users.delete({ id: newUser.id });
})
.then(function () {
console.log('User deleted successfully.');
})
.catch(function (error) {
console.error('API operation failed:', error.message || error);
});