apinterface v4.0.0
APInterface
APInterface aims to provide a fluid interface to a collection of different APIs.
Supported Services
Is there a service you'd like to see added? Let me know.
Installation
npm i --save apinterface
Usage
import { Twitch } from 'apinterface';
Twitch.setConfig({
client_id: '',
access_token: '',
});
Twitch.getFollowers('oyed', {
limit: 20,
}).then(data => {
//
});
Automatically Refresh Tokens
For services that don't supply long-lived Tokens, you need to refresh them every once in a while. APInterface will allow you to do this automatically when it detects an invalid/expired Token, but requires a bit of setup.
Web Applications
As refreshing an Access Token requires your Application Secret Key, which should never be publicly visible, APInterface will require a URL to send a request to in order to refresh the Access Token. Here are a few Guides on implementing refreshing Tokens:
You can then specify a refresh.url
when setting the config for a given Interface, for example:
YouTube.refresh.url = '/my/refresh/url';
APInterface will send a request to this URL and expects a JSON response with keys matching the given Interfaces config keys. For example, the YouTube Interface requires an access_token
to be given through YouTube.setConfig
, so when a request is made to the Refresh URL, it'll expect the response JSON to contain an access_token
field containing the newly refreshed Access Token.
Any information in the JSON response will be run through Api.setConfig
for the given Interface. You can also pass data to the Refresh URL by passing through the refresh.data
config parameter:
YouTube.refresh = {
url: '/my/refresh/url',
data: {
hello: 'world',
},
};
This data will be passed to the Refresh URL as GET
parameters.
Node Applications
As Node is server-side, you don't have to send a request to your server, getting rid of some overhead and generally making the process a little easier. Instead of passing through a refresh.url
, you can instead pass a refresh.callback
, which will be called when a Token Refresh is needed.
APInterface expects this function to return a Promise
. For example:
YouTube.refresh.callback = () => new Promise((resolve, reject) => {
// Attempt to refresh the Token in here.
// If successful, make the following call.
resolve(data);
// Otherwise, make the following call.
reject();
});
The data
parameter passed to the resolve
method in the Promise
will, like with the Web App process, be ran through YouTube.setConfig
to set the new config values for the Interface. For example, with the YouTube Interface, you'd have to provide an object with an access_token
property so that it can overwrite the now expired Token.
TODO
- Finish initial Interfaces
- Add and maintain Documentation
- Add Transformers to unify return structures
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago