-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers.js
62 lines (52 loc) · 1.39 KB
/
helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const get = require('lodash.get');
const isObject = (o) => o && typeof o === 'object';
const hasProperty = (target, prop) =>
isObject(target) && prop in target;
const getProperty = (target, props) =>
props.reduce(
(curr, next) =>
isObject(target) && next in target
? target[next]
: curr,
target,
);
exports.invoke = (methodName, payload) => (client) => {
if (
!hasProperty(client, methodName) ||
typeof client[methodName] !== 'function'
)
throw new Error(
'Method not available through this adapter',
);
const fn = client[methodName];
return fn(payload);
};
exports.prefix = (obj = {}, frontmatter = '') => {
const reassignKeyValue = (input) =>
Object.entries(input).reduce(
(output, [key, value]) =>
Object.assign(output, {
[`${frontmatter}:${key}`]:
typeof value === 'object'
? reassignKeyValue(value)
: value,
}),
{},
);
return reassignKeyValue(obj);
};
exports.getError = (e) =>
Promise.reject(
getProperty(e, ['Error', 'Fault', 'Body', 'message']),
);
exports.getResponse = (responseKey) => ([response]) => {
if (
hasProperty(
response,
'ResponseInformation' &&
hasProperty(response.ResponseInformation, 'Errors'),
)
)
throw response.ResponseInformation.Errors;
return get(response, responseKey, response);
};