@api-client/core 0.4.0 → 0.4.1

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 (106) hide show
  1. package/build/browser.d.ts +5 -2
  2. package/build/browser.js +7 -2
  3. package/build/browser.js.map +1 -1
  4. package/build/index.d.ts +6 -4
  5. package/build/index.js +4 -3
  6. package/build/index.js.map +1 -1
  7. package/build/src/models/HttpHistory.d.ts +29 -0
  8. package/build/src/models/HttpHistory.js.map +1 -1
  9. package/build/src/models/RequestLog.d.ts +8 -0
  10. package/build/src/models/RequestLog.js +14 -1
  11. package/build/src/models/RequestLog.js.map +1 -1
  12. package/build/src/runtime/node/InteropInterfaces.d.ts +115 -0
  13. package/build/src/runtime/node/InteropInterfaces.js +2 -0
  14. package/build/src/runtime/node/InteropInterfaces.js.map +1 -0
  15. package/build/src/runtime/node/ProjectParallelRunner.d.ts +2 -7
  16. package/build/src/runtime/node/ProjectParallelRunner.js.map +1 -1
  17. package/build/src/runtime/node/ProjectRequestRunner.d.ts +17 -49
  18. package/build/src/runtime/node/ProjectRequestRunner.js +39 -10
  19. package/build/src/runtime/node/ProjectRequestRunner.js.map +1 -1
  20. package/build/src/runtime/node/ProjectRunner.d.ts +1 -59
  21. package/build/src/runtime/node/ProjectRunner.js.map +1 -1
  22. package/build/src/runtime/node/ProjectRunnerWorker.js.map +1 -1
  23. package/build/src/runtime/store/AuthSdk.d.ts +28 -0
  24. package/build/src/runtime/store/AuthSdk.js +123 -0
  25. package/build/src/runtime/store/AuthSdk.js.map +1 -0
  26. package/build/src/runtime/store/BackendSdk.d.ts +8 -0
  27. package/build/src/runtime/store/BackendSdk.js +25 -0
  28. package/build/src/runtime/store/BackendSdk.js.map +1 -0
  29. package/build/src/runtime/store/HistorySdk.d.ts +48 -0
  30. package/build/src/runtime/store/HistorySdk.js +142 -0
  31. package/build/src/runtime/store/HistorySdk.js.map +1 -0
  32. package/build/src/runtime/store/Http.d.ts +14 -0
  33. package/build/src/runtime/store/Http.js +4 -0
  34. package/build/src/runtime/store/Http.js.map +1 -0
  35. package/build/src/runtime/store/HttpNode.d.ts +16 -0
  36. package/build/src/runtime/store/HttpNode.js +95 -0
  37. package/build/src/runtime/store/HttpNode.js.map +1 -0
  38. package/build/src/runtime/store/HttpWeb.d.ts +15 -0
  39. package/build/src/runtime/store/HttpWeb.js +83 -0
  40. package/build/src/runtime/store/HttpWeb.js.map +1 -0
  41. package/build/src/runtime/store/ProjectsSdk.d.ts +43 -0
  42. package/build/src/runtime/store/ProjectsSdk.js +144 -0
  43. package/build/src/runtime/store/ProjectsSdk.js.map +1 -0
  44. package/build/src/runtime/store/RouteBuilder.d.ts +42 -0
  45. package/build/src/runtime/store/RouteBuilder.js +75 -0
  46. package/build/src/runtime/store/RouteBuilder.js.map +1 -0
  47. package/build/src/runtime/store/Sdk.d.ts +79 -0
  48. package/build/src/runtime/store/Sdk.js +117 -0
  49. package/build/src/runtime/store/Sdk.js.map +1 -0
  50. package/build/src/runtime/store/SdkBase.d.ts +45 -0
  51. package/build/src/runtime/store/SdkBase.js +40 -0
  52. package/build/src/runtime/store/SdkBase.js.map +1 -0
  53. package/build/src/runtime/store/SpacesSdk.d.ts +50 -0
  54. package/build/src/runtime/store/SpacesSdk.js +185 -0
  55. package/build/src/runtime/store/SpacesSdk.js.map +1 -0
  56. package/build/src/runtime/store/StoreSdkNode.d.ts +10 -0
  57. package/build/src/runtime/store/StoreSdkNode.js +11 -0
  58. package/build/src/runtime/store/StoreSdkNode.js.map +1 -0
  59. package/build/src/runtime/store/StoreSdkWeb.d.ts +10 -0
  60. package/build/src/runtime/store/StoreSdkWeb.js +11 -0
  61. package/build/src/runtime/store/StoreSdkWeb.js.map +1 -0
  62. package/build/src/runtime/store/UsersSdk.d.ts +18 -0
  63. package/build/src/runtime/store/UsersSdk.js +88 -0
  64. package/build/src/runtime/store/UsersSdk.js.map +1 -0
  65. package/build/src/runtime/store/WsClient.d.ts +24 -0
  66. package/build/src/runtime/store/WsClient.js +4 -0
  67. package/build/src/runtime/store/WsClient.js.map +1 -0
  68. package/build/src/runtime/store/WsClientNode.d.ts +24 -0
  69. package/build/src/runtime/store/WsClientNode.js +66 -0
  70. package/build/src/runtime/store/WsClientNode.js.map +1 -0
  71. package/build/src/runtime/store/WsClientWeb.d.ts +23 -0
  72. package/build/src/runtime/store/WsClientWeb.js +73 -0
  73. package/build/src/runtime/store/WsClientWeb.js.map +1 -0
  74. package/package.json +4 -6
  75. package/src/models/HttpHistory.ts +30 -0
  76. package/src/models/RequestLog.ts +17 -1
  77. package/src/runtime/node/InteropInterfaces.ts +121 -0
  78. package/src/runtime/node/ProjectParallelRunner.ts +2 -9
  79. package/src/runtime/node/ProjectRequestRunner.ts +42 -61
  80. package/src/runtime/node/ProjectRunner.ts +1 -60
  81. package/src/runtime/node/ProjectRunnerWorker.ts +2 -1
  82. package/src/runtime/store/AuthSdk.ts +126 -0
  83. package/src/runtime/store/BackendSdk.ts +25 -0
  84. package/src/runtime/store/HistorySdk.ts +157 -0
  85. package/src/runtime/store/Http.ts +18 -0
  86. package/src/runtime/store/HttpNode.ts +100 -0
  87. package/src/runtime/store/HttpWeb.ts +89 -0
  88. package/src/runtime/store/ProjectsSdk.ts +147 -0
  89. package/src/runtime/store/RouteBuilder.ts +89 -0
  90. package/src/runtime/store/Sdk.ts +131 -0
  91. package/src/runtime/store/SdkBase.ts +72 -0
  92. package/src/runtime/store/SpacesSdk.ts +190 -0
  93. package/src/runtime/store/StoreSdkNode.ts +13 -0
  94. package/src/runtime/store/StoreSdkWeb.ts +13 -0
  95. package/src/runtime/store/UsersSdk.ts +89 -0
  96. package/src/runtime/store/WsClient.ts +28 -0
  97. package/src/runtime/store/WsClientNode.ts +68 -0
  98. package/src/runtime/store/WsClientWeb.ts +75 -0
  99. package/build/src/models/HistoryIndex.d.ts +0 -45
  100. package/build/src/models/HistoryIndex.js +0 -56
  101. package/build/src/models/HistoryIndex.js.map +0 -1
  102. package/build/src/runtime/store/StoreSdk.d.ts +0 -279
  103. package/build/src/runtime/store/StoreSdk.js +0 -842
  104. package/build/src/runtime/store/StoreSdk.js.map +0 -1
  105. package/src/models/HistoryIndex.ts +0 -81
  106. package/src/runtime/store/StoreSdk.ts +0 -895
