# relient

> Relient is an util set working closely with Relient-Cli. It will be useful if your project is built on React, Redux and of course Relient-Cli

Latest version **5.2.0** (published 2021-07-30) · MIT license · 0 weekly downloads

## Install

```sh
npm install relient
pnpm add relient
yarn add relient
bun add relient
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 5.2.0 |
| Published | 2021-07-30 |
| First published | 2017-11-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=12.0 |
| Dependencies | 19 |
| Unpacked size | 68.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Liu Yang |
| Maintainers | zation |
| Keywords | react, redux, i18n |

## Links

- npm: https://www.npmjs.com/package/relient
- Repository: https://github.com/zation/relient
- Homepage: https://github.com/zation/relient#readme
- Issues: https://github.com/zation/relient/issues
- npm.io page: https://npm.io/package/relient

## Dependencies (19)

- [react](https://npm.io/package/react.md) >=17.0.2
- [redux](https://npm.io/package/redux.md) >=4.0.5
- [config](https://npm.io/package/config.md) >=3.3.0
- [lodash](https://npm.io/package/lodash.md) >=4.17.19
- [date-fns](https://npm.io/package/date-fns.md) >=2.21.1
- [normalizr](https://npm.io/package/normalizr.md) >=3.6.0
- [@types/node](https://npm.io/package/@types/node.md) ^14.14.41
- [react-redux](https://npm.io/package/react-redux.md) >=7.2.2
- [@types/react](https://npm.io/package/@types/react.md) ^17.0.3
- [bignumber.js](https://npm.io/package/bignumber.js.md) ^9.0.0
- [query-string](https://npm.io/package/query-string.md) >=7.0.0
- [@types/config](https://npm.io/package/@types/config.md) ^0.0.38
- [@types/lodash](https://npm.io/package/@types/lodash.md) ^4.14.159
- [redux-actions](https://npm.io/package/redux-actions.md) >=2.6.5
- [universal-router](https://npm.io/package/universal-router.md) >=9.0.1
- [@types/redux-actions](https://npm.io/package/@types/redux-actions.md) ^2.6.1
- [serialize-javascript](https://npm.io/package/serialize-javascript.md) >=5.0.1
- [@types/serialize-javascript](https://npm.io/package/@types/serialize-javascript.md) ^5.0.0
- [@formatjs/icu-messageformat-parser](https://npm.io/package/@formatjs/icu-messageformat-parser.md) >=1.1.4

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 5.2.0 (latest) — 2021-07-30
- 5.1.4 — 2021-05-24
- 5.1.3 — 2021-04-23
- 5.1.2 — 2021-04-23
- 5.1.1 — 2021-04-23
- 5.1.0 — 2021-04-23
- 5.0.8 — 2021-04-21
- 5.0.7 — 2021-04-21
- 5.0.6 — 2021-04-16
- 5.0.5 — 2021-04-16
- 5.0.4 — 2021-04-16
- 5.0.3 — 2021-04-14
- 5.0.2 — 2021-04-14
- 5.0.1 — 2021-04-11
- 5.0.0 — 2021-04-11
- … 86 more at https://npm.io/package/relient/versions

## README

# Relient

Relient is an util set working closely with [Relient-CLI](https://github.com/zation/relient-cli). It will be useful
if your project is built on React, Redux and of course Relient-CLI.

## Install

```bash
$ npm install relient --save
```

## TOC

* [Redux utils](#redux-utils)
  * [Create action](#create-action)
  * [Create reducer](#create-reducer)
  * [Create store](#create-store)
* [Config](#config)
* [Formatters](#formatters)
* [I18N](#i18n)
* [DOM](#dom)

## Redux utils

Relient provides action, middleware, reducer utils to help you handle API request and merge the payload. [Check the API document](docs/redux.md)

### Create action

actions/account.ts

```js
import { createAction, actionTypeCreator, post, read, put, del } from 'relient/actions';

const actionType = actionTypeCreator(__filename);

export const READ_ALL = actionType('READ_ALL');
export const CREATE = actionType('CREATE');
export const UPDATE = actionType('UPDATE');
export const REMOVE = actionType('REMOVE');

