Client/server agnostic Http Request library utilising the fetch
API.
You can install this package using npm:
$ npm install instant-request
Here is a quick example to get you started:
ES Modules
import Request from 'instant-request';
const request = new Request('https://example.com');
const test = async () => {
const response = await request.get('/api/records'); // => https://example.com/api/records
const json = await response.json();
console.log(json);
};
CommonJS Modules
var Request = require('instant-request').Request;
var request = new Request('https://example.com');
request
.get('/api/records') // => https://example.com/api/records
.then(function(response) {
response.json().then(function(json) {
console.log(json);
});
});
Create an instance of the Request class.
baseUrl (String)
: Base URL for all future requests.
options (Object) = {}
: Configuration object
instanceof Request
const request = new Request('https://example.com');
Make an HTTP GET request to the specified URI.
uri (String)
: The URI to request, e.g /api/records
.
query (Object)
: The query object to append to the fully qualified URL, e.g { active: 1 }
=> https://example.com/api/records?active=1
.
Response
async function getCountries() {
// https://example.com/api/countries
const response = await request.get('/api/countries');
const json = await response.json();
return json;
}
Make an HTTP POST request to the specified URI.
uri (String)
: The URI to request, e.g /api/records
.
data (Object)
: The data to POST.
query (Object)
: The query object to append to the fully qualified URL, e.g { active: 1 }
=> https://example.com/api/records?active=1
.
Response
async function createCountries() {
// https://example.com/api/countries
const response = await request.post('/api/countries', {
id: 1,
name: 'Australia',
});
const json = await response.json();
return json;
}
Make an HTTP PUT request to the specified URI.
uri (String)
: The URI to request, e.g /api/records
.
data (Object)
: The data to PUT.
query (Object)
: The query object to append to the fully qualified URL, e.g { active: 1 }
=> https://example.com/api/records?active=1
.
Response
async function updateCountry() {
// https://example.com/api/countries
const response = await request.put('/api/countries/1', {
animal: 'Kangaroo',
});
const json = await response.json();
return json;
}
Make an HTTP DELETE request to the specified URI.
uri (String)
: The URI to request, e.g /api/records
.
query (Object)
: The query object to append to the fully qualified URL, e.g { active: 1 }
=> https://example.com/api/records?active=1
.
Response
async function deleteCountry() {
// https://example.com/api/countries
const response = await request.delete('/api/countries/1');
const json = await response.json();
return json;
}
We'd greatly appreciate any contribution you make.