# @mj-studio/react-native-rest-util

> Custom React hook and Rest adapter for network call

Latest version **3.0.2** (published 2024-02-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install @mj-studio/react-native-rest-util
pnpm add @mj-studio/react-native-rest-util
yarn add @mj-studio/react-native-rest-util
bun add @mj-studio/react-native-rest-util
```

## Health

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

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

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.2 |
| Published | 2024-02-13 |
| First published | 2020-08-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 34.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | MJ Studio |
| Maintainers | mj_studio |
| Keywords | react-native |

## Links

- npm: https://www.npmjs.com/package/@mj-studio/react-native-rest-util
- Repository: https://github.com/mym0404/react-native-rest-util
- npm.io page: https://npm.io/package/@mj-studio/react-native-rest-util

## Dependencies (1)

- [@mj-studio/js-util](https://npm.io/package/@mj-studio/js-util.md) 1.0.5

## Recent versions

- 3.0.2 (latest) — 2024-02-13
- 3.0.1 — 2024-02-13
- 3.0.0 — 2024-02-13
- 2.5.0 — 2024-02-08
- 2.4.11 — 2023-09-16
- 2.4.10 — 2023-09-11
- 2.4.9 — 2023-09-11
- 2.4.8 — 2023-07-24
- 2.4.7 — 2023-07-24
- 2.4.6 — 2023-02-24
- 2.4.5 — 2023-02-12
- 2.4.4 — 2022-11-30
- 2.4.3 — 2022-11-30
- 2.4.2 — 2022-11-30
- 2.4.1 — 2022-08-19
- … 21 more at https://npm.io/package/@mj-studio/react-native-rest-util/versions

## README

## React Native REST utilities

![JS Check](https://github.com/mym0404/react-native-rest-util/workflows/JS%20Check/badge.svg)

---
### Install

```
yarn add @mj-studio/react-native-rest-util
npm install @mj-studio/react-native-rest-util
```

---
### Usage

* `GET`, `POST`, `PUT`, `DELETE`, `PATCH` : return a tuple that first item is function to create network call promise and second is function to abort network call
* `useRest` : React custom hook for network call
* `useCall` : lazy version of useRest

### 1. Set default settings for network call process.(Optional)

Put following code in the application entry like `index.js`.
All fields are optional.
The default values are following.

```ts
setApiDefaultSettings({
  headers: {
    'Content-Type': 'application/json',
    Accept: 'application/json',
  },
  baseUrl: '',
  timeout: 5000,
  requestInterceptor: (request) => request,
  responseInterceptor: (response) => response,
  responseInterceptorAddons: [],
  responseCodeWhiteListRange: { minInclude: 200, maxExclude: 300 },
  responseCodeWhiteList: [], // number[]
  responseCodeBlackList: [], // number[]
  logging: false,
});
```

You can reset to default values.

```ts
clearApiDefaultSettings();
```

In `responseInterceptor`, the parameter is body(data) object(or array)in response.

`requestInterceptor` and `responseInterceptor` also receive `Promise` for async tasks.
**You must return processed `request` and `response` in the interceptors!**

```ts
setApiDefaultSettings({
  requestInterceptor: async (request) => {
    request.headers.Authorization = AsyncStorage.getItem('accessToken') || '';
    return request;
  },
  responseInterceptor: async (response) => {
    await logToServer(response)
    return response;
  },
});
```

There are response interceptor addons(currently, only `CAMELCASE`).
You can set this addons to `setApiDefaultSettings`

```ts
setApiDefaultSettings({
  // response data from server like { my_name: 'mj' } is converted with { myName: 'mj' }
  responseInterceptorAddons: [ResponseInterceptorAddOn.CAMELCASE], 
});
```

### 2. Declare your REST api routers

- Use `GET`, `POST`, `PUT`, `PATCH`, `DELETE` in the library.
**this is even not async function or return promise!** 😀

- Return with `ApiResult<your data type>`

```ts
type FetchVersion = {
  androidMinimumVersion: number;
  iosMinimumVersion: number;
};
export const fetchVersion = (): ApiResult<FetchVersion> => {
  return GET('version/', {
    headers: { token: myToken },
    body: { bodyData: 1 },
    files: [{ key: 'movie', file: { name: 'movie', type: 'video/*', uri: 'video/path' } }],
    queryParams: { name: 'queryParamsName' },
  });
};
```

---
_Source: https://npm.io/package/@mj-studio/react-native-rest-util · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
