@salesforce/webapp-experimental 1.33.2 → 1.33.4
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/dist/api/utils/user.d.ts.map +1 -1
- package/dist/api/utils/user.js +20 -19
- package/dist/api/utils/user.test.js +50 -50
- package/package.json +3 -3
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"user.d.ts","sourceRoot":"","sources":["../../../src/api/utils/user.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAKH,UAAU,IAAI;IACb,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;CACb;
|
|
1
|
+
{"version":3,"file":"user.d.ts","sourceRoot":"","sources":["../../../src/api/utils/user.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAKH,UAAU,IAAI;IACb,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;CACb;AAmBD;;;;GAIG;AACH,wBAAsB,cAAc,IAAI,OAAO,CAAC,IAAI,GAAG,IAAI,CAAC,CAuB3D"}
|
package/dist/api/utils/user.js
CHANGED
|
@@ -3,19 +3,16 @@
|
|
|
3
3
|
* All rights reserved.
|
|
4
4
|
* For full license text, see the LICENSE.txt file
|
|
5
5
|
*/
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
`;
|
|
6
|
+
import { createDataSDK } from "@salesforce/sdk-data";
|
|
7
|
+
import { API_VERSION } from "../clients.js";
|
|
8
|
+
// Lazy-initialized SDK instance
|
|
9
|
+
let sdkInstance = null;
|
|
10
|
+
async function getSDK() {
|
|
11
|
+
if (!sdkInstance) {
|
|
12
|
+
sdkInstance = await createDataSDK();
|
|
13
|
+
}
|
|
14
|
+
return sdkInstance;
|
|
15
|
+
}
|
|
19
16
|
/**
|
|
20
17
|
* Fetch current user information from Salesforce
|
|
21
18
|
* Uses Chatter API to get current user details
|
|
@@ -23,14 +20,18 @@ const CURRENT_USER_QUERY = gql `
|
|
|
23
20
|
*/
|
|
24
21
|
export async function getCurrentUser() {
|
|
25
22
|
try {
|
|
26
|
-
const
|
|
27
|
-
const
|
|
28
|
-
if (!
|
|
29
|
-
throw new Error(
|
|
23
|
+
const sdk = await getSDK();
|
|
24
|
+
const response = await sdk.fetch(`/services/data/v${API_VERSION}/chatter/users/me`);
|
|
25
|
+
if (!response.ok) {
|
|
26
|
+
throw new Error(`HTTP ${response.status}`);
|
|
27
|
+
}
|
|
28
|
+
const userData = await response.json();
|
|
29
|
+
if (!userData || !userData.id) {
|
|
30
|
+
throw new Error("No user data found in Chatter API response");
|
|
30
31
|
}
|
|
31
32
|
return {
|
|
32
|
-
id: userData.
|
|
33
|
-
name: userData.
|
|
33
|
+
id: userData.id,
|
|
34
|
+
name: userData.name || "User",
|
|
34
35
|
};
|
|
35
36
|
}
|
|
36
37
|
catch (error) {
|
|
@@ -3,21 +3,14 @@
|
|
|
3
3
|
* All rights reserved.
|
|
4
4
|
* For full license text, see the LICENSE.txt file
|
|
5
5
|
*/
|
|
6
|
+
import { createDataSDK } from "@salesforce/sdk-data";
|
|
6
7
|
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
|
7
8
|
import { getCurrentUser } from "./user.js";
|
|
8
|
-
|
|
9
|
-
vi.
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
return prev + curr + (values[i] ?? "");
|
|
14
|
-
}, "");
|
|
15
|
-
}),
|
|
16
|
-
executeGraphQL: vi.fn(),
|
|
17
|
-
};
|
|
18
|
-
});
|
|
19
|
-
// Get reference to the mocked function for assertions
|
|
20
|
-
const mockGraphql = vi.mocked(executeGraphQL);
|
|
9
|
+
vi.mock("@salesforce/sdk-data", () => ({
|
|
10
|
+
createDataSDK: vi.fn(),
|
|
11
|
+
}));
|
|
12
|
+
const mockFetch = vi.fn();
|
|
13
|
+
const mockSDK = { fetch: mockFetch };
|
|
21
14
|
// Mock console.error to avoid noise in test output
|
|
22
15
|
const mockConsoleError = vi.spyOn(console, "error").mockImplementation(() => {
|
|
23
16
|
/* noop */
|
|
@@ -25,36 +18,37 @@ const mockConsoleError = vi.spyOn(console, "error").mockImplementation(() => {
|
|
|
25
18
|
describe("User API Utils", () => {
|
|
26
19
|
beforeEach(() => {
|
|
27
20
|
vi.clearAllMocks();
|
|
21
|
+
vi.mocked(createDataSDK).mockResolvedValue(mockSDK);
|
|
28
22
|
});
|
|
29
23
|
afterEach(() => {
|
|
30
24
|
mockConsoleError.mockClear();
|
|
31
25
|
});
|
|
32
26
|
describe("getCurrentUser", () => {
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
},
|
|
39
|
-
},
|
|
27
|
+
const mockChatterUser = {
|
|
28
|
+
id: "005000000000001",
|
|
29
|
+
name: "John Doe",
|
|
30
|
+
email: "john@example.com",
|
|
31
|
+
username: "john@example.com",
|
|
40
32
|
};
|
|
41
33
|
it("successfully fetches current user", async () => {
|
|
42
|
-
|
|
34
|
+
mockFetch.mockResolvedValue({
|
|
35
|
+
ok: true,
|
|
36
|
+
json: () => Promise.resolve(mockChatterUser),
|
|
37
|
+
});
|
|
43
38
|
const result = await getCurrentUser();
|
|
44
|
-
expect(
|
|
39
|
+
expect(mockFetch).toHaveBeenCalledWith("/services/data/v99.0/chatter/users/me");
|
|
45
40
|
expect(result).toEqual({
|
|
46
41
|
id: "005000000000001",
|
|
47
42
|
name: "John Doe",
|
|
48
43
|
});
|
|
49
44
|
});
|
|
50
45
|
it('falls back to "User" when name is empty', async () => {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
},
|
|
46
|
+
mockFetch.mockResolvedValue({
|
|
47
|
+
ok: true,
|
|
48
|
+
json: () => Promise.resolve({
|
|
49
|
+
id: "005000000000003",
|
|
50
|
+
name: "",
|
|
51
|
+
}),
|
|
58
52
|
});
|
|
59
53
|
const result = await getCurrentUser();
|
|
60
54
|
expect(result).toEqual({
|
|
@@ -63,13 +57,12 @@ describe("User API Utils", () => {
|
|
|
63
57
|
});
|
|
64
58
|
});
|
|
65
59
|
it('falls back to "User" when name is null', async () => {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
},
|
|
60
|
+
mockFetch.mockResolvedValue({
|
|
61
|
+
ok: true,
|
|
62
|
+
json: () => Promise.resolve({
|
|
63
|
+
id: "005000000000004",
|
|
64
|
+
name: null,
|
|
65
|
+
}),
|
|
73
66
|
});
|
|
74
67
|
const result = await getCurrentUser();
|
|
75
68
|
expect(result).toEqual({
|
|
@@ -78,13 +71,11 @@ describe("User API Utils", () => {
|
|
|
78
71
|
});
|
|
79
72
|
});
|
|
80
73
|
it('falls back to "User" when name is undefined', async () => {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
},
|
|
87
|
-
},
|
|
74
|
+
mockFetch.mockResolvedValue({
|
|
75
|
+
ok: true,
|
|
76
|
+
json: () => Promise.resolve({
|
|
77
|
+
id: "005000000000005",
|
|
78
|
+
}),
|
|
88
79
|
});
|
|
89
80
|
const result = await getCurrentUser();
|
|
90
81
|
expect(result).toEqual({
|
|
@@ -93,17 +84,26 @@ describe("User API Utils", () => {
|
|
|
93
84
|
});
|
|
94
85
|
});
|
|
95
86
|
it("throws error when request fails", async () => {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
87
|
+
mockFetch.mockResolvedValue({
|
|
88
|
+
ok: false,
|
|
89
|
+
status: 500,
|
|
90
|
+
});
|
|
91
|
+
await expect(getCurrentUser()).rejects.toThrow("HTTP 500");
|
|
92
|
+
expect(mockConsoleError).toHaveBeenCalled();
|
|
100
93
|
});
|
|
101
94
|
it("throws error when no user data is found", async () => {
|
|
102
|
-
|
|
103
|
-
|
|
95
|
+
mockFetch.mockResolvedValue({
|
|
96
|
+
ok: true,
|
|
97
|
+
json: () => Promise.resolve({}),
|
|
104
98
|
});
|
|
105
|
-
await expect(getCurrentUser()).rejects.toThrow("No user data found");
|
|
99
|
+
await expect(getCurrentUser()).rejects.toThrow("No user data found in Chatter API response");
|
|
106
100
|
expect(mockConsoleError).toHaveBeenCalled();
|
|
107
101
|
});
|
|
102
|
+
it("throws error when fetch rejects", async () => {
|
|
103
|
+
const apiError = new Error("Network error");
|
|
104
|
+
mockFetch.mockRejectedValue(apiError);
|
|
105
|
+
await expect(getCurrentUser()).rejects.toThrow("Network error");
|
|
106
|
+
expect(mockConsoleError).toHaveBeenCalledWith("Error fetching user data:", apiError);
|
|
107
|
+
});
|
|
108
108
|
});
|
|
109
109
|
});
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@salesforce/webapp-experimental",
|
|
3
3
|
"description": "[experimental] Core package for Salesforce Web Applications",
|
|
4
|
-
"version": "1.33.
|
|
4
|
+
"version": "1.33.4",
|
|
5
5
|
"license": "SEE LICENSE IN LICENSE.txt",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "./dist/index.js",
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
42
|
"@salesforce/core": "^8.23.4",
|
|
43
|
-
"@salesforce/sdk-data": "^1.33.
|
|
43
|
+
"@salesforce/sdk-data": "^1.33.4",
|
|
44
44
|
"axios": "^1.7.7",
|
|
45
45
|
"micromatch": "^4.0.8",
|
|
46
46
|
"path-to-regexp": "^8.3.0"
|
|
@@ -55,5 +55,5 @@
|
|
|
55
55
|
"publishConfig": {
|
|
56
56
|
"access": "public"
|
|
57
57
|
},
|
|
58
|
-
"gitHead": "
|
|
58
|
+
"gitHead": "eeddfb5c8c8d44e2096b3196ca2ee902129e5655"
|
|
59
59
|
}
|