Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/j3k0/ganomede-base-client
Base HTTP client that extends restify's JsonClient
https://github.com/j3k0/ganomede-base-client
Last synced: 24 days ago
JSON representation
Base HTTP client that extends restify's JsonClient
- Host: GitHub
- URL: https://github.com/j3k0/ganomede-base-client
- Owner: j3k0
- License: apache-2.0
- Created: 2017-04-11T07:19:18.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-04-11T09:36:00.000Z (over 7 years ago)
- Last Synced: 2024-11-17T08:48:01.678Z (about 1 month ago)
- Language: JavaScript
- Size: 31.3 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ganomede-base-client
Simple wrapper around [`restify.JsonClient`](http://restify.com/#jsonclient) to
start with when writing own API Clients. Changes:- single `request()`-like method (`#apiCall({method, path, body, headers, qs}, callback)`), meaning:
- base urls;
- per request headers;
- easier query string;
- supports path prefixes (useful if you want to have something like `/api/v1` in front of all requests);
- if `body` or `qs` contain `req_id` param, sets `x-request-id` header to value of it.## Basic Usage
Import `BaseClient` and define higher-level APIs.
``` js
const BaseClient = require('ganomede-base-client');class MyApi extends BaseClient {
constructor ({hostname = 'my-api.example.org', version = 1, apiKey}) {
super(
`https://${hostname}/version-${version}`, // base url
{headers: {'X-API-Key': apiKey}} // more options
);
}ping (callback) {
// Will issue HTTP GET to
// https://my-api.example.org/version-1/ping?check-auth=1
//
// Callback will contain JSON response (if any, otherwise empty object),
// see restify docs for more details at:
// https://github.com/restify/clients
//
// `call` is what restify methods return (like #get(), #post(), etc.).
const call = this.apiCall(
{method: 'get', path: '/ping', qs: {'check-auth': 1}},
callback
);
}
}
```## Custom defaults
Some defaults are changed from those of restify:
- up to 3 attempts on establishing TCP connections
with exponential timeout between 1 and 5 seconds;
- default headers:
- `accept` of `application/json`;
- `accept-encoding` of `gzip,deflate`.