@@ -0,0 +1,147 @@
1
+ import { JsonPatch } from 'json8-patch';
2
+ import { SdkBase, E_RESPONSE_STATUS, E_RESPONSE_NO_VALUE, E_INVALID_JSON, E_RESPONSE_UNKNOWN, E_RESPONSE_LOCATION } from './SdkBase.js';
3
+ import { RouteBuilder } from './RouteBuilder.js';
4
+ import { IListOptions, IListResponse } from '../../models/Backend.js';
5
+ import { HttpProject, IHttpProject, Kind as HttpProjectKind } from '../../models/HttpProject.js';
6
+
7
+ export class ProjectsSdk extends SdkBase {
8
+ /**
9
+ * Creates a project in a user space.
10
+ *
11
+ * @param space The user space key
12
+ * @param project THe project to create
13
+ * @returns The key of the created project.
14
+ */
15
+ async create(space: string, project: IHttpProject | HttpProject): Promise<string> {
16
+ const { token } = this.sdk;
17
+ const url = this.sdk.getUrl(RouteBuilder.spaceProjects(space));
18
+ const body = JSON.stringify(project);
19
+ const result = await this.sdk.http.post(url.toString(), { token, body });
20
+ this.inspectCommonStatusCodes(result.status);
21
+ const E_PREFIX = 'Unable to create a project. ';
22
+ if (result.status !== 204) {
23
+ this.logInvalidResponse(result);
24
+ throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
25
+ }
26
+ const location = result.headers.get('location');
27
+ if (!location) {
28
+ throw new Error(`${E_PREFIX}${E_RESPONSE_LOCATION}`);
29
+ }
30
+ const id = location.split('/').pop();
31
+ return id as string;
32
+ }
33
+
34
+ /**
35
+ * Reads a project definition from the store.
36
+ * @param space The user space key
37
+ * @param project The project key
38
+ * @returns The definition of the project.
39
+ */
40
+ async read(space: string, project: string): Promise<IHttpProject> {
41
+ const { token } = this.sdk;
42
+ const url = this.sdk.getUrl(RouteBuilder.spaceProject(space, project));
43
+ const result = await this.sdk.http.get(url.toString(), { token });
44
+ this.inspectCommonStatusCodes(result.status);
45
+ const E_PREFIX = 'Unable to read a project. ';
46
+ if (result.status !== 200) {
47
+ this.logInvalidResponse(result);
48
+ throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
49
+ }
50
+ if (!result.body) {
51
+ throw new Error(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`);
52
+ }
53
+ let data: IHttpProject;
54
+ try {
55
+ data = JSON.parse(result.body);
56
+ } catch (e) {
57
+ throw new Error(`${E_PREFIX}${E_INVALID_JSON}.`);
58
+ }
59
+ if (data.kind !== HttpProjectKind) {
60
+ throw new Error(`${E_PREFIX}${E_RESPONSE_UNKNOWN}.`);
61
+ }
62
+ return data;
63
+ }
64
+
65
+ /**
66
+ * Lists projects in the space
67
+ *
68
+ * @param space The user space key
69
+ * @param options Optional query options.
70
+ */
71
+ async list(space: string, options?: IListOptions): Promise<IListResponse> {
72
+ const { token } = this.sdk;
73
+ const url = this.sdk.getUrl(RouteBuilder.spaceProjects(space));
74
+ this.sdk.appendListOptions(url, options);
75
+ const result = await this.sdk.http.get(url.toString(), { token });
76
+ this.inspectCommonStatusCodes(result.status);
77
+ const E_PREFIX = 'Unable to list projects. ';
78
+ if (result.status !== 200) {
79
+ this.logInvalidResponse(result);
80
+ throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
81
+ }
82
+ if (!result.body) {
83
+ throw new Error(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`);
84
+ }
85
+ let data: IListResponse;
86
+ try {
87
+ data = JSON.parse(result.body);
88
+ } catch (e) {
89
+ throw new Error(`${E_PREFIX}${E_INVALID_JSON}.`);
90
+ }
91
+ if (!Array.isArray(data.data)) {
92
+ throw new Error(`${E_PREFIX}${E_RESPONSE_UNKNOWN}.`);
93
+ }
94
+ return data;
95
+ }
96
+
97
+ /**
98
+ * Deletes a project form the store.
99
+ *
100
+ * @param space The key of the parent space.
101
+ * @param project The key of the project to delete.
102
+ */
103
+ async delete(space: string, project: string): Promise<void> {
104
+ const { token } = this.sdk;
105
+ const url = this.sdk.getUrl(RouteBuilder.spaceProject(space, project));
106
+ const result = await this.sdk.http.delete(url.toString(), { token });
107
+ this.inspectCommonStatusCodes(result.status);
108
+ const E_PREFIX = 'Unable to delete a project. ';
109
+ if (result.status !== 204) {
110
+ this.logInvalidResponse(result);
111
+ throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
112
+ }
113
+ }
114
+
115
+ /**
116
+ * Patches a project in the store.
117
+ * @param space The key of the parent user space
118
+ * @param project The key of project to patch.
119
+ * @param value The JSON patch to be processed.
120
+ * @returns The JSON patch to revert the change using the `json8-patch` library
121
+ */
122
+ async patch(space: string, project: string, value: JsonPatch): Promise<JsonPatch> {
123
+ const { token } = this.sdk;
124
+ const url = this.sdk.getUrl(RouteBuilder.spaceProject(space, project));
125
+ const body = JSON.stringify(value);
126
+ const result = await this.sdk.http.patch(url.toString(), { token, body });
127
+ this.inspectCommonStatusCodes(result.status);
128
+ const E_PREFIX = 'Unable to patch a project. ';
129
+ if (result.status !== 200) {
130
+ this.logInvalidResponse(result);
131
+ throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
132
+ }
133
+ if (!result.body) {
134
+ throw new Error(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`);
135
+ }
136
+ let data: any;
137
+ try {
138
+ data = JSON.parse(result.body);
139
+ } catch (e) {
140
+ throw new Error(`${E_PREFIX}${E_INVALID_JSON}.`);
141
+ }
142
+ if (!data.revert) {
143
+ throw new Error(`${E_PREFIX}${E_RESPONSE_UNKNOWN}.`);
144
+ }
145
+ return data.revert as JsonPatch;
146
+ }
147
+ }
@@ -0,0 +1,89 @@
1
+ /**
2
+ * A helper class to make sure routes user and reported by this service are consistent.
3
+ */
4
+ export class RouteBuilder {
5
+ /**
6
+ * @returns The path to the /spaces route.
7
+ */
8
+ static spaces(): string {
9
+ return '/spaces';
10
+ }
11
+
12
+ /**
13
+ * @returns The path to the /spaces/[id] route.
14
+ */
15
+ static space(key: string): string {
16
+ return `/spaces/${key}`;
17
+ }
18
+
19
+ /**
20
+ * @returns The path to the /spaces/[id]/users route.
21
+ */
22
+ static spaceUsers(key: string): string {
23
+ return `/spaces/${key}/users`;
24
+ }
25
+
26
+ /**
27
+ * @returns The path to the /spaces/[id]/projects route.
28
+ */
29
+ static spaceProjects(key: string): string {
30
+ return `/spaces/${key}/projects`;
31
+ }
32
+
33
+ /**
34
+ * @returns The path to the /spaces/[id]/projects/[id] route.
35
+ */
36
+ static spaceProject(space: string, project: string): string {
37
+ return `/spaces/${space}/projects/${project}`;
38
+ }
39
+
40
+ /**
41
+ * @returns The path to the /spaces/[id]/projects/[id]/revisions route.
42
+ */
43
+ static projectRevisions(space: string, project: string): string {
44
+ return `/spaces/${space}/projects/${project}/revisions`;
45
+ }
46
+
47
+ /**
48
+ * @returns The path to the /backend route.
49
+ */
50
+ static backend(): string {
51
+ return '/store';
52
+ }
53
+
54
+ static sessions(): string {
55
+ return '/sessions'
56
+ }
57
+
58
+ static sessionRenew(): string {
59
+ return '/sessions/renew'
60
+ }
61
+
62
+ static usersMe(): string {
63
+ return '/users/me'
64
+ }
65
+
66
+ static users(): string {
67
+ return '/users'
68
+ }
69
+
70
+ static user(key: string): string {
71
+ return `/users/${key}`
72
+ }
73
+
74
+ static history(): string {
75
+ return `/history`;
76
+ }
77
+
78
+ static historyBatchCreate(): string {
79
+ return `/history/batch/create`;
80
+ }
81
+
82
+ static historyBatchDelete(): string {
83
+ return `/history/batch/delete`;
84
+ }
85
+
86
+ static historyItem(key: string): string {
87
+ return `/history/${key}`;
88
+ }
89
+ }
@@ -0,0 +1,131 @@
1
+ import { IListOptions } from '../../models/Backend.js';
2
+ import { Http } from './Http.js';
3
+ import { WsClient } from './WsClient.js';
4
+ import { AuthSdk } from './AuthSdk.js';
5
+ import { BackendSdk } from './BackendSdk.js';
6
+ import { SpacesSdk } from './SpacesSdk.js';
7
+ import { ProjectsSdk } from './ProjectsSdk.js';
8
+ import { UsersSdk } from './UsersSdk.js';
9
+ import { HistorySdk } from './HistorySdk.js';
10
+
11
+ const baseUriSymbol = Symbol('baseUri');
12
+
13
+ /**
14
+ * NodeJS API for API Client's net-store module.
15
+ */
16
+ export abstract class Sdk {
17
+ /**
18
+ * The token to use with requests. Can be overwritten with each API call.
19
+ */
20
+ token?: string;
21
+ /**
22
+ * The user authentication features.
23
+ */
24
+ auth = new AuthSdk(this);
25
+ /**
26
+ * The store information features
27
+ */
28
+ store = new BackendSdk(this);
29
+ /**
30
+ * The user spaces features.
31
+ */
32
+ space = new SpacesSdk(this);
33
+ /**
34
+ * The user projects features.
35
+ */
36
+ project = new ProjectsSdk(this);
37
+ /**
38
+ * Reads user information.
39
+ */
40
+ user = new UsersSdk(this);
41
+ /**
42
+ * The HTTP(S) requests.
43
+ */
44
+ abstract http: Http;
45
+ /**
46
+ * The web socket requests.
47
+ */
48
+ abstract ws: WsClient;
49
+ /**
50
+ * The history data.
51
+ */
52
+ history = new HistorySdk(this);
53
+
54
+
55
+ [baseUriSymbol] = '';
56
+
57
+ /**
58
+ * @returns The base URI to the store.
59
+ */
60
+ get baseUri(): string {
61
+ return this[baseUriSymbol];
62
+ }
63
+
64
+ /**
65
+ * @param value The base URI to the store.
66
+ */
67
+ set baseUri(value: string) {
68
+ if (!value) {
69
+ throw new Error(`Invalid value for the baseUri`);
70
+ }
71
+ let url: URL;
72
+ try {
73
+ url = new URL(value);
74
+ } catch (e) {
75
+ throw new Error(`Invalid baseUri: Not an URL`);
76
+ }
77
+ this[baseUriSymbol] = url.origin;
78
+ const { pathname } = url;
79
+ if (pathname && pathname !== '/') {
80
+ this.basePath = pathname;
81
+ } else {
82
+ this.basePath = undefined;
83
+ }
84
+ }
85
+
86
+ /**
87
+ * @param baseUri The base URI to the store.
88
+ * @param basePath The base path (the router prefix) for the server.
89
+ */
90
+ constructor(baseUri: string, public basePath?: string) {
91
+ this.baseUri = baseUri;
92
+ }
93
+
94
+ /**
95
+ * Creates a full URL for a path.
96
+ * This adds the server's base path and the passed path to the configured base URL.
97
+ * @param path The path to the endpoint, without the base path.
98
+ */
99
+ getUrl(path='/'): URL {
100
+ const { baseUri, basePath='' } = this;
101
+ let userPath = path;
102
+ if (basePath && userPath.startsWith(basePath)) {
103
+ userPath = userPath.substring(basePath.length);
104
+ }
105
+ return new URL(`${basePath}${userPath}`, baseUri);
106
+ }
107
+
108
+ /**
109
+ * Appends list options to the query parameters.
110
+ * @param url The URL object to the API endpoint
111
+ * @param options The list options, if any
112
+ */
113
+ appendListOptions(url: URL, options: IListOptions = {}): void {
114
+ const { searchParams } = url;
115
+ if (options.cursor) {
116
+ searchParams.set('cursor', options.cursor);
117
+ } else {
118
+ if (typeof options.limit === 'number') {
119
+ searchParams.set('limit', String(options.limit));
120
+ }
121
+ if (options.query) {
122
+ searchParams.set('query', options.query);
123
+ }
124
+ if (Array.isArray(options.queryField)) {
125
+ options.queryField.forEach((field) => {
126
+ searchParams.append('queryField', field);
127
+ });
128
+ }
129
+ }
130
+ }
131
+ }
@@ -0,0 +1,72 @@
1
+ import { Headers } from '../../lib/headers/Headers.js';
2
+ import { Sdk } from './Sdk.js';
3
+
4
+ export interface IStoreRequestOptions {
5
+ method?: 'GET' | 'POST' | 'PATCH' | 'DELETE';
6
+ headers?: Record<string, string>,
7
+ body?: string | Buffer;
8
+ /**
9
+ * Adds the token to the headers.
10
+ */
11
+ token?: string;
12
+ }
13
+
14
+ export interface IStoreResponse {
15
+ status: number;
16
+ headers: Headers;
17
+ body?: string;
18
+ }
19
+
20
+ export interface IStoreTokenInfo {
21
+ /**
22
+ * The token to use.
23
+ */
24
+ token: string;
25
+ /**
26
+ * The timestamp when the token expires.
27
+ * Note, clients may have a different date than the store.
28
+ *
29
+ * May not be set when the token does not expire.
30
+ */
31
+ expires?: number;
32
+ }
33
+
34
+ export const E_INVALID_JSON = 'The response is not a valid JSON.';
35
+ export const E_RESPONSE_NO_VALUE = 'The response has no value.';
36
+ export const E_RESPONSE_STATUS = 'Invalid response status: ';
37
+ export const E_RESPONSE_UNKNOWN = 'The response has unknown format.';
38
+ export const E_RESPONSE_LOCATION = 'The response has no "location" header.';
39
+
40
+
41
+ export class SdkBase {
42
+ constructor(public sdk: Sdk) {}
43
+
44
+ protected logInvalidResponse(response: IStoreResponse): void {
45
+ if (response.body) {
46
+ try {
47
+ const data = JSON.parse(response.body);
48
+ if (data.message) {
49
+ console.warn(`[Store message]: ${data.message}`);
50
+ }
51
+ } catch (e) {
52
+ // .
53
+ }
54
+ }
55
+ }
56
+
57
+ /**
58
+ * Throws unified message for a common error status codes.
59
+ * It handles 404, 403, and 401 status codes.
60
+ */
61
+ protected inspectCommonStatusCodes(status: number): void {
62
+ if (status === 404) {
63
+ throw new Error(`Not found.`);
64
+ }
65
+ if (status === 403) {
66
+ throw new Error(`You have no access to this resource.`);
67
+ }
68
+ if (status === 401) {
69
+ throw new Error(`Not authorized.`);
70
+ }
71
+ }
72
+ }
@@ -0,0 +1,190 @@
1
+ import { JsonPatch } from 'json8-patch';
2
+ import { SdkBase, E_RESPONSE_STATUS, E_RESPONSE_NO_VALUE, E_INVALID_JSON, E_RESPONSE_UNKNOWN, E_RESPONSE_LOCATION } from './SdkBase.js';
3
+ import { RouteBuilder } from './RouteBuilder.js';
4
+ import { IListOptions, IListResponse } from '../../models/Backend.js';
5
+ import { IWorkspace, IUserWorkspace, Workspace, Kind as WorkspaceKind } from '../../models/Workspace.js';
6
+ import { UserAccessOperation } from '../../models/User.js';
7
+
8
+ export class SpacesSdk extends SdkBase {
9
+ /**
10
+ * Lists spaces in the store.
11
+ * @param options Optional query options.
12
+ */
13
+ async list(options?: IListOptions): Promise<IListResponse> {
14
+ const { token } = this.sdk;
15
+ const url = this.sdk.getUrl(RouteBuilder.spaces());
16
+ this.sdk.appendListOptions(url, options);
17
+ const result = await this.sdk.http.get(url.toString(), { token });
18
+ this.inspectCommonStatusCodes(result.status);
19
+ const E_PREFIX = 'Unable to list spaces. ';
20
+ if (result.status !== 200) {
21
+ this.logInvalidResponse(result);
22
+ throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
23
+ }
24
+ if (!result.body) {
25
+ throw new Error(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`);
26
+ }
27
+ let data: IListResponse;
28
+ try {
29
+ data = JSON.parse(result.body);
30
+ } catch (e) {
31
+ throw new Error(`${E_PREFIX}${E_INVALID_JSON}.`);
32
+ }
33
+ if (!Array.isArray(data.data)) {
34
+ throw new Error(`${E_PREFIX}${E_RESPONSE_UNKNOWN}.`);
35
+ }
36
+ return data;
37
+ }
38
+
39
+ /**
40
+ * Creates a workspace in the store.
41
+ * @param space The workspace definition.
42
+ * @returns The key of the creates space.
43
+ */
44
+ async create(space: IWorkspace | Workspace): Promise<string> {
45
+ const { token } = this.sdk;
46
+ const url = this.sdk.getUrl(RouteBuilder.spaces());
47
+ const body = JSON.stringify(space);
48
+ const result = await this.sdk.http.post(url.toString(), { token, body });
49
+ this.inspectCommonStatusCodes(result.status);
50
+ const E_PREFIX = 'Unable to create a user space. ';
51
+ if (result.status !== 204) {
52
+ this.logInvalidResponse(result);
53
+ throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
54
+ }
55
+ const location = result.headers.get('location');
56
+ if (!location) {
57
+ throw new Error(`${E_PREFIX}${E_RESPONSE_LOCATION}`);
58
+ }
59
+ const id = location.split('/').pop();
60
+ return id as string;
61
+ }
62
+
63
+ /**
64
+ * Reads a user space definition from the store.
65
+ * @param key The user space key
66
+ * @returns The definition of the user space.
67
+ */
68
+ async read(key: string): Promise<IUserWorkspace> {
69
+ const { token } = this.sdk;
70
+ const url = this.sdk.getUrl(RouteBuilder.space(key));
71
+ const result = await this.sdk.http.get(url.toString(), { token });
72
+ this.inspectCommonStatusCodes(result.status);
73
+ const E_PREFIX = 'Unable to read a user space. ';
74
+ if (result.status !== 200) {
75
+ this.logInvalidResponse(result);
76
+ throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
77
+ }
78
+ if (!result.body) {
79
+ throw new Error(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`);
80
+ }
81
+ let data: IUserWorkspace;
82
+ try {
83
+ data = JSON.parse(result.body);
84
+ } catch (e) {
85
+ throw new Error(`${E_PREFIX}${E_INVALID_JSON}.`);
86
+ }
87
+ if (data.kind !== WorkspaceKind) {
88
+ throw new Error(`${E_PREFIX}${E_RESPONSE_UNKNOWN}.`);
89
+ }
90
+ return data;
91
+ }
92
+
93
+ /**
94
+ * Patches a user space in the store.
95
+ * @param key The key of the user space to patch
96
+ * @param value The JSON patch to be processed.
97
+ * @returns The JSON patch to revert the change using the `json8-patch` library
98
+ */
99
+ async patch(key: string, value: JsonPatch): Promise<JsonPatch> {
100
+ const { token } = this.sdk;
101
+ const url = this.sdk.getUrl(RouteBuilder.space(key));
102
+ const body = JSON.stringify(value);
103
+ const result = await this.sdk.http.patch(url.toString(), { token, body });
104
+ this.inspectCommonStatusCodes(result.status);
105
+ const E_PREFIX = 'Unable to patch a user space. ';
106
+ if (result.status !== 200) {
107
+ this.logInvalidResponse(result);
108
+ throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
109
+ }
110
+ if (!result.body) {
111
+ throw new Error(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`);
112
+ }
113
+ let data: any;
114
+ try {
115
+ data = JSON.parse(result.body);
116
+ } catch (e) {
117
+ throw new Error(`${E_PREFIX}${E_INVALID_JSON}.`);
118
+ }
119
+ if (!data.revert) {
120
+ throw new Error(`${E_PREFIX}${E_RESPONSE_UNKNOWN}.`);
121
+ }
122
+ return data.revert as JsonPatch;
123
+ }
124
+
125
+ /**
126
+ * Deletes the space in the store.
127
+ *
128
+ * @param key The key of the space to delete.
129
+ */
130
+ async delete(key: string): Promise<void> {
131
+ const { token } = this.sdk;
132
+ const url = this.sdk.getUrl(RouteBuilder.space(key));
133
+ const result = await this.sdk.http.delete(url.toString(), { token });
134
+ this.inspectCommonStatusCodes(result.status);
135
+ const E_PREFIX = 'Unable to delete a user space. ';
136
+ if (result.status !== 204) {
137
+ this.logInvalidResponse(result);
138
+ throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
139
+ }
140
+ }
141
+
142
+ /**
143
+ * Updates the sharing options of the space.
144
+ *
145
+ * @param key The user space key
146
+ * @param value The patch operation on the space's ACL
147
+ */
148
+ async patchUsers(key: string, value: UserAccessOperation[]): Promise<void> {
149
+ const { token } = this.sdk;
150
+ const url = this.sdk.getUrl(RouteBuilder.spaceUsers(key));
151
+ const body = JSON.stringify(value);
152
+ const result = await this.sdk.http.patch(url.toString(), { token, body });
153
+ this.inspectCommonStatusCodes(result.status);
154
+ const E_PREFIX = 'Unable to patch a user space. ';
155
+ if (result.status !== 204) {
156
+ this.logInvalidResponse(result);
157
+ throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
158
+ }
159
+ }
160
+
161
+ /**
162
+ * Lists uses having access to the user space.
163
+ *
164
+ * @param key The user space key
165
+ */
166
+ async listUsers(key: string): Promise<IListResponse> {
167
+ const { token } = this.sdk;
168
+ const url = this.sdk.getUrl(RouteBuilder.spaceUsers(key));
169
+ const result = await this.sdk.http.get(url.toString(), { token });
170
+ this.inspectCommonStatusCodes(result.status);
171
+ const E_PREFIX = 'Unable to list users in the space. ';
172
+ if (result.status !== 200) {
173
+ this.logInvalidResponse(result);
174
+ throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
175
+ }
176
+ if (!result.body) {
177
+ throw new Error(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`);
178
+ }
179
+ let data: IListResponse;
180
+ try {
181
+ data = JSON.parse(result.body);
182
+ } catch (e) {
183
+ throw new Error(`${E_PREFIX}${E_INVALID_JSON}.`);
184
+ }
185
+ if (!Array.isArray(data.data)) {
186
+ throw new Error(`${E_PREFIX}${E_RESPONSE_UNKNOWN}.`);
187
+ }
188
+ return data;
189
+ }
190
+ }
@@ -0,0 +1,13 @@
1
+ import { HttpNode } from './HttpNode.js';
2
+ import { WsClientNode } from './WsClientNode.js';
3
+ import { Sdk } from './Sdk.js';
4
+ import { Http } from './Http.js';
5
+ import { WsClient } from './WsClient.js';
6
+
7
+ /**
8
+ * NodeJS API for API Client's net-store module.
9
+ */
10
+ export class StoreSdk extends Sdk {
11
+ http: Http = new HttpNode(this);
12
+ ws: WsClient = new WsClientNode(this);
13
+ }
@@ -0,0 +1,13 @@
1
+ import { HttpWeb } from './HttpWeb.js';
2
+ import { WsClientWeb } from './WsClientWeb.js';
3
+ import { Sdk } from './Sdk.js';
4
+ import { Http } from './Http.js';
5
+ import { WsClient } from './WsClient.js';
6
+
7
+ /**
8
+ * NodeJS API for API Client's net-store module.
9
+ */
10
+ export class StoreSdk extends Sdk {
11
+ http: Http = new HttpWeb(this);
12
+ ws: WsClient = new WsClientWeb(this);
13
+ }