@adobe/spacecat-shared-tokowaka-client 1.4.1 → 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 CHANGED
@@ -1,3 +1,10 @@
1
+ # [@adobe/spacecat-shared-tokowaka-client-v1.4.2](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-tokowaka-client-v1.4.1...@adobe/spacecat-shared-tokowaka-client-v1.4.2) (2025-12-12)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * use preview key instead of site api key in Edge Preview API ([#1228](https://github.com/adobe/spacecat-shared/issues/1228)) ([8e0c1a9](https://github.com/adobe/spacecat-shared/commit/8e0c1a9c228a740de66c29bf2480042d29aa88a6))
7
+
1
8
  # [@adobe/spacecat-shared-tokowaka-client-v1.4.1](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-tokowaka-client-v1.4.0...@adobe/spacecat-shared-tokowaka-client-v1.4.1) (2025-12-12)
2
9
 
3
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/spacecat-shared-tokowaka-client",
3
- "version": "1.4.1",
3
+ "version": "1.4.2",
4
4
  "description": "Tokowaka Client for SpaceCat - Edge optimization config management",
5
5
  "type": "module",
6
6
  "engines": {
package/src/index.js CHANGED
@@ -700,11 +700,11 @@ class TokowakaClient {
700
700
  */
701
701
  async previewSuggestions(site, opportunity, suggestions, options = {}) {
702
702
  // Get site's forwarded host for preview
703
- const { forwardedHost, apiKey } = site.getConfig()?.getTokowakaConfig() || {};
703
+ const { forwardedHost } = site.getConfig()?.getTokowakaConfig() || {};
704
704
 
705
- if (!hasText(forwardedHost) || !hasText(apiKey)) {
705
+ if (!hasText(forwardedHost)) {
706
706
  throw this.#createError(
707
- 'Site does not have a Tokowaka API key or forwarded host configured. '
707
+ 'Site does not have a Tokowaka forwarded host configured. '
708
708
  + 'Please onboard the site to Tokowaka first.',
709
709
  HTTP_BAD_REQUEST,
710
710
  );
@@ -720,6 +720,15 @@ class TokowakaClient {
720
720
  );
721
721
  }
722
722
 
723
+ // TOKOWAKA_PREVIEW_API_KEY is mandatory for preview
724
+ const tokowakaPreviewApiKey = this.env.TOKOWAKA_PREVIEW_API_KEY;
725
+ if (!hasText(tokowakaPreviewApiKey)) {
726
+ throw this.#createError(
727
+ 'TOKOWAKA_PREVIEW_API_KEY is required for preview functionality',
728
+ HTTP_INTERNAL_SERVER_ERROR,
729
+ );
730
+ }
731
+
723
732
  // TOKOWAKA_EDGE_URL is mandatory for preview
724
733
  const tokowakaEdgeUrl = this.env.TOKOWAKA_EDGE_URL;
725
734
  if (!hasText(tokowakaEdgeUrl)) {
@@ -817,7 +826,7 @@ class TokowakaClient {
817
826
  // Fetch original HTML (without preview)
818
827
  originalHtml = await fetchHtmlWithWarmup(
819
828
  previewUrl,
820
- apiKey,
829
+ tokowakaPreviewApiKey,
821
830
  forwardedHost,
822
831
  tokowakaEdgeUrl,
823
832
  this.log,
@@ -827,7 +836,7 @@ class TokowakaClient {
827
836
  // Then fetch optimized HTML (with preview)
828
837
  optimizedHtml = await fetchHtmlWithWarmup(
829
838
  previewUrl,
830
- apiKey,
839
+ tokowakaPreviewApiKey,
831
840
  forwardedHost,
832
841
  tokowakaEdgeUrl,
833
842
  this.log,
@@ -89,7 +89,7 @@ async function fetchWithRetry(url, options, maxRetries, retryDelayMs, log, fetch
89
89
  * Fetches HTML content from Tokowaka edge with warmup call and retry logic
90
90
  * Makes an initial warmup call, waits, then makes the actual call with retries
91
91
  * @param {string} url - Full URL to fetch
92
- * @param {string} apiKey - Tokowaka API key
92
+ * @param {string} previewApiKey - Tokowaka preview API key (internal)
93
93
  * @param {string} forwardedHost - Host to forward in x-forwarded-host header
94
94
  * @param {string} tokowakaEdgeUrl - Tokowaka edge URL
95
95
  * @param {boolean} isOptimized - Whether to fetch optimized HTML (with preview param)
@@ -103,7 +103,7 @@ async function fetchWithRetry(url, options, maxRetries, retryDelayMs, log, fetch
103
103
  */
104
104
  export async function fetchHtmlWithWarmup(
105
105
  url,
106
- apiKey,
106
+ previewApiKey,
107
107
  forwardedHost,
108
108
  tokowakaEdgeUrl,
109
109
  log,
@@ -115,8 +115,8 @@ export async function fetchHtmlWithWarmup(
115
115
  throw new Error('URL is required for fetching HTML');
116
116
  }
117
117
 
118
- if (!hasText(apiKey)) {
119
- throw new Error('Tokowaka API key is required for fetching HTML');
118
+ if (!hasText(previewApiKey)) {
119
+ throw new Error('Tokowaka preview API key is required for fetching HTML');
120
120
  }
121
121
 
122
122
  if (!hasText(forwardedHost)) {
@@ -143,7 +143,7 @@ export async function fetchHtmlWithWarmup(
143
143
 
144
144
  const headers = {
145
145
  'x-forwarded-host': forwardedHost,
146
- 'x-tokowaka-api-key': apiKey,
146
+ 'x-tokowaka-preview-api-key': previewApiKey,
147
147
  'x-tokowaka-url': urlPath,
148
148
  };
149
149
 
@@ -64,8 +64,7 @@ describe('TokowakaClient', () => {
64
64
  getBaseURL: () => 'https://example.com',
65
65
  getConfig: () => ({
66
66
  getTokowakaConfig: () => ({
67
- forwardedHost: 'example.com',
68
- apiKey: 'test-api-key',
67
+ forwardedHost: 'www.example.com',
69
68
  }),
70
69
  }),
71
70
  };
@@ -1698,14 +1697,16 @@ describe('TokowakaClient', () => {
1698
1697
  // Stub fetchConfig to return null by default (no existing config)
1699
1698
  sinon.stub(client, 'fetchConfig').resolves(null);
1700
1699
 
1701
- // Add TOKOWAKA_EDGE_URL to env
1700
+ // Add TOKOWAKA_EDGE_URL and TOKOWAKA_PREVIEW_API_KEY to env
1702
1701
  client.env.TOKOWAKA_EDGE_URL = 'https://edge-dev.tokowaka.now';
1702
+ client.env.TOKOWAKA_PREVIEW_API_KEY = 'internal-preview-key-123';
1703
1703
  });
1704
1704
 
1705
1705
  afterEach(() => {
1706
1706
  // fetchStub will be restored by global afterEach sinon.restore()
1707
1707
  // Just clean up env changes
1708
1708
  delete client.env.TOKOWAKA_EDGE_URL;
1709
+ delete client.env.TOKOWAKA_PREVIEW_API_KEY;
1709
1710
  });
1710
1711
 
1711
1712
  it('should preview suggestions successfully with HTML', async () => {
@@ -1734,6 +1735,18 @@ describe('TokowakaClient', () => {
1734
1735
  expect(s3Client.send).to.have.been.calledOnce;
1735
1736
  });
1736
1737
 
1738
+ it('should throw error if TOKOWAKA_PREVIEW_API_KEY is not configured', async () => {
1739
+ delete client.env.TOKOWAKA_PREVIEW_API_KEY;
1740
+
1741
+ try {
1742
+ await client.previewSuggestions(mockSite, mockOpportunity, mockSuggestions);
1743
+ expect.fail('Should have thrown error');
1744
+ } catch (error) {
1745
+ expect(error.message).to.include('TOKOWAKA_PREVIEW_API_KEY is required for preview');
1746
+ expect(error.status).to.equal(500);
1747
+ }
1748
+ });
1749
+
1737
1750
  it('should preview prerender-only suggestions with no patches', async () => {
1738
1751
  // Update fetchConfig to return existing config with deployed patches
1739
1752
  client.fetchConfig.resolves({
@@ -1813,7 +1826,7 @@ describe('TokowakaClient', () => {
1813
1826
  await client.previewSuggestions(mockSite, mockOpportunity, mockSuggestions);
1814
1827
  expect.fail('Should have thrown error');
1815
1828
  } catch (error) {
1816
- expect(error.message).to.include('Site does not have a Tokowaka API key or forwarded host configured');
1829
+ expect(error.message).to.include('Site does not have a Tokowaka forwarded host configured');
1817
1830
  expect(error.status).to.equal(400);
1818
1831
  }
1819
1832
  });
@@ -1827,7 +1840,7 @@ describe('TokowakaClient', () => {
1827
1840
  await client.previewSuggestions(mockSite, mockOpportunity, mockSuggestions);
1828
1841
  expect.fail('Should have thrown error');
1829
1842
  } catch (error) {
1830
- expect(error.message).to.include('Site does not have a Tokowaka API key or forwarded host configured');
1843
+ expect(error.message).to.include('Site does not have a Tokowaka forwarded host configured');
1831
1844
  expect(error.status).to.equal(400);
1832
1845
  }
1833
1846
  });
@@ -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 apiKey is missing', async () => {
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,