@aemforms/af-core 0.22.85 → 0.22.87

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.
@@ -2553,6 +2553,24 @@ const urlEncoded = (data) => {
2553
2553
  });
2554
2554
  return formData;
2555
2555
  };
2556
+ const submit = async (context, success, error, submitAs = 'multipart/form-data', input_data = null, action = '', metadata = null) => {
2557
+ const endpoint = action || context.form.action;
2558
+ let data = input_data;
2559
+ if (typeof data != 'object' || data == null) {
2560
+ data = context.form.exportData();
2561
+ }
2562
+ const attachments = getAttachments(context.form, true);
2563
+ let submitContentType = submitAs;
2564
+ const submitDataAndMetaData = { 'data': data, ...metadata };
2565
+ let formData = submitDataAndMetaData;
2566
+ if (Object.keys(attachments).length > 0 || submitAs === 'multipart/form-data') {
2567
+ formData = multipartFormData(submitDataAndMetaData, attachments);
2568
+ submitContentType = 'multipart/form-data';
2569
+ }
2570
+ await request(context, endpoint, 'POST', formData, success, error, {
2571
+ 'Content-Type': submitContentType
2572
+ });
2573
+ };
2556
2574
  const multipartFormData = (data, attachments) => {
2557
2575
  const formData = new FormData();
2558
2576
  Object.entries(data).forEach(([key, value]) => {
@@ -2585,24 +2603,6 @@ const multipartFormData = (data, attachments) => {
2585
2603
  }
2586
2604
  return formData;
2587
2605
  };
2588
- const submit = async (context, success, error, submitAs = 'multipart/form-data', input_data = null, action = '', metadata = null) => {
2589
- const endpoint = action || context.form.action;
2590
- let data = input_data;
2591
- if (typeof data != 'object' || data == null) {
2592
- data = context.form.exportData();
2593
- }
2594
- const attachments = getAttachments(context.form, true);
2595
- let submitContentType = submitAs;
2596
- const submitDataAndMetaData = { 'data': data, ...metadata };
2597
- let formData = submitDataAndMetaData;
2598
- if (Object.keys(attachments).length > 0 || submitAs === 'multipart/form-data') {
2599
- formData = multipartFormData(submitDataAndMetaData, attachments);
2600
- submitContentType = 'multipart/form-data';
2601
- }
2602
- await request(context, endpoint, 'POST', formData, success, error, {
2603
- 'Content-Type': submitContentType
2604
- });
2605
- };
2606
2606
  const createAction = (name, payload = {}) => {
2607
2607
  switch (name) {
2608
2608
  case 'change':
@@ -2799,7 +2799,7 @@ class FunctionRuntimeImpl {
2799
2799
  _signature: []
2800
2800
  },
2801
2801
  submitForm: {
2802
- _func: (args, data, interpreter) => {
2802
+ _func: async (args, data, interpreter) => {
2803
2803
  let success = null;
2804
2804
  let error = null;
2805
2805
  let submit_data;
@@ -2818,6 +2818,24 @@ class FunctionRuntimeImpl {
2818
2818
  submit_data = args.length > 3 ? valueOf(args[3]) : null;
2819
2819
  validate_form = args.length > 4 ? valueOf(args[4]) : true;
2820
2820
  }
2821
+ const form = interpreter.globals.form;
2822
+ if (form.captcha && form.captcha.properties['fd:captcha'].config.version === 'enterprise'
2823
+ && form.captcha.properties['fd:captcha'].config.keyType === 'score') {
2824
+ if (typeof interpreter.runtime.functionTable.fetchCaptchaToken?._func !== 'function') {
2825
+ interpreter.globals.form.logger.error('fetchCaptchaToken is not defined');
2826
+ interpreter.globals.form.dispatch(new SubmitError({ type: 'FetchCaptchaTokenNotDefined' }));
2827
+ return {};
2828
+ }
2829
+ try {
2830
+ const token = await interpreter.runtime.functionTable.fetchCaptchaToken._func([], data, interpreter);
2831
+ form.captcha.value = token;
2832
+ }
2833
+ catch (e) {
2834
+ interpreter.globals.form.logger.error('Error while fetching captcha token');
2835
+ interpreter.globals.form.dispatch(new SubmitError({ type: 'FetchCaptchaTokenFailed' }));
2836
+ return {};
2837
+ }
2838
+ }
2821
2839
  interpreter.globals.form.dispatch(new Submit({
2822
2840
  success,
2823
2841
  error,
@@ -2981,6 +2999,7 @@ class Form extends Container {
2981
2999
  _fields = {};
2982
3000
  _ids;
2983
3001
  _invalidFields = [];
3002
+ _captcha = null;
2984
3003
  constructor(n, fieldFactory, _ruleEngine, _eventQueue = new EventQueue(), logLevel = 'off', mode = 'create') {
2985
3004
  super(n, { fieldFactory: fieldFactory, mode });
2986
3005
  this._ruleEngine = _ruleEngine;
@@ -3081,7 +3100,7 @@ class Form extends Container {
3081
3100
  const options = {
3082
3101
  lang: this.lang,
3083
3102
  captchaInfo: captchaInfoObj,
3084
- additionalSubmitMetadata: { ...this.additionalSubmitMetadata }
3103
+ ...this.additionalSubmitMetadata
3085
3104
  };
3086
3105
  return new SubmitMetaData(options);
3087
3106
  }
@@ -3184,6 +3203,9 @@ class Form extends Container {
3184
3203
  return this._ids.next().value;
3185
3204
  }
3186
3205
  fieldAdded(field) {
3206
+ if (field.fieldType === 'captcha' && !this._captcha) {
3207
+ this._captcha = field;
3208
+ }
3187
3209
  this._fields[field.id] = field;
3188
3210
  field.subscribe((action) => {
3189
3211
  if (this._invalidFields.indexOf(action.target.id) === -1) {
@@ -3315,6 +3337,9 @@ class Form extends Container {
3315
3337
  get title() {
3316
3338
  return this._jsonModel.title || '';
3317
3339
  }
3340
+ get captcha() {
3341
+ return this._captcha;
3342
+ }
3318
3343
  }
3319
3344
 
3320
3345
  function stringToNumber(str, language) {
@@ -4452,6 +4477,9 @@ class Captcha extends Field {
4452
4477
  getDataNode() {
4453
4478
  return undefined;
4454
4479
  }
4480
+ custom_setProperty(action) {
4481
+ this.applyUpdates(action.payload);
4482
+ }
4455
4483
  }
4456
4484
 
4457
4485
  class Button extends Field {
@@ -1,5 +1,7 @@
1
1
  import Field from './Field';
2
+ import { Action } from './types';
2
3
  declare class Captcha extends Field {
3
4
  getDataNode(): undefined;
5
+ custom_setProperty(action: Action): void;
4
6
  }
5
7
  export default Captcha;
@@ -1,10 +1,10 @@
1
1
  import Container from './Container';
2
- import { Action, BaseModel, FieldModel, FieldsetModel, FormCreationMode, FormJson, FocusOption, FormModel, IFormFieldFactory } from './types/index';
3
2
  import FormMetaData from './FormMetaData';
4
3
  import SubmitMetaData from './SubmitMetaData';
5
4
  import EventQueue from './controller/EventQueue';
6
- import { Logger, LogLevel } from './controller/Logger';
5
+ import { LogLevel, Logger } from './controller/Logger';
7
6
  import RuleEngine from './rules/RuleEngine';
7
+ import { Action, BaseModel, FieldModel, FieldsetModel, FocusOption, FormCreationMode, FormJson, FormModel, IFormFieldFactory } from './types/index';
8
8
  import { Version } from './utils/Version';
9
9
  export declare const currentVersion: Version;
10
10
  declare class Form extends Container<FormJson> implements FormModel {
@@ -15,6 +15,7 @@ declare class Form extends Container<FormJson> implements FormModel {
15
15
  private _fields;
16
16
  _ids: Generator<string, void, string>;
17
17
  private _invalidFields;
18
+ private _captcha;
18
19
  constructor(n: FormJson, fieldFactory: IFormFieldFactory, _ruleEngine: RuleEngine, _eventQueue?: EventQueue, logLevel?: LogLevel, mode?: FormCreationMode);
19
20
  protected _applyDefaultsInModel(): void;
20
21
  private _logger;
@@ -135,5 +136,6 @@ declare class Form extends Container<FormJson> implements FormModel {
135
136
  get value(): null;
136
137
  get id(): string;
137
138
  get title(): string;
139
+ get captcha(): FieldModel | null;
138
140
  }
139
141
  export default Form;
@@ -8,7 +8,7 @@ declare abstract class Scriptable<T extends RulesJson> extends BaseNode<T> imple
8
8
  getRules(): import("./types/Json").Items<string>;
9
9
  private getCompiledRule;
10
10
  private getCompiledEvent;
11
- private applyUpdates;
11
+ protected applyUpdates(updates: any): void;
12
12
  protected executeAllRules(context: any): void;
13
13
  private getExpressionScope;
14
14
  private executeEvent;
@@ -37,7 +37,7 @@ declare class FunctionRuntimeImpl {
37
37
  _signature: never[];
38
38
  };
39
39
  submitForm: {
40
- _func: (args: Array<unknown>, data: unknown, interpreter: any) => {};
40
+ _func: (args: Array<unknown>, data: unknown, interpreter: any) => Promise<{}>;
41
41
  _signature: never[];
42
42
  };
43
43
  saveForm: {
package/lib/Captcha.d.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  import Field from './Field';
2
+ import { Action } from './types';
2
3
  declare class Captcha extends Field {
3
4
  getDataNode(): undefined;
5
+ custom_setProperty(action: Action): void;
4
6
  }
5
7
  export default Captcha;
package/lib/Captcha.js CHANGED
@@ -8,5 +8,8 @@ class Captcha extends Field_1.default {
8
8
  getDataNode() {
9
9
  return undefined;
10
10
  }
11
+ custom_setProperty(action) {
12
+ this.applyUpdates(action.payload);
13
+ }
11
14
  }
12
15
  exports.default = Captcha;
package/lib/Form.d.ts CHANGED
@@ -1,10 +1,10 @@
1
1
  import Container from './Container';
2
- import { Action, BaseModel, FieldModel, FieldsetModel, FormCreationMode, FormJson, FocusOption, FormModel, IFormFieldFactory } from './types/index';
3
2
  import FormMetaData from './FormMetaData';
4
3
  import SubmitMetaData from './SubmitMetaData';
5
4
  import EventQueue from './controller/EventQueue';
6
- import { Logger, LogLevel } from './controller/Logger';
5
+ import { LogLevel, Logger } from './controller/Logger';
7
6
  import RuleEngine from './rules/RuleEngine';
7
+ import { Action, BaseModel, FieldModel, FieldsetModel, FocusOption, FormCreationMode, FormJson, FormModel, IFormFieldFactory } from './types/index';
8
8
  import { Version } from './utils/Version';
9
9
  export declare const currentVersion: Version;
10
10
  declare class Form extends Container<FormJson> implements FormModel {
@@ -15,6 +15,7 @@ declare class Form extends Container<FormJson> implements FormModel {
15
15
  private _fields;
16
16
  _ids: Generator<string, void, string>;
17
17
  private _invalidFields;
18
+ private _captcha;
18
19
  constructor(n: FormJson, fieldFactory: IFormFieldFactory, _ruleEngine: RuleEngine, _eventQueue?: EventQueue, logLevel?: LogLevel, mode?: FormCreationMode);
19
20
  protected _applyDefaultsInModel(): void;
20
21
  private _logger;
@@ -135,5 +136,6 @@ declare class Form extends Container<FormJson> implements FormModel {
135
136
  get value(): null;
136
137
  get id(): string;
137
138
  get title(): string;
139
+ get captcha(): FieldModel | null;
138
140
  }
139
141
  export default Form;
package/lib/Form.js CHANGED
@@ -11,15 +11,15 @@ var _Form_instances, _Form_getNavigableChildren, _Form_getFirstNavigableChild, _
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.currentVersion = void 0;
13
13
  const Container_1 = __importDefault(require("./Container"));
14
- const index_1 = require("./types/index");
15
14
  const FormMetaData_1 = __importDefault(require("./FormMetaData"));
16
15
  const SubmitMetaData_1 = __importDefault(require("./SubmitMetaData"));
17
16
  const EventQueue_1 = __importDefault(require("./controller/EventQueue"));
17
+ const Events_1 = require("./controller/Events");
18
18
  const Logger_1 = require("./controller/Logger");
19
- const FormUtils_1 = require("./utils/FormUtils");
20
19
  const DataGroup_1 = __importDefault(require("./data/DataGroup"));
21
20
  const FunctionRuntime_1 = require("./rules/FunctionRuntime");
22
- const Events_1 = require("./controller/Events");
21
+ const index_1 = require("./types/index");
22
+ const FormUtils_1 = require("./utils/FormUtils");
23
23
  const Version_1 = require("./utils/Version");
24
24
  exports.currentVersion = new Version_1.Version('0.13');
25
25
  const changeEventVersion = new Version_1.Version('0.13');
@@ -32,6 +32,7 @@ class Form extends Container_1.default {
32
32
  this.additionalSubmitMetadata = {};
33
33
  this._fields = {};
34
34
  this._invalidFields = [];
35
+ this._captcha = null;
35
36
  this.dataRefRegex = /("[^"]+?"|[^.]+?)(?:\.|$)/g;
36
37
  this._logger = new Logger_1.Logger(logLevel);
37
38
  this._applyDefaultsInModel();
@@ -125,11 +126,7 @@ class Form extends Container_1.default {
125
126
  captchaInfoObj[field.qualifiedName] = field.value;
126
127
  }
127
128
  });
128
- const options = {
129
- lang: this.lang,
130
- captchaInfo: captchaInfoObj,
131
- additionalSubmitMetadata: Object.assign({}, this.additionalSubmitMetadata)
132
- };
129
+ const options = Object.assign({ lang: this.lang, captchaInfo: captchaInfoObj }, this.additionalSubmitMetadata);
133
130
  return new SubmitMetaData_1.default(options);
134
131
  }
135
132
  setFocus(field, focusOption) {
@@ -192,6 +189,9 @@ class Form extends Container_1.default {
192
189
  return this._ids.next().value;
193
190
  }
194
191
  fieldAdded(field) {
192
+ if (field.fieldType === 'captcha' && !this._captcha) {
193
+ this._captcha = field;
194
+ }
195
195
  this._fields[field.id] = field;
196
196
  field.subscribe((action) => {
197
197
  if (this._invalidFields.indexOf(action.target.id) === -1) {
@@ -326,6 +326,9 @@ class Form extends Container_1.default {
326
326
  get title() {
327
327
  return this._jsonModel.title || '';
328
328
  }
329
+ get captcha() {
330
+ return this._captcha;
331
+ }
329
332
  }
330
333
  _Form_instances = new WeakSet(), _Form_getNavigableChildren = function _Form_getNavigableChildren(children) {
331
334
  return children.filter(child => child.visible === true);
@@ -8,7 +8,7 @@ declare abstract class Scriptable<T extends RulesJson> extends BaseNode<T> imple
8
8
  getRules(): import("./types/Json").Items<string>;
9
9
  private getCompiledRule;
10
10
  private getCompiledEvent;
11
- private applyUpdates;
11
+ protected applyUpdates(updates: any): void;
12
12
  protected executeAllRules(context: any): void;
13
13
  private getExpressionScope;
14
14
  private executeEvent;
@@ -37,7 +37,7 @@ declare class FunctionRuntimeImpl {
37
37
  _signature: never[];
38
38
  };
39
39
  submitForm: {
40
- _func: (args: Array<unknown>, data: unknown, interpreter: any) => {};
40
+ _func: (args: Array<unknown>, data: unknown, interpreter: any) => Promise<{}>;
41
41
  _signature: never[];
42
42
  };
43
43
  saveForm: {
@@ -91,6 +91,25 @@ const urlEncoded = (data) => {
91
91
  });
92
92
  return formData;
93
93
  };
94
+ const submit = (context, success, error, submitAs = 'multipart/form-data', input_data = null, action = '', metadata = null) => __awaiter(void 0, void 0, void 0, function* () {
95
+ const endpoint = action || context.form.action;
96
+ let data = input_data;
97
+ if (typeof data != 'object' || data == null) {
98
+ data = context.form.exportData();
99
+ }
100
+ const attachments = (0, FormUtils_1.getAttachments)(context.form, true);
101
+ let submitContentType = submitAs;
102
+ const submitDataAndMetaData = Object.assign({ 'data': data }, metadata);
103
+ let formData = submitDataAndMetaData;
104
+ if (Object.keys(attachments).length > 0 || submitAs === 'multipart/form-data') {
105
+ formData = multipartFormData(submitDataAndMetaData, attachments);
106
+ submitContentType = 'multipart/form-data';
107
+ }
108
+ yield (0, exports.request)(context, endpoint, 'POST', formData, success, error, {
109
+ 'Content-Type': submitContentType
110
+ });
111
+ });
112
+ exports.submit = submit;
94
113
  const multipartFormData = (data, attachments) => {
95
114
  const formData = new FormData();
96
115
  Object.entries(data).forEach(([key, value]) => {
@@ -123,25 +142,6 @@ const multipartFormData = (data, attachments) => {
123
142
  }
124
143
  return formData;
125
144
  };
126
- const submit = (context, success, error, submitAs = 'multipart/form-data', input_data = null, action = '', metadata = null) => __awaiter(void 0, void 0, void 0, function* () {
127
- const endpoint = action || context.form.action;
128
- let data = input_data;
129
- if (typeof data != 'object' || data == null) {
130
- data = context.form.exportData();
131
- }
132
- const attachments = (0, FormUtils_1.getAttachments)(context.form, true);
133
- let submitContentType = submitAs;
134
- const submitDataAndMetaData = Object.assign({ 'data': data }, metadata);
135
- let formData = submitDataAndMetaData;
136
- if (Object.keys(attachments).length > 0 || submitAs === 'multipart/form-data') {
137
- formData = multipartFormData(submitDataAndMetaData, attachments);
138
- submitContentType = 'multipart/form-data';
139
- }
140
- yield (0, exports.request)(context, endpoint, 'POST', formData, success, error, {
141
- 'Content-Type': submitContentType
142
- });
143
- });
144
- exports.submit = submit;
145
145
  const createAction = (name, payload = {}) => {
146
146
  switch (name) {
147
147
  case 'change':
@@ -338,7 +338,8 @@ class FunctionRuntimeImpl {
338
338
  _signature: []
339
339
  },
340
340
  submitForm: {
341
- _func: (args, data, interpreter) => {
341
+ _func: (args, data, interpreter) => __awaiter(this, void 0, void 0, function* () {
342
+ var _a;
342
343
  let success = null;
343
344
  let error = null;
344
345
  let submit_data;
@@ -357,6 +358,24 @@ class FunctionRuntimeImpl {
357
358
  submit_data = args.length > 3 ? valueOf(args[3]) : null;
358
359
  validate_form = args.length > 4 ? valueOf(args[4]) : true;
359
360
  }
361
+ const form = interpreter.globals.form;
362
+ if (form.captcha && form.captcha.properties['fd:captcha'].config.version === 'enterprise'
363
+ && form.captcha.properties['fd:captcha'].config.keyType === 'score') {
364
+ if (typeof ((_a = interpreter.runtime.functionTable.fetchCaptchaToken) === null || _a === void 0 ? void 0 : _a._func) !== 'function') {
365
+ interpreter.globals.form.logger.error('fetchCaptchaToken is not defined');
366
+ interpreter.globals.form.dispatch(new Events_1.SubmitError({ type: 'FetchCaptchaTokenNotDefined' }));
367
+ return {};
368
+ }
369
+ try {
370
+ const token = yield interpreter.runtime.functionTable.fetchCaptchaToken._func([], data, interpreter);
371
+ form.captcha.value = token;
372
+ }
373
+ catch (e) {
374
+ interpreter.globals.form.logger.error('Error while fetching captcha token');
375
+ interpreter.globals.form.dispatch(new Events_1.SubmitError({ type: 'FetchCaptchaTokenFailed' }));
376
+ return {};
377
+ }
378
+ }
360
379
  interpreter.globals.form.dispatch(new Events_1.Submit({
361
380
  success,
362
381
  error,
@@ -365,7 +384,7 @@ class FunctionRuntimeImpl {
365
384
  data: submit_data
366
385
  }));
367
386
  return {};
368
- },
387
+ }),
369
388
  _signature: []
370
389
  },
371
390
  saveForm: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aemforms/af-core",
3
- "version": "0.22.85",
3
+ "version": "0.22.87",
4
4
  "description": "Core Module for Forms Runtime",
5
5
  "author": "Adobe Systems",
6
6
  "license": "Adobe Proprietary",
@@ -37,7 +37,7 @@
37
37
  },
38
38
  "dependencies": {
39
39
  "@adobe/json-formula": "0.1.50",
40
- "@aemforms/af-formatters": "^0.22.85"
40
+ "@aemforms/af-formatters": "^0.22.87"
41
41
  },
42
42
  "devDependencies": {
43
43
  "@babel/preset-env": "^7.20.2",
@@ -59,6 +59,7 @@
59
59
  "jest-junit": "^12.2.0",
60
60
  "nock": "^13.1.3",
61
61
  "node-fetch": "^2.6.1",
62
+ "parse-multipart-data": "^1.5.0",
62
63
  "ts-jest": "29.0",
63
64
  "typedoc": "0.22.11",
64
65
  "typedoc-plugin-markdown": "3.11.13",