# @tpr/ajax

> TPR Ajax components

Latest version **1.0.7** (published 2020-02-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install @tpr/ajax
pnpm add @tpr/ajax
yarn add @tpr/ajax
bun add @tpr/ajax
```

## 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 | 1.0.7 |
| Published | 2020-02-28 |
| First published | 2020-02-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 8 |
| Unpacked size | 36.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | David Alekna |
| Maintainers | alekna, jamesdarrall, rickmason, tobiasbunyan_tpr |
| Keywords | tpr |

## Links

- npm: https://www.npmjs.com/package/@tpr/ajax
- Repository: https://dev.azure.com/thepensionsregulator/TPR/_git/react-components
- Homepage: https://dev.azure.com/thepensionsregulator/TPR/_git/react-components#readme
- Issues: https://dev.azure.com/thepensionsregulator/TPR/_git/react-components/issues
- npm.io page: https://npm.io/package/@tpr/ajax

## Dependencies (8)

- [qs](https://npm.io/package/qs.md) ^6.9.1
- [rxjs](https://npm.io/package/rxjs.md) ^6.5.4
- [immer](https://npm.io/package/immer.md) ^5.3.6
- [ramda](https://npm.io/package/ramda.md) ^0.27.0
- [tslib](https://npm.io/package/tslib.md) ^1.11.1
- [lodash](https://npm.io/package/lodash.md) ^4.17.15
- [@tpr/core](https://npm.io/package/@tpr/core.md) ^1.0.12
- [@alekna/react-store](https://npm.io/package/@alekna/react-store.md) ^1.2.12-alpha.0

## Recent versions

- 1.0.7 (latest) — 2020-02-28
- 1.0.6 — 2020-02-26
- 1.0.5 — 2020-02-21
- 1.0.4 — 2020-02-18
- 1.0.3 — 2020-02-18
- 1.0.1 — 2020-02-18

## README

<h1 align="center">
  Ajax Network Layer
</h1>
<p align="center" style="font-size: 1.2rem;">Helps to keep track of data statuses and efficiently reuse data that has already been fetched.</p>
<hr />

## Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.

## Features

- Transport and protocol agnostic data fetching
- Request deduplication
- Local mutation
- Pagination
- TypeScript ready
- Network status
- Refetch queries upon successful mutation

## Table of Contents

<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->

- [Installation](#installation)
- [`AjaxProvider` Props](#ajaxprovider-props)
  - [children](#children)
  - [api](#api)
  - [stores](#stores)
  - [initialState](#initialstate)
  - [persistOn](#persiston)
- [AjaxQuery Props](#ajaxquery-props)
  - [endpoint](#endpoint)
  - [type](#type)
  - [headers](#headers)
  - [variables](#variables)
  - [store](#store)
  - [dataPath](#datapath)
  - [errorPath](#errorpath)
  - [mergeData](#mergedata)
- [`AjaxQuery` Render Props](#ajaxquery-render-props)
- [`AjaxMutation` Props](#ajaxmutation-props)
- [`AjaxMutation` Render Props](#ajaxmutation-render-props)
- [`useUpdate` Hook](#useupdate-hook)
- [Examples](#examples)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

## Installation

This module is distributed via [npm](https://www.npmjs.com) which is bundled with [node](https://nodejs.org) and
should be installed as one of your project's `dependencies`:

```
npm i @tpr/ajax
```

> This package also depends on react and react-dom. Please make sure you have those installed as well.

## Testing

Run following command from the root of the project and follow the instructions.

```
yarn test:watch
```

## `AjaxProvider` Props

### children

> `JSX.Element` _required_

### api

> `Endpoint[]` _required_

Accepts objects of type `Endpoint`, at least one is required. First `Endpoint` will be the default.

```
Endpoint = {
	name: string;
	instance: Observable
}
```

### stores

> `Store[]` _required_

Accepts a configuration of stores where the data will be persisted.

```
Store = {
	name: string;
	persist?: boolean
}
```

### initialState

> `{}` _optional_

Use initialState to rehydrate the store from the `localStorage`

### persistOn

> `string`: _optional_

If store is persisted, it will be persisted on this key in the `localStorage`

## AjaxQuery, useQuery Props

### endpoint

> `string` _required_

### type

> `'get' | 'post'` _optional_

### headers

> `object` _optional_

### variables

> `object` _optional_

### store

> `string` _required_

Store name from the global store object that was defined in the Provider.

### dataPath

> 'string[]' _optional_

Define path to the data that comes back from the network request to be extracted.

### errorPath

> 'string[]' _optional_

Define path to the error that comes back from the server from network request.

### mergeData

> `(first: any, second: any) => [...first, ...second]` _optional_

Provide a method to merge data for `fetchMore` function

## `AjaxQuery, useQuery` Render Props

TODO: ...

## `AjaxMutation, useMutation` Props

TODO: ...

## `AjaxMutation, useMutation` Render Props

TODO: ...

## `useUpdate` Props

This hook provides a way to interact with one of available stores. Can be used for optimistic updates or update the state whenever it is appropriate todo so.

> `useUpdate(args: UpdateProps): (search?: string | null, options: FindAndModifyProps) => void`

## Examples

> NOTE: all examples will be hosted on the codesandbox

- useQuery
- useMutation
- useUpdate

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