Skip to content
This repository was archived by the owner on Jun 18, 2020. It is now read-only.

Commit cf4a23c

Browse files
authored
Improvement/request params (#190)
* added new concept of request params * fill in empty object as default for requestParams * add integration spec for request params * updated readme * added specs for derive request id * 6.1.0-0
1 parent ab31d66 commit cf4a23c

24 files changed

+394
-59
lines changed

README.md

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ Each type must export a certain set of attributes. These contain descriptions
4444
about the structure of each type and also methods to read, create or modify an
4545
instance of that type.
4646

47-
| Attribute | Required | Description |
48-
| ------------------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
49-
| schema | `true` | A [normalizr](https://github.com/paularmstrong/normalizr) schema that is used to store and access your data. |
50-
| collection | `true` | Identifier where all retrieved instances for that type will be stored. |
51-
| fetch(query, body) | `false` | A method that takes a custom set of properties and maps it to a call of [`callAPI`](https://github.com/signavio/generic-api/blob/master/src/callApi.js) to retrieve data from your backend. |
52-
| create(query, body) | `false` | Function that describes how an instance is created. |
53-
| update(query, body) | `false` | Function that describes how an instance is updaetd. |
54-
| remove(query, body) | `false` | Function that describes how an instance is removed. |
55-
| cachePolicy | `false` | The cache policy that should be used for that type. See [cache policies](TODO) for the possible options. |
47+
| Attribute | Required | Description |
48+
| ---------------------------------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
49+
| schema | `true` | A [normalizr](https://github.com/paularmstrong/normalizr) schema that is used to store and access your data. |
50+
| collection | `true` | Identifier where all retrieved instances for that type will be stored. |
51+
| fetch(query, body, requestParams) | `false` | A method that takes a custom set of properties and maps it to a call of [`callAPI`](https://github.com/signavio/generic-api/blob/master/src/callApi.js) to retrieve data from your backend. |
52+
| create(query, body, requestParams) | `false` | Function that describes how an instance is created. |
53+
| update(query, body, requestParams) | `false` | Function that describes how an instance is updaetd. |
54+
| remove(query, body, requestParams) | `false` | Function that describes how an instance is removed. |
55+
| cachePolicy | `false` | The cache policy that should be used for that type. See [cache policies](TODO) for the possible options. |
5656

5757
### `callApi(fullUrl, schema[, options])`
5858

@@ -234,14 +234,15 @@ and created a new prop `userFetch` that represents the API request. You include
234234
certain configuration options to get more power over when and how requests go
235235
out.
236236

237-
| Option | Default | Required | Description |
238-
| ----------- | ----------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------------------- |
239-
| type | `undefined` | `true` | The API type which is concered. |
240-
| query | `undefined` | `false` | An object of query parameters. |
241-
| id | `undefined` | `false` | Shortcut to provide an ID query parameter, `id: 1` is equivalent to `query: { id: 1 }` |
242-
| method | `fetch` | `false` | Either one of `fetch`, `create`, `update`, or `remove`. |
243-
| lazy | `false` | `false` | Only useful in combination with `method: 'fetch'`. Per default, the resource is fetched on component mount. Set `lazy: true` to not fetch on mount. |
244-
| denormalize | `false` | `false` | Inline all referenced data into the injected prop. |
237+
| Option | Default | Required | Description |
238+
| ------------- | ----------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------------------- |
239+
| type | `undefined` | `true` | The API type which is concered. |
240+
| query | `undefined` | `false` | An object of query parameters. |
241+
| requestParams | `undefined` |  `false` | An object of extra parameters to tell different requests apart. |
242+
| id | `undefined` | `false` | Shortcut to provide an ID query parameter, `id: 1` is equivalent to `query: { id: 1 }` |
243+
| method | `fetch` | `false` | Either one of `fetch`, `create`, `update`, or `remove`. |
244+
| lazy | `false` | `false` | Only useful in combination with `method: 'fetch'`. Per default, the resource is fetched on component mount. Set `lazy: true` to not fetch on mount. |
245+
| denormalize | `false` | `false` | Inline all referenced data into the injected prop. |
245246

246247
#### Notes on using `denormalize: true`
247248

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@signavio/kraken",
3-
"version": "6.0.1",
3+
"version": "6.1.0-0",
44
"description": "Load API entities",
55
"repository": "git@github.com:signavio/kraken.git",
66
"main": "lib/index.js",

src/components/helpers/mapDispatchToPropsFactory.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const mapDispatchToPropsFactory = ({
4040
type: entityType,
4141
method,
4242
query = {},
43+
requestParams = {},
4344
refresh,
4445
requiredFields,
4546
}) => {
@@ -55,14 +56,23 @@ const mapDispatchToPropsFactory = ({
5556
switch (method) {
5657
case 'fetch':
5758
return (body: Body) =>
58-
actionCreator({ entityType, query, refresh, requiredFields, body })
59+
actionCreator({
60+
entityType,
61+
query,
62+
requestParams,
63+
refresh,
64+
requiredFields,
65+
body,
66+
})
5967
case 'create':
6068
return (body: Body) =>
61-
actionCreator({ entityType, elementId, query, body })
69+
actionCreator({ entityType, elementId, query, requestParams, body })
6270
case 'update':
63-
return (body: Body) => actionCreator({ entityType, query, body })
71+
return (body: Body) =>
72+
actionCreator({ entityType, query, requestParams, body })
6473
case 'remove':
65-
return (body: Body) => actionCreator({ entityType, query, body })
74+
return (body: Body) =>
75+
actionCreator({ entityType, query, requestParams, body })
6676
default:
6777
throw new Error(`Unkown dispatch method ${method}`)
6878
}

src/components/helpers/mapStateToProps.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,35 @@ const mapStateToProps = ({ types, finalMapPropsToPromiseProps }) => () => {
2121
// function can figure out whether s.th. has changed
2222
const stateProps = {
2323
...mapKeys(
24-
mapValues(promiseProps, ({ query, type, method }, propName) =>
25-
getRequestState(types, state, {
26-
type: actionTypes[`${method.toUpperCase()}_DISPATCH`],
27-
payload: {
28-
entityType: type,
29-
query,
30-
elementId,
31-
propName,
32-
},
33-
})
24+
mapValues(
25+
promiseProps,
26+
({ query, requestParams, type, method }, propName) =>
27+
getRequestState(types, state, {
28+
type: actionTypes[`${method.toUpperCase()}_DISPATCH`],
29+
payload: {
30+
entityType: type,
31+
query,
32+
requestParams,
33+
elementId,
34+
propName,
35+
},
36+
})
3437
),
3538
(val, propName) => `${propName}_request`
3639
),
3740
...mapKeys(
3841
mapValues(
3942
promiseProps,
40-
({ query, refresh, type, method, denormalize }, propName) => {
43+
(
44+
{ query, requestParams, refresh, type, method, denormalize },
45+
propName
46+
) => {
4147
const entityState = getEntityState(types, state, {
4248
type: actionTypes[`${method.toUpperCase()}_DISPATCH`],
4349
payload: {
4450
entityType: type,
4551
query,
52+
requestParams,
4653
refresh,
4754
elementId,
4855
denormalizeValue: denormalize,

src/components/helpers/mergeProps.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import { mapValues } from 'lodash'
22

3-
const mergeProps = ({ finalMapPropsToPromiseProps }) => (stateProps, dispatchProps, ownProps) => {
3+
const mergeProps = ({ finalMapPropsToPromiseProps }) => (
4+
stateProps,
5+
dispatchProps,
6+
ownProps
7+
) => {
48
const promiseProps = finalMapPropsToPromiseProps(ownProps)
59

6-
const joinPromiseValue = (propName) => {
10+
const joinPromiseValue = propName => {
711
const promise = stateProps[`${propName}_request`]
812
const entity = stateProps[`${propName}_entity`]
913

@@ -28,17 +32,19 @@ const mergeProps = ({ finalMapPropsToPromiseProps }) => (stateProps, dispatchPro
2832
}
2933

3034
const mergePromiseStateToActionCreator = (propName, promiseState) => {
31-
return Object.assign((...args) => dispatchProps[propName](...args), promiseState)
35+
return Object.assign(
36+
(...args) => dispatchProps[propName](...args),
37+
promiseState
38+
)
3239
}
3340

3441
// now it's time to join the `${propName}_entity` with the `${propName}_request` props
3542
return {
3643
...ownProps,
3744
...dispatchProps,
38-
...mapValues(promiseProps, (value, propName) => mergePromiseStateToActionCreator(
39-
propName,
40-
joinPromiseValue(propName)
41-
)),
45+
...mapValues(promiseProps, (value, propName) =>
46+
mergePromiseStateToActionCreator(propName, joinPromiseValue(propName))
47+
),
4248
}
4349
}
4450

src/reducers/requestsReducer.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const requestsReducer = (state: RequestsState, action: Action) => {
2424
...request,
2525
outstanding: true,
2626
query: payload.query,
27+
requestParams: payload.requestParams,
2728
pending: true,
2829
refresh:
2930
payload.refresh !== undefined ? payload.refresh : request.refresh,

src/sagas/watchCreateDispatch.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ export const createCreateDispatch = (types: ApiTypeMap) => {
1515
const entityType = action.payload.entityType
1616
const create = getCreate(types, entityType)
1717

18-
const result = yield call(create, action.payload.query, action.payload.body)
18+
const result = yield call(
19+
create,
20+
action.payload.query,
21+
action.payload.body,
22+
action.payload.requestParams
23+
)
1924

2025
if (!result.error) {
2126
yield put(

src/sagas/watchFetchDispatch.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ export const createFetchSaga = (types: ApiTypeMap) => {
3636
const { response, error, status } = yield call(
3737
fetch,
3838
action.payload.query,
39-
action.payload.body
39+
action.payload.body,
40+
action.payload.requestParams
4041
)
4142

4243
if (!error) {

src/sagas/watchRemoveDispatch.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ export function createRemoveDispatch(types: ApiTypeMap) {
1818
const { error, status } = yield call(
1919
remove,
2020
action.payload.query,
21-
action.payload.body
21+
action.payload.body,
22+
action.payload.requestParams
2223
)
2324

2425
if (!error) {

src/sagas/watchUpdateDispatch.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@ export function createUpdateDispatch(types: ApiTypeMap) {
1919
const entityType = action.payload.entityType
2020

2121
const update = getUpdate(types, entityType)
22-
const result = yield call(update, action.payload.query, action.payload.body)
22+
const result = yield call(
23+
update,
24+
action.payload.query,
25+
action.payload.body,
26+
action.payload.requestParams
27+
)
2328

2429
if (result.response) {
2530
yield put(

0 commit comments

Comments
 (0)