20.1.2 • Published 4 months ago

@karhoo/demand-api v20.1.2

Weekly downloads
254
License
BSD-2-Clause
Repository
github
Last release
4 months ago

This library provides the ability to contact Karhoo's public API and allows you to send and receive network calls and responses. The Demand API is designed to enable it's consumers to integrate faster because they do not need to create their own complete network stack.

Read The Docs

License

Installation

npm i @karhoo/demand-api

Usage

You can use each service separately, or you can use getApi method which returns all available services

import {
  getApi,
  HttpService,
  LocationService,
  PoiService,
  QuotesV2Service,
  errorCodes,
} from '@karhoo/demand-api'

const url = 'https://public-api.karhoo.com' // please note that there should not be a slash at the end of the url

const correlationIdPrefix = 'prefix'

const requestOptionsGetter = () => ({
  headers: {
    'custom-header': 'Custom header',
  },
})

const middleware = <T>(response: HttpResponse<T>): HttpResponse<T> => {
  console.log(response.status)

  return response
}

Please note that by default fetch will be called with following config

{
  credentials: 'include',
  mode: 'cors',
}

You can override this default settings using defaultRequestOptionsGetter

getApi usage:

All config fields are optional, default value for url - https://public-api.karhoo.com in case if NODE_ENV === 'production', otherwise https://public-api.sandbox.karhoo.com.

Default value for correlationIdPrefix - ''.

Default value for authServiceDefaultOptionsGetter - () => ({}).

const options = {
  url,
  defaultRequestOptionsGetter: requestOptionsGetter,
  authServiceDefaultOptionsGetter,
  responseMiddleware: middleware,
  correlationIdPrefix,
}

const api = getApi(options)

Http service usage:

const apiV1 = 'https://public-api.karhoo.com/v1' // please note that version should be specified

const httpService = new HttpService(url)
  .setCorrelationIdPrefix(correlationIdPrefix)
  .setDefaultRequestOptionsGetter(requestOptionsGetter)
  .setResponseMiddleware(middleware)

const response = await httpService.post('locations/address-autocomplete', { query: 'lond' })

Location service usage:

const locationService = new LocationService(httpService)

Poi service:

const poiService = new PoiService(httpService)

Quotes service:

:warning: QuotesService is deprecated. Please use QuotesV2Service instead

const quotesService = new QuotesV2Service(httpService)

Trip service:

const tripService = new TripService(httpService)

Fare service:

const fareService = new FareService(httpService)

Payment service:

const paymentService = new PaymentService(httpService)

Flags service:

const flagsService = new FlagsService(httpService)

User service:

const userService = new UserService(httpService)

Auth service:

const authService = new AuthService(httpService)

Example

Let's see how to use one of the services, listed above.

For example, you need to get a list of quotes for trip from one location to another. For this purpose you have to use Quotes service and call quotesSearch method:

const quotesSearchParams = {
  origin: {
    latitude: '51.501364',
    longitude: '-0.14189',
    displayAddress: 'Buckingham Palace, London SW1A 1AA',
  },
  destination: {
    latitude: '41.78650',
    longitude: '1.78954',
    displayAddress: 'Big Ben, Westminster, London SW1A 0AA, UK',
  },
  local_time_of_pickup: '2020-05-12T10:00',
}

const quotesResponse = quotesService
  .quotesSearch(quotesSearchParams)
  .then(result => {
    //handleResult
  })
  .catch(error => {
    //handle error
  })

Issues

Looking to contribute?

🐛 Bugs

Please file an issue for bugs, missing documentation, or unexpected behavior with a label API

💡 Feature Requests

Please file an issue to suggest new features with a label API. Vote on feature requests by adding a 👍. This helps maintainers prioritize what to work on.

❓ Questions

For questions related to using the library, please re-visit a documentation first. If there are no answer, please create an issue with a label help needed and API.

Contributing

License

BSD-2-Clause

