@jam-comments/server-utilities 4.1.0 → 4.3.0

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.
@@ -1,9 +1,229 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
2
25
  Object.defineProperty(exports, "__esModule", { value: true });
3
26
  const vitest_1 = require("vitest");
4
- const markupFetcher_1 = require("./markupFetcher");
27
+ const injectSchema = __importStar(require("./injectSchema"));
28
+ const fetcherExports = __importStar(require("./markupFetcher"));
29
+ const node_test_1 = require("node:test");
30
+ const utilsExports = __importStar(require("./utils"));
31
+ const { deleteTempDirectory } = utilsExports;
32
+ const { markupFetcher, batchMarkupFetcher } = fetcherExports;
33
+ (0, vitest_1.beforeEach)(() => {
34
+ deleteTempDirectory();
35
+ });
36
+ (0, node_test_1.afterEach)(() => {
37
+ vitest_1.vi.resetAllMocks();
38
+ });
39
+ (0, vitest_1.describe)("fetchAll()", () => {
40
+ (0, vitest_1.it)("fetches all comments in a single request", async () => {
41
+ const saveFileSpy = vitest_1.vi.spyOn(utilsExports, "saveFile");
42
+ const mockBatchFetcher = vitest_1.vi.fn().mockReturnValue({
43
+ data: [
44
+ { path: "/test", markup: "markup1" },
45
+ { path: "/test2", markup: "markup2" },
46
+ ],
47
+ meta: {
48
+ current_page: 1,
49
+ from: 1,
50
+ last_page: 1,
51
+ path: "/test",
52
+ per_page: 10,
53
+ to: 2,
54
+ total: 2,
55
+ },
56
+ });
57
+ const batchMarkupFetcherMock = vitest_1.vi.fn().mockImplementation((a, b) => {
58
+ return mockBatchFetcher;
59
+ });
60
+ await fetcherExports.fetchAll({
61
+ domain: "test.com",
62
+ apiKey: "123abc",
63
+ environment: "production",
64
+ }, "test_platform", vitest_1.vi.fn(), batchMarkupFetcherMock);
65
+ (0, vitest_1.expect)(batchMarkupFetcherMock).toHaveBeenCalledWith("test_platform", vitest_1.expect.anything());
66
+ (0, vitest_1.expect)(mockBatchFetcher).toHaveBeenCalledWith({
67
+ domain: "test.com",
68
+ apiKey: "123abc",
69
+ baseUrl: "https://go.jamcomments.com",
70
+ environment: "production",
71
+ page: 1,
72
+ tz: undefined,
73
+ });
74
+ (0, vitest_1.expect)(saveFileSpy).toHaveBeenCalledTimes(2);
75
+ (0, vitest_1.expect)(saveFileSpy).toHaveBeenCalledWith("/test", "markup1");
76
+ (0, vitest_1.expect)(saveFileSpy).toHaveBeenCalledWith("/test2", "markup2");
77
+ });
78
+ (0, vitest_1.it)("fetches all comments in multiple requests", async () => {
79
+ const saveFileSpy = vitest_1.vi.spyOn(utilsExports, "saveFile");
80
+ const mockBatchFetcher = vitest_1.vi
81
+ .fn()
82
+ .mockReturnValueOnce({
83
+ data: [
84
+ { path: "/test", markup: "markup1" },
85
+ { path: "/test2", markup: "markup2" },
86
+ ],
87
+ meta: {
88
+ current_page: 1,
89
+ from: 1,
90
+ last_page: 2,
91
+ path: "/test",
92
+ per_page: 2,
93
+ to: 2,
94
+ },
95
+ })
96
+ .mockReturnValueOnce({
97
+ data: [
98
+ { path: "/test3", markup: "markup3" },
99
+ { path: "/test4", markup: "markup4" },
100
+ ],
101
+ meta: {
102
+ current_page: 2,
103
+ from: 3,
104
+ last_page: 2,
105
+ path: "/test",
106
+ per_page: 2,
107
+ to: 4,
108
+ },
109
+ });
110
+ const batchMarkupFetcherMock = vitest_1.vi.fn().mockImplementation((a, b) => {
111
+ return mockBatchFetcher;
112
+ });
113
+ await fetcherExports.fetchAll({
114
+ domain: "test.com",
115
+ apiKey: "123abc",
116
+ environment: "production",
117
+ }, "test_platform", vitest_1.vi.fn(), batchMarkupFetcherMock);
118
+ (0, vitest_1.expect)(batchMarkupFetcherMock).toHaveBeenCalledWith("test_platform", vitest_1.expect.anything());
119
+ (0, vitest_1.expect)(mockBatchFetcher).toHaveBeenCalledWith({
120
+ domain: "test.com",
121
+ apiKey: "123abc",
122
+ baseUrl: "https://go.jamcomments.com",
123
+ environment: "production",
124
+ page: 1,
125
+ tz: undefined,
126
+ });
127
+ (0, vitest_1.expect)(mockBatchFetcher).toHaveBeenCalledTimes(2);
128
+ (0, vitest_1.expect)(mockBatchFetcher).toHaveBeenCalledWith({
129
+ domain: "test.com",
130
+ apiKey: "123abc",
131
+ baseUrl: "https://go.jamcomments.com",
132
+ environment: "production",
133
+ page: 2,
134
+ tz: undefined,
135
+ });
136
+ (0, vitest_1.expect)(saveFileSpy).toHaveBeenCalledTimes(4);
137
+ (0, vitest_1.expect)(saveFileSpy).toHaveBeenCalledWith("/test", "markup1");
138
+ (0, vitest_1.expect)(saveFileSpy).toHaveBeenCalledWith("/test2", "markup2");
139
+ });
140
+ (0, vitest_1.it)("deletes the temp directory of anything fails", async () => {
141
+ const deleteTempDirectorySpy = vitest_1.vi.spyOn(utilsExports, "deleteTempDirectory");
142
+ const mockBatchFetcher = vitest_1.vi.fn().mockImplementation(() => {
143
+ throw new Error("test error");
144
+ });
145
+ const batchMarkupFetcherMock = vitest_1.vi.fn().mockImplementation((a, b) => {
146
+ return mockBatchFetcher;
147
+ });
148
+ try {
149
+ await fetcherExports.fetchAll({
150
+ domain: "test.com",
151
+ apiKey: "123abc",
152
+ environment: "production",
153
+ }, "test_platform", vitest_1.vi.fn(), batchMarkupFetcherMock);
154
+ }
155
+ catch (e) {
156
+ (0, vitest_1.expect)(e.message).toEqual("test error");
157
+ }
158
+ (0, vitest_1.expect)(deleteTempDirectorySpy).toHaveBeenCalledOnce();
159
+ });
160
+ });
161
+ (0, vitest_1.describe)("batchMarkupFetcher", () => {
162
+ (0, vitest_1.it)("constructs fetch request correctly", async () => {
163
+ const injectSchemaSpy = vitest_1.vi.spyOn(injectSchema, "injectSchema");
164
+ const fetchMock = vitest_1.vi.fn().mockImplementation(() => {
165
+ return {
166
+ status: 200,
167
+ ok: true,
168
+ json: () => "results!",
169
+ };
170
+ });
171
+ const fetcher = batchMarkupFetcher("test", fetchMock);
172
+ const result = await fetcher({
173
+ domain: "test.com",
174
+ apiKey: "123abc",
175
+ environment: "production",
176
+ });
177
+ (0, vitest_1.expect)(injectSchemaSpy).not.toHaveBeenCalled();
178
+ (0, vitest_1.expect)(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup/all?domain=test.com&page=1", vitest_1.expect.objectContaining({
179
+ headers: vitest_1.expect.objectContaining({
180
+ Accept: "application/json",
181
+ Authorization: "Bearer 123abc",
182
+ "X-Platform": "test",
183
+ }),
184
+ method: "GET",
185
+ }));
186
+ (0, vitest_1.expect)(result).toEqual("results!");
187
+ });
188
+ (0, vitest_1.it)("stubs comments", async () => {
189
+ const fetchMock = vitest_1.vi.fn().mockImplementation(() => {
190
+ return {
191
+ status: 200,
192
+ ok: true,
193
+ json: () => "results!",
194
+ };
195
+ });
196
+ const fetcher = batchMarkupFetcher("test", fetchMock);
197
+ const result = await fetcher({
198
+ domain: "test.com",
199
+ apiKey: "123abc",
200
+ environment: "development",
201
+ });
202
+ (0, vitest_1.expect)(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup/all?domain=test.com&page=1&stub=true", vitest_1.expect.anything());
203
+ (0, vitest_1.expect)(result).toEqual("results!");
204
+ });
205
+ (0, vitest_1.it)("uses different base URL", async () => {
206
+ const fetchMock = vitest_1.vi.fn().mockImplementation(() => {
207
+ return {
208
+ status: 200,
209
+ ok: true,
210
+ json: () => "results!",
211
+ };
212
+ });
213
+ const fetcher = batchMarkupFetcher("test", fetchMock);
214
+ const result = await fetcher({
215
+ domain: "test.com",
216
+ apiKey: "123abc",
217
+ baseUrl: "http://ur-mom.com",
218
+ environment: "production",
219
+ });
220
+ (0, vitest_1.expect)(fetchMock).toHaveBeenCalledWith("http://ur-mom.com/api/v3/markup/all?domain=test.com&page=1", vitest_1.expect.anything());
221
+ (0, vitest_1.expect)(result).toEqual("results!");
222
+ });
223
+ });
5
224
  (0, vitest_1.describe)("markupFetcher", () => {
6
225
  (0, vitest_1.it)("constructs fetch request correctly", async () => {
226
+ const injectSchemaSpy = vitest_1.vi.spyOn(injectSchema, "injectSchema");
7
227
  const fetchMock = vitest_1.vi.fn().mockImplementation(() => {
8
228
  return {
9
229
  status: 200,
@@ -11,14 +231,15 @@ const markupFetcher_1 = require("./markupFetcher");
11
231
  text: () => "results!",
12
232
  };
13
233
  });
14
- const fetcher = (0, markupFetcher_1.markupFetcher)("test", fetchMock);
234
+ const fetcher = markupFetcher("test", fetchMock);
15
235
  const result = await fetcher({
16
236
  path: "/test",
17
237
  domain: "test.com",
18
238
  apiKey: "123abc",
19
239
  environment: "production",
20
240
  });
21
- (0, vitest_1.expect)(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?path=%2Ftest&domain=test.com", vitest_1.expect.objectContaining({
241
+ (0, vitest_1.expect)(injectSchemaSpy).not.toHaveBeenCalled();
242
+ (0, vitest_1.expect)(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?domain=test.com&path=%2Ftest", vitest_1.expect.objectContaining({
22
243
  headers: vitest_1.expect.objectContaining({
23
244
  Accept: "application/json",
24
245
  Authorization: "Bearer 123abc",
@@ -36,14 +257,14 @@ const markupFetcher_1 = require("./markupFetcher");
36
257
  text: () => "results!",
37
258
  };
38
259
  });
39
- const fetcher = (0, markupFetcher_1.markupFetcher)("test", fetchMock);
260
+ const fetcher = markupFetcher("test", fetchMock);
40
261
  const result = await fetcher({
41
262
  path: "/test",
42
263
  domain: "test.com",
43
264
  apiKey: "123abc",
44
265
  environment: "development",
45
266
  });
46
- (0, vitest_1.expect)(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?path=%2Ftest&domain=test.com&stub=true", vitest_1.expect.anything());
267
+ (0, vitest_1.expect)(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?domain=test.com&path=%2Ftest&stub=true", vitest_1.expect.anything());
47
268
  (0, vitest_1.expect)(result).toEqual("results!");
48
269
  });
49
270
  (0, vitest_1.it)("uses different base URL", async () => {
@@ -54,7 +275,7 @@ const markupFetcher_1 = require("./markupFetcher");
54
275
  text: () => "results!",
55
276
  };
56
277
  });
57
- const fetcher = (0, markupFetcher_1.markupFetcher)("test", fetchMock);
278
+ const fetcher = markupFetcher("test", fetchMock);
58
279
  const result = await fetcher({
59
280
  path: "/test",
60
281
  domain: "test.com",
@@ -62,7 +283,7 @@ const markupFetcher_1 = require("./markupFetcher");
62
283
  baseUrl: "http://ur-mom.com",
63
284
  environment: "production",
64
285
  });
65
- (0, vitest_1.expect)(fetchMock).toHaveBeenCalledWith("http://ur-mom.com/api/v3/markup?path=%2Ftest&domain=test.com", vitest_1.expect.anything());
286
+ (0, vitest_1.expect)(fetchMock).toHaveBeenCalledWith("http://ur-mom.com/api/v3/markup?domain=test.com&path=%2Ftest", vitest_1.expect.anything());
66
287
  (0, vitest_1.expect)(result).toEqual("results!");
67
288
  });
68
289
  (0, vitest_1.it)("respects production!!!", async () => {
@@ -73,7 +294,7 @@ const markupFetcher_1 = require("./markupFetcher");
73
294
  text: () => "results!",
74
295
  };
75
296
  });
76
- const fetcher = (0, markupFetcher_1.markupFetcher)("test", fetchMock);
297
+ const fetcher = markupFetcher("test", fetchMock);
77
298
  const result = await fetcher({
78
299
  path: "/test",
79
300
  domain: "test.com",
@@ -81,7 +302,7 @@ const markupFetcher_1 = require("./markupFetcher");
81
302
  baseUrl: "http://ur-mom.com",
82
303
  environment: "production",
83
304
  });
84
- (0, vitest_1.expect)(fetchMock).toHaveBeenCalledWith("http://ur-mom.com/api/v3/markup?path=%2Ftest&domain=test.com", vitest_1.expect.anything());
305
+ (0, vitest_1.expect)(fetchMock).toHaveBeenCalledWith("http://ur-mom.com/api/v3/markup?domain=test.com&path=%2Ftest", vitest_1.expect.anything());
85
306
  (0, vitest_1.expect)(result).toEqual("results!");
86
307
  });
