@jam-comments/server-utilities 4.0.1 → 4.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.
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.markupFetcher = void 0;
4
4
  const utils_1 = require("./utils");
5
5
  const markupFetcher = (platform, fetchImplementation = fetch) => {
6
- return async ({ tz = undefined, path, domain, apiKey, baseUrl = "https://go.jamcomments.com", environment = (0, utils_1.getEnvironment)(), }) => {
6
+ return async ({ tz = undefined, path, domain, apiKey, schema, baseUrl = "https://go.jamcomments.com", environment = (0, utils_1.getEnvironment)(), }) => {
7
7
  const trimmedTimezone = tz?.trim();
8
8
  if (trimmedTimezone && !(0, utils_1.isValidTimezone)(trimmedTimezone)) {
9
9
  throw new Error(`The timezone passed to JamComments is invalid: ${trimmedTimezone}`);
@@ -12,6 +12,10 @@ const markupFetcher = (platform, fetchImplementation = fetch) => {
12
12
  path: path || "/",
13
13
  domain,
14
14
  });
15
+ if (schema) {
16
+ const preparedSchema = typeof schema !== "string" ? JSON.stringify(schema) : schema;
17
+ params.set("schema", preparedSchema);
18
+ }
15
19
  if (trimmedTimezone) {
16
20
  params.set("tz", trimmedTimezone);
17
21
  }
@@ -185,3 +185,43 @@ const markupFetcher_1 = require("./markupFetcher");
185
185
  });
186
186
  });
187
187
  });
188
+ (0, vitest_1.describe)("passing schema", function () {
189
+ (0, vitest_1.it)("first stringifies schema if given an object", async () => {
190
+ const fetchMock = vitest_1.vi.fn().mockImplementation(() => {
191
+ return {
192
+ status: 200,
193
+ ok: true,
194
+ text: () => "results!",
195
+ };
196
+ });
197
+ const fetcher = (0, markupFetcher_1.markupFetcher)("test", fetchMock);
198
+ const result = await fetcher({
199
+ path: "/test",
200
+ domain: "test.com",
201
+ apiKey: "123abc",
202
+ schema: { foo: "bar" },
203
+ environment: "production",
204
+ });
205
+ (0, vitest_1.expect)(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?path=%2Ftest&domain=test.com&schema=%7B%22foo%22%3A%22bar%22%7D", vitest_1.expect.anything());
206
+ (0, vitest_1.expect)(result).toEqual("results!");
207
+ });
208
+ (0, vitest_1.it)("does not stringify schema if given a string", async () => {
209
+ const fetchMock = vitest_1.vi.fn().mockImplementation(() => {
210
+ return {
211
+ status: 200,
212
+ ok: true,
213
+ text: () => "results!",
214
+ };
215
+ });
216
+ const fetcher = (0, markupFetcher_1.markupFetcher)("test", fetchMock);
217
+ const result = await fetcher({
218
+ path: "/test",
219
+ domain: "test.com",
220
+ apiKey: "123abc",
221
+ schema: '{"foo":"bar"}',
222
+ environment: "production",
223
+ });
224
+ (0, vitest_1.expect)(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?path=%2Ftest&domain=test.com&schema=%7B%22foo%22%3A%22bar%22%7D", vitest_1.expect.anything());
225
+ (0, vitest_1.expect)(result).toEqual("results!");
226
+ });
227
+ });
package/dist/cjs/utils.js CHANGED
@@ -15,7 +15,9 @@ function getEnvironment() {
15
15
  if (typeof process === "undefined") {
16
16
  return "production";
17
17
  }
18
- return (process.env?.JAM_COMMENTS_ENV || process.env?.NODE_ENV || "development");
18
+ return (process.env?.JAM_COMMENTS_ENVIRONMENT ||
19
+ process.env?.NODE_ENV ||
20
+ "development");
19
21
  }
20
22
  exports.getEnvironment = getEnvironment;
21
23
  function reAppendMarkup(element, markup) {
@@ -24,22 +24,22 @@ const env = process.env;
24
24
  (0, vitest_1.expect)((0, utils_1.getEnvironment)()).toEqual("production");
25
25
  });
26
26
  });
