@jam-comments/server-utilities 5.11.0 → 5.12.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.
|
@@ -51,11 +51,11 @@ function batchMarkupFetcher(platform, fetchImplementation = fetch) {
|
|
|
51
51
|
return response.json();
|
|
52
52
|
};
|
|
53
53
|
}
|
|
54
|
-
async function fetchFreshMarkup({ tz, path, copy, domain, apiKey, dateFormat, baseUrl = exports.BASE_URL, environment = (0, utils_1.getEnvironment)(), }, fetchImplementation = fetch, platform) {
|
|
55
|
-
const response = await makeMarkupRequest({ tz, path, domain, apiKey, baseUrl, environment, copy, dateFormat }, "/api/v3/markup", fetchImplementation, platform);
|
|
54
|
+
async function fetchFreshMarkup({ tz, path, copy, cache, domain, apiKey, dateFormat, baseUrl = exports.BASE_URL, environment = (0, utils_1.getEnvironment)(), }, fetchImplementation = fetch, platform) {
|
|
55
|
+
const response = await makeMarkupRequest({ tz, path, domain, apiKey, baseUrl, environment, copy, dateFormat, cache }, "/api/v3/markup", fetchImplementation, platform);
|
|
56
56
|
return response.text();
|
|
57
57
|
}
|
|
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
|
+
async function makeMarkupRequest({ tz, path, page, cache, domain, apiKey, dateFormat, copy = {}, baseUrl = exports.BASE_URL, environment = (0, utils_1.getEnvironment)(), }, baseServicePath, fetchImplementation = fetch, platform) {
|
|
59
59
|
const trimmedTimezone = tz?.trim();
|
|
60
60
|
if (trimmedTimezone && !(0, utils_1.isValidTimezone)(trimmedTimezone)) {
|
|
61
61
|
throw new Error(`The timezone passed to JamComments is invalid: ${trimmedTimezone}`);
|
|
@@ -63,6 +63,10 @@ async function makeMarkupRequest({ tz, path, page, domain, apiKey, dateFormat, c
|
|
|
63
63
|
const params = new URLSearchParams({
|
|
64
64
|
domain,
|
|
65
65
|
});
|
|
66
|
+
if (cache !== undefined) {
|
|
67
|
+
console.info("DOING IT", cache);
|
|
68
|
+
params.set("cache", cache.toString());
|
|
69
|
+
}
|
|
66
70
|
if (path) {
|
|
67
71
|
params.set("path", path);
|
|
68
72
|
}
|
|
@@ -99,7 +103,7 @@ async function makeMarkupRequest({ tz, path, page, domain, apiKey, dateFormat, c
|
|
|
99
103
|
return response;
|
|
100
104
|
}
|
|
101
105
|
function markupFetcher(platform, fetchImplementation = fetch, store = new store_1.Store()) {
|
|
102
|
-
return async ({ tz = undefined, path, domain, apiKey, schema, dateFormat, baseUrl = exports.BASE_URL, environment = (0, utils_1.getEnvironment)(), copy = {}, }) => {
|
|
106
|
+
return async ({ tz = undefined, path, cache, domain, apiKey, schema, dateFormat, baseUrl = exports.BASE_URL, environment = (0, utils_1.getEnvironment)(), copy = {}, }) => {
|
|
103
107
|
path = path || "/";
|
|
104
108
|
const cachedMarkup = (() => {
|
|
105
109
|
if (!store.hasData) {
|
|
@@ -109,7 +113,17 @@ function markupFetcher(platform, fetchImplementation = fetch, store = new store_
|
|
|
109
113
|
})();
|
|
110
114
|
const markup = cachedMarkup
|
|
111
115
|
? cachedMarkup
|
|
112
|
-
: await fetchFreshMarkup({
|
|
116
|
+
: await fetchFreshMarkup({
|
|
117
|
+
tz,
|
|
118
|
+
path,
|
|
119
|
+
domain,
|
|
120
|
+
apiKey,
|
|
121
|
+
baseUrl,
|
|
122
|
+
environment,
|
|
123
|
+
copy,
|
|
124
|
+
dateFormat,
|
|
125
|
+
cache,
|
|
126
|
+
}, fetchImplementation, platform);
|
|
113
127
|
if (schema) {
|
|
114
128
|
const preparedSchema = typeof schema !== "string" ? JSON.stringify(schema) : schema;
|
|
115
129
|
const parsedSchema = (0, utils_1.parseJson)(preparedSchema);
|
|
@@ -369,6 +369,25 @@ const { markupFetcher, batchMarkupFetcher } = fetcherExports;
|
|
|
369
369
|
(0, vitest_1.expect)(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?domain=test.com&path=%2Ftest&stub=true", vitest_1.expect.anything());
|
|
370
370
|
(0, vitest_1.expect)(result).toEqual("results!");
|
|
371
371
|
});
|
|
372
|
+
(0, vitest_1.it)("sets cache control header on response", async () => {
|
|
373
|
+
const fetchMock = vitest_1.vi.fn().mockImplementation(() => {
|
|
374
|
+
return {
|
|
375
|
+
status: 200,
|
|
376
|
+
ok: true,
|
|
377
|
+
text: () => "results!",
|
|
378
|
+
};
|
|
379
|
+
});
|
|
380
|
+
const fetcher = markupFetcher("test", fetchMock);
|
|
381
|
+
const result = await fetcher({
|
|
382
|
+
path: "/test",
|
|
383
|
+
domain: "test.com",
|
|
384
|
+
apiKey: "123abc",
|
|
385
|
+
environment: "development",
|
|
386
|
+
cache: true,
|
|
387
|
+
});
|
|
388
|
+
(0, vitest_1.expect)(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?domain=test.com&cache=true&path=%2Ftest&stub=true", vitest_1.expect.anything());
|
|
389
|
+
(0, vitest_1.expect)(result).toEqual("results!");
|
|
390
|
+
});
|
|
372
391
|
(0, vitest_1.it)("uses different base URL", async () => {
|
|
373
392
|
const fetchMock = vitest_1.vi.fn().mockImplementation(() => {
|
|
374
393
|
return {
|
|
@@ -43,11 +43,11 @@ export function batchMarkupFetcher(platform, fetchImplementation = fetch) {
|
|
|
43
43
|
return response.json();
|
|
44
44
|
};
|
|
45
45
|
}
|
|
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
|
+
export async function fetchFreshMarkup({ tz, path, copy, cache, domain, apiKey, dateFormat, baseUrl = BASE_URL, environment = getEnvironment(), }, fetchImplementation = fetch, platform) {
|
|
47
|
+
const response = await makeMarkupRequest({ tz, path, domain, apiKey, baseUrl, environment, copy, dateFormat, cache }, "/api/v3/markup", fetchImplementation, platform);
|
|
48
48
|
return response.text();
|
|
49
49
|
}
|
|
50
|
-
export async function makeMarkupRequest({ tz, path, page, domain, apiKey, dateFormat, copy = {}, baseUrl = BASE_URL, environment = getEnvironment(), }, baseServicePath, fetchImplementation = fetch, platform) {
|
|
50
|
+
export async function makeMarkupRequest({ tz, path, page, cache, domain, apiKey, dateFormat, copy = {}, baseUrl = BASE_URL, environment = getEnvironment(), }, baseServicePath, fetchImplementation = fetch, platform) {
|
|
51
51
|
const trimmedTimezone = tz?.trim();
|
|
52
52
|
if (trimmedTimezone && !isValidTimezone(trimmedTimezone)) {
|
|
53
53
|
throw new Error(`The timezone passed to JamComments is invalid: ${trimmedTimezone}`);
|
|
@@ -55,6 +55,10 @@ export async function makeMarkupRequest({ tz, path, page, domain, apiKey, dateFo
|
|
|
55
55
|
const params = new URLSearchParams({
|
|
56
56
|
domain,
|
|
57
57
|
});
|
|
58
|
+
if (cache !== undefined) {
|
|
59
|
+
console.info("DOING IT", cache);
|
|
60
|
+
params.set("cache", cache.toString());
|
|
61
|
+
}
|
|
58
62
|
if (path) {
|
|
59
63
|
params.set("path", path);
|
|
60
64
|
}
|
|
@@ -91,7 +95,7 @@ export async function makeMarkupRequest({ tz, path, page, domain, apiKey, dateFo
|
|
|
91
95
|
return response;
|
|
92
96
|
}
|
|
93
97
|
export function markupFetcher(platform, fetchImplementation = fetch, store = new Store()) {
|
|
94
|
-
return async ({ tz = undefined, path, domain, apiKey, schema, dateFormat, baseUrl = BASE_URL, environment = getEnvironment(), copy = {}, }) => {
|
|
98
|
+
return async ({ tz = undefined, path, cache, domain, apiKey, schema, dateFormat, baseUrl = BASE_URL, environment = getEnvironment(), copy = {}, }) => {
|
|
95
99
|
path = path || "/";
|
|
96
100
|
const cachedMarkup = (() => {
|
|
97
101
|
if (!store.hasData) {
|
|
@@ -101,7 +105,17 @@ export function markupFetcher(platform, fetchImplementation = fetch, store = new
|
|
|
101
105
|
})();
|
|
102
106
|
const markup = cachedMarkup
|
|
103
107
|
? cachedMarkup
|
|
104
|
-
: await fetchFreshMarkup({
|
|
108
|
+
: await fetchFreshMarkup({
|
|
109
|
+
tz,
|
|
110
|
+
path,
|
|
111
|
+
domain,
|
|
112
|
+
apiKey,
|
|
113
|
+
baseUrl,
|
|
114
|
+
environment,
|
|
115
|
+
copy,
|
|
116
|
+
dateFormat,
|
|
117
|
+
cache,
|
|
118
|
+
}, fetchImplementation, platform);
|
|
105
119
|
if (schema) {
|
|
106
120
|
const preparedSchema = typeof schema !== "string" ? JSON.stringify(schema) : schema;
|
|
107
121
|
const parsedSchema = parseJson(preparedSchema);
|
|
@@ -334,6 +334,25 @@ describe("markupFetcher", () => {
|
|
|
334
334
|
expect(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?domain=test.com&path=%2Ftest&stub=true", expect.anything());
|
|
335
335
|
expect(result).toEqual("results!");
|
|
336
336
|
});
|
|
337
|
+
it("sets cache control header on response", async () => {
|
|
338
|
+
const fetchMock = vi.fn().mockImplementation(() => {
|
|
339
|
+
return {
|
|
340
|
+
status: 200,
|
|
341
|
+
ok: true,
|
|
342
|
+
text: () => "results!",
|
|
343
|
+
};
|
|
344
|
+
});
|
|
345
|
+
const fetcher = markupFetcher("test", fetchMock);
|
|
346
|
+
const result = await fetcher({
|
|
347
|
+
path: "/test",
|
|
348
|
+
domain: "test.com",
|
|
349
|
+
apiKey: "123abc",
|
|
350
|
+
environment: "development",
|
|
351
|
+
cache: true,
|
|
352
|
+
});
|
|
353
|
+
expect(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?domain=test.com&cache=true&path=%2Ftest&stub=true", expect.anything());
|
|
354
|
+
expect(result).toEqual("results!");
|
|
355
|
+
});
|
|
337
356
|
it("uses different base URL", async () => {
|
|
338
357
|
const fetchMock = vi.fn().mockImplementation(() => {
|
|
339
358
|
return {
|
|
@@ -19,6 +19,7 @@ export interface IFetchData {
|
|
|
19
19
|
baseUrl?: string;
|
|
20
20
|
environment?: string;
|
|
21
21
|
dateFormat?: string;
|
|
22
|
+
cache?: boolean;
|
|
22
23
|
copy?: {
|
|
23
24
|
copy_confirmation_message?: string;
|
|
24
25
|
copy_submit_button?: string;
|
|
@@ -52,7 +53,7 @@ interface IBatchResponse {
|
|
|
52
53
|
export declare const BASE_URL = "https://go.jamcomments.com";
|
|
53
54
|
export declare function fetchAll({ tz, dateFormat, domain, apiKey, baseUrl, environment, copy, }: IBatchFetchData, platform: string, fetchImplementation?: any, batchMarkupFetcherImpl?: any, store?: Store): Promise<Map<string, string>>;
|
|
54
55
|
export declare function batchMarkupFetcher(platform: string, fetchImplementation?: typeof fetch): (args: IBatchFetchData) => Promise<IBatchResponse>;
|
|
55
|
-
export declare function fetchFreshMarkup({ tz, path, copy, domain, apiKey, dateFormat, baseUrl, environment, }: IFetchData, fetchImplementation: typeof fetch, platform: string): Promise<string>;
|
|
56
|
-
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>;
|
|
56
|
+
export declare function fetchFreshMarkup({ tz, path, copy, cache, domain, apiKey, dateFormat, baseUrl, environment, }: IFetchData, fetchImplementation: typeof fetch, platform: string): Promise<string>;
|
|
57
|
+
export declare function makeMarkupRequest<T extends Partial<IBatchFetchData & IFetchData>>({ tz, path, page, cache, domain, apiKey, dateFormat, copy, baseUrl, environment, }: T, baseServicePath: string, fetchImplementation: typeof fetch, platform: string): Promise<Response>;
|
|
57
58
|
export declare function markupFetcher(platform: string, fetchImplementation?: typeof fetch, store?: Store): (args: IFetchData) => Promise<string>;
|
|
58
59
|
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jam-comments/server-utilities",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.12.0",
|
|
4
4
|
"description": "Various JavaScript utilities for JamComments.",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"homepage": "https://jamcomments.com",
|
|
23
23
|
"license": "GPL-2.0",
|
|
24
24
|
"devDependencies": {
|
|
25
|
-
"@types/node": "^22.10.
|
|
25
|
+
"@types/node": "^22.10.1",
|
|
26
26
|
"prettier": "^3.4.1",
|
|
27
27
|
"typescript": "^5.7.2",
|
|
28
28
|
"vite": "^6.0.1",
|