@jam-comments/server-utilities 4.2.0 → 4.3.1

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/cjs/index.js CHANGED
@@ -1,10 +1,17 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.logError = exports.log = exports.reAppendMarkup = exports.markupFetcher = void 0;
6
+ exports.logError = exports.log = exports.deleteTempDirectory = exports.reAppendMarkup = exports.fetchAll = exports.markupFetcher = exports.TEMP_DIRECTORY = void 0;
7
+ const path_1 = __importDefault(require("path"));
8
+ exports.TEMP_DIRECTORY = path_1.default.join(process.cwd(), "_temp_jc");
4
9
  var markupFetcher_1 = require("./markupFetcher");
5
10
  Object.defineProperty(exports, "markupFetcher", { enumerable: true, get: function () { return markupFetcher_1.markupFetcher; } });
11
+ Object.defineProperty(exports, "fetchAll", { enumerable: true, get: function () { return markupFetcher_1.fetchAll; } });
6
12
  var utils_1 = require("./utils");
7
13
  Object.defineProperty(exports, "reAppendMarkup", { enumerable: true, get: function () { return utils_1.reAppendMarkup; } });
14
+ Object.defineProperty(exports, "deleteTempDirectory", { enumerable: true, get: function () { return utils_1.deleteTempDirectory; } });
8
15
  var log_1 = require("./log");
9
16
  Object.defineProperty(exports, "log", { enumerable: true, get: function () { return log_1.log; } });
10
17
  Object.defineProperty(exports, "logError", { enumerable: true, get: function () { return log_1.logError; } });
@@ -1,40 +1,100 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.markupFetcher = void 0;
3
+ exports.markupFetcher = exports.makeMarkupRequest = exports.fetchFreshMarkup = exports.batchMarkupFetcher = exports.fetchAll = void 0;
4
4
  const injectSchema_1 = require("./injectSchema");
5
5
  const utils_1 = require("./utils");
