@jam-comments/astro 2.6.2 → 2.8.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.
package/index.d.ts CHANGED
@@ -14,6 +14,7 @@ export interface JamCommentsProps {
14
14
  domain?: string;
15
15
  apiKey?: string;
16
16
  baseUrl?: string;
17
+ dateFormat?: string;
17
18
  environment?: string;
18
19
  tz?: string;
19
20
  copy?: CustomCopy;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jam-comments/astro",
3
- "version": "2.6.2",
3
+ "version": "2.8.0",
4
4
  "author": "Alex MacArthur <alex@macarthur.me> (https://macarthur.me)",
5
5
  "license": "GPL-2.0",
6
6
  "description": "Easily add performant, SEO-friendly comments to your Astro blog with JamComments.",
@@ -38,17 +38,17 @@
38
38
  "access": "public"
39
39
  },
40
40
  "dependencies": {
41
- "@astrojs/check": "^0.8.2",
42
- "@jam-comments/server-utilities": "^5.2.0",
43
- "astro": "^4.12.2",
41
+ "@astrojs/check": "^0.9.3",
42
+ "@jam-comments/server-utilities": "^5.10.0",
43
+ "astro": "^4.15.1",
44
44
  "node-fetch": "^3.3.2",
45
- "typescript": "^5.5.3"
45
+ "typescript": "^5.5.4"
46
46
  },
47
47
  "gitHead": "2b1cefb90c89a2f70099213881b09216f20e3dc2",
48
48
  "devDependencies": {
49
- "@types/node": "^20.14.11",
49
+ "@types/node": "^22.5.1",
50
50
  "prettier": "^3.3.3",
51
51
  "prettier-plugin-astro": "^0.14.1",
52
- "vitest": "^2.0.3"
52
+ "vitest": "^2.0.5"
53
53
  }
54
54
  }
@@ -1,70 +1,37 @@
1
1
  ---
2
- import pkg from "@jam-comments/server-utilities";
3
- import nodeFetch from "node-fetch";
4
2
  import type { JamCommentsProps } from "..";
5
- import { getCurrentPath, removeFalseyValues } from "./utils";
6
- const { logError, markupFetcher } = pkg;
3
+ import { fetchCommentData, getCurrentPath } from "./utils";
4
+ import { Store } from "@jam-comments/server-utilities";
7
5
 
8
6
  type Props = JamCommentsProps;
9
7
 
10
- const fetchMarkup = markupFetcher("astro", nodeFetch as any);
11
-
12
- async function fetchCommentData({
13
- copy,
14
- tz = undefined,
15
- path = undefined,
16
- schema = undefined,
17
- domain = import.meta.env.JAM_COMMENTS_DOMAIN as string,
18
- apiKey = import.meta.env.JAM_COMMENTS_API_KEY as string,
19
- baseUrl = import.meta.env.JAM_COMMENTS_BASE_URL as string,
20
- environment = import.meta.env.JAM_COMMENTS_ENVIRONMENT as string,
21
- }: JamCommentsProps) {
22
- try {
23
- return await fetchMarkup({
24
- path,
25
- schema,
26
- domain,
27
- apiKey,
28
- baseUrl,
29
- environment,
30
- tz,
31
- copy: removeFalseyValues({
32
- copy_confirmation_message: copy.confirmationMessage,
33
- copy_submit_button: copy.submitButton,
34
- copy_name_placeholder: copy.namePlaceholder,
35
- copy_email_placeholder: copy.emailPlaceholder,
36
- copy_comment_placeholder: copy.commentPlaceholder,
37
- copy_write_tab: copy.writeTab,
38
- copy_preview_tab: copy.previewTab,
39
- copy_auth_button: copy.authButton,
40
- copy_log_out_button: copy.logOutButton,
41
- }),
42
- });
43
- } catch (e) {
44
- logError(e as string);
45
- return null;
46
- }
47
- }
48
-
49
8
  const {
9
+ tz,
50
10
  path,
11
+ schema,
51
12
  domain,
52
13
  baseUrl,
14
+ dateFormat,
53
15
  environment,
54
- tz,
55
- schema,
56
16
  copy = {},
57
17
  } = Astro.props as Props;
58
18
 
59
- const markup = await fetchCommentData({
60
- path: path || getCurrentPath(Astro),
61
- schema,
62
- domain,
63
- baseUrl,
64
- environment,
65
- tz,
66
- copy,
67
- });
19
+ const store = new Store();
20
+ const pagePath = path || getCurrentPath(Astro);
21
+ const cachedMarkup = store.get(pagePath) || store.emptyMarkup;
22
+
23
+ const markup =
24
+ cachedMarkup ||
25
+ (await fetchCommentData({
26
+ tz,
27
+ copy,
28
+ path: pagePath,
29
+ schema,
30
+ domain,
31
+ baseUrl,
32
+ environment,
33
+ dateFormat,
34
+ }));
68
35
  ---
69
36
 
70
37
  <div set:html={markup} />
package/src/config.ts CHANGED
@@ -7,6 +7,8 @@ interface PluginArgs {
7
7
  environment?: string;
8
8
  }
9
9
 
