@kalkuz/node-fetch-http v0.1.4
Node Fetch HTTP
A simple HTTP client for Node.js using node-fetch.
Installation
You can install this package using npm:
npm install @kalkuz/node-fetch-httpUsing yarn:
yarn add @kalkuz/node-fetch-httpDependencies
Node Fetch HTTP only depends on node-fetch.
Usage
Importing The Package
For configuring the package, you can use the default export:
import FetchHttp from '@kalkuz/node-fetch-http';
FetchHttp.Configurate(...);Or you can use the named export:
import { Configurate } from '@kalkuz/node-fetch-http';
Configurate(...);For supported types of HTTP requests, you can use the named export:
import { Get, Post, Put, Patch, Delete } from '@kalkuz/node-fetch-http';Configuring The Package
Configuration is optional. If you don't configure the package, it will use the default configuration.
The default configuration is:
{
baseUrl: '',
getAuthorization: () => '',
}Configuring The Base URL
The baseUrl parameter will be used as the base URL for all requests. Example:
// Configured URL: https://example.com/api/v1
Get('/users');
// Final URL: https://example.com/api/v1/users If you don't want to use a base URL, you can pass an empty string as the baseUrl parameter or skip the configuration. When you skip configuration of baseUrl, you can pass the full URL as the first parameter of the HTTP request function.
Get('https://example.com/api/v1/users');
// Final URL: https://example.com/api/v1/users If you configured the baseUrl parameter and you want to use another full URL endpoint, you can pass the full URL as the optionalApiAddress parameter of the HTTP request function. Example:
// Configured URL: https://example.com/api/v1
Configurate({ baseUrl: 'https://example.com/api/v1' });
// Get(endpoint, headers, optionalApiAddress)
Get('/users', null, 'https://example.com/api/v2');
// Post(endpoint, body, headers, optionalApiAddress)
Post('/users', { name: 'John Doe' }, null, 'https://example.com/api/v2')
// Final URL: https://example.com/api/v2/usersConfiguring The Authorization Header
The getAuthorization function will be used to get the authorization header for all requests.
The getAuthorization function will be called before every request and it should return the authorization header value. Example:
Configurate({
getAuthorization: () => {
const token = localStorage.getItem('token');
// as bearer token
return `Bearer ${token}`;
// or as basic auth
return `${token}`;
},
});
// If you send a request:
Get('/users');
// Headers will be something like:
{
...
'Content-Type': 'application/json',
authorization: 'Bearer %YOUR_TOKEN%',
// or
authorization: '%YOUR_TOKEN%',
}Promise Based
All HTTP request functions return a promise. Example:
Get('/users')
.then((response) => {
// response is a json
// response.json() is applied before
})
.catch((error) => {
// handle the error
});