87
308
  (0, vitest_1.it)("falls back to root path", async () => {
@@ -92,14 +313,14 @@ const markupFetcher_1 = require("./markupFetcher");
92
313
  text: () => "results!",
93
314
  };
94
315
  });
95
- const fetcher = (0, markupFetcher_1.markupFetcher)("test", fetchMock);
316
+ const fetcher = markupFetcher("test", fetchMock);
96
317
  const result = await fetcher({
97
318
  path: null,
98
319
  domain: "test.com",
99
320
  apiKey: "123abc",
100
321
  environment: "production",
101
322
  });
102
- (0, vitest_1.expect)(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?path=%2F&domain=test.com", vitest_1.expect.anything());
323
+ (0, vitest_1.expect)(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?domain=test.com&path=%2F", vitest_1.expect.anything());
103
324
  (0, vitest_1.expect)(result).toEqual("results!");
104
325
  });
105
326
  (0, vitest_1.it)("credentials are invalid", async () => {
@@ -110,7 +331,7 @@ const markupFetcher_1 = require("./markupFetcher");
110
331
  text: () => "bad results!",
111
332
  };
112
333
  });
113
- const fetcher = (0, markupFetcher_1.markupFetcher)("test", fetchMock);
334
+ const fetcher = markupFetcher("test", fetchMock);
114
335
  (0, vitest_1.expect)(fetcher({
115
336
  path: "/test",
116
337
  domain: "test.com",
@@ -125,7 +346,7 @@ const markupFetcher_1 = require("./markupFetcher");
125
346
  text: () => "bad results!",
126
347
  };
127
348
  });
