@car-co-uk/api-client

0.0.1 • Public • Published

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)

Readme

Keywords

none

Package Sidebar

Install

npm i @car-co-uk/api-client

Weekly Downloads

3

Version

0.0.1

License

none

Unpacked Size

34.3 kB

Total Files

38

Last publish

Collaborators

  • lgh06