# mobx-collection-rest-api

> REST API decorator for mobx-collection

Latest version **0.1.0** (published 2016-07-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install mobx-collection-rest-api
pnpm add mobx-collection-rest-api
yarn add mobx-collection-rest-api
bun add mobx-collection-rest-api
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2016-07-12 |
| First published | 2016-07-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Łukasz Grolik |
| Maintainers | lukaszgrolik |
| Keywords | rest, api, decorator, mobx, collection |

## Links

- npm: https://www.npmjs.com/package/mobx-collection-rest-api
- Repository: https://github.com/lukaszgrolik/mobx-collection-rest-api
- Issues: https://github.com/lukaszgrolik/mobx-collection-rest-api/issues
- npm.io page: https://npm.io/package/mobx-collection-rest-api

## 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

- 0.1.0 (latest) — 2016-07-12

## README

# mobx-collection-rest-api

REST API decorator for [mobx-collection](https://github.com/lukaszgrolik/mobx-collection)

## Getting started

`npm install --save axios mobx-collection mobx-collection-rest-api`

```js
import axios from 'axios';
import Collection from 'mobx-collection';
import restApi from 'mobx-collection-rest-api';

@restApi({
  axios: axios.create({
    baseURL: '/api',
  }),
  endpoint: '/foos',
  transformPayload: data => ({foo: data}),
})
class FooCollection extends Collection {

}

const foos = new FooCollection();

foos.fetchAll()
.then(() => {
  foos.filter();
  // => [/* records from "GET /api/foos" response */]
});
```

## API

### Options

#### axios

Axios instance to be used for API requests.

- type: [axios](https://github.com/mzabriskie/axios)
- required

```js
@restApi({
  // ...
  axios: axios.create({
    baseURL: '/api/v2',
  }),
})
```

#### endpoint

Endpoint for CRUD operations.

- type: *string*
- default: `'/'`

```js
@restApi({
  // ...
  endpoint: '/users',
})
```

#### transformPayload

Transform payload before making POST or PUT request using `#create` or `#update` method.

- type: *function* with signature `function(record)` where record is object to be transformed

```js
@restApi({
  // ...
  transformPayload: user => {
    return {
      user: {
        id: user.id,
        name: user.username,
      },
    };
  },
})
```

### Methods

Decorator extends Collection's prototype with following methods:

- `#fetchAll()` - GET /{endpoint}
- `#fetch(id)` - GET /{endpoint}/{id}
- `#create(payload)` - POST /{endpoint}
- `#update(id, payload)` - PUT /{endpoint}/{id}
- `#delete(id)` - DELETE /{endpoint}/{id}

Record(s) from API response are automatically injected into collection (or ejected in case of `#delete` method).

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