128
- const fetcher = (0, markupFetcher_1.markupFetcher)("test", fetchMock);
349
+ const fetcher = markupFetcher("test", fetchMock);
129
350
  (0, vitest_1.expect)(fetcher({
130
351
  path: "/test",
131
352
  domain: "test.com",
@@ -134,9 +355,9 @@ const markupFetcher_1 = require("./markupFetcher");
134
355
  });
135
356
  (0, vitest_1.describe)("timezone validation", () => {
136
357
  (0, vitest_1.it)("throws error when invalid timezone is provided", async () => {
137
- const fetchMock = vitest_1.vi.fn();
138
- const fetcher = (0, markupFetcher_1.markupFetcher)("test", fetchMock);
139
- (0, vitest_1.expect)(fetchMock).not.toHaveBeenCalled();
358
+ const fetchImplMock = vitest_1.vi.fn();
359
+ const fetcher = markupFetcher("test", fetchImplMock);
360
+ (0, vitest_1.expect)(fetchImplMock).not.toHaveBeenCalled();
140
361
  (0, vitest_1.expect)(fetcher({
141
362
  path: "/test",
142
363
  domain: "test.com",
@@ -153,15 +374,15 @@ const markupFetcher_1 = require("./markupFetcher");
153
374
  text: () => "results!",
154
375
  };
155
376
  });
156
- const fetcher = (0, markupFetcher_1.markupFetcher)("test", fetchMock);
377
+ const fetcher = markupFetcher("test", fetchMock);
157
378
  const result = await fetcher({
158
- path: null,
379
+ path: "/some/path",
159
380
  domain: "test.com",
160
381
  apiKey: "123abc",
161
382
  tz: "America/New_York",
162
383
  environment: "production",
163
384
  });
164
- (0, vitest_1.expect)(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?path=%2F&domain=test.com&tz=America%2FNew_York", vitest_1.expect.anything());
385
+ (0, vitest_1.expect)(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?domain=test.com&path=%2Fsome%2Fpath&tz=America%2FNew_York", vitest_1.expect.anything());
165
386
  (0, vitest_1.expect)(result).toEqual("results!");
166
387
  });
167
388
  (0, vitest_1.it)("trims a valid timezone", async () => {
@@ -172,21 +393,22 @@ const markupFetcher_1 = require("./markupFetcher");
172
393
  text: () => "results!",
173
394
  };
174
395
  });
175
- const fetcher = (0, markupFetcher_1.markupFetcher)("test", fetchMock);
396
+ const fetcher = markupFetcher("test", fetchMock);
176
397
  const result = await fetcher({
177
- path: null,
398
+ path: "/some/path",
178
399
  domain: "test.com",
179
400
  apiKey: "123abc",
180
401
  tz: " America/Chicago ",
181
402
  environment: "production",
182
403
  });
183
- (0, vitest_1.expect)(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?path=%2F&domain=test.com&tz=America%2FChicago", vitest_1.expect.anything());
404
+ (0, vitest_1.expect)(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?domain=test.com&path=%2Fsome%2Fpath&tz=America%2FChicago", vitest_1.expect.anything());
184
405
  (0, vitest_1.expect)(result).toEqual("results!");
185
406
  });
186
407
  });
187
408
  });
