0.0.1 • Published 4 years ago
@car-co-uk/api-client v0.0.1
Car.co.uk API client library
Allows asynchronous requests to Car.co.uk REST API and get results with native Promise or async/await.
Initialize
import APIClient from '@car-co-uk/api-client';
const api = new APIClient({
apiUrl: 'https://website.com/api/v1',
apiToken: '<token>'
});
Usage
// fetch all auctions with await
const auctionsResponse = await api.auctions.list();
const auctions = auctionsResponse.json;
for(const car of auctions){
console.log(car.name)
}
// fetch all auctions with Promise
api.auctions.list().then(({status, json}) => {
const auctions = json;
for(const car of auctions){
console.log(car.name)
}
});
// create a car
api.auctions.create({ name: 'My Awesome Auction' }).then(({status, json}) => {
const carId = json.id;
});
Error Handling
// with await
try {
const createResult = await api.auctions.create({ name: 'My Awesome Auction' });
const newcar = createResult.json;
} catch(e) {
console.log(e);
}
// with Promise
api.auctions.create({ name: 'My Awesome Auction' })
.then(({status, json}) => {
if(status === 200) {
// success
const newcar = json;
} else {
// 404 or bad request
}
})
.catch(err => {
console.log(err)
});
Methods
api.authorize(baseUrl, user, pass)
0.0.1
4 years ago