@api-client/core 0.4.0 → 0.4.3

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 (110) hide show
  1. package/build/browser.d.ts +6 -3
  2. package/build/browser.js +7 -2
  3. package/build/browser.js.map +1 -1
  4. package/build/index.d.ts +7 -5
  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/HttpProject.d.ts +37 -1
  10. package/build/src/models/HttpProject.js +49 -1
  11. package/build/src/models/HttpProject.js.map +1 -1
  12. package/build/src/models/RequestLog.d.ts +8 -0
  13. package/build/src/models/RequestLog.js +14 -1
  14. package/build/src/models/RequestLog.js.map +1 -1
  15. package/build/src/runtime/node/InteropInterfaces.d.ts +115 -0
  16. package/build/src/runtime/node/InteropInterfaces.js +2 -0
  17. package/build/src/runtime/node/InteropInterfaces.js.map +1 -0
  18. package/build/src/runtime/node/ProjectParallelRunner.d.ts +2 -7
  19. package/build/src/runtime/node/ProjectParallelRunner.js.map +1 -1
  20. package/build/src/runtime/node/ProjectRequestRunner.d.ts +17 -49
  21. package/build/src/runtime/node/ProjectRequestRunner.js +39 -10
  22. package/build/src/runtime/node/ProjectRequestRunner.js.map +1 -1
  23. package/build/src/runtime/node/ProjectRunner.d.ts +1 -59
  24. package/build/src/runtime/node/ProjectRunner.js.map +1 -1
  25. package/build/src/runtime/node/ProjectRunnerWorker.js.map +1 -1
  26. package/build/src/runtime/store/AuthSdk.d.ts +28 -0
  27. package/build/src/runtime/store/AuthSdk.js +123 -0
  28. package/build/src/runtime/store/AuthSdk.js.map +1 -0
  29. package/build/src/runtime/store/BackendSdk.d.ts +8 -0
  30. package/build/src/runtime/store/BackendSdk.js +25 -0
  31. package/build/src/runtime/store/BackendSdk.js.map +1 -0
  32. package/build/src/runtime/store/HistorySdk.d.ts +48 -0
  33. package/build/src/runtime/store/HistorySdk.js +142 -0
  34. package/build/src/runtime/store/HistorySdk.js.map +1 -0
  35. package/build/src/runtime/store/Http.d.ts +14 -0
  36. package/build/src/runtime/store/Http.js +4 -0
  37. package/build/src/runtime/store/Http.js.map +1 -0
  38. package/build/src/runtime/store/HttpNode.d.ts +16 -0
  39. package/build/src/runtime/store/HttpNode.js +95 -0
  40. package/build/src/runtime/store/HttpNode.js.map +1 -0
  41. package/build/src/runtime/store/HttpWeb.d.ts +15 -0
  42. package/build/src/runtime/store/HttpWeb.js +83 -0
  43. package/build/src/runtime/store/HttpWeb.js.map +1 -0
  44. package/build/src/runtime/store/ProjectsSdk.d.ts +43 -0
  45. package/build/src/runtime/store/ProjectsSdk.js +144 -0
  46. package/build/src/runtime/store/ProjectsSdk.js.map +1 -0
  47. package/build/src/runtime/store/RouteBuilder.d.ts +42 -0
  48. package/build/src/runtime/store/RouteBuilder.js +75 -0
  49. package/build/src/runtime/store/RouteBuilder.js.map +1 -0
  50. package/build/src/runtime/store/Sdk.d.ts +79 -0
  51. package/build/src/runtime/store/Sdk.js +117 -0
  52. package/build/src/runtime/store/Sdk.js.map +1 -0
  53. package/build/src/runtime/store/SdkBase.d.ts +45 -0
  54. package/build/src/runtime/store/SdkBase.js +40 -0
  55. package/build/src/runtime/store/SdkBase.js.map +1 -0
  56. package/build/src/runtime/store/SpacesSdk.d.ts +50 -0
  57. package/build/src/runtime/store/SpacesSdk.js +185 -0
  58. package/build/src/runtime/store/SpacesSdk.js.map +1 -0
  59. package/build/src/runtime/store/StoreSdkNode.d.ts +10 -0
  60. package/build/src/runtime/store/StoreSdkNode.js +11 -0
  61. package/build/src/runtime/store/StoreSdkNode.js.map +1 -0
  62. package/build/src/runtime/store/StoreSdkWeb.d.ts +10 -0
  63. package/build/src/runtime/store/StoreSdkWeb.js +11 -0
  64. package/build/src/runtime/store/StoreSdkWeb.js.map +1 -0
  65. package/build/src/runtime/store/UsersSdk.d.ts +18 -0
  66. package/build/src/runtime/store/UsersSdk.js +88 -0
  67. package/build/src/runtime/store/UsersSdk.js.map +1 -0
  68. package/build/src/runtime/store/WsClient.d.ts +24 -0
  69. package/build/src/runtime/store/WsClient.js +4 -0
  70. package/build/src/runtime/store/WsClient.js.map +1 -0
  71. package/build/src/runtime/store/WsClientNode.d.ts +24 -0
  72. package/build/src/runtime/store/WsClientNode.js +66 -0
  73. package/build/src/runtime/store/WsClientNode.js.map +1 -0
  74. package/build/src/runtime/store/WsClientWeb.d.ts +23 -0
  75. package/build/src/runtime/store/WsClientWeb.js +73 -0
  76. package/build/src/runtime/store/WsClientWeb.js.map +1 -0
  77. package/package.json +4 -6
  78. package/src/models/HttpHistory.ts +30 -0
  79. package/src/models/HttpProject.ts +82 -1
  80. package/src/models/RequestLog.ts +17 -1
  81. package/src/runtime/node/InteropInterfaces.ts +121 -0
  82. package/src/runtime/node/ProjectParallelRunner.ts +2 -9
  83. package/src/runtime/node/ProjectRequestRunner.ts +42 -61
  84. package/src/runtime/node/ProjectRunner.ts +1 -60
  85. package/src/runtime/node/ProjectRunnerWorker.ts +2 -1
  86. package/src/runtime/store/AuthSdk.ts +126 -0
  87. package/src/runtime/store/BackendSdk.ts +25 -0
  88. package/src/runtime/store/HistorySdk.ts +157 -0
  89. package/src/runtime/store/Http.ts +18 -0
  90. package/src/runtime/store/HttpNode.ts +100 -0
  91. package/src/runtime/store/HttpWeb.ts +89 -0
  92. package/src/runtime/store/ProjectsSdk.ts +147 -0
  93. package/src/runtime/store/RouteBuilder.ts +89 -0
  94. package/src/runtime/store/Sdk.ts +131 -0
  95. package/src/runtime/store/SdkBase.ts +72 -0
  96. package/src/runtime/store/SpacesSdk.ts +190 -0
  97. package/src/runtime/store/StoreSdkNode.ts +13 -0
  98. package/src/runtime/store/StoreSdkWeb.ts +13 -0
  99. package/src/runtime/store/UsersSdk.ts +89 -0
  100. package/src/runtime/store/WsClient.ts +28 -0
  101. package/src/runtime/store/WsClientNode.ts +68 -0
  102. package/src/runtime/store/WsClientWeb.ts +75 -0
  103. package/build/src/models/HistoryIndex.d.ts +0 -45
  104. package/build/src/models/HistoryIndex.js +0 -56
  105. package/build/src/models/HistoryIndex.js.map +0 -1
  106. package/build/src/runtime/store/StoreSdk.d.ts +0 -279
  107. package/build/src/runtime/store/StoreSdk.js +0 -842
  108. package/build/src/runtime/store/StoreSdk.js.map +0 -1
  109. package/src/models/HistoryIndex.ts +0 -81
  110. package/src/runtime/store/StoreSdk.ts +0 -895
