@jam-comments/server-utilities 3.0.1 → 3.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.
@@ -1,12 +1,19 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.markupFetcher = void 0;
4
+ const utils_1 = require("./utils");
4
5
  const markupFetcher = (platform, fetchImplementation = fetch) => {
5
- return async ({ path, domain, apiKey, baseUrl = "https://go.jamcomments.com", environment = "production", }) => {
6
+ return async ({ tz = undefined, path, domain, apiKey, baseUrl = "https://go.jamcomments.com", environment = "production", }) => {
7
+ if (tz && !(0, utils_1.isValidTimezone)(tz)) {
8
+ throw new Error(`The timezone passed to JamComments is invalid: ${tz}`);
9
+ }
6
10
  const params = new URLSearchParams({
7
11
  path: path || "/",
8
12
  domain,
9
13
  });
14
+ if (tz) {
15
+ params.set("tz", tz);
16
+ }
10
17
  if (environment !== "production") {
11
18
  params.set("stub", "true");
12
19
  }
@@ -109,4 +109,34 @@ const markupFetcher_1 = require("./markupFetcher");
109
109
  apiKey: "123abc",
110
110
  })).rejects.toThrowError(/request failed! Status code: 500/);
111
111
  });
112
+ (0, vitest_1.describe)("timezone validation", () => {
113
+ (0, vitest_1.it)("throws error when invalid timezone is provided", async () => {
114
+ const fetchMock = vitest_1.vi.fn();
115
+ const fetcher = (0, markupFetcher_1.markupFetcher)("test", fetchMock);
116
+ (0, vitest_1.expect)(fetchMock).not.toHaveBeenCalled();
117
+ (0, vitest_1.expect)(fetcher({
118
+ path: "/test",
119
+ domain: "test.com",
120
+ apiKey: "123abc",
121
+ tz: "in/valid",
122
+ })).rejects.toThrowError("The timezone passed to JamComments is invalid: in/valid");
123
+ });
124
+ (0, vitest_1.it)("does not throw error when valid timezone is provided", async () => {
125
+ const fetchMock = vitest_1.vi.fn().mockImplementation(() => {
126
+ return {
127
+ status: 200,
128
+ ok: true,
129
+ text: () => "results!",
130
+ };
131
+ });
132
+ const fetcher = (0, markupFetcher_1.markupFetcher)("test", fetchMock);
133
+ const result = await fetcher({
134
+ path: null,
135
+ domain: "test.com",
136
+ tz: "America/New_York",
137
+ });
138
+ (0, vitest_1.expect)(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v2/markup?path=%2F&domain=test.com&tz=America%2FNew_York", vitest_1.expect.anything());
139
+ (0, vitest_1.expect)(result).toEqual("results!");
140
+ });
141
+ });
112
142
  });
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isValidTimezone = void 0;
4
+ function isValidTimezone(tz) {
5
+ try {
6
+ Intl.DateTimeFormat(undefined, { timeZone: tz });
7
+ return true;
8
+ }
9
+ catch (ex) {
10
+ return false;
11
+ }
12
+ }
13
+ exports.isValidTimezone = isValidTimezone;
@@ -1,9 +1,16 @@
1
+ import { isValidTimezone } from "./utils";
1
2
  export const markupFetcher = (platform, fetchImplementation = fetch) => {
2
- return async ({ path, domain, apiKey, baseUrl = "https://go.jamcomments.com", environment = "production", }) => {
3
+ return async ({ tz = undefined, path, domain, apiKey, baseUrl = "https://go.jamcomments.com", environment = "production", }) => {
4
+ if (tz && !isValidTimezone(tz)) {
5
+ throw new Error(`The timezone passed to JamComments is invalid: ${tz}`);
6
+ }
3
7
  const params = new URLSearchParams({
4
8
  path: path || "/",
5
9
  domain,
6
10
  });
11
+ if (tz) {
12
+ params.set("tz", tz);
13
+ }
7
14
  if (environment !== "production") {
8
15
  params.set("stub", "true");
9
16
  }
@@ -107,4 +107,34 @@ describe("markupFetcher", () => {
107
107
  apiKey: "123abc",
108
108
  })).rejects.toThrowError(/request failed! Status code: 500/);
109
109
  });
110
+ describe("timezone validation", () => {
111
+ it("throws error when invalid timezone is provided", async () => {
112
+ const fetchMock = vi.fn();
113
+ const fetcher = markupFetcher("test", fetchMock);
114
+ expect(fetchMock).not.toHaveBeenCalled();
115
+ expect(fetcher({
116
+ path: "/test",
117
+ domain: "test.com",
118
+ apiKey: "123abc",
119
+ tz: "in/valid",
120
+ })).rejects.toThrowError("The timezone passed to JamComments is invalid: in/valid");
121
+ });
122
+ it("does not throw error when valid timezone is provided", async () => {
123
+ const fetchMock = vi.fn().mockImplementation(() => {
124
+ return {
125
+ status: 200,
126
+ ok: true,
127
+ text: () => "results!",
128
+ };
129
+ });
130
+ const fetcher = markupFetcher("test", fetchMock);
131
+ const result = await fetcher({
132
+ path: null,
133
+ domain: "test.com",
134
+ tz: "America/New_York",
135
+ });
136
+ expect(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v2/markup?path=%2F&domain=test.com&tz=America%2FNew_York", expect.anything());
137
+ expect(result).toEqual("results!");
138
+ });
139
+ });
110
140
  });
@@ -0,0 +1,9 @@
1
+ export function isValidTimezone(tz) {
2
+ try {
3
+ Intl.DateTimeFormat(undefined, { timeZone: tz });
4
+ return true;
5
+ }
6
+ catch (ex) {
7
+ return false;
8
+ }
9
+ }
@@ -0,0 +1 @@
1
+ export declare function isValidTimezone(tz: string): boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jam-comments/server-utilities",
3
- "version": "3.0.1",
3
+ "version": "3.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": "^18.15.8",
25
- "prettier": "^2.8.7",
26
- "typescript": "^5.0.2",
27
- "vite": "^4.2.1",
28
- "vitest": "^0.29.7"
24
+ "@types/node": "^20.2.5",
25
+ "prettier": "^2.8.8",
26
+ "typescript": "^5.0.4",
27
+ "vite": "^4.3.9",
28
+ "vitest": "^0.31.1"
29
29
  },
30
30
  "publishConfig": {
31
31
  "access": "public"