nbrc-http
v0.0.22
Published
```javascript let configuration = { engine: 'axios', // multiple groups Request-Config config: { // function | object "default": function () { return { baseURL: 'https://api.example.com/',
Downloads
4
Readme
Configuration
let configuration = {
engine: 'axios',
// multiple groups Request-Config
config: {
// function | object
"default": function () {
return {
baseURL: 'https://api.example.com/',
headers: {
"x-custom-header": "some string..."
}
}
},
},
// multiple groups of Interceptors
interceptor: {
// group 1
"default": {
// request interceptor
request: function (config) {
return getUser().then(user => {
if (user.id) {
config.headers.Authorization = `Bearer ${user.token}`;
}
return config;
});
},
request_error: function (error) {
return Promise.reject(error);
},
// response interceptor
response: function (response) {
return jwe.decode(response.data, jwk.private)
},
response_error: function (error) {
return Promise.reject(error);
}
},
// group 2
"jwe": {
request: function (config) {
return jwe.encode(config.data, jwk1.public)
},
},
// group 3
"jwe_admin": {
request: function (config) {
return jwe.encode(config, jwk2.public)
},
},
}
};
Create Instance
import httpLib from 'nbrc-http';
let http = httpLib.createInstance(configuration);
// use "default" Request-Config && Interceptors
http.get('./path/a/?name=hello').then(resp=>{...})
// use "default" Request-Config and combination of "default" && "jwe" Interceptors
http.post('./path/b/', {password:"secretString"}, "default", ["default","jwe"]).then(resp=>{...})
Methods
// get delete head options
http.get(url[, config[, interceptors[, engine]]])
// post put patch postForm putForm patchForm
http.post(url, data[, config[, interceptors[, engine]]])
// request
http.request([config[, interceptors[, engine]]])
/**
* config ( string | object | function | array )
* string: config group key in configuration
* object: config value
* function: return config value
* array: combination of any "string", "object" or "function", merge in order
*
* interceptors ( string | array )
* string: interceptor group key in configuration. example: "jwe"
* array: merge multiple Interceptor Groups. example: [ "default", "jwe" ]
*
* engine ( string )
* string: engine name, Priority is higher than the engine in configuration
* axios: https://axios-http.com/
* fetch: Fetch API (https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API)
* background: in browser extension, Content_Script ask Background_Script to make the http request
*/