@jam-comments/server-utilities 5.9.1 → 5.10.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 +3 -6
- package/dist/cjs/markupFetcher.js +11 -17
- package/dist/cjs/markupFetcher.test.js +43 -47
- package/dist/cjs/store.js +22 -30
- package/dist/esm/index.js +1 -2
- package/dist/esm/markupFetcher.js +8 -11
- package/dist/esm/markupFetcher.test.js +43 -44
- package/dist/esm/store.js +20 -30
- package/dist/types/index.d.ts +1 -2
- package/dist/types/markupFetcher.d.ts +3 -2
- package/dist/types/store.d.ts +7 -9
- package/package.json +1 -1
package/dist/cjs/index.js
CHANGED
|
@@ -1,11 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.
|
|
7
|
-
const store_1 = __importDefault(require("./store"));
|
|
8
|
-
exports.store = store_1.default;
|
|
3
|
+
exports.Store = exports.logError = exports.log = exports.removeFalseyValues = exports.fetchAll = exports.markupFetcher = void 0;
|
|
9
4
|
var markupFetcher_1 = require("./markupFetcher");
|
|
10
5
|
Object.defineProperty(exports, "markupFetcher", { enumerable: true, get: function () { return markupFetcher_1.markupFetcher; } });
|
|
11
6
|
Object.defineProperty(exports, "fetchAll", { enumerable: true, get: function () { return markupFetcher_1.fetchAll; } });
|
|
@@ -14,3 +9,5 @@ Object.defineProperty(exports, "removeFalseyValues", { enumerable: true, get: fu
|
|
|
14
9
|
var log_1 = require("./log");
|
|
15
10
|
Object.defineProperty(exports, "log", { enumerable: true, get: function () { return log_1.log; } });
|
|
16
11
|
Object.defineProperty(exports, "logError", { enumerable: true, get: function () { return log_1.logError; } });
|
|
12
|
+
var store_1 = require("./store");
|
|
13
|
+
Object.defineProperty(exports, "Store", { enumerable: true, get: function () { return store_1.Store; } });
|
|
@@ -1,7 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.fetchAll = fetchAll;
|
|
7
4
|
exports.batchMarkupFetcher = batchMarkupFetcher;
|
|
@@ -10,11 +7,10 @@ exports.makeMarkupRequest = makeMarkupRequest;
|
|
|
10
7
|
exports.markupFetcher = markupFetcher;
|
|
11
8
|
const injectSchema_1 = require("./injectSchema");
|
|
12
9
|
const utils_1 = require("./utils");
|
|
13
|
-
const store_1 =
|
|
10
|
+
const store_1 = require("./store");
|
|
14
11
|
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) {
|
|
12
|
+
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.Store()) {
|
|
16
13
|
const fetchBatchMarkup = batchMarkupFetcherImpl(platform, fetchImplementation);
|
|
17
|
-
store_1.default.init();
|
|
18
14
|
let shouldKeepFetching = true;
|
|
19
15
|
let page = 1;
|
|
20
16
|
console.log("Fetching comments from JamComments! This might take a sec.");
|
|
@@ -32,7 +28,7 @@ async function fetchAll({ tz = undefined, dateFormat = undefined, domain, apiKey
|
|
|
32
28
|
});
|
|
33
29
|
console.log(`Checking for comment data. Batch: ${current_page}/${last_page}`);
|
|
34
30
|
items.forEach((item) => {
|
|
35
|
-
|
|
31
|
+
store.set(item.path, item.markup);
|
|
36
32
|
});
|
|
37
33
|
if (current_page === last_page) {
|
|
38
34
|
shouldKeepFetching = false;
|
|
@@ -43,10 +39,10 @@ async function fetchAll({ tz = undefined, dateFormat = undefined, domain, apiKey
|
|
|
43
39
|
}
|
|
44
40
|
}
|
|
45
41
|
catch (error) {
|
|
46
|
-
|
|
42
|
+
store.clear();
|
|
47
43
|
throw error;
|
|
48
44
|
}
|
|
49
|
-
return
|
|
45
|
+
return store.store;
|
|
50
46
|
}
|
|
51
47
|
function batchMarkupFetcher(platform, fetchImplementation = fetch) {
|
|
52
48
|
return async ({ tz, copy, domain, apiKey, dateFormat, baseUrl = BASE_URL, environment = (0, utils_1.getEnvironment)(), page = 1, }) => {
|
|
@@ -101,19 +97,17 @@ async function makeMarkupRequest({ tz, path, page, domain, apiKey, dateFormat, c
|
|
|
101
97
|
}
|
|
102
98
|
return response;
|
|
103
99
|
}
|
|
104
|
-
function markupFetcher(platform, fetchImplementation = fetch) {
|
|
100
|
+
function markupFetcher(platform, fetchImplementation = fetch, store = new store_1.Store()) {
|
|
105
101
|
return async ({ tz = undefined, path, domain, apiKey, schema, dateFormat, baseUrl = BASE_URL, environment = (0, utils_1.getEnvironment)(), copy = {}, }) => {
|
|
106
102
|
path = path || "/";
|
|
107
|
-
const
|
|
108
|
-
if (!
|
|
109
|
-
console.log("Store is not initialized.");
|
|
103
|
+
const cachedMarkup = (() => {
|
|
104
|
+
if (!store.hasData) {
|
|
110
105
|
return null;
|
|
111
106
|
}
|
|
112
|
-
|
|
113
|
-
return store_1.default.get(path) || store_1.default.getEmptyMarkup();
|
|
107
|
+
return store.get(path) || store.emptyMarkup;
|
|
114
108
|
})();
|
|
115
|
-
const markup =
|
|
116
|
-
?
|
|
109
|
+
const markup = cachedMarkup
|
|
110
|
+
? cachedMarkup
|
|
117
111
|
: await fetchFreshMarkup({ tz, path, domain, apiKey, baseUrl, environment, copy, dateFormat }, fetchImplementation, platform);
|
|
118
112
|
if (schema) {
|
|
119
113
|
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
|
-
|
|
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
|
|
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)(
|
|
79
|
-
(0, vitest_1.expect)(
|
|
80
|
-
(0, vitest_1.expect)(
|
|
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
|
|
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)(
|
|
145
|
-
(0, vitest_1.expect)(
|
|
146
|
-
(0, vitest_1.expect)(
|
|
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
|
|
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)(
|
|
168
|
+
(0, vitest_1.expect)(store.clear).toHaveBeenCalledOnce();
|
|
167
169
|
});
|
|
168
170
|
(0, vitest_1.it)("handles copy overrides", async () => {
|
|
169
|
-
const
|
|
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)(
|
|
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
|
|
502
|
-
|
|
503
|
-
.mockReturnValue(
|
|
504
|
-
|
|
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)(
|
|
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
|
|
526
|
-
|
|
527
|
-
.mockReturnValue(
|
|
528
|
-
|
|
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)(
|
|
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
|
|
546
|
-
.
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
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)(
|
|
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,35 +1,27 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
return STORE !== null;
|
|
12
|
-
},
|
|
3
|
+
exports.Store = void 0;
|
|
4
|
+
class Store {
|
|
5
|
+
store;
|
|
6
|
+
constructor() {
|
|
7
|
+
globalThis.jamCommentsStore =
|
|
8
|
+
globalThis.jamCommentsStore || new Map();
|
|
9
|
+
this.store = globalThis.jamCommentsStore;
|
|
10
|
+
}
|
|
13
11
|
set(path, markup) {
|
|
14
|
-
|
|
15
|
-
}
|
|
12
|
+
this.store.set(path, markup);
|
|
13
|
+
}
|
|
16
14
|
get(path) {
|
|
17
|
-
return
|
|
18
|
-
}
|
|
15
|
+
return this.store.get(path) || null;
|
|
16
|
+
}
|
|
19
17
|
clear() {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
return
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
},
|
|
31
|
-
get store() {
|
|
32
|
-
return STORE;
|
|
33
|
-
},
|
|
34
|
-
};
|
|
35
|
-
exports.default = store;
|
|
18
|
+
this.store.clear();
|
|
19
|
+
}
|
|
20
|
+
get hasData() {
|
|
21
|
+
return this.store.size > 0;
|
|
22
|
+
}
|
|
23
|
+
get emptyMarkup() {
|
|
24
|
+
return this.store.get("EMPTY") || null;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
exports.Store = Store;
|
package/dist/esm/index.js
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import { injectSchema } from "./injectSchema";
|
|
2
2
|
import { getEnvironment, isValidTimezone, parseJson } from "./utils";
|
|
3
|
-
import
|
|
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.");
|
|
@@ -91,19 +90,17 @@ export async function makeMarkupRequest({ tz, path, page, domain, apiKey, dateFo
|
|
|
91
90
|
}
|
|
92
91
|
return response;
|
|
93
92
|
}
|
|
94
|
-
export function markupFetcher(platform, fetchImplementation = fetch) {
|
|
93
|
+
export function markupFetcher(platform, fetchImplementation = fetch, store = new Store()) {
|
|
95
94
|
return async ({ tz = undefined, path, domain, apiKey, schema, dateFormat, baseUrl = BASE_URL, environment = getEnvironment(), copy = {}, }) => {
|
|
96
95
|
path = path || "/";
|
|
97
|
-
const
|
|
98
|
-
if (!store.
|
|
99
|
-
console.log("Store is not initialized.");
|
|
96
|
+
const cachedMarkup = (() => {
|
|
97
|
+
if (!store.hasData) {
|
|
100
98
|
return null;
|
|
101
99
|
}
|
|
102
|
-
|
|
103
|
-
return store.get(path) || store.getEmptyMarkup();
|
|
100
|
+
return store.get(path) || store.emptyMarkup;
|
|
104
101
|
})();
|
|
105
|
-
const markup =
|
|
106
|
-
?
|
|
102
|
+
const markup = cachedMarkup
|
|
103
|
+
? cachedMarkup
|
|
107
104
|
: await fetchFreshMarkup({ tz, path, domain, apiKey, baseUrl, environment, copy, dateFormat }, fetchImplementation, platform);
|
|
108
105
|
if (schema) {
|
|
109
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
|
-
|
|
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
|
|
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(
|
|
51
|
-
expect(
|
|
52
|
-
expect(
|
|
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
|
|
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(
|
|
117
|
-
expect(
|
|
118
|
-
expect(
|
|
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
|
|
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(
|
|
143
|
+
expect(store.clear).toHaveBeenCalledOnce();
|
|
139
144
|
});
|
|
140
145
|
it("handles copy overrides", async () => {
|
|
141
|
-
const
|
|
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(
|
|
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
|
|
474
|
-
|
|
475
|
-
.mockReturnValue(
|
|
476
|
-
|
|
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(
|
|
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
|
|
498
|
-
|
|
499
|
-
.mockReturnValue(
|
|
500
|
-
|
|
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(
|
|
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
|
|
518
|
-
.
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
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(
|
|
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,33 +1,23 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}
|
|
8
|
-
isInitialized() {
|
|
9
|
-
return STORE !== null;
|
|
10
|
-
},
|
|
1
|
+
export 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
|
-
|
|
13
|
-
}
|
|
9
|
+
this.store.set(path, markup);
|
|
10
|
+
}
|
|
14
11
|
get(path) {
|
|
15
|
-
return
|
|
16
|
-
}
|
|
12
|
+
return this.store.get(path) || null;
|
|
13
|
+
}
|
|
17
14
|
clear() {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
return
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
return path.replace(/^\//, "").replace(/\//g, "::");
|
|
28
|
-
},
|
|
29
|
-
get store() {
|
|
30
|
-
return STORE;
|
|
31
|
-
},
|
|
32
|
-
};
|
|
33
|
-
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
|
+
}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -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 {
|
|
5
|
+
export { 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<Map<string, string>>;
|
|
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 {};
|
package/dist/types/store.d.ts
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
declare
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
export declare class Store {
|
|
2
|
+
store: Map<string, string>;
|
|
3
|
+
constructor();
|
|
4
4
|
set(path: string, markup: string): void;
|
|
5
|
-
get(path: string):
|
|
5
|
+
get(path: string): string | null;
|
|
6
6
|
clear(): void;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
};
|
|
11
|
-
export default store;
|
|
7
|
+
get hasData(): boolean;
|
|
8
|
+
get emptyMarkup(): string | null;
|
|
9
|
+
}
|