@magda/typescript-common 2.3.3 → 3.0.0-alpha.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 (249) hide show
  1. package/dist/AspectBuilder.d.ts +1 -1
  2. package/dist/AspectBuilder.js +1 -2
  3. package/dist/AspectCreationFailure.js +3 -4
  4. package/dist/AspectCreationFailure.js.map +1 -1
  5. package/dist/AsyncPage.js +59 -79
  6. package/dist/AsyncPage.js.map +1 -1
  7. package/dist/BaseApiClient.d.ts +3 -3
  8. package/dist/BaseApiClient.js +20 -13
  9. package/dist/BaseApiClient.js.map +1 -1
  10. package/dist/ConnectionResult.d.ts +2 -2
  11. package/dist/ConnectionResult.js +17 -22
  12. package/dist/ConnectionResult.js.map +1 -1
  13. package/dist/ConnectorRecordId.d.ts +1 -1
  14. package/dist/ConnectorRecordId.js +9 -13
  15. package/dist/ConnectorRecordId.js.map +1 -1
  16. package/dist/IndexerApiClient.d.ts +1 -1
  17. package/dist/IndexerApiClient.js +13 -32
  18. package/dist/IndexerApiClient.js.map +1 -1
  19. package/dist/JsonConnector.d.ts +9 -9
  20. package/dist/JsonConnector.js +163 -206
  21. package/dist/JsonConnector.js.map +1 -1
  22. package/dist/JsonTransformer.d.ts +3 -3
  23. package/dist/JsonTransformer.js +71 -75
  24. package/dist/JsonTransformer.js.map +1 -1
  25. package/dist/OpaCompileResponseParser.d.ts +4 -4
  26. package/dist/OpaCompileResponseParser.js +279 -212
  27. package/dist/OpaCompileResponseParser.js.map +1 -1
  28. package/dist/RecordCreationFailure.d.ts +1 -1
  29. package/dist/RecordCreationFailure.js +4 -4
  30. package/dist/RecordCreationFailure.js.map +1 -1
  31. package/dist/SQLUtils.d.ts +3 -3
  32. package/dist/SQLUtils.js +139 -195
  33. package/dist/SQLUtils.js.map +1 -1
  34. package/dist/ServerError.js +2 -4
  35. package/dist/ServerError.js.map +1 -1
  36. package/dist/Try.js +20 -35
  37. package/dist/Try.js.map +1 -1
  38. package/dist/addTrailingSlash.js +1 -4
  39. package/dist/addTrailingSlash.js.map +1 -1
  40. package/dist/appendUrlSegments.js +3 -8
  41. package/dist/appendUrlSegments.js.map +1 -1
  42. package/dist/authorization-api/AccessControlError.d.ts +1 -1
  43. package/dist/authorization-api/AccessControlError.js +2 -8
  44. package/dist/authorization-api/AccessControlError.js.map +1 -1
  45. package/dist/authorization-api/ApiClient.d.ts +3 -3
  46. package/dist/authorization-api/ApiClient.js +211 -272
  47. package/dist/authorization-api/ApiClient.js.map +1 -1
  48. package/dist/authorization-api/AuthError.d.ts +1 -1
  49. package/dist/authorization-api/AuthError.js +2 -8
  50. package/dist/authorization-api/AuthError.js.map +1 -1
  51. package/dist/authorization-api/GenericError.js +2 -4
  52. package/dist/authorization-api/GenericError.js.map +1 -1
  53. package/dist/authorization-api/authMiddleware.d.ts +3 -3
  54. package/dist/authorization-api/authMiddleware.js +19 -40
  55. package/dist/authorization-api/authMiddleware.js.map +1 -1
  56. package/dist/authorization-api/constants.js +9 -12
  57. package/dist/authorization-api/constants.js.map +1 -1
  58. package/dist/authorization-api/model.d.ts +8 -8
  59. package/dist/authorization-api/model.js +1 -2
  60. package/dist/coerceJson.js +1 -3
  61. package/dist/coerceJson.js.map +1 -1
  62. package/dist/createNoCacheFetchOptions.d.ts +1 -1
  63. package/dist/createNoCacheFetchOptions.js +4 -7
  64. package/dist/createNoCacheFetchOptions.js.map +1 -1
  65. package/dist/createServiceError.d.ts +1 -1
  66. package/dist/createServiceError.js +7 -11
  67. package/dist/createServiceError.js.map +1 -1
  68. package/dist/delay.js +1 -3
  69. package/dist/delay.js.map +1 -1
  70. package/dist/express/getNoCacheHeaders.js +1 -3
  71. package/dist/express/getNoCacheHeaders.js.map +1 -1
  72. package/dist/express/setResponseNoCache.d.ts +1 -1
  73. package/dist/express/setResponseNoCache.js +3 -8
  74. package/dist/express/setResponseNoCache.js.map +1 -1
  75. package/dist/express/status.d.ts +14 -15
  76. package/dist/express/status.js +145 -148
  77. package/dist/express/status.js.map +1 -1
  78. package/dist/fetchRequest.d.ts +6 -6
  79. package/dist/fetchRequest.js +36 -51
  80. package/dist/fetchRequest.js.map +1 -1
  81. package/dist/formatServiceError.js +4 -6
  82. package/dist/formatServiceError.js.map +1 -1
  83. package/dist/generated/registry/api.d.ts +67 -55
  84. package/dist/generated/registry/api.js +557 -765
  85. package/dist/generated/registry/api.js.map +1 -1
  86. package/dist/getAbsoluteUrl.js +8 -11
  87. package/dist/getAbsoluteUrl.js.map +1 -1
  88. package/dist/getBasePathFromUrl.js +3 -9
  89. package/dist/getBasePathFromUrl.js.map +1 -1
  90. package/dist/getBoolValWithDefault.js +1 -3
  91. package/dist/getBoolValWithDefault.js.map +1 -1
  92. package/dist/getRequest.js +9 -26
  93. package/dist/getRequest.js.map +1 -1
  94. package/dist/getRequestNoCache.js +4 -21
  95. package/dist/getRequestNoCache.js.map +1 -1
  96. package/dist/getStorageUrl.js +13 -23
  97. package/dist/getStorageUrl.js.map +1 -1
  98. package/dist/getUserAgent.d.ts +2 -0
  99. package/dist/getUserAgent.js +10 -0
  100. package/dist/getUserAgent.js.map +1 -0
  101. package/dist/handleServerError.js +3 -9
  102. package/dist/handleServerError.js.map +1 -1
  103. package/dist/markdownToHtml.js +5 -11
  104. package/dist/markdownToHtml.js.map +1 -1
  105. package/dist/opa/AspectQuery.js +55 -79
  106. package/dist/opa/AspectQuery.js.map +1 -1
  107. package/dist/opa/AuthDecision.d.ts +2 -2
  108. package/dist/opa/AuthDecision.js +52 -69
  109. package/dist/opa/AuthDecision.js.map +1 -1
  110. package/dist/opa/AuthDecisionQueryClient.d.ts +2 -2
  111. package/dist/opa/AuthDecisionQueryClient.js +72 -102
  112. package/dist/opa/AuthDecisionQueryClient.js.map +1 -1
  113. package/dist/pgTypes.js +4 -9
  114. package/dist/pgTypes.js.map +1 -1
  115. package/dist/registry/AuthorizedRegistryClient.d.ts +4 -4
  116. package/dist/registry/AuthorizedRegistryClient.js +55 -95
  117. package/dist/registry/AuthorizedRegistryClient.js.map +1 -1
  118. package/dist/registry/RegistryClient.d.ts +2 -3
  119. package/dist/registry/RegistryClient.js +61 -78
  120. package/dist/registry/RegistryClient.js.map +1 -1
  121. package/dist/registry/TenantConsts.js +3 -6
  122. package/dist/registry/TenantConsts.js.map +1 -1
  123. package/dist/registry/model.js +1 -2
  124. package/dist/registry-manual/api.d.ts +1 -1
  125. package/dist/registry-manual/api.js +18 -7
  126. package/dist/registry-manual/api.js.map +1 -1
  127. package/dist/retry.d.ts +1 -1
  128. package/dist/retry.js +3 -9
  129. package/dist/retry.js.map +1 -1
  130. package/dist/retryBackoff.js +3 -9
  131. package/dist/retryBackoff.js.map +1 -1
  132. package/dist/runLater.js +1 -4
  133. package/dist/runLater.js.map +1 -1
  134. package/dist/session/GetUserId.d.ts +2 -2
  135. package/dist/session/GetUserId.js +10 -16
  136. package/dist/session/GetUserId.js.map +1 -1
  137. package/dist/session/GetUserSession.d.ts +1 -1
  138. package/dist/session/GetUserSession.js +6 -10
  139. package/dist/session/GetUserSession.js.map +1 -1
  140. package/dist/session/addJwtSecretFromEnvVar.js +1 -4
  141. package/dist/session/addJwtSecretFromEnvVar.js.map +1 -1
  142. package/dist/session/buildJwt.d.ts +1 -1
  143. package/dist/session/buildJwt.js +2 -5
  144. package/dist/session/buildJwt.js.map +1 -1
  145. package/dist/session/buildJwtFromReq.d.ts +1 -1
  146. package/dist/session/buildJwtFromReq.js +3 -8
  147. package/dist/session/buildJwtFromReq.js.map +1 -1
  148. package/dist/session/cookieUtils.d.ts +2 -13
  149. package/dist/session/cookieUtils.js +8 -9
  150. package/dist/session/cookieUtils.js.map +1 -1
  151. package/dist/session/destroySession.js +18 -33
  152. package/dist/session/destroySession.js.map +1 -1
  153. package/dist/session/getSessionId.js +5 -11
  154. package/dist/session/getSessionId.js.map +1 -1
  155. package/dist/tenant-api/AuthorizedTenantClient.d.ts +1 -1
  156. package/dist/tenant-api/AuthorizedTenantClient.js +22 -36
  157. package/dist/tenant-api/AuthorizedTenantClient.js.map +1 -1
  158. package/dist/tenant-api/Tenant.js +13 -5
  159. package/dist/tenant-api/Tenant.js.map +1 -1
  160. package/dist/test/JsonConnector.spec.js +56 -61
  161. package/dist/test/JsonConnector.spec.js.map +1 -1
  162. package/dist/test/JsonTransformer.spec.js +15 -19
  163. package/dist/test/JsonTransformer.spec.js.map +1 -1
  164. package/dist/test/arbitraries.d.ts +21 -21
  165. package/dist/test/arbitraries.js +85 -99
  166. package/dist/test/arbitraries.js.map +1 -1
  167. package/dist/test/aspect-templates/organization-details.d.ts +1 -4
  168. package/dist/test/aspect-templates/organization-details.js +1 -1
  169. package/dist/test/aspect-templates/organization-details.js.map +1 -1
  170. package/dist/test/buildApiClient.spec.js +81 -107
  171. package/dist/test/buildApiClient.spec.js.map +1 -1
  172. package/dist/test/buildAuthorizedTenantClient.spec.js +25 -40
  173. package/dist/test/buildAuthorizedTenantClient.spec.js.map +1 -1
  174. package/dist/test/connectors/MockExpressServer.js +9 -23
  175. package/dist/test/connectors/MockExpressServer.js.map +1 -1
  176. package/dist/test/connectors/MockRegistry.d.ts +1 -1
  177. package/dist/test/connectors/MockRegistry.js +11 -18
  178. package/dist/test/connectors/MockRegistry.js.map +1 -1
  179. package/dist/test/connectors/runConnectorTest.js +33 -55
  180. package/dist/test/connectors/runConnectorTest.js.map +1 -1
  181. package/dist/test/createMockAuthDecisionQueryClient.d.ts +4 -4
  182. package/dist/test/createMockAuthDecisionQueryClient.js +4 -9
  183. package/dist/test/createMockAuthDecisionQueryClient.js.map +1 -1
  184. package/dist/test/db/getTestDBConfig.js +1 -3
  185. package/dist/test/db/getTestDBConfig.js.map +1 -1
  186. package/dist/test/db/runMigrationSql.js +37 -58
  187. package/dist/test/db/runMigrationSql.js.map +1 -1
  188. package/dist/test/express/status.spec.js +81 -72
  189. package/dist/test/express/status.spec.js.map +1 -1
  190. package/dist/test/fakeArgv.js +6 -5
  191. package/dist/test/fakeArgv.js.map +1 -1
  192. package/dist/test/getBasePathFromUrl.spec.js +5 -10
  193. package/dist/test/getBasePathFromUrl.spec.js.map +1 -1
  194. package/dist/test/getStorageUrl.spec.js +22 -46
  195. package/dist/test/getStorageUrl.spec.js.map +1 -1
  196. package/dist/test/mockAuthApiHost.js +26 -28
  197. package/dist/test/mockAuthApiHost.js.map +1 -1
  198. package/dist/test/mockAuthorization.js +4 -10
  199. package/dist/test/mockAuthorization.js.map +1 -1
  200. package/dist/test/mockTenantDataStore.d.ts +1 -1
  201. package/dist/test/mockTenantDataStore.js +3 -5
  202. package/dist/test/mockTenantDataStore.js.map +1 -1
  203. package/dist/test/mockUserDataStore.d.ts +1 -1
  204. package/dist/test/mockUserDataStore.js +4 -6
  205. package/dist/test/mockUserDataStore.js.map +1 -1
  206. package/dist/test/registry/buildAuthorizedClient.spec.js +65 -87
  207. package/dist/test/registry/buildAuthorizedClient.spec.js.map +1 -1
  208. package/dist/test/session/buildJwt.spec.js +15 -19
  209. package/dist/test/session/buildJwt.spec.js.map +1 -1
  210. package/dist/test/session/buildJwtForRegistryEsriOpaGroupsAndOwnerTest.spec.js +6 -11
  211. package/dist/test/session/buildJwtForRegistryEsriOpaGroupsAndOwnerTest.spec.js.map +1 -1
  212. package/dist/test/session/buildJwtForRegistryEsriOpaGroupsTest.spec.js +6 -11
  213. package/dist/test/session/buildJwtForRegistryEsriOpaGroupsTest.spec.js.map +1 -1
  214. package/dist/test/session/buildJwtForRegistryEsriOpaOwnerTest.spec.js +6 -11
  215. package/dist/test/session/buildJwtForRegistryEsriOpaOwnerTest.spec.js.map +1 -1
  216. package/dist/test/testAsyncPage.spec.js +84 -105
  217. package/dist/test/testAsyncPage.spec.js.map +1 -1
  218. package/dist/test/testOpaCompileResponseParser.spec.js +192 -196
  219. package/dist/test/testOpaCompileResponseParser.spec.js.map +1 -1
  220. package/dist/test/util.js +1 -5
  221. package/dist/test/util.js.map +1 -1
  222. package/dist/unknown2Error.d.ts +2 -0
  223. package/dist/unknown2Error.js +3 -0
  224. package/dist/unknown2Error.js.map +1 -0
  225. package/dist/util/arrayToMaybe.d.ts +1 -1
  226. package/dist/util/arrayToMaybe.js +3 -6
  227. package/dist/util/arrayToMaybe.js.map +1 -1
  228. package/dist/util/cleanOrgTitle.js +1 -4
  229. package/dist/util/cleanOrgTitle.js.map +1 -1
  230. package/dist/util/escapeRegExp.js +1 -3
  231. package/dist/util/escapeRegExp.js.map +1 -1
  232. package/dist/util/getMinikubeIP.js +3 -9
  233. package/dist/util/getMinikubeIP.js.map +1 -1
  234. package/dist/util/isArray.js +1 -3
  235. package/dist/util/isArray.js.map +1 -1
  236. package/dist/util/isUuid.js +1 -3
  237. package/dist/util/isUuid.js.map +1 -1
  238. package/dist/util/unionToThrowable.d.ts +1 -1
  239. package/dist/util/unionToThrowable.js +3 -9
  240. package/dist/util/unionToThrowable.js.map +1 -1
  241. package/dist/utilityTypes.d.ts +1 -1
  242. package/dist/utilityTypes.js +1 -2
  243. package/package.json +45 -29
  244. package/dist/request.d.ts +0 -3
  245. package/dist/request.js +0 -19
  246. package/dist/request.js.map +0 -1
  247. package/dist/test/jsverify.d.ts +0 -2
  248. package/dist/test/jsverify.js +0 -8
  249. package/dist/test/jsverify.js.map +0 -1
