@elementor/editor-current-user 3.32.0-37 → 3.32.0-39
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/index.d.mts +10 -7
- package/dist/index.d.ts +10 -7
- package/dist/index.js +37 -20
- package/dist/index.mjs +36 -20
- package/package.json +4 -4
- package/src/api.ts +1 -1
- package/src/ensure-current-user.ts +9 -16
- package/src/get-current-user.ts +7 -2
- package/src/index.ts +2 -1
- package/src/on-set-user.ts +24 -0
- package/src/use-current-user.ts +2 -2
- package/src/__tests__/ensure-current-user.test.tsx +0 -36
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { QueryClient } from '@elementor/query';
|
|
2
|
-
|
|
3
1
|
declare const useSuppressedMessage: (messageKey: string) => readonly [boolean, () => void];
|
|
4
2
|
|
|
5
3
|
declare const useCurrentUserCapabilities: () => {
|
|
@@ -7,13 +5,18 @@ declare const useCurrentUserCapabilities: () => {
|
|
|
7
5
|
capabilities: string[] | undefined;
|
|
8
6
|
};
|
|
9
7
|
|
|
10
|
-
|
|
8
|
+
type UserModel = {
|
|
9
|
+
suppressedMessages: string[];
|
|
10
|
+
capabilities: string[];
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
declare const getCurrentUser: () => UserModel | undefined;
|
|
14
|
+
|
|
15
|
+
declare function ensureUser(): Promise<{
|
|
11
16
|
capabilities: string[];
|
|
12
17
|
suppressedMessages: string[];
|
|
13
18
|
}>;
|
|
14
19
|
|
|
15
|
-
declare function
|
|
16
|
-
queryClient: QueryClient;
|
|
17
|
-
}): unknown;
|
|
20
|
+
declare function onSetUser(callback: (user: UserModel | null) => void): () => void;
|
|
18
21
|
|
|
19
|
-
export {
|
|
22
|
+
export { ensureUser, getCurrentUser, onSetUser, useCurrentUserCapabilities, useSuppressedMessage };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { QueryClient } from '@elementor/query';
|
|
2
|
-
|
|
3
1
|
declare const useSuppressedMessage: (messageKey: string) => readonly [boolean, () => void];
|
|
4
2
|
|
|
5
3
|
declare const useCurrentUserCapabilities: () => {
|
|
@@ -7,13 +5,18 @@ declare const useCurrentUserCapabilities: () => {
|
|
|
7
5
|
capabilities: string[] | undefined;
|
|
8
6
|
};
|
|
9
7
|
|
|
10
|
-
|
|
8
|
+
type UserModel = {
|
|
9
|
+
suppressedMessages: string[];
|
|
10
|
+
capabilities: string[];
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
declare const getCurrentUser: () => UserModel | undefined;
|
|
14
|
+
|
|
15
|
+
declare function ensureUser(): Promise<{
|
|
11
16
|
capabilities: string[];
|
|
12
17
|
suppressedMessages: string[];
|
|
13
18
|
}>;
|
|
14
19
|
|
|
15
|
-
declare function
|
|
16
|
-
queryClient: QueryClient;
|
|
17
|
-
}): unknown;
|
|
20
|
+
declare function onSetUser(callback: (user: UserModel | null) => void): () => void;
|
|
18
21
|
|
|
19
|
-
export {
|
|
22
|
+
export { ensureUser, getCurrentUser, onSetUser, useCurrentUserCapabilities, useSuppressedMessage };
|
package/dist/index.js
CHANGED
|
@@ -20,8 +20,9 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
|
-
|
|
23
|
+
ensureUser: () => ensureUser,
|
|
24
24
|
getCurrentUser: () => getCurrentUser,
|
|
25
|
+
onSetUser: () => onSetUser,
|
|
25
26
|
useCurrentUserCapabilities: () => useCurrentUserCapabilities,
|
|
26
27
|
useSuppressedMessage: () => useSuppressedMessage
|
|
27
28
|
});
|
|
@@ -44,16 +45,11 @@ var apiClient = {
|
|
|
44
45
|
})
|
|
45
46
|
};
|
|
46
47
|
|
|
47
|
-
// src/get-current-user.ts
|
|
48
|
-
var getCurrentUser = () => {
|
|
49
|
-
return apiClient.get();
|
|
50
|
-
};
|
|
51
|
-
|
|
52
48
|
// src/use-current-user.ts
|
|
53
49
|
var EDITOR_CURRENT_USER_QUERY_KEY = "editor-current-user";
|
|
54
50
|
var useCurrentUser = () => (0, import_query.useQuery)({
|
|
55
51
|
queryKey: [EDITOR_CURRENT_USER_QUERY_KEY],
|
|
56
|
-
queryFn:
|
|
52
|
+
queryFn: apiClient.get
|
|
57
53
|
});
|
|
58
54
|
|
|
59
55
|
// src/use-update-current-user.ts
|
|
@@ -90,26 +86,47 @@ var useCurrentUserCapabilities = () => {
|
|
|
90
86
|
return { canUser, capabilities: data?.capabilities };
|
|
91
87
|
};
|
|
92
88
|
|
|
89
|
+
// src/get-current-user.ts
|
|
90
|
+
var import_query3 = require("@elementor/query");
|
|
91
|
+
var getCurrentUser = () => {
|
|
92
|
+
const queryClient = (0, import_query3.getQueryClient)();
|
|
93
|
+
return queryClient.getQueryData([EDITOR_CURRENT_USER_QUERY_KEY]);
|
|
94
|
+
};
|
|
95
|
+
|
|
93
96
|
// src/ensure-current-user.ts
|
|
97
|
+
var import_query4 = require("@elementor/query");
|
|
98
|
+
async function ensureUser() {
|
|
99
|
+
const queryClient = (0, import_query4.getQueryClient)();
|
|
100
|
+
return queryClient.ensureQueryData({
|
|
101
|
+
queryKey: [EDITOR_CURRENT_USER_QUERY_KEY],
|
|
102
|
+
queryFn: apiClient.get,
|
|
103
|
+
retry: false
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// src/on-set-user.ts
|
|
94
108
|
var import_editor_v1_adapters = require("@elementor/editor-v1-adapters");
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
}
|
|
109
|
+
var import_query5 = require("@elementor/query");
|
|
110
|
+
function onSetUser(callback) {
|
|
111
|
+
let unsubscribeQuery;
|
|
112
|
+
const unsubscribeListener = (0, import_editor_v1_adapters.__privateListenTo)((0, import_editor_v1_adapters.v1ReadyEvent)(), () => {
|
|
113
|
+
const queryClient = (0, import_query5.getQueryClient)();
|
|
114
|
+
unsubscribeQuery = queryClient.getQueryCache().subscribe((event) => {
|
|
115
|
+
if (event.query.queryKey.includes(EDITOR_CURRENT_USER_QUERY_KEY)) {
|
|
116
|
+
callback(event.query.state.data);
|
|
117
|
+
}
|
|
118
|
+
});
|
|
106
119
|
});
|
|
107
|
-
return
|
|
120
|
+
return () => {
|
|
121
|
+
unsubscribeQuery();
|
|
122
|
+
unsubscribeListener();
|
|
123
|
+
};
|
|
108
124
|
}
|
|
109
125
|
// Annotate the CommonJS export names for ESM import in node:
|
|
110
126
|
0 && (module.exports = {
|
|
111
|
-
|
|
127
|
+
ensureUser,
|
|
112
128
|
getCurrentUser,
|
|
129
|
+
onSetUser,
|
|
113
130
|
useCurrentUserCapabilities,
|
|
114
131
|
useSuppressedMessage
|
|
115
132
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -15,16 +15,11 @@ var apiClient = {
|
|
|
15
15
|
})
|
|
16
16
|
};
|
|
17
17
|
|
|
18
|
-
// src/get-current-user.ts
|
|
19
|
-
var getCurrentUser = () => {
|
|
20
|
-
return apiClient.get();
|
|
21
|
-
};
|
|
22
|
-
|
|
23
18
|
// src/use-current-user.ts
|
|
24
19
|
var EDITOR_CURRENT_USER_QUERY_KEY = "editor-current-user";
|
|
25
20
|
var useCurrentUser = () => useQuery({
|
|
26
21
|
queryKey: [EDITOR_CURRENT_USER_QUERY_KEY],
|
|
27
|
-
queryFn:
|
|
22
|
+
queryFn: apiClient.get
|
|
28
23
|
});
|
|
29
24
|
|
|
30
25
|
// src/use-update-current-user.ts
|
|
@@ -61,25 +56,46 @@ var useCurrentUserCapabilities = () => {
|
|
|
61
56
|
return { canUser, capabilities: data?.capabilities };
|
|
62
57
|
};
|
|
63
58
|
|
|
59
|
+
// src/get-current-user.ts
|
|
60
|
+
import { getQueryClient } from "@elementor/query";
|
|
61
|
+
var getCurrentUser = () => {
|
|
62
|
+
const queryClient = getQueryClient();
|
|
63
|
+
return queryClient.getQueryData([EDITOR_CURRENT_USER_QUERY_KEY]);
|
|
64
|
+
};
|
|
65
|
+
|
|
64
66
|
// src/ensure-current-user.ts
|
|
65
|
-
import {
|
|
66
|
-
function
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
retry: false
|
|
73
|
-
});
|
|
74
|
-
} catch {
|
|
75
|
-
queryClient.setQueryData([EDITOR_CURRENT_USER_QUERY_KEY], null);
|
|
76
|
-
}
|
|
67
|
+
import { getQueryClient as getQueryClient2 } from "@elementor/query";
|
|
68
|
+
async function ensureUser() {
|
|
69
|
+
const queryClient = getQueryClient2();
|
|
70
|
+
return queryClient.ensureQueryData({
|
|
71
|
+
queryKey: [EDITOR_CURRENT_USER_QUERY_KEY],
|
|
72
|
+
queryFn: apiClient.get,
|
|
73
|
+
retry: false
|
|
77
74
|
});
|
|
78
|
-
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// src/on-set-user.ts
|
|
78
|
+
import { __privateListenTo as listenTo, v1ReadyEvent } from "@elementor/editor-v1-adapters";
|
|
79
|
+
import { getQueryClient as getQueryClient3 } from "@elementor/query";
|
|
80
|
+
function onSetUser(callback) {
|
|
81
|
+
let unsubscribeQuery;
|
|
82
|
+
const unsubscribeListener = listenTo(v1ReadyEvent(), () => {
|
|
83
|
+
const queryClient = getQueryClient3();
|
|
84
|
+
unsubscribeQuery = queryClient.getQueryCache().subscribe((event) => {
|
|
85
|
+
if (event.query.queryKey.includes(EDITOR_CURRENT_USER_QUERY_KEY)) {
|
|
86
|
+
callback(event.query.state.data);
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
return () => {
|
|
91
|
+
unsubscribeQuery();
|
|
92
|
+
unsubscribeListener();
|
|
93
|
+
};
|
|
79
94
|
}
|
|
80
95
|
export {
|
|
81
|
-
|
|
96
|
+
ensureUser,
|
|
82
97
|
getCurrentUser,
|
|
98
|
+
onSetUser,
|
|
83
99
|
useCurrentUserCapabilities,
|
|
84
100
|
useSuppressedMessage
|
|
85
101
|
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@elementor/editor-current-user",
|
|
3
3
|
"description": "Elementor Editor Current User",
|
|
4
|
-
"version": "3.32.0-
|
|
4
|
+
"version": "3.32.0-39",
|
|
5
5
|
"private": false,
|
|
6
6
|
"author": "Elementor Team",
|
|
7
7
|
"homepage": "https://elementor.com/",
|
|
@@ -36,9 +36,9 @@
|
|
|
36
36
|
"react": "^18.3.1"
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@elementor/editor-v1-adapters": "3.32.0-
|
|
40
|
-
"@elementor/http-client": "3.32.0-
|
|
41
|
-
"@elementor/query": "3.32.0-
|
|
39
|
+
"@elementor/editor-v1-adapters": "3.32.0-39",
|
|
40
|
+
"@elementor/http-client": "3.32.0-39",
|
|
41
|
+
"@elementor/query": "3.32.0-39"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
44
|
"tsup": "^8.3.5"
|
package/src/api.ts
CHANGED
|
@@ -1,21 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { type QueryClient } from '@elementor/query';
|
|
1
|
+
import { getQueryClient } from '@elementor/query';
|
|
3
2
|
|
|
4
|
-
import {
|
|
3
|
+
import { apiClient } from './api';
|
|
5
4
|
import { EDITOR_CURRENT_USER_QUERY_KEY } from './use-current-user';
|
|
6
5
|
|
|
7
|
-
export function
|
|
8
|
-
|
|
9
|
-
try {
|
|
10
|
-
await queryClient.ensureQueryData( {
|
|
11
|
-
queryKey: [ EDITOR_CURRENT_USER_QUERY_KEY ],
|
|
12
|
-
queryFn: getCurrentUser,
|
|
13
|
-
retry: false,
|
|
14
|
-
} );
|
|
15
|
-
} catch {
|
|
16
|
-
queryClient.setQueryData( [ EDITOR_CURRENT_USER_QUERY_KEY ], null );
|
|
17
|
-
}
|
|
18
|
-
} );
|
|
6
|
+
export async function ensureUser() {
|
|
7
|
+
const queryClient = getQueryClient();
|
|
19
8
|
|
|
20
|
-
return queryClient.
|
|
9
|
+
return queryClient.ensureQueryData( {
|
|
10
|
+
queryKey: [ EDITOR_CURRENT_USER_QUERY_KEY ],
|
|
11
|
+
queryFn: apiClient.get,
|
|
12
|
+
retry: false,
|
|
13
|
+
} );
|
|
21
14
|
}
|
package/src/get-current-user.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { getQueryClient } from '@elementor/query';
|
|
2
|
+
|
|
3
|
+
import { type UserModel } from './api';
|
|
4
|
+
import { EDITOR_CURRENT_USER_QUERY_KEY } from './use-current-user';
|
|
2
5
|
|
|
3
6
|
export const getCurrentUser = () => {
|
|
4
|
-
|
|
7
|
+
const queryClient = getQueryClient();
|
|
8
|
+
|
|
9
|
+
return queryClient.getQueryData< UserModel >( [ EDITOR_CURRENT_USER_QUERY_KEY ] );
|
|
5
10
|
};
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export { useSuppressedMessage } from './use-suppressed-message';
|
|
2
2
|
export { useCurrentUserCapabilities } from './use-current-user-capabilities';
|
|
3
3
|
export { getCurrentUser } from './get-current-user';
|
|
4
|
-
export {
|
|
4
|
+
export { ensureUser } from './ensure-current-user';
|
|
5
|
+
export { onSetUser } from './on-set-user';
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { __privateListenTo as listenTo, v1ReadyEvent } from '@elementor/editor-v1-adapters';
|
|
2
|
+
import { getQueryClient } from '@elementor/query';
|
|
3
|
+
|
|
4
|
+
import { type UserModel } from './api';
|
|
5
|
+
import { EDITOR_CURRENT_USER_QUERY_KEY } from './use-current-user';
|
|
6
|
+
|
|
7
|
+
export function onSetUser( callback: ( user: UserModel | null ) => void ): () => void {
|
|
8
|
+
let unsubscribeQuery: () => void;
|
|
9
|
+
|
|
10
|
+
const unsubscribeListener = listenTo( v1ReadyEvent(), () => {
|
|
11
|
+
const queryClient = getQueryClient();
|
|
12
|
+
|
|
13
|
+
unsubscribeQuery = queryClient.getQueryCache().subscribe( ( event ) => {
|
|
14
|
+
if ( event.query.queryKey.includes( EDITOR_CURRENT_USER_QUERY_KEY ) ) {
|
|
15
|
+
callback( event.query.state.data as UserModel | null );
|
|
16
|
+
}
|
|
17
|
+
} );
|
|
18
|
+
} );
|
|
19
|
+
|
|
20
|
+
return () => {
|
|
21
|
+
unsubscribeQuery();
|
|
22
|
+
unsubscribeListener();
|
|
23
|
+
};
|
|
24
|
+
}
|
package/src/use-current-user.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { useQuery } from '@elementor/query';
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { apiClient } from './api';
|
|
4
4
|
|
|
5
5
|
export const EDITOR_CURRENT_USER_QUERY_KEY = 'editor-current-user';
|
|
6
6
|
|
|
7
7
|
export const useCurrentUser = () =>
|
|
8
8
|
useQuery( {
|
|
9
9
|
queryKey: [ EDITOR_CURRENT_USER_QUERY_KEY ],
|
|
10
|
-
queryFn:
|
|
10
|
+
queryFn: apiClient.get,
|
|
11
11
|
} );
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { registerDataHook } from '@elementor/editor-v1-adapters';
|
|
2
|
-
import { type QueryClient } from '@elementor/query';
|
|
3
|
-
|
|
4
|
-
import { ensureCurrentUser } from '../ensure-current-user';
|
|
5
|
-
|
|
6
|
-
jest.mock( '@elementor/editor-v1-adapters', () => ( {
|
|
7
|
-
...jest.requireActual( '@elementor/editor-v1-adapters' ),
|
|
8
|
-
registerDataHook: jest.fn(),
|
|
9
|
-
} ) );
|
|
10
|
-
|
|
11
|
-
describe( 'ensureCurrentUser', () => {
|
|
12
|
-
it( 'should not fail the attach preview command is user fetch failed', async () => {
|
|
13
|
-
// Arrange.
|
|
14
|
-
const queryClient = {
|
|
15
|
-
ensureQueryData: jest.fn().mockRejectedValue( new Error( 'Failed to fetch current user' ) ),
|
|
16
|
-
setQueryData: jest.fn(),
|
|
17
|
-
getQueryData: jest.fn().mockReturnValue( null ),
|
|
18
|
-
} as unknown as QueryClient;
|
|
19
|
-
|
|
20
|
-
jest.mocked( registerDataHook ).mockImplementation( ( _hook, _command, callback ) => {
|
|
21
|
-
return callback( {} ) as never;
|
|
22
|
-
} );
|
|
23
|
-
|
|
24
|
-
// Act.
|
|
25
|
-
const result = await ensureCurrentUser( { queryClient } );
|
|
26
|
-
|
|
27
|
-
// Assert.
|
|
28
|
-
expect( queryClient.ensureQueryData ).toHaveBeenCalledWith( {
|
|
29
|
-
queryKey: [ 'editor-current-user' ],
|
|
30
|
-
queryFn: expect.any( Function ),
|
|
31
|
-
retry: false,
|
|
32
|
-
} );
|
|
33
|
-
|
|
34
|
-
expect( result ).toBeNull();
|
|
35
|
-
} );
|
|
36
|
-
} );
|