10
+ globalThis.jamCommentsStore = new Map<string, string>();
11
+
10
12
  export function jamComments({
11
13
  domain,
12
14
  apiKey,
@@ -16,8 +18,8 @@ export function jamComments({
16
18
  return {
17
19
  name: "jamcomments",
18
20
  hooks: {
19
- "astro:build:start": () => {
20
- return fetchAll(
21
+ "astro:build:start": async () => {
22
+ globalThis.jamCommentsStore = await fetchAll(
21
23
  {
22
24
  tz: timezone,
23
25
  domain,
@@ -30,7 +32,7 @@ export function jamComments({
30
32
  },
31
33
 
32
34
  "astro:build:done": () => {
33
- // deleteTempDirectory();
35
+ globalThis.jamCommentsStore.clear();
34
36
  },
35
37
  },
36
38
  };
package/src/env.d.ts CHANGED
@@ -1 +1,2 @@
1
+ /// <reference path="../.astro/types.d.ts" />
1
2
  /// <reference types="astro/client" />
package/src/utils.test.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { describe, expect, it } from "vitest";
2
- import { getCurrentPath, removeFalseyValues } from "./utils";
1
+ import { describe, expect, it, vi } from "vitest";
2
+ import { fetchCommentData, getCurrentPath, removeFalseyValues } from "./utils";
3
3
  import { AstroGlobal } from "astro";
4
4
 
5
5
  describe("getCurrentPath()", () => {
@@ -46,3 +46,62 @@ describe("removeFalseyValues()", () => {
46
46
  });
47
47
  });
48
48
  });
49
+
50
+ describe("fetchCommentData()", () => {
51
+ it("passes the correct arguments to fetchMarkup", async () => {
52
+ const mockResponse = vi.fn();
53
+ const fetchMock = vi.fn().mockImplementation(() => mockResponse);
54
+
55
+ const result = await fetchCommentData(
56
+ {
57
+ copy: {
58
+ confirmationMessage: "my confirmation message",
59
+ },
60
+ apiKey: "123",
61
+ dateFormat: "YYYY-MM-DD",
62
+ path: "/test",
63
+ domain: "example.com",
64
+ environment: "production",
65
+ baseUrl: "https://example.com",
66
+ },
67
+ fetchMock,
68
+ );
69
+
70
+ expect(fetchMock).toHaveBeenCalledWith({
71
+ copy: {
72
+ copy_confirmation_message: "my confirmation message",
73
+ },
74
+ apiKey: "123",
75
+ dateFormat: "YYYY-MM-DD",
76
+ path: "/test",
77
+ domain: "example.com",
78
+ environment: "production",
79
+ baseUrl: "https://example.com",
80
+ });
81
+
82
+ expect(result).toBe(mockResponse);
83
+ });
84
+
85
+ it("returns null if fetchMarkup throws an error", async () => {
86
+ const fetchMock = vi.fn().mockImplementation(() => {
87
+ throw new Error("test error");
88
+ });
89
+
90
+ const result = await fetchCommentData(
91
+ {
92
+ copy: {
93
+ confirmationMessage: "my confirmation message",
94
+ },
95
+ apiKey: "123",
96
+ path: "/test",
97
+ domain: "example.com",
98
+ dateFormat: "YYYY-MM-DD",
99
+ environment: "production",
100
+ baseUrl: "https://example.com",
101
+ },
102
+ fetchMock,
103
+ );
104
+
105
+ expect(result).toBeNull();
106
+ });
107
+ });
package/src/utils.ts CHANGED
@@ -1,4 +1,7 @@
1
+ // import nodeFetch from "node-fetch";
1
2
  import { AstroGlobal } from "astro";
3
+ import { JamCommentsProps } from "..";
4
+ import { logError, markupFetcher } from "@jam-comments/server-utilities";
2
5
 
3
6
  export function getCurrentPath(astro: AstroGlobal) {
4
7
  return new URL(astro.request.url).pathname;
@@ -9,3 +12,47 @@ export function removeFalseyValues<T>(obj: T): Partial<T> {
9
12
 
10
13
  return Object.fromEntries(filteredItems) as Partial<T>;
11
14
  }
15
+
16
+ const fetchMarkup = markupFetcher("astro");
17
+
18
+ export async function fetchCommentData(
19
+ {
20
+ copy,
21
+ tz = undefined,
22
+ path = undefined,
23
+ schema = undefined,
24
+ dateFormat = undefined,
25
+ domain = import.meta.env.JAM_COMMENTS_DOMAIN as string,
26
+ apiKey = import.meta.env.JAM_COMMENTS_API_KEY as string,
27
+ baseUrl = import.meta.env.JAM_COMMENTS_BASE_URL as string,
28
+ environment = import.meta.env.JAM_COMMENTS_ENVIRONMENT as string,
29
+ }: JamCommentsProps,
30
+ fetchFunc = fetchMarkup,
31
+ ) {
32
+ try {
33
+ return fetchFunc({
34
+ path,
35
+ schema,
36
+ domain,
37
+ apiKey,
38
+ baseUrl,
39
+ environment,
40
+ dateFormat,
41
+ tz,
42
+ copy: removeFalseyValues({
43
+ copy_confirmation_message: copy.confirmationMessage,
44
+ copy_submit_button: copy.submitButton,
45
+ copy_name_placeholder: copy.namePlaceholder,
46
+ copy_email_placeholder: copy.emailPlaceholder,
47
+ copy_comment_placeholder: copy.commentPlaceholder,
48
+ copy_write_tab: copy.writeTab,
49
+ copy_preview_tab: copy.previewTab,
50
+ copy_auth_button: copy.authButton,
51
+ copy_log_out_button: copy.logOutButton,
52
+ }),
53
+ });
54
+ } catch (e) {
55
+ logError(e as string);
56
+ return null;
57
+ }
58
+ }