integreat 0.7.42 → 0.7.44
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.
- package/index.d.ts +4 -0
- package/lib/actions/expire.js +35 -26
- package/lib/actions/getIdent.js +33 -16
- package/lib/actions/set.js +18 -16
- package/lib/actions/sync.js +33 -18
- package/lib/dispatch.js +13 -1
- package/lib/integreat.js +1 -1
- package/package.json +5 -4
package/index.d.ts
CHANGED
package/lib/actions/expire.js
CHANGED
|
@@ -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 = {
|
|
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(
|
|
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(
|
|
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(
|
|
31
|
-
'DELETE',
|
|
32
|
-
|
|
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
|
|
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(
|
|
62
|
+
return createError("Can't delete expired without a specified service")
|
|
58
63
|
}
|
|
59
64
|
if (!payload.endpoint) {
|
|
60
|
-
return createError(
|
|
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(
|
|
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
|
package/lib/actions/getIdent.js
CHANGED
|
@@ -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,
|
|
11
|
+
id,
|
|
12
|
+
roles,
|
|
13
|
+
tokens,
|
|
12
14
|
})
|
|
13
15
|
|
|
14
16
|
const prepareParams = (ident, keys) =>
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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) => ({
|
|
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(
|
|
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
|
|
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(
|
|
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(
|
|
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)
|
package/lib/actions/set.js
CHANGED
|
@@ -5,8 +5,9 @@ const appendToAction = require('../utils/appendToAction')
|
|
|
5
5
|
const { mergeDeepWith } = require('ramda')
|
|
6
6
|
|
|
7
7
|
const mergeDiff = (left, right) =>
|
|
8
|
-
|
|
9
|
-
? left
|
|
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
|
-
|
|
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) =>
|
|
24
|
-
|
|
25
|
-
|
|
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) =>
|
|
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
|
|
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 =
|
|
59
|
+
const endpointDebug = endpoint ? `at endpoint '${endpoint}'` : ''
|
|
55
60
|
debug('SET: Send to service %s %s', service.id, endpointDebug)
|
|
56
61
|
|
|
57
|
-
const {
|
|
58
|
-
|
|
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)
|
package/lib/actions/sync.js
CHANGED
|
@@ -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
|
-
(
|
|
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
|
-
|
|
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
|
-
|
|
97
|
+
meta
|
|
91
98
|
)
|
|
92
99
|
)
|
|
93
100
|
)
|
|
@@ -107,7 +114,7 @@ const generateFromParams = async (
|
|
|
107
114
|
untilDelta,
|
|
108
115
|
metaKey,
|
|
109
116
|
},
|
|
110
|
-
|
|
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
|
-
|
|
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 = (
|
|
233
|
+
const createSetActions = (
|
|
234
|
+
toAction,
|
|
235
|
+
data,
|
|
236
|
+
toParams,
|
|
237
|
+
{ id: gid, cid, ...meta },
|
|
227
238
|
useIndividualSet
|
|
228
|
-
|
|
229
|
-
|
|
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
|
|
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
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "integreat",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.44",
|
|
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.
|
|
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.
|
|
66
|
+
"nock": "^13.5.6",
|
|
66
67
|
"nyc": "^15.1.0",
|
|
67
|
-
"prettier": "^2.8.
|
|
68
|
+
"prettier": "^2.8.8",
|
|
68
69
|
"sinon": "^11.1.2"
|
|
69
70
|
}
|
|
70
71
|
}
|