Registry / testing / nice-axios

nice-axios

JSON →
library0.1.33jsnpmunverified

NiceAxios is a plugin-based wrapper around Axios that implements the 'onion model' middleware pattern (inspired by Koa) for intercepting and processing HTTP requests and responses. Current stable version: 0.1.33. It allows developers to compose custom plugins with ordered execution (lower order for pre-request, higher for post-response) using a compose function. Supports TypeScript types, easy extension, and seamless integration with existing Axios instances. Ideal for adding cross-cutting concerns like authentication, logging, and error handling in a modular, reusable way.

npm install nice-axios
INSTALL
IMPORT
SIG · NICE-AXIOS
N
nice-axios
testingjavascriptv0.1.33
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.

createNiceAxios
import { createNiceAxios } from 'nice-axios'
import NiceAxios from 'nice-axios'
Default export is not available; must use named import.
NiceAxiosPlugin
import type { NiceAxiosPlugin } from 'nice-axios'
import { NiceAxiosPlugin } from 'nice-axios'
NiceAxiosPlugin is a TypeScript type; use `import type` to avoid runtime errors in some bundlers.
NiceAxiosExecutor
import type { NiceAxiosExecutor } from 'nice-axios'
import { NiceAxiosExecutor } from 'nice-axios'
NiceAxiosExecutor is a TypeScript type; must be imported as a type.

Shows how to create a NiceAxios instance with a custom plugin that adds an Authorization header to every request.

import { createNiceAxios } from 'nice-axios'; import axios, { AxiosResponse } from 'axios'; // Define a simple plugin to add a token header const addTokenPlugin = async (next, config) => { // Modify config before request if (config?.headers) { config.headers['Authorization'] = 'Bearer ' + (process.env.API_TOKEN ?? 'default-token'); } return next(config).then((result) => { // Modify response if needed return result; }); }; // Create NiceAxios instance with plugins const niceAxios = createNiceAxios( { baseURL: 'https://api.example.com' }, [ { order: -100, // Runs first before request, last after response executor: addTokenPlugin, desc: 'Add authorization token', }, ] ); // Make a request niceAxios.get<AxiosResponse>('/data').then(console.log).catch(console.error);
Debug
Known issues
gotchaPlugin order: lower order values execute first before the request, but after the request they execute in reverse order (higher order first). Misunderstanding the order logic can lead to unexpected middleware execution.
fix
Set pre-request plugins with negative order values (e.g., -100 for early execution) and post-response plugins with positive order values (e.g., 100 for early execution after request).
affects: >=0.0.0
deprecatedThe package documentation mentions an 'afterPluginOption' and 'AjaxConfigMeta', but these types/options are not exported and may be removed in future versions.
fix
Avoid relying on undocumented options; stick to the documented `createNiceAxios` signature and plugin interface.
affects: >=0.1.0
gotchaThe `config` object passed to plugin executors is the same reference throughout the request lifecycle. Mutating it will affect subsequent plugins and the final Axios request.
fix
Clone the config object before mutation if you need to isolate changes, e.g., `const newConfig = { ...config, headers: { ...config.headers, 'X-Custom': 'value' } };`
affects: >=0.0.0
gotchaThe library does not bundle Axios; you must install `axios` separately as a dependency.
fix
Ensure axios is in your package.json dependencies: `npm install axios`
affects: >=0.0.0
gotchaCDN usage: the UMD build expects axios and lodash to be available globally. If lodash is not present, it may cause runtime errors.
fix
Include lodash script tag before the nice-axios UMD script.
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'headers')
Plugin executor is called with undefined config when no Axios config is provided.
fix
Add null check in plugin: `if (config?.headers) { ... }`
Module '"nice-axios"' has no exported member 'NiceAxiosPlugin'.
Trying to import a type at runtime without using `import type`.
fix
Use `import type { NiceAxiosPlugin } from 'nice-axios'`
Uncaught TypeError: axios is not a function
Using the UMD build without including axios script first.
fix
Add `<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>` before the nice-axios UMD script.
TypeError: next is not a function
Plugin executor is not returning `next(config).then(...)` correctly.
fix
Ensure your plugin executor calls `next(config)` and returns the promise chain.
Upgrade
Version history
0.1.33latest on npm
Audit
Dependencies
axiosrequiredCore HTTP client dependency; NiceAxios wraps Axios instances.
Agent activity
9 hits · last 30 days
node
8
Amazon
1
Resources