# @eitje/easy_api

> Easy communcation between a modern front-end application & a RESTful API

Latest version **3.0.32** (published 2025-07-17) · ISC license · 0 weekly downloads

## Install

```sh
npm install @eitje/easy_api
pnpm add @eitje/easy_api
yarn add @eitje/easy_api
bun add @eitje/easy_api
```

## Health

**Score 35/100 (D)** — status: stable.

Positive: no vulnerabilities.

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

Negative: stale.

## Facts

| | |
|---|---|
| Version | 3.0.32 |
| Published | 2025-07-17 |
| First published | 2020-11-03 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 141.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Abel van Hoek |
| Maintainers | jurriaan, abeltje1, thijs-eitje |

## Links

- npm: https://www.npmjs.com/package/@eitje/easy_api
- Repository: https://github.com/eitje-app/easy_api
- Homepage: https://github.com/eitje-app/easy_api#readme
- Issues: https://github.com/eitje-app/easy_api/issues
- npm.io page: https://npm.io/package/@eitje/easy_api

## Recent versions

- 3.0.32 (latest) — 2025-07-17
- 3.0.6-alpha.1 (alpha) — 2023-04-19
- 3.0.31 — 2025-06-23
- 3.0.30 — 2025-06-03
- 3.0.29 — 2025-06-03
- 3.0.28 — 2025-05-28
- 3.0.27 — 2025-02-10
- 3.0.26 — 2025-02-06
- 3.0.25 — 2024-05-29
- 3.0.24 — 2024-05-28
- 3.0.23 — 2024-05-28
- 3.0.22 — 2024-05-28
- 3.0.21 — 2024-05-24
- 3.0.20 — 2024-05-17
- 3.0.19 — 2024-02-28
- … 134 more at https://npm.io/package/@eitje/easy_api/versions

## README

### Easy communcation between a modern front-end application & a RESTful API 

It exports a few things, the most important being 'API'.

## API:

API handles quite a bit: 
- it knows about the RESTful structure of our back-end and thus knows which endpoints map to which actions.
- it knows about our redux store and therefore is able to save the results automatically to the store
- it handles caching of the index endpoint by sending the latest updated_at value to the back-end (gets this through redux)
- it allows for easy after/before effects for all CRUD actions


Most important methods:

`index, create, update, destroy` 

Parameters:

For every method, the first parameter is 'kind', the pluralized name of the resource you're working, with, eg 'posts' or 'users'.

### Index:

Second argument is an object with possible keys:

| Key        | Explanation           | Default value  |
| ------------- |:-------------:| -----:|
| ignoreStamp     | do not send last updated stamp (disable caching) |  |
| inverted    | Flip directions: get all records updated BEFORE stamp      |    |
| localKind | The local redux 'kind', useful if you want to save it differently in your local store than it's called in your back-end     |    |
| refresh | Ignore caching & reset redux store    |    |
| params | extra params to be sent with the request   | {}    |



### Create/Update:

Create/update share the exact same parameters. The first argument is kind, the second 'params' (the data you wanna send) which is automatically converted to Rails' strong parameter style, as: `{record_name: data}` and the third is an object again with all other options:


| Key        | Explanation           | Default value  |
| ------------- |:-------------:| -----:|
| local    |  If local is false, it will only save it in the back-end but not in the local redux store    | true   |
| localKind | The local redux 'kind', useful if you want to save it differently in your local store than it's called in your back-end     |    |
| extraParams | extra params which will be inserted at the top level  | {}    |


### Destroy:

Destroy takes an id as a second argument, and accepts an object with as only key extraParams to be included at the top-level of your data.



Examples:

`const users = API.index("users") ## will get only new users`

`const users = API.index("users", {refresh: true}) ## will reset the redux store and fetch all fresh users from the back-end`



`const newUser = API.create("users", {name: 'Amazing guy', fat: false})`

`const updatedUser = API.update("users", {id: 4, fat: true}) ## you have to insert the ID when updating`


`API.destroy("users", 1) # destroy always goes by id`


## backend:

 Backend is actually just an apisauce instance, with which you can do everything you can do with apisauce. 
 It adds a few niceties: 
 - Headers needed for JSON communication
 - Auto inserting of access token
 - Dispatching start_load & end_load to the store for every non-get request so you can show a loading indicator
 - Handles & displays errors
 
 __NOTE:__ If you want to prevent the library from dispatching loading actions or prevent the lib from showing the success message, add `doNotLoad: true` to either the data or headers of your request. 
 
 
 ## selectors:
 
 It exports a few selectors for easy interaction with your redux store. All selectors take the store as the first argument and the kind as the second argument.
 The most important are:
 
 #### find:
   Find a record by a query (NOTE: is like Rails' find_by)
   
 `useSelector(state => find(state, 'users', {id: 4}) ## gets user 4`
 
  `useSelector(state => find(state, 'users', {activated: true, name: 'Jordan'}) ## gets user who's actived and whose name is Jordan`
  
  #### where:
  
  Identical to find in usage but returns multiple records
  
  ### all:
  Returns all records for a given kind
  `useSelector(state => all(state, 'users')`
  
  ### betweenDays:
  
  Takes an object as third parameter with **start** and **end** as keys and returns all records within that date range.
  
  `useSelector(state => all(state, 'posts', {start: "2020-01-01", end: "2020-12-31"})`
  
  **NOTE**: for this to work, your record should have a 'date' field.
  
  
  ### includes:
  
  Returns all records with at least one match, useful for finding associated records & querying array fields.
  
  `useSelector(state => includes(state, 'users', {team_ids: [1]})` Will return all users who have 1 in their team_ids array.
  
  **NOTE**: where would find all users whose team_ids value is EXACTLY EQUAL to [1]

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