@adobe/spacecat-shared-tokowaka-client 1.4.0 → 1.4.2
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/CHANGELOG.md +14 -0
- package/README.md +107 -7
- package/package.json +1 -1
- package/src/cdn/cdn-client-registry.js +2 -0
- package/src/cdn/fastly-cdn-client.js +156 -0
- package/src/index.d.ts +47 -11
- package/src/index.js +152 -86
- package/src/mappers/generic-mapper.js +5 -1
- package/src/utils/custom-html-utils.js +5 -5
- package/test/cdn/fastly-cdn-client.test.js +484 -0
- package/test/index.test.js +331 -118
- package/test/mappers/generic-mapper.test.js +82 -0
- package/test/utils/html-utils.test.js +12 -12
|
@@ -663,6 +663,36 @@ describe('GenericMapper', () => {
|
|
|
663
663
|
expect(patch.valueFormat).to.equal('hast');
|
|
664
664
|
});
|
|
665
665
|
|
|
666
|
+
it('should parse JSON patchValue when format is json', () => {
|
|
667
|
+
const jsonValue = {
|
|
668
|
+
title: 'Test Title',
|
|
669
|
+
description: 'Test description',
|
|
670
|
+
metadata: { key: 'value' },
|
|
671
|
+
};
|
|
672
|
+
|
|
673
|
+
const suggestion = {
|
|
674
|
+
getId: () => 'sugg-json-format',
|
|
675
|
+
getUpdatedAt: () => '2025-01-15T10:00:00.000Z',
|
|
676
|
+
getData: () => ({
|
|
677
|
+
transformRules: {
|
|
678
|
+
action: 'replace',
|
|
679
|
+
selector: '.content',
|
|
680
|
+
},
|
|
681
|
+
patchValue: JSON.stringify(jsonValue),
|
|
682
|
+
format: 'json',
|
|
683
|
+
url: 'https://example.com/page',
|
|
684
|
+
}),
|
|
685
|
+
};
|
|
686
|
+
|
|
687
|
+
const patches = mapper.suggestionsToPatches('/page', [suggestion], 'opp-json');
|
|
688
|
+
|
|
689
|
+
expect(patches.length).to.equal(1);
|
|
690
|
+
const patch = patches[0];
|
|
691
|
+
|
|
692
|
+
expect(patch.value).to.deep.equal(jsonValue);
|
|
693
|
+
expect(patch.valueFormat).to.equal('json');
|
|
694
|
+
});
|
|
695
|
+
|
|
666
696
|
it('should use raw patchValue when format is not hast', () => {
|
|
667
697
|
const suggestion = {
|
|
668
698
|
getId: () => 'sugg-text',
|
|
@@ -686,6 +716,58 @@ describe('GenericMapper', () => {
|
|
|
686
716
|
expect(patch.valueFormat).to.equal('text');
|
|
687
717
|
});
|
|
688
718
|
|
|
719
|
+
it('should parse and include attrs when provided', () => {
|
|
720
|
+
const attrs = {
|
|
721
|
+
id: 'custom-id',
|
|
722
|
+
class: 'custom-class',
|
|
723
|
+
'data-test': 'test-value',
|
|
724
|
+
};
|
|
725
|
+
|
|
726
|
+
const suggestion = {
|
|
727
|
+
getId: () => 'sugg-with-attrs',
|
|
728
|
+
getUpdatedAt: () => '2025-01-15T10:00:00.000Z',
|
|
729
|
+
getData: () => ({
|
|
730
|
+
transformRules: {
|
|
731
|
+
action: 'insertAfter',
|
|
732
|
+
selector: '#selector',
|
|
733
|
+
},
|
|
734
|
+
patchValue: 'Content with attributes',
|
|
735
|
+
attrs: JSON.stringify(attrs),
|
|
736
|
+
url: 'https://example.com/page',
|
|
737
|
+
}),
|
|
738
|
+
};
|
|
739
|
+
|
|
740
|
+
const patches = mapper.suggestionsToPatches('/page', [suggestion], 'opp-attrs');
|
|
741
|
+
|
|
742
|
+
expect(patches.length).to.equal(1);
|
|
743
|
+
const patch = patches[0];
|
|
744
|
+
|
|
745
|
+
expect(patch.attrs).to.deep.equal(attrs);
|
|
746
|
+
expect(patch.value).to.equal('Content with attributes');
|
|
747
|
+
});
|
|
748
|
+
|
|
749
|
+
it('should not include attrs when not provided', () => {
|
|
750
|
+
const suggestion = {
|
|
751
|
+
getId: () => 'sugg-no-attrs',
|
|
752
|
+
getUpdatedAt: () => '2025-01-15T10:00:00.000Z',
|
|
753
|
+
getData: () => ({
|
|
754
|
+
transformRules: {
|
|
755
|
+
action: 'insertAfter',
|
|
756
|
+
selector: '#selector',
|
|
757
|
+
},
|
|
758
|
+
patchValue: 'Content without attributes',
|
|
759
|
+
url: 'https://example.com/page',
|
|
760
|
+
}),
|
|
761
|
+
};
|
|
762
|
+
|
|
763
|
+
const patches = mapper.suggestionsToPatches('/page', [suggestion], 'opp-no-attrs');
|
|
764
|
+
|
|
765
|
+
expect(patches.length).to.equal(1);
|
|
766
|
+
const patch = patches[0];
|
|
767
|
+
|
|
768
|
+
expect(patch.attrs).to.be.undefined;
|
|
769
|
+
});
|
|
770
|
+
|
|
689
771
|
it('should not include UI-only fields in patch', () => {
|
|
690
772
|
const suggestion = {
|
|
691
773
|
getId: () => 'sugg-ui',
|
|
@@ -54,7 +54,7 @@ describe('HTML Utils', () => {
|
|
|
54
54
|
try {
|
|
55
55
|
await fetchHtmlWithWarmup(
|
|
56
56
|
'https://example.com/page',
|
|
57
|
-
'api-key',
|
|
57
|
+
'preview-api-key',
|
|
58
58
|
'',
|
|
59
59
|
'edge-url',
|
|
60
60
|
log,
|
|
@@ -70,7 +70,7 @@ describe('HTML Utils', () => {
|
|
|
70
70
|
try {
|
|
71
71
|
await fetchHtmlWithWarmup(
|
|
72
72
|
'https://example.com/page',
|
|
73
|
-
'api-key',
|
|
73
|
+
'preview-api-key',
|
|
74
74
|
'host',
|
|
75
75
|
'',
|
|
76
76
|
log,
|
|
@@ -82,7 +82,7 @@ describe('HTML Utils', () => {
|
|
|
82
82
|
}
|
|
83
83
|
});
|
|
84
84
|
|
|
85
|
-
it('should throw error when
|
|
85
|
+
it('should throw error when preview API key is missing', async () => {
|
|
86
86
|
try {
|
|
87
87
|
await fetchHtmlWithWarmup(
|
|
88
88
|
'https://example.com/page',
|
|
@@ -94,7 +94,7 @@ describe('HTML Utils', () => {
|
|
|
94
94
|
);
|
|
95
95
|
expect.fail('Should have thrown error');
|
|
96
96
|
} catch (error) {
|
|
97
|
-
expect(error.message).to.equal('Tokowaka API key is required for fetching HTML');
|
|
97
|
+
expect(error.message).to.equal('Tokowaka preview API key is required for fetching HTML');
|
|
98
98
|
}
|
|
99
99
|
});
|
|
100
100
|
|
|
@@ -111,7 +111,7 @@ describe('HTML Utils', () => {
|
|
|
111
111
|
|
|
112
112
|
const html = await fetchHtmlWithWarmup(
|
|
113
113
|
'https://example.com/page',
|
|
114
|
-
'api-key',
|
|
114
|
+
'preview-api-key',
|
|
115
115
|
'host',
|
|
116
116
|
'https://edge.example.com',
|
|
117
117
|
log,
|
|
@@ -136,7 +136,7 @@ describe('HTML Utils', () => {
|
|
|
136
136
|
|
|
137
137
|
const html = await fetchHtmlWithWarmup(
|
|
138
138
|
'https://example.com/page?param=value',
|
|
139
|
-
'api-key',
|
|
139
|
+
'preview-api-key',
|
|
140
140
|
'host',
|
|
141
141
|
'https://edge.example.com',
|
|
142
142
|
log,
|
|
@@ -178,7 +178,7 @@ describe('HTML Utils', () => {
|
|
|
178
178
|
try {
|
|
179
179
|
await fetchHtmlWithWarmup(
|
|
180
180
|
'https://example.com/page',
|
|
181
|
-
'api-key',
|
|
181
|
+
'preview-api-key',
|
|
182
182
|
'host',
|
|
183
183
|
'https://edge.example.com',
|
|
184
184
|
log,
|
|
@@ -208,7 +208,7 @@ describe('HTML Utils', () => {
|
|
|
208
208
|
try {
|
|
209
209
|
await fetchHtmlWithWarmup(
|
|
210
210
|
'https://example.com/page',
|
|
211
|
-
'api-key',
|
|
211
|
+
'preview-api-key',
|
|
212
212
|
'host',
|
|
213
213
|
'https://edge.example.com',
|
|
214
214
|
log,
|
|
@@ -239,7 +239,7 @@ describe('HTML Utils', () => {
|
|
|
239
239
|
try {
|
|
240
240
|
await fetchHtmlWithWarmup(
|
|
241
241
|
'https://example.com/page',
|
|
242
|
-
'api-key',
|
|
242
|
+
'preview-api-key',
|
|
243
243
|
'host',
|
|
244
244
|
'https://edge.example.com',
|
|
245
245
|
log,
|
|
@@ -319,7 +319,7 @@ describe('HTML Utils', () => {
|
|
|
319
319
|
|
|
320
320
|
const html = await fetchHtmlWithWarmup(
|
|
321
321
|
'https://example.com/page',
|
|
322
|
-
'api-key',
|
|
322
|
+
'preview-api-key',
|
|
323
323
|
'host',
|
|
324
324
|
'https://edge.example.com',
|
|
325
325
|
log,
|
|
@@ -375,7 +375,7 @@ describe('HTML Utils', () => {
|
|
|
375
375
|
try {
|
|
376
376
|
await fetchHtmlWithWarmup(
|
|
377
377
|
'https://example.com/page',
|
|
378
|
-
'api-key',
|
|
378
|
+
'preview-api-key',
|
|
379
379
|
'host',
|
|
380
380
|
'https://edge.example.com',
|
|
381
381
|
log,
|
|
@@ -416,7 +416,7 @@ describe('HTML Utils', () => {
|
|
|
416
416
|
|
|
417
417
|
const html = await fetchHtmlWithWarmup(
|
|
418
418
|
'https://example.com/page',
|
|
419
|
-
'api-key',
|
|
419
|
+
'preview-api-key',
|
|
420
420
|
'host',
|
|
421
421
|
'https://edge.example.com',
|
|
422
422
|
log,
|