Axios PhraseApp
Axios middleware/interceptor that helps to guard a bit against PhraseApp rate limiting. Highly configurable
Installation
npm install axios-phraseapp
Note :rotating_light:
Not working with axios 0.19.0
(As most other libraries). For details see the bug. axios 0.19.1
has fixed this bug.
Usage
The simple, no configuration behaviour of axios-phraseapp
is to add a phraseApp
property on the request/response configuration of axios requests.
const { config } = await axios.get(PHRASEAPP_REQUEST);
console.log(config.phraseApp);
This phraseApp
property has following structure that you can extract information returned from the PhraseApp API calls and even add your own logic on top of them.
{
moduleConfig : {},
state: {
inFlightRequests: 0,
remaining: null,
reset: null,
limit: null,
resetDate: null,
started: false
}
}
Importing
const { attach, detach } = require("axios-phraseapp");
import { attach, detach } from "axios-phraseapp";
import {
attach as phraseAppAttach,
detach as phraseAppDetach
} from "axios-phraseapp";
Attaching
attach( [ AxiosInstance|AxiosStatic ], [ AxiosPhraseAppConfig ] ) : Array<InterceptorId, InterceptorId>
The attach
method, registers the request interceptors on either an axios
instance or the main imported axios
object by default.
import axios from "axios";
attach(axios);
import axios from "axios";
attach();
import axios from "axios";
const axiosInstance = axios.create({});
attach(axiosInstance);
* The return value of the attach
method, is an array <InterceptorId, InterceptorId>
that can be then passed as it is to detach
and remove the interceptors.
Detaching
To remove the axios-phraseapp
interceptors you can use the detach
method.
import axios from "axios";
const interceptorIds = attach(axios);
detach(axios, interceptorIds);
import axios from "axios";
const axiosInstance = axios.create({});
const interceptorIds = attach(axiosInstance);
detach(axiosInstance, interceptorIds);
Configuration
The second argument to the attach
method, can take a custom configuration object, or uses some non-intrusive defaults.
{
cancelOnQuota?: boolean;
logOnLimit?: number;
logFunction?: function;
}
Next Steps
License
refer to LICENSE
file in this repository.
:exclamation: This repository is in no way affiliated with PhraseApp