laziness-axios
v1.0.1
Published
🚀 Axios Lazy – Một wrapper Axios không cần thiết lập, có đầy đủ kiểu dữ liệu dành cho các nhà phát triển lười biếng. Phiên bản được cấu hình sẵn, bộ chặn tích hợp sẵn và xuất kiểu dữ liệu hoàn chỉnh. Chỉ cần nhập và chờ.😎
Readme
Lazy Axios for lazy people
🚀 Axios Lazy – Một wrapper Axios không cần thiết lập, có đầy đủ kiểu dữ liệu dành cho các nhà phát triển lười biếng. Phiên bản được cấu hình sẵn, bộ chặn tích hợp sẵn và xuất kiểu dữ liệu hoàn chỉnh. Chỉ cần nhập và chờ.😎
Axios Lazy là một thư viện “kế thừa Axios" dành cho những lập trình viên lười (nhưng thông minh).
Chỉ cần cài đặt và import – bạn sẽ có ngay:
- ⚙️ Một Axios instance cấu hình sẵn với baseURL, timeout, và interceptor.
- 🔁 Tự động xử lý token, request/response interceptors.
- 🧩 Tích hợp đầy đủ type của Axios (AxiosResponse, AxiosError, v.v.).
- 💤 Không cần setup gì thêm — chỉ cần import và dùng.
import { httpRequest } from "lazy-axios";
export const instance = httpRequest({
config: {
authType: "none",
baseURL: "https://your-api-base-url.com",
headers_parameters: {
"key": "value",
},
},
});Interceptors Features
* Tự động xử lý token trong header.
* Cấu hình request interceptors tùy chỉnh của bạn hoặc dùng mặc định đã cấu hình sẵn.
* Cấu hình response success interceptors tùy chỉnh của bạn hoặc dùng mặc định đã cấu hình sẵn.
* Cấu hình response error interceptors tùy chỉnh của bạn hoặc dùng mặc định đã cấu hình sẵn, và đang hỗ trợ các error sau.
- Lỗi khi server có quá nhiều request vào (429 Too Many Requests).
- Lỗi khi bạn không quyền lấy tài nguyên (403 Forbidden).
- Lỗi khi bạn không có quyền truy cập vào tài nguyên (401 Unauthorized).
- Đang update thêm các lỗi khác.Installation
Package manager
Using npm:
npm install axiosUsing yarn:
yarn add axiosUsing pnpm
pnpm add axiosOnce the package is installed, you can import the library using import or require approach.
For ES6 modules:
import axios, { httpRequest } from "lazy-axios";For CommonJS modules:
const axios = require("lazy-axios");
const { httpRequest } = require("lazy-axios");const instance = httpRequest({
config: {
authType: "none",
baseURL: "https://dyour-api-base-url.com",
headers_parameters: {
"key": "value",
},
},
interceptors: {
response: {},
request: {},
},
token: "123456",
});
Chi tiết thuộc tính
Request Config
Kế thừa tất cả thuộc tính từ Axios
Ngoài ra tôi còn support thêm các thuộc tính sau:
config: {
authType: "none",
baseURL: "https://dyour-api-base-url.",
headers_parameters: {
"client-id": "test_client_id",
"client-secret": "test_client_secret",
},
},
// Loại xác thực default là "none"
authType: "bearer" | "basic" | "api_key" | "none";
// Content-Type header default là "application/json"
contentType?: string;
// Accept header default là "application/json"
acceptHeader?: string;
// Your custom headers
// Example: { "X-Custom-Header": "value" }
headers_parameters: {
[k: string]: string;
};Interceptors Config
Kế thừa tất cả thuộc tính từ Axios
Và tôi đã phát triển các tính năng về request, response interceptors, và bộ xử lý lỗi (forbidden, unauthorized, too many requests).
Cách dùng như sau:
interceptors: {
// Your custom Response Interceptors with AxiosResponse
response: {
// Case error
error: {
manyRequest: {
args: {
retries: 3,
delayMs: 1100,
},
functions: async ({ ...args }) => {
Your custom function here
},
},
forbidden: {
args: {
redirect_url: "/403",
},
functions: async ({ ...args }) => {
Your custom function here
},
},
unauthorized: {
args: {
redirect_url: "/login",
},
functions: async ({ ...args }) => {
Your custom function here
},
},
},
// Case success
success: () => {
Your custom function here
},
},
// Your custom Request Interceptors
request: {
"key": "value",
},
}Chi tiết thuộc tính
interceptors: {
// Your custom Request Interceptors with AxiosRequestConfig
request: {
"key": "value",
},
// Your custom Response Interceptors with AxiosResponse
response:{
// Your custom Error Interceptors with AxiosError
error:{},
// Your custom Success Interceptors with AxiosResponse
success:()=>{},
}
}interceptors: {
response:{
// Your custom Success Interceptors with AxiosResponse
success:()=>{
// Your custom function here
},
}
}
Case many request with status 429 Too Many Requests
interceptors: {
response:{
// Your custom Error Interceptors with AxiosError
error:{
manyRequest: {
args: {
retries: 3,
delayMs: 1100,
},
},
},
}
}
// argument for config
args?: {
/**
* Config of request
*/
config?: AxiosRequestConfig;
/**
* Number of retries
*/
retries?: number;
/**
* Delay between retries in milliseconds
*/
delayMs?: number;
/**
* Instance of axios
*/
instance?: AxiosInstance;
}
// Function to handle your logic many request error
functions?: ({ ...params }: args) => MaybePromise<AxiosResponse<any>>;
Case status 401 Authentication request rejected or invalid
interceptors: {
response:{
// Your custom Error Interceptors with AxiosError
error:{
unauthorized: {
args: {
retries: 3,
delayMs: 1100,
},
},
},
}
}
// argument for config
args?: {
/**
* Redirect url when unauthorized
*/
redirect_url?: string;
/**
* Url of request
*/
url?: string;
/**
* Method of request
*/
method?: string;
}
// Function to handle your logic unauthorized error
functions?: ({ ...params }: args) => MaybePromise<void>;
Case status 403 You no longer have access to system resources.
interceptors: {
response:{
// Your custom Error Interceptors with AxiosError
error:{
unauthorized: {
args: {
retries: 3,
delayMs: 1100,
},
},
},
}
}
// argument for config
args?: {
/**
* Redirect url when unauthorized
*/
redirect_url?: string;
/**
* Url of request
*/
url?: string;
/**
* Method of request
*/
method?: string;
}
// Function to handle your logic forbidden error
functions?: ({ ...params }: args) => MaybePromise<void>;
Những trường hợp lỗi đang phát triển và sẽ phát hành trong tương lai
status 404 Couldn't find the resource you needed?
notFound()
status 409 is in conflict with server resources
conflict()
status 422 Missing required field, or invalid data.()
unprocessable
status > 500 error from server
serverError()