The Axios API, as a tiny Fetch wrapper. Rewritten in TypeScript with full type definitions and axios-compatible error handling.
This is a fork of developit/redaxios.
The original redaxios by Jason Miller is an excellent project that provides the Axios API in a tiny package using the browser's native fetch(). However, the original project has not been actively maintained since version 0.5.1, and has several limitations:
- No TypeScript source -- written in JavaScript with JSDoc type annotations, resulting in incomplete type definitions.
- Error handling incompatible with Axios -- rejected promises returned the response object directly instead of an
Errorinstance, makingtry/catchpatterns and error inspection unreliable. - Outdated toolchain -- relied on legacy tools (microbundle, karmatic/Jasmine, ESLint + Prettier, npm) that are no longer well-maintained.
This fork addresses all of the above while keeping the original design philosophy: a minimal, drop-in replacement for Axios built on fetch().
- Full TypeScript rewrite with exported types:
Options,Response,RedaxiosError,RedaxiosInstance, etc. - Axios-compatible error handling -- errors are proper
Errorinstances withmessage,status,code(ERR_BAD_REQUEST/ERR_BAD_RESPONSE/ERR_NETWORK/ERR_CANCELED/ECONNABORTED),response,config, andisAxiosErrorflag. axios.isAxiosError()helper for type-safe error checking.- Request cancellation via
AbortController/signaloption, withaxios.isCancel()helper. - Request timeout via
timeoutoption (in milliseconds), usingAbortSignal.timeout(). - Removed deprecated/legacy features:
CancelToken,axios.all(),axios.spread(), XSRF cookie handling. - Modern toolchain: Bun for package management, building, and testing; Biome for linting and formatting.
- ESM + CJS dual output (UMD removed as obsolete).
npm install @kouhin/redaxios
# or
bun add @kouhin/redaxios
# or
pnpm add @kouhin/redaxiosimport axios from '@kouhin/redaxios';
// GET request
const res = await axios.get('/api/users');
console.log(res.data);
// POST request with JSON body
const res = await axios.post('/api/users', {
name: 'Alice',
});
// Error handling (axios-compatible)
try {
await axios.get('/api/not-found');
} catch (err) {
if (axios.isAxiosError(err)) {
console.error(err.message); // "Request failed with status code 404"
console.error(err.status); // 404
console.error(err.code); // "ERR_BAD_REQUEST"
console.error(err.response?.data); // response body
}
}
// Request cancellation
const controller = new AbortController();
axios.get('/api/slow', { signal: controller.signal })
.catch(err => {
if (axios.isCancel(err)) {
console.log('Request canceled');
}
});
controller.abort();
// Request timeout (in milliseconds)
try {
await axios.get('/api/slow', { timeout: 5000 });
} catch (err) {
if (axios.isAxiosError(err) && err.code === 'ECONNABORTED') {
console.error('Request timed out');
}
}
// Create an instance with defaults
const api = axios.create({
baseURL: 'https://api.example.com',
headers: { Authorization: 'Bearer token' },
timeout: 10000,
});
await api.get('/me');This library is designed as a drop-in replacement for Axios. Refer to the Axios Documentation for full API details.
axios(url, config?)/axios(config)axios.get(),.post(),.put(),.patch(),.delete(),.head(),.options()axios.create(defaults)axios.isAxiosError()/axios.isCancel()- Request cancellation via
signal(AbortSignal) - Request timeout via
timeout(milliseconds) - Request body transforms via
transformRequest baseURL,params,paramsSerializer,headers,auth,validateStatusresponseType(text,json,blob,arrayBuffer,stream,formData)- Custom
fetchimplementation viaoptions.fetch
Apache-2.0 -- originally created by Jason Miller at Google.