@inweb/client 25.5.4 → 25.6.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.
@@ -465,9 +465,8 @@ export declare class Client extends EventEmitter2<ClientEventMap> {
465
465
  * @param description - Project description.
466
466
  * @param startDate - Project start date.
467
467
  * @param endDate - Project end date.
468
- * @param avatarUrl - Project preview image URL. Can be <a
469
- * href="https://developer.mozilla.org/docs/Web/HTTP/Basics_of_HTTP/Data_URIs"
470
- * target="_blank">Data URL</a>.
468
+ * @param avatarUrl - Project preview image URL. Can be
469
+ * {@link https://developer.mozilla.org/docs/Web/HTTP/Basics_of_HTTP/Data_URIs | Data URL}.
471
470
  */
472
471
  createProject(name: string, description?: string, startDate?: Date | string, endDate?: Date | string, avatarUrl?: string): Promise<Project>;
473
472
  /**
@@ -3,6 +3,7 @@ export declare class HttpClient implements IHttpClient {
3
3
  serverUrl: string;
4
4
  headers: HeadersInit;
5
5
  signInUserId: string;
6
+ signInUserIsAdmin: boolean;
6
7
  constructor(serverUrl: string);
7
8
  get(relativePath: string, signal?: AbortSignal): Promise<Response>;
8
9
  post(relativePath: string, body?: BodyInit | object): Promise<Response>;
@@ -2,6 +2,7 @@ export interface IHttpClient {
2
2
  serverUrl: string;
3
3
  headers: HeadersInit;
4
4
  signInUserId: string;
5
+ signInUserIsAdmin: boolean;
5
6
  get(relativePath: string, signal?: AbortSignal): Promise<Response>;
6
7
  post(relativePath: string, body?: BodyInit | object): Promise<Response>;
7
8
  put(relativePath: string, body?: BodyInit | object): Promise<Response>;
package/lib/Api/User.d.ts CHANGED
@@ -166,14 +166,23 @@ export declare class User {
166
166
  * administrator rights, he can only set his own avatar, otherwise an exception will be thrown.
167
167
  *
168
168
  * @async
169
- * @param image - Avatar image. Can be a <a
170
- * href="https://developer.mozilla.org/docs/Web/HTTP/Basics_of_HTTP/Data_URIs"
171
- * target="_blank">Data URL</a> string, <a
172
- * href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"
173
- * target="_blank">ArrayBuffer</a>, <a
174
- * href="https://developer.mozilla.org/docs/Web/API/Blob/Blob" target="_blank">Blob</a> or
175
- * Web API <a href="https://developer.mozilla.org/docs/Web/API/File"
176
- * target="_blank">File</a> object. Setting the `image` to `null` will remove the avatar.
177
- */
178
- setAvatar(image: BodyInit | null): Promise<this>;
169
+ * @param image - Avatar image. Can be a
170
+ * {@link https://developer.mozilla.org/docs/Web/HTTP/Basics_of_HTTP/Data_URIs | Data URL}
171
+ * string,
172
+ * {@link https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer | ArrayBuffer},
173
+ * {@link https://developer.mozilla.org/docs/Web/API/Blob/Blob | Blob} or Web API
174
+ * {@link https://developer.mozilla.org/docs/Web/API/File | File} object. Setting the
175
+ * `image` to `null` will remove the avatar.
176
+ */
177
+ setAvatar(image?: BodyInit | null): Promise<this>;
178
+ /**
179
+ * Remove the user avatar.
180
+ *
181
+ * Only admins can remove the avatar of other users, if the current logged in user does not
182
+ * have administrator rights, they can only remove their own avatar, otherwise an exception
183
+ * will be thrown.
184
+ *
185
+ * @async
186
+ */
187
+ deleteAvatar(): Promise<this>;
179
188
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inweb/client",
3
- "version": "25.5.4",
3
+ "version": "25.6.0",
4
4
  "description": "JavaScript REST API client for the Open Cloud Server",
5
5
  "homepage": "https://cloud.opendesign.com/docs/index.html",
6
6
  "license": "SEE LICENSE IN LICENSE",
@@ -26,6 +26,6 @@
26
26
  "ts-docs": "typedoc"
27
27
  },
28
28
  "dependencies": {
29
- "@inweb/eventemitter2": "~25.4.10"
29
+ "@inweb/eventemitter2": "~25.6.0"
30
30
  }
31
31
  }
@@ -22,12 +22,12 @@
22
22
  ///////////////////////////////////////////////////////////////////////////////
23
23
 
24
24
  import { IHttpClient } from "./IHttpClient";
25
+ import { FetchError } from "./FetchError";
25
26
  import { IAssociatedFileData, IAssemblyVersionInfo } from "./IAssembly";
26
27
  import { IShortUserDescription } from "./IUser";
27
28
  import { Model } from "./Model";
28
29
  import { ClashTest } from "./ClashTest";
29
30
  import { waitFor, userFullName, userInitials } from "./Utils";
30
- import { FetchError } from "./FetchError";
31
31
 
32
32
  /**
33
33
  * The class representing a `assembly` entity.
package/src/Api/Client.ts CHANGED
@@ -25,6 +25,7 @@ import { EventEmitter2 } from "@inweb/eventemitter2";
25
25
 
26
26
  import { IHttpClient } from "./IHttpClient";
27
27
  import { HttpClient } from "./HttpClient";
28
+ import { FetchError } from "./FetchError";
28
29
  import { ClientEventMap } from "./ClientEvents";
29
30
  import { Assembly } from "./Assembly";
30
31
  import { File } from "./File";
@@ -223,6 +224,7 @@ export class Client extends EventEmitter2<ClientEventMap> {
223
224
  this._user = new User(data, this.httpClient);
224
225
  this.httpClient.headers = { Authorization: data.tokenInfo.token };
225
226
  this.httpClient.signInUserId = this._user.id;
227
+ this.httpClient.signInUserIsAdmin = this._user.isAdmin;
226
228
  return this._user;
227
229
  }
228
230
 
@@ -230,6 +232,7 @@ export class Client extends EventEmitter2<ClientEventMap> {
230
232
  this._user = null;
231
233
  this.httpClient.headers = {};
232
234
  this.httpClient.signInUserId = "";
235
+ this.httpClient.signInUserIsAdmin = false;
233
236
  }
234
237
 
235
238
  /**
@@ -303,18 +306,20 @@ export class Client extends EventEmitter2<ClientEventMap> {
303
306
  * @param userId - User ID.
304
307
  */
305
308
  getUser(userId: string): Promise<User> {
306
- if (userId === this.httpClient.signInUserId) {
309
+ if (this.httpClient.signInUserIsAdmin) {
307
310
  return this.httpClient
308
- .get("/user")
311
+ .get(`/users/${userId}`)
309
312
  .then((response) => response.json())
310
- .then((data) => ({ id: userId, ...data }))
313
+ .then((data) => ({ id: data.id, ...data.userBrief }))
311
314
  .then((data) => new User(data, this.httpClient));
312
- } else {
315
+ } else if (userId === this.httpClient.signInUserId) {
313
316
  return this.httpClient
314
- .get(`/users/${userId}`)
317
+ .get("/user")
315
318
  .then((response) => response.json())
316
- .then((data) => ({ id: data.id, ...data.userBrief }))
319
+ .then((data) => ({ id: userId, ...data }))
317
320
  .then((data) => new User(data, this.httpClient));
321
+ } else {
322
+ return Promise.reject(new FetchError(403));
318
323
  }
319
324
  }
320
325
 
@@ -383,15 +388,19 @@ export class Client extends EventEmitter2<ClientEventMap> {
383
388
  * @returns Returns the raw data of a deleted user.
384
389
  */
385
390
  deleteUser(userId: string): Promise<any> {
386
- return this.httpClient
387
- .delete(`/users/${userId}`)
388
- .then((response) => response.json())
389
- .then((data) => {
390
- if (userId === this.httpClient.signInUserId) {
391
- this.clearCurrentUser();
392
- }
393
- return data;
394
- });
391
+ if (this.httpClient.signInUserIsAdmin) {
392
+ return this.httpClient
393
+ .delete(`/users/${userId}`)
394
+ .then((response) => response.json())
395
+ .then((data) => {
396
+ if (userId === this.httpClient.signInUserId) {
397
+ this.clearCurrentUser();
398
+ }
399
+ return data;
400
+ });
401
+ } else {
402
+ return Promise.reject(new FetchError(403));
403
+ }
395
404
  }
396
405
 
397
406
  /**
@@ -879,9 +888,8 @@ export class Client extends EventEmitter2<ClientEventMap> {
879
888
  * @param description - Project description.
880
889
  * @param startDate - Project start date.
881
890
  * @param endDate - Project end date.
882
- * @param avatarUrl - Project preview image URL. Can be <a
883
- * href="https://developer.mozilla.org/docs/Web/HTTP/Basics_of_HTTP/Data_URIs"
884
- * target="_blank">Data URL</a>.
891
+ * @param avatarUrl - Project preview image URL. Can be
892
+ * {@link https://developer.mozilla.org/docs/Web/HTTP/Basics_of_HTTP/Data_URIs | Data URL}.
885
893
  */
886
894
  createProject(
887
895
  name: string,
@@ -29,6 +29,7 @@ export class HttpClient implements IHttpClient {
29
29
  public serverUrl: string;
30
30
  public headers: HeadersInit = {};
31
31
  public signInUserId = "";
32
+ public signInUserIsAdmin = false;
32
33
 
33
34
  constructor(serverUrl: string) {
34
35
  this.serverUrl = serverUrl;
@@ -25,6 +25,7 @@ export interface IHttpClient {
25
25
  serverUrl: string;
26
26
  headers: HeadersInit;
27
27
  signInUserId: string;
28
+ signInUserIsAdmin: boolean;
28
29
 
29
30
  get(relativePath: string, signal?: AbortSignal): Promise<Response>;
30
31
 
package/src/Api/User.ts CHANGED
@@ -22,6 +22,7 @@
22
22
  ///////////////////////////////////////////////////////////////////////////////
23
23
 
24
24
  import { IHttpClient } from "./IHttpClient";
25
+ import { FetchError } from "./FetchError";
25
26
  import { userFullName, userInitials } from "./Utils";
26
27
 
27
28
  /**
@@ -236,14 +237,16 @@ export class User {
236
237
  * administrator rights, hi can only checkout himself, otherwise an exception will be thrown.
237
238
  */
238
239
  async checkout(): Promise<this> {
239
- if (this.id === this.httpClient.signInUserId) {
240
+ if (this.httpClient.signInUserIsAdmin) {
241
+ const response = await this.httpClient.get(`/users/${this.id}`);
242
+ const data = await response.json();
243
+ this.data = { id: data.id, ...data.userBrief };
244
+ } else if (this.id === this.httpClient.signInUserId) {
240
245
  const response = await this.httpClient.get("/user");
241
246
  const data = await response.json();
242
247
  this.data = { id: this.id, ...data };
243
248
  } else {
244
- const response = await this.httpClient.get(`/users/${this.id}`);
245
- const data = await response.json();
246
- this.data = { id: data.id, ...data.userBrief };
249
+ return Promise.reject(new FetchError(403));
247
250
  }
248
251
  return this;
249
252
  }
@@ -258,14 +261,16 @@ export class User {
258
261
  * @param data - Raw user data.
259
262
  */
260
263
  async update(data: any): Promise<this> {
261
- if (this.id === this.httpClient.signInUserId) {
264
+ if (this.httpClient.signInUserIsAdmin) {
265
+ const response = await this.httpClient.put(`/users/${this.id}`, { userBrief: data });
266
+ const newData = await response.json();
267
+ this.data = { id: newData.id, ...newData.userBrief };
268
+ } else if (this.id === this.httpClient.signInUserId) {
262
269
  const response = await this.httpClient.put("/user", data);
263
270
  const newData = await response.json();
264
271
  this.data = { id: this.id, ...newData };
265
272
  } else {
266
- const response = await this.httpClient.put(`/users/${this.id}`, { userBrief: data });
267
- const newData = await response.json();
268
- this.data = { id: newData.id, ...newData.userBrief };
273
+ return Promise.reject(new FetchError(403));
269
274
  }
270
275
  return this;
271
276
  }
@@ -285,16 +290,21 @@ export class User {
285
290
  * @returns Returns the raw data of a deleted user.
286
291
  */
287
292
  delete(): Promise<any> {
288
- return this.httpClient
289
- .delete(`/users/${this.id}`)
290
- .then((response) => response.json())
291
- .then((data) => {
292
- if (this.id === this.httpClient.signInUserId) {
293
- this.httpClient.headers = {};
294
- this.httpClient.signInUserId = "";
295
- }
296
- return data;
297
- });
293
+ if (this.httpClient.signInUserIsAdmin) {
294
+ return this.httpClient
295
+ .delete(`/users/${this.id}`)
296
+ .then((response) => response.json())
297
+ .then((data) => {
298
+ if (this.id === this.httpClient.signInUserId) {
299
+ this.httpClient.headers = {};
300
+ this.httpClient.signInUserId = "";
301
+ this.httpClient.signInUserIsAdmin = false;
302
+ }
303
+ return data;
304
+ });
305
+ } else {
306
+ return Promise.reject(new FetchError(403));
307
+ }
298
308
  }
299
309
 
300
310
  /**
@@ -314,36 +324,51 @@ export class User {
314
324
  * administrator rights, he can only set his own avatar, otherwise an exception will be thrown.
315
325
  *
316
326
  * @async
317
- * @param image - Avatar image. Can be a <a
318
- * href="https://developer.mozilla.org/docs/Web/HTTP/Basics_of_HTTP/Data_URIs"
319
- * target="_blank">Data URL</a> string, <a
320
- * href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"
321
- * target="_blank">ArrayBuffer</a>, <a
322
- * href="https://developer.mozilla.org/docs/Web/API/Blob/Blob" target="_blank">Blob</a> or
323
- * Web API <a href="https://developer.mozilla.org/docs/Web/API/File"
324
- * target="_blank">File</a> object. Setting the `image` to `null` will remove the avatar.
327
+ * @param image - Avatar image. Can be a
328
+ * {@link https://developer.mozilla.org/docs/Web/HTTP/Basics_of_HTTP/Data_URIs | Data URL}
329
+ * string,
330
+ * {@link https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer | ArrayBuffer},
331
+ * {@link https://developer.mozilla.org/docs/Web/API/Blob/Blob | Blob} or Web API
332
+ * {@link https://developer.mozilla.org/docs/Web/API/File | File} object. Setting the
333
+ * `image` to `null` will remove the avatar.
334
+ */
335
+ async setAvatar(image?: BodyInit | null): Promise<this> {
336
+ if (!image) {
337
+ await this.deleteAvatar();
338
+ } else if (this.httpClient.signInUserIsAdmin) {
339
+ const response = await this.httpClient.post(`/users/${this.id}/avatar`, image);
340
+ const data = await response.json();
341
+ this.data = { id: data.id, ...data.userBrief };
342
+ } else if (this.id === this.httpClient.signInUserId) {
343
+ const response = await this.httpClient.post("/user/avatar", image);
344
+ const data = await response.json();
345
+ this.data = { id: this.id, ...data };
346
+ } else {
347
+ return Promise.reject(new FetchError(403));
348
+ }
349
+ return this;
350
+ }
351
+
352
+ /**
353
+ * Remove the user avatar.
354
+ *
355
+ * Only admins can remove the avatar of other users, if the current logged in user does not
356
+ * have administrator rights, they can only remove their own avatar, otherwise an exception
357
+ * will be thrown.
358
+ *
359
+ * @async
325
360
  */
326
- async setAvatar(image: BodyInit | null): Promise<this> {
327
- if (image) {
328
- if (this.id === this.httpClient.signInUserId) {
329
- const response = await this.httpClient.post("/user/avatar", image);
330
- const data = await response.json();
331
- this.data = { id: this.id, ...data };
332
- } else {
333
- const response = await this.httpClient.post(`/users/${this.id}/avatar`, image);
334
- const data = await response.json();
335
- this.data = { id: data.id, ...data.userBrief };
336
- }
361
+ async deleteAvatar(): Promise<this> {
362
+ if (this.httpClient.signInUserIsAdmin) {
363
+ const response = await this.httpClient.delete(`/users/${this.id}/avatar`);
364
+ const data = await response.json();
365
+ this.data = { id: data.id, ...data.userBrief };
366
+ } else if (this.id === this.httpClient.signInUserId) {
367
+ const response = await this.httpClient.delete("/user/avatar");
368
+ const data = await response.json();
369
+ this.data = { id: this.id, ...data };
337
370
  } else {
338
- if (this.id === this.httpClient.signInUserId) {
339
- const response = await this.httpClient.delete("/user/avatar");
340
- const data = await response.json();
341
- this.data = { id: this.id, ...data };
342
- } else {
343
- const response = await this.httpClient.delete(`/users/${this.id}/avatar`);
344
- const data = await response.json();
345
- this.data = { id: data.id, ...data.userBrief };
346
- }
371
+ return Promise.reject(new FetchError(403));
347
372
  }
348
373
  return this;
349
374
  }