@@ -1,35 +1,21 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- const isomorphic_fetch_1 = __importDefault(require("isomorphic-fetch"));
16
- const tsmonad_1 = require("tsmonad");
17
- const lodash_1 = __importDefault(require("lodash"));
18
- const buildJwt_1 = __importDefault(require("../session/buildJwt"));
19
- const addTrailingSlash_1 = __importDefault(require("../addTrailingSlash"));
20
- const urijs_1 = __importDefault(require("urijs"));
21
- const ServerError_1 = __importDefault(require("../ServerError"));
22
- const isUuid_1 = __importDefault(require("../util/isUuid"));
23
- class ApiClient {
1
+ import fetch from "cross-fetch";
2
+ import { Maybe } from "@magda/tsmonad";
3
+ import lodash from "lodash";
4
+ import buildJwt from "../session/buildJwt.js";
5
+ import addTrailingSlash from "../addTrailingSlash.js";
6
+ import urijs from "urijs";
7
+ import ServerError from "../ServerError.js";
8
+ import isUuid from "../util/isUuid.js";
9
+ export default class ApiClient {
10
+ jwt = null;
11
+ requestInitOption = null;
12
+ baseUrl = "";
24
13
  constructor(
25
14
  // e.g. http://authorization-api/v0
26
15
  baseUrl, jwtSecret = null, userId = null) {
27
- this.jwt = null;
28
- this.requestInitOption = null;
29
- this.baseUrl = "";
30
- this.baseUrl = addTrailingSlash_1.default(baseUrl);
16
+ this.baseUrl = addTrailingSlash(baseUrl);
31
17
  if (jwtSecret && userId) {
32
- this.jwt = buildJwt_1.default(jwtSecret, userId);
18
+ this.jwt = buildJwt(jwtSecret, userId);
33
19
  }
34
20
  if (this.jwt) {
35
21
  this.requestInitOption = {
@@ -41,8 +27,8 @@ class ApiClient {
41
27
  }
42
28
  getMergeRequestInitOption(extraOptions = null) {
43
29
  let defaultContentTypeCfg = {};
44
- if ((extraOptions === null || extraOptions === void 0 ? void 0 : extraOptions.body) &&
45
- (!(extraOptions === null || extraOptions === void 0 ? void 0 : extraOptions.headers) ||
30
+ if (extraOptions?.body &&
31
+ (!extraOptions?.headers ||
46
32
  (typeof extraOptions.headers === "object" &&
47
33
  Object.keys(extraOptions.headers)
48
34
  .map((key) => key.toLowerCase())
@@ -53,18 +39,16 @@ class ApiClient {
53
39
  }
54
40
  };
55
41
  }
56
- return lodash_1.default.merge({}, this.requestInitOption, extraOptions, defaultContentTypeCfg);
42
+ return lodash.merge({}, this.requestInitOption, extraOptions, defaultContentTypeCfg);
57
43
  }
58
- processJsonResponse(res) {
59
- return __awaiter(this, void 0, void 0, function* () {
60
- if (res.status >= 200 && res.status < 300) {
61
- return (yield res.json());
62
- }
63
- else {
64
- const responseText = yield res.text();
65
- throw new ServerError_1.default(`Error: ${res.statusText}. ${responseText.replace(/<(.|\n)*?>/g, "")}`, res.status);
66
- }
67
- });
44
+ async processJsonResponse(res) {
45
+ if (res.status >= 200 && res.status < 300) {
46
+ return (await res.json());
47
+ }
48
+ else {
49
+ const responseText = await res.text();
50
+ throw new ServerError(`Error: ${res.statusText}. ${responseText.replace(/<(.|\n)*?>/g, "")}`, res.status);
51
+ }
68
52
  }
69
53
  /**
70
54
  * Get the data of a user.
@@ -73,10 +57,8 @@ class ApiClient {
73
57
  * @returns {Promise<Maybe<User>>}
74
58
  * @memberof ApiClient
75
59
  */
76
- getUser(userId) {
77
- return __awaiter(this, void 0, void 0, function* () {
78
- return yield this.handleGetResult(isomorphic_fetch_1.default(`${this.baseUrl}public/users/${userId}`, this.getMergeRequestInitOption()));
79
- });
60
+ async getUser(userId) {
61
+ return await this.handleGetResult(fetch(`${this.baseUrl}public/users/${userId}`, this.getMergeRequestInitOption()));
80
62
  }
81
63
  /**
82
64
  * Lookup user by source (identity provider) & sourceId (identity ID)
@@ -86,29 +68,27 @@ class ApiClient {
86
68
  * @returns {Promise<Maybe<User>>}
87
69
  * @memberof ApiClient
88
70
  */
89
- lookupUser(source, sourceId) {
90
- return __awaiter(this, void 0, void 0, function* () {
91
- if (!source) {
92
- throw new ServerError_1.default("source cannot be empty!", 400);
93
- }
94
- if (!sourceId) {
95
- throw new ServerError_1.default("sourceId cannot be empty!", 400);
96
- }
97
- const uri = urijs_1.default(`${this.baseUrl}public/users`).search({
98
- source,
99
- sourceId,
100
- limit: 1
101
- });
102
- const res = yield isomorphic_fetch_1.default(uri.toString(), this.getMergeRequestInitOption());
103
- if (!res.ok) {
104
- throw new ServerError_1.default(yield res.text(), res.status);
105
- }
106
- const data = yield res.json();
107
- if (!(data === null || data === void 0 ? void 0 : data.length)) {
108
- return tsmonad_1.Maybe.nothing();
109
- }
110
- return tsmonad_1.Maybe.just(data[0]);
71
+ async lookupUser(source, sourceId) {
72
+ if (!source) {
73
+ throw new ServerError("source cannot be empty!", 400);
74
+ }
75
+ if (!sourceId) {
76
+ throw new ServerError("sourceId cannot be empty!", 400);
77
+ }
78
+ const uri = urijs(`${this.baseUrl}public/users`).search({
79
+ source,
80
+ sourceId,
81
+ limit: 1
111
82
  });
83
+ const res = await fetch(uri.toString(), this.getMergeRequestInitOption());
84
+ if (!res.ok) {
85
+ throw new ServerError(await res.text(), res.status);
86
+ }
87
+ const data = await res.json();
88
+ if (!data?.length) {
89
+ return Maybe.nothing();
90
+ }
91
+ return Maybe.just(data[0]);
112
92
  }
113
93
  /**
114
94
  * create a user
@@ -117,27 +97,25 @@ class ApiClient {
117
97
  * @returns {Promise<UserRecord>}
118
98
  * @memberof ApiClient
119
99
  */
120
- createUser(user) {
121
- return __awaiter(this, void 0, void 0, function* () {
122
- try {
123
- const res = yield isomorphic_fetch_1.default(`${this.baseUrl}public/users`, this.getMergeRequestInitOption({
124
- method: "POST",
125
- headers: {
126
- "Content-Type": "application/json"
127
- },
128
- body: JSON.stringify(user)
129
- }));
130
- if (res.status >= 400) {
131
- throw new Error(`Encountered error ${res.status}: ${yield res.text()} when creating new user to ${this.baseUrl}public/users`);
132
- }
133
- const resData = yield res.json();
134
- return resData;
135
- }
136
- catch (e) {
137
- console.error(e);
138
- throw e;
100
+ async createUser(user) {
101
+ try {
102
+ const res = await fetch(`${this.baseUrl}public/users`, this.getMergeRequestInitOption({
103
+ method: "POST",
104
+ headers: {
105
+ "Content-Type": "application/json"
106
+ },
107
+ body: JSON.stringify(user)
108
+ }));
109
+ if (res.status >= 400) {
110
+ throw new Error(`Encountered error ${res.status}: ${await res.text()} when creating new user to ${this.baseUrl}public/users`);
139
111
  }
140
- });
112
+ const resData = await res.json();
113
+ return resData;
114
+ }
115
+ catch (e) {
116
+ console.error(e);
117
+ throw e;
118
+ }
141
119
  }
142
120
  /**
143
121
  * Add Roles to a user.
@@ -148,17 +126,15 @@ class ApiClient {
148
126
  * @returns {Promise<string[]>}
149
127
  * @memberof ApiClient
150
128
  */
151
- addUserRoles(userId, roleIds) {
152
- return __awaiter(this, void 0, void 0, function* () {
153
- const res = yield isomorphic_fetch_1.default(`${this.baseUrl}public/users/${userId}/roles`, this.getMergeRequestInitOption({
154
- method: "POST",
155
- headers: {
156
- "Content-Type": "application/json"
157
- },
158
- body: JSON.stringify(roleIds)
159
- }));
160
- return yield this.processJsonResponse(res);
161
- });
129
+ async addUserRoles(userId, roleIds) {
130
+ const res = await fetch(`${this.baseUrl}public/users/${userId}/roles`, this.getMergeRequestInitOption({
131
+ method: "POST",
132
+ headers: {
133
+ "Content-Type": "application/json"
134
+ },
135
+ body: JSON.stringify(roleIds)
136
+ }));
137
+ return await this.processJsonResponse(res);
162
138
  }
163
139
  /**
164
140
  * Remove a list roles from a user.
@@ -168,17 +144,15 @@ class ApiClient {
168
144
  * @returns {Promise<void>}
169
145
  * @memberof ApiClient
170
146
  */
171
- deleteUserRoles(userId, roleIds) {
172
- return __awaiter(this, void 0, void 0, function* () {
173
- const res = yield isomorphic_fetch_1.default(`${this.baseUrl}public/user/${userId}/roles`, this.getMergeRequestInitOption({
174
- method: "DELETE",
175
- headers: {
176
- "Content-Type": "application/json"
177
- },
178
- body: JSON.stringify(roleIds)
179
- }));
180
- yield this.processJsonResponse(res);
181
- });
147
+ async deleteUserRoles(userId, roleIds) {
148
+ const res = await fetch(`${this.baseUrl}public/user/${userId}/roles`, this.getMergeRequestInitOption({
149
+ method: "DELETE",
150
+ headers: {
151
+ "Content-Type": "application/json"
152
+ },
153
+ body: JSON.stringify(roleIds)
154
+ }));
155
+ await this.processJsonResponse(res);
182
156
  }
183
157
  /**
184
158
  * Get all roles of a user
@@ -187,11 +161,9 @@ class ApiClient {
187
161
  * @returns {Promise<Role[]>}
188
162
  * @memberof ApiClient
189
163
  */
190
- getUserRoles(userId) {
191
- return __awaiter(this, void 0, void 0, function* () {
192
- const res = yield isomorphic_fetch_1.default(`${this.baseUrl}public/users/${userId}/roles`, this.getMergeRequestInitOption());
193
- return yield this.processJsonResponse(res);
194
- });
164
+ async getUserRoles(userId) {
165
+ const res = await fetch(`${this.baseUrl}public/users/${userId}/roles`, this.getMergeRequestInitOption());
166
+ return await this.processJsonResponse(res);
195
167
  }
196
168
  /**
197
169
  * Get all permissions of a user
@@ -200,11 +172,9 @@ class ApiClient {
200
172
  * @returns {Promise<Permission[]>}
201
173
  * @memberof ApiClient
202
174
  */
203
- getUserPermissions(userId) {
204
- return __awaiter(this, void 0, void 0, function* () {
205
- const res = yield isomorphic_fetch_1.default(`${this.baseUrl}public/users/${userId}/permissions`, this.getMergeRequestInitOption());
206
- return yield this.processJsonResponse(res);
207
- });
175
+ async getUserPermissions(userId) {
176
+ const res = await fetch(`${this.baseUrl}public/users/${userId}/permissions`, this.getMergeRequestInitOption());
177
+ return await this.processJsonResponse(res);
208
178
  }
209
179
  /**
210
180
  * Get all permissions of a role
@@ -213,11 +183,9 @@ class ApiClient {
213
183
  * @returns {Promise<Permission[]>}
214
184
  * @memberof ApiClient
215
185
  */
216
- getRolePermissions(roleId) {
217
- return __awaiter(this, void 0, void 0, function* () {
218
- const res = yield isomorphic_fetch_1.default(`${this.baseUrl}public/roles/${roleId}/permissions`, this.getMergeRequestInitOption());
219
- return yield this.processJsonResponse(res);
220
- });
186
+ async getRolePermissions(roleId) {
187
+ const res = await fetch(`${this.baseUrl}public/roles/${roleId}/permissions`, this.getMergeRequestInitOption());
188
+ return await this.processJsonResponse(res);
221
189
  }
222
190
  /**
223
191
  * List OrgUnits at certain org tree level.
@@ -229,18 +197,16 @@ class ApiClient {
229
197
  * @returns {Promise<OrgUnit[]>}
230
198
  * @memberof ApiClient
231
199
  */
232
- getOrgUnitsByLevel(orgLevel, relationshipOrgUnitId) {
233
- return __awaiter(this, void 0, void 0, function* () {
234
- const uri = urijs_1.default(`${this.baseUrl}public/orgunits/bylevel`).segmentCoded(`${orgLevel}`);
235
- const queries = {};
236
- if (relationshipOrgUnitId) {
237
- queries["relationshipOrgUnitId"] = relationshipOrgUnitId;
238
- }
239
- const res = yield isomorphic_fetch_1.default(Object.keys(queries).length
240
- ? uri.search(queries).toString()
241
- : uri.toString(), this.getMergeRequestInitOption());
242
- return yield this.processJsonResponse(res);
243
- });
200
+ async getOrgUnitsByLevel(orgLevel, relationshipOrgUnitId) {
201
+ const uri = urijs(`${this.baseUrl}public/orgunits/bylevel`).segmentCoded(`${orgLevel}`);
202
+ const queries = {};
203
+ if (relationshipOrgUnitId) {
204
+ queries["relationshipOrgUnitId"] = relationshipOrgUnitId;
205
+ }
206
+ const res = await fetch(Object.keys(queries).length
207
+ ? uri.search(queries).toString()
208
+ : uri.toString(), this.getMergeRequestInitOption());
209
+ return await this.processJsonResponse(res);
244
210
  }
245
211
  /**
246
212
  * Get orgunits by name
@@ -251,19 +217,17 @@ class ApiClient {
251
217
  * @returns {Promise<OrgUnit[]>}
252
218
  * @memberof ApiClient
253
219
  */
254
- getOrgUnitsByName(nodeName, leafNodesOnly = false, relationshipOrgUnitId) {
255
- return __awaiter(this, void 0, void 0, function* () {
256
- const uri = urijs_1.default(`${this.baseUrl}public/orgunits`);
257
- const queries = {
258
- nodeName,
259
- leafNodesOnly
260
- };
261
- if (relationshipOrgUnitId) {
262
- queries["relationshipOrgUnitId"] = relationshipOrgUnitId;
263
- }
264
- const res = yield isomorphic_fetch_1.default(uri.search(queries).toString(), this.getMergeRequestInitOption());
265
- return yield this.processJsonResponse(res);
266
- });
220
+ async getOrgUnitsByName(nodeName, leafNodesOnly = false, relationshipOrgUnitId) {
221
+ const uri = urijs(`${this.baseUrl}public/orgunits`);
222
+ const queries = {
223
+ nodeName,
224
+ leafNodesOnly
225
+ };
226
+ if (relationshipOrgUnitId) {
227
+ queries["relationshipOrgUnitId"] = relationshipOrgUnitId;
228
+ }
229
+ const res = await fetch(uri.search(queries).toString(), this.getMergeRequestInitOption());
230
+ return await this.processJsonResponse(res);
267
231
  }
268
232
  /**
269
233
  * Gets the root organisation unit (top of the tree).
@@ -271,11 +235,9 @@ class ApiClient {
271
235
  * @returns {Promise<OrgUnit>}
272
236
  * @memberof ApiClient
273
237
  */
274
- getRootOrgUnit() {
275
- return __awaiter(this, void 0, void 0, function* () {
276
- const res = yield isomorphic_fetch_1.default(`${this.baseUrl}public/orgunits/root`, this.getMergeRequestInitOption());
277
- return yield this.processJsonResponse(res);
278
- });
238
+ async getRootOrgUnit() {
239
+ const res = await fetch(`${this.baseUrl}public/orgunits/root`, this.getMergeRequestInitOption());
240
+ return await this.processJsonResponse(res);
279
241
  }
280
242
  /**
281
243
  * Gets the details of the node with its id.
@@ -284,12 +246,10 @@ class ApiClient {
284
246
  * @returns {Promise<OrgUnit>}
285
247
  * @memberof ApiClient
286
248
  */
287
- getOrgUnitById(nodeId) {
288
- return __awaiter(this, void 0, void 0, function* () {
289
- const uri = urijs_1.default(`${this.baseUrl}public/orgunits`).segmentCoded(nodeId);
290
- const res = yield isomorphic_fetch_1.default(uri.toString(), this.getMergeRequestInitOption());
291
- return yield this.processJsonResponse(res);
292
- });
249
+ async getOrgUnitById(nodeId) {
250
+ const uri = urijs(`${this.baseUrl}public/orgunits`).segmentCoded(nodeId);
251
+ const res = await fetch(uri.toString(), this.getMergeRequestInitOption());
252
+ return await this.processJsonResponse(res);
293
253
  }
294
254
  /**
295
255
  * Gets all the children immediately below the requested node. If the node doesn't exist, returns an empty list.
@@ -298,15 +258,13 @@ class ApiClient {
298
258
  * @returns {Promise<OrgUnit[]>}
299
259
  * @memberof ApiClient
300
260
  */
301
- getImmediateOrgUnitChildren(nodeId) {
302
- return __awaiter(this, void 0, void 0, function* () {
303
- const uri = urijs_1.default(`${this.baseUrl}public/orgunits`)
304
- .segmentCoded(nodeId)
305
- .segmentCoded("children")
306
- .segmentCoded("immediate");
307
- const res = yield isomorphic_fetch_1.default(uri.toString(), this.getMergeRequestInitOption());
308
- return yield this.processJsonResponse(res);
309
- });
261
+ async getImmediateOrgUnitChildren(nodeId) {
262
+ const uri = urijs(`${this.baseUrl}public/orgunits`)
263
+ .segmentCoded(nodeId)
264
+ .segmentCoded("children")
265
+ .segmentCoded("immediate");
266
+ const res = await fetch(uri.toString(), this.getMergeRequestInitOption());
267
+ return await this.processJsonResponse(res);
310
268
  }
311
269
  /**
312
270
  * Gets all the children below the requested node recursively. If node doesn't exist, returns an empty list.
@@ -315,118 +273,99 @@ class ApiClient {
315
273
  * @returns {Promise<OrgUnit[]>}
316
274
  * @memberof ApiClient
317
275
  */
318
- getAllOrgUnitChildren(nodeId) {
319
- return __awaiter(this, void 0, void 0, function* () {
320
- const uri = urijs_1.default(`${this.baseUrl}public/orgunits`)
321
- .segmentCoded(nodeId)
322
- .segmentCoded("children")
323
- .segmentCoded("all");
324
- const res = yield isomorphic_fetch_1.default(uri.toString(), this.getMergeRequestInitOption());
325
- return yield this.processJsonResponse(res);
326
- });
276
+ async getAllOrgUnitChildren(nodeId) {
277
+ const uri = urijs(`${this.baseUrl}public/orgunits`)
278
+ .segmentCoded(nodeId)
279
+ .segmentCoded("children")
280
+ .segmentCoded("all");
281
+ const res = await fetch(uri.toString(), this.getMergeRequestInitOption());
282
+ return await this.processJsonResponse(res);
327
283
  }
328
- createOrgNode(parentNodeId, node) {
329
- return __awaiter(this, void 0, void 0, function* () {
330
- const uri = urijs_1.default(`${this.baseUrl}public/orgunits`)
331
- .segmentCoded(parentNodeId)
332
- .segmentCoded("insert");
333
- const res = yield isomorphic_fetch_1.default(uri.toString(), this.getMergeRequestInitOption({
334
- method: "post",
335
- body: JSON.stringify(node)
336
- }));
337
- return yield this.processJsonResponse(res);
338
- });
284
+ async createOrgNode(parentNodeId, node) {
285
+ const uri = urijs(`${this.baseUrl}public/orgunits`)
286
+ .segmentCoded(parentNodeId)
287
+ .segmentCoded("insert");
288
+ const res = await fetch(uri.toString(), this.getMergeRequestInitOption({
289
+ method: "post",
290
+ body: JSON.stringify(node)
291
+ }));
292
+ return await this.processJsonResponse(res);
339
293
  }
340
- createRole(name, desc) {
341
- return __awaiter(this, void 0, void 0, function* () {
342
- const uri = urijs_1.default(`${this.baseUrl}public/roles`);
343
- const res = yield isomorphic_fetch_1.default(uri.toString(), this.getMergeRequestInitOption({
344
- method: "post",
345
- body: JSON.stringify({
346
- name,
347
- description: desc ? desc : ""
348
- })
349
- }));
350
- return yield this.processJsonResponse(res);
351
- });
294
+ async createRole(name, desc) {
295
+ const uri = urijs(`${this.baseUrl}public/roles`);
296
+ const res = await fetch(uri.toString(), this.getMergeRequestInitOption({
297
+ method: "post",
298
+ body: JSON.stringify({
299
+ name,
300
+ description: desc ? desc : ""
301
+ })
302
+ }));
303
+ return await this.processJsonResponse(res);
352
304
  }
353
- createRolePermission(roleId, permissionData) {
354
- return __awaiter(this, void 0, void 0, function* () {
355
- if (!isUuid_1.default(roleId)) {
356
- throw new ServerError_1.default(`roleId: ${roleId} is not a valid UUID.`);
357
- }
358
- const uri = urijs_1.default(`${this.baseUrl}public/roles/${encodeURIComponent(roleId)}/permissions`);
359
- const res = yield isomorphic_fetch_1.default(uri.toString(), this.getMergeRequestInitOption({
360
- method: "post",
361
- body: JSON.stringify(permissionData)
362
- }));
363
- return yield this.processJsonResponse(res);
364
- });
305
+ async createRolePermission(roleId, permissionData) {
306
+ if (!isUuid(roleId)) {
307
+ throw new ServerError(`roleId: ${roleId} is not a valid UUID.`);
308
+ }
309
+ const uri = urijs(`${this.baseUrl}public/roles/${encodeURIComponent(roleId)}/permissions`);
310
+ const res = await fetch(uri.toString(), this.getMergeRequestInitOption({
311
+ method: "post",
312
+ body: JSON.stringify(permissionData)
313
+ }));
314
+ return await this.processJsonResponse(res);
365
315
  }
366
- createPermission(permissionData) {
367
- return __awaiter(this, void 0, void 0, function* () {
368
- const uri = urijs_1.default(`${this.baseUrl}public/permissions`);
369
- const res = yield isomorphic_fetch_1.default(uri.toString(), this.getMergeRequestInitOption({
370
- method: "post",
371
- body: JSON.stringify(permissionData)
372
- }));
373
- return yield this.processJsonResponse(res);
374
- });
316
+ async createPermission(permissionData) {
317
+ const uri = urijs(`${this.baseUrl}public/permissions`);
318
+ const res = await fetch(uri.toString(), this.getMergeRequestInitOption({
319
+ method: "post",
320
+ body: JSON.stringify(permissionData)
321
+ }));
322
+ return await this.processJsonResponse(res);
375
323
  }
376
- updatePermission(id, permissionData) {
377
- return __awaiter(this, void 0, void 0, function* () {
378
- if (!permissionData || !Object.keys(permissionData).length) {
379
- throw new Error("Empty data supplied to update permission!");
380
- }
381
- const uri = urijs_1.default(`${this.baseUrl}public/permissions/${encodeURIComponent(id)}`);
382
- const res = yield isomorphic_fetch_1.default(uri.toString(), this.getMergeRequestInitOption({
383
- method: "put",
384
- body: JSON.stringify(permissionData)
385
- }));
386
- return yield this.processJsonResponse(res);
387
- });
324
+ async updatePermission(id, permissionData) {
325
+ if (!permissionData || !Object.keys(permissionData).length) {
326
+ throw new Error("Empty data supplied to update permission!");
327
+ }
328
+ const uri = urijs(`${this.baseUrl}public/permissions/${encodeURIComponent(id)}`);
329
+ const res = await fetch(uri.toString(), this.getMergeRequestInitOption({
330
+ method: "put",
331
+ body: JSON.stringify(permissionData)
332
+ }));
333
+ return await this.processJsonResponse(res);
388
334
  }
389
- getOperationByUri(opUri) {
390
- return __awaiter(this, void 0, void 0, function* () {
391
- const uri = urijs_1.default(`${this.baseUrl}public/operations/byUri/${opUri}`);
392
- const res = yield isomorphic_fetch_1.default(uri.toString(), this.getMergeRequestInitOption());
393
- return yield this.processJsonResponse(res);
394
- });
335
+ async getOperationByUri(opUri) {
336
+ const uri = urijs(`${this.baseUrl}public/operations/byUri/${opUri}`);
337
+ const res = await fetch(uri.toString(), this.getMergeRequestInitOption());
338
+ return await this.processJsonResponse(res);
395
339
  }
396
- getResourceByUri(resUri) {
397
- return __awaiter(this, void 0, void 0, function* () {
398
- const uri = urijs_1.default(`${this.baseUrl}public/resources/byUri/${resUri}`);
399
- const res = yield isomorphic_fetch_1.default(uri.toString(), this.getMergeRequestInitOption());
400
- return yield this.processJsonResponse(res);
401
- });
340
+ async getResourceByUri(resUri) {
341
+ const uri = urijs(`${this.baseUrl}public/resources/byUri/${resUri}`);
342
+ const res = await fetch(uri.toString(), this.getMergeRequestInitOption());
343
+ return await this.processJsonResponse(res);
402
344
  }
403
- handleGetResult(promise) {
404
- return __awaiter(this, void 0, void 0, function* () {
405
- return promise
406
- .then((res) => {
407
- if (res.status === 404) {
408
- return Promise.resolve(tsmonad_1.Maybe.nothing());
409
- }
410
- else {
411
- return res
412
- .text()
413
- .then((resText) => {
414
- try {
415
- return JSON.parse(resText);
416
- }
417
- catch (e) {
418
- throw new Error(resText);
419
- }
420
- })
421
- .then((user) => tsmonad_1.Maybe.just(user));
422
- }
423
- })
424
- .catch((e) => {
425
- console.error(e);
426
- throw e;
427
- });
345
+ async handleGetResult(promise) {
346
+ return promise
347
+ .then((res) => {
348
+ if (res.status === 404) {
349
+ return Promise.resolve(Maybe.nothing());
350
+ }
351
+ else {
352
+ return res
353
+ .text()
354
+ .then((resText) => {
355
+ try {
356
+ return JSON.parse(resText);
357
+ }
358
+ catch (e) {
359
+ throw new Error(resText);
360
+ }
361
+ })
362
+ .then((user) => Maybe.just(user));
363
+ }
364
+ })
365
+ .catch((e) => {
366
+ console.error(e);
367
+ throw e;
428
368
  });
429
369
  }
430
370
  }
431
- exports.default = ApiClient;
432
371
  //# sourceMappingURL=ApiClient.js.map