1.0.0 • Published 10 months ago

@lullaby6/apix v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

apix

apix is a lightweight JavaScript utility designed for making HTTP requests to REST APIs. It includes built-in support for retries, timeouts, and automatic JSON parsing. When making a request, apix ensures that the response is valid by checking if the request was successful and verifying that the response format is JSON before parsing it.

Installation

NPM

Install the library using NPM:

npm i @lullaby6/apix

Import

// CommonJS
const apix = require('@lullaby6/apix');

// ES Modules
import apix from '@lullaby6/apix';

CDN

<script src='https://cdn.jsdelivr.net/gh/lullaby6/apix/apix.min.js'></script>

Download

Download and include the downloaded file in your project:

<script src="/path/to/apix.min.js"></script>

Usage

const data = await apix('https://api.example.com/data');
console.log(data);

With custom options:

const data = await apix('https://api.example.com/data', {
    method: 'POST',
    body: { key: 'value' },
    headers: { 'Authorization': 'Bearer token' },
    timeout: 5000, // 5 seconds timeout
    retries: 3, // Retry up to 3 times
    retryDelay: 2000 // Wait 2 seconds before retrying
});

Timeout

You can specify a timeout in milliseconds using the timeout option. If the request exceeds this duration, it will be automatically aborted.

By default the timeout is 10s.

const data = await apix('https://api.example.com/slow-endpoint', { timeout: 3000 });
console.log(data);

Retries

apix supports automatic retries for failed requests. You can configure the maximum number of retry attempts and the delay between retries using retries and retryDelay options.

By default the retries is 0 and the retrayDelay is 1s.

const data = await apix('https://api.example.com/unstable-endpoint', {
    retries: 5, // Retry up to 5 times
    retryDelay: 1000 // Wait 1 second between retries
});
console.log(data);

License

MIT

1.0.0

10 months ago