6
- const markupFetcher = (platform, fetchImplementation = fetch) => {
7
- return async ({ tz = undefined, path, domain, apiKey, schema, baseUrl = "https://go.jamcomments.com", environment = (0, utils_1.getEnvironment)(), }) => {
8
- const trimmedTimezone = tz?.trim();
9
- if (trimmedTimezone && !(0, utils_1.isValidTimezone)(trimmedTimezone)) {
10
- throw new Error(`The timezone passed to JamComments is invalid: ${trimmedTimezone}`);
11
- }
12
- const params = new URLSearchParams({
13
- path: path || "/",
14
- domain,
15
- });
16
- if (trimmedTimezone) {
17
- params.set("tz", trimmedTimezone);
18
- }
19
- if (environment !== "production") {
20
- params.set("stub", "true");
21
- }
22
- const requestUrl = `${baseUrl}/api/v3/markup?${params}`;
23
- const response = await fetchImplementation(requestUrl, {
24
- method: "GET",
25
- headers: {
26
- Authorization: `Bearer ${apiKey}`,
27
- Accept: "application/json",
28
- "X-Platform": platform,
29
- },
30
- });
31
- if (response.status === 401) {
32
- throw new Error(`Unauthorized! Are your domain and JamComments API key set correctly?`);
33
- }
34
- if (!response.ok) {
35
- throw new Error(`JamComments request failed! Status code: ${response.status}, message: ${response.statusText}, request URL: ${requestUrl}`);
6
+ async function fetchAll({ tz = undefined, domain, apiKey, baseUrl = "https://go.jamcomments.com", environment = (0, utils_1.getEnvironment)(), }, platform, fetchImplementation = fetch, batchMarkupFetcherImpl = batchMarkupFetcher) {
7
+ const fetchBatchMarkup = batchMarkupFetcherImpl(platform, fetchImplementation);
8
+ (0, utils_1.createTempDirectory)();
9
+ let shouldKeepFetching = true;
10
+ let page = 1;
11
+ console.log("Fetching comments from JamComments! This might take a sec.");
12
+ try {
13
+ while (shouldKeepFetching) {
14
+ const { data: items, meta: { current_page, last_page }, } = await fetchBatchMarkup({
15
+ domain,
16
+ apiKey,
17
+ baseUrl,
18
+ environment,
19
+ page,
20
+ tz,
21
+ });
22
+ console.log(`Checking for comment data. Batch: ${current_page}/${last_page}`);
23
+ const saveMarkupPromises = items.map((item) => {
24
+ return (0, utils_1.saveFile)(item.path, item.markup);
25
+ });
26
+ await Promise.all(saveMarkupPromises);
27
+ if (current_page === last_page) {
28
+ shouldKeepFetching = false;
29
+ }
30
+ else {
31
+ page++;
32
+ }
36
33
  }
37
- const markup = await response.text();
34
+ }
35
+ catch (error) {
36
+ (0, utils_1.deleteTempDirectory)();
37
+ throw error;
38
+ }
39
+ }
40
+ exports.fetchAll = fetchAll;
41
+ function batchMarkupFetcher(platform, fetchImplementation = fetch) {
42
+ return async ({ tz = undefined, domain, apiKey, baseUrl = "https://go.jamcomments.com", environment = (0, utils_1.getEnvironment)(), page = 1, }) => {
43
+ const response = await makeMarkupRequest({ tz, domain, apiKey, baseUrl, environment, page }, "/api/v3/markup/all", fetchImplementation, platform);
44
+ return response.json();
45
+ };
46
+ }
47
+ exports.batchMarkupFetcher = batchMarkupFetcher;
48
+ async function fetchFreshMarkup({ tz = undefined, path, domain, apiKey, baseUrl = "https://go.jamcomments.com", environment = (0, utils_1.getEnvironment)(), }, fetchImplementation = fetch, platform) {
49
+ const response = await makeMarkupRequest({ tz, path, domain, apiKey, baseUrl, environment }, "/api/v3/markup", fetchImplementation, platform);
50
+ return response.text();
51
+ }
52
+ exports.fetchFreshMarkup = fetchFreshMarkup;
53
+ async function makeMarkupRequest({ tz = undefined, path = undefined, domain, apiKey, baseUrl = "https://go.jamcomments.com", environment = (0, utils_1.getEnvironment)(), page = undefined, }, baseServicePath, fetchImplementation = fetch, platform) {
54
+ const trimmedTimezone = tz?.trim();
55
+ if (trimmedTimezone && !(0, utils_1.isValidTimezone)(trimmedTimezone)) {
56
+ throw new Error(`The timezone passed to JamComments is invalid: ${trimmedTimezone}`);
57
+ }
58
+ const params = new URLSearchParams({
59
+ domain,
60
+ });
61
+ if (path) {
62
+ params.set("path", path);
63
+ }
64
+ if (page) {
65
+ params.set("page", page.toString());
66
+ }
67
+ if (trimmedTimezone) {
68
+ params.set("tz", trimmedTimezone);
69
+ }
70
+ if (environment !== "production") {
71
+ params.set("stub", "true");
72
+ }
73
+ const requestUrl = `${baseUrl}${baseServicePath}?${params}`;
74
+ const response = await fetchImplementation(requestUrl, {
75
+ method: "GET",
76
+ headers: {
77
+ Authorization: `Bearer ${apiKey}`,
78
+ Accept: "application/json",
79
+ "X-Platform": platform,
80
+ },
81
+ });
82
+ if (response.status === 401) {
83
+ throw new Error(`Unauthorized! Are your domain and JamComments API key set correctly?`);
84
+ }
85
+ if (!response.ok) {
86
+ throw new Error(`JamComments request failed! Status code: ${response.status}, message: ${response.statusText}, request URL: ${requestUrl}`);
87
+ }
88
+ return response;
89
+ }
90
+ exports.makeMarkupRequest = makeMarkupRequest;
91
+ function markupFetcher(platform, fetchImplementation = fetch) {
92
+ return async ({ tz = undefined, path, domain, apiKey, schema, baseUrl = "https://go.jamcomments.com", environment = (0, utils_1.getEnvironment)(), }) => {
93
+ path = path || "/";
94
+ const savedFile = (0, utils_1.readFile)(path);
95
+ const markup = savedFile
96
+ ? savedFile
97
+ : await fetchFreshMarkup({ tz, path, domain, apiKey, baseUrl, environment }, fetchImplementation, platform);
38
98
  if (schema) {
39
99
  const preparedSchema = typeof schema !== "string" ? JSON.stringify(schema) : schema;
40
100
  const parsedSchema = (0, utils_1.parseJson)(preparedSchema);
@@ -45,5 +105,5 @@ const markupFetcher = (platform, fetchImplementation = fetch) => {
45
105
  }
46
106
  return markup;
47
107
  };
48
- };
108
+ }
49
109
  exports.markupFetcher = markupFetcher;
@@ -25,7 +25,202 @@ var __importStar = (this && this.__importStar) || function (mod) {
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
26
  const vitest_1 = require("vitest");
27
27
  const injectSchema = __importStar(require("./injectSchema"));
28
- const markupFetcher_1 = require("./markupFetcher");
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
+ });
29
224
  (0, vitest_1.describe)("markupFetcher", () => {
30
225
  (0, vitest_1.it)("constructs fetch request correctly", async () => {
31
226
  const injectSchemaSpy = vitest_1.vi.spyOn(injectSchema, "injectSchema");
@@ -36,7 +231,7 @@ const markupFetcher_1 = require("./markupFetcher");
36
231
  text: () => "results!",
37
232
  };
38
233
  });
39
- const fetcher = (0, markupFetcher_1.markupFetcher)("test", fetchMock);
234
+ const fetcher = markupFetcher("test", fetchMock);
40
235
  const result = await fetcher({
41
236
  path: "/test",
42
237
  domain: "test.com",
@@ -44,7 +239,7 @@ const markupFetcher_1 = require("./markupFetcher");
44
239
  environment: "production",
45
240
  });
46
241
  (0, vitest_1.expect)(injectSchemaSpy).not.toHaveBeenCalled();
47
- (0, vitest_1.expect)(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?path=%2Ftest&domain=test.com", vitest_1.expect.objectContaining({
242
+ (0, vitest_1.expect)(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?domain=test.com&path=%2Ftest", vitest_1.expect.objectContaining({
48
243
  headers: vitest_1.expect.objectContaining({
49
244
  Accept: "application/json",
50
245
  Authorization: "Bearer 123abc",
@@ -62,14 +257,14 @@ const markupFetcher_1 = require("./markupFetcher");
62
257
  text: () => "results!",
63
258
  };
64
259
  });
65
- const fetcher = (0, markupFetcher_1.markupFetcher)("test", fetchMock);
260
+ const fetcher = markupFetcher("test", fetchMock);
66
261
  const result = await fetcher({
67
262
  path: "/test",
68
263
  domain: "test.com",
69
264
  apiKey: "123abc",
70
265
  environment: "development",
71
266
  });
72
- (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());
73
268
  (0, vitest_1.expect)(result).toEqual("results!");
74
269
  });
75
270
  (0, vitest_1.it)("uses different base URL", async () => {
@@ -80,7 +275,7 @@ const markupFetcher_1 = require("./markupFetcher");
80
275
  text: () => "results!",
81
276
  };
82
277
  });
83
- const fetcher = (0, markupFetcher_1.markupFetcher)("test", fetchMock);
278
+ const fetcher = markupFetcher("test", fetchMock);
84
279
  const result = await fetcher({
85
280
  path: "/test",
86
281
  domain: "test.com",
@@ -88,7 +283,7 @@ const markupFetcher_1 = require("./markupFetcher");
88
283
  baseUrl: "http://ur-mom.com",
89
284
  environment: "production",
90
285
  });
91
- (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());
92
287
  (0, vitest_1.expect)(result).toEqual("results!");
93
288
  });
