@jam-comments/server-utilities 4.3.4 → 5.1.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,14 +3,13 @@ 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.logError = exports.log = exports.deleteTempDirectory = exports.reAppendMarkup = exports.fetchAll = exports.markupFetcher = exports.TEMP_DIRECTORY = void 0;
6
+ exports.logError = exports.log = exports.deleteTempDirectory = exports.fetchAll = exports.markupFetcher = exports.TEMP_DIRECTORY = void 0;
7
7
  const path_1 = __importDefault(require("path"));
8
8
  exports.TEMP_DIRECTORY = path_1.default.join(process.cwd(), "_temp_jc");
9
9
  var markupFetcher_1 = require("./markupFetcher");
10
10
  Object.defineProperty(exports, "markupFetcher", { enumerable: true, get: function () { return markupFetcher_1.markupFetcher; } });
11
11
  Object.defineProperty(exports, "fetchAll", { enumerable: true, get: function () { return markupFetcher_1.fetchAll; } });
12
12
  var utils_1 = require("./utils");
13
- Object.defineProperty(exports, "reAppendMarkup", { enumerable: true, get: function () { return utils_1.reAppendMarkup; } });
14
13
  Object.defineProperty(exports, "deleteTempDirectory", { enumerable: true, get: function () { return utils_1.deleteTempDirectory; } });
15
14
  var log_1 = require("./log");
16
15
  Object.defineProperty(exports, "log", { enumerable: true, get: function () { return log_1.log; } });
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.injectSchema = void 0;
3
+ exports.injectSchema = injectSchema;
4
4
  const utils_1 = require("./utils");
