Registry / http-networking / micro-api-client

micro-api-client

JSON →
library3.3.0jsnpmunverified

This is a compact, promise-based REST API client specifically designed for streamlined interaction with 'micro REST APIs.' It leverages the native `fetch` API for making HTTP requests, simplifying data retrieval from JSON endpoints. The current stable version is 3.3.0, indicating active maintenance with recent patch releases. Key differentiators include its minimal footprint, a clear focus on lightweight API interactions (distinct from comprehensive enterprise API clients), and built-in functionality for handling pagination within JSON responses. The library also provides specialized error types, `HTTPError`, `TextHTTPError`, and `JSONHTTPError`, to facilitate robust error handling. It is designed to be compatible with both browser environments and Node.js, although Node.js usage typically requires a `fetch` polyfill.

npm install micro-api-client
INSTALL
IMPORT
SIG · MICRO-API-CLIENT
M
micro-api-client
http-networkingjavascriptv3.3.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

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

API
import API from 'micro-api-client'
import { API } from 'micro-api-client'
API is the default export of the package.
getPagination
import { getPagination } from 'micro-api-client'
import getPagination from 'micro-api-client'
getPagination is a named export, primarily for internal use or advanced pagination logic.
JSONHTTPError
import { JSONHTTPError } from 'micro-api-client'
const { JSONHTTPError } = require('micro-api-client')
Error classes like JSONHTTPError, HTTPError, and TextHTTPError are named exports, crucial for specific error handling.

Demonstrates instantiating the API client, making a GET request, and handling different types of API errors, including JSONHTTPError and generic HTTP errors. Includes a placeholder for Node.js fetch polyfill.

import API, { JSONHTTPError } from 'micro-api-client'; // For Node.js environments, you might need a fetch polyfill: // import fetch from 'node-fetch'; // global.fetch = fetch; const api = new API('/api/v1', { defaultHeaders: { 'Authorization': `Bearer ${process.env.API_KEY ?? ''}` } }); async function fetchData() { try { const response = await api.request('posts', { method: 'GET', headers: { 'X-Custom-Header': 'Hello' } }); // Check if the response has pagination info if (response.pagination && response.items) { console.log('Paginated items:', response.items); console.log('Pagination info:', response.pagination); } else { console.log('Response data:', response); } } catch (err) { if (err instanceof JSONHTTPError) { console.error('API Error (JSON):', err.json); console.error('Status:', err.status); } else if (err.status) { console.error('HTTP Error:', err.status, err.message); } else { console.error('An unexpected error occurred:', err); } } } fetchData();
Debug
Known issues
breakingMajor version 3 introduced unspecified breaking changes. While specific details are not provided in the README, upgrading from v2.x or earlier to v3.x should be done with caution and thorough testing.
fix
Review the GitHub releases and commit history for v3.0.0 if encountering issues after upgrade. Test existing API client integrations comprehensively.
affects: >=3.0.0
gotchaThis library relies on the global `fetch` API. In Node.js environments, `fetch` is not native and must be polyfilled (e.g., using `node-fetch` or similar libraries) for the client to function correctly.
fix
For Node.js projects, install a `fetch` polyfill (e.g., `npm install node-fetch`) and make sure it's globally available before initializing the `micro-api-client`.
affects: >=1.0.0
gotchaThe package uses custom error classes (`HTTPError`, `TextHTTPError`, `JSONHTTPError`) which must be explicitly imported and checked with `instanceof` for robust error handling. Generic `catch (err)` blocks may not fully leverage the detailed error information provided.
fix
Always import and use the specific error classes provided by the library (e.g., `import { JSONHTTPError } from 'micro-api-client'`) to accurately determine the type of API error and access relevant properties like `err.json` or `err.status`.
affects: >=1.0.0
Errors
Common errors & fixes
ReferenceError: fetch is not defined
Attempting to use `micro-api-client` in a Node.js environment without a global `fetch` polyfill.
fix
Install and configure a `fetch` polyfill (e.g., `npm install node-fetch`) and ensure it's made globally available before any `micro-api-client` calls, e.g., `global.fetch = require('node-fetch');`.
TypeError: API is not a constructor
Incorrectly trying to import `API` as a named export when it is the default export.
fix
Change the import statement from `import { API } from 'micro-api-client'` to `import API from 'micro-api-client'`.
TypeError: Cannot read properties of undefined (reading 'request')
The `API` instance is undefined, often due to an incorrect import or instantiation, leading to `api.request()` failing.
fix
Verify that `API` is correctly imported as the default export (`import API from 'micro-api-client'`) and that `new API()` is called with appropriate arguments.
Upgrade
Version history
3.3.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
micro-api-client — npm install micro-api-client · libregistry