integreat 0.7.41 → 0.7.43

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.
@@ -1,37 +1,42 @@
1
1
  const action = require('../utils/createAction')
2
2
  const createError = require('../utils/createError')
3
3
 
4
- const getExpired = async (payload, ident, dispatch) => {
5
- const {
6
- service,
7
- type,
8
- endpoint,
9
- msFromNow = 0
10
- } = payload
4
+ const getExpired = async (payload, ident, dispatch, gid, cid) => {
5
+ const { service, type, endpoint, msFromNow = 0 } = payload
11
6
 
12
7
  const timestamp = Date.now() + msFromNow
13
8
  const isodate = new Date(timestamp).toISOString()
14
- const payloadGet = { service, type, endpoint, onlyMappedValues: true, timestamp, isodate }
9
+ const payloadGet = {
10
+ service,
11
+ type,
12
+ endpoint,
13
+ onlyMappedValues: true,
14
+ timestamp,
15
+ isodate,
16
+ }
15
17
 
16
- return dispatch(action('GET', payloadGet, { ident }))
18
+ return dispatch(action('GET', payloadGet, { ident, gid, cid }))
17
19
  }
18
20
 
19
- const deleteExpired = async (response, service, ident, dispatch) => {
21
+ const deleteExpired = async (response, service, ident, dispatch, gid, cid) => {
20
22
  if (response.status !== 'ok' || !Array.isArray(response.data)) {
21
- return createError(`Could not get items from service '${service}'. Reason: ${response.status} ${response.error}`, 'noaction')
23
+ return createError(
24
+ `Could not get items from service '${service}'. Reason: ${response.status} ${response.error}`,
25
+ 'noaction'
26
+ )
22
27
  }
23
28
  if (response.data.length === 0) {
24
- return createError(`No items to expire from service '${service}'`, 'noaction')
29
+ return createError(
30
+ `No items to expire from service '${service}'`,
31
+ 'noaction'
32
+ )
25
33
  }
26
34
 
27
- const data = response.data.map((item) =>
28
- ({ id: item.id, type: item.type }))
35
+ const data = response.data.map((item) => ({ id: item.id, type: item.type }))
29
36
 
30
- return dispatch(action(
31
- 'DELETE',
32
- { service, data },
33
- { queue: true, ident }
34
- ))
37
+ return dispatch(
38
+ action('DELETE', { service, data }, { queue: true, ident, gid, cid })
39
+ )
35
40
  }
36
41
 
37
42
  /**
@@ -49,23 +54,27 @@ const deleteExpired = async (response, service, ident, dispatch) => {
49
54
  * @param {Object} resources - Dispatch and queue functions
50
55
  * @returns {Object} Response object
51
56
  */
52
- async function expire ({ payload, meta = {} }, { dispatch }) {
57
+ async function expire({ payload, meta = {} }, { dispatch }) {
53
58
  const { service } = payload
54
- const { ident } = meta
59
+ const { ident, id, cid } = meta
55
60
 
56
61
  if (!service) {
57
- return createError('Can\'t delete expired without a specified service')
62
+ return createError("Can't delete expired without a specified service")
58
63
  }
59
64
  if (!payload.endpoint) {
60
- return createError(`Can't delete expired from service '${service}' without an endpoint`)
65
+ return createError(
66
+ `Can't delete expired from service '${service}' without an endpoint`
67
+ )
61
68
  }
62
69
  if (!payload.type) {
63
- return createError(`Can't delete expired from service '${service}' without one or more specified types`)
70
+ return createError(
71
+ `Can't delete expired from service '${service}' without one or more specified types`
72
+ )
64
73
  }
65
74
 
66
- const response = await getExpired(payload, ident, dispatch)
75
+ const response = await getExpired(payload, ident, dispatch, id, cid)
67
76
 
68
- return deleteExpired(response, service, ident, dispatch)
77
+ return deleteExpired(response, service, ident, dispatch, id, cid)
69
78
  }
70
79
 
71
80
  module.exports = expire
@@ -50,12 +50,6 @@ function filterResponseData(data, id) {
50
50
  return data.filter((data) => ids.includes(data.id))
51
51
  }
52
52
 
53
- const removeData = ({ type, payload: { data, ...payload }, meta }) => ({
54
- type,
55
- payload,
56
- meta,
57
- })
58
-
59
53
  /**
60
54
  * Get several items from a service, based on the given action object.
61
55
  * @param {Object} action - payload and ident from the action object
@@ -90,7 +84,7 @@ async function get(action, { getService } = {}) {
90
84
  debug('GET: Fetch from service %s at %s', service.id, endpointDebug)
91
85
 
92
86
  const { response } = await service.send(
93
- appendToAction(removeData(action), { id, onlyMappedValues })
87
+ appendToAction(action, { id, onlyMappedValues })
94
88
  )
95
89
 
96
90
  return filterWithId
@@ -6,17 +6,25 @@ const createUnknownServiceError = require('../utils/createUnknownServiceError')
6
6
  const preparePropKeys = ({
7
7
  id = 'id',
8
8
  roles = 'roles',
9
- tokens = 'tokens'
9
+ tokens = 'tokens',
10
10
  } = {}) => ({
11
- id, roles, tokens
11
+ id,
12
+ roles,
13
+ tokens,
12
14
  })
13
15
 
14
16
  const prepareParams = (ident, keys) =>
15
- (ident.id) ? { [keys.id]: ident.id }
16
- : (ident.withToken) ? { [keys.tokens]: ident.withToken }
17
- : null
17
+ ident.id
18
+ ? { [keys.id]: ident.id }
19
+ : ident.withToken
20
+ ? { [keys.tokens]: ident.withToken }
21
+ : null
18
22
 
19
- const wrapOk = (data, ident) => ({ status: 'ok', data, access: { status: 'granted', ident } })
23
+ const wrapOk = (data, ident) => ({
24
+ status: 'ok',
25
+ data,
26
+ access: { status: 'granted', ident },
27
+ })
20
28
 
21
29
  const prepareResponse = (response, params, propKeys) => {
22
30
  const { data } = response
@@ -25,28 +33,34 @@ const prepareResponse = (response, params, propKeys) => {
25
33
  const completeIdent = {
26
34
  id: getField(data[0], propKeys.id),
27
35
  roles: getField(data[0], propKeys.roles),
28
- tokens: getField(data[0], propKeys.tokens)
36
+ tokens: getField(data[0], propKeys.tokens),
29
37
  }
30
38
  return wrapOk(data[0], completeIdent)
31
39
  } else {
32
- return createError(`Could not find ident with params ${util.inspect(params)}`, 'notfound')
40
+ return createError(
41
+ `Could not find ident with params ${util.inspect(params)}`,
42
+ 'notfound'
43
+ )
33
44
  }
34
45
  }
35
46
 
36
47
  /**
37
- * Get an ident item from service, based on the meta.ident object on the action.
38
- * @param {Object} action - Action object
39
- * @param {Object} resources - Object with getService and identOptions
40
- * @returns {Object} Response object with ident item as data
48
+ * Get an ident item from service, based on the meta.ident object on the action.
49
+ * @param {Object} action - Action object
50
+ * @param {Object} resources - Object with getService and identOptions
51
+ * @returns {Object} Response object with ident item as data
41
52
  */
42
- async function getIdent ({ payload, meta }, { getService, identOptions = {} }) {
53
+ async function getIdent({ payload, meta }, { getService, identOptions = {} }) {
43
54
  if (!meta.ident) {
44
55
  return createError('GET_IDENT: The request has no ident', 'noaction')
45
56
  }
46
57
 
47
58
  const { type } = identOptions
48
59
  if (!type) {
49
- return createError('GET_IDENT: Integreat is not set up with authentication', 'noaction')
60
+ return createError(
61
+ 'GET_IDENT: Integreat is not set up with authentication',
62
+ 'noaction'
63
+ )
50
64
  }
51
65
 
52
66
  const service = getService(type)
@@ -57,13 +71,16 @@ async function getIdent ({ payload, meta }, { getService, identOptions = {} }) {
57
71
  const propKeys = preparePropKeys(identOptions.props)
58
72
  const params = prepareParams(meta.ident, propKeys)
59
73
  if (!params) {
60
- return createError('GET_IDENT: The request has no ident with id or withToken', 'noaction')
74
+ return createError(
75
+ 'GET_IDENT: The request has no ident with id or withToken',
76
+ 'noaction'
77
+ )
61
78
  }
62
79
 
63
80
  const { response } = await service.send({
64
81
  type: 'GET',
65
82
  payload: { type, ...params },
66
- meta: { ident: { root: true } }
83
+ meta: { ident: { root: true } },
67
84
  })
68
85
 
69
86
  return prepareResponse(response, payload, propKeys)
@@ -5,8 +5,9 @@ const appendToAction = require('../utils/appendToAction')
5
5
  const { mergeDeepWith } = require('ramda')
6
6
 
7
7
  const mergeDiff = (left, right) =>
8
- (is.undefined(right) || (is.emptyArray(right) && is.nonEmptyArray(left)))
9
- ? left : right
8
+ is.undefined(right) || (is.emptyArray(right) && is.nonEmptyArray(left))
9
+ ? left
10
+ : right
10
11
 
11
12
  const merge = (requestData, responseData) => {
12
13
  requestData = [].concat(requestData)
@@ -15,16 +16,20 @@ const merge = (requestData, responseData) => {
15
16
  }
16
17
  responseData = [].concat(responseData)
17
18
 
18
- return requestData.map(
19
- (data, index) => (data) ? mergeDeepWith(mergeDiff, data, responseData[index]) : responseData[index]
19
+ return requestData.map((data, index) =>
20
+ data
21
+ ? mergeDeepWith(mergeDiff, data, responseData[index])
22
+ : responseData[index]
20
23
  )
21
24
  }
22
25
 
23
- const mergeRequestAndResponseData = (response, requestData) => (response.status === 'ok')
24
- ? { ...response, data: merge(requestData, response.data) }
25
- : response
26
+ const mergeRequestAndResponseData = (response, requestData) =>
27
+ response.status === 'ok'
28
+ ? { ...response, data: merge(requestData, response.data) }
29
+ : response
26
30
 
27
- const extractType = (action, data) => action.payload.type || (data && data.type) || undefined
31
+ const extractType = (action, data) =>
32
+ action.payload.type || (data && data.type) || undefined
28
33
 
29
34
  const extractId = (data) => (data && data.id) || undefined
30
35
 
@@ -34,14 +39,14 @@ const extractId = (data) => (data && data.id) || undefined
34
39
  * @param {Object} resources - Object with getService
35
40
  * @returns {Object} Response object with any data returned from the service
36
41
  */
37
- async function set (action, { getService, schemas }) {
42
+ async function set(action, { getService }) {
38
43
  debug('Action: SET')
39
44
 
40
45
  const {
41
46
  service: serviceId,
42
47
  data,
43
48
  endpoint,
44
- onlyMappedValues = true
49
+ onlyMappedValues = true,
45
50
  } = action.payload
46
51
  const type = extractType(action, data)
47
52
  const id = extractId(data)
@@ -51,14 +56,11 @@ async function set (action, { getService, schemas }) {
51
56
  return createUnknownServiceError(type, serviceId, 'SET')
52
57
  }
53
58
 
54
- const endpointDebug = (endpoint) ? `at endpoint '${endpoint}'` : ''
59
+ const endpointDebug = endpoint ? `at endpoint '${endpoint}'` : ''
55
60
  debug('SET: Send to service %s %s', service.id, endpointDebug)
56
61
 
57
- const {
58
- response,
59
- authorizedRequestData,
60
- mapResponseWithType
61
- } = await service.send(appendToAction(action, { id, type, onlyMappedValues }))
62
+ const { response, authorizedRequestData, mapResponseWithType } =
63
+ await service.send(appendToAction(action, { id, type, onlyMappedValues }))
62
64
 
63
65
  return mapResponseWithType
64
66
  ? mergeRequestAndResponseData(response, authorizedRequestData)
@@ -7,8 +7,8 @@ const createError = require('../utils/createError')
7
7
  const createTransformAction = (
8
8
  { action: type, ...payload },
9
9
  data,
10
- { ident, project }
11
- ) => action(type, { ...payload, data }, { ident, project })
10
+ { ident, project, id: gid, cid }
11
+ ) => action(type, { ...payload, data }, { ident, project, gid, cid })
12
12
 
13
13
  const makeErrorString = (results) =>
14
14
  results
@@ -27,13 +27,20 @@ const generateUpdatedAfter = (updatedAfter, afterDelta) => ({
27
27
  })
28
28
 
29
29
  const setUpdatedParams =
30
- (dispatch, dontSetUntil, afterDelta, untilDelta, metaKey, ident) =>
30
+ (
31
+ dispatch,
32
+ dontSetUntil,
33
+ afterDelta,
34
+ untilDelta,
35
+ metaKey,
36
+ { ident, id: gid, cid }
37
+ ) =>
31
38
  async (params) => {
32
39
  const { status, data, error } = await dispatch(
33
40
  action(
34
41
  'GET_META',
35
42
  { service: params.service, metaKey, keys: 'lastSyncedAt' },
36
- { ident }
43
+ { ident, gid, cid }
37
44
  )
38
45
  )
39
46
 
@@ -59,7 +66,7 @@ const setUpdatedParams =
59
66
  const generateParamsWithUpdatedDates = async (
60
67
  params,
61
68
  dispatch,
62
- ident,
69
+ meta,
63
70
  updatedAfter,
64
71
  updatedUntil,
65
72
  afterDelta,
@@ -87,7 +94,7 @@ const generateParamsWithUpdatedDates = async (
87
94
  afterDelta,
88
95
  untilDelta,
89
96
  metaKey,
90
- ident
97
+ meta
91
98
  )
92
99
  )
93
100
  )
@@ -107,7 +114,7 @@ const generateFromParams = async (
107
114
  untilDelta,
108
115
  metaKey,
109
116
  },
110
- { ident },
117
+ meta,
111
118
  dispatch
112
119
  ) => {
113
120
  const fromParams = [].concat(from).map(paramsFromStringOrObject)
@@ -115,7 +122,7 @@ const generateFromParams = async (
115
122
  return generateParamsWithUpdatedDates(
116
123
  fromParams,
117
124
  dispatch,
118
- ident,
125
+ meta,
119
126
  updatedAfter,
120
127
  updatedUntil,
121
128
  afterDelta,
@@ -167,10 +174,10 @@ const isWithinUpdateWindow = (updatedAfter, updatedUntil) => (item) =>
167
174
  (!updatedUntil || item.attributes.updatedAt <= updatedUntil)
168
175
 
169
176
  const getFromService =
170
- (dispatch, type, noFilter = false, { project, ident }) =>
177
+ (dispatch, type, noFilter = false, { project, ident, id: gid, cid }) =>
171
178
  async ({ action: fromAction = 'GET', ...fromParams }) => {
172
179
  const response = await dispatch(
173
- action(fromAction, { type, ...fromParams }, { project, ident })
180
+ action(fromAction, { type, ...fromParams }, { project, ident, gid, cid })
174
181
  )
175
182
  if (response.status !== 'ok') {
176
183
  return createError(
@@ -199,7 +206,7 @@ const createSetMetas = (
199
206
  fromParams,
200
207
  lastSyncedAt,
201
208
  metaKey,
202
- ident,
209
+ { ident, id: gid, cid },
203
210
  dispatch,
204
211
  getService
205
212
  ) =>
@@ -218,24 +225,32 @@ const createSetMetas = (
218
225
  action(
219
226
  'SET_META',
220
227
  { service, metaKey, meta: { lastSyncedAt } },
221
- { ident }
228
+ { ident, gid, cid }
222
229
  )
223
230
  )
224
231
  )
225
232
 
226
- const createSetActions = (toAction, data, toParams, meta, useIndividualSet) =>
233
+ const createSetActions = (
234
+ toAction,
235
+ data,
236
+ toParams,
237
+ { id: gid, cid, ...meta },
227
238
  useIndividualSet
228
- ? data.map((item) => action(toAction, { data: item, ...toParams }, meta))
229
- : [action(toAction, { data, ...toParams }, meta)]
239
+ ) =>
240
+ useIndividualSet
241
+ ? data.map((item) =>
242
+ action(toAction, { data: item, ...toParams }, { ...meta, gid, cid })
243
+ )
244
+ : [action(toAction, { data, ...toParams }, { ...meta, gid, cid })]
230
245
 
231
246
  const createDoneAction = (
232
247
  data,
233
248
  { action: type = 'SET', ...params },
234
- { ident, project }
249
+ { ident, project, id: gid, cid }
235
250
  ) => ({
236
251
  type,
237
252
  payload: { ...params, data },
238
- meta: { ident, project },
253
+ meta: { ident, project, gid, cid },
239
254
  })
240
255
 
241
256
  const isError = (response) =>
@@ -381,7 +396,7 @@ async function sync({ payload, meta = {} }, { dispatch, getService }) {
381
396
  fromParams,
382
397
  lastSyncedAt,
383
398
  metaKey,
384
- meta.ident,
399
+ meta,
385
400
  dispatch,
386
401
  getService
387
402
  )
package/lib/dispatch.js CHANGED
@@ -1,3 +1,4 @@
1
+ const { nanoid } = require('nanoid')
1
2
  const debug = require('debug')('great')
2
3
  const PProgress = require('p-progress')
3
4
  const setupGetService = require('./utils/getService')
@@ -22,6 +23,17 @@ const handleAction = (action, resources, actionHandlers) => {
22
23
  return PProgress.resolve({ status: 'noaction' })
23
24
  }
24
25
 
26
+ function setIds(action) {
27
+ if (!action) {
28
+ return action
29
+ }
30
+ const meta = action.meta || {}
31
+ const id = meta.id || nanoid()
32
+ const cid = meta.cid || id
33
+ const gid = meta.gid || id
34
+ return { ...action, meta: { ...meta, id, cid, gid } }
35
+ }
36
+
25
37
  /**
26
38
  * Setup and return dispatch function. The dispatch function will call the
27
39
  * relevant action handler.
@@ -60,7 +72,7 @@ function setupDispatch({
60
72
  },
61
73
  actionHandlers
62
74
  )
63
- )(action)
75
+ )(setIds(action))
64
76
  )
65
77
  } catch (err) {
66
78
  reject(err)
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.41'
8
+ const version = '0.7.43'
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.41",
3
+ "version": "0.7.43",
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,11 +46,12 @@
46
46
  },
47
47
  "dependencies": {
48
48
  "@sindresorhus/is": "^1.2.0",
49
- "debug": "^4.3.4",
49
+ "debug": "^4.3.7",
50
50
  "got": "^9.6.0",
51
51
  "later": "^1.2.0",
52
52
  "map-any": "^0.2.1",
53
53
  "map-transform": "^0.3.12",
54
+ "nanoid": "^3.3.7",
54
55
  "p-limit": "^2.3.0",
55
56
  "p-progress": "^0.5.1",
56
57
  "ramda": "^0.27.2",
@@ -62,9 +63,9 @@
62
63
  "coveralls": "^3.1.1",
63
64
  "dotenv": "^10.0.0",
64
65
  "integreat-adapter-json": "^0.2.1",
65
- "nock": "^13.2.9",
66
+ "nock": "^13.5.6",
66
67
  "nyc": "^15.1.0",
67
- "prettier": "^2.8.1",
68
+ "prettier": "^2.8.8",
68
69
  "sinon": "^11.1.2"
69
70
  }
70
71
  }