Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
config-req
Advanced tools
Axios wrapper based on a config file
This module allows to set up a programmatic HTTP client based on axios. To set it up, it just the url and the HTTP method per environment to be setup ¡et voilà!, a new and shiny HTTP client is ready to be used.
Axios options can be found here;
npm install config-req
const request = require('config-req');
// Your env configuration
const config = {
activateAccount: {
url: 'http://localhost:5000/v1/account/activate',
method: 'post',
},
};
const api = request(config);
console.log(api); // returns an object like this { activateAccount: <Promise> }
// Api instance contains a request with the method and URL already configured
api.activateAccount()
.then(response => {
console.log(response); // Axios response
});
const request = require('config-req');
// Your env configuration with nested objects
const nestedOptions = {
registration: {
activateAccount: {
url: 'http://localhost:5000/v1/account/activate',
method: 'post',
},
},
};
const api = request(nestedOptions);
console.log(api); // returns an object like this { registration: { activateAccount: <Promise> } }
// Api instance contains a request with the method and URL already configured
api.registration.activateAccount()
.then(response => {
console.log(response); // Axios response
});
const request = require('config-req');
const options = {
advanced: {
withBodyInfo: {
url: 'http://localhost:5000/v1/account/activate',
method: 'post',
},
withURLParams: {
url: 'http://localhost:5000/v1/account/:id/activate',
method: 'get',
},
withBasicAuth: { // This will affect each call to this endpoint
url: 'http://localhost:5000/v1/account/:id/activate',
method: 'get',
auth: { password: 'pwd', username: 'nickname' },
}
},
};
const api = request(options);
api.advanced.withBodyInfo({
body: { example: 'example' }, // this is how to send body params
query: { example: 'example' }, // this is how to send query params
headers: { Authorization: 'Bearer example' }, // this is how to send header params
})
.then(response => {
console.log(response); // Axios response
});
// This is when we want to handle dynamic url params like this
// http://localhost:5000/v1/account/:id/activate
// To change that ID we need to setup the urlParams
api.advanced.withURLParams({
body: { example: 'example' }, // this is how to send body params
query: { example: 'example' }, // this is how to send query params
headers: { Authorization: 'Bearer example' }, // this is how to send header params
params: { id: 'urlParam' },
})
.then(response => {
console.log(response); // Axios response
});
// Basic auth
api.advanced.withURLParams({
body: { example: 'example' }, // this is how to send body params
query: { example: 'example' }, // this is how to send query params
headers: { Authorization: 'Bearer example' }, // this is how to send header params
params: { id: 'urlParam' }, // This is how to send path params
auth: { password: 'pwd', username: 'nickname' }, // this is how you add basic auth for each request
})
.then(response => {
console.log(response); // Axios response
});
In some scenarios, it might be needed to have a fine-grained control on request or the responses. For example, to refresh a token when it is expired or to handle errors in a specific way. This can be achieved by using the interceptors option provided by Axios. These interceptors can be set-up in the following way:
const request = require('config-req');
// Your env configuration
const config = {
activateAccount: {
url: 'http://localhost:5000/v1/account/activate',
method: 'post',
},
};
const api = request(config, {
interceptors: {
request: (config) => {
// Do something before request is sent
return config;
},
response: {
success: (response) => {
// Do something with response data
return response;
},
error: (error) => {
// Do something with response error
return Promise.reject(error);
}
}
}
});
To contribute you must send a PR. This is how you can run the project as a developer.
Install dependencies
npm install
Execute tests
npm run test
FAQs
Axios wrapper based on a config file
We found that config-req demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers 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
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.