@jam-comments/server-utilities 5.0.0 → 5.2.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.
@@ -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.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,7 +30,6 @@ function getEnvironment() {
21
30
  process.env?.NODE_ENV ||
22
31
  "development");
23
32
  }
24
- exports.getEnvironment = getEnvironment;
25
33
  function parseJson(json) {
26
34
  try {
27
35
  return JSON.parse(json);
@@ -30,7 +38,6 @@ function parseJson(json) {
30
38
  return null;
31
39
  }
32
40
  }
33
- exports.parseJson = parseJson;
34
41
  function unescapeHTML(str) {
35
42
  var htmlEntities = {
36
43
  nbsp: " ",
@@ -62,23 +69,19 @@ function unescapeHTML(str) {
62
69
  }
63
70
  });
64
71
  }
65
- exports.unescapeHTML = unescapeHTML;
66
72
  function deleteTempDirectory() {
67
73
  if ((0, fs_1.existsSync)(_1.TEMP_DIRECTORY)) {
68
74
  (0, fs_1.rmdirSync)(_1.TEMP_DIRECTORY, { recursive: true });
69
75
  }
70
76
  }
71
- exports.deleteTempDirectory = deleteTempDirectory;
72
77
  function createTempDirectory(mkdirSyncImpl = fs_1.mkdirSync, existsSyncImpl = fs_1.existsSync) {
73
78
  if (!tempDirectoryExists(existsSyncImpl)) {
74
79
  mkdirSyncImpl(_1.TEMP_DIRECTORY);
75
80
  }
76
81
  }
77
- exports.createTempDirectory = createTempDirectory;
78
82
  function toSavedFileName(name) {
79
83
  return name.replace(/^\//, "").replace(/\//g, "::");
80
84
  }
81
- exports.toSavedFileName = toSavedFileName;
82
85
  function readFile(name, readFileSyncImpl = fs_1.readFileSync) {
83
86
  const fileName = toSavedFileName(name);
84
87
  try {
@@ -88,15 +91,12 @@ function readFile(name, readFileSyncImpl = fs_1.readFileSync) {
88
91
  return null;
89
92
  }
90
93
  }
91
- exports.readFile = readFile;
92
94
  function getEmptyMarkup() {
93
95
  return readFile("EMPTY");
94
96
  }
95
- exports.getEmptyMarkup = getEmptyMarkup;
96
97
  function tempDirectoryExists(existsSyncImpl = fs_1.existsSync) {
97
98
  return existsSyncImpl(_1.TEMP_DIRECTORY);
98
99
  }
99
- exports.tempDirectoryExists = tempDirectoryExists;
100
100
  function saveFile(name, data, writeFileImpl = fs_1.writeFile) {
101
101
  const fileName = toSavedFileName(name);
102
102
  return new Promise((resolve) => {
@@ -105,4 +105,3 @@ function saveFile(name, data, writeFileImpl = fs_1.writeFile) {
105
105
  });
106
106
  });
107
107
  }
108
- exports.saveFile = saveFile;
@@ -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();
@@ -1,3 +1,4 @@
1
+ export type { CustomCopy } from "./markupFetcher";
1
2
  export declare const TEMP_DIRECTORY: string;
2
3
  export { markupFetcher, fetchAll } from "./markupFetcher";
3
4
  export { deleteTempDirectory } from "./utils";
@@ -1,3 +1,14 @@
1
+ export interface CustomCopy {
2
+ confirmationMessage?: string;
3
+ submitButton?: string;
4
+ namePlaceholder?: string;
5
+ emailPlaceholder?: string;
6
+ commentPlaceholder?: string;
7
+ writeTab?: string;
8
+ previewTab?: string;
9
+ authButton?: string;
10
+ logOutButton?: string;
11
+ }
1
12
  export interface IFetchData {
2
13
  path: string;
3
14
  domain: string;
@@ -6,6 +17,17 @@ export interface IFetchData {
6
17
  tz?: string;
7
18
  baseUrl?: string;
8
19
  environment?: string;
20
+ copy?: {
21
+ copy_confirmation_message?: string;
22
+ copy_submit_button?: string;
23
+ copy_name_placeholder?: string;
24
+ copy_email_placeholder?: string;
25
+ copy_comment_placeholder?: string;
26
+ copy_write_tab?: string;
27
+ copy_preview_tab?: string;
28
+ copy_auth_button?: string;
29
+ copy_log_out_button?: string;
30
+ };
9
31
  }
10
32
  export type IBatchFetchData = Omit<IFetchData, "path" | "schema"> & {
11
33
  page?: number;
@@ -25,9 +47,9 @@ interface IBatchResponse {
25
47
  total: number;
26
48
  };
27
49
  }
28
- export declare function fetchAll({ tz, domain, apiKey, baseUrl, environment, }: IBatchFetchData, platform: string, fetchImplementation?: any, batchMarkupFetcherImpl?: any): Promise<void>;
50
+ export declare function fetchAll({ tz, domain, apiKey, baseUrl, environment, copy, }: IBatchFetchData, platform: string, fetchImplementation?: any, batchMarkupFetcherImpl?: any): Promise<void>;
29
51
  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>;
52
+ export declare function fetchFreshMarkup({ tz, path, domain, apiKey, baseUrl, environment, copy, }: IFetchData, fetchImplementation: typeof fetch, platform: string): Promise<string>;
53
+ 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
54
  export declare function markupFetcher(platform: string, fetchImplementation?: typeof fetch): (args: IFetchData) => Promise<string>;
33
55
  export {};
@@ -1,4 +1,3 @@
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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jam-comments/server-utilities",
3
- "version": "5.0.0",
3
+ "version": "5.2.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"