A set of utilities I find myself using across projects
ApiRequest is a wrapper/helper class for generating axios requests. It supports adding endpoints, replace endpoint keys as well as adding query params.
new ApiRequest({
endpoint,
method,
applyAuthToken,
urlParams,
endpointParams,
data
});
This would perform a DELETE
request to http://localhost:8080/api/v1/users/1
const req = new ApiRequest(
'http://localhost:8080/api/v1/users/:id',
'DELETE',
true,
[{ key: ':id', value: 1 }]
);
const res = req.performRequest();
This would perform a GET
request to http://localhost:8080/api/v1/users?name=adam
const req = new ApiRequest(
'http://localhost:8080/api/v1/users',
'GET',
true,
undefined,
[{ key: 'name', value: 'adam' }]
);
const res = req.performRequest();
This would perform a POST
request to http://localhost:8080/api/v1/users
with the body: { username: 'adam', password: 'password' }
const req = new ApiRequest(
'http://localhost:8080/api/v1/users',
'POST',
true,
undefined,
undefined,
{
username: 'adam',
password: 'password'
}
);
const res = req.performRequest();