@js4cytoscape/ndex-client 0.5.0-alpha.9 → 0.6.0-alpha.2

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,233 @@
1
+ import { HTTPService } from './HTTPService';
2
+ import {
3
+ NDExUser,
4
+ PaginationParams,
5
+ NetworkSummaryV2
6
+ } from '../types';
7
+
8
+ /**
9
+ * UserService - NDEx user-related operations
10
+ * Handles authentication, user management, and user workspace operations
11
+ */
12
+ export class UserService {
13
+ constructor(private http: HTTPService) {}
14
+
15
+ // ============================================================================
16
+ // Authentication Methods
17
+ // ============================================================================
18
+
19
+ /**
20
+ * Get authenticated user (equivalent to getSignedInUser from legacy client)
21
+ * Returns the current signed-in user object from NDEx server
22
+ *
23
+ * For basic authentication: Gets the current user using the /user endpoint
24
+ * For OAuth authentication: Uses the /users/signin endpoint with ID token
25
+ *
26
+ * @returns The authenticated user object
27
+ * @note When authenticating with OAuth ID token, if a user with matching email
28
+ * doesn't exist on the server, this function will automatically create
29
+ * that user and return the newly created user object.
30
+ */
31
+ async authenticate(): Promise<NDExUser> {
32
+ const authType = this.http.getAuthType();
33
+ if (!authType) {
34
+ throw new Error('Authentication parameters are missing in NDEx client.');
35
+ }
36
+
37
+ if (authType === 'basic') {
38
+ // For basic auth, use the standard user endpoint with valid parameter
39
+ const endpoint = 'user';
40
+ const params = { valid: true };
41
+ return this.http.get<NDExUser>(endpoint, { params });
42
+ } else if (authType === 'oauth') {
43
+ // For OAuth, use the signin endpoint with ID token (v3 API)
44
+ const idToken = this.http.getIdToken();
45
+ if (!idToken) {
46
+ throw new Error('OAuth ID token is missing.');
47
+ }
48
+
49
+ const endpoint = 'users/signin';
50
+ const data = { idToken };
51
+ return this.http.post<NDExUser>(endpoint, data, { version: 'v3' });
52
+ }
53
+
54
+ throw new Error(`Unsupported authentication type: ${authType}`);
55
+ }
56
+
57
+ /**
58
+ * Get current user profile (equivalent to getSignedInUser from legacy client)
59
+ * Returns the current signed-in user object from NDEx server
60
+ *
61
+ * Uses the /user endpoint with {valid: true} parameter for both basic and OAuth authentication.
62
+ * Unlike authenticate(), this function will NOT automatically create a user account for OAuth.
63
+ * If the OAuth ID token doesn't match an existing user, it will return an unauthorized error.
64
+ *
65
+ * @returns The current user object
66
+ * @note This function requires existing authentication and will not create new users
67
+ */
68
+ async getCurrentUser(): Promise<NDExUser> {
69
+ const authType = this.http.getAuthType();
70
+ if (!authType) {
71
+ throw new Error('Authentication parameters are missing in NDEx client.');
72
+ }
73
+
74
+ const endpoint = 'user';
75
+ const params = { valid: true };
76
+ return this.http.get<NDExUser>(endpoint, { params });
77
+ }
78
+
79
+ /**
80
+ * Update current user profile
81
+ * Uses the UUID from the userUpdate object and server validates against authenticated user
82
+ */
83
+ async updateCurrentUser(userUpdate: Partial<NDExUser>): Promise<void> {
84
+ if (!userUpdate.externalId) {
85
+ throw new Error('User uuid is required in userUpdate object');
86
+ }
87
+
88
+ const endpoint = `user/${userUpdate.externalId}`;
89
+ return this.http.put<void>(endpoint, userUpdate);
90
+ }
91
+
92
+
93
+ /**
94
+ * Request password reset
95
+ */
96
+ async requestPasswordReset(email: string): Promise<void> {
97
+ const endpoint = 'user/forgot-password';
98
+ return this.http.post<void>(endpoint, { email });
99
+ }
100
+
101
+ /**
102
+ * Reset password with user UUID (server validates UUID matches authenticated user)
103
+ * @param userUUID - User UUID (must match authenticated user)
104
+ * @param newPassword - New password as plain text
105
+ */
106
+ async resetPassword(userUUID: string, newPassword: string): Promise<void> {
107
+ const endpoint = `user/${userUUID}/password`;
108
+ const config: any = {
109
+ headers: {
110
+ 'Content-Type': 'text/plain' // Send password as plain text
111
+ }
112
+ };
113
+
114
+ return this.http.put<void>(endpoint, newPassword, config);
115
+ }
116
+
117
+ // ============================================================================
118
+ // User Management Methods
119
+ // ============================================================================
120
+
121
+ /**
122
+ * Get user by UUID
123
+ */
124
+ async getUser(userUUID: string): Promise<NDExUser> {
125
+ const endpoint = `user/${userUUID}`;
126
+ return this.http.get<NDExUser>(endpoint);
127
+ }
128
+
129
+ /**
130
+ * Get user by account name or email (matches server getUserByAccountNameOrAuthenticatUser)
131
+ * @param searchBy - Search criteria - either by username OR by email, but not both
132
+ * @returns User object matching the search criteria
133
+ * @note If both username and email are provided, only username will be used
134
+ */
135
+ async getUserByNameOrEmail(searchBy:
136
+ | { username: string }
137
+ | { email: string }
138
+ ): Promise<NDExUser> {
139
+ const params = new URLSearchParams();
140
+
141
+ if ('username' in searchBy) {
142
+ params.append('username', searchBy.username);
143
+ } else if ('email' in searchBy) {
144
+ params.append('email', searchBy.email);
145
+ }
146
+
147
+ const endpoint = `user?${params.toString()}`;
148
+ return this.http.get<NDExUser>(endpoint);
149
+ }
150
+
151
+ /**
152
+ * Get multiple users by their UUIDs (batch operation)
153
+ * @param uuidList - Array of user UUIDs to fetch
154
+ * @returns Array of user objects corresponding to the provided UUIDs
155
+ */
156
+ async getUsersByUUIDs(uuidList: string[]): Promise<NDExUser[]> {
157
+ const endpoint = 'batch/user';
158
+ return this.http.post<NDExUser[]>(endpoint, uuidList);
159
+ }
160
+
161
+ /**
162
+ * Search users (equivalent to searchUsers from legacy client)
163
+ * @param searchTerms - Search string to find users
164
+ * @param start - Starting offset for pagination (optional)
165
+ * @param size - Maximum number of results to return (optional)
166
+ * @returns Array of users matching the search criteria
167
+ */
168
+ async searchUsers(
169
+ searchTerms: string,
170
+ start?: number,
171
+ size?: number
172
+ ): Promise<NDExUser[]> {
173
+ const params: Record<string, string> = {};
174
+
175
+ if (start !== undefined) {
176
+ params.start = start.toString();
177
+ }
178
+ if (size !== undefined) {
179
+ params.limit = size.toString();
180
+ }
181
+
182
+ const data = { searchString: searchTerms };
183
+ const endpoint = 'search/user';
184
+
185
+ return this.http.post<NDExUser[]>(endpoint, data, { params });
186
+ }
187
+
188
+
189
+ // ============================================================================
190
+ // User Network Methods
191
+ // ============================================================================
192
+
193
+ /**
194
+ * Get networks for a user's account page (equivalent to getAccountPageNetworks from legacy client)
195
+ *
196
+ * @param userUUID - User UUID to get networks for
197
+ * @param offset - Starting offset for pagination (optional)
198
+ * @param limit - Maximum number of networks to return (optional)
199
+ * @returns Array of V2 network summaries for the specified user
200
+ * @note This function requires authentication. The auth attribute must be set in the client configuration.
201
+ */
202
+ async getAccountPageNetworks(
203
+ userUUID: string,
204
+ offset?: number,
205
+ limit?: number
206
+ ): Promise<NetworkSummaryV2[]> {
207
+ // Build parameters for the network summary request
208
+ const params: Record<string, string> = {};
209
+ if (offset !== undefined) {
210
+ params.offset = offset.toString();
211
+ }
212
+ if (limit !== undefined) {
213
+ params.limit = limit.toString();
214
+ }
215
+
216
+ // Get network summaries for the user
217
+ const endpoint = `user/${userUUID}/networksummary`;
218
+ return this.http.get<NetworkSummaryV2[]>(endpoint, { params });
219
+ }
220
+
221
+
222
+
223
+ /**
224
+ * Delete user account (server validates userid matches authenticated user)
225
+ * @param userUUID - User UUID to delete (must match authenticated user)
226
+ * @note This function requires authentication. The auth attribute must be set in the client configuration.
227
+ * @note The userUUID parameter must be the UUID of the currently authenticated user. The server will validate this.
228
+ */
229
+ async deleteUserAccount(userUUID: string): Promise<void> {
230
+ const endpoint = `user/${userUUID}`;
231
+ return this.http.delete<void>(endpoint);
232
+ }
233
+ }
@@ -0,0 +1,159 @@
1
+ import { HTTPService } from './HTTPService';
2
+ import { CyWebWorkspace } from '../types';
3
+
4
+ /**
5
+ * WorkspaceService - NDEx CyWeb workspace operations
6
+ *
7
+ * Provides methods for managing CyWeb workspaces including creation, retrieval,
8
+ * updates, and deletion of workspaces and their associated networks.
9
+ * All workspace operations use the V3 API.
10
+ */
11
+ export class WorkspaceService {
12
+ constructor(private http: HTTPService) {}
13
+
14
+ /**
15
+ * Create a new CyWeb workspace (migrated from original NDEx.js)
16
+ *
17
+ * Creates a new workspace with the specified workspace data.
18
+ *
19
+ * @param workspace - The workspace object to create
20
+ * @returns Promise resolving to the response from the server (typically contains workspace location)
21
+ *
22
+ * @example
23
+ * ```typescript
24
+ * const workspaceData = {
25
+ * name: "My Workspace",
26
+ * description: "A sample workspace",
27
+ * networkIds: []
28
+ * };
29
+ * const response = await client.workspace.createCyWebWorkspace(workspaceData);
30
+ * ```
31
+ */
32
+ async createCyWebWorkspace(workspace: CyWebWorkspace): Promise<string> {
33
+ const endpoint = 'workspaces';
34
+ return this.http.post<string>(endpoint, workspace, { version: 'v3' });
35
+ }
36
+
37
+ /**
38
+ * Get a CyWeb workspace by ID (migrated from original NDEx.js)
39
+ *
40
+ * Retrieves a workspace and its details by workspace ID.
41
+ *
42
+ * @param workspaceId - The UUID of the workspace to retrieve
43
+ * @returns Promise resolving to the workspace object
44
+ *
45
+ * @example
46
+ * ```typescript
47
+ * const workspace = await client.workspace.getCyWebWorkspace('workspace-uuid');
48
+ * console.log(workspace.name);
49
+ * ```
50
+ */
51
+ async getCyWebWorkspace(workspaceId: string): Promise<CyWebWorkspace> {
52
+ const endpoint = `workspaces/${workspaceId}`;
53
+ return this.http.get<CyWebWorkspace>(endpoint, { version: 'v3' });
54
+ }
55
+
56
+ /**
57
+ * Delete a CyWeb workspace (migrated from original NDEx.js)
58
+ *
59
+ * Deletes the specified workspace permanently.
60
+ *
61
+ * @param workspaceId - The UUID of the workspace to delete
62
+ * @returns Promise resolving when the workspace is deleted
63
+ *
64
+ * @example
65
+ * ```typescript
66
+ * await client.workspace.deleteCyWebWorkspace('workspace-uuid');
67
+ * ```
68
+ */
69
+ async deleteCyWebWorkspace(workspaceId: string): Promise<void> {
70
+ const endpoint = `workspaces/${workspaceId}`;
71
+ return this.http.delete<void>(endpoint, { version: 'v3' });
72
+ }
73
+
74
+ /**
75
+ * Update a CyWeb workspace (migrated from original NDEx.js)
76
+ *
77
+ * Updates a workspace with new data, replacing the entire workspace object.
78
+ *
79
+ * @param workspaceId - The UUID of the workspace to update
80
+ * @param workspaceObj - The updated workspace object
81
+ * @returns Promise resolving when the workspace is updated
82
+ *
83
+ * @example
84
+ * ```typescript
85
+ * const updatedWorkspace = {
86
+ * name: "Updated Workspace Name",
87
+ * description: "Updated description",
88
+ * networkIds: ["network-uuid-1", "network-uuid-2"]
89
+ * };
90
+ * await client.workspace.updateCyWebWorkspace('workspace-uuid', updatedWorkspace);
91
+ * ```
92
+ */
93
+ async updateCyWebWorkspace(workspaceId: string, workspaceObj: CyWebWorkspace): Promise<void> {
94
+ const endpoint = `workspaces/${workspaceId}`;
95
+ return this.http.put<void>(endpoint, workspaceObj, { version: 'v3' });
96
+ }
97
+
98
+ /**
99
+ * Update CyWeb workspace name (migrated from original NDEx.js)
100
+ *
101
+ * Updates only the name of a workspace.
102
+ *
103
+ * @param workspaceId - The UUID of the workspace to update
104
+ * @param newName - The new name for the workspace
105
+ * @returns Promise resolving when the workspace name is updated
106
+ *
107
+ * @example
108
+ * ```typescript
109
+ * await client.workspace.updateCyWebWorkspaceName('workspace-uuid', 'New Workspace Name');
110
+ * ```
111
+ */
112
+ async updateCyWebWorkspaceName(workspaceId: string, newName: string): Promise<void> {
113
+ const endpoint = `workspaces/${workspaceId}/name`;
114
+ return this.http.put<void>(endpoint, { name: newName }, { version: 'v3' });
115
+ }
116
+
117
+ /**
118
+ * Update CyWeb workspace networks (migrated from original NDEx.js)
119
+ *
120
+ * Updates the list of network IDs associated with a workspace.
121
+ *
122
+ * @param workspaceId - The UUID of the workspace to update
123
+ * @param networkIds - Array of network UUIDs to associate with the workspace
124
+ * @returns Promise resolving when the workspace networks are updated
125
+ *
126
+ * @example
127
+ * ```typescript
128
+ * const networkIds = ['network-uuid-1', 'network-uuid-2', 'network-uuid-3'];
129
+ * await client.workspace.updateCyWebWorkspaceNetworks('workspace-uuid', networkIds);
130
+ * ```
131
+ */
132
+ async updateCyWebWorkspaceNetworks(workspaceId: string, networkIds: string[]): Promise<void> {
133
+ const endpoint = `workspaces/${workspaceId}/networkids`;
134
+ return this.http.put<void>(endpoint, networkIds, { version: 'v3' });
135
+ }
136
+
137
+ /**
138
+ * Get user's CyWeb workspaces (migrated from original NDEx.js)
139
+ *
140
+ * Retrieves all workspaces belonging to the currently authenticated user.
141
+ * Requires authentication.
142
+ *
143
+ * @returns Promise resolving to array of workspace objects
144
+ *
145
+ * @example
146
+ * ```typescript
147
+ * const workspaces = await client.workspace.getUserCyWebWorkspaces();
148
+ * workspaces.forEach(workspace => {
149
+ * console.log(`Workspace: ${workspace.name}`);
150
+ * });
151
+ * ```
152
+ */
153
+ async getUserCyWebWorkspaces(): Promise<CyWebWorkspace[]> {
154
+ // First get the current user to get their externalId
155
+ const user = await this.http.get<any>('user', { params: { valid: true } });
156
+ const endpoint = `users/${user.externalId}/workspaces`;
157
+ return this.http.get<CyWebWorkspace[]>(endpoint, { version: 'v3' });
158
+ }
159
+ }
@@ -0,0 +1,81 @@
1
+ /**
2
+ * Type definitions for CyNDEx (Cytoscape-NDEx Bridge) operations
3
+ */
4
+
5
+ /**
6
+ * Configuration for CyNDEx service
7
+ */
8
+ export interface CyNDExConfig {
9
+ /** Port number for Cytoscape REST API (default: 1234) */
10
+ port?: number;
11
+ /** Base URL for Cytoscape REST API (default: 'http://127.0.0.1') */
12
+ cyRestBaseURL?: string;
13
+ /** NDEx server base URL (default: 'https://www.ndexbio.org') */
14
+ ndexBaseURL?: string;
15
+ }
16
+
17
+ /**
18
+ * Authentication configuration for NDEx operations
19
+ * This is used as parameters passed to Cytoscape for NDEx authentication
20
+ */
21
+ export interface CyNDExAuthConfig {
22
+ type: 'basic' | 'oauth';
23
+ username?: string;
24
+ password?: string;
25
+ idToken?: string;
26
+ }
27
+
28
+ /**
29
+ * Parameters for importing NDEx network to Cytoscape
30
+ */
31
+ export interface NDExImportParams {
32
+ serverUrl: string;
33
+ uuid: string;
34
+ accessKey?: string;
35
+ createView?: boolean;
36
+ username?: string;
37
+ password?: string;
38
+ idToken?: string;
39
+ }
40
+
41
+ /**
42
+ * Parameters for exporting Cytoscape network to NDEx
43
+ */
44
+ export interface NDExExportParams {
45
+ serverUrl: string;
46
+ uuid?: string;
47
+ username?: string;
48
+ password?: string;
49
+ idToken?: string;
50
+ }
51
+
52
+ /**
53
+ * Cytoscape network summary response
54
+ */
55
+ export interface CytoscapeNetworkSummary {
56
+ suid?: string;
57
+ title?: string;
58
+ name?: string;
59
+ nodeCount?: number;
60
+ edgeCount?: number;
61
+ selected?: boolean;
62
+ [key: string]: any; // Allow additional properties from Cytoscape
63
+ }
64
+
65
+ /**
66
+ * CyNDEx status response
67
+ */
68
+ export interface CyNDExStatusResponse {
69
+ version?: string;
70
+ status?: string;
71
+ [key: string]: any; // Allow additional status properties
72
+ }
73
+
74
+ /**
75
+ * CX2 import parameters for Cytoscape
76
+ */
77
+ export interface CX2ImportParams {
78
+ format: 'cx2';
79
+ collection?: string;
80
+ title?: string;
81
+ }