188
409
  (0, vitest_1.describe)("passing schema", function () {
189
410
  (0, vitest_1.it)("first stringifies schema if given an object", async () => {
411
+ const injectSchemaSpy = vitest_1.vi.spyOn(injectSchema, "injectSchema");
190
412
  const fetchMock = vitest_1.vi.fn().mockImplementation(() => {
191
413
  return {
192
414
  status: 200,
@@ -194,7 +416,7 @@ const markupFetcher_1 = require("./markupFetcher");
194
416
  text: () => "results!",
195
417
  };
196
418
  });
197
- const fetcher = (0, markupFetcher_1.markupFetcher)("test", fetchMock);
419
+ const fetcher = markupFetcher("test", fetchMock);
198
420
  const result = await fetcher({
199
421
  path: "/test",
200
422
  domain: "test.com",
@@ -202,10 +424,12 @@ const markupFetcher_1 = require("./markupFetcher");
202
424
  schema: { foo: "bar" },
203
425
  environment: "production",
204
426
  });
205
- (0, vitest_1.expect)(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?path=%2Ftest&domain=test.com&schema=%7B%22foo%22%3A%22bar%22%7D", vitest_1.expect.anything());
427
+ (0, vitest_1.expect)(injectSchemaSpy).toHaveBeenCalledWith("results!", { foo: "bar" });
428
+ (0, vitest_1.expect)(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?domain=test.com&path=%2Ftest", vitest_1.expect.anything());
206
429
  (0, vitest_1.expect)(result).toEqual("results!");
207
430
  });
208
431
  (0, vitest_1.it)("does not stringify schema if given a string", async () => {
432
+ const injectSchemaSpy = vitest_1.vi.spyOn(injectSchema, "injectSchema");
209
433
  const fetchMock = vitest_1.vi.fn().mockImplementation(() => {
210
434
  return {
211
435
  status: 200,
@@ -213,7 +437,7 @@ const markupFetcher_1 = require("./markupFetcher");
213
437
  text: () => "results!",
214
438
  };
215
439
  });
216
- const fetcher = (0, markupFetcher_1.markupFetcher)("test", fetchMock);
440
+ const fetcher = markupFetcher("test", fetchMock);
217
441
  const result = await fetcher({
218
442
  path: "/test",
219
443
  domain: "test.com",
@@ -221,7 +445,29 @@ const markupFetcher_1 = require("./markupFetcher");
221
445
  schema: '{"foo":"bar"}',
222
446
  environment: "production",
223
447
  });
224
- (0, vitest_1.expect)(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?path=%2Ftest&domain=test.com&schema=%7B%22foo%22%3A%22bar%22%7D", vitest_1.expect.anything());
448
+ (0, vitest_1.expect)(injectSchemaSpy).toHaveBeenCalledWith("results!", { foo: "bar" });
449
+ (0, vitest_1.expect)(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?domain=test.com&path=%2Ftest", vitest_1.expect.anything());
450
+ (0, vitest_1.expect)(result).toEqual("results!");
451
+ });
452
+ (0, vitest_1.it)("returns markup if invalid json is provided", async () => {
453
+ const injectSchemaSpy = vitest_1.vi.spyOn(injectSchema, "injectSchema");
454
+ const fetchMock = vitest_1.vi.fn().mockImplementation(() => {
455
+ return {
456
+ status: 200,
457
+ ok: true,
458
+ text: () => "results!",
459
+ };
460
+ });
461
+ const fetcher = markupFetcher("test", fetchMock);
462
+ const result = await fetcher({
463
+ path: "/test",
464
+ domain: "test.com",
465
+ apiKey: "123abc",
466
+ schema: "not-valid-json",
467
+ environment: "production",
468
+ });
469
+ (0, vitest_1.expect)(injectSchemaSpy).not.toHaveBeenCalled();
470
+ (0, vitest_1.expect)(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?domain=test.com&path=%2Ftest", vitest_1.expect.anything());
225
471
  (0, vitest_1.expect)(result).toEqual("results!");
226
472
  });
227
473
  });
package/dist/cjs/utils.js CHANGED
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.reAppendMarkup = exports.getEnvironment = exports.isValidTimezone = void 0;
3
+ exports.saveFile = exports.readFile = exports.toSavedFileName = exports.createTempDirectory = exports.deleteTempDirectory = exports.unescapeHTML = exports.parseJson = exports.reAppendMarkup = exports.getEnvironment = exports.isValidTimezone = void 0;
4
+ const fs_1 = require("fs");
5
+ const _1 = require(".");
4
6
  function isValidTimezone(tz) {
5
7
  try {
6
8
  Intl.DateTimeFormat(undefined, { timeZone: tz });
@@ -28,3 +30,79 @@ function reAppendMarkup(element, markup) {
28
30
  element.append(documentFragment);
29
31
  }
30
32
  exports.reAppendMarkup = reAppendMarkup;
33
+ function parseJson(json) {
34
+ try {
35
+ return JSON.parse(json);
36
+ }
37
+ catch (ex) {
38
+ return null;
39
+ }
40
+ }
41
+ exports.parseJson = parseJson;
42
+ function unescapeHTML(str) {
43
+ var htmlEntities = {
44
+ nbsp: " ",
45
+ cent: "¢",
46
+ pound: "£",
47
+ yen: "¥",
48
+ euro: "€",
49
+ copy: "©",
50
+ reg: "®",
51
+ lt: "<",
52
+ gt: ">",
53
+ quot: '"',
54
+ amp: "&",
55
+ apos: "'",
56
+ };
57
+ return str.replace(/\&([^;]+);/g, function (entity, entityCode) {
58
+ var match;
59
+ if (entityCode in htmlEntities) {
60
+ return htmlEntities[entityCode];
61
+ }
62
+ else if ((match = entityCode.match(/^#x([\da-fA-F]+)$/))) {
63
+ return String.fromCharCode(parseInt(match[1], 16));
64
+ }
65
+ else if ((match = entityCode.match(/^#(\d+)$/))) {
66
+ return String.fromCharCode(~~match[1]);
67
+ }
68
+ else {
69
+ return entity;
70
+ }
71
+ });
72
+ }
73
+ exports.unescapeHTML = unescapeHTML;
74
+ function deleteTempDirectory() {
75
+ if ((0, fs_1.existsSync)(_1.TEMP_DIRECTORY)) {
76
+ (0, fs_1.rmdirSync)(_1.TEMP_DIRECTORY, { recursive: true });
77
+ }
78
+ }
79
+ exports.deleteTempDirectory = deleteTempDirectory;
80
+ function createTempDirectory(mkdirSyncImpl = fs_1.mkdirSync, existsSyncImpl = fs_1.existsSync) {
81
+ if (!existsSyncImpl(_1.TEMP_DIRECTORY)) {
82
+ mkdirSyncImpl(_1.TEMP_DIRECTORY);
83
+ }
84
+ }
85
+ exports.createTempDirectory = createTempDirectory;
86
+ function toSavedFileName(name) {
87
+ return name.replace(/^\//, "").replace(/\//g, "::");
88
+ }
89
+ exports.toSavedFileName = toSavedFileName;
90
+ function readFile(name, readFileSyncImpl = fs_1.readFileSync) {
91
+ const fileName = toSavedFileName(name);
92
+ try {
93
+ return readFileSyncImpl(`${_1.TEMP_DIRECTORY}/${fileName}`, "utf8");
94
+ }
95
+ catch (error) {
96
+ return null;
97
+ }
98
+ }
99
+ exports.readFile = readFile;
100
+ function saveFile(name, data, writeFileImpl = fs_1.writeFile) {
101
+ const fileName = toSavedFileName(name);
102
+ return new Promise((resolve) => {
103
+ writeFileImpl(`${_1.TEMP_DIRECTORY}/${fileName}`, data, () => {
104
+ resolve();
105
+ });
106
+ });
107
+ }
108
+ exports.saveFile = saveFile;
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const vitest_1 = require("vitest");
4
4
  const utils_1 = require("./utils");
5
+ const _1 = require(".");
5
6
  const env = process.env;
6
7
  (0, vitest_1.beforeEach)(() => {
7
8
  vitest_1.vi.resetModules();
@@ -44,3 +45,45 @@ const env = process.env;
44
45
  });
45
46
  });
46
47
  });
48
+ (0, vitest_1.describe)("createTempDirectory()", () => {
49
+ (0, vitest_1.it)("creates temp directory fresh", () => {
50
+ const mkdirSyncMock = vitest_1.vi.fn();
51
+ const existsSyncMock = vitest_1.vi.fn().mockReturnValue(false);
52
+ (0, vitest_1.expect)(() => (0, utils_1.createTempDirectory)(mkdirSyncMock, existsSyncMock)).not.toThrow();
53
+ (0, vitest_1.expect)(mkdirSyncMock).toHaveBeenCalledWith(_1.TEMP_DIRECTORY);
54
+ });
55
+ (0, vitest_1.it)("does not throw error when it already exists", () => {
56
+ const mkdirSyncMock = vitest_1.vi.fn().mockImplementation(() => {
57
+ throw new Error("Directory already exists");
58
+ });
59
+ const existsSyncMock = vitest_1.vi.fn().mockReturnValue(true);
60
+ (0, vitest_1.expect)(() => (0, utils_1.createTempDirectory)(mkdirSyncMock, existsSyncMock)).not.toThrow();
61
+ (0, vitest_1.expect)(mkdirSyncMock).not.toHaveBeenCalledWith();
62
+ });
63
+ });
64
+ (0, vitest_1.describe)("toSavedFileName()", () => {
65
+ (0, vitest_1.it)("replaces slashes with double colons", () => {
66
+ (0, vitest_1.expect)((0, utils_1.toSavedFileName)("/hey/there")).toEqual("hey::there");
67
+ });
68
+ });
69
+ (0, vitest_1.describe)("readFile()", () => {
70
+ (0, vitest_1.it)("reads the file", () => {
71
+ const readFileSyncMock = vitest_1.vi.fn().mockReturnValue("file contents");
72
+ (0, vitest_1.expect)((0, utils_1.readFile)("hey/there", readFileSyncMock)).toEqual("file contents");
73
+ (0, vitest_1.expect)(readFileSyncMock).toHaveBeenCalledWith(`${_1.TEMP_DIRECTORY}/hey::there`, "utf8");
74
+ });
75
+ (0, vitest_1.it)("returns null when file doesn't exist", () => {
76
+ const readFileSyncMock = vitest_1.vi.fn().mockImplementation(() => {
77
+ throw new Error("File not found");
78
+ });
79
+ (0, vitest_1.expect)((0, utils_1.readFile)("hey/there", readFileSyncMock)).toEqual(null);
80
+ });
81
+ });
82
+ (0, vitest_1.describe)("saveFile()", () => {
83
+ (0, vitest_1.it)("saves the file", async () => {
84
+ const writeFileMock = vitest_1.vi.fn();
85
+ const result = (0, utils_1.saveFile)("hey/there", "file contents", writeFileMock);
86
+ (0, vitest_1.expect)(writeFileMock).toHaveBeenCalledWith(`${_1.TEMP_DIRECTORY}/hey::there`, "file contents", vitest_1.expect.any(Function));
87
+ (0, vitest_1.expect)(result).toBeInstanceOf(Promise);
88
+ });
89
+ });
package/dist/esm/index.js CHANGED
@@ -1,3 +1,5 @@
1
- export { markupFetcher } from "./markupFetcher";
2
- export { reAppendMarkup } from "./utils";
1
+ import path from "path";
2
+ export const TEMP_DIRECTORY = path.join(process.cwd(), "_temp_jc");
3
+ export { markupFetcher, fetchAll } from "./markupFetcher";
4
+ export { reAppendMarkup, deleteTempDirectory } from "./utils";
3
5
  export { log, logError } from "./log";
@@ -0,0 +1,15 @@
1
+ import { parseJson, unescapeHTML } from "./utils";
2
+ export function injectSchema(markup, schema) {
3
+ const commentSchema = markup.match(/<div jc-data="jcSchema" data-schema="(.*)"><\/div>/)?.[1];
4
+ if (!commentSchema) {
5
+ return markup;
6
+ }
7
+ const json = parseJson(unescapeHTML(commentSchema));
8
+ if (!json) {
9
+ return markup;
10
+ }
11
+ schema.comment = json;
12
+ return markup
13
+ .replace("<!-- JC:SCHEMA -->", `<script type="application/ld+json">${JSON.stringify(schema)}</script>`)
14
+ .replace(/<div jc-data="jcSchema" data-schema=".*"><\/div>(?:\n+)?/, "");
15
+ }