1.22.7 • Published 3 years ago

fetch-normalize-data v1.22.7

Weekly downloads
130
License
MPL-2.0
Repository
github
Last release
3 years ago

A library to obtain a state of normalized data. Special fetch and reducer helpers are also provided in the export.

An implementation with a redux-thunk context is here, there for redux-saga. You can see also a poc with only a React Context (redux free) at this. Also, see this post for a presentation based on the pass culture project.

CircleCI npm version

Basic Usage

Merge

If you want to merge patched data into an already existing state:

import { getNormalizedMergedState } from 'fetch-normalize-data'

const state = {
  authors: [{ id: 0, name: "John Marxou" }],
  books: [{ authorId: 0, id: 0, text: "my foo" }]
}

const patch = {
  books: [
    {
      author: { id: 1, name: "Edmond Frostan" },
      id: 1,
      text: "you foo"
    }
  ]
}

const config = {
  normalizer: {
    books: {
      normalizer: {
        // short syntax here: <datumKey>: <stateKey>
        author: "authors"
      },
      stateKey: "books"
    }
  }
}

const nextState = getNormalizedMergedState(state, patch, config)

console.log(nextState)

We have:

{
  authors: [
    { id: 0, name: "John Marxou" },
    { id: 1, name: "Edmond Frostan" }
  ],
  books: [
    { authorId: 0, id: 0, text: "my foo" },
    {
      authorId: 1,
      id: 1,
      text: "you foo"
    }
  ]
}

Delete

import { getNormalizedDeletedState } from 'fetch-normalize-data'

const state = {
  authors: [{ id: 0, name: "John Marxou" }],
  books: [{ authorId: 0, id: 0, text: "my foo" }]
}

const patch = {
  books: [{ id: 1 }]
}

const nextState = getNormalizedDeletedState(state, patch, config)

console.log(nextState)

We have:

{
  authors: [
    { id: 0, name: "John Marxou" }
  ],
  books: []
}

Usage with config

config of getNormalizedMergedState can have:

nametypeexampleisRequireddefaultdescription
isMergingArrayboolSee testnontruedecide if nextState. will be a merge of previous and next data or just a replace with the new array
isMergingDatumboolSee testnonfalsedecide if nextState.<arrayName>[...<datum>] will be a merge from previous and next datum or just a replace with next datum
isMutatingArrayboolSee testnontruedecide if nextState. will be a concat or a merge from previous array
isMutatingDatumboolSee testnonfalsedecide if nextState.<arrayName>[...<datum>] will be a clone or a merge into the previous datum
normalizerobjetSee testnonnulla nested object giving relationships between datumKeys and entities to be store at stateKeys

Fetch usage

fetchData

nametypeexampleisRequireddefaultdescription
apiPathstring/foosnoundefinedapiPath will be join with rootUrl to build the request url
handleFailfunction(state, action)TBPnoundefinedcallback called if request has failed
handleSuccessfunction(state, action)TBPnoundefinedcallback called if request is a success
methodSTRINGPOSTno'GET'http method for the request
stateKeystringfoosno<computed from apiPath or url>key into the store.getState().data.<stateKey> where normalized merged or deleted data will be applied
urlstringhttps://momarx.com/foosnoundefinedtotal url of the request that will be used if apiPath is not used

Reducer usage

fetch-normalize-data can play mainly with 3 types of actions:

  • REQUEST_DATA_(DELETE|GET|POST|PUT|PATCH)_(.*)
  • SUCCESS_DATA_(DELETE|GET|POST|PUT|PATCH)_(.*)
  • FAIL_DATA_(DELETE|GET|POST|PUT|PATCH)_(.*)

The action creator requestData is almost the only one you will need to use for fetching data at mount time or at event mutation time.

requestData

You can play with actions, but you need a special installation given the async action handler you take in your app:

See for example an example with https://github.com/betagouv/redux-thunk-data, but global use is like:

import { requestData } from 'fetch-normalize-data'

const config = {
  apiPath: '/foos',
  normalizer: {
    'bar': {
      stateKey: 'bars'
    }
  }
}

store.dispatch(requestData(config))

where config is all the possible config parameters you can find in config of getNormalizedMergedState or in the config of fetchData.

1.22.7

3 years ago

1.22.5

3 years ago

1.22.6

3 years ago

1.22.4

3 years ago

1.22.0

