@inweb/client 25.12.0 → 26.1.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.
Files changed (50) hide show
  1. package/dist/client.js +618 -322
  2. package/dist/client.js.map +1 -1
  3. package/dist/client.min.js +1 -1
  4. package/dist/client.module.js +393 -312
  5. package/dist/client.module.js.map +1 -1
  6. package/lib/Api/Assembly.d.ts +7 -10
  7. package/lib/Api/ClashTest.d.ts +4 -8
  8. package/lib/Api/Client.d.ts +53 -4
  9. package/lib/Api/Endpoint.d.ts +73 -0
  10. package/lib/Api/Fetch.d.ts +3 -3
  11. package/lib/Api/File.d.ts +32 -14
  12. package/lib/Api/HttpClient.d.ts +7 -7
  13. package/lib/Api/IFile.d.ts +1 -1
  14. package/lib/Api/IHttpClient.d.ts +31 -26
  15. package/lib/Api/ISharedLink.d.ts +36 -0
  16. package/lib/Api/Job.d.ts +2 -5
  17. package/lib/Api/Member.d.ts +2 -6
  18. package/lib/Api/Model.d.ts +2 -4
  19. package/lib/Api/OAuthClient.d.ts +2 -6
  20. package/lib/Api/Permission.d.ts +3 -7
  21. package/lib/Api/Project.d.ts +3 -7
  22. package/lib/Api/Role.d.ts +2 -5
  23. package/lib/Api/SharedFile.d.ts +9 -0
  24. package/lib/Api/SharedLink.d.ts +70 -0
  25. package/lib/Api/User.d.ts +2 -2
  26. package/lib/Api/XMLHttp.d.ts +1 -1
  27. package/lib/index.d.ts +5 -1
  28. package/package.json +2 -2
  29. package/src/Api/Assembly.ts +45 -58
  30. package/src/Api/ClashTest.ts +10 -24
  31. package/src/Api/Client.ts +88 -9
  32. package/src/Api/Endpoint.ts +130 -0
  33. package/src/Api/Fetch.ts +20 -20
  34. package/src/Api/File.ts +101 -75
  35. package/src/Api/HttpClient.ts +40 -17
  36. package/src/Api/IFile.ts +1 -1
  37. package/src/Api/IHttpClient.ts +32 -26
  38. package/src/Api/ISharedLink.ts +63 -0
  39. package/src/Api/Job.ts +7 -19
  40. package/src/Api/Member.ts +7 -21
  41. package/src/Api/Model.ts +4 -7
  42. package/src/Api/OAuthClient.ts +8 -24
  43. package/src/Api/Permission.ts +8 -22
  44. package/src/Api/Project.ts +30 -43
  45. package/src/Api/Role.ts +8 -19
  46. package/src/Api/SharedFile.ts +54 -0
  47. package/src/Api/SharedLink.ts +135 -0
  48. package/src/Api/User.ts +16 -16
  49. package/src/Api/XMLHttp.ts +1 -1
  50. package/src/index.ts +5 -9
package/src/Api/Client.ts CHANGED
@@ -33,6 +33,9 @@ import { Job } from "./Job";
33
33
  import { Project } from "./Project";
34
34
  import { User } from "./User";
35
35
  import { OAuthClient } from "./OAuthClient";
36
+ import { ISharedLinkPermissions } from "./ISharedLink";
37
+ import { SharedLink } from "./SharedLink";
38
+ import { SharedFile } from "./SharedFile";
36
39
  import { parseArgs } from "./Utils";
37
40
 
38
41
  /**
@@ -47,7 +50,7 @@ export class Client extends EventEmitter2<ClientEventMap> {
47
50
 
48
51
  /**
49
52
  * @param params - An object containing client configuration parameters.
50
- * @param params.serverUrl - Open Cloud Server REST API base URL.
53
+ * @param params.serverUrl - Open Cloud REST API server URL.
51
54
  * @param params.url - Deprecated since `25.8`. Use `serverUrl` instead.
52
55
  */