94
289
  (0, vitest_1.it)("respects production!!!", async () => {
@@ -99,7 +294,7 @@ const markupFetcher_1 = require("./markupFetcher");
99
294
  text: () => "results!",
100
295
  };
101
296
  });
102
- const fetcher = (0, markupFetcher_1.markupFetcher)("test", fetchMock);
297
+ const fetcher = markupFetcher("test", fetchMock);
103
298
  const result = await fetcher({
104
299
  path: "/test",
105
300
  domain: "test.com",
@@ -107,7 +302,7 @@ const markupFetcher_1 = require("./markupFetcher");
107
302
  baseUrl: "http://ur-mom.com",
108
303
  environment: "production",
109
304
  });
110
- (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());
111
306
  (0, vitest_1.expect)(result).toEqual("results!");
112
307
  });
113
308
  (0, vitest_1.it)("falls back to root path", async () => {
@@ -118,14 +313,14 @@ const markupFetcher_1 = require("./markupFetcher");
118
313
  text: () => "results!",
119
314
  };
120
315
  });
121
- const fetcher = (0, markupFetcher_1.markupFetcher)("test", fetchMock);
316
+ const fetcher = markupFetcher("test", fetchMock);
122
317
  const result = await fetcher({
123
318
  path: null,
124
319
  domain: "test.com",
125
320
  apiKey: "123abc",
126
321
  environment: "production",
127
322
  });