3 years ago

1.22.3

3 years ago

1.22.1

3 years ago

1.22.2

3 years ago

1.21.17

3 years ago

1.21.14

3 years ago

1.21.15

3 years ago

1.21.16

3 years ago

1.21.8

3 years ago

1.21.9

3 years ago

1.21.7

3 years ago

1.21.10

3 years ago

1.21.11

3 years ago

1.21.12

3 years ago

1.21.13

3 years ago

1.21.6

3 years ago

1.21.5

3 years ago

1.18.20

3 years ago

1.21.4

3 years ago

1.21.0

3 years ago

1.21.2

3 years ago

1.21.3

3 years ago

1.20.1

3 years ago

1.20.0

3 years ago

1.19.0

3 years ago

1.19.1

3 years ago

1.18.19

3 years ago

1.18.18

3 years ago

1.18.17

3 years ago

1.18.16

3 years ago

1.18.15

3 years ago

1.18.14

3 years ago

1.18.13

3 years ago

1.18.9

3 years ago

1.18.12

3 years ago

1.18.11

3 years ago

1.18.10

3 years ago

1.18.8

3 years ago

1.18.7

3 years ago

1.18.5

3 years ago

1.18.6

3 years ago

1.18.4

3 years ago

1.18.3

3 years ago

1.18.2

3 years ago

1.18.1

3 years ago

1.18.0

3 years ago

1.17.6

3 years ago

1.17.5

3 years ago

1.17.4

3 years ago

1.17.3

3 years ago

1.17.2

3 years ago

1.17.1

3 years ago

1.16.6

3 years ago

1.16.5

3 years ago

1.16.4

3 years ago

1.16.3

3 years ago

1.16.2

3 years ago

1.16.1

3 years ago

1.16.0

3 years ago

1.13.3-async3

3 years ago

1.13.3-async1

3 years ago

1.15.1

3 years ago

1.15.0

3 years ago

1.14.3

3 years ago

1.13.3

3 years ago

1.13.2

3 years ago

1.13.1

3 years ago

1.12.2

4 years ago

1.12.1

4 years ago

1.12.0

4 years ago

1.11.0

4 years ago

1.10.19

4 years ago

1.10.18-rc6

4 years ago

1.10.18-rc3

4 years ago

1.10.18-rc4

4 years ago

1.10.18-rc5

4 years ago

1.10.18-rc2

4 years ago

1.10.18-rc1

4 years ago

1.10.17

4 years ago

1.10.16-rc4

4 years ago

1.10.16-rc3

4 years ago

1.10.16-rc1

4 years ago

1.10.15

4 years ago

1.10.14

4 years ago

1.10.11

4 years ago

1.10.10

4 years ago

1.10.9

4 years ago

1.10.8

4 years ago

1.10.7

4 years ago

1.10.6

4 years ago

1.10.5

4 years ago

1.10.4

4 years ago

1.10.3

4 years ago

1.10.2

4 years ago

1.10.1

4 years ago

1.10.0-beta-1

4 years ago

1.10.0-timeout-6

4 years ago

1.10.0-timeout-5

4 years ago

1.10.0-timeout-4

4 years ago

1.10.0-timeout

4 years ago

1.10.0-timeout-3

4 years ago

1.10.0-timeout-2

4 years ago

1.10.0-rc3

4 years ago

1.10.0-rc2

4 years ago

1.10.0-rc1

4 years ago

1.9.0

4 years ago

1.8.6

5 years ago

1.8.4

5 years ago

1.8.3

5 years ago

1.8.2

5 years ago

1.8.1

5 years ago

1.8.0-rc3

5 years ago

1.8.0-rc2

5 years ago

1.8.0-rc1

5 years ago

1.7.0

5 years ago

1.6.0

5 years ago

1.5.0-rc6

5 years ago

1.5.0-rc5

5 years ago

1.5.0-rc4

5 years ago

1.5.0-rc3

5 years ago

1.5.0-rc2

5 years ago

1.5.0-rc1

5 years ago

1.5.0-rc

5 years ago

1.4.1

5 years ago

1.4.0

5 years ago

1.3.2

5 years ago

1.3.1

5 years ago

1.3.0

5 years ago

1.2.3

5 years ago

1.2.2

5 years ago

1.2.0

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.0

5 years ago

0.0.12

5 years ago

0.0.11

5 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago