integreat 0.7.39 → 0.7.40

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,50 +3,76 @@ const appendToAction = require('../utils/appendToAction')
3
3
  const createUnknownServiceError = require('../utils/createUnknownServiceError')
4
4
 
5
5
  const hasCollectionEndpoint = (endpoints) =>
6
- endpoints.some((endpoint) => endpoint.match && endpoint.match.scope === 'members')
6
+ endpoints.some(
7
+ (endpoint) => endpoint.match && endpoint.match.scope === 'members'
8
+ )
7
9
 
8
10
  const getIndividualItems = async (ids, action, getService) => {
9
- const responses = await Promise.all(ids.map((id) => get(appendToAction(action, { id }), { getService })))
10
- if (responses.some((response) => response.status !== 'ok' && response.status !== 'notfound')) {
11
- return { status: 'error', error: `One or more of the requests for ids ${ids} failed.` }
11
+ const responses = await Promise.all(
12
+ ids.map((id) => get(appendToAction(action, { id }), { getService }))
13
+ )
14
+ if (
15
+ responses.some(
16
+ (response) => response.status !== 'ok' && response.status !== 'notfound'
17
+ )
18
+ ) {
19
+ return {
20
+ status: 'error',
21
+ error: `One or more of the requests for ids ${ids} failed.`,
22
+ }
12
23
  }
13
24
  return {
14
25
  status: 'ok',
15
26
  data: responses.reduce(
16
27
  (data, response) => [...data, ...[].concat(response.data)],
17
28
  []
18
- )
29
+ ),
19
30
  }
20
31
  }
21
32
 
22
- const getIdFromPayload = ({ id }) =>
23
- (Array.isArray(id) && id.length === 1) ? id[0] : id
33
+ const getIdFromData = (data) =>
34
+ Array.isArray(data)
35
+ ? data.length === 1 && data[0]
36
+ ? data[0].id
37
+ : undefined
38
+ : data
39
+ ? data.id
40
+ : undefined
24
41
 
25
- function filterResponseData (data, id) {
42
+ const getIdFromPayload = ({ id, data }) =>
43
+ Array.isArray(id) && id.length === 1 ? id[0] : id ? id : getIdFromData(data)
44
+
45
+ function filterResponseData(data, id) {
26
46
  if (!id || !Array.isArray(data)) {
27
47
  return data
28
48
  }
29
49
  const ids = [].concat(id)
30
- return data.filter(data => ids.includes(data.id))
50
+ return data.filter((data) => ids.includes(data.id))
31
51
  }
32
52
 
53
+ const removeData = ({ type, payload: { data, ...payload }, meta }) => ({
54
+ type,
55
+ payload,
56
+ meta,
57
+ })
58
+
33
59
  /**
34
60
  * Get several items from a service, based on the given action object.
35
61
  * @param {Object} action - payload and ident from the action object
36
62
  * @param {Object} resources - Object with getService
37
63
  * @returns {array} Array of data from the service
38
64
  */
39
- async function get (action, { getService } = {}) {
65
+ async function get(action, { getService } = {}) {
40
66
  const {
41
67
  type,
42
68
  service: serviceId = null,
43
69
  onlyMappedValues = false,
44
70
  endpoint,
45
- filterWithId = false
71
+ filterWithId = false,
46
72
  } = action.payload
47
73
 
48
- const service = (typeof getService === 'function')
49
- ? getService(type, serviceId) : null
74
+ const service =
75
+ typeof getService === 'function' ? getService(type, serviceId) : null
50
76
  if (!service) {
51
77
  return createUnknownServiceError(type, serviceId, 'GET')
52
78
  }
@@ -58,16 +84,20 @@ async function get (action, { getService } = {}) {
58
84
  return getIndividualItems(id, action, getService)
59
85
  }
60
86
 
61
- const endpointDebug = (endpoint) ? `endpoint '${endpoint}'` : `endpoint matching type '${type}' and id '${id}'`
87
+ const endpointDebug = endpoint
88
+ ? `endpoint '${endpoint}'`
89
+ : `endpoint matching type '${type}' and id '${id}'`
62
90
  debug('GET: Fetch from service %s at %s', service.id, endpointDebug)
63
91
 
64
- const { response } = await service.send(appendToAction(action, { id, onlyMappedValues }))
92
+ const { response } = await service.send(
93
+ appendToAction(removeData(action), { id, onlyMappedValues })
94
+ )
65
95
 
66
96
  return filterWithId
67
97
  ? {
68
- ...response,
69
- data: filterResponseData(response.data, id)
70
- }
98
+ ...response,
99
+ data: filterResponseData(response.data, id),
100
+ }
71
101
  : response
72
102
  }
73
103
 
package/lib/integreat.js CHANGED
@@ -5,7 +5,7 @@ const setupMapping = require('./mapping')
5
5
  const setupDispatch = require('./dispatch')
6
6
  const builtinActions = require('./actions')
7
7
 
8
- const version = '0.7.39'
8
+ const version = '0.7.40'
9
9
 
10
10
  /**
11
11
  * Return an Integreat instance with a dispatch method.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "integreat",
3
- "version": "0.7.39",
3
+ "version": "0.7.40",
4
4
  "description": "Node.js integration layer",
5
5
  "author": "Kjell-Morten Bratsberg Thorsen <post@kjellmorten.no> (http://kjellmorten.no/)",
6
6
  "license": "ISC",
@@ -46,7 +46,7 @@
46
46
  },
47
47
  "dependencies": {
48
48
  "@sindresorhus/is": "^1.2.0",
49
- "debug": "^4.3.3",
49
+ "debug": "^4.3.4",
50
50
  "got": "^9.6.0",
51
51
  "later": "^1.2.0",
52
52
  "map-any": "^0.2.1",
@@ -62,9 +62,9 @@
62
62
  "coveralls": "^3.1.1",
63
63
  "dotenv": "^10.0.0",
64
64
  "integreat-adapter-json": "^0.2.1",
65
- "nock": "^13.2.4",
65
+ "nock": "^13.2.9",
66
66
  "nyc": "^15.1.0",
67
- "prettier": "^2.5.1",
67
+ "prettier": "^2.8.1",
68
68
  "sinon": "^11.1.2"
69
69
  }
70
70
  }