@@ -0,0 +1,25 @@
1
+ import { SdkBase } from './SdkBase.js';
2
+ import { RouteBuilder } from './RouteBuilder.js';
3
+ import { IBackendInfo } from '../../models/Backend.js';
4
+
5
+ export class BackendSdk extends SdkBase {
6
+ /**
7
+ * @returns Client information about the store configuration.
8
+ */
9
+ async getInfo(): Promise<IBackendInfo> {
10
+ const url = this.sdk.getUrl(RouteBuilder.backend());
11
+ const result = await this.sdk.http.get(url.toString());
12
+ this.inspectCommonStatusCodes(result.status);
13
+ if (result.status !== 200) {
14
+ throw new Error(`Invalid store response. Expected 200 status and ${result.status} received.`);
15
+ }
16
+ const body = result.body as string;
17
+ let data;
18
+ try {
19
+ data = JSON.parse(body);
20
+ } catch (e) {
21
+ throw new Error(`The server returned invalid response. Unable to read store status.`);
22
+ }
23
+ return data as IBackendInfo;
24
+ }
25
+ }
@@ -0,0 +1,157 @@
1
+ import { SdkBase, E_RESPONSE_STATUS, E_RESPONSE_NO_VALUE, E_INVALID_JSON, E_RESPONSE_UNKNOWN, E_RESPONSE_LOCATION } from './SdkBase.js';
2
+ import { RouteBuilder } from './RouteBuilder.js';
3
+ import { IListResponse, HistoryListOptions } from '../../models/Backend.js';
4
+ import { IHttpHistory, IHttpHistoryBulkAdd, Kind as HttpHistoryKind } from '../../models/HttpHistory.js';
5
+
6
+ export class HistorySdk extends SdkBase {
7
+ /**
8
+ * Creates a history object.
9
+ * A history object can be created per app (type = app) or a store's space/[project/[request]].
10
+ *
11
+ * The user can always read their own history. If the history is created for a space/project then
12
+ * history records are shared as any other object in the space.
13
+ *
14
+ * Note, history objects cannot be updated. They can only be created or deleted.
15
+ *
16
+ * @param history The history to create
17
+ * @returns The key of the created history.
18
+ */
19
+ async create(history: IHttpHistory): Promise<string> {
20
+ const { token } = this.sdk;
21
+ const url = this.sdk.getUrl(RouteBuilder.history());
22
+ const body = JSON.stringify(history);
23
+ const result = await this.sdk.http.post(url.toString(), { token, body });
24
+ this.inspectCommonStatusCodes(result.status);
25
+ const E_PREFIX = 'Unable to create a history. ';
26
+ if (result.status !== 200) {
27
+ this.logInvalidResponse(result);
28
+ throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
29
+ }
30
+ const location = result.headers.get('location');
31
+ if (!location) {
32
+ throw new Error(`${E_PREFIX}${E_RESPONSE_LOCATION}`);
33
+ }
34
+ const id = location.split('/').pop();
35
+ return id as string;
36
+ }
37
+
38
+ /**
39
+ * Creates a multiple history objects in a batch operation.
40
+ *
41
+ * @param info The bulk create info object.
42
+ */
43
+ async createBulk(info: IHttpHistoryBulkAdd): Promise<string[]> {
44
+ const { token } = this.sdk;
45
+ const url = this.sdk.getUrl(RouteBuilder.historyBatchCreate());
46
+ const body = JSON.stringify(info);
47
+ const result = await this.sdk.http.post(url.toString(), { token, body });
48
+ this.inspectCommonStatusCodes(result.status);
49
+ const E_PREFIX = 'Unable to create a bulk history. ';
50
+ if (result.status !== 200) {
51
+ this.logInvalidResponse(result);
52
+ throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
53
+ }
54
+ if (!result.body) {
55
+ throw new Error(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`);
56
+ }
57
+ let data: string[];
58
+ try {
59
+ data = JSON.parse(result.body);
60
+ } catch (e) {
61
+ throw new Error(`${E_PREFIX}${E_INVALID_JSON}.`);
62
+ }
63
+ return data;
64
+ }
65
+
66
+ /**
67
+ * Lists the history.
68
+ *
69
+ * @param options Optional query options.
70
+ */
71
+ async list(options: HistoryListOptions): Promise<IListResponse> {
72
+ const { token } = this.sdk;
73
+ const url = this.sdk.getUrl(RouteBuilder.history());
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 history. ';
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 history object form the store.
99
+ *
100
+ * @param key The key returned by the store when created the history. Also available via the `key` property on the history object.
101
+ */
102
+ delete(key: string): Promise<void>;
103
+ /**
104
+ * Deletes a list of history objects in a batch operation.
105
+ *
106
+ * @param keys The keys returned by the store when created the history. Also available via the `key` property on the history object.
107
+ */
108
+ delete(keys: string[]): Promise<void>;
109
+ /**
110
+ * Deletes a history or a list of history objects from the store.
111
+ * @param key A key or a list of keys.
112
+ */
113
+ async delete(key: string | string[]): Promise<void> {
114
+ const { token } = this.sdk;
115
+ const isArray = Array.isArray(key);
116
+ const path = isArray ? RouteBuilder.historyBatchDelete() : RouteBuilder.historyItem(key);
117
+ const url = this.sdk.getUrl(path);
118
+
119
+ const result = isArray ? await this.sdk.http.post(url.toString(), { token, body: JSON.stringify(key) }) : await this.sdk.http.delete(url.toString(), { token });
120
+ this.inspectCommonStatusCodes(result.status);
121
+ const E_PREFIX = 'Unable to delete history. ';
122
+ if (result.status !== 204) {
123
+ this.logInvalidResponse(result);
124
+ throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
125
+ }
126
+ }
127
+
128
+ /**
129
+ * Reads a history definition from the store.
130
+ * @param key The history key
131
+ * @returns The history object
132
+ */
133
+ async read(key: string): Promise<IHttpHistory> {
134
+ const { token } = this.sdk;
135
+ const url = this.sdk.getUrl(RouteBuilder.historyItem(key));
136
+ const result = await this.sdk.http.get(url.toString(), { token });
137
+ this.inspectCommonStatusCodes(result.status);
138
+ const E_PREFIX = 'Unable to read a history. ';
139
+ if (result.status !== 200) {
140
+ this.logInvalidResponse(result);
141
+ throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
142
+ }
143
+ if (!result.body) {
144
+ throw new Error(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`);
145
+ }
146
+ let data: IHttpHistory;
147
+ try {
148
+ data = JSON.parse(result.body);
149
+ } catch (e) {
150
+ throw new Error(`${E_PREFIX}${E_INVALID_JSON}.`);
151
+ }
152
+ if (data.kind !== HttpHistoryKind) {
153
+ throw new Error(`${E_PREFIX}${E_RESPONSE_UNKNOWN}.`);
154
+ }
155
+ return data;
156
+ }
157
+ }
@@ -0,0 +1,18 @@
1
+ import { SdkBase, IStoreRequestOptions, IStoreResponse } from './SdkBase.js';
2
+
3
+ export abstract class Http extends SdkBase {
4
+ /**
5
+ * Performs the GET request.
6
+ *
7
+ * @param url The request URL
8
+ * @param opts The request options
9
+ * @returns The response info.
10
+ */
11
+ abstract get(url: string, opts?: IStoreRequestOptions): Promise<IStoreResponse>;
12
+
13
+ abstract post(url: string, opts?: IStoreRequestOptions): Promise<IStoreResponse>;
14
+
15
+ abstract patch(url: string, opts?: IStoreRequestOptions): Promise<IStoreResponse>;
16
+
17
+ abstract delete(url: string, opts?: IStoreRequestOptions): Promise<IStoreResponse>;
18
+ }
@@ -0,0 +1,100 @@
1
+ import http from 'http';
2
+ import https from 'https';
3
+ import { Headers } from '../../lib/headers/Headers.js';
4
+ import { Http } from './Http.js';
5
+ import { IStoreRequestOptions, IStoreResponse } from './SdkBase.js';
6
+
7
+ export class HttpNode extends Http {
8
+ /**
9
+ * Performs the GET request.
10
+ *
11
+ * @param url The request URL
12
+ * @param opts The request options
13
+ * @returns The response info.
14
+ */
15
+ async get(url: string, opts: IStoreRequestOptions = {}): Promise<IStoreResponse> {
16
+ const result = await this._get(url, opts);
17
+ if ([301, 302, 308, 307].includes(result.status)) {
18
+ const loc = result.headers.get('location');
19
+ if (!loc) {
20
+ throw new Error('Expected redirection but no "location" header.')
21
+ }
22
+ const newUrl = new URL(loc, url);
23
+ return this.get(newUrl.toString(), opts);
24
+ }
25
+ return result;
26
+ }
27
+
28
+ private _get(url: string, opts: IStoreRequestOptions = {}): Promise<IStoreResponse> {
29
+ return new Promise((resolve, reject) => {
30
+ const { method='GET', headers={}, token = this.sdk.token } = opts;
31
+ if (token) {
32
+ headers.authorization = `Bearer ${token}`;
33
+ }
34
+ const isSsl = url.startsWith('https:');
35
+ const lib = isSsl ? https : http;
36
+ // console.log(`${method} ${url}`);
37
+ const request = lib.request(url, {
38
+ method,
39
+ headers,
40
+ rejectUnauthorized: false,
41
+ });
42
+ request.on('response', (response) => {
43
+ const ro: IStoreResponse = {
44
+ status: response.statusCode as number,
45
+ headers: new Headers(response.headers),
46
+ body: '',
47
+ };
48
+ response.on('data', (chunk) => {
49
+ ro.body += chunk;
50
+ });
51
+ response.on('end', () => resolve(ro));
52
+ });
53
+ request.on('error', (error) => reject(error));
54
+ request.end();
55
+ });
56
+ }
57
+
58
+ post(url: string, opts: IStoreRequestOptions = {}): Promise<IStoreResponse> {
59
+ return new Promise((resolve, reject) => {
60
+ const { method='POST', headers={}, token = this.sdk.token } = opts;
61
+ if (token) {
62
+ headers.authorization = `Bearer ${token}`;
63
+ }
64
+ const isSsl = url.startsWith('https:');
65
+ const lib = isSsl ? https : http;
66
+ const request = lib.request(url, {
67
+ method,
68
+ headers,
69
+ });
70
+ request.on('response', (response) => {
71
+ const ro: IStoreResponse = {
72
+ status: response.statusCode as number,
73
+ headers: new Headers(response.headers),
74
+ body: '',
75
+ };
76
+ response.on('data', (chunk) => {
77
+ ro.body += chunk;
78
+ });
79
+ response.on('end', () => resolve(ro));
80
+ });
81
+ request.on('error', (error) => reject(error));
82
+ if (opts.body) {
83
+ request.write(opts.body);
84
+ }
85
+ request.end();
86
+ });
87
+ }
88
+
89
+ patch(url: string, opts: IStoreRequestOptions = {}): Promise<IStoreResponse> {
90
+ const options = { ...opts };
91
+ options.method = 'PATCH';
92
+ return this.post(url, options);
93
+ }
94
+
95
+ delete(url: string, opts: IStoreRequestOptions = {}): Promise<IStoreResponse> {
96
+ const options = { ...opts };
97
+ options.method = 'DELETE';
98
+ return this.post(url, options);
99
+ }
100
+ }
@@ -0,0 +1,89 @@
1
+ import { Http } from './Http.js';
2
+ import { IStoreRequestOptions, IStoreResponse } from './SdkBase.js';
3
+ import { Headers } from '../../lib/headers/Headers.js';
4
+
5
+ export class HttpWeb extends Http {
6
+ /**
7
+ * Performs the GET request.
8
+ *
9
+ * @param url The request URL
10
+ * @param opts The request options
11
+ * @returns The response info.
12
+ */
13
+ async get(url: string, opts: IStoreRequestOptions = {}): Promise<IStoreResponse> {
14
+ const { method='GET', headers={}, token = this.sdk.token } = opts;
15
+ if (token) {
16
+ headers.authorization = `Bearer ${token}`;
17
+ }
18
+ // eslint-disable-next-line no-undef
19
+ const init: RequestInit = {
20
+ method,
21
+ headers,
22
+ redirect: 'follow',
23
+ };
24
+ const response = await fetch(url, init);
25
+ let body: string | undefined;
26
+ try {
27
+ body = await response.text();
28
+ } catch (e) {
29
+ // ...
30
+ }
31
+
32
+ const responseHeaders = new Headers();
33
+ response.headers.forEach((value: string, key: string) => {
34
+ responseHeaders.append(key, value);
35
+ });
36
+
37
+ const result: IStoreResponse = {
38
+ status: response.status,
39
+ headers: responseHeaders,
40
+ body,
41
+ };
42
+ return result;
43
+ }
44
+
45
+ async post(url: string, opts: IStoreRequestOptions = {}): Promise<IStoreResponse> {
46
+ const { method='POST', headers={}, token = this.sdk.token } = opts;
47
+ if (token) {
48
+ headers.authorization = `Bearer ${token}`;
49
+ }
50
+ // eslint-disable-next-line no-undef
51
+ const init: RequestInit = {
52
+ method,
53
+ headers,
54
+ redirect: 'follow',
55
+ };
56
+ if (opts.body) {
57
+ init.body = opts.body.toString();
58
+ }
59
+ const response = await fetch(url, init);
60
+ let body: string | undefined;
61
+ try {
62
+ body = await response.text();
63
+ } catch (e) {
64
+ // ...
65
+ }
66
+
67
+ const responseHeaders = new Headers();
68
+ response.headers.forEach((value: string, key: string) => {
69
+ responseHeaders.append(key, value);
70
+ });
71
+
72
+ const result: IStoreResponse = {
73
+ status: response.status,
74
+ headers: responseHeaders,
75
+ body,
76
+ };
77
+ return result;
78
+ }
79
+
80
+ patch(url: string, opts: IStoreRequestOptions = {}): Promise<IStoreResponse> {
81
+ const options: IStoreRequestOptions = { ...opts, method: 'PATCH' };
82
+ return this.post(url, options);
83
+ }
84
+
85
+ delete(url: string, opts: IStoreRequestOptions = {}): Promise<IStoreResponse> {
86
+ const options: IStoreRequestOptions = { ...opts, method: 'DELETE' };
87
+ return this.post(url, options);
88
+ }
89
+ }
@@ -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
+ }