Mappersmith REST Client
Mappersmith is a lightweight, isomorphic REST client designed for both Node.js and browser environments, currently stable at version 2.47.1. It streamlines API integration by providing a centralized configuration for all HTTP requests, abstracting away complex network configurations. The library emphasizes a clean, declarative approach to defining API resources and their methods, allowing developers to focus on business logic rather than boilerplate. It supports a robust middleware system for extending client capabilities with features like authentication, retry mechanisms, logging, and error handling. While its release cadence isn't explicitly stated, the high patch version suggests continuous, active development with frequent smaller updates. Its key differentiators include its lightweight nature, isomorphic compatibility, and highly configurable middleware architecture, offering a powerful alternative to more opinionated or heavier HTTP clients.
Common errors
-
TypeError: mappersmith is not a function
cause Attempting to call the main `mappersmith` module directly as a function in CommonJS, rather than accessing its default export.fixCorrect the import for CommonJS: `const forge = require('mappersmith').default;` -
TypeError: Cannot read properties of undefined (reading 'data')
cause Attempting to access methods or properties (like `data()`) on the `response` object before the Promise returned by the client call has resolved.fixEnsure the client call is `await`ed or chained with `.then()` before accessing response properties: `client.Resource.method().then(response => console.log(response.data()));` -
TypeError: (0 , mappersmith_gateway_fetch_1.default) is not a constructor
cause Attempting to import a gateway (e.g., `Fetch`) as a named export (`import { Fetch } from '...'`) when it is a default export from its specific module path.fixUse a default import for gateways: `import Fetch from 'mappersmith/gateway/fetch';` -
Mappersmith: [Middleware] 'YourCustomMiddleware' returned an invalid value. Middleware should return a plain object or a Promise that resolves to a plain object.
cause A custom middleware function did not return a valid `{ request, response }` object (or a subset) or a Promise resolving to such an object, violating the middleware contract.fixReview your custom middleware implementation to ensure it always returns a plain object (e.g., `{ request }`, `{ response }`, or both) or a Promise that resolves to such an object.
Warnings
- deprecated The `context` argument within custom middleware creation is deprecated and will be removed in future major versions. Developers should update their middleware implementations to use the current API.
- gotcha When using Mappersmith with CommonJS, the main `forge` function and specific gateways like `Fetch` are exported as default exports. Directly `require('mappersmith')` or `require('mappersmith/gateway/fetch')` will not provide the expected function/class without appending `.default`.
- gotcha Mappersmith clients return Promises. Directly accessing properties like `response.data()` on the raw Promise object before it resolves will result in `undefined` or a `TypeError`. You must `await` the response or use `.then()`.
- gotcha The built-in `Timeout` middleware configures a client-side timeout for the request. This means the client will stop waiting after the specified duration, but the request might still complete on the server, consuming resources. It does not cancel server-side processing.
Install
-
npm install mappersmith -
yarn add mappersmith -
pnpm add mappersmith
Imports
- forge
import { forge } from 'mappersmith';import forge from 'mappersmith';
- configs
import configs from 'mappersmith';
import { configs } from 'mappersmith'; - Fetch
import { Fetch } from 'mappersmith/gateway/fetch';import Fetch from 'mappersmith/gateway/fetch';
Quickstart
import forge, { configs } from 'mappersmith';
import Fetch from 'mappersmith/gateway/fetch';
// Configure the Fetch gateway globally
configs.gateway = Fetch;
// Define your API client with resources and methods
const githubStatusClient = forge({
clientId: 'github-status-api',
host: 'https://www.githubstatus.com',
resources: {
Status: {
current: { path: '/api/v2/status.json' },
summary: { path: '/api/v2/summary.json' },
components: { path: '/api/v2/components.json' }
},
},
});
// Make an API call and handle the response
githubStatusClient.Status.current()
.then((response) => {
// response.data() contains the parsed JSON body
console.log('Current GitHub Status Summary:', response.data().status.description);
console.log('API Request successful.');
})
.catch((error) => {
console.error('Failed to fetch GitHub status:', error);
if (error.response) {
console.error('Response status:', error.response.status());
console.error('Response data:', error.response.data());
}
});