integreat 0.7.39 → 0.7.41

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
 
@@ -135,10 +135,12 @@ const generateDoneParams = ({ from, type, done }) => {
135
135
  }
136
136
 
137
137
  const generateTransformParams = ({ transform }, { service, type }) => {
138
- if (transform) {
139
- return { service, type, ...transform }
138
+ if (Array.isArray(transform)) {
139
+ return transform.map((trans) => ({ service, type, ...trans }))
140
+ } else if (transform) {
141
+ return [{ service, type, ...transform }]
140
142
  }
141
- return null
143
+ return []
142
144
  }
143
145
 
144
146
  // TODO: Updated dates from the first fromParams are always used on toParams.
@@ -259,6 +261,19 @@ function getLastSyncedAt(
259
261
  }
260
262
  return undefined
261
263
  }
264
+
265
+ async function transformPipeline(data, dispatch, transformParams, meta) {
266
+ let response
267
+ for (const params of transformParams) {
268
+ const nextData = response ? response.data : data
269
+ response = await dispatch(createTransformAction(params, nextData, meta))
270
+ if (response.status !== 'ok') {
271
+ return response
272
+ }
273
+ }
274
+ return response
275
+ }
276
+
262
277
  async function transformData(
263
278
  data,
264
279
  dispatch,
@@ -269,13 +284,11 @@ async function transformData(
269
284
  if (useIndividualTransform) {
270
285
  return await Promise.all(
271
286
  data
272
- .map((item) =>
273
- dispatch(createTransformAction(transformParams, item, meta))
274
- )
287
+ .map((item) => transformPipeline(item, dispatch, transformParams, meta))
275
288
  .map((p) => pLimit(1)(() => p))
276
289
  )
277
290
  } else {
278
- return [await dispatch(createTransformAction(transformParams, data, meta))]
291
+ return [await transformPipeline(data, dispatch, transformParams, meta)]
279
292
  }
280
293
  }
281
294
 
@@ -337,7 +350,7 @@ async function sync({ payload, meta = {} }, { dispatch, getService }) {
337
350
  ) || new Date()
338
351
 
339
352
  // Dispatch transform action
340
- if (transformParams) {
353
+ if (transformParams.length > 0) {
341
354
  const transformResults = await transformData(
342
355
  data,
343
356
  dispatch,
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.41'
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.41",
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
  }