instant-request
v1.0.1
Published
Client/server agnostic http request library.
Downloads
5
Readme
Instant Utils
Client/server agnostic http request library.
Table of Contents
Installation
You can install this package using npm:
$ npm install instant-request
Usage
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);
});
});
API
Instantiation
Create an instance of the Request class.
Arguments
baseUrl (String)
: Base URL for all future requests.
options (Object) = {}
: Configuration object
Returns
instanceof Request
Example Usage
const request = new Request('https://example.com');
get
Make an HTTP GET request to the specified URI.
Arguments
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
.
Returns
Response
Example Usage
async function getCountries() {
// https://example.com/api/countries
const response = await request.get('/api/countries');
const json = await response.json();
return json;
}
post
Make an HTTP POST request to the specified URI.
Arguments
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
.
Returns
Response
Example Usage
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;
}
put
Make an HTTP PUT request to the specified URI.
Arguments
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
.
Returns
Response
Example Usage
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;
}
delete
Make an HTTP DELETE request to the specified URI.
Arguments
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
.
Returns
Response
Example Usage
async function deleteCountry() {
// https://example.com/api/countries
const response = await request.delete('/api/countries/1');
const json = await response.json();
return json;
}
Contributing
We'd greatly appreciate any contribution you make.