@jam-comments/server-utilities 5.3.0 → 5.4.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.
@@ -7,7 +7,8 @@ exports.makeMarkupRequest = makeMarkupRequest;
7
7
  exports.markupFetcher = markupFetcher;
8
8
  const injectSchema_1 = require("./injectSchema");
9
9
  const utils_1 = require("./utils");
10
- async function fetchAll({ tz = undefined, domain, apiKey, baseUrl = "https://go.jamcomments.com", environment = (0, utils_1.getEnvironment)(), copy = {}, }, platform, fetchImplementation = fetch, batchMarkupFetcherImpl = batchMarkupFetcher) {
10
+ const BASE_URL = "https://go.jamcomments.com";
11
+ async function fetchAll({ tz = undefined, dateFormat = undefined, domain, apiKey, baseUrl = BASE_URL, environment = (0, utils_1.getEnvironment)(), copy = {}, }, platform, fetchImplementation = fetch, batchMarkupFetcherImpl = batchMarkupFetcher) {
11
12
  const fetchBatchMarkup = batchMarkupFetcherImpl(platform, fetchImplementation);
12
13
  (0, utils_1.createTempDirectory)();
13
14
  let shouldKeepFetching = true;
@@ -22,6 +23,7 @@ async function fetchAll({ tz = undefined, domain, apiKey, baseUrl = "https://go.
22
23
  environment,
23
24
  page,
24
25
  tz,
26
+ dateFormat,
25
27
  copy,
26
28
  });
27
29
  console.log(`Checking for comment data. Batch: ${current_page}/${last_page}`);
@@ -43,16 +45,16 @@ async function fetchAll({ tz = undefined, domain, apiKey, baseUrl = "https://go.
43
45
  }
44
46
  }
45
47
  function batchMarkupFetcher(platform, fetchImplementation = fetch) {
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);
48
+ return async ({ tz, copy, domain, apiKey, dateFormat, baseUrl = BASE_URL, environment = (0, utils_1.getEnvironment)(), page = 1, }) => {
49
+ const response = await makeMarkupRequest({ tz, domain, apiKey, baseUrl, environment, page, copy, dateFormat }, "/api/v3/markup/all", fetchImplementation, platform);
48
50
  return response.json();
49
51
  };
50
52
  }
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);
53
+ async function fetchFreshMarkup({ tz, path, copy, domain, apiKey, dateFormat, baseUrl = BASE_URL, environment = (0, utils_1.getEnvironment)(), }, fetchImplementation = fetch, platform) {
54
+ const response = await makeMarkupRequest({ tz, path, domain, apiKey, baseUrl, environment, copy, dateFormat }, "/api/v3/markup", fetchImplementation, platform);
53
55
  return response.text();
54
56
  }
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) {
57
+ async function makeMarkupRequest({ tz, path, page, domain, apiKey, dateFormat, copy = {}, baseUrl = BASE_URL, environment = (0, utils_1.getEnvironment)(), }, baseServicePath, fetchImplementation = fetch, platform) {
56
58
  const trimmedTimezone = tz?.trim();
57
59
  if (trimmedTimezone && !(0, utils_1.isValidTimezone)(trimmedTimezone)) {
58
60
  throw new Error(`The timezone passed to JamComments is invalid: ${trimmedTimezone}`);
@@ -69,6 +71,9 @@ async function makeMarkupRequest({ tz = undefined, path = undefined, domain, api
69
71
  if (trimmedTimezone) {
70
72
  params.set("tz", trimmedTimezone);
71
73
  }
74
+ if (dateFormat) {
75
+ params.set("date_format", dateFormat);
76
+ }
72
77
  if (environment !== "production") {
73
78
  params.set("stub", "true");
74
79
  }
@@ -93,7 +98,7 @@ async function makeMarkupRequest({ tz = undefined, path = undefined, domain, api
93
98
  return response;
94
99
  }
95
100
  function markupFetcher(platform, fetchImplementation = fetch) {
96
- return async ({ tz = undefined, path, domain, apiKey, schema, baseUrl = "https://go.jamcomments.com", environment = (0, utils_1.getEnvironment)(), copy = {}, }) => {
101
+ return async ({ tz = undefined, path, domain, apiKey, schema, dateFormat, baseUrl = BASE_URL, environment = (0, utils_1.getEnvironment)(), copy = {}, }) => {
97
102
  path = path || "/";
98
103
  const savedFile = (() => {
99
104
  if (!(0, utils_1.tempDirectoryExists)()) {
@@ -103,7 +108,7 @@ function markupFetcher(platform, fetchImplementation = fetch) {
103
108
  })();
104
109
  const markup = savedFile
105
110
  ? savedFile
106
- : await fetchFreshMarkup({ tz, path, domain, apiKey, baseUrl, environment, copy }, fetchImplementation, platform);
111
+ : await fetchFreshMarkup({ tz, path, domain, apiKey, baseUrl, environment, copy, dateFormat }, fetchImplementation, platform);
107
112
  if (schema) {
108
113
  const preparedSchema = typeof schema !== "string" ? JSON.stringify(schema) : schema;
109
114
  const parsedSchema = (0, utils_1.parseJson)(preparedSchema);
@@ -67,6 +67,8 @@ const { markupFetcher, batchMarkupFetcher } = fetcherExports;
67
67
  domain: "test.com",
68
68
  apiKey: "123abc",
69
69
  baseUrl: "https://go.jamcomments.com",
70
+ copy: {},
71
+ dateFormat: undefined,
70
72
  environment: "production",
71
73
  page: 1,
72
74
  tz: undefined,
@@ -120,6 +122,8 @@ const { markupFetcher, batchMarkupFetcher } = fetcherExports;
120
122
  domain: "test.com",
121
123
  apiKey: "123abc",
122
124
  baseUrl: "https://go.jamcomments.com",
125
+ copy: {},
126
+ dateFormat: undefined,
123
127
  environment: "production",
124
128
  page: 1,
125
129
  tz: undefined,
@@ -129,6 +133,8 @@ const { markupFetcher, batchMarkupFetcher } = fetcherExports;
129
133
  domain: "test.com",
130
134
  apiKey: "123abc",
131
135
  baseUrl: "https://go.jamcomments.com",
136
+ copy: {},
137
+ dateFormat: undefined,
132
138
  environment: "production",
133
139
  page: 2,
134
140
  tz: undefined,
@@ -157,7 +163,7 @@ const { markupFetcher, batchMarkupFetcher } = fetcherExports;
157
163
  }
158
164
  (0, vitest_1.expect)(deleteTempDirectorySpy).toHaveBeenCalledOnce();
159
165
  });
160
- vitest_1.it.only("handles copy overrides", async () => {
166
+ (0, vitest_1.it)("handles copy overrides", async () => {
161
167
  const saveFileSpy = vitest_1.vi.spyOn(utilsExports, "saveFile");
162
168
  const mockBatchFetcher = vitest_1.vi.fn().mockReturnValue({
163
169
  data: [
@@ -201,6 +207,42 @@ const { markupFetcher, batchMarkupFetcher } = fetcherExports;
201
207
  });
202
208
  (0, vitest_1.expect)(saveFileSpy).toHaveBeenCalledTimes(2);
203
209
  });
210
+ (0, vitest_1.it)("allows you to pass a custom date format", async () => {
211
+ const mockBatchFetcher = vitest_1.vi.fn().mockReturnValue({
212
+ data: [
213
+ { path: "/test", markup: "markup1" },
214
+ { path: "/test2", markup: "markup2" },
215
+ ],
216
+ meta: {
217
+ current_page: 1,
218
+ from: 1,
219
+ last_page: 1,
220
+ path: "/test",
221
+ per_page: 10,
222
+ to: 2,
223
+ total: 2,
224
+ },
225
+ });
226
+ const batchMarkupFetcherMock = vitest_1.vi.fn().mockImplementation((a, b) => {
227
+ return mockBatchFetcher;
228
+ });
229
+ await fetcherExports.fetchAll({
230
+ domain: "test.com",
231
+ apiKey: "123abc",
232
+ environment: "production",
233
+ dateFormat: "yyyy-MM-dd",
234
+ }, "test_platform", vitest_1.vi.fn(), batchMarkupFetcherMock);
235
+ (0, vitest_1.expect)(mockBatchFetcher).toHaveBeenCalledWith({
236
+ domain: "test.com",
237
+ apiKey: "123abc",
238
+ baseUrl: "https://go.jamcomments.com",
239
+ copy: {},
240
+ dateFormat: "yyyy-MM-dd",
241
+ environment: "production",
242
+ page: 1,
243
+ tz: undefined,
244
+ });
245
+ });
204
246
  });
205
247
  (0, vitest_1.describe)("batchMarkupFetcher", () => {
206
248
  (0, vitest_1.it)("constructs fetch request correctly", async () => {
@@ -419,6 +461,24 @@ const { markupFetcher, batchMarkupFetcher } = fetcherExports;
419
461
  (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
462
  (0, vitest_1.expect)(result).toEqual("results!");
421
463
  });
464
+ (0, vitest_1.it)("passes date format string", async () => {
465
+ const fetchMock = vitest_1.vi.fn().mockImplementation(() => {
466
+ return {
467
+ status: 200,
468
+ ok: true,
469
+ text: () => "results!",
470
+ };
471
+ });
472
+ const fetcher = markupFetcher("test", fetchMock);
473
+ await fetcher({
474
+ path: null,
475
+ domain: "test.com",
476
+ apiKey: "123abc",
477
+ environment: "production",
478
+ dateFormat: "yyyy-MM-dd",
479
+ });
480
+ (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());
481
+ });
422
482
  (0, vitest_1.it)("response isn't ok", async () => {
423
483
  const fetchMock = vitest_1.vi.fn().mockImplementation(() => {
424
484
  return {
@@ -1,6 +1,7 @@
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(), copy = {}, }, platform, fetchImplementation = fetch, batchMarkupFetcherImpl = batchMarkupFetcher) {
3
+ const BASE_URL = "https://go.jamcomments.com";
4
+ export async function fetchAll({ tz = undefined, dateFormat = undefined, domain, apiKey, baseUrl = BASE_URL, environment = getEnvironment(), copy = {}, }, platform, fetchImplementation = fetch, batchMarkupFetcherImpl = batchMarkupFetcher) {
4
5
  const fetchBatchMarkup = batchMarkupFetcherImpl(platform, fetchImplementation);
5
6
  createTempDirectory();
6
7
  let shouldKeepFetching = true;
@@ -15,6 +16,7 @@ export async function fetchAll({ tz = undefined, domain, apiKey, baseUrl = "http
15
16
  environment,
16
17
  page,
17
18
  tz,
19
+ dateFormat,
18
20
  copy,
19
21
  });
20
22
  console.log(`Checking for comment data. Batch: ${current_page}/${last_page}`);
@@ -36,16 +38,16 @@ export async function fetchAll({ tz = undefined, domain, apiKey, baseUrl = "http
36
38
  }
37
39
  }
38
40
  export function batchMarkupFetcher(platform, fetchImplementation = fetch) {
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);
41
+ return async ({ tz, copy, domain, apiKey, dateFormat, baseUrl = BASE_URL, environment = getEnvironment(), page = 1, }) => {
42
+ const response = await makeMarkupRequest({ tz, domain, apiKey, baseUrl, environment, page, copy, dateFormat }, "/api/v3/markup/all", fetchImplementation, platform);
41
43
  return response.json();
42
44
  };
43
45
  }
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);
46
+ export async function fetchFreshMarkup({ tz, path, copy, domain, apiKey, dateFormat, baseUrl = BASE_URL, environment = getEnvironment(), }, fetchImplementation = fetch, platform) {
47
+ const response = await makeMarkupRequest({ tz, path, domain, apiKey, baseUrl, environment, copy, dateFormat }, "/api/v3/markup", fetchImplementation, platform);
46
48
  return response.text();
47
49
  }
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) {
50
+ export async function makeMarkupRequest({ tz, path, page, domain, apiKey, dateFormat, copy = {}, baseUrl = BASE_URL, environment = getEnvironment(), }, baseServicePath, fetchImplementation = fetch, platform) {
49
51
  const trimmedTimezone = tz?.trim();
50
52
  if (trimmedTimezone && !isValidTimezone(trimmedTimezone)) {
51
53
  throw new Error(`The timezone passed to JamComments is invalid: ${trimmedTimezone}`);
@@ -62,6 +64,9 @@ export async function makeMarkupRequest({ tz = undefined, path = undefined, doma
62
64
  if (trimmedTimezone) {
63
65
  params.set("tz", trimmedTimezone);
64
66
  }
67
+ if (dateFormat) {
68
+ params.set("date_format", dateFormat);
69
+ }
65
70
  if (environment !== "production") {
66
71
  params.set("stub", "true");
67
72
  }
@@ -86,7 +91,7 @@ export async function makeMarkupRequest({ tz = undefined, path = undefined, doma
86
91
  return response;
87
92
  }
88
93
  export function markupFetcher(platform, fetchImplementation = fetch) {
89
- return async ({ tz = undefined, path, domain, apiKey, schema, baseUrl = "https://go.jamcomments.com", environment = getEnvironment(), copy = {}, }) => {
94
+ return async ({ tz = undefined, path, domain, apiKey, schema, dateFormat, baseUrl = BASE_URL, environment = getEnvironment(), copy = {}, }) => {
90
95
  path = path || "/";
91
96
  const savedFile = (() => {
92
97
  if (!tempDirectoryExists()) {
@@ -96,7 +101,7 @@ export function markupFetcher(platform, fetchImplementation = fetch) {
96
101
  })();
97
102
  const markup = savedFile
98
103
  ? savedFile
99
- : await fetchFreshMarkup({ tz, path, domain, apiKey, baseUrl, environment, copy }, fetchImplementation, platform);
104
+ : await fetchFreshMarkup({ tz, path, domain, apiKey, baseUrl, environment, copy, dateFormat }, fetchImplementation, platform);
100
105
  if (schema) {
101
106
  const preparedSchema = typeof schema !== "string" ? JSON.stringify(schema) : schema;
102
107
  const parsedSchema = parseJson(preparedSchema);
@@ -42,6 +42,8 @@ describe("fetchAll()", () => {
42
42
  domain: "test.com",
43
43
  apiKey: "123abc",
44
44
  baseUrl: "https://go.jamcomments.com",
45
+ copy: {},
46
+ dateFormat: undefined,
45
47
  environment: "production",
46
48
  page: 1,
47
49
  tz: undefined,
@@ -95,6 +97,8 @@ describe("fetchAll()", () => {
95
97
  domain: "test.com",
96
98
  apiKey: "123abc",
97
99
  baseUrl: "https://go.jamcomments.com",
100
+ copy: {},
101
+ dateFormat: undefined,
98
102
  environment: "production",
99
103
  page: 1,
100
104
  tz: undefined,
@@ -104,6 +108,8 @@ describe("fetchAll()", () => {
104
108
  domain: "test.com",
105
109
  apiKey: "123abc",
106
110
  baseUrl: "https://go.jamcomments.com",
111
+ copy: {},
112
+ dateFormat: undefined,
107
113
  environment: "production",
108
114
  page: 2,
109
115
  tz: undefined,
@@ -132,7 +138,7 @@ describe("fetchAll()", () => {
132
138
  }
133
139
  expect(deleteTempDirectorySpy).toHaveBeenCalledOnce();
134
140
  });
135
- it.only("handles copy overrides", async () => {
141
+ it("handles copy overrides", async () => {
136
142
  const saveFileSpy = vi.spyOn(utilsExports, "saveFile");
137
143
  const mockBatchFetcher = vi.fn().mockReturnValue({
138
144
  data: [
@@ -176,6 +182,42 @@ describe("fetchAll()", () => {
176
182
  });
177
183
  expect(saveFileSpy).toHaveBeenCalledTimes(2);
178
184
  });
185
+ it("allows you to pass a custom date format", async () => {
186
+ const mockBatchFetcher = vi.fn().mockReturnValue({
187
+ data: [
188
+ { path: "/test", markup: "markup1" },
189
+ { path: "/test2", markup: "markup2" },
190
+ ],
191
+ meta: {
192
+ current_page: 1,
193
+ from: 1,
194
+ last_page: 1,
195
+ path: "/test",
196
+ per_page: 10,
197
+ to: 2,
198
+ total: 2,
199
+ },
200
+ });
201
+ const batchMarkupFetcherMock = vi.fn().mockImplementation((a, b) => {
202
+ return mockBatchFetcher;
203
+ });
204
+ await fetcherExports.fetchAll({
205
+ domain: "test.com",
206
+ apiKey: "123abc",
207
+ environment: "production",
208
+ dateFormat: "yyyy-MM-dd",
209
+ }, "test_platform", vi.fn(), batchMarkupFetcherMock);
210
+ expect(mockBatchFetcher).toHaveBeenCalledWith({
211
+ domain: "test.com",
212
+ apiKey: "123abc",
213
+ baseUrl: "https://go.jamcomments.com",
214
+ copy: {},
215
+ dateFormat: "yyyy-MM-dd",
216
+ environment: "production",
217
+ page: 1,
218
+ tz: undefined,
219
+ });
220
+ });
179
221
  });
180
222
  describe("batchMarkupFetcher", () => {
181
223
  it("constructs fetch request correctly", async () => {
@@ -394,6 +436,24 @@ describe("markupFetcher", () => {
394
436
  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
437
  expect(result).toEqual("results!");
396
438
  });
439
+ it("passes date format string", async () => {
440
+ const fetchMock = vi.fn().mockImplementation(() => {
441
+ return {
442
+ status: 200,
443
+ ok: true,
444
+ text: () => "results!",
445
+ };
446
+ });
447
+ const fetcher = markupFetcher("test", fetchMock);
448
+ await fetcher({
449
+ path: null,
450
+ domain: "test.com",
451
+ apiKey: "123abc",
452
+ environment: "production",
453
+ dateFormat: "yyyy-MM-dd",
454
+ });
455
+ expect(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?domain=test.com&path=%2F&date_format=yyyy-MM-dd", expect.anything());
456
+ });
397
457
  it("response isn't ok", async () => {
398
458
  const fetchMock = vi.fn().mockImplementation(() => {
399
459
  return {
@@ -17,6 +17,7 @@ export interface IFetchData {
17
17
  tz?: string;
18
18
  baseUrl?: string;
19
19
  environment?: string;
20
+ dateFormat?: string;
20
21
  copy?: {
21
22
  copy_confirmation_message?: string;
22
23
  copy_submit_button?: string;
@@ -47,9 +48,9 @@ interface IBatchResponse {
47
48
  total: number;
48
49
  };
49
50
  }
50
- export declare function fetchAll({ tz, domain, apiKey, baseUrl, environment, copy, }: IBatchFetchData, platform: string, fetchImplementation?: any, batchMarkupFetcherImpl?: any): Promise<void>;
51
+ export declare function fetchAll({ tz, dateFormat, domain, apiKey, baseUrl, environment, copy, }: IBatchFetchData, platform: string, fetchImplementation?: any, batchMarkupFetcherImpl?: any): Promise<void>;
51
52
  export declare function batchMarkupFetcher(platform: string, fetchImplementation?: typeof fetch): (args: IBatchFetchData) => Promise<IBatchResponse>;
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>;
53
+ export declare function fetchFreshMarkup({ tz, path, copy, domain, apiKey, dateFormat, baseUrl, environment, }: IFetchData, fetchImplementation: typeof fetch, platform: string): Promise<string>;
54
+ export declare function makeMarkupRequest<T extends Partial<IBatchFetchData & IFetchData>>({ tz, path, page, domain, apiKey, dateFormat, copy, baseUrl, environment, }: T, baseServicePath: string, fetchImplementation: typeof fetch, platform: string): Promise<Response>;
54
55
  export declare function markupFetcher(platform: string, fetchImplementation?: typeof fetch): (args: IFetchData) => Promise<string>;
55
56
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jam-comments/server-utilities",
3
- "version": "5.3.0",
3
+ "version": "5.4.0",
4
4
  "description": "Various JavaScript utilities for JamComments.",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -24,9 +24,9 @@
24
24
  "devDependencies": {
25
25
  "@types/node": "^20.14.11",
26
26
  "prettier": "^3.3.3",
27
- "typescript": "^5.5.3",
27
+ "typescript": "^5.5.4",
28
28
  "vite": "^5.3.4",
29
- "vitest": "^2.0.3"
29
+ "vitest": "^2.0.4"
30
30
  },
31
31
  "publishConfig": {
32
32
  "access": "public"