3.0.19 • Published 2 months ago

@eitje/easy_api v3.0.19

Weekly downloads
263
License
ISC
Repository
github
Last release
2 months ago

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:

KeyExplanationDefault value
ignoreStampdo not send last updated stamp (disable caching)
invertedFlip directions: get all records updated BEFORE stamp
localKindThe local redux 'kind', useful if you want to save it differently in your local store than it's called in your back-end
refreshIgnore caching & reset redux store
paramsextra 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:

KeyExplanationDefault value
localIf local is false, it will only save it in the back-end but not in the local redux storetrue
localKindThe local redux 'kind', useful if you want to save it differently in your local store than it's called in your back-end
extraParamsextra 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

3.0.19

2 months ago

3.0.16

9 months ago

3.0.17

9 months ago

3.0.14

10 months ago

3.0.15

10 months ago

3.0.18

9 months ago

3.0.13

11 months ago

3.0.12

11 months ago

3.0.9-alpha.slow

11 months ago

3.0.10

11 months ago

3.0.11

11 months ago

3.0.6-alpha.1

1 year ago

3.0.6-alpha.0

1 year ago

3.0.11-alpha.2

11 months ago

3.0.11-alpha.3

11 months ago

3.0.11-alpha.1

11 months ago

3.0.9-alpha.2

11 months ago

3.0.8

12 months ago

3.0.7

1 year ago

3.0.6

1 year ago

3.0.5

1 year ago

3.0.9

11 months ago

3.0.4

1 year ago

3.0.3-eitje

2 years ago

3.0.4-eitje

2 years ago

3.0.5-eitje

2 years ago

3.0.3

1 year ago

3.0.6-eitje

2 years ago

3.0.2-eitje

2 years ago

3.0.1-eitje

2 years ago

3.0.2

2 years ago

3.0.1

2 years ago

3.0.0

2 years ago

3.0.0-eitje

2 years ago

3.0.17-alpha

2 years ago

2.1.14

2 years ago

2.1.12

2 years ago

2.1.13

2 years ago

2.1.10

2 years ago

2.1.11

2 years ago

3.0.18-alpha

2 years ago

3.0.16-alpha

2 years ago

3.0.12-alpha

2 years ago

3.0.15-alpha

2 years ago

3.0.14-alpha

2 years ago

3.0.13-alpha

2 years ago

3.0.11-alpha

2 years ago

3.0.8-alpha

2 years ago

3.0.10-alpha

2 years ago

3.0.7-alpha

2 years ago

3.0.9-alpha

2 years ago

3.0.1-alpha

2 years ago

3.0.4-alpha

2 years ago

2.1.9

2 years ago

3.0.6-alpha

2 years ago

3.0.3-alpha

2 years ago

2.1.2

2 years ago

2.1.1

2 years ago

2.1.4

2 years ago

2.1.3

2 years ago

2.1.6

2 years ago

2.1.5

2 years ago

2.0.12

2 years ago

2.1.8

2 years ago

2.1.7

2 years ago

2.1.0

2 years ago

3.0.2-alpha

2 years ago

3.0.5-alpha

2 years ago

2.0.9

2 years ago

2.0.8

2 years ago

3.0.0-alpha

2 years ago

2.0.11

2 years ago

2.0.10

2 years ago

2.0.5

2 years ago

2.0.4

2 years ago

2.0.7

2 years ago

2.0.6

2 years ago

2.0.3

2 years ago

2.0.2

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.2.3

3 years ago

1.2.2

3 years ago

1.2.0

3 years ago

1.2.1

3 years ago

1.1.19

3 years ago

1.1.18

3 years ago

1.1.16

3 years ago

1.1.15

3 years ago

1.1.17

3 years ago

1.1.9

3 years ago

1.1.8

3 years ago

1.1.12

3 years ago

1.1.11

3 years ago

1.1.10

3 years ago

1.1.14

3 years ago

1.1.13

3 years ago

1.1.7

3 years ago

1.1.6

3 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.31

3 years ago

1.0.30

3 years ago

1.0.29

3 years ago

1.0.28

3 years ago

1.0.27

3 years ago

1.0.26

3 years ago

1.0.25

3 years ago

1.0.24

3 years ago

1.0.23

3 years ago

1.0.19

3 years ago

1.0.22

3 years ago

1.0.21

3 years ago

1.0.20

3 years ago

1.0.18

3 years ago

1.0.17

3 years ago

1.0.16

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.9

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

4 years ago

1.0.0

4 years ago