# @distributedlab/json-api-client

> API Client for JSON API

Latest version **2.2.1** (published 2023-03-06) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install @distributedlab/json-api-client
pnpm add @distributedlab/json-api-client
yarn add @distributedlab/json-api-client
bun add @distributedlab/json-api-client
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 2.2.1 |
| Published | 2023-03-06 |
| First published | 2022-09-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 5 |
| Unpacked size | 731.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | artsuslov, napalmpapalam, volendi |

## Links

- npm: https://www.npmjs.com/package/@distributedlab/json-api-client
- npm.io page: https://npm.io/package/@distributedlab/json-api-client

## Dependencies (5)

- [axios](https://npm.io/package/axios.md) ^1.0.0
- [jsona](https://npm.io/package/jsona.md) ^1.9.3
- [lodash](https://npm.io/package/lodash.md) ^4.17.21
- [@babel/runtime](https://npm.io/package/@babel/runtime.md) ^7.19.0
- [http-status-codes](https://npm.io/package/http-status-codes.md) ^2.2.0

## Recent versions

- 2.2.1 (latest) — 2023-03-06
- 2.2.0 — 2022-12-15
- 2.1.1 — 2022-12-01
- 2.1.0 — 2022-12-01
- 2.0.4 — 2022-11-07
- 2.0.3 — 2022-10-20
- 2.0.2 — 2022-10-12
- 2.0.1 — 2022-10-06
- 2.0.0 — 2022-09-30
- 1.0.2 — 2022-09-27
- 1.0.1 — 2022-09-23
- 1.0.0 — 2022-09-23

## README

# @distributedlab/json-api-client
JSON API client

![version (scoped package)](https://badgen.net/npm/v/@distributedlab/json-api-client)
![types](https://badgen.net/npm/types/@distributedlab/json-api-client)
![tree-shaking](https://badgen.net/bundlephobia/tree-shaking/@distributedlab/json-api-client)
![checks](https://badgen.net/github/checks/distributed-lab/web-kit-old/main)

### This package moved to the [new repository](https://github.com/distributed-lab/web-kit) and not be maintained anymore.

## Usage
#### Bearer token
```typescript
// interceptors.ts
import { HTTPS_STATUS_CODES } from '@distributedlab/json-api-client'
import { AxiosInstance, AxiosRequestConfig } from 'axios'
import { useAuthStore } from '@/store'
import { router } from '@/router'
import { Bus } from '@/utils'
import { ROUTE_NAMES } from '@/enums'
import { useI18n } from '@/localization'

export function attachBearerInjector(axios: AxiosInstance): void {
  axios.interceptors.request.use((request): AxiosRequestConfig => {
    // Some authentication store in the client app
    const authStore = useAuthStore()
    if (!authStore.accessToken) return request

    if (!request.headers) request.headers = {}
    // Attach bearer token to every request
    request.headers['Authorization'] = `Bearer ${authStore.accessToken}`
    return request
  })
}

export function attachStaleTokenHandler(axios: AxiosInstance): void {
  axios.interceptors.response.use(
    response => response,
    async error => {
      const config = error?.config
      const isUnauthorized = (
        error?.response?.status === HTTPS_STATUS_CODES.UNAUTHORIZED &&
        !config?._retry
      )

      // If error isn't unauthorized or request was already retried - return error
      if (!isUnauthorized) return Promise.reject(error)

      // Some authentication store in the client app
      const authStore = useAuthStore()
      const { $t } = useI18n()

      try {
        config._retry = true
        // Executes some refresh token logic in the client app
        await authStore.refreshToken()

        // Reset default axios authorization header witn new token
        axios.defaults.headers.common['Authorization'] = `Bearer ${authStore.accessToken}`

        return axios(config)
      } catch (_error) {

        /** Example of handling refresh token error in the client app
         *
         * Implementation may differ from example
         *
         * We can logout user and redirect him to the login page and
         * emit bus error event to show user that session expired
        */
        authStore.logout()
        router.push({ name: ROUTE_NAMES.login })
        Bus.error({
          title: $t('api-errors.session-expired-title'),
          message: $t('api-errors.session-expired-desc'),
        })
        return Promise.reject(_error)
      }
    },
  )
}

// api.ts
import { JsonApiClient } from '@distributedlab/json-api-client';
import { attachBearerInjector, attachStaleTokenHandler } from '@/interceptors';

const axiosInstance = axios.create()
attachBearerInjector(axiosInstance)
attachStaleTokenHandler(axiosInstance)

export const api = new JsonApiClient({
  baseUrl: 'https://api.example.com',
  axios: axiosInstance,
});
```

## Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

<details><summary>2.2.1</summary>
  <h4>Change</h4>
    <ul>
      <li>Deprecate package</li>
    </ul>
</details>
<details><summary>2.2.0</summary>
  <h4>Added</h4>
  <ul>
    <li>Meta typing in response</li>
  </ul>
</details>
<details><summary>2.1.1</summary>
  <h4>Fixed</h4>
  <ul>
    <li>Exporting JsonApiBodyBuilder from package</li>
  </ul>
</details>
<details><summary>2.1.0</summary>
  <h4>Added</h4>
  <ul>
    <li>Util that helps to create the body for POST requests</li>
  </ul>
</details>
<details><summary>2.0.4</summary>
  <h4>Removed</h4>
  <ul>
    <li>Axios paramsSerializer encode config</li>
  </ul>
</details>
<details><summary>2.0.3</summary>
  <h4>Changed</h4>
  <ul>
    <li>Updated axios to <code>1.0.0</code></li>
  </ul>
</details>
<details><summary>2.0.2</summary>
  <h4>Added</h4>
  <ul>
    <li>Export helpers, enums and types</li>
  </ul>
</details>
<details><summary>2.0.1</summary>
  <h4>Fixed</h4>
  <ul>
    <li>Build content in NPM package</li>
  </ul>
</details>
<details><summary>2.0.0</summary>
  <h4>Added</h4>
  <ul>
    <li>
      Ability to provide axios instance to make possible to inject interceptors
      from client code to handle authorization and refresh token logic
    </li>
  </ul>
  <h4>Removed</h4>
  <ul>
    <li>Ability to provide auth token</li>
  </ul>
</details>
<details><summary>1.0.2</summary>
  <h4>Fixed</h4>
  <ul>
    <li>@babel/runtime dependency</li>
  </ul>
</details>
<details><summary>1.0.1</summary>
  <h4>Fixed</h4>
  <ul>
    <li>Readme</li>
  </ul>
</details>
<details><summary>1.0.0</summary>
  <h4>Under the hood changes</h4>
  <ul>
    <li>Initiated project</li>
  </ul>
</details>

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