@merchi/sdk 1.42.20 → 1.42.21
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/dist/entities/cart.test.js +69 -0
- package/dist/entity.js +15 -2
- package/package.json +1 -1
- package/src/entities/cart.test.ts +48 -0
- package/src/entity.ts +15 -2
|
@@ -1,3 +1,18 @@
|
|
|
1
|
+
var __extends = (this && this.__extends) || (function () {
|
|
2
|
+
var extendStatics = function (d, b) {
|
|
3
|
+
extendStatics = Object.setPrototypeOf ||
|
|
4
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
5
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
6
|
+
return extendStatics(d, b);
|
|
7
|
+
};
|
|
8
|
+
return function (d, b) {
|
|
9
|
+
if (typeof b !== "function" && b !== null)
|
|
10
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
11
|
+
extendStatics(d, b);
|
|
12
|
+
function __() { this.constructor = d; }
|
|
13
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
14
|
+
};
|
|
15
|
+
})();
|
|
1
16
|
import { Merchi } from '../merchi.js';
|
|
2
17
|
import { setup, mockFetch } from '../test_util.js';
|
|
3
18
|
setup();
|
|
@@ -55,6 +70,60 @@ test('preserves isTest', function () {
|
|
|
55
70
|
entity.fromJson({ id: 1, isTest: true });
|
|
56
71
|
expect(entity.toJson()).toMatchObject({ isTest: true });
|
|
57
72
|
});
|
|
73
|
+
test('serialises a new cart when FormData has no set', function () {
|
|
74
|
+
var AppendOnlyFormData = /** @class */ (function () {
|
|
75
|
+
function AppendOnlyFormData() {
|
|
76
|
+
this.fields = new Map();
|
|
77
|
+
}
|
|
78
|
+
AppendOnlyFormData.prototype.append = function (name, value) {
|
|
79
|
+
this.fields.set(name, String(value));
|
|
80
|
+
};
|
|
81
|
+
AppendOnlyFormData.prototype.get = function (name) {
|
|
82
|
+
return this.fields.has(name) ? this.fields.get(name) : null;
|
|
83
|
+
};
|
|
84
|
+
AppendOnlyFormData.prototype.entries = function () {
|
|
85
|
+
return this.fields.entries();
|
|
86
|
+
};
|
|
87
|
+
return AppendOnlyFormData;
|
|
88
|
+
}());
|
|
89
|
+
var ReplacingFormData = /** @class */ (function (_super) {
|
|
90
|
+
__extends(ReplacingFormData, _super);
|
|
91
|
+
function ReplacingFormData() {
|
|
92
|
+
var _this = _super !== null && _super.apply(this, arguments) || this;
|
|
93
|
+
_this.ready = false;
|
|
94
|
+
return _this;
|
|
95
|
+
}
|
|
96
|
+
ReplacingFormData.prototype.append = function (name, value) {
|
|
97
|
+
if (!this.ready) {
|
|
98
|
+
throw new Error("delete was not called before append(".concat(name, ")"));
|
|
99
|
+
}
|
|
100
|
+
this.ready = false;
|
|
101
|
+
_super.prototype.append.call(this, name, value);
|
|
102
|
+
};
|
|
103
|
+
ReplacingFormData.prototype.delete = function () {
|
|
104
|
+
this.ready = true;
|
|
105
|
+
};
|
|
106
|
+
return ReplacingFormData;
|
|
107
|
+
}(AppendOnlyFormData));
|
|
108
|
+
var serialise = function (form) {
|
|
109
|
+
var merchi = new Merchi();
|
|
110
|
+
var cart = new merchi.Cart();
|
|
111
|
+
cart.isTest = false;
|
|
112
|
+
var domain = new merchi.Domain();
|
|
113
|
+
domain.id = 9;
|
|
114
|
+
cart.domain = domain;
|
|
115
|
+
cart.toFormData({ existing: form });
|
|
116
|
+
return form;
|
|
117
|
+
};
|
|
118
|
+
var appended = serialise(new AppendOnlyFormData());
|
|
119
|
+
expect(appended.get('isTest')).toBe('false');
|
|
120
|
+
expect(appended.get('domain-0-id')).toBe('9');
|
|
121
|
+
expect(appended.get('domain-count')).toBe('1');
|
|
122
|
+
var replaced = serialise(new ReplacingFormData());
|
|
123
|
+
expect(replaced.get('isTest')).toBe('false');
|
|
124
|
+
expect(replaced.get('domain-0-id')).toBe('9');
|
|
125
|
+
expect(replaced.get('domain-count')).toBe('1');
|
|
126
|
+
});
|
|
58
127
|
test('preserves source attribution', function () {
|
|
59
128
|
var merchi = new Merchi();
|
|
60
129
|
var entity = new merchi.Cart();
|
package/dist/entity.js
CHANGED
|
@@ -302,6 +302,19 @@ var Entity = /** @class */ (function () {
|
|
|
302
302
|
if (fileIndex === undefined) {
|
|
303
303
|
fileIndex = { value: 0 };
|
|
304
304
|
}
|
|
305
|
+
// Prefer set so a repeated key is replaced. Fall back to append for
|
|
306
|
+
// FormData implementations that never added set.
|
|
307
|
+
var writeField = function (target, name, value) {
|
|
308
|
+
if (typeof target.set === 'function') {
|
|
309
|
+
target.set(name, value);
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
var legacy = target;
|
|
313
|
+
if (typeof legacy.delete === 'function') {
|
|
314
|
+
legacy.delete(name);
|
|
315
|
+
}
|
|
316
|
+
target.append(name, value);
|
|
317
|
+
};
|
|
305
318
|
var appendData = function (name, value) {
|
|
306
319
|
/* istanbul ignore next */
|
|
307
320
|
if (name === undefined || value === undefined) {
|
|
@@ -311,10 +324,10 @@ var Entity = /** @class */ (function () {
|
|
|
311
324
|
if (prefix) {
|
|
312
325
|
name = prefix + '-' + name;
|
|
313
326
|
}
|
|
314
|
-
result
|
|
327
|
+
writeField(result, name, value);
|
|
315
328
|
};
|
|
316
329
|
if (_this.fileData !== undefined) {
|
|
317
|
-
result
|
|
330
|
+
writeField(result, String(fileIndex.value), _this.fileData);
|
|
318
331
|
appendData('fileDataIndex', fileIndex.value);
|
|
319
332
|
fileIndex.value++;
|
|
320
333
|
}
|
package/package.json
CHANGED
|
@@ -63,6 +63,54 @@ test('preserves isTest', () => {
|
|
|
63
63
|
expect(entity.toJson()).toMatchObject({ isTest: true });
|
|
64
64
|
});
|
|
65
65
|
|
|
66
|
+
test('serialises a new cart when FormData has no set', () => {
|
|
67
|
+
class AppendOnlyFormData {
|
|
68
|
+
fields = new Map<string, string>();
|
|
69
|
+
append(name: string, value: any) {
|
|
70
|
+
this.fields.set(name, String(value));
|
|
71
|
+
}
|
|
72
|
+
get(name: string) {
|
|
73
|
+
return this.fields.has(name) ? this.fields.get(name) : null;
|
|
74
|
+
}
|
|
75
|
+
entries() {
|
|
76
|
+
return this.fields.entries();
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
class ReplacingFormData extends AppendOnlyFormData {
|
|
80
|
+
private ready = false;
|
|
81
|
+
append(name: string, value: any) {
|
|
82
|
+
if (!this.ready) {
|
|
83
|
+
throw new Error(`delete was not called before append(${name})`);
|
|
84
|
+
}
|
|
85
|
+
this.ready = false;
|
|
86
|
+
super.append(name, value);
|
|
87
|
+
}
|
|
88
|
+
delete() {
|
|
89
|
+
this.ready = true;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
const serialise = (form: AppendOnlyFormData) => {
|
|
93
|
+
const merchi = new Merchi();
|
|
94
|
+
const cart = new merchi.Cart();
|
|
95
|
+
cart.isTest = false;
|
|
96
|
+
const domain = new merchi.Domain();
|
|
97
|
+
domain.id = 9;
|
|
98
|
+
cart.domain = domain;
|
|
99
|
+
cart.toFormData({existing: form as unknown as FormData});
|
|
100
|
+
return form;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const appended = serialise(new AppendOnlyFormData());
|
|
104
|
+
expect(appended.get('isTest')).toBe('false');
|
|
105
|
+
expect(appended.get('domain-0-id')).toBe('9');
|
|
106
|
+
expect(appended.get('domain-count')).toBe('1');
|
|
107
|
+
|
|
108
|
+
const replaced = serialise(new ReplacingFormData());
|
|
109
|
+
expect(replaced.get('isTest')).toBe('false');
|
|
110
|
+
expect(replaced.get('domain-0-id')).toBe('9');
|
|
111
|
+
expect(replaced.get('domain-count')).toBe('1');
|
|
112
|
+
});
|
|
113
|
+
|
|
66
114
|
test('preserves source attribution', () => {
|
|
67
115
|
const merchi = new Merchi();
|
|
68
116
|
const entity = new merchi.Cart();
|
package/src/entity.ts
CHANGED
|
@@ -928,6 +928,19 @@ export class Entity {
|
|
|
928
928
|
if (fileIndex === undefined) {
|
|
929
929
|
fileIndex = {value: 0};
|
|
930
930
|
}
|
|
931
|
+
// Prefer set so a repeated key is replaced. Fall back to append for
|
|
932
|
+
// FormData implementations that never added set.
|
|
933
|
+
const writeField = (target: FormData, name: string, value: any) => {
|
|
934
|
+
if (typeof target.set === 'function') {
|
|
935
|
+
target.set(name, value);
|
|
936
|
+
return;
|
|
937
|
+
}
|
|
938
|
+
const legacy = target as FormData & {delete?: (field: string) => void};
|
|
939
|
+
if (typeof legacy.delete === 'function') {
|
|
940
|
+
legacy.delete(name);
|
|
941
|
+
}
|
|
942
|
+
target.append(name, value);
|
|
943
|
+
};
|
|
931
944
|
const appendData = (name: string, value: any) => {
|
|
932
945
|
/* istanbul ignore next */
|
|
933
946
|
if (name === undefined || value === undefined) {
|
|
@@ -937,10 +950,10 @@ export class Entity {
|
|
|
937
950
|
if (prefix) {
|
|
938
951
|
name = prefix + '-' + name;
|
|
939
952
|
}
|
|
940
|
-
result
|
|
953
|
+
writeField(result, name, value);
|
|
941
954
|
};
|
|
942
955
|
if ((this as any).fileData !== undefined) {
|
|
943
|
-
result
|
|
956
|
+
writeField(result, String(fileIndex.value), (this as any).fileData);
|
|
944
957
|
appendData('fileDataIndex', fileIndex.value);
|
|
945
958
|
fileIndex.value++;
|
|
946
959
|
}
|