20.1.2

4 months ago

20.1.1

4 months ago

20.1.0

4 months ago

20.0.0

5 months ago

19.1.1

1 year ago

19.0.1

1 year ago

19.0.0

1 year ago

19.1.0

1 year ago

18.2.1

1 year ago

18.2.0

1 year ago

17.13.1

1 year ago

18.1.0

1 year ago

18.0.0

1 year ago

17.12.0

2 years ago

17.13.0

2 years ago

17.10.0

2 years ago

17.9.2

2 years ago

17.11.0

2 years ago

17.9.1

2 years ago

17.7.0

2 years ago

17.8.0

2 years ago

17.5.5

2 years ago

17.9.0

2 years ago

17.6.0

2 years ago

17.5.2

2 years ago

17.5.4

2 years ago

17.5.3

2 years ago

17.3.0

2 years ago

17.4.0

2 years ago

17.0.1

2 years ago

17.5.0

2 years ago

17.5.1

2 years ago

17.2.0

2 years ago

17.0.0

2 years ago

16.15.0

2 years ago

16.15.1

2 years ago

16.14.0

2 years ago

16.13.0

2 years ago

16.16.0

2 years ago

16.9.0

2 years ago

16.11.0

2 years ago

16.8.0

2 years ago

16.10.0

2 years ago

16.12.0

2 years ago

16.7.0

3 years ago

16.6.1

3 years ago

16.6.0

3 years ago

16.5.0

3 years ago

16.4.0

3 years ago

16.3.0

3 years ago

16.2.0

3 years ago

16.1.4

3 years ago

16.1.3

3 years ago

16.1.2

3 years ago

16.1.1

3 years ago

16.1.0

3 years ago

16.0.1

3 years ago

15.2.2

3 years ago

16.0.0

3 years ago

15.2.1

3 years ago

15.2.0

3 years ago

15.1.0

3 years ago

15.0.1

3 years ago

15.0.0

3 years ago

14.0.1

3 years ago

14.0.0

3 years ago

13.0.2

3 years ago

13.0.1

3 years ago

13.0.0

3 years ago

12.0.0

3 years ago

11.8.0

3 years ago

11.7.0

3 years ago

11.6.0

3 years ago

11.5.3

3 years ago

11.5.1

3 years ago

11.5.2

3 years ago

11.5.0

3 years ago

11.4.0

3 years ago

11.3.0

3 years ago

11.2.0

3 years ago

11.1.1

3 years ago

11.1.0

3 years ago

11.0.0

3 years ago

11.0.1

3 years ago

10.7.0

3 years ago

10.6.1

3 years ago

10.6.0

3 years ago

10.5.0

3 years ago

10.4.0

3 years ago

10.3.1

4 years ago

10.3.0

4 years ago

10.2.0

4 years ago

10.1.0

4 years ago

10.0.1

4 years ago

10.0.0

4 years ago

9.1.0

4 years ago

9.0.0

4 years ago

8.7.0

4 years ago

8.6.0

4 years ago

8.5.0

4 years ago

8.4.0

4 years ago

8.3.0

4 years ago

8.1.0

4 years ago

8.2.0

4 years ago

8.0.0

4 years ago

7.2.0

4 years ago

7.1.0

4 years ago

7.0.0

4 years ago

6.4.0

4 years ago

6.3.0

4 years ago

6.2.0

4 years ago

6.1.0

4 years ago

6.0.0

4 years ago

5.0.0

4 years ago

4.1.0

4 years ago

4.0.0

4 years ago

3.0.0

4 years ago

2.1.1

4 years ago

2.1.0

4 years ago

2.0.0

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.11.0

4 years ago

0.10.2

4 years ago

0.10.1

4 years ago

0.10.0

4 years ago

0.9.2

4 years ago

0.9.1

4 years ago

0.9.0

4 years ago

0.8.2

4 years ago

0.8.1

4 years ago