This is a library for building HTTP API clients.
Initialize a new client.
Your client should extend the Papi Client
class and call super(options)
with the appropriate options.
Options
_log
callsAdvanced options
Usage
const papi = require('papi');
class GitHub extends papi.Client {
constructor(opts) {
opts = opts || {};
opts.baseUrl = 'https://api.github.com';
opts.header = { accept: 'application/vnd.github.v3+json' };
opts.timeout = 15 * 1000;
super(opts);
}
}
Make an HTTP request.
Your client should use this or the shortcut methods listed below to execute HTTP requests in your client methods.
Arguments
request.err
or request.res
. Call next
without arguments to continue execution, next(err)
to break with an error, or next(false, value)
to return immediately.Request
/user/{id}
)cancel
to abort request_log
callsThere are also _get
, _head
, _post
, _put
, _delete
, _patch
,
and _options
shortcuts with the same method signature as _request
.
Usage
class GitHub extends papi.Client {
constructor() {
// see example constructor above
}
async gists(username) {
const opts = {
path: '/users/{username}/gists',
params: { username: username },
};
const res = await this._get(opts);
return res.body;
}
}
Result
[ { url: 'https://api.github.com/gists/9458207',
...
Emit log events.
Arguments
Usage
client.on('log', function(tags) {
console.log({
tags: tags,
data: Array.prototype.slice.call(arguments, 1),
});
});;
client._log(['github', 'gist'], 'silas');
Result
{ data: [ 'silas' ], tags: [ 'debug', 'github', 'gist' ] }
Register an extension function.
Arguments
Usage
client._ext('onRequest', (request, next) => {
console.log('request', request.opts.method + ' ' + request.opts.path);
request.start = new Date();
next();
});
client._ext('onResponse', (request, next) => {
const duration = new Date() - request.start;
const statusCode = request.res ? request.res.statusCode : 'none';
console.log('response', request.opts.method, request.opts.path, statusCode, duration + 'ms');
next();
});
Result
request GET /users/{username}/gists
response GET /users/{username}/gists 200 1141ms
Register a plugin.
Arguments
Usage
client._plugin(require('papi-retry'));
Shortcuts for making one-off requests.
See client request for full options list, with the exception
that path
is replaced with url
.
Request
http://example.org/
)There are also get
, head
, post
, put
, delete
(del
), patch
, and
options
shortcuts with the same method signature as request
.
Usage
async function show() {
const res = await papi.get('https://api.github.com/users/silas/gists');
res.body.forEach(gist => console.log(gist.url));
}
const papi = require('papi');
/**
* GitHub API client
*/
class GitHub extends papi.Client {
constructor(opts) {
opts = opts || {};
if (!opts.baseUrl) {
opts.baseUrl = 'https://api.github.com';
}
if (!opts.headers) {
opts.headers = {};
}
if (!opts.headers.accept) {
opts.headers.accept = 'application/vnd.github.v3+json';
}
if (!opts.headers['user-agent']) {
opts.headers['user-agent'] = 'PapiGitHub/0.1.0';
}
if (opts.tags) {
opts.tags = ['github'].concat(opts.tags);
} else {
opts.tags = ['github'];
}
if (!opts.timeout) {
opts.timeout = 60 * 1000;
}
super(opts);
if (opts.debug) {
this.on('log', console.log);
}
}
/**
* Get user gists
*/
async gists(username) {
const opts = {
path: '/users/{username}/gists',
params: { username: username },
};
const res = await this._get(opts);
return res.body;
}
}
// Print gists for user `silas`
async function main() {
const github = new GitHub({ debug: true });
const gists = await github.gists('silas');
console.log('----');
gists.forEach(function(gist) {
if (gist.description) console.log(gist.description);
});
}
if (require.main === module) {
main();
} else {
module.exports = GitHub;
}
This work is licensed under the MIT License (see the LICENSE file).