128
- (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());
129
324
  (0, vitest_1.expect)(result).toEqual("results!");
130
325
  });
131
326
  (0, vitest_1.it)("credentials are invalid", async () => {
@@ -136,7 +331,7 @@ const markupFetcher_1 = require("./markupFetcher");
136
331
  text: () => "bad results!",
137
332
  };
138
333
  });
139
- const fetcher = (0, markupFetcher_1.markupFetcher)("test", fetchMock);
334
+ const fetcher = markupFetcher("test", fetchMock);
140
335
  (0, vitest_1.expect)(fetcher({
141
336
  path: "/test",
142
337
  domain: "test.com",
@@ -151,7 +346,7 @@ const markupFetcher_1 = require("./markupFetcher");
151
346
  text: () => "bad results!",
152
347
  };
153
348
  });
154
- const fetcher = (0, markupFetcher_1.markupFetcher)("test", fetchMock);
349
+ const fetcher = markupFetcher("test", fetchMock);
155
350
  (0, vitest_1.expect)(fetcher({
156
351
  path: "/test",
157
352
  domain: "test.com",
@@ -160,9 +355,9 @@ const markupFetcher_1 = require("./markupFetcher");
160
355
  });
161
356
  (0, vitest_1.describe)("timezone validation", () => {
162
357
  (0, vitest_1.it)("throws error when invalid timezone is provided", async () => {
163
- const fetchMock = vitest_1.vi.fn();
164
- const fetcher = (0, markupFetcher_1.markupFetcher)("test", fetchMock);
165
- (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();
166
361
  (0, vitest_1.expect)(fetcher({
167
362
  path: "/test",
168
363
  domain: "test.com",
@@ -179,15 +374,15 @@ const markupFetcher_1 = require("./markupFetcher");
179
374
  text: () => "results!",
180
375
  };
181
376
  });
182
- const fetcher = (0, markupFetcher_1.markupFetcher)("test", fetchMock);
377
+ const fetcher = markupFetcher("test", fetchMock);
183
378
  const result = await fetcher({
184
- path: null,
379
+ path: "/some/path",
185
380
  domain: "test.com",
186
381
  apiKey: "123abc",
187
382
  tz: "America/New_York",
188
383
  environment: "production",
189
384
  });
190
- (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());
191
386
  (0, vitest_1.expect)(result).toEqual("results!");
192
387
  });
193
388
  (0, vitest_1.it)("trims a valid timezone", async () => {
@@ -198,15 +393,15 @@ const markupFetcher_1 = require("./markupFetcher");
198
393
  text: () => "results!",
199
394
  };
200
395
  });
201
- const fetcher = (0, markupFetcher_1.markupFetcher)("test", fetchMock);
396
+ const fetcher = markupFetcher("test", fetchMock);
202
397
  const result = await fetcher({
203
- path: null,
398
+ path: "/some/path",
204
399
  domain: "test.com",
205
400
  apiKey: "123abc",
206
401
  tz: " America/Chicago ",
207
402
  environment: "production",
208
403
  });
209
- (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());
210
405
  (0, vitest_1.expect)(result).toEqual("results!");
211
406
  });
212
407
  });