27
- (0, vitest_1.describe)("JAM_COMMENTS_ENV is set", () => {
27
+ (0, vitest_1.describe)("JAM_COMMENTS_ENVIRONMENT is set", () => {
28
28
  (0, vitest_1.it)("returns environment", () => {
29
- process.env.JAM_COMMENTS_ENV = "staging";
29
+ process.env.JAM_COMMENTS_ENVIRONMENT = "staging";
30
30
  (0, vitest_1.expect)((0, utils_1.getEnvironment)()).toEqual("staging");
31
31
  });
32
32
  (0, vitest_1.it)("falls back to NODE_ENV", () => {
33
- process.env.JAM_COMMENTS_ENV = undefined;
33
+ process.env.JAM_COMMENTS_ENVIRONMENT = undefined;
34
34
  (0, vitest_1.expect)((0, utils_1.getEnvironment)()).toEqual("test");
35
35
  });
36
36
  (0, vitest_1.it)("falls back to development", () => {
37
- process.env.JAM_COMMENTS_ENV = undefined;
37
+ process.env.JAM_COMMENTS_ENVIRONMENT = undefined;
38
38
  process.env.NODE_ENV = undefined;
39
39
  (0, vitest_1.expect)((0, utils_1.getEnvironment)()).toEqual("development");
40
40
  });
41
41
  (0, vitest_1.it)("returns production environment", () => {
42
- process.env.JAM_COMMENTS_ENV = "production";
42
+ process.env.JAM_COMMENTS_ENVIRONMENT = "production";
43
43
  (0, vitest_1.expect)((0, utils_1.getEnvironment)()).toEqual("production");
44
44
  });
45
45
  });
@@ -1,6 +1,6 @@
1
1
  import { getEnvironment, isValidTimezone } from "./utils";
2
2
  export const markupFetcher = (platform, fetchImplementation = fetch) => {
3
- return async ({ tz = undefined, path, domain, apiKey, baseUrl = "https://go.jamcomments.com", environment = getEnvironment(), }) => {
3
+ return async ({ tz = undefined, path, domain, apiKey, schema, baseUrl = "https://go.jamcomments.com", environment = getEnvironment(), }) => {
4
4
  const trimmedTimezone = tz?.trim();
5
5
  if (trimmedTimezone && !isValidTimezone(trimmedTimezone)) {
6
6
  throw new Error(`The timezone passed to JamComments is invalid: ${trimmedTimezone}`);
@@ -9,6 +9,10 @@ export const markupFetcher = (platform, fetchImplementation = fetch) => {
9
9
  path: path || "/",
10
10
  domain,
11
11
  });
12
+ if (schema) {
13
+ const preparedSchema = typeof schema !== "string" ? JSON.stringify(schema) : schema;
14
+ params.set("schema", preparedSchema);
15
+ }
12
16
  if (trimmedTimezone) {
13
17
  params.set("tz", trimmedTimezone);
14
18
  }
@@ -183,3 +183,43 @@ describe("markupFetcher", () => {
183
183
  });
184
184
  });
185
185
  });
186
+ describe("passing schema", function () {
187
+ it("first stringifies schema if given an object", async () => {
188
+ const fetchMock = vi.fn().mockImplementation(() => {
189
+ return {
190
+ status: 200,
191
+ ok: true,
192
+ text: () => "results!",
193
+ };
194
+ });
195
+ const fetcher = markupFetcher("test", fetchMock);
196
+ const result = await fetcher({
197
+ path: "/test",
198
+ domain: "test.com",
199
+ apiKey: "123abc",
200
+ schema: { foo: "bar" },
201
+ environment: "production",
202
+ });
203
+ expect(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?path=%2Ftest&domain=test.com&schema=%7B%22foo%22%3A%22bar%22%7D", expect.anything());
204
+ expect(result).toEqual("results!");
205
+ });
206
+ it("does not stringify schema if given a string", async () => {
207
+ const fetchMock = vi.fn().mockImplementation(() => {
208
+ return {
209
+ status: 200,
210
+ ok: true,
211
+ text: () => "results!",
212
+ };
213
+ });
214
+ const fetcher = markupFetcher("test", fetchMock);
215
+ const result = await fetcher({
216
+ path: "/test",
217
+ domain: "test.com",
218
+ apiKey: "123abc",
219
+ schema: '{"foo":"bar"}',
220
+ environment: "production",
221
+ });
222
+ expect(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?path=%2Ftest&domain=test.com&schema=%7B%22foo%22%3A%22bar%22%7D", expect.anything());
223
+ expect(result).toEqual("results!");
224
+ });
225
+ });
package/dist/esm/utils.js CHANGED
@@ -11,7 +11,9 @@ export function getEnvironment() {
11
11
  if (typeof process === "undefined") {
12
12
  return "production";
13
13
  }
14
- return (process.env?.JAM_COMMENTS_ENV || process.env?.NODE_ENV || "development");
14
+ return (process.env?.JAM_COMMENTS_ENVIRONMENT ||
15
+ process.env?.NODE_ENV ||
16
+ "development");
15
17
  }
16
18
  export function reAppendMarkup(element, markup) {
17
19
  const range = document.createRange();
@@ -22,22 +22,22 @@ describe("getEnvironment()", () => {
22
22
  expect(getEnvironment()).toEqual("production");
23
23
  });
24
24
  });
25
- describe("JAM_COMMENTS_ENV is set", () => {
25
+ describe("JAM_COMMENTS_ENVIRONMENT is set", () => {
26
26
  it("returns environment", () => {
27
- process.env.JAM_COMMENTS_ENV = "staging";
27
+ process.env.JAM_COMMENTS_ENVIRONMENT = "staging";
28
28
  expect(getEnvironment()).toEqual("staging");
29
29
  });
30
30
  it("falls back to NODE_ENV", () => {
31
- process.env.JAM_COMMENTS_ENV = undefined;
31
+ process.env.JAM_COMMENTS_ENVIRONMENT = undefined;
32
32
  expect(getEnvironment()).toEqual("test");
33
33
  });
34
34
  it("falls back to development", () => {
35
- process.env.JAM_COMMENTS_ENV = undefined;
35
+ process.env.JAM_COMMENTS_ENVIRONMENT = undefined;
36
36
  process.env.NODE_ENV = undefined;
37
37
  expect(getEnvironment()).toEqual("development");
38
38
  });
39
39
  it("returns production environment", () => {
40
- process.env.JAM_COMMENTS_ENV = "production";
40
+ process.env.JAM_COMMENTS_ENVIRONMENT = "production";
41
41
  expect(getEnvironment()).toEqual("production");
42
42
  });
43
43
  });
@@ -2,8 +2,9 @@ export interface IFetchData {
2
2
  path: string;
3
3
  domain: string;
4
4
  apiKey: string;
5
+ schema?: string | object;
5
6
  tz?: string;
6
7
  baseUrl?: string;
7
8
  environment?: string;
8
9
  }
9
- export declare const markupFetcher: (platform: string, fetchImplementation?: typeof fetch) => (args: IFetchData) => Promise<string>;
10
+ export declare const markupFetcher: (platform: string, fetchImplementation?: typeof fetch) => ((args: IFetchData) => Promise<string>);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jam-comments/server-utilities",
3
- "version": "4.0.1",
3
+ "version": "4.1.0",
4
4
  "description": "Various JavaScript utilities for JamComments.",
5
5
  "main": "dist/cjs/index.js",
6
6
  "types": "dist/types/index.d.ts",
@@ -21,11 +21,11 @@
21
21
  "homepage": "https://jamcomments.com",
22
22
  "license": "GPL-2.0",
23
23
  "devDependencies": {
24
- "@types/node": "^20.9.0",
25
- "prettier": "^3.0.3",
26
- "typescript": "^5.2.2",
27
- "vite": "^4.5.0",
28
- "vitest": "^0.34.6"
24
+ "@types/node": "^20.12.11",
25
+ "prettier": "^3.2.5",
26
+ "typescript": "^5.4.5",
27
+ "vite": "^5.2.11",
28
+ "vitest": "^1.6.0"
29
29
  },
30
30
  "publishConfig": {
31
31
  "access": "public"