# request-model

> A async task excute lib with the chain usage API.

Latest version **1.0.4** (published 2019-05-26) · ISC license · 0 weekly downloads

## Install

```sh
npm install request-model
pnpm add request-model
yarn add request-model
bun add request-model
```

## Health

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

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

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.4 |
| Published | 2019-05-26 |
| First published | 2018-05-31 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 179 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 6 |
| Author | luobata |
| Maintainers | luobata |

## Links

- npm: https://www.npmjs.com/package/request-model
- Repository: https://github.com/Luobata/request-model
- Homepage: https://github.com/Luobata/request-model#readme
- Issues: https://github.com/Luobata/request-model/issues
- npm.io page: https://npm.io/package/request-model

## Recent versions

- 1.0.4 (latest) — 2019-05-26
- 1.0.3 — 2019-05-26
- 1.0.2 — 2019-04-01
- 1.0.1 — 2018-11-01
- 1.0.0 — 2018-09-18
- 0.0.12 — 2018-09-03
- 0.0.11 — 2018-08-27
- 0.0.10 — 2018-07-12
- 0.0.9 — 2018-07-02
- 0.0.8 — 2018-06-29
- 0.0.7 — 2018-06-12
- 0.0.6 — 2018-06-11
- 0.0.5 — 2018-06-11
- 0.0.4 — 2018-06-11
- 0.0.3 — 2018-06-06
- … 1 more at https://npm.io/package/request-model/versions

## README

# request-model

request-model is a useful async function management lib and help you coding more happy.

If you like to **make your async function sequential execution and in a chain usage**, you will like request-model.

## Installation

```js
npm install --save-dev request-model
```

## Usage

```js
import requestModel from 'request-model';

const rModel = new RequestModel({
    request: {
        getNameById(params) {
            return new Promise((resolve, reject) => {
                setTimeout(() => {
                    resolve(1);
                }, 2000);
            });
        },
        enums(params, params2) {
            return new Promise((resolve, reject) => {
                setTimeout(() => {
                    resolve(2);
                }, 1000);
            });
            // 返回一个Promise
        },
    },
});

// first getNameById and finsh start enums
rModel
    .chain()
    .commit('getNameById', 1)
    .commit('enums', 2);
```

## API

#### Constructor(request-model)

```js
import requestModel from 'request-model';

const rModel = new RequestModel({
    ...options,
});
```

##### Construction options

-   **request**:

    type `{ [type: string]: Function }`

-   **config**: config for requestModel, the sub module config will extends its parent config.

    | key         | type    | default | description                              |
    | :---------- | :------ | :------ | :--------------------------------------- |
    | promiseWrap | boolean | false   | Wrap your request fucntion with Promise. |

-   **module**: sub modules to request-model(**multistage sub modules support now**)

    type `[type: string]: another request-model options like`

    ```js
    {
      key: {
        request: {
          [string]: Function
        },
        modules: {
            ...
        }
      }
    }
    ```

-   **action**: the collection of the commit.

    type `[type: string]: Function`

    params `(chain: Chain, ...args: any[])`

    ```js
    action: {
        init(chain, ...args) {
            return chain
                .commit('getNameById', args[0])
                .commit('enums', args[1]);
        },
    }
    ```

#### Methods

-   **chain**

    Use to start the chain usage.

    return `new Chain()`

-   collection
    Use to get a collection.

    return `new Collection()`

-   PROMISEWRAP(**static**)

    Wrap a function to promise.

    params`(fn: Function)`
    return`Function`

-   commitWrap

    params `key: string (the function name in the constructor request)`

-   commitAll

    params `The return Array of commitWrap`

    etc:

    ```js
    rModel
        .chain()
        .commit('getNameById', 1)
        .then(data => {
            return rModel.commitAll([
                rModel.commitWrap('enums2', 2),
                rModel.commitWrap('enums4', 4),
            ]);
        .finish(data => {
            // data = [1 [2, 4]]
        });
    ```

-   every method in Chain can be use and the same input and output.

    etc:

    ```js
    rModel.chain().commit('getNameById', 1);
    // same as
    rModel.commit('getNameById', 1);
    ```

---

#### Chain

generator after requestModel.chain()

#### Methods

-   **commit**

    params `key: string`

    return `Chain`

-   then

    join your commit chain with another Function.

    params `(resolve: Function, ?reject: Function, ?always: Function, ?before: Function)`

    return `Chain`

-   finish / finally (same usage)

    add a finish Function will resolve after all the commit.

    params `(resolve: Function, ?reject: Function, ?always: Function)`

    return `Chain`

-   catch

    add a error catch Functin.

    params `(reject: Function)`

    return `Chain`

-   always

    add a always Functin. **It will be called when after last resolve or reject(also resolve and reject could be empty)**
    **not support usage with always only(without commit/then)**

    params `(alwaysFn: Function)`

    return `Chain`

#### Collection(extends Chain)

generator after requestModel.collection().

-   add
    add a Function into the collection.

    params`add(fn: Function, key?: string)` The key is not nessary if the fn has its name(not anoymous)
    return `Collection`
    etc:

    ```Js
    const collection = rModel.collection();
    collection.add(
        RequestModel.PROMISEWRAP((resolve, reject, params) => {
            setTimeout(() => {
                resolve(params);
            }, 0);
        }),
        'a',
    );
    collection.add(
        RequestModel.PROMISEWRAP((resolve, reject, params) => {
            setTimeout(() => {
                resolve(params);
            }, 0);
        }),
        'b',
    );
    collection
        .commit('a', 1)
        .commit('b', 2)
        .finish(data => {
            assert.deepEqual(data, [1, 2]);
            done();
        });
    ```

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