export const readAll = createAction(
  READ_ALL,
  () => read('/account/all'),
);

export const create = createAction(
  CREATE,
  ({ username, password }) => post('/account', { username, password }),
);

export const update = createAction(
  UPDATE,
  ({ id, username, password }) => put(`/account/${id}`, { username, password }),
);

export const remove = createAction(REMOVE, ({ id }) => del(`/account/${id}`));

```

### Create reducer

reducers/account.ts

```js
import { merge, remove, handleActions, combineActions } from 'relient/reducers';
import { account } from '../schema';
import { UPDATE, READ_ALL, CREATE, REMOVE } from '../actions/account';

export default {
  account: handleActions({
    [combineActions(
      UPDATE,
      CREATE,
    )]: merge({ schema: account }),

    [READ_ALL]: merge({ schema: account }),

    [REMOVE]: remove(account),

  }, {}),
};

```

reducers/index.ts

```js
import { createEntitiesReducer, history, serverError } from 'relient/reducers';
import { combineReducers } from 'redux';
import { account } from './account'

export default combineReducers({
  ...createEntitiesReducer([ account ]),
  history,
  serverError,
});

```

### Create store

```js
import { fetch, history, serverError } from 'relient/middleware'
import { createStore, applyMiddleware } from 'redux';
import fetchInstance from 'isomorphic-fetch/fetch-npm-browserify' // or 'isomorphic-fetch/fetch-npm-node' in server side
import reducers from './reducers';

const initialState = {};

export default createStore(
  reducers,
  initialState,
  applyMiddleware({
    fetch({
      fetch: fetchInstance,
      apiDomain: 'https://your-api-domain',
      getDefaultHeader: ({ getState, withoutAuth }) => {
        // Return your default header object
      },
    })
    serverError({
      onUnauthorized: ({ payload, dispatch, getState }) => {
        // Handle 401 and 403 error
      },
      onGlobalWarning: ({ payload, dispatch, getState }) => {
        // Handle global API error
      }
    }),
  }),
);

```

## Config

Relient config provide consistent configuration between server side and client side based on [node-config](https://github.com/lorenwest/node-config).

You need to inject the global configs in client side:

html.js

```js
import React from 'react';
import getClientConfig from 'relient/config/client-config';

export default () => (
  <html className="no-js" lang="en">
    <head>
    	...
    </head>
    <body>
      <script dangerouslySetInnerHTML={{ __html: getClientConfig(['slogan']) }} />
      ...
    </body>
  </html>
)
```

Then you can use `getConfig` in your code:

header.js

```js
import getConfig from 'relient/config';

export default () => (
  <div>{getConfig('slogan')}</div>
)
```

## Formatters

Relient provides common formatters to display date, time, price and percentage. [Check the API document](docs/formatter.md)

```js
import { date, time, price, percentage } from 'relient/formatters'

const originalDate = new Date('2018-01-01');
const formattedDate = date('YYYY-M-D')(originalDate); // 2018-1-1

const originalTime = new Date('2018-01-01 01:01:01');
const formattedDate = date('YYYY-M-D H:m:s')(originalTime); // 2018-1-1 1:1:1

const originalPrice = 100;
const formattedPrice = price()(originalPrice); // 100.00

const originalPercentage = 0.2;
const formattedPercentage = percentage()(originalPercentage); // 20%
```

## I18N

Relient provides a simple i18n mechanism, which compatible with React: [Check the API document](docs/i18n.md)

```js
import React from 'react';
import i18n from 'relient/i18n';

const messages = {
  title: 'The total mount is {total}, and the top one is {top}',
}

export default () => (
  <div>{i18n(messages)('total', { total: 1, top: <b>Alex</b> })}</div>
)
```

## DOM

Relient provides a simple **head** elements maintenance utils: [Check the API document](docs/dom.md)

```js
import { updateMeta, updateCustomMeta, updateLink } from 'relient/dom';

updateMeta('description', 'web site description');
updateCustomMeta('og:url', 'canonical url');
updateLink('canonical', 'canonical url');
```

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