
Security News
vlt Launches "reproduce": A New Tool Challenging the Limits of Package Provenance
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
rest-api-handler
Advanced tools
This library will help you with requests to REST APIs. It uses Fetch API which isn't supported by node and some older browsers. Remember to include polyfill if need it.
Install npm library:
npm install api-handler --save
First initiate Api class and then you can send HTTP requests. As first parameter use base url.
import { Api } from 'api-handler';
const api = new Api('https://api.blockcypher.com');
api.get('v1/btc/main').then((response) => {
console.log(response); // same response as in Fetch API
});
In default configuration, response is same as in Fetch API. That why you can define your own processors that will parse responses or use default one provided by this library.
import { Api, defaultResponseProcessor } from 'api-handler';
const api = new Api('https://api.blockcypher.com', [
defaultResponseProcessor,
]);
api.get('v1/btc/main').then((response) => {
console.log(response.code); // 200
console.log(response.data); // parsed JSON
console.log(response.source); // original Response
});
Here is how to create your own response processors and use them in the chain:
const api = new Api('//some.api.com', [
defaultResponseProcessor,
onlyDataProcessor,
]);
function onlyDataProcessor(response) {
return Promise.resolve(response.data);
}
api.get('endpoint').then((response) => {
console.log(response); // parsed JSON
});
There are default methods for GET, POST, PUT and DELETE. But you can send any HTTP method using request.
api.get('v1/btc/main'); // GET https://api.blockcypher.com/v1/btc/main
api.get('v1/btc/main', { a: 'b' }); // GET https://api.blockcypher.com/v1/btc/main?a=b
api.post('method', { a: 'b' });
api.put('method', { a: 'b' });
api.delete('method');
// you can create your own requests
// use can use other parameters from Fetch API - https://developer.mozilla.org/en-US/docs/Web/API/Request
api.request('endpoint', 'PUT', {
body: 'Simple string request',
});
By default, data for POST and PUT are encoded as JSON. You can also encode them as FormData. This can be used for images or files uploading.
import { Api, defaultResponseProcessor, FORM_DATA_FORMAT } from 'api-handler';
const api = new Api('//some.api.com');
api.post('file-upload', {
file: fileObject,
}, FORM_DATA_FORMAT);
You can authorize to API by using default headers or set them after.
const api = new Api('//some.api.com', [], {
Authorization: 'Bearer XYZ',
'Content-Type': 'application/json',
});
// this will replace original default value
api.addDefaultHeader('Authorization', 'Bearer ABC');
// this will delete authorization
api.removeDefaultHeader('Authorization');
You can also set custom headers for every request:
api.request('endpoint', 'GET', {}, {
'Authorization': 'Bearer XYZ',
})
To use it as node library, just import Fetch polyfill:
require('isomorphic-fetch');
const { Api, defaultResponseProcessor } = require('api-handler');
const api = new Api('https://api.blockcypher.com', [ defaultResponseProcessor ]);
api.get('v1/btc/main').then((response) => {
console.log(response.data);
});
[1.0.0] 2018-01-26
FAQs
Handler for REST APIs
The npm package rest-api-handler receives a total of 39 weekly downloads. As such, rest-api-handler popularity was classified as not popular.
We found that rest-api-handler demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
Research
Security News
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
Research
The Socket Research Team discovered a malicious npm package, '@ton-wallet/create', stealing cryptocurrency wallet keys from developers and users in the TON ecosystem.