53
56
  constructor(params: { serverUrl?: string; url?: string } = {}) {
@@ -56,7 +59,7 @@ export class Client extends EventEmitter2<ClientEventMap> {
56
59
  }
57
60
 
58
61
  /**
59
- * Open Cloud REST API server base URL. Use {@link configure | configure()} to change server URL.
62
+ * Open Cloud REST API server URL. Use {@link configure | configure()} to change server URL.
60
63
  *
61
64
  * @readonly
62
65
  */
@@ -124,7 +127,7 @@ export class Client extends EventEmitter2<ClientEventMap> {
124
127
  * After changing the parameters, you must re-login.
125
128
  *
126
129
  * @param params - An object containing new parameters.
127
- * @param params.serverUrl - Open Cloud Server REST API base URL.
130
+ * @param params.serverUrl - Open Cloud REST API server URL.
128
131
  */
129
132
  configure(params: { serverUrl?: string }): this {
130
133
  this._serverUrl = (params.serverUrl || "").replace(/\/+$/, "");
@@ -203,7 +206,7 @@ export class Client extends EventEmitter2<ClientEventMap> {
203
206
  */
204
207
  async signInWithEmail(email: string, password: string): Promise<User> {
205
208
  const credentials = btoa(unescape(encodeURIComponent(email + ":" + password)));
206
- this.httpClient.headers = { Authorization: "Basic " + credentials };
209
+ this.httpClient.headers["Authorization"] = "Basic " + credentials;
207
210
  const response = await this.httpClient.get("/token");
208
211
  const data = await response.json();
209
212
  return this.setCurrentUser(data);
@@ -215,17 +218,27 @@ export class Client extends EventEmitter2<ClientEventMap> {
215
218
  * @param token - An access token for authentication request. See {@link User.token} for more details.
216
219
  */
217
220
  async signInWithToken(token: string): Promise<User> {
218
- this.httpClient.headers = { Authorization: token };
221
+ this.httpClient.headers["Authorization"] = token;
219
222
  const response = await this.httpClient.get("/user");
220
223
  const data = await response.json();
221
224
  return this.setCurrentUser(data);
222
225
  }
223
226
 
227
+ /**
228
+ * Log out.
229
+ *
230
+ * You must log in again using {@link signInWithEmail} or {@link signInWithToken} to continue
231
+ * making requests to the server
232
+ */
233
+ signOut(): void {
234
+ this.clearCurrentUser();
235
+ }
236
+
224
237
  // Save the current logged in user information for internal use.
225
238
 
226
239
  private setCurrentUser(data: any): User {
227
240
  this._user = new User(data, this.httpClient);
228
- this.httpClient.headers = { Authorization: data.tokenInfo.token };
241
+ this.httpClient.headers["Authorization"] = data.tokenInfo.token;
229
242
  this.httpClient.signInUserId = this._user.id;
230
243
  this.httpClient.signInUserIsAdmin = this._user.isAdmin;
231
244
  return this._user;
@@ -233,7 +246,7 @@ export class Client extends EventEmitter2<ClientEventMap> {
233
246
 
234
247
  private clearCurrentUser(): void {
235
248
  this._user = null;
236
- this.httpClient.headers = {};
249
+ delete this.httpClient.headers["Authorization"];
237
250
  this.httpClient.signInUserId = "";
238
251
  this.httpClient.signInUserIsAdmin = false;
239
252
  }
@@ -520,6 +533,7 @@ export class Client extends EventEmitter2<ClientEventMap> {
520
533
  * @param sortByDesc - Allows to specify the descending order of the result. By default,
521
534
  * files are sorted by name in ascending order.
522
535
  * @param sortField - Allows to specify sort field.
536
+ * @param shared - Returns shared files only.
523
537
  */
524
538
  getFiles(
525
539
  start?: number,
@@ -528,7 +542,8 @@ export class Client extends EventEmitter2<ClientEventMap> {
528
542
  ext?: string | string[],
529
543
  ids?: string | string[],
530
544
  sortByDesc?: boolean,
531
- sortField?: string
545
+ sortField?: string,
546
+ shared?: boolean
532
547
  ): Promise<{
533
548
  result: File[];
534
549
  start: number;
@@ -551,6 +566,7 @@ export class Client extends EventEmitter2<ClientEventMap> {
551
566
  }
552
567
  if (sortByDesc !== undefined) searchParams.set("sortBy", sortByDesc ? "desc" : "asc");
553
568
  if (sortField) searchParams.set("sortField", sortField);
569
+ if (shared) searchParams.set("shared", "true");
554
570
 
555
571
  let queryString = searchParams.toString();
556
572
  if (queryString) queryString = "?" + queryString;
@@ -668,7 +684,7 @@ export class Client extends EventEmitter2<ClientEventMap> {
668
684
  */
669
685
  downloadFile(fileId: string, onProgress?: (progress: number) => void, signal?: AbortSignal): Promise<ArrayBuffer> {
670
686
  return this.httpClient
671
- .downloadFile(`/files/${fileId}/downloads`, onProgress, signal)
687
+ .downloadFile(`/files/${fileId}/downloads`, onProgress, { signal })
672
688
  .then((response) => response.arrayBuffer());
673
689
  }
674
690
 
@@ -1038,4 +1054,67 @@ export class Client extends EventEmitter2<ClientEventMap> {
1038
1054
  }
1039
1055
  });
1040
1056
  }
1057
+
1058
+ /**
1059
+ * Returns information about the specified file shared link.
1060
+ *
1061
+ * @param token - Shared link token.
1062
+ */
1063
+ getSharedLink(token: string): Promise<SharedLink> {
1064
+ return this.httpClient
1065
+ .get(`/shares/${token}`)
1066
+ .then((response) => response.json())
1067
+ .then((data) => new SharedLink(data, this.httpClient));
1068
+ }
1069
+
1070
+ /**
1071
+ * Creates a shared link for the specified file.
1072
+ *
1073
+ * @param fileId - File ID.
1074
+ * @param permissions - Share permissions.
1075
+ */
1076
+ createSharedLink(fileId: string, permissions?: ISharedLinkPermissions): Promise<SharedLink> {
1077
+ return this.httpClient
1078
+ .post("/shares", {
1079
+ fileId,
1080
+ permissions,
1081
+ })
1082
+ .then((response) => response.json())
1083
+ .then((data) => new SharedLink(data, this.httpClient));
1084
+ }
1085
+
1086
+ /**
1087
+ * Deletes the specified shared link.
1088
+ *
1089
+ * Only file owner can delete shared link. If the current logged in user is not a file owner,
1090
+ * an exception will be thrown.
1091
+ *
1092
+ * @param token - Shared link token.
1093
+ * @returns Returns the raw data of a deleted shared link. For more information, see
1094
+ * {@link https://cloud.opendesign.com/docs//pages/server/api.html#ShareLinks | Open Cloud SharedLinks API}.
1095
+ */
1096
+ deleteSharedLink(token: string): Promise<any> {
1097
+ return this.httpClient.delete(`/shares/${token}`).then((response) => response.json());
1098
+ }
1099
+
1100
+ /**
1101
+ * Returns information about a file from a shared link.
1102
+ *
1103
+ * Some file features are not available via shared link:
1104
+ *
1105
+ * - Updating file properties, preview, and viewpoints
1106
+ * - Running file jobs
1107
+ * - Managing file permissions
1108
+ * - Managing file versions
1109
+ * - Deleting file
1110
+ *
1111
+ * @param token - Shared link token.
1112
+ * @param password - Password to get access to the file.
1113
+ */
1114
+ getSharedFile(token: string, password?: string): Promise<File> {
1115
+ return this.httpClient
1116
+ .get(`/shares/${token}/info`, { headers: { "InWeb-Password": password } })
1117
+ .then((response) => response.json())
1118
+ .then((data) => new SharedFile(data, password, this.httpClient));
1119
+ }
1041
1120
  }
@@ -0,0 +1,130 @@
1
+ ///////////////////////////////////////////////////////////////////////////////
2
+ // Copyright (C) 2002-2024, Open Design Alliance (the "Alliance").
3
+ // All rights reserved.
4
+ //
5
+ // This software and its documentation and related materials are owned by
6
+ // the Alliance. The software may only be incorporated into application
7
+ // programs owned by members of the Alliance, subject to a signed
8
+ // Membership Agreement and Supplemental Software License Agreement with the
9
+ // Alliance. The structure and organization of this software are the valuable
10
+ // trade secrets of the Alliance and its suppliers. The software is also
11
+ // protected by copyright law and international treaty provisions. Application
12
+ // programs incorporating this software must include the following statement
13
+ // with their copyright notices:
14
+ //
15
+ // This application incorporates Open Design Alliance software pursuant to a
16
+ // license agreement with Open Design Alliance.
17
+ // Open Design Alliance Copyright (C) 2002-2024 by Open Design Alliance.
18
+ // All rights reserved.
19
+ //
20
+ // By use of this software, its documentation or related materials, you
21
+ // acknowledge and accept the above terms.
22
+ ///////////////////////////////////////////////////////////////////////////////
23
+
24
+ import { IHttpClient } from "./IHttpClient";
25
+
26
+ /**
27
+ * Base class for the REST API endpoints.
28
+ */
29
+ export class Endpoint {
30
+ /**
31
+ * Endpoint API path relative to the REST API server URL.
32
+ */
33
+ public path: string;
34
+
35
+ /**
36
+ * Endpoint-specific HTTP headers for the `GET`, `POST`, `PUT` and `DELETE` requests. You can
37
+ * add custom headers at any time.
38
+ */
39
+ public headers: HeadersInit;
40
+
41
+ public httpClient: IHttpClient;
42
+ private _useVersion: number | undefined;
43
+
44
+ /**
45
+ * @ignore
46
+ * @param path - The API path of the endpoint relative to the REST API server URL of the
47
+ * specified HTTP client.
48
+ * @param httpClient - HTTP client instance used to send requests to the REST API server.
49
+ * @param headers - Endpoint-specific HTTP headers.
50
+ */
51
+ constructor(path: string, httpClient: IHttpClient, headers = {}) {
52
+ this.path = path;
53
+ this.httpClient = httpClient;
54
+ this.headers = headers;
55
+ }
56
+
57
+ appendVersionParam(relativePath: string): string {
58
+ if (this._useVersion === undefined) return relativePath;
59
+ const delimiter = relativePath.includes("?") ? "&" : "?";
60
+ return `${relativePath}${delimiter}version=${this._useVersion}`;
61
+ }
62
+
63
+ /**
64
+ * Returns the endpoint API path.
65
+ *
66
+ * @ignore
67
+ * @param relativePath - Nested endpoint relative path.
68
+ */
69
+ getEndpointPath(relativePath: string): string {
70
+ return this.appendVersionParam(`${this.path}${relativePath}`);
71
+ }
72
+
73
+ /**
74
+ * Sends the `GET` request to the endpoint.
75
+ *
76
+ * @ignore
77
+ * @param relativePath - Nested endpoint relative path.
78
+ * @param signal - An
79
+ * {@link https://developer.mozilla.org/docs/Web/API/AbortController | AbortController}
80
+ * signal. Allows to communicate with a fetch request and abort it if desired.
81
+ */
82
+ get(relativePath: string, signal?: AbortSignal): Promise<Response> {
83
+ return this.httpClient.get(this.getEndpointPath(relativePath), { signal, headers: this.headers });
84
+ }
85
+
86
+ /**
87
+ * Sends the `POST` request to the endpoint.
88
+ *
89
+ * @ignore
90
+ * @param relativePath - Nested endpoint relative path.
91
+ * @param body - Request body. Can be
92
+ * {@link https://developer.mozilla.org/docs/Web/API/FormData | FormData},
93
+ * {@link https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer | ArrayBuffer},
94
+ * {@link https://developer.mozilla.org/docs/Web/API/Blob/Blob | Blob}, JSON object or plain text.
95
+ */
96
+ post(relativePath: string, body?: BodyInit | object): Promise<Response> {
97
+ return this.httpClient.post(this.getEndpointPath(relativePath), body, { headers: this.headers });
98
+ }
99
+
100
+ /**
101
+ * Sends the `PUT` request to the endpoint.
102
+ *
103
+ * @ignore
104
+ * @param relativePath - Nested endpoint relative path.
105
+ * @param body - Request body. Can be
106
+ * {@link https://developer.mozilla.org/docs/Web/API/FormData | FormData},
107
+ * {@link https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer | ArrayBuffer},
108
+ * {@link https://developer.mozilla.org/docs/Web/API/Blob/Blob | Blob}, JSON object or plain text.
109
+ */
110
+ put(relativePath: string, body?: BodyInit | object): Promise<Response> {
111
+ return this.httpClient.put(this.getEndpointPath(relativePath), body, { headers: this.headers });
112
+ }
113
+
114
+ /**
115
+ * Sends the `DELETE` request to the endpoint.
116
+ *
117
+ * @ignore
118
+ * @param relativePath - Nested endpoint relative path.
119
+ */
120
+ delete(relativePath: string): Promise<Response> {
121
+ return this.httpClient.delete(this.getEndpointPath(relativePath), { headers: this.headers });
122
+ }
123
+
124
+ // Internal: append the `version` param to the endpoint requests.
125
+
126
+ useVersion(version?: number): this {
127
+ this._useVersion = version;
128
+ return this;
129
+ }
130
+ }
package/src/Api/Fetch.ts CHANGED
@@ -47,38 +47,38 @@ function handleFetchError(response: Response): Promise<Response> {
47
47
 
48
48
  export function $fetch(
49
49
  url: string,
50
- params: {
51
- method: "GET" | "POST" | "PUT" | "DELETE";
50
+ init: {
51
+ method?: "GET" | "POST" | "PUT" | "DELETE" | "HEAD";
52
52
  headers?: HeadersInit;
53
- body?: BodyInit | object;
53
+ body?: BodyInit | object | null;
54
54
  signal?: AbortSignal;
55
55
  } = { method: "GET" }
56
56
  ): Promise<Response> {
57
- const headers = { ...params.headers };
57
+ const headers = { ...init.headers };
58
58
  delete headers["Content-Type"];
59
59
 
60
+ Object.keys(headers)
61
+ .filter((x) => headers[x] === undefined)
62
+ .forEach((x) => delete headers[x]);
63
+
60
64
  let body: FormData | string | undefined = undefined;
61
- if (params.method === "POST" || params.method === "PUT") {
62
- if (params.body instanceof FormData) {
63
- body = params.body;
64
- } else if (params.body instanceof Blob) {
65
+ if (init.method === "POST" || init.method === "PUT") {
66
+ if (init.body instanceof FormData) {
67
+ body = init.body;
68
+ } else if (init.body instanceof Blob) {
65
69
  body = new FormData();
66
- body.append("file", params.body);
67
- } else if (params.body instanceof ArrayBuffer) {
70
+ body.append("file", init.body);
71
+ } else if (init.body instanceof ArrayBuffer) {
68
72
  body = new FormData();
69
- body.append("file", new Blob([params.body]));
70
- } else if (typeof params.body === "object") {
71
- body = JSON.stringify(params.body);
73
+ body.append("file", new Blob([init.body]));
74
+ } else if (typeof init.body === "object") {
75
+ body = JSON.stringify(init.body);
72
76
  headers["Content-Type"] = "application/json";
73
- } else if (typeof params.body === "string") {
74
- body = params.body;
77
+ } else if (typeof init.body === "string") {
78
+ body = init.body;
75
79
  headers["Content-Type"] = "text/plain";
76
80
  }
77
81
  }
78
82
 
79
- const init: RequestInit = { method: params.method, headers };
80
- if (body) init.body = body;
81
- if (params.signal) init.signal = params.signal;
82
-
83
- return fetch(url, init).then(handleFetchError);
83
+ return fetch(url, { ...init, headers, body }).then(handleFetchError);
84
84
  }