# @karhoo/demand-api

> The Demand API provides the ability to contact Karhoo's public API and allows you to send and receive network calls and responses.

Latest version **20.1.2** (published 2024-01-17) · BSD-2-Clause license · 0 weekly downloads

## Install

```sh
npm install @karhoo/demand-api
pnpm add @karhoo/demand-api
yarn add @karhoo/demand-api
bun add @karhoo/demand-api
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 20.1.2 |
| Published | 2024-01-17 |
| First published | 2020-04-22 |
| Weekly downloads | 0 |
| License | BSD-2-Clause |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 4 |
| Unpacked size | 479.8 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 20 |
| Maintainers | karhoo_js, karhoo_ops_tooling, karhoo_connect, karhoo_demand_web, karhoo_partner_central |

## Links

- npm: https://www.npmjs.com/package/@karhoo/demand-api
- Repository: https://github.com/karhoo/web-lib-demand
- Homepage: https://github.com/karhoo/web-lib-demand/tree/master/packages/demand-api#readme
- Issues: https://github.com/karhoo/web-lib-demand/issues
- npm.io page: https://npm.io/package/@karhoo/demand-api

## Dependencies (4)

- [uuid](https://npm.io/package/uuid.md) ^8.3.2
- [lodash](https://npm.io/package/lodash.md) ^4.17.21
- [core-js-pure](https://npm.io/package/core-js-pure.md) ^3.20.1
- [date-and-time](https://npm.io/package/date-and-time.md) ^2.0.1

## Recent versions

- 20.1.2 (latest) — 2024-01-17
- 20.1.1 — 2024-01-17
- 20.1.0 — 2024-01-03
- 20.0.0 — 2023-12-22
- 19.1.1 — 2023-05-12
- 19.1.0 — 2023-04-06
- 19.0.1 — 2023-03-31
- 19.0.0 — 2023-03-13
- 18.2.1 — 2022-12-13
- 18.2.0 — 2022-12-09
- 18.1.0 — 2022-12-08
- 18.0.0 — 2022-11-22
- 17.13.1 — 2022-11-18
- 17.13.0 — 2022-10-19
- 17.12.0 — 2022-10-18
- … 118 more at https://npm.io/package/@karhoo/demand-api/versions

## README

<div align="center">
<a href="https://karhoo.com">
  <img
    alt="Karhoo logo"
    width="250px"
    src="https://cdn.karhoo.com/s/images/logos/karhoo_logo.png"
  />
</a>

<h1>Karhoo Demand API</h1>

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.
<br />

[**Read The Docs**](https://developer.karhoo.com/docs/using-web-demand-api-package)

<hr />

[![License](https://img.shields.io/badge/License-BSD%202--Clause-orange.svg)](https://opensource.org/licenses/BSD-2-Clause)

</div>

## Installation

```sh
npm i @karhoo/demand-api
```

## Usage

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

```js
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

```js
{
  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` - `() => ({})`.

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

const api = getApi(options)
```

Http service usage:

```js
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:

```js
const locationService = new LocationService(httpService)
```

# Poi service:

```js
const poiService = new PoiService(httpService)
```

# Quotes service:

:warning: **QuotesService is deprecated. Please use QuotesV2Service instead**
```js
const quotesService = new QuotesV2Service(httpService)
```

# Trip service:

```js
const tripService = new TripService(httpService)
```

# Fare service:

```js
const fareService = new FareService(httpService)
```

# Payment service:

```js
const paymentService = new PaymentService(httpService)
```

# Flags service:

```js
const flagsService = new FlagsService(httpService)
```

# User service:

```js
const userService = new UserService(httpService)
```

# Auth service:

```js
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:

```js
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](../LICENSE)

---
_Source: https://npm.io/package/@karhoo/demand-api · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
