@jam-comments/server-utilities 5.10.1 → 5.11.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 +3 -1
- package/dist/cjs/markupFetcher.js +7 -6
- package/dist/cjs/markupFetcher.test.js +17 -7
- package/dist/cjs/simpleMarkupFetcher.js +21 -0
- package/dist/cjs/simpleMarkupFetcher.test.js +301 -0
- package/dist/cjs/store.js +12 -2
- package/dist/cjs/store.test.js +32 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/markupFetcher.js +1 -1
- package/dist/esm/simpleMarkupFetcher.js +18 -0
- package/dist/esm/simpleMarkupFetcher.test.js +266 -0
- package/dist/esm/store.js +12 -2
- package/dist/esm/store.test.js +30 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/markupFetcher.d.ts +1 -0
- package/dist/types/simpleMarkupFetcher.d.ts +2 -0
- package/dist/types/simpleMarkupFetcher.test.d.ts +1 -0
- package/dist/types/store.d.ts +1 -0
- package/dist/types/store.test.d.ts +1 -0
- package/package.json +6 -6
package/dist/cjs/index.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Store = exports.logError = exports.log = exports.removeFalseyValues = exports.fetchAll = exports.markupFetcher = void 0;
|
|
3
|
+
exports.Store = exports.logError = exports.log = exports.removeFalseyValues = exports.fetchAll = exports.markupFetcher = exports.simpleMarkupFetcher = void 0;
|
|
4
|
+
var simpleMarkupFetcher_1 = require("./simpleMarkupFetcher");
|
|
5
|
+
Object.defineProperty(exports, "simpleMarkupFetcher", { enumerable: true, get: function () { return simpleMarkupFetcher_1.simpleMarkupFetcher; } });
|
|
4
6
|
var markupFetcher_1 = require("./markupFetcher");
|
|
5
7
|
Object.defineProperty(exports, "markupFetcher", { enumerable: true, get: function () { return markupFetcher_1.markupFetcher; } });
|
|
6
8
|
Object.defineProperty(exports, "fetchAll", { enumerable: true, get: function () { return markupFetcher_1.fetchAll; } });
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BASE_URL = void 0;
|
|
3
4
|
exports.fetchAll = fetchAll;
|
|
4
5
|
exports.batchMarkupFetcher = batchMarkupFetcher;
|
|
5
6
|
exports.fetchFreshMarkup = fetchFreshMarkup;
|
|
@@ -8,8 +9,8 @@ exports.markupFetcher = markupFetcher;
|
|
|
8
9
|
const injectSchema_1 = require("./injectSchema");
|
|
9
10
|
const utils_1 = require("./utils");
|
|
10
11
|
const store_1 = require("./store");
|
|
11
|
-
|
|
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()) {
|
|
12
|
+
exports.BASE_URL = "https://go.jamcomments.com";
|
|
13
|
+
async function fetchAll({ tz = undefined, dateFormat = undefined, domain, apiKey, baseUrl = exports.BASE_URL, environment = (0, utils_1.getEnvironment)(), copy = {}, }, platform, fetchImplementation = fetch, batchMarkupFetcherImpl = batchMarkupFetcher, store = new store_1.Store()) {
|
|
13
14
|
const fetchBatchMarkup = batchMarkupFetcherImpl(platform, fetchImplementation);
|
|
14
15
|
let shouldKeepFetching = true;
|
|
15
16
|
let page = 1;
|
|
@@ -45,16 +46,16 @@ async function fetchAll({ tz = undefined, dateFormat = undefined, domain, apiKey
|
|
|
45
46
|
return store.store;
|
|
46
47
|
}
|
|
47
48
|
function batchMarkupFetcher(platform, fetchImplementation = fetch) {
|
|
48
|
-
return async ({ tz, copy, domain, apiKey, dateFormat, baseUrl = BASE_URL, environment = (0, utils_1.getEnvironment)(), page = 1, }) => {
|
|
49
|
+
return async ({ tz, copy, domain, apiKey, dateFormat, baseUrl = exports.BASE_URL, environment = (0, utils_1.getEnvironment)(), page = 1, }) => {
|
|
49
50
|
const response = await makeMarkupRequest({ tz, domain, apiKey, baseUrl, environment, page, copy, dateFormat }, "/api/v3/markup/all", fetchImplementation, platform);
|
|
50
51
|
return response.json();
|
|
51
52
|
};
|
|
52
53
|
}
|
|
53
|
-
async function fetchFreshMarkup({ tz, path, copy, domain, apiKey, dateFormat, baseUrl = BASE_URL, environment = (0, utils_1.getEnvironment)(), }, fetchImplementation = fetch, platform) {
|
|
54
|
+
async function fetchFreshMarkup({ tz, path, copy, domain, apiKey, dateFormat, baseUrl = exports.BASE_URL, environment = (0, utils_1.getEnvironment)(), }, fetchImplementation = fetch, platform) {
|
|
54
55
|
const response = await makeMarkupRequest({ tz, path, domain, apiKey, baseUrl, environment, copy, dateFormat }, "/api/v3/markup", fetchImplementation, platform);
|
|
55
56
|
return response.text();
|
|
56
57
|
}
|
|
57
|
-
async function makeMarkupRequest({ tz, path, page, domain, apiKey, dateFormat, copy = {}, baseUrl = BASE_URL, environment = (0, utils_1.getEnvironment)(), }, baseServicePath, fetchImplementation = fetch, platform) {
|
|
58
|
+
async function makeMarkupRequest({ tz, path, page, domain, apiKey, dateFormat, copy = {}, baseUrl = exports.BASE_URL, environment = (0, utils_1.getEnvironment)(), }, baseServicePath, fetchImplementation = fetch, platform) {
|
|
58
59
|
const trimmedTimezone = tz?.trim();
|
|
59
60
|
if (trimmedTimezone && !(0, utils_1.isValidTimezone)(trimmedTimezone)) {
|
|
60
61
|
throw new Error(`The timezone passed to JamComments is invalid: ${trimmedTimezone}`);
|
|
@@ -98,7 +99,7 @@ async function makeMarkupRequest({ tz, path, page, domain, apiKey, dateFormat, c
|
|
|
98
99
|
return response;
|
|
99
100
|
}
|
|
100
101
|
function markupFetcher(platform, fetchImplementation = fetch, store = new store_1.Store()) {
|
|
101
|
-
return async ({ tz = undefined, path, domain, apiKey, schema, dateFormat, baseUrl = BASE_URL, environment = (0, utils_1.getEnvironment)(), copy = {}, }) => {
|
|
102
|
+
return async ({ tz = undefined, path, domain, apiKey, schema, dateFormat, baseUrl = exports.BASE_URL, environment = (0, utils_1.getEnvironment)(), copy = {}, }) => {
|
|
102
103
|
path = path || "/";
|
|
103
104
|
const cachedMarkup = (() => {
|
|
104
105
|
if (!store.hasData) {
|
|
@@ -15,13 +15,23 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
|
|
|
15
15
|
}) : function(o, v) {
|
|
16
16
|
o["default"] = v;
|
|
17
17
|
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
};
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
25
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
36
|
const vitest_1 = require("vitest");
|
|
27
37
|
const injectSchema = __importStar(require("./injectSchema"));
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.simpleMarkupFetcher = simpleMarkupFetcher;
|
|
4
|
+
const injectSchema_1 = require("./injectSchema");
|
|
5
|
+
const markupFetcher_1 = require("./markupFetcher");
|
|
6
|
+
const utils_1 = require("./utils");
|
|
7
|
+
function simpleMarkupFetcher(platform, fetchImplementation = fetch) {
|
|
8
|
+
return async ({ tz = undefined, path, domain, apiKey, schema, dateFormat, baseUrl = markupFetcher_1.BASE_URL, environment = (0, utils_1.getEnvironment)(), copy = {}, }) => {
|
|
9
|
+
path = path || "/";
|
|
10
|
+
const markup = await (0, markupFetcher_1.fetchFreshMarkup)({ tz, path, domain, apiKey, baseUrl, environment, copy, dateFormat }, fetchImplementation, platform);
|
|
11
|
+
if (schema) {
|
|
12
|
+
const preparedSchema = typeof schema !== "string" ? JSON.stringify(schema) : schema;
|
|
13
|
+
const parsedSchema = (0, utils_1.parseJson)(preparedSchema);
|
|
14
|
+
if (!parsedSchema) {
|
|
15
|
+
return markup;
|
|
16
|
+
}
|
|
17
|
+
return (0, injectSchema_1.injectSchema)(markup, parsedSchema);
|
|
18
|
+
}
|
|
19
|
+
return markup;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
const injectSchema = __importStar(require("./injectSchema"));
|
|
37
|
+
const vitest_1 = require("vitest");
|
|
38
|
+
const simpleMarkupFetcher_1 = require("./simpleMarkupFetcher");
|
|
39
|
+
(0, vitest_1.describe)("simpleMarkupFetcher", () => {
|
|
40
|
+
(0, vitest_1.it)("constructs fetch request correctly", async () => {
|
|
41
|
+
const injectSchemaSpy = vitest_1.vi.spyOn(injectSchema, "injectSchema");
|
|
42
|
+
const fetchMock = vitest_1.vi.fn().mockImplementation(() => {
|
|
43
|
+
return {
|
|
44
|
+
status: 200,
|
|
45
|
+
ok: true,
|
|
46
|
+
text: () => "results!",
|
|
47
|
+
};
|
|
48
|
+
});
|
|
49
|
+
const fetcher = (0, simpleMarkupFetcher_1.simpleMarkupFetcher)("test", fetchMock);
|
|
50
|
+
const result = await fetcher({
|
|
51
|
+
path: "/test",
|
|
52
|
+
domain: "test.com",
|
|
53
|
+
apiKey: "123abc",
|
|
54
|
+
environment: "production",
|
|
55
|
+
});
|
|
56
|
+
(0, vitest_1.expect)(injectSchemaSpy).not.toHaveBeenCalled();
|
|
57
|
+
(0, vitest_1.expect)(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?domain=test.com&path=%2Ftest", vitest_1.expect.objectContaining({
|
|
58
|
+
headers: vitest_1.expect.objectContaining({
|
|
59
|
+
Accept: "application/json",
|
|
60
|
+
Authorization: "Bearer 123abc",
|
|
61
|
+
"X-Platform": "test",
|
|
62
|
+
}),
|
|
63
|
+
method: "GET",
|
|
64
|
+
}));
|
|
65
|
+
(0, vitest_1.expect)(result).toEqual("results!");
|
|
66
|
+
});
|
|
67
|
+
(0, vitest_1.it)("stubs comments", async () => {
|
|
68
|
+
const fetchMock = vitest_1.vi.fn().mockImplementation(() => {
|
|
69
|
+
return {
|
|
70
|
+
status: 200,
|
|
71
|
+
ok: true,
|
|
72
|
+
text: () => "results!",
|
|
73
|
+
};
|
|
74
|
+
});
|
|
75
|
+
const fetcher = (0, simpleMarkupFetcher_1.simpleMarkupFetcher)("test", fetchMock);
|
|
76
|
+
const result = await fetcher({
|
|
77
|
+
path: "/test",
|
|
78
|
+
domain: "test.com",
|
|
79
|
+
apiKey: "123abc",
|
|
80
|
+
environment: "development",
|
|
81
|
+
});
|
|
82
|
+
(0, vitest_1.expect)(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?domain=test.com&path=%2Ftest&stub=true", vitest_1.expect.anything());
|
|
83
|
+
(0, vitest_1.expect)(result).toEqual("results!");
|
|
84
|
+
});
|
|
85
|
+
(0, vitest_1.it)("uses different base URL", async () => {
|
|
86
|
+
const fetchMock = vitest_1.vi.fn().mockImplementation(() => {
|
|
87
|
+
return {
|
|
88
|
+
status: 200,
|
|
89
|
+
ok: true,
|
|
90
|
+
text: () => "results!",
|
|
91
|
+
};
|
|
92
|
+
});
|
|
93
|
+
const fetcher = (0, simpleMarkupFetcher_1.simpleMarkupFetcher)("test", fetchMock);
|
|
94
|
+
const result = await fetcher({
|
|
95
|
+
path: "/test",
|
|
96
|
+
domain: "test.com",
|
|
97
|
+
apiKey: "123abc",
|
|
98
|
+
baseUrl: "http://ur-mom.com",
|
|
99
|
+
environment: "production",
|
|
100
|
+
});
|
|
101
|
+
(0, vitest_1.expect)(fetchMock).toHaveBeenCalledWith("http://ur-mom.com/api/v3/markup?domain=test.com&path=%2Ftest", vitest_1.expect.anything());
|
|
102
|
+
(0, vitest_1.expect)(result).toEqual("results!");
|
|
103
|
+
});
|
|
104
|
+
(0, vitest_1.it)("respects production!!!", async () => {
|
|
105
|
+
const fetchMock = vitest_1.vi.fn().mockImplementation(() => {
|
|
106
|
+
return {
|
|
107
|
+
status: 200,
|
|
108
|
+
ok: true,
|
|
109
|
+
text: () => "results!",
|
|
110
|
+
};
|
|
111
|
+
});
|
|
112
|
+
const fetcher = (0, simpleMarkupFetcher_1.simpleMarkupFetcher)("test", fetchMock);
|
|
113
|
+
const result = await fetcher({
|
|
114
|
+
path: "/test",
|
|
115
|
+
domain: "test.com",
|
|
116
|
+
apiKey: "123abc",
|
|
117
|
+
baseUrl: "http://ur-mom.com",
|
|
118
|
+
environment: "production",
|
|
119
|
+
});
|
|
120
|
+
(0, vitest_1.expect)(fetchMock).toHaveBeenCalledWith("http://ur-mom.com/api/v3/markup?domain=test.com&path=%2Ftest", vitest_1.expect.anything());
|
|
121
|
+
(0, vitest_1.expect)(result).toEqual("results!");
|
|
122
|
+
});
|
|
123
|
+
(0, vitest_1.it)("falls back to root path", async () => {
|
|
124
|
+
const fetchMock = vitest_1.vi.fn().mockImplementation(() => {
|
|
125
|
+
return {
|
|
126
|
+
status: 200,
|
|
127
|
+
ok: true,
|
|
128
|
+
text: () => "results!",
|
|
129
|
+
};
|
|
130
|
+
});
|
|
131
|
+
const fetcher = (0, simpleMarkupFetcher_1.simpleMarkupFetcher)("test", fetchMock);
|
|
132
|
+
const result = await fetcher({
|
|
133
|
+
path: null,
|
|
134
|
+
domain: "test.com",
|
|
135
|
+
apiKey: "123abc",
|
|
136
|
+
environment: "production",
|
|
137
|
+
});
|
|
138
|
+
(0, vitest_1.expect)(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?domain=test.com&path=%2F", vitest_1.expect.anything());
|
|
139
|
+
(0, vitest_1.expect)(result).toEqual("results!");
|
|
140
|
+
});
|
|
141
|
+
(0, vitest_1.it)("credentials are invalid", async () => {
|
|
142
|
+
const fetchMock = vitest_1.vi.fn().mockImplementation(() => {
|
|
143
|
+
return {
|
|
144
|
+
status: 401,
|
|
145
|
+
ok: false,
|
|
146
|
+
text: () => "bad results!",
|
|
147
|
+
};
|
|
148
|
+
});
|
|
149
|
+
const fetcher = (0, simpleMarkupFetcher_1.simpleMarkupFetcher)("test", fetchMock);
|
|
150
|
+
(0, vitest_1.expect)(fetcher({
|
|
151
|
+
path: "/test",
|
|
152
|
+
domain: "test.com",
|
|
153
|
+
apiKey: "123abc",
|
|
154
|
+
})).rejects.toThrowError(/Unauthorized!/);
|
|
155
|
+
});
|
|
156
|
+
(0, vitest_1.it)("response isn't ok", async () => {
|
|
157
|
+
const fetchMock = vitest_1.vi.fn().mockImplementation(() => {
|
|
158
|
+
return {
|
|
159
|
+
status: 500,
|
|
160
|
+
ok: false,
|
|
161
|
+
text: () => "bad results!",
|
|
162
|
+
};
|
|
163
|
+
});
|
|
164
|
+
const fetcher = (0, simpleMarkupFetcher_1.simpleMarkupFetcher)("test", fetchMock);
|
|
165
|
+
(0, vitest_1.expect)(fetcher({
|
|
166
|
+
path: "/test",
|
|
167
|
+
domain: "test.com",
|
|
168
|
+
apiKey: "123abc",
|
|
169
|
+
})).rejects.toThrowError(/request failed! Status code: 500/);
|
|
170
|
+
});
|
|
171
|
+
(0, vitest_1.it)("passes custom copy", async () => {
|
|
172
|
+
const fetchMock = vitest_1.vi.fn().mockImplementation(() => {
|
|
173
|
+
return {
|
|
174
|
+
status: 200,
|
|
175
|
+
ok: true,
|
|
176
|
+
text: () => "results!",
|
|
177
|
+
};
|
|
178
|
+
});
|
|
179
|
+
const fetcher = (0, simpleMarkupFetcher_1.simpleMarkupFetcher)("test", fetchMock);
|
|
180
|
+
const result = await fetcher({
|
|
181
|
+
path: null,
|
|
182
|
+
domain: "test.com",
|
|
183
|
+
apiKey: "123abc",
|
|
184
|
+
environment: "production",
|
|
185
|
+
copy: {
|
|
186
|
+
copy_confirmation_message: "custom confirmation message",
|
|
187
|
+
copy_submit_button: "custom submit button",
|
|
188
|
+
},
|
|
189
|
+
});
|
|
190
|
+
(0, vitest_1.expect)(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?domain=test.com&path=%2F©_confirmation_message=custom+confirmation+message©_submit_button=custom+submit+button", vitest_1.expect.anything());
|
|
191
|
+
(0, vitest_1.expect)(result).toEqual("results!");
|
|
192
|
+
});
|
|
193
|
+
(0, vitest_1.it)("passes date format string", async () => {
|
|
194
|
+
const fetchMock = vitest_1.vi.fn().mockImplementation(() => {
|
|
195
|
+
return {
|
|
196
|
+
status: 200,
|
|
197
|
+
ok: true,
|
|
198
|
+
text: () => "results!",
|
|
199
|
+
};
|
|
200
|
+
});
|
|
201
|
+
const fetcher = (0, simpleMarkupFetcher_1.simpleMarkupFetcher)("test", fetchMock);
|
|
202
|
+
await fetcher({
|
|
203
|
+
path: null,
|
|
204
|
+
domain: "test.com",
|
|
205
|
+
apiKey: "123abc",
|
|
206
|
+
environment: "production",
|
|
207
|
+
dateFormat: "yyyy-MM-dd",
|
|
208
|
+
});
|
|
209
|
+
(0, vitest_1.expect)(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?domain=test.com&path=%2F&date_format=yyyy-MM-dd", vitest_1.expect.anything());
|
|
210
|
+
});
|
|
211
|
+
(0, vitest_1.it)("response isn't ok", async () => {
|
|
212
|
+
const fetchMock = vitest_1.vi.fn().mockImplementation(() => {
|
|
213
|
+
return {
|
|
214
|
+
status: 500,
|
|
215
|
+
ok: false,
|
|
216
|
+
text: () => "bad results!",
|
|
217
|
+
};
|
|
218
|
+
});
|
|
219
|
+
const fetcher = (0, simpleMarkupFetcher_1.simpleMarkupFetcher)("test", fetchMock);
|
|
220
|
+
(0, vitest_1.expect)(fetcher({
|
|
221
|
+
path: "/test",
|
|
222
|
+
domain: "test.com",
|
|
223
|
+
apiKey: "123abc",
|
|
224
|
+
})).rejects.toThrowError(/request failed! Status code: 500/);
|
|
225
|
+
});
|
|
226
|
+
(0, vitest_1.describe)("copy overrides", () => {
|
|
227
|
+
(0, vitest_1.it)("encodes copy overrides", async () => {
|
|
228
|
+
const fetchMock = vitest_1.vi.fn().mockImplementation(() => {
|
|
229
|
+
return {
|
|
230
|
+
status: 200,
|
|
231
|
+
ok: true,
|
|
232
|
+
text: () => "results!",
|
|
233
|
+
};
|
|
234
|
+
});
|
|
235
|
+
const fetcher = (0, simpleMarkupFetcher_1.simpleMarkupFetcher)("test", fetchMock);
|
|
236
|
+
await fetcher({
|
|
237
|
+
path: "/some/path",
|
|
238
|
+
domain: "test.com",
|
|
239
|
+
apiKey: "123abc",
|
|
240
|
+
environment: "production",
|
|
241
|
+
copy: {
|
|
242
|
+
copy_confirmation_message: "custom confirmation message",
|
|
243
|
+
copy_submit_button: "custom submit button",
|
|
244
|
+
},
|
|
245
|
+
});
|
|
246
|
+
(0, vitest_1.expect)(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?domain=test.com&path=%2Fsome%2Fpath©_confirmation_message=custom+confirmation+message©_submit_button=custom+submit+button", vitest_1.expect.anything());
|
|
247
|
+
});
|
|
248
|
+
});
|
|
249
|
+
(0, vitest_1.describe)("timezone validation", () => {
|
|
250
|
+
(0, vitest_1.it)("throws error when invalid timezone is provided", async () => {
|
|
251
|
+
const fetchImplMock = vitest_1.vi.fn();
|
|
252
|
+
const fetcher = (0, simpleMarkupFetcher_1.simpleMarkupFetcher)("test", fetchImplMock);
|
|
253
|
+
(0, vitest_1.expect)(fetchImplMock).not.toHaveBeenCalled();
|
|
254
|
+
(0, vitest_1.expect)(fetcher({
|
|
255
|
+
path: "/test",
|
|
256
|
+
domain: "test.com",
|
|
257
|
+
apiKey: "123abc",
|
|
258
|
+
tz: "in/valid",
|
|
259
|
+
environment: "production",
|
|
260
|
+
})).rejects.toThrowError("The timezone passed to JamComments is invalid: in/valid");
|
|
261
|
+
});
|
|
262
|
+
(0, vitest_1.it)("does not throw error when valid timezone is provided", async () => {
|
|
263
|
+
const fetchMock = vitest_1.vi.fn().mockImplementation(() => {
|
|
264
|
+
return {
|
|
265
|
+
status: 200,
|
|
266
|
+
ok: true,
|
|
267
|
+
text: () => "results!",
|
|
268
|
+
};
|
|
269
|
+
});
|
|
270
|
+
const fetcher = (0, simpleMarkupFetcher_1.simpleMarkupFetcher)("test", fetchMock);
|
|
271
|
+
const result = await fetcher({
|
|
272
|
+
path: "/some/path",
|
|
273
|
+
domain: "test.com",
|
|
274
|
+
apiKey: "123abc",
|
|
275
|
+
tz: "America/New_York",
|
|
276
|
+
environment: "production",
|
|
277
|
+
});
|
|
278
|
+
(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());
|
|
279
|
+
(0, vitest_1.expect)(result).toEqual("results!");
|
|
280
|
+
});
|
|
281
|
+
(0, vitest_1.it)("trims a valid timezone", async () => {
|
|
282
|
+
const fetchMock = vitest_1.vi.fn().mockImplementation(() => {
|
|
283
|
+
return {
|
|
284
|
+
status: 200,
|
|
285
|
+
ok: true,
|
|
286
|
+
text: () => "results!",
|
|
287
|
+
};
|
|
288
|
+
});
|
|
289
|
+
const fetcher = (0, simpleMarkupFetcher_1.simpleMarkupFetcher)("test", fetchMock);
|
|
290
|
+
const result = await fetcher({
|
|
291
|
+
path: "/some/path",
|
|
292
|
+
domain: "test.com",
|
|
293
|
+
apiKey: "123abc",
|
|
294
|
+
tz: " America/Chicago ",
|
|
295
|
+
environment: "production",
|
|
296
|
+
});
|
|
297
|
+
(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());
|
|
298
|
+
(0, vitest_1.expect)(result).toEqual("results!");
|
|
299
|
+
});
|
|
300
|
+
});
|
|
301
|
+
});
|
package/dist/cjs/store.js
CHANGED
|
@@ -9,10 +9,10 @@ class Store {
|
|
|
9
9
|
this.store = globalThis.jamCommentsStore;
|
|
10
10
|
}
|
|
11
11
|
set(path, markup) {
|
|
12
|
-
this.store.set(path, markup);
|
|
12
|
+
this.store.set(this.#normalizePath(path), markup);
|
|
13
13
|
}
|
|
14
14
|
get(path) {
|
|
15
|
-
return this.store.get(path) || null;
|
|
15
|
+
return this.store.get(this.#normalizePath(path)) || null;
|
|
16
16
|
}
|
|
17
17
|
clear() {
|
|
18
18
|
this.store.clear();
|
|
@@ -23,5 +23,15 @@ class Store {
|
|
|
23
23
|
get emptyMarkup() {
|
|
24
24
|
return this.store.get("EMPTY") || null;
|
|
25
25
|
}
|
|
26
|
+
#normalizePath(path) {
|
|
27
|
+
if (path === "EMPTY") {
|
|
28
|
+
return path;
|
|
29
|
+
}
|
|
30
|
+
let withLeadingSlash = path.startsWith("/") ? path : `/${path}`;
|
|
31
|
+
let withNoTrailingSlash = withLeadingSlash.endsWith("/")
|
|
32
|
+
? withLeadingSlash.slice(0, -1)
|
|
33
|
+
: withLeadingSlash;
|
|
34
|
+
return withNoTrailingSlash.toLowerCase();
|
|
35
|
+
}
|
|
26
36
|
}
|
|
27
37
|
exports.Store = Store;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const vitest_1 = require("vitest");
|
|
4
|
+
const store_1 = require("./store");
|
|
5
|
+
(0, vitest_1.describe)("paths", () => {
|
|
6
|
+
(0, vitest_1.it)("cleans path when setting markup", () => {
|
|
7
|
+
const store = new store_1.Store();
|
|
8
|
+
store.set("/path", "markup");
|
|
9
|
+
(0, vitest_1.expect)(store.get("/path")).toEqual("markup");
|
|
10
|
+
(0, vitest_1.expect)(store.get("path")).toEqual("markup");
|
|
11
|
+
(0, vitest_1.expect)(store.get("path/")).toEqual("markup");
|
|
12
|
+
});
|
|
13
|
+
(0, vitest_1.it)("cleans path with mixed casing", () => {
|
|
14
|
+
const store = new store_1.Store();
|
|
15
|
+
store.set("/Path", "markup");
|
|
16
|
+
(0, vitest_1.expect)(store.get("/path")).toEqual("markup");
|
|
17
|
+
(0, vitest_1.expect)(store.get("path")).toEqual("markup");
|
|
18
|
+
(0, vitest_1.expect)(store.get("path/")).toEqual("markup");
|
|
19
|
+
});
|
|
20
|
+
(0, vitest_1.it)("works just fine with deep paths", () => {
|
|
21
|
+
const store = new store_1.Store();
|
|
22
|
+
store.set("/path/to/deep", "markup");
|
|
23
|
+
(0, vitest_1.expect)(store.get("/path/to/deep")).toEqual("markup");
|
|
24
|
+
(0, vitest_1.expect)(store.get("path/to/deep")).toEqual("markup");
|
|
25
|
+
(0, vitest_1.expect)(store.get("path/to/deep/")).toEqual("markup");
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
(0, vitest_1.it)("retrieves EMPTY markup", () => {
|
|
29
|
+
const store = new store_1.Store();
|
|
30
|
+
store.set("EMPTY", "empty markup");
|
|
31
|
+
(0, vitest_1.expect)(store.emptyMarkup).toEqual("empty markup");
|
|
32
|
+
});
|
package/dist/esm/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { injectSchema } from "./injectSchema";
|
|
2
2
|
import { getEnvironment, isValidTimezone, parseJson } from "./utils";
|
|
3
3
|
import { Store } from "./store";
|
|
4
|
-
const BASE_URL = "https://go.jamcomments.com";
|
|
4
|
+
export const BASE_URL = "https://go.jamcomments.com";
|
|
5
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
7
|
let shouldKeepFetching = true;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { injectSchema } from "./injectSchema";
|
|
2
|
+
import { BASE_URL, fetchFreshMarkup } from "./markupFetcher";
|
|
3
|
+
import { getEnvironment, parseJson } from "./utils";
|
|
4
|
+
export function simpleMarkupFetcher(platform, fetchImplementation = fetch) {
|
|
5
|
+
return async ({ tz = undefined, path, domain, apiKey, schema, dateFormat, baseUrl = BASE_URL, environment = getEnvironment(), copy = {}, }) => {
|
|
6
|
+
path = path || "/";
|
|
7
|
+
const markup = await fetchFreshMarkup({ tz, path, domain, apiKey, baseUrl, environment, copy, dateFormat }, fetchImplementation, platform);
|
|
8
|
+
if (schema) {
|
|
9
|
+
const preparedSchema = typeof schema !== "string" ? JSON.stringify(schema) : schema;
|
|
10
|
+
const parsedSchema = parseJson(preparedSchema);
|
|
11
|
+
if (!parsedSchema) {
|
|
12
|
+
return markup;
|
|
13
|
+
}
|
|
14
|
+
return injectSchema(markup, parsedSchema);
|
|
15
|
+
}
|
|
16
|
+
return markup;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
import * as injectSchema from "./injectSchema";
|
|
2
|
+
import { describe, expect, it, vi } from "vitest";
|
|
3
|
+
import { simpleMarkupFetcher } from "./simpleMarkupFetcher";
|
|
4
|
+
describe("simpleMarkupFetcher", () => {
|
|
5
|
+
it("constructs fetch request correctly", async () => {
|
|
6
|
+
const injectSchemaSpy = vi.spyOn(injectSchema, "injectSchema");
|
|
7
|
+
const fetchMock = vi.fn().mockImplementation(() => {
|
|
8
|
+
return {
|
|
9
|
+
status: 200,
|
|
10
|
+
ok: true,
|
|
11
|
+
text: () => "results!",
|
|
12
|
+
};
|
|
13
|
+
});
|
|
14
|
+
const fetcher = simpleMarkupFetcher("test", fetchMock);
|
|
15
|
+
const result = await fetcher({
|
|
16
|
+
path: "/test",
|
|
17
|
+
domain: "test.com",
|
|
18
|
+
apiKey: "123abc",
|
|
19
|
+
environment: "production",
|
|
20
|
+
});
|
|
21
|
+
expect(injectSchemaSpy).not.toHaveBeenCalled();
|
|
22
|
+
expect(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?domain=test.com&path=%2Ftest", expect.objectContaining({
|
|
23
|
+
headers: expect.objectContaining({
|
|
24
|
+
Accept: "application/json",
|
|
25
|
+
Authorization: "Bearer 123abc",
|
|
26
|
+
"X-Platform": "test",
|
|
27
|
+
}),
|
|
28
|
+
method: "GET",
|
|
29
|
+
}));
|
|
30
|
+
expect(result).toEqual("results!");
|
|
31
|
+
});
|
|
32
|
+
it("stubs comments", async () => {
|
|
33
|
+
const fetchMock = vi.fn().mockImplementation(() => {
|
|
34
|
+
return {
|
|
35
|
+
status: 200,
|
|
36
|
+
ok: true,
|
|
37
|
+
text: () => "results!",
|
|
38
|
+
};
|
|
39
|
+
});
|
|
40
|
+
const fetcher = simpleMarkupFetcher("test", fetchMock);
|
|
41
|
+
const result = await fetcher({
|
|
42
|
+
path: "/test",
|
|
43
|
+
domain: "test.com",
|
|
44
|
+
apiKey: "123abc",
|
|
45
|
+
environment: "development",
|
|
46
|
+
});
|
|
47
|
+
expect(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?domain=test.com&path=%2Ftest&stub=true", expect.anything());
|
|
48
|
+
expect(result).toEqual("results!");
|
|
49
|
+
});
|
|
50
|
+
it("uses different base URL", async () => {
|
|
51
|
+
const fetchMock = vi.fn().mockImplementation(() => {
|
|
52
|
+
return {
|
|
53
|
+
status: 200,
|
|
54
|
+
ok: true,
|
|
55
|
+
text: () => "results!",
|
|
56
|
+
};
|
|
57
|
+
});
|
|
58
|
+
const fetcher = simpleMarkupFetcher("test", fetchMock);
|
|
59
|
+
const result = await fetcher({
|
|
60
|
+
path: "/test",
|
|
61
|
+
domain: "test.com",
|
|
62
|
+
apiKey: "123abc",
|
|
63
|
+
baseUrl: "http://ur-mom.com",
|
|
64
|
+
environment: "production",
|
|
65
|
+
});
|
|
66
|
+
expect(fetchMock).toHaveBeenCalledWith("http://ur-mom.com/api/v3/markup?domain=test.com&path=%2Ftest", expect.anything());
|
|
67
|
+
expect(result).toEqual("results!");
|
|
68
|
+
});
|
|
69
|
+
it("respects production!!!", async () => {
|
|
70
|
+
const fetchMock = vi.fn().mockImplementation(() => {
|
|
71
|
+
return {
|
|
72
|
+
status: 200,
|
|
73
|
+
ok: true,
|
|
74
|
+
text: () => "results!",
|
|
75
|
+
};
|
|
76
|
+
});
|
|
77
|
+
const fetcher = simpleMarkupFetcher("test", fetchMock);
|
|
78
|
+
const result = await fetcher({
|
|
79
|
+
path: "/test",
|
|
80
|
+
domain: "test.com",
|
|
81
|
+
apiKey: "123abc",
|
|
82
|
+
baseUrl: "http://ur-mom.com",
|
|
83
|
+
environment: "production",
|
|
84
|
+
});
|
|
85
|
+
expect(fetchMock).toHaveBeenCalledWith("http://ur-mom.com/api/v3/markup?domain=test.com&path=%2Ftest", expect.anything());
|
|
86
|
+
expect(result).toEqual("results!");
|
|
87
|
+
});
|
|
88
|
+
it("falls back to root path", async () => {
|
|
89
|
+
const fetchMock = vi.fn().mockImplementation(() => {
|
|
90
|
+
return {
|
|
91
|
+
status: 200,
|
|
92
|
+
ok: true,
|
|
93
|
+
text: () => "results!",
|
|
94
|
+
};
|
|
95
|
+
});
|
|
96
|
+
const fetcher = simpleMarkupFetcher("test", fetchMock);
|
|
97
|
+
const result = await fetcher({
|
|
98
|
+
path: null,
|
|
99
|
+
domain: "test.com",
|
|
100
|
+
apiKey: "123abc",
|
|
101
|
+
environment: "production",
|
|
102
|
+
});
|
|
103
|
+
expect(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?domain=test.com&path=%2F", expect.anything());
|
|
104
|
+
expect(result).toEqual("results!");
|
|
105
|
+
});
|
|
106
|
+
it("credentials are invalid", async () => {
|
|
107
|
+
const fetchMock = vi.fn().mockImplementation(() => {
|
|
108
|
+
return {
|
|
109
|
+
status: 401,
|
|
110
|
+
ok: false,
|
|
111
|
+
text: () => "bad results!",
|
|
112
|
+
};
|
|
113
|
+
});
|
|
114
|
+
const fetcher = simpleMarkupFetcher("test", fetchMock);
|
|
115
|
+
expect(fetcher({
|
|
116
|
+
path: "/test",
|
|
117
|
+
domain: "test.com",
|
|
118
|
+
apiKey: "123abc",
|
|
119
|
+
})).rejects.toThrowError(/Unauthorized!/);
|
|
120
|
+
});
|
|
121
|
+
it("response isn't ok", async () => {
|
|
122
|
+
const fetchMock = vi.fn().mockImplementation(() => {
|
|
123
|
+
return {
|
|
124
|
+
status: 500,
|
|
125
|
+
ok: false,
|
|
126
|
+
text: () => "bad results!",
|
|
127
|
+
};
|
|
128
|
+
});
|
|
129
|
+
const fetcher = simpleMarkupFetcher("test", fetchMock);
|
|
130
|
+
expect(fetcher({
|
|
131
|
+
path: "/test",
|
|
132
|
+
domain: "test.com",
|
|
133
|
+
apiKey: "123abc",
|
|
134
|
+
})).rejects.toThrowError(/request failed! Status code: 500/);
|
|
135
|
+
});
|
|
136
|
+
it("passes custom copy", async () => {
|
|
137
|
+
const fetchMock = vi.fn().mockImplementation(() => {
|
|
138
|
+
return {
|
|
139
|
+
status: 200,
|
|
140
|
+
ok: true,
|
|
141
|
+
text: () => "results!",
|
|
142
|
+
};
|
|
143
|
+
});
|
|
144
|
+
const fetcher = simpleMarkupFetcher("test", fetchMock);
|
|
145
|
+
const result = await fetcher({
|
|
146
|
+
path: null,
|
|
147
|
+
domain: "test.com",
|
|
148
|
+
apiKey: "123abc",
|
|
149
|
+
environment: "production",
|
|
150
|
+
copy: {
|
|
151
|
+
copy_confirmation_message: "custom confirmation message",
|
|
152
|
+
copy_submit_button: "custom submit button",
|
|
153
|
+
},
|
|
154
|
+
});
|
|
155
|
+
expect(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?domain=test.com&path=%2F©_confirmation_message=custom+confirmation+message©_submit_button=custom+submit+button", expect.anything());
|
|
156
|
+
expect(result).toEqual("results!");
|
|
157
|
+
});
|
|
158
|
+
it("passes date format string", async () => {
|
|
159
|
+
const fetchMock = vi.fn().mockImplementation(() => {
|
|
160
|
+
return {
|
|
161
|
+
status: 200,
|
|
162
|
+
ok: true,
|
|
163
|
+
text: () => "results!",
|
|
164
|
+
};
|
|
165
|
+
});
|
|
166
|
+
const fetcher = simpleMarkupFetcher("test", fetchMock);
|
|
167
|
+
await fetcher({
|
|
168
|
+
path: null,
|
|
169
|
+
domain: "test.com",
|
|
170
|
+
apiKey: "123abc",
|
|
171
|
+
environment: "production",
|
|
172
|
+
dateFormat: "yyyy-MM-dd",
|
|
173
|
+
});
|
|
174
|
+
expect(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?domain=test.com&path=%2F&date_format=yyyy-MM-dd", expect.anything());
|
|
175
|
+
});
|
|
176
|
+
it("response isn't ok", async () => {
|
|
177
|
+
const fetchMock = vi.fn().mockImplementation(() => {
|
|
178
|
+
return {
|
|
179
|
+
status: 500,
|
|
180
|
+
ok: false,
|
|
181
|
+
text: () => "bad results!",
|
|
182
|
+
};
|
|
183
|
+
});
|
|
184
|
+
const fetcher = simpleMarkupFetcher("test", fetchMock);
|
|
185
|
+
expect(fetcher({
|
|
186
|
+
path: "/test",
|
|
187
|
+
domain: "test.com",
|
|
188
|
+
apiKey: "123abc",
|
|
189
|
+
})).rejects.toThrowError(/request failed! Status code: 500/);
|
|
190
|
+
});
|
|
191
|
+
describe("copy overrides", () => {
|
|
192
|
+
it("encodes copy overrides", async () => {
|
|
193
|
+
const fetchMock = vi.fn().mockImplementation(() => {
|
|
194
|
+
return {
|
|
195
|
+
status: 200,
|
|
196
|
+
ok: true,
|
|
197
|
+
text: () => "results!",
|
|
198
|
+
};
|
|
199
|
+
});
|
|
200
|
+
const fetcher = simpleMarkupFetcher("test", fetchMock);
|
|
201
|
+
await fetcher({
|
|
202
|
+
path: "/some/path",
|
|
203
|
+
domain: "test.com",
|
|
204
|
+
apiKey: "123abc",
|
|
205
|
+
environment: "production",
|
|
206
|
+
copy: {
|
|
207
|
+
copy_confirmation_message: "custom confirmation message",
|
|
208
|
+
copy_submit_button: "custom submit button",
|
|
209
|
+
},
|
|
210
|
+
});
|
|
211
|
+
expect(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?domain=test.com&path=%2Fsome%2Fpath©_confirmation_message=custom+confirmation+message©_submit_button=custom+submit+button", expect.anything());
|
|
212
|
+
});
|
|
213
|
+
});
|
|
214
|
+
describe("timezone validation", () => {
|
|
215
|
+
it("throws error when invalid timezone is provided", async () => {
|
|
216
|
+
const fetchImplMock = vi.fn();
|
|
217
|
+
const fetcher = simpleMarkupFetcher("test", fetchImplMock);
|
|
218
|
+
expect(fetchImplMock).not.toHaveBeenCalled();
|
|
219
|
+
expect(fetcher({
|
|
220
|
+
path: "/test",
|
|
221
|
+
domain: "test.com",
|
|
222
|
+
apiKey: "123abc",
|
|
223
|
+
tz: "in/valid",
|
|
224
|
+
environment: "production",
|
|
225
|
+
})).rejects.toThrowError("The timezone passed to JamComments is invalid: in/valid");
|
|
226
|
+
});
|
|
227
|
+
it("does not throw error when valid timezone is provided", async () => {
|
|
228
|
+
const fetchMock = vi.fn().mockImplementation(() => {
|
|
229
|
+
return {
|
|
230
|
+
status: 200,
|
|
231
|
+
ok: true,
|
|
232
|
+
text: () => "results!",
|
|
233
|
+
};
|
|
234
|
+
});
|
|
235
|
+
const fetcher = simpleMarkupFetcher("test", fetchMock);
|
|
236
|
+
const result = await fetcher({
|
|
237
|
+
path: "/some/path",
|
|
238
|
+
domain: "test.com",
|
|
239
|
+
apiKey: "123abc",
|
|
240
|
+
tz: "America/New_York",
|
|
241
|
+
environment: "production",
|
|
242
|
+
});
|
|
243
|
+
expect(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?domain=test.com&path=%2Fsome%2Fpath&tz=America%2FNew_York", expect.anything());
|
|
244
|
+
expect(result).toEqual("results!");
|
|
245
|
+
});
|
|
246
|
+
it("trims a valid timezone", async () => {
|
|
247
|
+
const fetchMock = vi.fn().mockImplementation(() => {
|
|
248
|
+
return {
|
|
249
|
+
status: 200,
|
|
250
|
+
ok: true,
|
|
251
|
+
text: () => "results!",
|
|
252
|
+
};
|
|
253
|
+
});
|
|
254
|
+
const fetcher = simpleMarkupFetcher("test", fetchMock);
|
|
255
|
+
const result = await fetcher({
|
|
256
|
+
path: "/some/path",
|
|
257
|
+
domain: "test.com",
|
|
258
|
+
apiKey: "123abc",
|
|
259
|
+
tz: " America/Chicago ",
|
|
260
|
+
environment: "production",
|
|
261
|
+
});
|
|
262
|
+
expect(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?domain=test.com&path=%2Fsome%2Fpath&tz=America%2FChicago", expect.anything());
|
|
263
|
+
expect(result).toEqual("results!");
|
|
264
|
+
});
|
|
265
|
+
});
|
|
266
|
+
});
|
package/dist/esm/store.js
CHANGED
|
@@ -6,10 +6,10 @@ export class Store {
|
|
|
6
6
|
this.store = globalThis.jamCommentsStore;
|
|
7
7
|
}
|
|
8
8
|
set(path, markup) {
|
|
9
|
-
this.store.set(path, markup);
|
|
9
|
+
this.store.set(this.#normalizePath(path), markup);
|
|
10
10
|
}
|
|
11
11
|
get(path) {
|
|
12
|
-
return this.store.get(path) || null;
|
|
12
|
+
return this.store.get(this.#normalizePath(path)) || null;
|
|
13
13
|
}
|
|
14
14
|
clear() {
|
|
15
15
|
this.store.clear();
|
|
@@ -20,4 +20,14 @@ export class Store {
|
|
|
20
20
|
get emptyMarkup() {
|
|
21
21
|
return this.store.get("EMPTY") || null;
|
|
22
22
|
}
|
|
23
|
+
#normalizePath(path) {
|
|
24
|
+
if (path === "EMPTY") {
|
|
25
|
+
return path;
|
|
26
|
+
}
|
|
27
|
+
let withLeadingSlash = path.startsWith("/") ? path : `/${path}`;
|
|
28
|
+
let withNoTrailingSlash = withLeadingSlash.endsWith("/")
|
|
29
|
+
? withLeadingSlash.slice(0, -1)
|
|
30
|
+
: withLeadingSlash;
|
|
31
|
+
return withNoTrailingSlash.toLowerCase();
|
|
32
|
+
}
|
|
23
33
|
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { Store } from "./store";
|
|
3
|
+
describe("paths", () => {
|
|
4
|
+
it("cleans path when setting markup", () => {
|
|
5
|
+
const store = new Store();
|
|
6
|
+
store.set("/path", "markup");
|
|
7
|
+
expect(store.get("/path")).toEqual("markup");
|
|
8
|
+
expect(store.get("path")).toEqual("markup");
|
|
9
|
+
expect(store.get("path/")).toEqual("markup");
|
|
10
|
+
});
|
|
11
|
+
it("cleans path with mixed casing", () => {
|
|
12
|
+
const store = new Store();
|
|
13
|
+
store.set("/Path", "markup");
|
|
14
|
+
expect(store.get("/path")).toEqual("markup");
|
|
15
|
+
expect(store.get("path")).toEqual("markup");
|
|
16
|
+
expect(store.get("path/")).toEqual("markup");
|
|
17
|
+
});
|
|
18
|
+
it("works just fine with deep paths", () => {
|
|
19
|
+
const store = new Store();
|
|
20
|
+
store.set("/path/to/deep", "markup");
|
|
21
|
+
expect(store.get("/path/to/deep")).toEqual("markup");
|
|
22
|
+
expect(store.get("path/to/deep")).toEqual("markup");
|
|
23
|
+
expect(store.get("path/to/deep/")).toEqual("markup");
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
it("retrieves EMPTY markup", () => {
|
|
27
|
+
const store = new Store();
|
|
28
|
+
store.set("EMPTY", "empty markup");
|
|
29
|
+
expect(store.emptyMarkup).toEqual("empty markup");
|
|
30
|
+
});
|
package/dist/types/index.d.ts
CHANGED
|
@@ -49,6 +49,7 @@ interface IBatchResponse {
|
|
|
49
49
|
total: number;
|
|
50
50
|
};
|
|
51
51
|
}
|
|
52
|
+
export declare const BASE_URL = "https://go.jamcomments.com";
|
|
52
53
|
export declare function fetchAll({ tz, dateFormat, domain, apiKey, baseUrl, environment, copy, }: IBatchFetchData, platform: string, fetchImplementation?: any, batchMarkupFetcherImpl?: any, store?: Store): Promise<Map<string, string>>;
|
|
53
54
|
export declare function batchMarkupFetcher(platform: string, fetchImplementation?: typeof fetch): (args: IBatchFetchData) => Promise<IBatchResponse>;
|
|
54
55
|
export declare function fetchFreshMarkup({ tz, path, copy, domain, apiKey, dateFormat, baseUrl, environment, }: IFetchData, fetchImplementation: typeof fetch, platform: string): Promise<string>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/types/store.d.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jam-comments/server-utilities",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.11.0",
|
|
4
4
|
"description": "Various JavaScript utilities for JamComments.",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
@@ -22,11 +22,11 @@
|
|
|
22
22
|
"homepage": "https://jamcomments.com",
|
|
23
23
|
"license": "GPL-2.0",
|
|
24
24
|
"devDependencies": {
|
|
25
|
-
"@types/node": "^22.
|
|
26
|
-
"prettier": "^3.
|
|
27
|
-
"typescript": "^5.
|
|
28
|
-
"vite": "^
|
|
29
|
-
"vitest": "^2.
|
|
25
|
+
"@types/node": "^22.10.0",
|
|
26
|
+
"prettier": "^3.4.1",
|
|
27
|
+
"typescript": "^5.7.2",
|
|
28
|
+
"vite": "^6.0.1",
|
|
29
|
+
"vitest": "^2.1.6"
|
|
30
30
|
},
|
|
31
31
|
"publishConfig": {
|
|
32
32
|
"access": "public"
|