integreat 0.7.38 → 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.
package/index.d.ts CHANGED
@@ -25,7 +25,7 @@ declare namespace integreat {
25
25
  }
26
26
 
27
27
  export interface Meta {
28
- queue?: boolean
28
+ queue?: boolean | number
29
29
  ident?: Ident
30
30
  [key: string]: unknown
31
31
  }
@@ -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
 
@@ -267,21 +267,15 @@ async function transformData(
267
267
  useIndividualTransform
268
268
  ) {
269
269
  if (useIndividualTransform) {
270
- const responses = await Promise.all(
270
+ return await Promise.all(
271
271
  data
272
272
  .map((item) =>
273
273
  dispatch(createTransformAction(transformParams, item, meta))
274
274
  )
275
275
  .map((p) => pLimit(1)(() => p))
276
276
  )
277
- return responses.flatMap((response, index) =>
278
- response.status === 'ok' ? response.data : data[index]
279
- )
280
277
  } else {
281
- const transformResponse = await dispatch(
282
- createTransformAction(transformParams, data, meta)
283
- )
284
- return transformResponse.status === 'ok' ? transformResponse.data : data
278
+ return [await dispatch(createTransformAction(transformParams, data, meta))]
285
279
  }
286
280
  }
287
281
 
@@ -344,7 +338,7 @@ async function sync({ payload, meta = {} }, { dispatch, getService }) {
344
338
 
345
339
  // Dispatch transform action
346
340
  if (transformParams) {
347
- data = await transformData(
341
+ const transformResults = await transformData(
348
342
  data,
349
343
  dispatch,
350
344
  transformParams,
@@ -352,6 +346,16 @@ async function sync({ payload, meta = {} }, { dispatch, getService }) {
352
346
  payload.useIndividualTransform
353
347
  )
354
348
 
349
+ if (transformResults.some((result) => result.status !== 'ok')) {
350
+ return transformResults.length === 1
351
+ ? transformResults[0]
352
+ : createError(makeErrorString(transformResults))
353
+ }
354
+
355
+ data = flatten(transformResults.map((result) => result.data)).filter(
356
+ Boolean
357
+ )
358
+
355
359
  if (data.length === 0 && payload.syncNoData !== true) {
356
360
  return createError(
357
361
  `No items to update from service '${fromParams[0].service}' after transform action`,
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.38'
8
+ const version = '0.7.40'
9
9
 
10
10
  /**
11
11
  * Return an Integreat instance with a dispatch method.
@@ -2,8 +2,8 @@ const is = require('@sindresorhus/is')
2
2
 
3
3
  const generateErrorResponse = (status, error, access, authError) => ({
4
4
  status,
5
- error: (authError) ? `${error}: ${authError}` : error,
6
- access: { ...access, status: 'refused', scheme: 'service' }
5
+ error: authError ? `${error}: ${authError}` : error,
6
+ access: { ...access, status: 'refused', scheme: 'service' },
7
7
  })
8
8
 
9
9
  const responseFromAuthentication = (authentication, response, { access }) => {
@@ -11,18 +11,38 @@ const responseFromAuthentication = (authentication, response, { access }) => {
11
11
  case 'granted':
12
12
  return undefined
13
13
  case 'refused':
14
- return generateErrorResponse('noaccess', 'Authentication was refused', access)
14
+ return generateErrorResponse(
15
+ 'noaccess',
16
+ 'Authentication was refused',
17
+ access
18
+ )
15
19
  case 'error':
16
20
  case 'timeout':
17
- return generateErrorResponse('autherror', 'Could not authenticate', access, authentication.error)
21
+ return generateErrorResponse(
22
+ 'autherror',
23
+ 'Could not authenticate',
24
+ access,
25
+ authentication.error
26
+ )
18
27
  }
19
28
 
20
- return generateErrorResponse('autherror', 'Could not authenticate - unknown status from authenticator', access)
29
+ return generateErrorResponse(
30
+ 'autherror',
31
+ 'Could not authenticate - unknown status from authenticator',
32
+ access
33
+ )
21
34
  }
22
35
 
23
- const requestFromAuthentication = (authentication, authenticator, adapter, request) => {
36
+ const requestFromAuthentication = (
37
+ authentication,
38
+ authenticator,
39
+ adapter,
40
+ request,
41
+ authOptions
42
+ ) => {
24
43
  if (authentication.status === 'granted') {
25
- const fn = authenticator[adapter.authentication]
44
+ const method = authOptions.overrideAdapterMethod || adapter.authentication
45
+ const fn = authenticator[method]
26
46
  if (is.function(fn)) {
27
47
  const auth = fn(authentication)
28
48
  return { ...request, auth }
@@ -32,7 +52,12 @@ const requestFromAuthentication = (authentication, authenticator, adapter, reque
32
52
  return { ...request, auth: null }
33
53
  }
34
54
 
35
- const doAuthenticate = async (authenticator, authOptions, request, retries = 1) => {
55
+ const doAuthenticate = async (
56
+ authenticator,
57
+ authOptions,
58
+ request,
59
+ retries = 1
60
+ ) => {
36
61
  const authentication = await authenticator.authenticate(authOptions, request)
37
62
 
38
63
  if (authentication.status === 'timeout' && retries > 0) {
@@ -42,28 +67,53 @@ const doAuthenticate = async (authenticator, authOptions, request, retries = 1)
42
67
  return authentication
43
68
  }
44
69
 
45
- function authenticate ({ authenticator, authOptions, setAuthentication = () => {}, adapter }) {
46
- return (authenticator && authenticator.authenticate)
70
+ function authenticate({
71
+ authenticator,
72
+ authOptions,
73
+ setAuthentication = () => {},
74
+ adapter,
75
+ }) {
76
+ return authenticator && authenticator.authenticate
47
77
  ? async (args) => {
48
- const { authentication, response, request } = args
78
+ const { authentication, response, request } = args
49
79
 
50
- if (authentication && authenticator.isAuthenticated(authentication)) {
51
- return {
52
- ...args,
53
- request: requestFromAuthentication(authentication, authenticator, adapter, request)
80
+ if (authentication && authenticator.isAuthenticated(authentication)) {
81
+ return {
82
+ ...args,
83
+ request: requestFromAuthentication(
84
+ authentication,
85
+ authenticator,
86
+ adapter,
87
+ request,
88
+ authOptions
89
+ ),
90
+ }
54
91
  }
55
- }
56
92
 
57
- const nextAuthentication = await doAuthenticate(authenticator, authOptions, request)
58
- setAuthentication(nextAuthentication)
93
+ const nextAuthentication = await doAuthenticate(
94
+ authenticator,
95
+ authOptions,
96
+ request
97
+ )
98
+ setAuthentication(nextAuthentication)
59
99
 
60
- return {
61
- ...args,
62
- authentication: nextAuthentication,
63
- response: responseFromAuthentication(nextAuthentication, response, request),
64
- request: requestFromAuthentication(nextAuthentication, authenticator, adapter, request)
100
+ return {
101
+ ...args,
102
+ authentication: nextAuthentication,
103
+ response: responseFromAuthentication(
104
+ nextAuthentication,
105
+ response,
106
+ request
107
+ ),
108
+ request: requestFromAuthentication(
109
+ nextAuthentication,
110
+ authenticator,
111
+ adapter,
112
+ request,
113
+ authOptions
114
+ ),
115
+ }
65
116
  }
66
- }
67
117
  : async (args) => args
68
118
  }
69
119
 
@@ -6,9 +6,9 @@ const getPluralType = (type, schemas) =>
6
6
  * @param {Object} request - The request object to complete
7
7
  * @returns {Object} The completed request object
8
8
  */
9
- function requestFromAction (
9
+ function requestFromAction(
10
10
  { type: action, payload, meta = {} },
11
- { endpoint, schemas = {} } = {}
11
+ { endpoint, schemas = {}, auth } = {}
12
12
  ) {
13
13
  const { data, ...params } = payload
14
14
  const { ident = null, id } = meta
@@ -19,11 +19,12 @@ function requestFromAction (
19
19
  params,
20
20
  data,
21
21
  endpoint: (endpoint && endpoint.options) || null,
22
+ auth,
22
23
  access: { ident },
23
24
  meta: {
24
25
  id,
25
- typePlural
26
- }
26
+ typePlural,
27
+ },
27
28
  }
28
29
  }
29
30
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "integreat",
3
- "version": "0.7.38",
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,14 +46,14 @@
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",
53
53
  "map-transform": "^0.3.12",
54
54
  "p-limit": "^2.3.0",
55
55
  "p-progress": "^0.5.1",
56
- "ramda": "^0.27.1",
56
+ "ramda": "^0.27.2",
57
57
  "uuid": "^3.4.0"
58
58
  },
59
59
  "devDependencies": {
@@ -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.1",
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
  }