Skip to content

Commit c5f4f2d

Browse files
committed
Call extensions modules using one parameter.
1 parent 918f248 commit c5f4f2d

21 files changed

+211
-140
lines changed

src/__mocks__/containerInitFunction.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@ module.exports = (getDataElementValues) => ({
1515
'adobe-cloud-connector/src/lib/actions/sendData.js': {
1616
extensionName: 'adobe-cloud-connector',
1717
displayName: 'Send Beacon',
18-
script: (settings, _, { fetch }) =>
19-
fetch(settings.url).then(() => 'send data done')
18+
script: ({ utils: { getSettings, fetch } }) =>
19+
fetch(getSettings().url).then(() => 'send data done')
2020
},
2121
'core/src/lib/dataElements/customCode.js': {
2222
extensionName: 'core',
2323
displayName: 'Custom Code Data Element',
24-
script: (settings) => settings.source()
24+
script: ({ utils: { getSettings } }) => getSettings().source()
2525
},
2626
'core/src/lib/conditions/customCode.js': {
2727
extensionName: 'core',
2828
displayName: 'Custom Code Condition',
29-
script: (settings) => settings.source()
29+
script: ({ utils: { getSettings } }) => getSettings().source()
3030
}
3131
},
3232
dataElements: {

src/__tests__/createGetDataElementValue.test.js

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ const createGetDataElementValueModule = require('../createGetDataElementValue');
1313

1414
jest.mock('../cleanText.js');
1515

16+
const defaultContext = { arcAndUtils: {} };
17+
1618
const createGetDataElementDefinitionDefault = (dataDef) =>
1719
jest.fn().mockImplementation((dataElementName) => {
1820
if (dataElementName === 'testDataElement') {
@@ -29,7 +31,7 @@ const createGetDataElementValue = (
2931
delegateDefinition,
3032
{
3133
moduleProvider = {
32-
getModuleExports: () => (settings) => settings.foo
34+
getModuleExports: () => (context) => context.utils.getSettings().foo
3335
},
3436
createGetDataElementDefinition = createGetDataElementDefinitionDefault
3537
} = {}
@@ -52,20 +54,23 @@ describe('function returned by createGetDataElementValue', () => {
5254
}
5355
});
5456

55-
return getDataElementValue('testDataElement', {}).then((dataElementValue) =>
56-
expect(dataElementValue).toBe('bar')
57-
);
57+
return getDataElementValue(
58+
'testDataElement',
59+
defaultContext
60+
).then((dataElementValue) => expect(dataElementValue).toBe('bar'));
5861
});
5962

6063
test('returns a value from the contextData object as value', () => {
6164
const context = {
62-
contextData: {
63-
foo: 'bar'
65+
arcAndUtils: {
66+
arc: {
67+
foo: 'bar'
68+
}
6469
}
6570
};
6671

6772
const moduleProvider = {
68-
getModuleExports: () => (_, contextData) => contextData.foo
73+
getModuleExports: () => (c) => c.arc.foo
6974
};
7075

7176
const getDataElementValue = createGetDataElementValue(
@@ -85,9 +90,10 @@ describe('function returned by createGetDataElementValue', () => {
8590
settings: { foo: 'bar' }
8691
});
8792

88-
return getDataElementValue('testDataElement', {}).then((dataElementValue) =>
89-
expect(dataElementValue).toBe('cleaned:bar')
90-
);
93+
return getDataElementValue(
94+
'testDataElement',
95+
defaultContext
96+
).then((dataElementValue) => expect(dataElementValue).toBe('cleaned:bar'));
9197
});
9298

9399
[undefined, null].forEach((testDataElementValue) => {
@@ -106,7 +112,7 @@ describe('function returned by createGetDataElementValue', () => {
106112

107113
return getDataElementValue(
108114
'testDataElement',
109-
{}
115+
defaultContext
110116
).then((dataElementValue) =>
111117
expect(dataElementValue).toBe('defaultValue')
112118
);
@@ -123,7 +129,7 @@ describe('function returned by createGetDataElementValue', () => {
123129

124130
return getDataElementValue(
125131
'testDataElement',
126-
{}
132+
defaultContext
127133
).then((dataElementValue) =>
128134
expect(dataElementValue).toBe(testDataElementValue)
129135
);
@@ -146,7 +152,7 @@ describe('function returned by createGetDataElementValue', () => {
146152

147153
return getDataElementValue(
148154
'testDataElement',
149-
{}
155+
defaultContext
150156
).then((dataElementValue) =>
151157
expect(dataElementValue).toBe(testDataElementValue)
152158
);
@@ -161,9 +167,10 @@ describe('function returned by createGetDataElementValue', () => {
161167
}
162168
});
163169

164-
return getDataElementValue('testDataElement', {}).then((dataElementValue) =>
165-
expect(dataElementValue).toBe('bar')
166-
);
170+
return getDataElementValue(
171+
'testDataElement',
172+
defaultContext
173+
).then((dataElementValue) => expect(dataElementValue).toBe('bar'));
167174
});
168175

169176
test('lowercases the default value if forceLowerCase = true', () => {
@@ -173,9 +180,10 @@ describe('function returned by createGetDataElementValue', () => {
173180
settings: {}
174181
});
175182

176-
return getDataElementValue('testDataElement', {}).then((dataElementValue) =>
177-
expect(dataElementValue).toBe('bar')
178-
);
183+
return getDataElementValue(
184+
'testDataElement',
185+
defaultContext
186+
).then((dataElementValue) => expect(dataElementValue).toBe('bar'));
179187
});
180188

181189
test('throws an error when calling data element module exports fails', () => {
@@ -193,7 +201,7 @@ describe('function returned by createGetDataElementValue', () => {
193201
{ moduleProvider }
194202
);
195203

196-
return getDataElementValue('testDataElement', {}).catch((e) => {
204+
return getDataElementValue('testDataElement', defaultContext).catch((e) => {
197205
expect(e.message).toMatch(
198206
'Failed to execute module for data element "testDataElement". noob tried to divide by zero'
199207
);
@@ -214,7 +222,7 @@ describe('function returned by createGetDataElementValue', () => {
214222
{ moduleProvider }
215223
);
216224

217-
return getDataElementValue('testDataElement', {})
225+
return getDataElementValue('testDataElement', defaultContext)
218226
.then(() => {
219227
throw new Error('This section should not have been called.');
220228
})
@@ -234,7 +242,7 @@ noob tried to divide by zero'
234242
{ createGetDataElementDefinition: () => () => {} }
235243
);
236244

237-
return getDataElementValue('testDataElement', {})
245+
return getDataElementValue('testDataElement', defaultContext)
238246
.then(() => {
239247
throw new Error('This section should not have been called.');
240248
})
@@ -253,7 +261,8 @@ noob tried to divide by zero'
253261
});
254262

255263
return getDataElementValue('testDataElement', {
256-
dataElementCallStack: ['testDataElement']
264+
dataElementCallStack: ['testDataElement'],
265+
arcAndUtils: {}
257266
})
258267
.then(() => {
259268
throw new Error('This section should not have been called.');

src/__tests__/index.test.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ jest.mock('../createNewLogger.js');
2626

2727
describe('index', () => {
2828
test('executes rules conditions and actions and returns a JSON result', () => {
29-
const callData = { header: {}, body: { xdm: {} } };
29+
const callData = {
30+
event: { xdm: {}, data: {} },
31+
request: { header: {}, body: { xdm: {}, data: {} } }
32+
};
3033

3134
const execute = index.initialize(containerInitFunction, {
3235
fetch: globalFetch
@@ -44,11 +47,10 @@ describe('index', () => {
4447
['Rule "Rule 1" is being executed.'],
4548
[
4649
'Calling "Custom Code Condition" module from the "Core" extension.',
47-
'Input: ',
48-
'{"buildInfo":{},"rule":{"id":"RLbb1d94c79fee4733a510564a86ba3c59","name":"Rule 1",\
49-
"conditions":[{"modulePath":"core/src/lib/conditions/customCode.js","timeout":100}],\
50-
"actions":[{"modulePath":"adobe-cloud-connector/src/lib/actions/sendData.js","timeout":100}]\
51-
},"ruleStash":{},"header":{},"body":{"xdm":{}}}'
50+
'Event: ',
51+
'{"xdm":{},"data":{}}',
52+
'Rule Stash: ',
53+
'{}'
5254
],
5355
[
5456
'"Custom Code Condition" module from the "Core" extension returned.',
@@ -57,11 +59,10 @@ describe('index', () => {
5759
],
5860
[
5961
'Calling "Send Beacon" module from the "Adobe Cloud Connector" extension.',
60-
'Input: ',
61-
'{"buildInfo":{},"rule":{"id":"RLbb1d94c79fee4733a510564a86ba3c59","name":"Rule 1",\
62-
"conditions":[{"modulePath":"core/src/lib/conditions/customCode.js","timeout":100}],\
63-
"actions":[{"modulePath":"adobe-cloud-connector/src/lib/actions/sendData.js","timeout":100}]\
64-
},"ruleStash":{},"header":{},"body":{"xdm":{}},"extensionSettings":{}}'
62+
'Event: ',
63+
'{"xdm":{},"data":{}}',
64+
'Rule Stash: ',
65+
'{}'
6566
],
6667
[
6768
'FETCH',
@@ -88,10 +89,10 @@ describe('index', () => {
8889
['Rule "Rule 2" is being executed.'],
8990
[
9091
'Calling "Send Beacon" module from the "Adobe Cloud Connector" extension.',
91-
'Input: ',
92-
'{"buildInfo":{},"rule":{"id":"RLbb1d94c79fee4733a510564a86ba3c60","name":"Rule 2",\
93-
"actions":[{"modulePath":"adobe-cloud-connector/src/lib/actions/sendData.js","timeout":100}]\
94-
},"ruleStash":{},"header":{},"body":{"xdm":{}}}'
92+
'Event: ',
93+
'{"xdm":{},"data":{}}',
94+
'Rule Stash: ',
95+
'{}'
9596
],
9697
[
9798
'FETCH',

src/createGetDataElementValue.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ module.exports = (moduleProvider, getDataElementDefinition) => (
1919
name,
2020
context
2121
) => {
22-
const { dataElementCallStack = [], utils, contextData } = context;
22+
const { dataElementCallStack = [], arcAndUtils } = context;
23+
const { utils } = arcAndUtils;
2324

2425
const dataDef = getDataElementDefinition(name);
2526
if (!dataDef) {
@@ -45,7 +46,10 @@ module.exports = (moduleProvider, getDataElementDefinition) => (
4546

4647
const valuePromise = dataDef.getSettings(context).then((settings) => {
4748
try {
48-
return moduleExports(settings, contextData, utils);
49+
return moduleExports({
50+
...arcAndUtils,
51+
utils: { ...utils, getSettings: () => settings }
52+
});
4953
} catch (e) {
5054
enhanceErrorMessage(name, e);
5155
throw e;

src/executeRules.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,25 @@ module.exports = (
4343
const fetch = getRuleFetchFn(globalFetch, headersForSubrequests, logger);
4444

4545
const utils = {
46+
getRule: () => rule,
47+
getBuildInfo: () => buildInfo,
4648
logger,
4749
fetch
4850
};
4951

50-
const initialRuleContextData = {
51-
buildInfo,
52-
rule,
53-
ruleStash: {},
54-
...JSON.parse(freezedInitialCallData)
52+
const initialContext = {
53+
arcAndUtils: {
54+
utils,
55+
arc: {
56+
ruleStash: {},
57+
...JSON.parse(freezedInitialCallData)
58+
}
59+
}
5560
};
5661

57-
let lastPromiseInQueue = Promise.resolve(initialRuleContextData);
62+
let lastPromiseInQueue = Promise.resolve(initialContext);
5863

59-
lastPromiseInQueue = lastPromiseInQueue.then(logRuleStarting(logger));
64+
lastPromiseInQueue = lastPromiseInQueue.then(logRuleStarting);
6065

6166
if (rule.conditions) {
6267
rule.conditions.forEach((condition) => {

src/rules/__tests__/addActionResultToStash.test.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const addActionResultToStash = require('../addActionResultToStash');
1313

1414
const generateContext = (obj) => ({
1515
moduleOutput: 'actionResult',
16-
contextData: { ruleStash: {} },
16+
arcAndUtils: { arc: { ruleStash: {} } },
1717
delegateConfig: {
1818
extension: { name: 'extensionName' }
1919
},
@@ -23,7 +23,9 @@ const generateContext = (obj) => ({
2323
describe('addActionResultToStash', () => {
2424
test('adds the action result to the rule stash', () => {
2525
const ruleStash = {};
26-
addActionResultToStash(generateContext({ contextData: { ruleStash } }));
26+
addActionResultToStash(
27+
generateContext({ arcAndUtils: { arc: { ruleStash } } })
28+
);
2729
expect(ruleStash.extensionName).toBe('actionResult');
2830
});
2931

@@ -32,7 +34,7 @@ describe('addActionResultToStash', () => {
3234
addActionResultToStash(
3335
generateContext({
3436
moduleOutput: undefined,
35-
contextData: { ruleStash }
37+
arcAndUtils: { arc: { ruleStash } }
3638
})
3739
);
3840

@@ -45,7 +47,7 @@ describe('addActionResultToStash', () => {
4547
addActionResultToStash(
4648
generateContext({
4749
moduleOutput: '',
48-
contextData: { ruleStash }
50+
arcAndUtils: { arc: { ruleStash } }
4951
})
5052
);
5153

@@ -57,7 +59,7 @@ describe('addActionResultToStash', () => {
5759
addActionResultToStash(
5860
generateContext({
5961
moduleOutput: '',
60-
contextData: { ruleStash },
62+
arcAndUtils: { arc: { ruleStash } },
6163
delegateConfig: {
6264
extension: {}
6365
}

0 commit comments

Comments
 (0)