integreat 0.7.38 → 0.7.39

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
  }
@@ -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.39'
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.39",
4
4
  "description": "Node.js integration layer",
5
5
  "author": "Kjell-Morten Bratsberg Thorsen <post@kjellmorten.no> (http://kjellmorten.no/)",
6
6
  "license": "ISC",
@@ -53,7 +53,7 @@
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,7 +62,7 @@
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.4",
66
66
  "nyc": "^15.1.0",
67
67
  "prettier": "^2.5.1",
68
68
  "sinon": "^11.1.2"