@jam-comments/server-utilities 1.0.5 → 2.0.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.
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.markupFetcher = exports.logError = exports.log = void 0;
4
+ const markupFetcher_1 = require("./markupFetcher");
5
+ Object.defineProperty(exports, "markupFetcher", { enumerable: true, get: function () { return markupFetcher_1.markupFetcher; } });
6
+ var log_1 = require("./log");
7
+ Object.defineProperty(exports, "log", { enumerable: true, get: function () { return log_1.log; } });
8
+ Object.defineProperty(exports, "logError", { enumerable: true, get: function () { return log_1.logError; } });
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.logError = exports.log = void 0;
7
+ const chalk_1 = __importDefault(require("chalk"));
8
+ const log = (message) => {
9
+ console.log(`${chalk_1.default.magenta("JamComments:")} ${message}`);
10
+ };
11
+ exports.log = log;
12
+ const logError = (message) => {
13
+ console.error(`JamComments: ${message}`);
14
+ };
15
+ exports.logError = logError;
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.markupFetcher = void 0;
4
+ const markupFetcher = (platform, fetchImplementation = fetch) => {
5
+ return async ({ path, domain, apiKey, baseUrl = 'https://go.jamcomments.com', environment = 'production' }) => {
6
+ const params = new URLSearchParams({
7
+ path: path || "/",
8
+ domain
9
+ });
10
+ if (environment !== 'production') {
11
+ params.set('stub', 'true');
12
+ }
13
+ const requestUrl = `${baseUrl}/api/markup?${params}`;
14
+ const response = await fetchImplementation(requestUrl, {
15
+ method: 'GET',
16
+ headers: {
17
+ Authorization: `Bearer ${apiKey}`,
18
+ Accept: 'application/json',
19
+ 'X-Platform': platform
20
+ }
21
+ });
22
+ if (response.status === 401) {
23
+ throw new Error(`Unauthorized! Are your domain and JamComments API key set correctly?`);
24
+ }
25
+ if (!response.ok) {
26
+ throw new Error(`JamComments request failed! Status code: ${response.status}, message: ${response.statusText}, request URL: ${requestUrl}`);
27
+ }
28
+ return await response.text();
29
+ };
30
+ };
31
+ exports.markupFetcher = markupFetcher;
@@ -0,0 +1,112 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const vitest_1 = require("vitest");
4
+ const markupFetcher_1 = require("./markupFetcher");
5
+ (0, vitest_1.describe)("markupFetcher", () => {
6
+ (0, vitest_1.it)("constructs fetch request correctly", async () => {
7
+ const fetchMock = vitest_1.vi.fn().mockImplementation(() => {
8
+ return {
9
+ status: 200,
10
+ ok: true,
11
+ text: () => "results!"
12
+ };
13
+ });
14
+ const fetcher = (0, markupFetcher_1.markupFetcher)("test", fetchMock);
15
+ const result = await fetcher({
16
+ path: "/test",
17
+ domain: "test.com",
18
+ apiKey: "123abc"
19
+ });
20
+ (0, vitest_1.expect)(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/markup?path=%2Ftest&domain=test.com", vitest_1.expect.objectContaining({
21
+ headers: vitest_1.expect.objectContaining({
22
+ Accept: "application/json",
23
+ Authorization: "Bearer 123abc",
24
+ "X-Platform": "test"
25
+ }),
26
+ method: "GET"
27
+ }));
28
+ (0, vitest_1.expect)(result).toEqual("results!");
29
+ });
30
+ (0, vitest_1.it)("stubs comments", async () => {
31
+ const fetchMock = vitest_1.vi.fn().mockImplementation(() => {
32
+ return {
33
+ status: 200,
34
+ ok: true,
35
+ text: () => "results!"
36
+ };
37
+ });
38
+ const fetcher = (0, markupFetcher_1.markupFetcher)("test", fetchMock);
39
+ const result = await fetcher({
40
+ path: "/test",
41
+ domain: "test.com",
42
+ apiKey: "123abc",
43
+ environment: "development"
44
+ });
45
+ (0, vitest_1.expect)(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/markup?path=%2Ftest&domain=test.com&stub=true", vitest_1.expect.anything());
46
+ (0, vitest_1.expect)(result).toEqual("results!");
47
+ });
48
+ (0, vitest_1.it)("uses different base URL", async () => {
49
+ const fetchMock = vitest_1.vi.fn().mockImplementation(() => {
50
+ return {
51
+ status: 200,
52
+ ok: true,
53
+ text: () => "results!"
54
+ };
55
+ });
56
+ const fetcher = (0, markupFetcher_1.markupFetcher)("test", fetchMock);
57
+ const result = await fetcher({
58
+ path: "/test",
59
+ domain: "test.com",
60
+ apiKey: "123abc",
61
+ baseUrl: "http://ur-mom.com"
62
+ });
63
+ (0, vitest_1.expect)(fetchMock).toHaveBeenCalledWith("http://ur-mom.com/api/markup?path=%2Ftest&domain=test.com", vitest_1.expect.anything());
64
+ (0, vitest_1.expect)(result).toEqual("results!");
65
+ });
66
+ (0, vitest_1.it)("falls back to root path", async () => {
67
+ const fetchMock = vitest_1.vi.fn().mockImplementation(() => {
68
+ return {
69
+ status: 200,
70
+ ok: true,
71
+ text: () => "results!"
72
+ };
73
+ });
74
+ const fetcher = (0, markupFetcher_1.markupFetcher)("test", fetchMock);
75
+ const result = await fetcher({
76
+ path: null,
77
+ domain: "test.com"
78
+ });
79
+ (0, vitest_1.expect)(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/markup?path=%2F&domain=test.com", vitest_1.expect.anything());
80
+ (0, vitest_1.expect)(result).toEqual("results!");
81
+ });
82
+ (0, vitest_1.it)("credentials are invalid", async () => {
83
+ const fetchMock = vitest_1.vi.fn().mockImplementation(() => {
84
+ return {
85
+ status: 401,
86
+ ok: false,
87
+ text: () => "bad results!"
88
+ };
89
+ });
90
+ const fetcher = (0, markupFetcher_1.markupFetcher)("test", fetchMock);
91
+ (0, vitest_1.expect)(fetcher({
92
+ path: "/test",
93
+ domain: "test.com",
94
+ apiKey: "123abc"
95
+ })).rejects.toThrowError(/Unauthorized!/);
96
+ });
97
+ (0, vitest_1.it)("response isn't ok", async () => {
98
+ const fetchMock = vitest_1.vi.fn().mockImplementation(() => {
99
+ return {
100
+ status: 500,
101
+ ok: false,
102
+ text: () => "bad results!"
103
+ };
104
+ });
105
+ const fetcher = (0, markupFetcher_1.markupFetcher)("test", fetchMock);
106
+ (0, vitest_1.expect)(fetcher({
107
+ path: "/test",
108
+ domain: "test.com",
109
+ apiKey: "123abc"
110
+ })).rejects.toThrowError(/request failed! Status code: 500/);
111
+ });
112
+ });
File without changes
@@ -0,0 +1,7 @@
1
+ import chalk from "chalk";
2
+ export const log = (message) => {
3
+ console.log(`${chalk.magenta("JamComments:")} ${message}`);
4
+ };
5
+ export const logError = (message) => {
6
+ console.error(`JamComments: ${message}`);
7
+ };
@@ -0,0 +1,27 @@
1
+ export const markupFetcher = (platform, fetchImplementation = fetch) => {
2
+ return async ({ path, domain, apiKey, baseUrl = 'https://go.jamcomments.com', environment = 'production' }) => {
3
+ const params = new URLSearchParams({
4
+ path: path || "/",
5
+ domain
6
+ });
7
+ if (environment !== 'production') {
8
+ params.set('stub', 'true');
9
+ }
10
+ const requestUrl = `${baseUrl}/api/markup?${params}`;
11
+ const response = await fetchImplementation(requestUrl, {
12
+ method: 'GET',
13
+ headers: {
14
+ Authorization: `Bearer ${apiKey}`,
15
+ Accept: 'application/json',
16
+ 'X-Platform': platform
17
+ }
18
+ });
19
+ if (response.status === 401) {
20
+ throw new Error(`Unauthorized! Are your domain and JamComments API key set correctly?`);
21
+ }
22
+ if (!response.ok) {
23
+ throw new Error(`JamComments request failed! Status code: ${response.status}, message: ${response.statusText}, request URL: ${requestUrl}`);
24
+ }
25
+ return await response.text();
26
+ };
27
+ };
@@ -0,0 +1,110 @@
1
+ import { describe, expect, it, vi } from 'vitest';
2
+ import { markupFetcher } from "./markupFetcher";
3
+ describe("markupFetcher", () => {
4
+ it("constructs fetch request correctly", async () => {
5
+ const fetchMock = vi.fn().mockImplementation(() => {
6
+ return {
7
+ status: 200,
8
+ ok: true,
9
+ text: () => "results!"
10
+ };
11
+ });
12
+ const fetcher = markupFetcher("test", fetchMock);
13
+ const result = await fetcher({
14
+ path: "/test",
15
+ domain: "test.com",
16
+ apiKey: "123abc"
17
+ });
18
+ expect(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/markup?path=%2Ftest&domain=test.com", expect.objectContaining({
19
+ headers: expect.objectContaining({
20
+ Accept: "application/json",
21
+ Authorization: "Bearer 123abc",
22
+ "X-Platform": "test"
23
+ }),
24
+ method: "GET"
25
+ }));
26
+ expect(result).toEqual("results!");
27
+ });
28
+ it("stubs comments", async () => {
29
+ const fetchMock = vi.fn().mockImplementation(() => {
30
+ return {
31
+ status: 200,
32
+ ok: true,
33
+ text: () => "results!"
34
+ };
35
+ });
36
+ const fetcher = markupFetcher("test", fetchMock);
37
+ const result = await fetcher({
38
+ path: "/test",
39
+ domain: "test.com",
40
+ apiKey: "123abc",
41
+ environment: "development"
42
+ });
43
+ expect(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/markup?path=%2Ftest&domain=test.com&stub=true", expect.anything());
44
+ expect(result).toEqual("results!");
45
+ });
46
+ it("uses different base URL", async () => {
47
+ const fetchMock = vi.fn().mockImplementation(() => {
48
+ return {
49
+ status: 200,
50
+ ok: true,
51
+ text: () => "results!"
52
+ };
53
+ });
54
+ const fetcher = markupFetcher("test", fetchMock);
55
+ const result = await fetcher({
56
+ path: "/test",
57
+ domain: "test.com",
58
+ apiKey: "123abc",
59
+ baseUrl: "http://ur-mom.com"
60
+ });
61
+ expect(fetchMock).toHaveBeenCalledWith("http://ur-mom.com/api/markup?path=%2Ftest&domain=test.com", expect.anything());
62
+ expect(result).toEqual("results!");
63
+ });
64
+ it("falls back to root path", async () => {
65
+ const fetchMock = vi.fn().mockImplementation(() => {
66
+ return {
67
+ status: 200,
68
+ ok: true,
69
+ text: () => "results!"
70
+ };
71
+ });
72
+ const fetcher = markupFetcher("test", fetchMock);
73
+ const result = await fetcher({
74
+ path: null,
75
+ domain: "test.com"
76
+ });
77
+ expect(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/markup?path=%2F&domain=test.com", expect.anything());
78
+ expect(result).toEqual("results!");
79
+ });
80
+ it("credentials are invalid", async () => {
81
+ const fetchMock = vi.fn().mockImplementation(() => {
82
+ return {
83
+ status: 401,
84
+ ok: false,
85
+ text: () => "bad results!"
86
+ };
87
+ });
88
+ const fetcher = markupFetcher("test", fetchMock);
89
+ expect(fetcher({
90
+ path: "/test",
91
+ domain: "test.com",
92
+ apiKey: "123abc"
93
+ })).rejects.toThrowError(/Unauthorized!/);
94
+ });
95
+ it("response isn't ok", async () => {
96
+ const fetchMock = vi.fn().mockImplementation(() => {
97
+ return {
98
+ status: 500,
99
+ ok: false,
100
+ text: () => "bad results!"
101
+ };
102
+ });
103
+ const fetcher = markupFetcher("test", fetchMock);
104
+ expect(fetcher({
105
+ path: "/test",
106
+ domain: "test.com",
107
+ apiKey: "123abc"
108
+ })).rejects.toThrowError(/request failed! Status code: 500/);
109
+ });
110
+ });
@@ -0,0 +1,3 @@
1
+ import { markupFetcher } from "./markupFetcher";
2
+ export { log, logError } from "./log";
3
+ export { markupFetcher };
File without changes
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,22 +1,23 @@
1
1
  {
2
2
  "name": "@jam-comments/server-utilities",
3
- "version": "1.0.5",
3
+ "version": "2.0.0",
4
4
  "description": "Various JavaScript utilities for JamComments.",
5
- "main": "dist/index.umd.js",
6
- "module": "dist/index.es.js",
7
- "types": "dist/index.d.ts",
5
+ "main": "dist/cjs/index.js",
6
+ "module": "dist/esm/index.js",
7
+ "types": "dist/types/index.d.ts",
8
8
  "files": [
9
9
  "dist/"
10
10
  ],
11
11
  "exports": {
12
- "import": "./dist/index.es.js",
13
- "require": "./dist/index.umd.js"
12
+ "import": "./dist/esm/index.js",
13
+ "require": "./dist/cjs/index.js"
14
14
  },
15
15
  "scripts": {
16
16
  "watch": "npm run build -- --watch",
17
- "build": "vite build && tsc",
18
- "test": "echo 'No tests here...'",
19
- "prettify": "prettier ./**/*.{js,md,ts} --write",
17
+ "build": "rm -rf dist && npm run build:esm && npm run build:cjs",
18
+ "build:esm": "tsc --module es2020 --outDir dist/esm",
19
+ "build:cjs": "tsc --module commonjs --outDir dist/cjs",
20
+ "test": "vitest run src",
20
21
  "prepare": "npm run build"
21
22
  },
22
23
  "keywords": [],
@@ -24,19 +25,15 @@
24
25
  "homepage": "https://jamcomments.com",
25
26
  "license": "GPL-2.0",
26
27
  "dependencies": {
27
- "@jam-comments/server-utilities": "^1.0.5",
28
- "@jam-comments/shared-utilities": "^0.0.4",
29
- "graphql-quest": "^1.1.2",
30
- "typescript": "^4.9.3",
31
- "url-parse": "^1.5.10"
28
+ "typescript": "^4.9.3"
32
29
  },
33
30
  "devDependencies": {
34
31
  "@types/node": "^18.11.9",
35
32
  "prettier": "^2.7.1",
36
- "vite": "^3.2.4"
33
+ "vite": "^3.2.4",
34
+ "vitest": "^0.25.3"
37
35
  },
38
36
  "publishConfig": {
39
37
  "access": "public"
40
- },
41
- "gitHead": "94d1edbe4bf2e543bd9ddce05bc1106e69d88c1e"
38
+ }
42
39
  }