@jam-comments/server-utilities 5.9.0 → 5.10.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.
package/dist/cjs/index.js CHANGED
@@ -3,9 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.store = exports.logError = exports.log = exports.removeFalseyValues = exports.fetchAll = exports.markupFetcher = void 0;
7
- const store_1 = __importDefault(require("./store"));
8
- exports.store = store_1.default;
6
+ exports.Store = exports.logError = exports.log = exports.removeFalseyValues = exports.fetchAll = exports.markupFetcher = void 0;
9
7
  var markupFetcher_1 = require("./markupFetcher");
10
8
  Object.defineProperty(exports, "markupFetcher", { enumerable: true, get: function () { return markupFetcher_1.markupFetcher; } });
11
9
  Object.defineProperty(exports, "fetchAll", { enumerable: true, get: function () { return markupFetcher_1.fetchAll; } });
@@ -14,3 +12,5 @@ Object.defineProperty(exports, "removeFalseyValues", { enumerable: true, get: fu
14
12
  var log_1 = require("./log");
15
13
  Object.defineProperty(exports, "log", { enumerable: true, get: function () { return log_1.log; } });
16
14
  Object.defineProperty(exports, "logError", { enumerable: true, get: function () { return log_1.logError; } });
15
+ var store_1 = require("./store");
16
+ Object.defineProperty(exports, "Store", { enumerable: true, get: function () { return __importDefault(store_1).default; } });
@@ -12,9 +12,8 @@ const injectSchema_1 = require("./injectSchema");
12
12
  const utils_1 = require("./utils");
13
13
  const store_1 = __importDefault(require("./store"));
14
14
  const BASE_URL = "https://go.jamcomments.com";
15
- async function fetchAll({ tz = undefined, dateFormat = undefined, domain, apiKey, baseUrl = BASE_URL, environment = (0, utils_1.getEnvironment)(), copy = {}, }, platform, fetchImplementation = fetch, batchMarkupFetcherImpl = batchMarkupFetcher) {
15
+ async function fetchAll({ tz = undefined, dateFormat = undefined, domain, apiKey, baseUrl = BASE_URL, environment = (0, utils_1.getEnvironment)(), copy = {}, }, platform, fetchImplementation = fetch, batchMarkupFetcherImpl = batchMarkupFetcher, store = new store_1.default()) {
16
16
  const fetchBatchMarkup = batchMarkupFetcherImpl(platform, fetchImplementation);
17
- store_1.default.init();
18
17
  let shouldKeepFetching = true;
19
18
  let page = 1;
20
19
  console.log("Fetching comments from JamComments! This might take a sec.");
@@ -32,7 +31,7 @@ async function fetchAll({ tz = undefined, dateFormat = undefined, domain, apiKey
32
31
  });
33
32
  console.log(`Checking for comment data. Batch: ${current_page}/${last_page}`);
34
33
  items.forEach((item) => {
35
- store_1.default.set(item.path, item.markup);
34
+ store.set(item.path, item.markup);
36
35
  });
37
36
  if (current_page === last_page) {
38
37
  shouldKeepFetching = false;
@@ -43,9 +42,10 @@ async function fetchAll({ tz = undefined, dateFormat = undefined, domain, apiKey
43
42
  }
44
43
  }
45
44
  catch (error) {
46
- store_1.default.clear();
45
+ store.clear();
47
46
  throw error;
48
47
  }
48
+ return store.store;
49
49
  }
50
50
  function batchMarkupFetcher(platform, fetchImplementation = fetch) {
51
51
  return async ({ tz, copy, domain, apiKey, dateFormat, baseUrl = BASE_URL, environment = (0, utils_1.getEnvironment)(), page = 1, }) => {
@@ -100,19 +100,17 @@ async function makeMarkupRequest({ tz, path, page, domain, apiKey, dateFormat, c
100
100
  }
101
101
  return response;
102
102
  }
103
- function markupFetcher(platform, fetchImplementation = fetch) {
103
+ function markupFetcher(platform, fetchImplementation = fetch, store = new store_1.default()) {
104
104
  return async ({ tz = undefined, path, domain, apiKey, schema, dateFormat, baseUrl = BASE_URL, environment = (0, utils_1.getEnvironment)(), copy = {}, }) => {
105
105
  path = path || "/";
106
- const savedFile = (() => {
107
- if (!store_1.default.isInitialized()) {
108
- console.log("Store is not initialized.");
106
+ const cachedMarkup = (() => {
107
+ if (!store.hasData) {
109
108
  return null;
110
109
  }
111
- console.log("Getting cached comment markup");
112
- return store_1.default.get(path) || store_1.default.getEmptyMarkup();
110
+ return store.get(path) || store.emptyMarkup;
113
111
  })();
114
- const markup = savedFile
115
- ? savedFile
112
+ const markup = cachedMarkup
113
+ ? cachedMarkup
116
114
  : await fetchFreshMarkup({ tz, path, domain, apiKey, baseUrl, environment, copy, dateFormat }, fetchImplementation, platform);
117
115
  if (schema) {
118
116
  const preparedSchema = typeof schema !== "string" ? JSON.stringify(schema) : schema;
@@ -22,25 +22,23 @@ var __importStar = (this && this.__importStar) || function (mod) {
22
22
  __setModuleDefault(result, mod);
23
23
  return result;
24
24
  };
25
- var __importDefault = (this && this.__importDefault) || function (mod) {
26
- return (mod && mod.__esModule) ? mod : { "default": mod };
27
- };
28
25
  Object.defineProperty(exports, "__esModule", { value: true });
29
26
  const vitest_1 = require("vitest");
30
27
  const injectSchema = __importStar(require("./injectSchema"));
31
28
  const fetcherExports = __importStar(require("./markupFetcher"));
32
29
  const node_test_1 = require("node:test");
33
- const store_1 = __importDefault(require("./store"));
34
30
  const { markupFetcher, batchMarkupFetcher } = fetcherExports;
35
31
  (0, vitest_1.beforeEach)(() => {
36
- store_1.default.clear();
32
+ globalThis.jamCommentsStore = new Map();
37
33
  });
38
34
  (0, node_test_1.afterEach)(() => {
39
35
  vitest_1.vi.resetAllMocks();
40
36
  });
41
37
  (0, vitest_1.describe)("fetchAll()", () => {
42
38
  (0, vitest_1.it)("fetches all comments in a single request", async () => {
43
- const saveFileSpy = vitest_1.vi.spyOn(store_1.default, "set");
39
+ const store = {
40
+ set: vitest_1.vi.fn(),
41
+ };
44
42
  const mockBatchFetcher = vitest_1.vi.fn().mockReturnValue({
45
43
  data: [
46
44
  { path: "/test", markup: "markup1" },
@@ -63,7 +61,7 @@ const { markupFetcher, batchMarkupFetcher } = fetcherExports;
63
61
  domain: "test.com",
64
62
  apiKey: "123abc",
65
63
  environment: "production",
66
- }, "test_platform", vitest_1.vi.fn(), batchMarkupFetcherMock);
64
+ }, "test_platform", vitest_1.vi.fn(), batchMarkupFetcherMock, store);
67
65
  (0, vitest_1.expect)(batchMarkupFetcherMock).toHaveBeenCalledWith("test_platform", vitest_1.expect.anything());
68
66
  (0, vitest_1.expect)(mockBatchFetcher).toHaveBeenCalledWith({
69
67
  domain: "test.com",
@@ -75,12 +73,14 @@ const { markupFetcher, batchMarkupFetcher } = fetcherExports;
75
73
  page: 1,
76
74
  tz: undefined,
77
75
  });
78
- (0, vitest_1.expect)(saveFileSpy).toHaveBeenCalledTimes(2);
79
- (0, vitest_1.expect)(saveFileSpy).toHaveBeenCalledWith("/test", "markup1");
80
- (0, vitest_1.expect)(saveFileSpy).toHaveBeenCalledWith("/test2", "markup2");
76
+ (0, vitest_1.expect)(store.set).toHaveBeenCalledTimes(2);
77
+ (0, vitest_1.expect)(store.set).toHaveBeenCalledWith("/test", "markup1");
78
+ (0, vitest_1.expect)(store.set).toHaveBeenCalledWith("/test2", "markup2");
81
79
  });
82
80
  (0, vitest_1.it)("fetches all comments in multiple requests", async () => {
83
- const saveFileSpy = vitest_1.vi.spyOn(store_1.default, "set");
81
+ const store = {
82
+ set: vitest_1.vi.fn(),
83
+ };
84
84
  const mockBatchFetcher = vitest_1.vi
85
85
  .fn()
86
86
  .mockReturnValueOnce({
@@ -118,7 +118,7 @@ const { markupFetcher, batchMarkupFetcher } = fetcherExports;
118
118
  domain: "test.com",
119
119
  apiKey: "123abc",
120
120
  environment: "production",
121
- }, "test_platform", vitest_1.vi.fn(), batchMarkupFetcherMock);
121
+ }, "test_platform", vitest_1.vi.fn(), batchMarkupFetcherMock, store);
122
122
  (0, vitest_1.expect)(batchMarkupFetcherMock).toHaveBeenCalledWith("test_platform", vitest_1.expect.anything());
123
123
  (0, vitest_1.expect)(mockBatchFetcher).toHaveBeenCalledWith({
124
124
  domain: "test.com",
@@ -141,12 +141,14 @@ const { markupFetcher, batchMarkupFetcher } = fetcherExports;
141
141
  page: 2,
142
142
  tz: undefined,
143
143
  });
144
- (0, vitest_1.expect)(saveFileSpy).toHaveBeenCalledTimes(4);
145
- (0, vitest_1.expect)(saveFileSpy).toHaveBeenCalledWith("/test", "markup1");
146
- (0, vitest_1.expect)(saveFileSpy).toHaveBeenCalledWith("/test2", "markup2");
144
+ (0, vitest_1.expect)(store.set).toHaveBeenCalledTimes(4);
145
+ (0, vitest_1.expect)(store.set).toHaveBeenCalledWith("/test", "markup1");
146
+ (0, vitest_1.expect)(store.set).toHaveBeenCalledWith("/test2", "markup2");
147
147
  });
148
148
  (0, vitest_1.it)("deletes the temp directory of anything fails", async () => {
149
- const deleteTempDirectorySpy = vitest_1.vi.spyOn(store_1.default, "clear");
149
+ const store = {
150
+ clear: vitest_1.vi.fn(),
151
+ };
150
152
  const mockBatchFetcher = vitest_1.vi.fn().mockImplementation(() => {
151
153
  throw new Error("test error");
152
154
  });
@@ -158,15 +160,17 @@ const { markupFetcher, batchMarkupFetcher } = fetcherExports;
158
160
  domain: "test.com",
159
161
  apiKey: "123abc",
160
162
  environment: "production",
161
- }, "test_platform", vitest_1.vi.fn(), batchMarkupFetcherMock);
163
+ }, "test_platform", vitest_1.vi.fn(), batchMarkupFetcherMock, store);
162
164
  }
163
165
  catch (e) {
164
166
  (0, vitest_1.expect)(e.message).toEqual("test error");
165
167
  }
166
- (0, vitest_1.expect)(deleteTempDirectorySpy).toHaveBeenCalledOnce();
168
+ (0, vitest_1.expect)(store.clear).toHaveBeenCalledOnce();
167
169
  });
168
170
  (0, vitest_1.it)("handles copy overrides", async () => {
169
- const saveFileSpy = vitest_1.vi.spyOn(store_1.default, "set");
171
+ const store = {
172
+ set: vitest_1.vi.fn(),
173
+ };
170
174
  const mockBatchFetcher = vitest_1.vi.fn().mockReturnValue({
171
175
  data: [
172
176
  { path: "/test", markup: "markup1" },
@@ -193,7 +197,7 @@ const { markupFetcher, batchMarkupFetcher } = fetcherExports;
193
197
  copy_confirmation_message: "custom confirmation message",
194
198
  copy_submit_button: "custom submit button",
195
199
  },
196
- }, "test_platform", vitest_1.vi.fn(), batchMarkupFetcherMock);
200
+ }, "test_platform", vitest_1.vi.fn(), batchMarkupFetcherMock, store);
197
201
  (0, vitest_1.expect)(batchMarkupFetcherMock).toHaveBeenCalledWith("test_platform", vitest_1.expect.anything());
198
202
  (0, vitest_1.expect)(mockBatchFetcher).toHaveBeenCalledWith({
199
203
  domain: "test.com",
@@ -207,7 +211,7 @@ const { markupFetcher, batchMarkupFetcher } = fetcherExports;
207
211
  page: 1,
208
212
  tz: undefined,
209
213
  });
210
- (0, vitest_1.expect)(saveFileSpy).toHaveBeenCalledTimes(2);
214
+ (0, vitest_1.expect)(store.set).toHaveBeenCalledTimes(2);
211
215
  });
212
216
  (0, vitest_1.it)("allows you to pass a custom date format", async () => {
213
217
  const mockBatchFetcher = vitest_1.vi.fn().mockReturnValue({
@@ -498,10 +502,10 @@ const { markupFetcher, batchMarkupFetcher } = fetcherExports;
498
502
  });
499
503
  (0, vitest_1.describe)("using saved markup", () => {
500
504
  (0, vitest_1.it)("makes fresh request when temp directory does not exist", async () => {
501
- const tempDirectoryExistsSpy = vitest_1.vi
502
- .spyOn(store_1.default, "isInitialized")
503
- .mockReturnValue(false);
504
- const readFileSpy = vitest_1.vi.spyOn(store_1.default, "get");
505
+ const store = {
506
+ hasData: false,
507
+ get: vitest_1.vi.fn().mockReturnValue(null),
508
+ };
505
509
  const fetchMock = vitest_1.vi.fn().mockImplementation(() => {
506
510
  return {
507
511
  status: 200,
@@ -516,50 +520,42 @@ const { markupFetcher, batchMarkupFetcher } = fetcherExports;
516
520
  apiKey: "123abc",
517
521
  environment: "production",
518
522
  });
519
- (0, vitest_1.expect)(tempDirectoryExistsSpy).toHaveBeenCalledOnce();
520
- (0, vitest_1.expect)(readFileSpy).not.toHaveBeenCalled();
523
+ (0, vitest_1.expect)(store.get).not.toHaveBeenCalled();
521
524
  (0, vitest_1.expect)(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?domain=test.com&path=%2Ftest", vitest_1.expect.anything());
522
525
  (0, vitest_1.expect)(result).toEqual("results!");
523
526
  });
524
527
  (0, vitest_1.it)("uses saved markup when it exists", async () => {
525
- const tempDirectoryExistsSpy = vitest_1.vi
526
- .spyOn(store_1.default, "isInitialized")
527
- .mockReturnValue(true);
528
- const readFileSpy = vitest_1.vi
529
- .spyOn(store_1.default, "get")
530
- .mockReturnValue("saved markup");
528
+ const store = {
529
+ hasData: true,
530
+ get: vitest_1.vi.fn().mockReturnValue("saved markup"),
531
+ };
531
532
  const fetchMock = vitest_1.vi.fn();
532
- const fetcher = markupFetcher("test", fetchMock);
533
+ const fetcher = markupFetcher("test", fetchMock, store);
533
534
  const result = await fetcher({
534
535
  path: "/test",
535
536
  domain: "test.com",
536
537
  apiKey: "123abc",
537
538
  environment: "production",
538
539
  });
539
- (0, vitest_1.expect)(tempDirectoryExistsSpy).toHaveBeenCalledOnce();
540
- (0, vitest_1.expect)(readFileSpy).toHaveBeenCalledOnce();
540
+ (0, vitest_1.expect)(store.get).toHaveBeenCalledOnce();
541
541
  (0, vitest_1.expect)(fetchMock).not.toHaveBeenCalled();
542
542
  (0, vitest_1.expect)(result).toEqual("saved markup");
543
543
  });
544
544
  (0, vitest_1.it)("uses the EMPTY template when there's no saved file for a post", async () => {
545
- const tempDirectoryExistsSpy = vitest_1.vi
546
- .spyOn(store_1.default, "isInitialized")
547
- .mockReturnValue(true);
548
- const readFileSpy = vitest_1.vi.spyOn(store_1.default, "get").mockReturnValue(null);
549
- const getEmptyMarkupSpy = vitest_1.vi
550
- .spyOn(store_1.default, "getEmptyMarkup")
551
- .mockReturnValue("<!-- EMPTY -->");
545
+ const store = {
546
+ get: vitest_1.vi.fn().mockReturnValue(null),
547
+ emptyMarkup: "<!-- EMPTY -->",
548
+ hasData: true,
549
+ };
552
550
  const fetchMock = vitest_1.vi.fn();
553
- const fetcher = markupFetcher("test", fetchMock);
551
+ const fetcher = markupFetcher("test", fetchMock, store);
554
552
  const result = await fetcher({
555
553
  path: "/test",
556
554
  domain: "test.com",
557
555
  apiKey: "123abc",
558
556
  environment: "production",
559
557
  });
560
- (0, vitest_1.expect)(tempDirectoryExistsSpy).toHaveBeenCalledOnce();
561
- (0, vitest_1.expect)(readFileSpy).toHaveBeenCalledOnce();
562
- (0, vitest_1.expect)(getEmptyMarkupSpy).toHaveBeenCalledOnce();
558
+ (0, vitest_1.expect)(store.get).toHaveBeenCalledOnce();
563
559
  (0, vitest_1.expect)(fetchMock).not.toHaveBeenCalled();
564
560
  (0, vitest_1.expect)(result).toEqual("<!-- EMPTY -->");
565
561
  });
package/dist/cjs/store.js CHANGED
@@ -1,32 +1,26 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- let STORE = null;
4
- const store = {
5
- init() {
6
- if (!STORE) {
7
- STORE = new Map();
8
- }
9
- },
10
- isInitialized() {
11
- return STORE !== null;
12
- },
3
+ class Store {
4
+ store;
5
+ constructor() {
6
+ globalThis.jamCommentsStore =
7
+ globalThis.jamCommentsStore || new Map();
8
+ this.store = globalThis.jamCommentsStore;
9
+ }
13
10
  set(path, markup) {
14
- STORE.set(store.toPathKey(path), markup);
15
- },
11
+ this.store.set(path, markup);
12
+ }
16
13
  get(path) {
17
- return STORE.get(store.toPathKey(path)) || null;
18
- },
14
+ return this.store.get(path) || null;
15
+ }
19
16
  clear() {
20
- if (STORE) {
21
- STORE.clear();
22
- STORE = null;
23
- }
24
- },
25
- getEmptyMarkup() {
26
- return STORE.get("EMPTY");
27
- },
28
- toPathKey(path) {
29
- return path.replace(/^\//, "").replace(/\//g, "::");
30
- },
31
- };
32
- exports.default = store;
17
+ this.store.clear();
18
+ }
19
+ get hasData() {
20
+ return this.store.size > 0;
21
+ }
22
+ get emptyMarkup() {
23
+ return this.store.get("EMPTY") || null;
24
+ }
25
+ }
26
+ exports.default = Store;
package/dist/esm/index.js CHANGED
@@ -1,5 +1,4 @@
1
- import store from "./store";
2
1
  export { markupFetcher, fetchAll } from "./markupFetcher";
3
2
  export { removeFalseyValues } from "./utils";
4
3
  export { log, logError } from "./log";
5
- export { store };
4
+ export { default as Store } from "./store";
@@ -1,10 +1,9 @@
1
1
  import { injectSchema } from "./injectSchema";
2
2
  import { getEnvironment, isValidTimezone, parseJson } from "./utils";
3
- import store from "./store";
3
+ import Store from "./store";
4
4
  const BASE_URL = "https://go.jamcomments.com";
5
- export async function fetchAll({ tz = undefined, dateFormat = undefined, domain, apiKey, baseUrl = BASE_URL, environment = getEnvironment(), copy = {}, }, platform, fetchImplementation = fetch, batchMarkupFetcherImpl = batchMarkupFetcher) {
5
+ export async function fetchAll({ tz = undefined, dateFormat = undefined, domain, apiKey, baseUrl = BASE_URL, environment = getEnvironment(), copy = {}, }, platform, fetchImplementation = fetch, batchMarkupFetcherImpl = batchMarkupFetcher, store = new Store()) {
6
6
  const fetchBatchMarkup = batchMarkupFetcherImpl(platform, fetchImplementation);
7
- store.init();
8
7
  let shouldKeepFetching = true;
9
8
  let page = 1;
10
9
  console.log("Fetching comments from JamComments! This might take a sec.");
@@ -36,6 +35,7 @@ export async function fetchAll({ tz = undefined, dateFormat = undefined, domain,
36
35
  store.clear();
37
36
  throw error;
38
37
  }
38
+ return store.store;
39
39
  }
40
40
  export function batchMarkupFetcher(platform, fetchImplementation = fetch) {
41
41
  return async ({ tz, copy, domain, apiKey, dateFormat, baseUrl = BASE_URL, environment = getEnvironment(), page = 1, }) => {
@@ -90,19 +90,17 @@ export async function makeMarkupRequest({ tz, path, page, domain, apiKey, dateFo
90
90
  }
91
91
  return response;
92
92
  }
93
- export function markupFetcher(platform, fetchImplementation = fetch) {
93
+ export function markupFetcher(platform, fetchImplementation = fetch, store = new Store()) {
94
94
  return async ({ tz = undefined, path, domain, apiKey, schema, dateFormat, baseUrl = BASE_URL, environment = getEnvironment(), copy = {}, }) => {
95
95
  path = path || "/";
96
- const savedFile = (() => {
97
- if (!store.isInitialized()) {
98
- console.log("Store is not initialized.");
96
+ const cachedMarkup = (() => {
97
+ if (!store.hasData) {
99
98
  return null;
100
99
  }
101
- console.log("Getting cached comment markup");
102
- return store.get(path) || store.getEmptyMarkup();
100
+ return store.get(path) || store.emptyMarkup;
103
101
  })();
104
- const markup = savedFile
105
- ? savedFile
102
+ const markup = cachedMarkup
103
+ ? cachedMarkup
106
104
  : await fetchFreshMarkup({ tz, path, domain, apiKey, baseUrl, environment, copy, dateFormat }, fetchImplementation, platform);
107
105
  if (schema) {
108
106
  const preparedSchema = typeof schema !== "string" ? JSON.stringify(schema) : schema;
@@ -2,17 +2,18 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
2
2
  import * as injectSchema from "./injectSchema";
3
3
  import * as fetcherExports from "./markupFetcher";
4
4
  import { afterEach } from "node:test";
5
- import store from "./store";
6
5
  const { markupFetcher, batchMarkupFetcher } = fetcherExports;
7
6
  beforeEach(() => {
8
- store.clear();
7
+ globalThis.jamCommentsStore = new Map();
9
8
  });
10
9
  afterEach(() => {
11
10
  vi.resetAllMocks();
12
11
  });
13
12
  describe("fetchAll()", () => {
14
13
  it("fetches all comments in a single request", async () => {
15
- const saveFileSpy = vi.spyOn(store, "set");
14
+ const store = {
15
+ set: vi.fn(),
16
+ };
16
17
  const mockBatchFetcher = vi.fn().mockReturnValue({
17
18
  data: [
18
19
  { path: "/test", markup: "markup1" },
@@ -35,7 +36,7 @@ describe("fetchAll()", () => {
35
36
  domain: "test.com",
36
37
  apiKey: "123abc",
37
38
  environment: "production",
38
- }, "test_platform", vi.fn(), batchMarkupFetcherMock);
39
+ }, "test_platform", vi.fn(), batchMarkupFetcherMock, store);
39
40
  expect(batchMarkupFetcherMock).toHaveBeenCalledWith("test_platform", expect.anything());
40
41
  expect(mockBatchFetcher).toHaveBeenCalledWith({
41
42
  domain: "test.com",
@@ -47,12 +48,14 @@ describe("fetchAll()", () => {
47
48
  page: 1,
48
49
  tz: undefined,
49
50
  });
50
- expect(saveFileSpy).toHaveBeenCalledTimes(2);
51
- expect(saveFileSpy).toHaveBeenCalledWith("/test", "markup1");
52
- expect(saveFileSpy).toHaveBeenCalledWith("/test2", "markup2");
51
+ expect(store.set).toHaveBeenCalledTimes(2);
52
+ expect(store.set).toHaveBeenCalledWith("/test", "markup1");
53
+ expect(store.set).toHaveBeenCalledWith("/test2", "markup2");
53
54
  });
54
55
  it("fetches all comments in multiple requests", async () => {
55
- const saveFileSpy = vi.spyOn(store, "set");
56
+ const store = {
57
+ set: vi.fn(),
58
+ };
56
59
  const mockBatchFetcher = vi
57
60
  .fn()
58
61
  .mockReturnValueOnce({
@@ -90,7 +93,7 @@ describe("fetchAll()", () => {
90
93
  domain: "test.com",
91
94
  apiKey: "123abc",
92
95
  environment: "production",
93
- }, "test_platform", vi.fn(), batchMarkupFetcherMock);
96
+ }, "test_platform", vi.fn(), batchMarkupFetcherMock, store);
94
97
  expect(batchMarkupFetcherMock).toHaveBeenCalledWith("test_platform", expect.anything());
95
98
  expect(mockBatchFetcher).toHaveBeenCalledWith({
96
99
  domain: "test.com",
@@ -113,12 +116,14 @@ describe("fetchAll()", () => {
113
116
  page: 2,
114
117
  tz: undefined,
115
118
  });
116
- expect(saveFileSpy).toHaveBeenCalledTimes(4);
117
- expect(saveFileSpy).toHaveBeenCalledWith("/test", "markup1");
118
- expect(saveFileSpy).toHaveBeenCalledWith("/test2", "markup2");
119
+ expect(store.set).toHaveBeenCalledTimes(4);
120
+ expect(store.set).toHaveBeenCalledWith("/test", "markup1");
121
+ expect(store.set).toHaveBeenCalledWith("/test2", "markup2");
119
122
  });
120
123
  it("deletes the temp directory of anything fails", async () => {
121
- const deleteTempDirectorySpy = vi.spyOn(store, "clear");
124
+ const store = {
125
+ clear: vi.fn(),
126
+ };
122
127
  const mockBatchFetcher = vi.fn().mockImplementation(() => {
123
128
  throw new Error("test error");
124
129
  });
@@ -130,15 +135,17 @@ describe("fetchAll()", () => {
130
135
  domain: "test.com",
131
136
  apiKey: "123abc",
132
137
  environment: "production",
133
- }, "test_platform", vi.fn(), batchMarkupFetcherMock);
138
+ }, "test_platform", vi.fn(), batchMarkupFetcherMock, store);
134
139
  }
135
140
  catch (e) {
136
141
  expect(e.message).toEqual("test error");
137
142
  }
138
- expect(deleteTempDirectorySpy).toHaveBeenCalledOnce();
143
+ expect(store.clear).toHaveBeenCalledOnce();
139
144
  });
140
145
  it("handles copy overrides", async () => {
141
- const saveFileSpy = vi.spyOn(store, "set");
146
+ const store = {
147
+ set: vi.fn(),
148
+ };
142
149
  const mockBatchFetcher = vi.fn().mockReturnValue({
143
150
  data: [
144
151
  { path: "/test", markup: "markup1" },
@@ -165,7 +172,7 @@ describe("fetchAll()", () => {
165
172
  copy_confirmation_message: "custom confirmation message",
166
173
  copy_submit_button: "custom submit button",
167
174
  },
168
- }, "test_platform", vi.fn(), batchMarkupFetcherMock);
175
+ }, "test_platform", vi.fn(), batchMarkupFetcherMock, store);
169
176
  expect(batchMarkupFetcherMock).toHaveBeenCalledWith("test_platform", expect.anything());
170
177
  expect(mockBatchFetcher).toHaveBeenCalledWith({
171
178
  domain: "test.com",
@@ -179,7 +186,7 @@ describe("fetchAll()", () => {
179
186
  page: 1,
180
187
  tz: undefined,
181
188
  });
182
- expect(saveFileSpy).toHaveBeenCalledTimes(2);
189
+ expect(store.set).toHaveBeenCalledTimes(2);
183
190
  });
184
191
  it("allows you to pass a custom date format", async () => {
185
192
  const mockBatchFetcher = vi.fn().mockReturnValue({
@@ -470,10 +477,10 @@ describe("markupFetcher", () => {
470
477
  });
471
478
  describe("using saved markup", () => {
472
479
  it("makes fresh request when temp directory does not exist", async () => {
473
- const tempDirectoryExistsSpy = vi
474
- .spyOn(store, "isInitialized")
475
- .mockReturnValue(false);
476
- const readFileSpy = vi.spyOn(store, "get");
480
+ const store = {
481
+ hasData: false,
482
+ get: vi.fn().mockReturnValue(null),
483
+ };
477
484
  const fetchMock = vi.fn().mockImplementation(() => {
478
485
  return {
479
486
  status: 200,
@@ -488,50 +495,42 @@ describe("markupFetcher", () => {
488
495
  apiKey: "123abc",
489
496
  environment: "production",
490
497
  });
491
- expect(tempDirectoryExistsSpy).toHaveBeenCalledOnce();
492
- expect(readFileSpy).not.toHaveBeenCalled();
498
+ expect(store.get).not.toHaveBeenCalled();
493
499
  expect(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?domain=test.com&path=%2Ftest", expect.anything());
494
500
  expect(result).toEqual("results!");
495
501
  });
496
502
  it("uses saved markup when it exists", async () => {
497
- const tempDirectoryExistsSpy = vi
498
- .spyOn(store, "isInitialized")
499
- .mockReturnValue(true);
500
- const readFileSpy = vi
501
- .spyOn(store, "get")
502
- .mockReturnValue("saved markup");
503
+ const store = {
504
+ hasData: true,
505
+ get: vi.fn().mockReturnValue("saved markup"),
506
+ };
503
507
  const fetchMock = vi.fn();
504
- const fetcher = markupFetcher("test", fetchMock);
508
+ const fetcher = markupFetcher("test", fetchMock, store);
505
509
  const result = await fetcher({
506
510
  path: "/test",
507
511
  domain: "test.com",
508
512
  apiKey: "123abc",
509
513
  environment: "production",
510
514
  });
511
- expect(tempDirectoryExistsSpy).toHaveBeenCalledOnce();
512
- expect(readFileSpy).toHaveBeenCalledOnce();
515
+ expect(store.get).toHaveBeenCalledOnce();
513
516
  expect(fetchMock).not.toHaveBeenCalled();
514
517
  expect(result).toEqual("saved markup");
515
518
  });
516
519
  it("uses the EMPTY template when there's no saved file for a post", async () => {
517
- const tempDirectoryExistsSpy = vi
518
- .spyOn(store, "isInitialized")
519
- .mockReturnValue(true);
520
- const readFileSpy = vi.spyOn(store, "get").mockReturnValue(null);
521
- const getEmptyMarkupSpy = vi
522
- .spyOn(store, "getEmptyMarkup")
523
- .mockReturnValue("<!-- EMPTY -->");
520
+ const store = {
521
+ get: vi.fn().mockReturnValue(null),
522
+ emptyMarkup: "<!-- EMPTY -->",
523
+ hasData: true,
524
+ };
524
525
  const fetchMock = vi.fn();
525
- const fetcher = markupFetcher("test", fetchMock);
526
+ const fetcher = markupFetcher("test", fetchMock, store);
526
527
  const result = await fetcher({
527
528
  path: "/test",
528
529
  domain: "test.com",
529
530
  apiKey: "123abc",
530
531
  environment: "production",
531
532
  });
532
- expect(tempDirectoryExistsSpy).toHaveBeenCalledOnce();
533
- expect(readFileSpy).toHaveBeenCalledOnce();
534
- expect(getEmptyMarkupSpy).toHaveBeenCalledOnce();
533
+ expect(store.get).toHaveBeenCalledOnce();
535
534
  expect(fetchMock).not.toHaveBeenCalled();
536
535
  expect(result).toEqual("<!-- EMPTY -->");
537
536
  });
package/dist/esm/store.js CHANGED
@@ -1,30 +1,24 @@
1
- let STORE = null;
2
- const store = {
3
- init() {
4
- if (!STORE) {
5
- STORE = new Map();
6
- }
7
- },
8
- isInitialized() {
9
- return STORE !== null;
10
- },
1
+ class Store {
2
+ store;
3
+ constructor() {
4
+ globalThis.jamCommentsStore =
5
+ globalThis.jamCommentsStore || new Map();
6
+ this.store = globalThis.jamCommentsStore;
7
+ }
11
8
  set(path, markup) {
12
- STORE.set(store.toPathKey(path), markup);
13
- },
9
+ this.store.set(path, markup);
10
+ }
14
11
  get(path) {
15
- return STORE.get(store.toPathKey(path)) || null;
16
- },
12
+ return this.store.get(path) || null;
13
+ }
17
14
  clear() {
18
- if (STORE) {
19
- STORE.clear();
20
- STORE = null;
21
- }
22
- },
23
- getEmptyMarkup() {
24
- return STORE.get("EMPTY");
25
- },
26
- toPathKey(path) {
27
- return path.replace(/^\//, "").replace(/\//g, "::");
28
- },
29
- };
30
- export default store;
15
+ this.store.clear();
16
+ }
17
+ get hasData() {
18
+ return this.store.size > 0;
19
+ }
20
+ get emptyMarkup() {
21
+ return this.store.get("EMPTY") || null;
22
+ }
23
+ }
24
+ export default Store;
@@ -1,6 +1,5 @@
1
1
  export type { CustomCopy } from "./markupFetcher";
2
- import store from "./store";
3
2
  export { markupFetcher, fetchAll } from "./markupFetcher";
4
3
  export { removeFalseyValues } from "./utils";
5
4
  export { log, logError } from "./log";
6
- export { store };
5
+ export { default as Store } from "./store";
@@ -1,3 +1,4 @@
1
+ import Store from "./store";
1
2
  export interface CustomCopy {
2
3
  confirmationMessage?: string;
3
4
  submitButton?: string;
@@ -48,9 +49,9 @@ interface IBatchResponse {
48
49
  total: number;
49
50
  };
50
51
  }
51
- export declare function fetchAll({ tz, dateFormat, domain, apiKey, baseUrl, environment, copy, }: IBatchFetchData, platform: string, fetchImplementation?: any, batchMarkupFetcherImpl?: any): Promise<void>;
52
+ export declare function fetchAll({ tz, dateFormat, domain, apiKey, baseUrl, environment, copy, }: IBatchFetchData, platform: string, fetchImplementation?: any, batchMarkupFetcherImpl?: any, store?: Store): Promise<Map<string, string>>;
52
53
  export declare function batchMarkupFetcher(platform: string, fetchImplementation?: typeof fetch): (args: IBatchFetchData) => Promise<IBatchResponse>;
53
54
  export declare function fetchFreshMarkup({ tz, path, copy, domain, apiKey, dateFormat, baseUrl, environment, }: IFetchData, fetchImplementation: typeof fetch, platform: string): Promise<string>;
54
55
  export declare function makeMarkupRequest<T extends Partial<IBatchFetchData & IFetchData>>({ tz, path, page, domain, apiKey, dateFormat, copy, baseUrl, environment, }: T, baseServicePath: string, fetchImplementation: typeof fetch, platform: string): Promise<Response>;
55
- export declare function markupFetcher(platform: string, fetchImplementation?: typeof fetch): (args: IFetchData) => Promise<string>;
56
+ export declare function markupFetcher(platform: string, fetchImplementation?: typeof fetch, store?: Store): (args: IFetchData) => Promise<string>;
56
57
  export {};
@@ -1,10 +1,10 @@
1
- declare const store: {
2
- init(): void;
3
- isInitialized(): boolean;
1
+ declare class Store {
2
+ store: Map<string, string>;
3
+ constructor();
4
4
  set(path: string, markup: string): void;
5
- get(path: string): any;
5
+ get(path: string): string | null;
6
6
  clear(): void;
7
- getEmptyMarkup(): any;
8
- toPathKey(path: string): string;
9
- };
10
- export default store;
7
+ get hasData(): boolean;
8
+ get emptyMarkup(): string | null;
9
+ }
10
+ export default Store;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jam-comments/server-utilities",
3
- "version": "5.9.0",
3
+ "version": "5.10.0",
4
4
  "description": "Various JavaScript utilities for JamComments.",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",