@@ -221,7 +416,7 @@ const markupFetcher_1 = require("./markupFetcher");
221
416
  text: () => "results!",
222
417
  };
223
418
  });
224
- const fetcher = (0, markupFetcher_1.markupFetcher)("test", fetchMock);
419
+ const fetcher = markupFetcher("test", fetchMock);
225
420
  const result = await fetcher({
226
421
  path: "/test",
227
422
  domain: "test.com",
@@ -230,7 +425,7 @@ const markupFetcher_1 = require("./markupFetcher");
230
425
  environment: "production",
231
426
  });
232
427
  (0, vitest_1.expect)(injectSchemaSpy).toHaveBeenCalledWith("results!", { foo: "bar" });
233
- (0, vitest_1.expect)(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?path=%2Ftest&domain=test.com", vitest_1.expect.anything());
428
+ (0, vitest_1.expect)(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?domain=test.com&path=%2Ftest", vitest_1.expect.anything());
234
429
  (0, vitest_1.expect)(result).toEqual("results!");
235
430
  });
236
431
  (0, vitest_1.it)("does not stringify schema if given a string", async () => {
@@ -242,7 +437,7 @@ const markupFetcher_1 = require("./markupFetcher");
242
437
  text: () => "results!",
243
438
  };
244
439
  });
245
- const fetcher = (0, markupFetcher_1.markupFetcher)("test", fetchMock);
440
+ const fetcher = markupFetcher("test", fetchMock);
246
441
  const result = await fetcher({
247
442
  path: "/test",
248
443
  domain: "test.com",
@@ -251,7 +446,7 @@ const markupFetcher_1 = require("./markupFetcher");
251
446
  environment: "production",
252
447
  });
253
448
  (0, vitest_1.expect)(injectSchemaSpy).toHaveBeenCalledWith("results!", { foo: "bar" });
254
- (0, vitest_1.expect)(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?path=%2Ftest&domain=test.com", vitest_1.expect.anything());
449
+ (0, vitest_1.expect)(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?domain=test.com&path=%2Ftest", vitest_1.expect.anything());
255
450
  (0, vitest_1.expect)(result).toEqual("results!");
256
451
  });
257
452
  (0, vitest_1.it)("returns markup if invalid json is provided", async () => {
@@ -263,7 +458,7 @@ const markupFetcher_1 = require("./markupFetcher");
263
458
  text: () => "results!",
264
459
  };
265
460
  });
266
- const fetcher = (0, markupFetcher_1.markupFetcher)("test", fetchMock);
461
+ const fetcher = markupFetcher("test", fetchMock);
267
462
  const result = await fetcher({
268
463
  path: "/test",
269
464
  domain: "test.com",
@@ -272,7 +467,7 @@ const markupFetcher_1 = require("./markupFetcher");
272
467
  environment: "production",
273
468
  });
274
469
  (0, vitest_1.expect)(injectSchemaSpy).not.toHaveBeenCalled();
275
- (0, vitest_1.expect)(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?path=%2Ftest&domain=test.com", vitest_1.expect.anything());
470
+ (0, vitest_1.expect)(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?domain=test.com&path=%2Ftest", vitest_1.expect.anything());
276
471
  (0, vitest_1.expect)(result).toEqual("results!");
277
472
  });
278
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.unescapeHTML = exports.parseJson = 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 });
@@ -37,21 +39,21 @@ function parseJson(json) {
37
39
  }
38
40
  }
39
41
  exports.parseJson = parseJson;
40
- var htmlEntities = {
41
- nbsp: " ",
42
- cent: "¢",
43
- pound: "£",
44
- yen: "¥",
45
- euro: "€",
46
- copy: "©",
47
- reg: "®",
48
- lt: "<",
49
- gt: ">",
50
- quot: '"',
51
- amp: "&",
52
- apos: "'",
53
- };
54
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
+ };
55
57
  return str.replace(/\&([^;]+);/g, function (entity, entityCode) {
56
58
  var match;
57
59
  if (entityCode in htmlEntities) {
@@ -69,3 +71,38 @@ function unescapeHTML(str) {
69
71
  });
70
72
  }
71
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;