5
5
  function injectSchema(markup, schema) {
6
6
  const commentSchema = markup.match(/<div jc-data="jcSchema" data-schema="(.*)"><\/div>/)?.[1];
@@ -12,4 +12,3 @@ function injectSchema(markup, schema) {
12
12
  .replace("<!-- JC:SCHEMA -->", `<script type="application/ld+json">${JSON.stringify(schema)}</script>`)
13
13
  .replace(/<div jc-data="jcSchema" data-schema=".*"><\/div>(?:\n+)?/, "");
14
14
  }
15
- exports.injectSchema = injectSchema;
@@ -1,9 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.markupFetcher = exports.makeMarkupRequest = exports.fetchFreshMarkup = exports.batchMarkupFetcher = exports.fetchAll = void 0;
3
+ exports.fetchAll = fetchAll;
4
+ exports.batchMarkupFetcher = batchMarkupFetcher;
5
+ exports.fetchFreshMarkup = fetchFreshMarkup;
6
+ exports.makeMarkupRequest = makeMarkupRequest;
7
+ exports.markupFetcher = markupFetcher;
4
8
  const injectSchema_1 = require("./injectSchema");
5
9
  const utils_1 = require("./utils");
6
- async function fetchAll({ tz = undefined, domain, apiKey, baseUrl = "https://go.jamcomments.com", environment = (0, utils_1.getEnvironment)(), }, platform, fetchImplementation = fetch, batchMarkupFetcherImpl = batchMarkupFetcher) {
10
+ async function fetchAll({ tz = undefined, domain, apiKey, baseUrl = "https://go.jamcomments.com", environment = (0, utils_1.getEnvironment)(), copy = {}, }, platform, fetchImplementation = fetch, batchMarkupFetcherImpl = batchMarkupFetcher) {
7
11
  const fetchBatchMarkup = batchMarkupFetcherImpl(platform, fetchImplementation);
8
12
  (0, utils_1.createTempDirectory)();
9
13
  let shouldKeepFetching = true;
@@ -18,6 +22,7 @@ async function fetchAll({ tz = undefined, domain, apiKey, baseUrl = "https://go.
18
22
  environment,
19
23
  page,
20
24
  tz,
25
+ copy,
21
26
  });
22
27
  console.log(`Checking for comment data. Batch: ${current_page}/${last_page}`);
23
28
  const saveMarkupPromises = items.map((item) => {
@@ -37,20 +42,17 @@ async function fetchAll({ tz = undefined, domain, apiKey, baseUrl = "https://go.
37
42
  throw error;
38
43
  }
39
44
  }
40
- exports.fetchAll = fetchAll;
41
45
  function batchMarkupFetcher(platform, fetchImplementation = fetch) {
42
- return async ({ tz = undefined, domain, apiKey, baseUrl = "https://go.jamcomments.com", environment = (0, utils_1.getEnvironment)(), page = 1, }) => {
43
- const response = await makeMarkupRequest({ tz, domain, apiKey, baseUrl, environment, page }, "/api/v3/markup/all", fetchImplementation, platform);
46
+ return async ({ tz = undefined, domain, apiKey, baseUrl = "https://go.jamcomments.com", environment = (0, utils_1.getEnvironment)(), page = 1, copy = {}, }) => {
47
+ const response = await makeMarkupRequest({ tz, domain, apiKey, baseUrl, environment, page, copy }, "/api/v3/markup/all", fetchImplementation, platform);
44
48
  return response.json();
45
49
  };
46
50
  }
47
- exports.batchMarkupFetcher = batchMarkupFetcher;
48
- async function fetchFreshMarkup({ tz = undefined, path, domain, apiKey, baseUrl = "https://go.jamcomments.com", environment = (0, utils_1.getEnvironment)(), }, fetchImplementation = fetch, platform) {
49
- const response = await makeMarkupRequest({ tz, path, domain, apiKey, baseUrl, environment }, "/api/v3/markup", fetchImplementation, platform);
51
+ async function fetchFreshMarkup({ tz = undefined, path, domain, apiKey, baseUrl = "https://go.jamcomments.com", environment = (0, utils_1.getEnvironment)(), copy = {}, }, fetchImplementation = fetch, platform) {
52
+ const response = await makeMarkupRequest({ tz, path, domain, apiKey, baseUrl, environment, copy }, "/api/v3/markup", fetchImplementation, platform);
50
53
  return response.text();
51
54
  }
52
- exports.fetchFreshMarkup = fetchFreshMarkup;
53
- async function makeMarkupRequest({ tz = undefined, path = undefined, domain, apiKey, baseUrl = "https://go.jamcomments.com", environment = (0, utils_1.getEnvironment)(), page = undefined, }, baseServicePath, fetchImplementation = fetch, platform) {
55
+ async function makeMarkupRequest({ tz = undefined, path = undefined, domain, apiKey, baseUrl = "https://go.jamcomments.com", environment = (0, utils_1.getEnvironment)(), page = undefined, copy = {}, }, baseServicePath, fetchImplementation = fetch, platform) {
54
56
  const trimmedTimezone = tz?.trim();
55
57
  if (trimmedTimezone && !(0, utils_1.isValidTimezone)(trimmedTimezone)) {
56
58
  throw new Error(`The timezone passed to JamComments is invalid: ${trimmedTimezone}`);
@@ -70,6 +72,9 @@ async function makeMarkupRequest({ tz = undefined, path = undefined, domain, api
70
72
  if (environment !== "production") {
71
73
  params.set("stub", "true");
72
74
  }
75
+ for (const [key, value] of Object.entries(copy)) {
76
+ params.set(key, value);
77
+ }
73
78
  const requestUrl = `${baseUrl}${baseServicePath}?${params}`;
74
79
  const response = await fetchImplementation(requestUrl, {
75
80
  method: "GET",
@@ -87,9 +92,8 @@ async function makeMarkupRequest({ tz = undefined, path = undefined, domain, api
87
92
  }
88
93
  return response;
89
94
  }
90
- exports.makeMarkupRequest = makeMarkupRequest;
91
95
  function markupFetcher(platform, fetchImplementation = fetch) {
92
- return async ({ tz = undefined, path, domain, apiKey, schema, baseUrl = "https://go.jamcomments.com", environment = (0, utils_1.getEnvironment)(), }) => {
96
+ return async ({ tz = undefined, path, domain, apiKey, schema, baseUrl = "https://go.jamcomments.com", environment = (0, utils_1.getEnvironment)(), copy = {}, }) => {
93
97
  path = path || "/";
94
98
  const savedFile = (() => {
95
99
  if (!(0, utils_1.tempDirectoryExists)()) {
@@ -99,7 +103,7 @@ function markupFetcher(platform, fetchImplementation = fetch) {
99
103
  })();
100
104
  const markup = savedFile
101
105
  ? savedFile
102
- : await fetchFreshMarkup({ tz, path, domain, apiKey, baseUrl, environment }, fetchImplementation, platform);
106
+ : await fetchFreshMarkup({ tz, path, domain, apiKey, baseUrl, environment, copy }, fetchImplementation, platform);
103
107
  if (schema) {
104
108
  const preparedSchema = typeof schema !== "string" ? JSON.stringify(schema) : schema;
105
109
  const parsedSchema = (0, utils_1.parseJson)(preparedSchema);
@@ -111,4 +115,3 @@ function markupFetcher(platform, fetchImplementation = fetch) {
111
115
  return markup;
112
116
  };
113
117
  }
114
- exports.markupFetcher = markupFetcher;
@@ -157,6 +157,50 @@ const { markupFetcher, batchMarkupFetcher } = fetcherExports;
157
157
  }
158
158
  (0, vitest_1.expect)(deleteTempDirectorySpy).toHaveBeenCalledOnce();
159
159
  });
160
+ vitest_1.it.only("handles copy overrides", async () => {
161
+ const saveFileSpy = vitest_1.vi.spyOn(utilsExports, "saveFile");
162
+ const mockBatchFetcher = vitest_1.vi.fn().mockReturnValue({
163
+ data: [
164
+ { path: "/test", markup: "markup1" },
165
+ { path: "/test2", markup: "markup2" },
166
+ ],
167
+ meta: {
168
+ current_page: 1,
169
+ from: 1,
170
+ last_page: 1,
171
+ path: "/test",
172
+ per_page: 10,
173
+ to: 2,
174
+ total: 2,
175
+ },
176
+ });
177
+ const batchMarkupFetcherMock = vitest_1.vi.fn().mockImplementation((a, b) => {
178
+ return mockBatchFetcher;
179
+ });
180
+ await fetcherExports.fetchAll({
181
+ domain: "test.com",
182
+ apiKey: "123abc",
183
+ environment: "production",
184
+ copy: {
185
+ copy_confirmation_message: "custom confirmation message",
186
+ copy_submit_button: "custom submit button",
187
+ },
188
+ }, "test_platform", vitest_1.vi.fn(), batchMarkupFetcherMock);
189
+ (0, vitest_1.expect)(batchMarkupFetcherMock).toHaveBeenCalledWith("test_platform", vitest_1.expect.anything());
190
+ (0, vitest_1.expect)(mockBatchFetcher).toHaveBeenCalledWith({
191
+ domain: "test.com",
192
+ apiKey: "123abc",
193
+ baseUrl: "https://go.jamcomments.com",
194
+ copy: {
195
+ copy_confirmation_message: "custom confirmation message",
196
+ copy_submit_button: "custom submit button",
197
+ },
198
+ environment: "production",
199
+ page: 1,
200
+ tz: undefined,
201
+ });
202
+ (0, vitest_1.expect)(saveFileSpy).toHaveBeenCalledTimes(2);
203
+ });
160
204
  });
161
205
  (0, vitest_1.describe)("batchMarkupFetcher", () => {
162
206
  (0, vitest_1.it)("constructs fetch request correctly", async () => {
@@ -353,6 +397,43 @@ const { markupFetcher, batchMarkupFetcher } = fetcherExports;
353
397
  apiKey: "123abc",
354
398
  })).rejects.toThrowError(/request failed! Status code: 500/);
355
399
  });
400
+ (0, vitest_1.it)("passes custom copy", async () => {
401
+ const fetchMock = vitest_1.vi.fn().mockImplementation(() => {
402
+ return {
403
+ status: 200,
404
+ ok: true,
405
+ text: () => "results!",
406
+ };
407
+ });
408
+ const fetcher = markupFetcher("test", fetchMock);
409
+ const result = await fetcher({
410
+ path: null,
411
+ domain: "test.com",
412
+ apiKey: "123abc",
413
+ environment: "production",
414
+ copy: {
415
+ copy_confirmation_message: "custom confirmation message",
416
+ copy_submit_button: "custom submit button",
417
+ },
418
+ });
419
+ (0, vitest_1.expect)(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?domain=test.com&path=%2F&copy_confirmation_message=custom+confirmation+message&copy_submit_button=custom+submit+button", vitest_1.expect.anything());
420
+ (0, vitest_1.expect)(result).toEqual("results!");
421
+ });
422
+ (0, vitest_1.it)("response isn't ok", async () => {
423
+ const fetchMock = vitest_1.vi.fn().mockImplementation(() => {
424
+ return {
425
+ status: 500,
426
+ ok: false,
427
+ text: () => "bad results!",
428
+ };
429
+ });
430
+ const fetcher = markupFetcher("test", fetchMock);
431
+ (0, vitest_1.expect)(fetcher({
432
+ path: "/test",
433
+ domain: "test.com",
434
+ apiKey: "123abc",
435
+ })).rejects.toThrowError(/request failed! Status code: 500/);
436
+ });
356
437
  (0, vitest_1.describe)("using saved markup", () => {
357
438
  (0, vitest_1.it)("makes fresh request when temp directory does not exist", async () => {
358
439
  const tempDirectoryExistsSpy = vitest_1.vi
@@ -423,6 +504,29 @@ const { markupFetcher, batchMarkupFetcher } = fetcherExports;
423
504
  (0, vitest_1.expect)(result).toEqual("<!-- EMPTY -->");
424
505
  });
425
506
  });
507
+ (0, vitest_1.describe)("copy overrides", () => {
508
+ (0, vitest_1.it)("encodes copy overrides", async () => {
509
+ const fetchMock = vitest_1.vi.fn().mockImplementation(() => {
510
+ return {
511
+ status: 200,
512
+ ok: true,
513
+ text: () => "results!",
514
+ };
515
+ });
516
+ const fetcher = markupFetcher("test", fetchMock);
517
+ await fetcher({
518
+ path: "/some/path",
519
+ domain: "test.com",
520
+ apiKey: "123abc",
521
+ environment: "production",
522
+ copy: {
523
+ copy_confirmation_message: "custom confirmation message",
524
+ copy_submit_button: "custom submit button",
525
+ },
526
+ });
527
+ (0, vitest_1.expect)(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?domain=test.com&path=%2Fsome%2Fpath&copy_confirmation_message=custom+confirmation+message&copy_submit_button=custom+submit+button", vitest_1.expect.anything());
528
+ });
529
+ });
426
530
  (0, vitest_1.describe)("timezone validation", () => {
427
531
  (0, vitest_1.it)("throws error when invalid timezone is provided", async () => {
428
532
  const fetchImplMock = vitest_1.vi.fn();
package/dist/cjs/utils.js CHANGED
@@ -1,6 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.saveFile = exports.tempDirectoryExists = exports.getEmptyMarkup = exports.readFile = exports.toSavedFileName = exports.createTempDirectory = exports.deleteTempDirectory = exports.unescapeHTML = exports.parseJson = exports.reAppendMarkup = exports.getEnvironment = exports.isValidTimezone = void 0;
3
+ exports.isValidTimezone = isValidTimezone;
4
+ exports.getEnvironment = getEnvironment;
5
+ exports.parseJson = parseJson;
6
+ exports.unescapeHTML = unescapeHTML;
7
+ exports.deleteTempDirectory = deleteTempDirectory;
8
+ exports.createTempDirectory = createTempDirectory;
9
+ exports.toSavedFileName = toSavedFileName;
10
+ exports.readFile = readFile;
11
+ exports.getEmptyMarkup = getEmptyMarkup;
12
+ exports.tempDirectoryExists = tempDirectoryExists;
13
+ exports.saveFile = saveFile;
4
14
  const fs_1 = require("fs");
5
15
  const _1 = require(".");
6
16
  function isValidTimezone(tz) {
@@ -12,7 +22,6 @@ function isValidTimezone(tz) {
12
22
  return false;
13
23
  }
14
24
  }
15
- exports.isValidTimezone = isValidTimezone;
16
25
  function getEnvironment() {
17
26
  if (typeof process === "undefined") {
18
27
  return "production";
@@ -21,15 +30,6 @@ function getEnvironment() {
21
30
  process.env?.NODE_ENV ||
22
31
  "development");
23
32
  }
24
- exports.getEnvironment = getEnvironment;
25
- function reAppendMarkup(element, markup) {
26
- const range = document.createRange();
27
- range.selectNode(element);
28
- const documentFragment = range.createContextualFragment(markup);
29
- element.innerHTML = "";
30
- element.append(documentFragment);
31
- }
32
- exports.reAppendMarkup = reAppendMarkup;
33
33
  function parseJson(json) {
34
34
  try {
35
35
  return JSON.parse(json);
@@ -38,7 +38,6 @@ function parseJson(json) {
38
38
  return null;
39
39
  }
40
40
  }
41
- exports.parseJson = parseJson;
42
41
  function unescapeHTML(str) {
43
42
  var htmlEntities = {
44
43
  nbsp: " ",
@@ -70,23 +69,19 @@ function unescapeHTML(str) {
70
69
  }
71
70
  });
72
71
  }
73
- exports.unescapeHTML = unescapeHTML;
74
72
  function deleteTempDirectory() {
75
73
  if ((0, fs_1.existsSync)(_1.TEMP_DIRECTORY)) {
76
74
  (0, fs_1.rmdirSync)(_1.TEMP_DIRECTORY, { recursive: true });
77
75
  }
78
76
  }
79
- exports.deleteTempDirectory = deleteTempDirectory;
80
77
  function createTempDirectory(mkdirSyncImpl = fs_1.mkdirSync, existsSyncImpl = fs_1.existsSync) {
81
78
  if (!tempDirectoryExists(existsSyncImpl)) {
82
79
  mkdirSyncImpl(_1.TEMP_DIRECTORY);
83
80
  }
84
81
  }
85
- exports.createTempDirectory = createTempDirectory;
86
82
  function toSavedFileName(name) {
87
83
  return name.replace(/^\//, "").replace(/\//g, "::");
88
84
  }
89
- exports.toSavedFileName = toSavedFileName;
90
85
  function readFile(name, readFileSyncImpl = fs_1.readFileSync) {
91
86
  const fileName = toSavedFileName(name);
92
87
  try {
@@ -96,15 +91,12 @@ function readFile(name, readFileSyncImpl = fs_1.readFileSync) {
96
91
  return null;
97
92
  }
98
93
  }
99
- exports.readFile = readFile;
100
94
  function getEmptyMarkup() {
101
95
  return readFile("EMPTY");
102
96
  }
103
- exports.getEmptyMarkup = getEmptyMarkup;
104
97
  function tempDirectoryExists(existsSyncImpl = fs_1.existsSync) {
105
98
  return existsSyncImpl(_1.TEMP_DIRECTORY);
106
99
  }
107
- exports.tempDirectoryExists = tempDirectoryExists;
108
100
  function saveFile(name, data, writeFileImpl = fs_1.writeFile) {
109
101
  const fileName = toSavedFileName(name);
110
102
  return new Promise((resolve) => {
@@ -113,4 +105,3 @@ function saveFile(name, data, writeFileImpl = fs_1.writeFile) {
113
105
  });
114
106
  });
115
107
  }
116
- exports.saveFile = saveFile;
package/dist/esm/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import path from "path";
2
2
  export const TEMP_DIRECTORY = path.join(process.cwd(), "_temp_jc");
3
3
  export { markupFetcher, fetchAll } from "./markupFetcher";
4
- export { reAppendMarkup, deleteTempDirectory } from "./utils";
4
+ export { deleteTempDirectory } from "./utils";
5
5
  export { log, logError } from "./log";
@@ -1,6 +1,6 @@
1
1
  import { injectSchema } from "./injectSchema";
2
2
  import { createTempDirectory, deleteTempDirectory, getEmptyMarkup, getEnvironment, isValidTimezone, parseJson, readFile, saveFile, tempDirectoryExists, } from "./utils";
3
- export async function fetchAll({ tz = undefined, domain, apiKey, baseUrl = "https://go.jamcomments.com", environment = getEnvironment(), }, platform, fetchImplementation = fetch, batchMarkupFetcherImpl = batchMarkupFetcher) {
3
+ export async function fetchAll({ tz = undefined, domain, apiKey, baseUrl = "https://go.jamcomments.com", environment = getEnvironment(), copy = {}, }, platform, fetchImplementation = fetch, batchMarkupFetcherImpl = batchMarkupFetcher) {
4
4
  const fetchBatchMarkup = batchMarkupFetcherImpl(platform, fetchImplementation);
5
5
  createTempDirectory();
6
6
  let shouldKeepFetching = true;
@@ -15,6 +15,7 @@ export async function fetchAll({ tz = undefined, domain, apiKey, baseUrl = "http
15
15
  environment,
16
16
  page,
17
17
  tz,
18
+ copy,
18
19
  });
19
20
  console.log(`Checking for comment data. Batch: ${current_page}/${last_page}`);
20
21
  const saveMarkupPromises = items.map((item) => {
@@ -35,16 +36,16 @@ export async function fetchAll({ tz = undefined, domain, apiKey, baseUrl = "http
35
36
  }
36
37
  }
37
38
  export function batchMarkupFetcher(platform, fetchImplementation = fetch) {
38
- return async ({ tz = undefined, domain, apiKey, baseUrl = "https://go.jamcomments.com", environment = getEnvironment(), page = 1, }) => {
39
- const response = await makeMarkupRequest({ tz, domain, apiKey, baseUrl, environment, page }, "/api/v3/markup/all", fetchImplementation, platform);
39
+ return async ({ tz = undefined, domain, apiKey, baseUrl = "https://go.jamcomments.com", environment = getEnvironment(), page = 1, copy = {}, }) => {
40
+ const response = await makeMarkupRequest({ tz, domain, apiKey, baseUrl, environment, page, copy }, "/api/v3/markup/all", fetchImplementation, platform);
40
41
  return response.json();
41
42
  };
42
43
  }
43
- export async function fetchFreshMarkup({ tz = undefined, path, domain, apiKey, baseUrl = "https://go.jamcomments.com", environment = getEnvironment(), }, fetchImplementation = fetch, platform) {
44
- const response = await makeMarkupRequest({ tz, path, domain, apiKey, baseUrl, environment }, "/api/v3/markup", fetchImplementation, platform);
44
+ export async function fetchFreshMarkup({ tz = undefined, path, domain, apiKey, baseUrl = "https://go.jamcomments.com", environment = getEnvironment(), copy = {}, }, fetchImplementation = fetch, platform) {
45
+ const response = await makeMarkupRequest({ tz, path, domain, apiKey, baseUrl, environment, copy }, "/api/v3/markup", fetchImplementation, platform);
45
46
  return response.text();
46
47
  }
47
- export async function makeMarkupRequest({ tz = undefined, path = undefined, domain, apiKey, baseUrl = "https://go.jamcomments.com", environment = getEnvironment(), page = undefined, }, baseServicePath, fetchImplementation = fetch, platform) {
48
+ export async function makeMarkupRequest({ tz = undefined, path = undefined, domain, apiKey, baseUrl = "https://go.jamcomments.com", environment = getEnvironment(), page = undefined, copy = {}, }, baseServicePath, fetchImplementation = fetch, platform) {
48
49
  const trimmedTimezone = tz?.trim();
49
50
  if (trimmedTimezone && !isValidTimezone(trimmedTimezone)) {
50
51
  throw new Error(`The timezone passed to JamComments is invalid: ${trimmedTimezone}`);
@@ -64,6 +65,9 @@ export async function makeMarkupRequest({ tz = undefined, path = undefined, doma
64
65
  if (environment !== "production") {
65
66
  params.set("stub", "true");
66
67
  }
68
+ for (const [key, value] of Object.entries(copy)) {
69
+ params.set(key, value);
70
+ }
67
71
  const requestUrl = `${baseUrl}${baseServicePath}?${params}`;
68
72
  const response = await fetchImplementation(requestUrl, {
69
73
  method: "GET",
@@ -82,7 +86,7 @@ export async function makeMarkupRequest({ tz = undefined, path = undefined, doma
82
86
  return response;
83
87
  }
84
88
  export function markupFetcher(platform, fetchImplementation = fetch) {
85
- return async ({ tz = undefined, path, domain, apiKey, schema, baseUrl = "https://go.jamcomments.com", environment = getEnvironment(), }) => {
89
+ return async ({ tz = undefined, path, domain, apiKey, schema, baseUrl = "https://go.jamcomments.com", environment = getEnvironment(), copy = {}, }) => {
86
90
  path = path || "/";
87
91
  const savedFile = (() => {
88
92
  if (!tempDirectoryExists()) {
@@ -92,7 +96,7 @@ export function markupFetcher(platform, fetchImplementation = fetch) {
92
96
  })();
93
97
  const markup = savedFile
94
98
  ? savedFile
95
- : await fetchFreshMarkup({ tz, path, domain, apiKey, baseUrl, environment }, fetchImplementation, platform);
99
+ : await fetchFreshMarkup({ tz, path, domain, apiKey, baseUrl, environment, copy }, fetchImplementation, platform);
96
100
  if (schema) {
97
101
  const preparedSchema = typeof schema !== "string" ? JSON.stringify(schema) : schema;
98
102
  const parsedSchema = parseJson(preparedSchema);
@@ -132,6 +132,50 @@ describe("fetchAll()", () => {
132
132
  }
133
133
  expect(deleteTempDirectorySpy).toHaveBeenCalledOnce();
134
134
  });
135
+ it.only("handles copy overrides", async () => {
136
+ const saveFileSpy = vi.spyOn(utilsExports, "saveFile");
137
+ const mockBatchFetcher = vi.fn().mockReturnValue({
138
+ data: [
139
+ { path: "/test", markup: "markup1" },
140
+ { path: "/test2", markup: "markup2" },
141
+ ],
142
+ meta: {
143
+ current_page: 1,
144
+ from: 1,
145
+ last_page: 1,
146
+ path: "/test",
147
+ per_page: 10,
148
+ to: 2,
149
+ total: 2,
150
+ },
151
+ });
152
+ const batchMarkupFetcherMock = vi.fn().mockImplementation((a, b) => {
153
+ return mockBatchFetcher;
154
+ });
155
+ await fetcherExports.fetchAll({
156
+ domain: "test.com",
157
+ apiKey: "123abc",
158
+ environment: "production",
159
+ copy: {
160
+ copy_confirmation_message: "custom confirmation message",
161
+ copy_submit_button: "custom submit button",
162
+ },
163
+ }, "test_platform", vi.fn(), batchMarkupFetcherMock);
164
+ expect(batchMarkupFetcherMock).toHaveBeenCalledWith("test_platform", expect.anything());
165
+ expect(mockBatchFetcher).toHaveBeenCalledWith({
166
+ domain: "test.com",
167
+ apiKey: "123abc",
168
+ baseUrl: "https://go.jamcomments.com",
169
+ copy: {
170
+ copy_confirmation_message: "custom confirmation message",
171
+ copy_submit_button: "custom submit button",
172
+ },
173
+ environment: "production",
174
+ page: 1,
175
+ tz: undefined,
176
+ });
177
+ expect(saveFileSpy).toHaveBeenCalledTimes(2);
178
+ });
135
179
  });
136
180
  describe("batchMarkupFetcher", () => {
137
181
  it("constructs fetch request correctly", async () => {
@@ -328,6 +372,43 @@ describe("markupFetcher", () => {
328
372
  apiKey: "123abc",
329
373
  })).rejects.toThrowError(/request failed! Status code: 500/);
330
374
  });
375
+ it("passes custom copy", async () => {
376
+ const fetchMock = vi.fn().mockImplementation(() => {
377
+ return {
378
+ status: 200,
379
+ ok: true,
380
+ text: () => "results!",
381
+ };
382
+ });
383
+ const fetcher = markupFetcher("test", fetchMock);
384
+ const result = await fetcher({
385
+ path: null,
386
+ domain: "test.com",
387
+ apiKey: "123abc",
388
+ environment: "production",
389
+ copy: {
390
+ copy_confirmation_message: "custom confirmation message",
391
+ copy_submit_button: "custom submit button",
392
+ },
393
+ });
394
+ expect(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?domain=test.com&path=%2F&copy_confirmation_message=custom+confirmation+message&copy_submit_button=custom+submit+button", expect.anything());
395
+ expect(result).toEqual("results!");
396
+ });
397
+ it("response isn't ok", async () => {
398
+ const fetchMock = vi.fn().mockImplementation(() => {
399
+ return {
400
+ status: 500,
401
+ ok: false,
402
+ text: () => "bad results!",
403
+ };
404
+ });
405
+ const fetcher = markupFetcher("test", fetchMock);
406
+ expect(fetcher({
407
+ path: "/test",
408
+ domain: "test.com",
409
+ apiKey: "123abc",
410
+ })).rejects.toThrowError(/request failed! Status code: 500/);
411
+ });
331
412
  describe("using saved markup", () => {
332
413
  it("makes fresh request when temp directory does not exist", async () => {
333
414
  const tempDirectoryExistsSpy = vi
@@ -398,6 +479,29 @@ describe("markupFetcher", () => {
398
479
  expect(result).toEqual("<!-- EMPTY -->");
399
480
  });
400
481
  });
482
+ describe("copy overrides", () => {
483
+ it("encodes copy overrides", async () => {
484
+ const fetchMock = vi.fn().mockImplementation(() => {
485
+ return {
486
+ status: 200,
487
+ ok: true,
488
+ text: () => "results!",
489
+ };
490
+ });
491
+ const fetcher = markupFetcher("test", fetchMock);
492
+ await fetcher({
493
+ path: "/some/path",
494
+ domain: "test.com",
495
+ apiKey: "123abc",
496
+ environment: "production",
497
+ copy: {
498
+ copy_confirmation_message: "custom confirmation message",
499
+ copy_submit_button: "custom submit button",
500
+ },
501
+ });
502
+ expect(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?domain=test.com&path=%2Fsome%2Fpath&copy_confirmation_message=custom+confirmation+message&copy_submit_button=custom+submit+button", expect.anything());
503
+ });
504
+ });
401
505
  describe("timezone validation", () => {
402
506
  it("throws error when invalid timezone is provided", async () => {
403
507
  const fetchImplMock = vi.fn();
package/dist/esm/utils.js CHANGED
@@ -17,13 +17,6 @@ export function getEnvironment() {
17
17
  process.env?.NODE_ENV ||
18
18
  "development");
19
19
  }
20
- export function reAppendMarkup(element, markup) {
21
- const range = document.createRange();
22
- range.selectNode(element);
23
- const documentFragment = range.createContextualFragment(markup);
24
- element.innerHTML = "";
25
- element.append(documentFragment);
26
- }
27
20
  export function parseJson(json) {
28
21
  try {
29
22
  return JSON.parse(json);
@@ -1,4 +1,4 @@
1
1
  export declare const TEMP_DIRECTORY: string;
2
2
  export { markupFetcher, fetchAll } from "./markupFetcher";
3
- export { reAppendMarkup, deleteTempDirectory } from "./utils";
3
+ export { deleteTempDirectory } from "./utils";
4
4
  export { log, logError } from "./log";
@@ -6,6 +6,17 @@ export interface IFetchData {
6
6
  tz?: string;
7
7
  baseUrl?: string;
8
8
  environment?: string;
9
+ copy?: {
10
+ copy_confirmation_message?: string;
11
+ copy_submit_button?: string;
12
+ copy_name_placeholder?: string;
13
+ copy_email_placeholder?: string;
14
+ copy_comment_placeholder?: string;
15
+ copy_write_tab?: string;
16
+ copy_preview_tab?: string;
17
+ copy_auth_button?: string;
18
+ copy_log_out_button?: string;
19
+ };
9
20
  }
10
21
  export type IBatchFetchData = Omit<IFetchData, "path" | "schema"> & {
11
22
  page?: number;
@@ -25,9 +36,9 @@ interface IBatchResponse {
25
36
  total: number;
26
37
  };
27
38
  }
28
- export declare function fetchAll({ tz, domain, apiKey, baseUrl, environment, }: IBatchFetchData, platform: string, fetchImplementation?: any, batchMarkupFetcherImpl?: any): Promise<void>;
39
+ export declare function fetchAll({ tz, domain, apiKey, baseUrl, environment, copy, }: IBatchFetchData, platform: string, fetchImplementation?: any, batchMarkupFetcherImpl?: any): Promise<void>;
29
40
  export declare function batchMarkupFetcher(platform: string, fetchImplementation?: typeof fetch): (args: IBatchFetchData) => Promise<IBatchResponse>;
30
- export declare function fetchFreshMarkup({ tz, path, domain, apiKey, baseUrl, environment, }: IFetchData, fetchImplementation: typeof fetch, platform: string): Promise<string>;
31
- export declare function makeMarkupRequest<T extends Partial<IBatchFetchData & IFetchData>>({ tz, path, domain, apiKey, baseUrl, environment, page, }: T, baseServicePath: string, fetchImplementation: typeof fetch, platform: string): Promise<Response>;
41
+ export declare function fetchFreshMarkup({ tz, path, domain, apiKey, baseUrl, environment, copy, }: IFetchData, fetchImplementation: typeof fetch, platform: string): Promise<string>;
42
+ export declare function makeMarkupRequest<T extends Partial<IBatchFetchData & IFetchData>>({ tz, path, domain, apiKey, baseUrl, environment, page, copy, }: T, baseServicePath: string, fetchImplementation: typeof fetch, platform: string): Promise<Response>;
32
43
  export declare function markupFetcher(platform: string, fetchImplementation?: typeof fetch): (args: IFetchData) => Promise<string>;
33
44
  export {};
@@ -1,8 +1,6 @@
1
- /// <reference types="node" />
2
1
  import { existsSync, mkdirSync, readFileSync } from "fs";
3
2
  export declare function isValidTimezone(tz: string): boolean;
4
3
  export declare function getEnvironment(): string;
5
- export declare function reAppendMarkup(element: HTMLElement, markup: string): void;
6
4
  export declare function parseJson(json: string): any;
7
5
  export declare function unescapeHTML(str: any): any;
8
6
  export declare function deleteTempDirectory(): void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jam-comments/server-utilities",
3
- "version": "4.3.4",
3
+ "version": "5.1.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": "^20.12.12",
26
- "prettier": "^3.2.5",
27
- "typescript": "^5.4.5",
28
- "vite": "^5.2.11",
29
- "vitest": "^1.6.0"
25
+ "@types/node": "^20.14.11",
26
+ "prettier": "^3.3.3",
27
+ "typescript": "^5.5.3",
28
+ "vite": "^5.3.4",
29
+ "vitest": "^2.0.3"
30
30
  },
31
31
  "publishConfig": {
32
32
  "access": "public"