# @knovator/api

> Simple API Wrapper for axios to achieve productive api calls

Latest version **1.0.4** (published 2022-12-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install @knovator/api
pnpm add @knovator/api
yarn add @knovator/api
bun add @knovator/api
```

## Health

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

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.4 |
| Published | 2022-12-13 |
| First published | 2021-12-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 217.5 KB |
| Known vulnerabilities | 0 (+23 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | knovator |
| Maintainers | sagar.modhvaniya, chavda-bhavik, jenishpaghadar, rajnishkatharotiya, vimlesai |
| Keywords | Api, api, wrapper, axios, fetch, knovator, vimlesai |

## Links

- npm: https://www.npmjs.com/package/@knovator/api
- Repository: https://github.com/knovator/api
- Homepage: https://github.com/knovator/api#readme
- Issues: https://github.com/knovator/api/issues
- npm.io page: https://npm.io/package/@knovator/api

## Dependencies (2)

- [axios](https://npm.io/package/axios.md) ^0.27
- [tslib](https://npm.io/package/tslib.md) ^2.3.0

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 1.0.4 (latest) — 2022-12-13
- 1.0.3 — 2022-12-13
- 1.0.2 — 2022-09-12
- 1.0.1 — 2022-09-12
- 1.0.0 — 2022-09-08
- 0.0.10 — 2022-06-27
- 0.0.9 — 2022-03-10
- 0.0.8 — 2022-03-09
- 0.0.7 — 2022-02-18
- 0.0.6 — 2022-02-02
- 0.0.5 — 2021-12-21
- 0.0.4 — 2021-12-15
- 0.0.3 — 2021-12-08
- 0.0.2 — 2021-12-08
- 0.0.1 — 2021-12-08

## README

# @knovator/api

![npm](https://img.shields.io/npm/v/@knovator/api?style=flat-square) ![npm](https://img.shields.io/npm/dt/@knovator/api?style=flat-square) ![npm bundle size](https://img.shields.io/bundlephobia/minzip/@knovator/api?style=flat-square)

`@knovator/api` provides simple wrapper for api calls and makes it much easier to mange your api calls. It provides two methods `fetchUrl` and `setAPIConfig`.

## setAPIConfig

You need to set wrapper's config one time before calling api. You can find docs for it.

| config        | type                   | use                                                          |
| ------------- | ---------------------- | ------------------------------------------------------------ |
| `getToken`    | `function` or `string` | for private APIs getToken method to get an updated token from localStorage or state or it's up to you and you also pass a string to it |
| `baseUrl`     | `string`               | API endpoint url `(Note:do not pass '/' at the end of url)`  |
| `prefix`      | `function` or `string` | You can use a common prefix between API Endpoint and the URL of API. a prefix is an optional parameter. A prefix can be the function as well as string type. If you are using prefix as a function then you will get your passed config in arguments. |
| `onError`     | `function`             | It is optional but if you pass function then it will be called on any error in API |
| `handleCache` | `boolean`              | To enable API cancellation set it true. When enabled,  If the last API URL and the current API URL are the same then the last API will get canceled to prevent unnecessary API calls. |
| `tokenPrefix` | `false` or `string` | by default it will use `Bearer` for prefix, to not pass any prefix with authorization pass `false` |



```jsx
import { setAPIConfig } from "@knovator/api"

const getToken = () => {
    // do something and return token for authorization
    return token
}

setAPIConfig({
    getToken: getToken(),
    prefix: 'admin',
    baseUrl: `${process.env.FETCH_URL}`,
})
...
```



## fetchUrl

#### fetchUrl takes single parameter under that

| Param  | Use                                                          | Default Value |
| :----: | :----------------------------------------------------------- | :-----------: |
|  type  | type of api call like `get`, `post` etc.                     |     `get`     |
|  url   | is is url of your api call (not including endpoint of api). refer Example to understand it |   undefined   |
|  data  | payload that you want to pass in api                         |      {}       |
| config | some configurations like headers, authToken (default true) and hash |      {}       |


```jsx
import fetchUrl from "@knovator/api"

function App() {
    ...
    useEffect(() => {
        // for get method
        fetchUrl({
            url: 'users'
        }).then(res => console.log('success', res))
        // for post or other method
         fetchUrl({
            method: 'post',
            url: 'user'
            data: { name: "VimLeSai" }
        }).then(res => console.log('success', res))
    }, [])
    ...
}
```

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