x-mcjs
v1.1.0
Published
X-mcjs is a modern, lightweight, and fully-featured HTTP client SDK for Node.js designed to simplify API integration and request management. It provides a clean and intuitive interface for handling GET, POST, PUT, PATCH, DELETE, HEAD, and OPTIONS requests
Maintainers
Readme
Installation
Install using npm:
npm install x-mcjsInstall using yarn:
yarn add x-mcjsInstall using pnpm:
pnpm add x-mcjsUsage
x-mcjs supports both CommonJS and ES Modules.
CommonJS
const Kyxzz = require('x-mcjs');
(async () => {
const { data } = await Kyxzz.get('/ai/gpt', {
q: 'Hello World'
});
console.log(data);
})();ES Modules
import Kyxzz from 'x-mcjs';
const { data } = await Kyxzz.get('/ai/gpt', {
q: 'Hello World'
});
console.log(data);Base URL
The package automatically uses the configured API URL.
Example:
await Kyxzz.get('/ai/gpt');Automatically requests:
GET https://your-api-domain.com/api/ai/gptNo need to manually write the full URL.
GET Request
const { data } = await Kyxzz.get('/ai/gpt', {
q: 'Testing'
});
console.log(data);POST Request
const { data } = await Kyxzz.post('/user/create', {
username: 'Xyraa',
age: 20
});
console.log(data);Query Parameters
All additional properties are automatically converted into query parameters.
await Kyxzz.get('/search', {
q: 'Naruto',
page: 1,
limit: 10
});Generated URL:
/search?q=Naruto&page=1&limit=10Custom Headers
await Kyxzz.get('/ai/gpt', {
q: 'Hello',
headers: {
Authorization: 'Bearer YOUR_TOKEN'
}
});Retry Requests
Automatically retry failed requests.
await Kyxzz.get('/ai/gpt', {
q: 'Hello',
retry: 3
});Cache Responses
Cache a request for 60 seconds.
await Kyxzz.get('/anime', {
cache: 60000
});Timeout
Abort a request after 10 seconds.
await Kyxzz.get('/ai/gpt', {
timeout: 10000
});Upload Files
const form = new FormData();
form.append(
'image',
imageFile
);
const res =
await Kyxzz.postForm(
'/upload',
form
);
console.log(res.data);Abort Requests
const controller =
Kyxzz.cancelToken();
Kyxzz.get('/ai/gpt', {
signal: controller.signal
});
controller.abort();Request Hooks
Before Request
Kyxzz.hook(
'beforeRequest',
({ url }) => {
console.log(
'Request:',
url
);
}
);After Response
Kyxzz.hook(
'afterResponse',
response => {
console.log(
'Status:',
response.status
);
}
);Error Handler
Kyxzz.hook(
'onError',
error => {
console.error(error);
}
);Response Structure
Successful requests return:
{
data: {},
status: 200,
ok: true,
headers: {},
url: '',
metrics: {
duration: 52
},
rateLimit: {
limit: '100',
remaining: '99',
reset: '123456789'
}
}Failed Request
When a network error occurs:
{
creator: 'Xyraa Kyxzz',
status: false,
data: {
message: 'Api invalid',
error: 'fetch failed'
}
}Author
Xyraa Kyxzz
GitHub: https://github.com/Xyraakyzzz
License
MIT
