@api-client/core 0.3.12 → 0.4.2

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 (347) hide show
  1. package/build/browser.d.ts +10 -8
  2. package/build/browser.js +11 -7
  3. package/build/browser.js.map +1 -1
  4. package/build/index.d.ts +12 -11
  5. package/build/index.js +9 -9
  6. package/build/index.js.map +1 -1
  7. package/build/src/data/JmesparthReader.d.ts +1 -1
  8. package/build/src/data/JmesparthReader.js +1 -1
  9. package/build/src/events/models/ProjectEvents.d.ts +1 -1
  10. package/build/src/events/models/ProjectEvents.js +1 -2
  11. package/build/src/events/models/ProjectEvents.js.map +1 -1
  12. package/build/src/lib/transformers/PayloadSerializer.d.ts +2 -2
  13. package/build/src/lib/transformers/PayloadSerializer.js +3 -3
  14. package/build/src/lib/transformers/PayloadSerializer.js.map +1 -1
  15. package/build/src/mocking/ProjectMock.d.ts +1 -1
  16. package/build/src/mocking/lib/Request.d.ts +2 -2
  17. package/build/src/mocking/lib/Request.js +3 -2
  18. package/build/src/mocking/lib/Request.js.map +1 -1
  19. package/build/src/mocking/lib/Response.d.ts +6 -6
  20. package/build/src/mocking/lib/Response.js +6 -6
  21. package/build/src/mocking/lib/Response.js.map +1 -1
  22. package/build/src/mocking/lib/User.js +3 -0
  23. package/build/src/mocking/lib/User.js.map +1 -1
  24. package/build/src/models/AuthorizationData.d.ts +2 -2
  25. package/build/src/models/AuthorizationData.js +2 -2
  26. package/build/src/models/ClientCertificate.d.ts +1 -1
  27. package/build/src/models/Environment.d.ts +3 -3
  28. package/build/src/models/Environment.js +3 -3
  29. package/build/src/models/Environment.js.map +1 -1
  30. package/build/src/models/ErrorResponse.d.ts +1 -1
  31. package/build/src/models/ErrorResponse.js +1 -1
  32. package/build/src/models/HostRule.d.ts +4 -4
  33. package/build/src/models/HostRule.js +4 -4
  34. package/build/src/models/HostRule.js.map +1 -1
  35. package/build/src/models/HttpHistory.d.ts +30 -1
  36. package/build/src/models/HttpHistory.js +1 -1
  37. package/build/src/models/HttpHistory.js.map +1 -1
  38. package/build/src/models/HttpProject.d.ts +41 -6
  39. package/build/src/models/HttpProject.js +52 -4
  40. package/build/src/models/HttpProject.js.map +1 -1
  41. package/build/src/models/HttpProjectListItem.d.ts +1 -1
  42. package/build/src/models/HttpProjectListItem.js +1 -1
  43. package/build/src/models/HttpProjectListItem.js.map +1 -1
  44. package/build/src/models/HttpRequest.d.ts +2 -2
  45. package/build/src/models/HttpRequest.js +2 -2
  46. package/build/src/models/HttpRequest.js.map +1 -1
  47. package/build/src/models/HttpResponse.d.ts +2 -2
  48. package/build/src/models/HttpResponse.js +3 -3
  49. package/build/src/models/HttpResponse.js.map +1 -1
  50. package/build/src/models/License.d.ts +3 -3
  51. package/build/src/models/License.js +3 -3
  52. package/build/src/models/License.js.map +1 -1
  53. package/build/src/models/ProjectFolder.d.ts +2 -2
  54. package/build/src/models/ProjectFolder.js +3 -3
  55. package/build/src/models/ProjectFolder.js.map +1 -1
  56. package/build/src/models/ProjectItem.d.ts +1 -1
  57. package/build/src/models/ProjectItem.js +2 -2
  58. package/build/src/models/ProjectItem.js.map +1 -1
  59. package/build/src/models/ProjectRequest.d.ts +2 -2
  60. package/build/src/models/ProjectRequest.js +1 -1
  61. package/build/src/models/ProjectRequest.js.map +1 -1
  62. package/build/src/models/ProjectSchema.d.ts +2 -2
  63. package/build/src/models/ProjectSchema.js +1 -1
  64. package/build/src/models/ProjectSchema.js.map +1 -1
  65. package/build/src/models/Property.d.ts +4 -4
  66. package/build/src/models/Property.js +4 -4
  67. package/build/src/models/Property.js.map +1 -1
  68. package/build/src/models/Provider.d.ts +3 -3
  69. package/build/src/models/Provider.js +3 -3
  70. package/build/src/models/Provider.js.map +1 -1
  71. package/build/src/models/Request.d.ts +2 -2
  72. package/build/src/models/Request.js +3 -3
  73. package/build/src/models/Request.js.map +1 -1
  74. package/build/src/models/RequestActions.d.ts +2 -4
  75. package/build/src/models/RequestActions.js +2 -4
  76. package/build/src/models/RequestActions.js.map +1 -1
  77. package/build/src/models/RequestAuthorization.d.ts +3 -5
  78. package/build/src/models/RequestAuthorization.js +2 -4
  79. package/build/src/models/RequestAuthorization.js.map +1 -1
  80. package/build/src/models/RequestConfig.d.ts +3 -5
  81. package/build/src/models/RequestConfig.js +1 -3
  82. package/build/src/models/RequestConfig.js.map +1 -1
  83. package/build/src/models/RequestLog.d.ts +14 -8
  84. package/build/src/models/RequestLog.js +18 -7
  85. package/build/src/models/RequestLog.js.map +1 -1
  86. package/build/src/models/RequestTime.d.ts +4 -6
  87. package/build/src/models/RequestTime.js +2 -4
  88. package/build/src/models/RequestTime.js.map +1 -1
  89. package/build/src/models/RequestUiMeta.d.ts +5 -5
  90. package/build/src/models/RequestUiMeta.js +2 -2
  91. package/build/src/models/RequestUiMeta.js.map +1 -1
  92. package/build/src/models/RequestsSize.d.ts +1 -3
  93. package/build/src/models/RequestsSize.js +1 -3
  94. package/build/src/models/RequestsSize.js.map +1 -1
  95. package/build/src/models/{ArcResponse.d.ts → Response.d.ts} +7 -9
  96. package/build/src/models/{ArcResponse.js → Response.js} +4 -6
  97. package/build/src/models/Response.js.map +1 -0
  98. package/build/src/models/ResponseAuthorization.d.ts +3 -3
  99. package/build/src/models/ResponseAuthorization.js +3 -3
  100. package/build/src/models/ResponseAuthorization.js.map +1 -1
  101. package/build/src/models/ResponseRedirect.d.ts +3 -5
  102. package/build/src/models/ResponseRedirect.js +1 -3
  103. package/build/src/models/ResponseRedirect.js.map +1 -1
  104. package/build/src/models/RevisionInfo.d.ts +1 -1
  105. package/build/src/models/RevisionInfo.js +1 -1
  106. package/build/src/models/RevisionInfo.js.map +1 -1
  107. package/build/src/models/SentRequest.d.ts +2 -4
  108. package/build/src/models/SentRequest.js +1 -3
  109. package/build/src/models/SentRequest.js.map +1 -1
  110. package/build/src/models/Server.d.ts +3 -3
  111. package/build/src/models/Server.js +3 -3
  112. package/build/src/models/Server.js.map +1 -1
  113. package/build/src/models/Thing.d.ts +3 -3
  114. package/build/src/models/Thing.js +3 -3
  115. package/build/src/models/Thing.js.map +1 -1
  116. package/build/src/models/Url.d.ts +2 -4
  117. package/build/src/models/Url.js +2 -4
  118. package/build/src/models/Url.js.map +1 -1
  119. package/build/src/models/User.d.ts +2 -1
  120. package/build/src/models/User.js +1 -1
  121. package/build/src/models/User.js.map +1 -1
  122. package/build/src/models/WebApi.d.ts +1 -1
  123. package/build/src/models/WebApi.js +1 -1
  124. package/build/src/models/WebApi.js.map +1 -1
  125. package/build/src/models/WebApiIndex.d.ts +1 -1
  126. package/build/src/models/WebApiIndex.js +1 -1
  127. package/build/src/models/WebApiIndex.js.map +1 -1
  128. package/build/src/models/Workspace.d.ts +4 -4
  129. package/build/src/models/Workspace.js +4 -4
  130. package/build/src/models/Workspace.js.map +1 -1
  131. package/build/src/models/actions/Action.d.ts +2 -2
  132. package/build/src/models/actions/Action.js +1 -1
  133. package/build/src/models/actions/Action.js.map +1 -1
  134. package/build/src/models/actions/Condition.d.ts +2 -2
  135. package/build/src/models/actions/Condition.js +1 -1
  136. package/build/src/models/actions/Condition.js.map +1 -1
  137. package/build/src/models/actions/RunnableAction.d.ts +1 -1
  138. package/build/src/models/actions/RunnableAction.js +1 -1
  139. package/build/src/models/actions/RunnableAction.js.map +1 -1
  140. package/build/src/models/actions/runnable/DeleteCookieAction.d.ts +2 -2
  141. package/build/src/models/actions/runnable/DeleteCookieAction.js +1 -1
  142. package/build/src/models/actions/runnable/DeleteCookieAction.js.map +1 -1
  143. package/build/src/models/actions/runnable/SetCookieAction.d.ts +2 -2
  144. package/build/src/models/actions/runnable/SetCookieAction.js +1 -1
  145. package/build/src/models/actions/runnable/SetCookieAction.js.map +1 -1
  146. package/build/src/models/actions/runnable/SetVariableAction.d.ts +2 -2
  147. package/build/src/models/actions/runnable/SetVariableAction.js +1 -1
  148. package/build/src/models/actions/runnable/SetVariableAction.js.map +1 -1
  149. package/build/src/models/transformers/ImportUtils.d.ts +1 -1
  150. package/build/src/models/transformers/ImportUtils.js +1 -1
  151. package/build/src/models/transformers/PostmanBackupTransformer.js +2 -1
  152. package/build/src/models/transformers/PostmanBackupTransformer.js.map +1 -1
  153. package/build/src/models/transformers/PostmanV21Transformer.js +2 -1
  154. package/build/src/models/transformers/PostmanV21Transformer.js.map +1 -1
  155. package/build/src/models/transformers/PostmanV2Transformer.js +2 -1
  156. package/build/src/models/transformers/PostmanV2Transformer.js.map +1 -1
  157. package/build/src/runtime/actions/RunnableCondition.d.ts +6 -7
  158. package/build/src/runtime/actions/RunnableCondition.js +4 -5
  159. package/build/src/runtime/actions/RunnableCondition.js.map +1 -1
  160. package/build/src/runtime/http-engine/{ArcEngine.d.ts → CoreEngine.d.ts} +3 -3
  161. package/build/src/runtime/http-engine/{ArcEngine.js → CoreEngine.js} +8 -8
  162. package/build/src/runtime/http-engine/CoreEngine.js.map +1 -0
  163. package/build/src/runtime/http-engine/HttpEngine.d.ts +3 -3
  164. package/build/src/runtime/http-engine/HttpEngine.js +3 -20
  165. package/build/src/runtime/http-engine/HttpEngine.js.map +1 -1
  166. package/build/src/runtime/modules/BasicAuthCache.js +2 -5
  167. package/build/src/runtime/modules/BasicAuthCache.js.map +1 -1
  168. package/build/src/runtime/modules/RequestCookies.d.ts +1 -1
  169. package/build/src/runtime/modules/RequestCookies.js +3 -2
  170. package/build/src/runtime/modules/RequestCookies.js.map +1 -1
  171. package/build/src/runtime/node/InteropInterfaces.d.ts +115 -0
  172. package/build/src/runtime/node/InteropInterfaces.js +2 -0
  173. package/build/src/runtime/node/InteropInterfaces.js.map +1 -0
  174. package/build/src/runtime/node/ProjectParallelRunner.d.ts +2 -7
  175. package/build/src/runtime/node/ProjectParallelRunner.js.map +1 -1
  176. package/build/src/runtime/node/ProjectRequestRunner.d.ts +17 -49
  177. package/build/src/runtime/node/ProjectRequestRunner.js +39 -10
  178. package/build/src/runtime/node/ProjectRequestRunner.js.map +1 -1
  179. package/build/src/runtime/node/ProjectRunner.d.ts +1 -59
  180. package/build/src/runtime/node/ProjectRunner.js.map +1 -1
  181. package/build/src/runtime/node/ProjectRunnerWorker.js.map +1 -1
  182. package/build/src/runtime/node/RequestFactory.d.ts +1 -1
  183. package/build/src/runtime/node/RequestFactory.js +3 -3
  184. package/build/src/runtime/node/RequestFactory.js.map +1 -1
  185. package/build/src/runtime/reporters/ProjectRunCliReporter.js.map +1 -1
  186. package/build/src/runtime/reporters/Reporter.js.map +1 -1
  187. package/build/src/runtime/store/AuthSdk.d.ts +28 -0
  188. package/build/src/runtime/store/AuthSdk.js +123 -0
  189. package/build/src/runtime/store/AuthSdk.js.map +1 -0
  190. package/build/src/runtime/store/BackendSdk.d.ts +8 -0
  191. package/build/src/runtime/store/BackendSdk.js +25 -0
  192. package/build/src/runtime/store/BackendSdk.js.map +1 -0
  193. package/build/src/runtime/store/HistorySdk.d.ts +48 -0
  194. package/build/src/runtime/store/HistorySdk.js +142 -0
  195. package/build/src/runtime/store/HistorySdk.js.map +1 -0
  196. package/build/src/runtime/store/Http.d.ts +14 -0
  197. package/build/src/runtime/store/Http.js +4 -0
  198. package/build/src/runtime/store/Http.js.map +1 -0
  199. package/build/src/runtime/store/HttpNode.d.ts +16 -0
  200. package/build/src/runtime/store/HttpNode.js +95 -0
  201. package/build/src/runtime/store/HttpNode.js.map +1 -0
  202. package/build/src/runtime/store/HttpWeb.d.ts +15 -0
  203. package/build/src/runtime/store/HttpWeb.js +83 -0
  204. package/build/src/runtime/store/HttpWeb.js.map +1 -0
  205. package/build/src/runtime/store/ProjectsSdk.d.ts +43 -0
  206. package/build/src/runtime/store/ProjectsSdk.js +144 -0
  207. package/build/src/runtime/store/ProjectsSdk.js.map +1 -0
  208. package/build/src/runtime/store/RouteBuilder.d.ts +42 -0
  209. package/build/src/runtime/store/RouteBuilder.js +75 -0
  210. package/build/src/runtime/store/RouteBuilder.js.map +1 -0
  211. package/build/src/runtime/store/Sdk.d.ts +79 -0
  212. package/build/src/runtime/store/Sdk.js +117 -0
  213. package/build/src/runtime/store/Sdk.js.map +1 -0
  214. package/build/src/runtime/store/SdkBase.d.ts +45 -0
  215. package/build/src/runtime/store/SdkBase.js +40 -0
  216. package/build/src/runtime/store/SdkBase.js.map +1 -0
  217. package/build/src/runtime/store/SpacesSdk.d.ts +50 -0
  218. package/build/src/runtime/store/SpacesSdk.js +185 -0
  219. package/build/src/runtime/store/SpacesSdk.js.map +1 -0
  220. package/build/src/runtime/store/StoreSdkNode.d.ts +10 -0
  221. package/build/src/runtime/store/StoreSdkNode.js +11 -0
  222. package/build/src/runtime/store/StoreSdkNode.js.map +1 -0
  223. package/build/src/runtime/store/StoreSdkWeb.d.ts +10 -0
  224. package/build/src/runtime/store/StoreSdkWeb.js +11 -0
  225. package/build/src/runtime/store/StoreSdkWeb.js.map +1 -0
  226. package/build/src/runtime/store/UsersSdk.d.ts +18 -0
  227. package/build/src/runtime/store/UsersSdk.js +88 -0
  228. package/build/src/runtime/store/UsersSdk.js.map +1 -0
  229. package/build/src/runtime/store/WsClient.d.ts +24 -0
  230. package/build/src/runtime/store/WsClient.js +4 -0
  231. package/build/src/runtime/store/WsClient.js.map +1 -0
  232. package/build/src/runtime/store/WsClientNode.d.ts +24 -0
  233. package/build/src/runtime/store/WsClientNode.js +66 -0
  234. package/build/src/runtime/store/WsClientNode.js.map +1 -0
  235. package/build/src/runtime/store/WsClientWeb.d.ts +23 -0
  236. package/build/src/runtime/store/WsClientWeb.js +73 -0
  237. package/build/src/runtime/store/WsClientWeb.js.map +1 -0
  238. package/package.json +6 -8
  239. package/readme.md +5 -5
  240. package/src/data/JmesparthReader.ts +1 -1
  241. package/src/events/models/ProjectEvents.ts +2 -2
  242. package/src/events/readme.md +1 -1
  243. package/src/lib/transformers/PayloadSerializer.ts +3 -3
  244. package/src/mocking/ProjectMock.ts +1 -1
  245. package/src/mocking/lib/Request.ts +5 -5
  246. package/src/mocking/lib/Response.ts +10 -10
  247. package/src/mocking/lib/User.ts +2 -1
  248. package/src/models/AuthorizationData.ts +2 -2
  249. package/src/models/ClientCertificate.ts +1 -1
  250. package/src/models/Environment.ts +4 -4
  251. package/src/models/ErrorResponse.ts +1 -1
  252. package/src/models/HostRule.ts +5 -5
  253. package/src/models/HttpHistory.ts +31 -1
  254. package/src/models/HttpProject.ts +87 -7
  255. package/src/models/HttpProjectListItem.ts +1 -1
  256. package/src/models/HttpRequest.ts +2 -2
  257. package/src/models/HttpResponse.ts +3 -3
  258. package/src/models/License.ts +4 -4
  259. package/src/models/ProjectFolder.ts +3 -3
  260. package/src/models/ProjectItem.ts +2 -2
  261. package/src/models/ProjectRequest.ts +2 -2
  262. package/src/models/ProjectSchema.ts +2 -2
  263. package/src/models/Property.ts +5 -5
  264. package/src/models/Provider.ts +4 -4
  265. package/src/models/README.md +2 -2
  266. package/src/models/Request.ts +4 -4
  267. package/src/models/RequestActions.ts +2 -4
  268. package/src/models/RequestAuthorization.ts +3 -5
  269. package/src/models/RequestConfig.ts +3 -5
  270. package/src/models/RequestLog.ts +25 -11
  271. package/src/models/RequestTime.ts +4 -6
  272. package/src/models/RequestUiMeta.ts +5 -5
  273. package/src/models/RequestsSize.ts +1 -3
  274. package/src/models/{ArcResponse.ts → Response.ts} +12 -14
  275. package/src/models/ResponseAuthorization.ts +4 -4
  276. package/src/models/ResponseRedirect.ts +3 -5
  277. package/src/models/RevisionInfo.ts +1 -1
  278. package/src/models/SentRequest.ts +2 -4
  279. package/src/models/Server.ts +4 -4
  280. package/src/models/Thing.ts +4 -4
  281. package/src/models/Url.ts +2 -4
  282. package/src/models/User.ts +2 -1
  283. package/src/models/WebApi.ts +1 -1
  284. package/src/models/WebApiIndex.ts +1 -1
  285. package/src/models/Workspace.ts +5 -5
  286. package/src/models/actions/Action.ts +2 -2
  287. package/src/models/actions/Condition.ts +2 -2
  288. package/src/models/actions/RunnableAction.ts +1 -1
  289. package/src/models/actions/runnable/DeleteCookieAction.ts +2 -2
  290. package/src/models/actions/runnable/SetCookieAction.ts +2 -2
  291. package/src/models/actions/runnable/SetVariableAction.ts +2 -2
  292. package/src/models/transformers/ImportUtils.ts +1 -1
  293. package/src/models/transformers/PostmanBackupTransformer.ts +2 -1
  294. package/src/models/transformers/PostmanV21Transformer.ts +2 -1
  295. package/src/models/transformers/PostmanV2Transformer.ts +2 -1
  296. package/src/runtime/actions/RunnableCondition.ts +6 -7
  297. package/src/runtime/http-engine/{ArcEngine.ts → CoreEngine.ts} +7 -7
  298. package/src/runtime/http-engine/HttpEngine.ts +5 -24
  299. package/src/runtime/modules/BasicAuthCache.ts +2 -6
  300. package/src/runtime/modules/RequestCookies.ts +6 -5
  301. package/src/runtime/node/InteropInterfaces.ts +121 -0
  302. package/src/runtime/node/ProjectParallelRunner.ts +2 -9
  303. package/src/runtime/node/ProjectRequestRunner.ts +42 -61
  304. package/src/runtime/node/ProjectRunner.ts +1 -60
  305. package/src/runtime/node/ProjectRunnerWorker.ts +2 -1
  306. package/src/runtime/node/RequestFactory.ts +3 -3
  307. package/src/runtime/reporters/ProjectRunCliReporter.ts +2 -2
  308. package/src/runtime/reporters/Reporter.ts +4 -4
  309. package/src/runtime/store/AuthSdk.ts +126 -0
  310. package/src/runtime/store/BackendSdk.ts +25 -0
  311. package/src/runtime/store/HistorySdk.ts +157 -0
  312. package/src/runtime/store/Http.ts +18 -0
  313. package/src/runtime/store/HttpNode.ts +100 -0
  314. package/src/runtime/store/HttpWeb.ts +89 -0
  315. package/src/runtime/store/ProjectsSdk.ts +147 -0
  316. package/src/runtime/store/RouteBuilder.ts +89 -0
  317. package/src/runtime/store/Sdk.ts +131 -0
  318. package/src/runtime/store/SdkBase.ts +72 -0
  319. package/src/runtime/store/SpacesSdk.ts +190 -0
  320. package/src/runtime/store/StoreSdkNode.ts +13 -0
  321. package/src/runtime/store/StoreSdkWeb.ts +13 -0
  322. package/src/runtime/store/UsersSdk.ts +89 -0
  323. package/src/runtime/store/WsClient.ts +28 -0
  324. package/src/runtime/store/WsClientNode.ts +68 -0
  325. package/src/runtime/store/WsClientWeb.ts +75 -0
  326. package/build/src/models/ArcResponse.js.map +0 -1
  327. package/build/src/models/HistoryIndex.d.ts +0 -43
  328. package/build/src/models/HistoryIndex.js +0 -53
  329. package/build/src/models/HistoryIndex.js.map +0 -1
  330. package/build/src/models/HistoryRequest.d.ts +0 -13
  331. package/build/src/models/HistoryRequest.js +0 -25
  332. package/build/src/models/HistoryRequest.js.map +0 -1
  333. package/build/src/runtime/http-engine/ArcEngine.js.map +0 -1
  334. package/build/src/runtime/http-engine/NodeEngine.d.ts +0 -154
  335. package/build/src/runtime/http-engine/NodeEngine.js +0 -766
  336. package/build/src/runtime/http-engine/NodeEngine.js.map +0 -1
  337. package/build/src/runtime/http-engine/NodeEngineDirect.d.ts +0 -113
  338. package/build/src/runtime/http-engine/NodeEngineDirect.js +0 -449
  339. package/build/src/runtime/http-engine/NodeEngineDirect.js.map +0 -1
  340. package/build/src/runtime/store/StoreSdk.d.ts +0 -279
  341. package/build/src/runtime/store/StoreSdk.js +0 -840
  342. package/build/src/runtime/store/StoreSdk.js.map +0 -1
  343. package/src/models/HistoryIndex.ts +0 -76
  344. package/src/models/HistoryRequest.ts +0 -28
  345. package/src/runtime/http-engine/NodeEngine.ts +0 -792
  346. package/src/runtime/http-engine/NodeEngineDirect.ts +0 -482
  347. package/src/runtime/store/StoreSdk.ts +0 -895
@@ -1,895 +0,0 @@
1
- import http from 'http';
2
- import https from 'https';
3
- import WebSocket from 'ws';
4
- import { URL } from 'url';
5
- import { JsonPatch } from 'json8-patch';
6
- import { IBackendInfo, IListOptions, IListResponse } from '../../models/Backend.js';
7
- import { IWorkspace, IUserWorkspace, Workspace } from '../../models/Workspace.js';
8
- import { UserAccessOperation, IUser } from '../../models/User.js';
9
- import { HttpProject, IHttpProject } from '../../models/HttpProject.js';
10
-
11
- export interface IStoreRequestOptions {
12
- method?: 'GET' | 'POST' | 'PATCH' | 'DELETE';
13
- headers?: Record<string, string>,
14
- body?: string | Buffer;
15
- /**
16
- * Adds the token to the headers.
17
- */
18
- token?: string;
19
- }
20
-
21
- export interface IStoreResponse {
22
- status: number;
23
- headers: http.IncomingHttpHeaders;
24
- body?: string;
25
- }
26
-
27
- export interface IStoreTokenInfo {
28
- /**
29
- * The token to use.
30
- */
31
- token: string;
32
- /**
33
- * The timestamp when the token expires.
34
- * Note, clients may have a different date than the store.
35
- *
36
- * May not be set when the token does not expire.
37
- */
38
- expires?: number;
39
- }
40
-
41
- const E_INVALID_JSON = 'The response is not a valid JSON.';
42
- const E_RESPONSE_NO_VALUE = 'The response has no value.';
43
- const E_RESPONSE_STATUS = 'Invalid response status: ';
44
- const E_RESPONSE_UNKNOWN = 'The response has unknown format.';
45
- const E_RESPONSE_LOCATION = 'The response has no "location" header.';
46
-
47
- const baseUriSymbol = Symbol('baseUri');
48
-
49
- /**
50
- * NodeJS API for API Client's net-store module.
51
- */
52
- export class StoreSdk {
53
- /**
54
- * The token to use with requests. Can be overwritten with each API call.
55
- */
56
- token?: string;
57
- /**
58
- * The user authentication features.
59
- */
60
- auth = new AuthSdk(this);
61
- /**
62
- * The store information features
63
- */
64
- store = new BackendSdk(this);
65
- /**
66
- * The user spaces features.
67
- */
68
- space = new SpacesSdk(this);
69
- /**
70
- * The user projects features.
71
- */
72
- project = new ProjectsSdk(this);
73
- /**
74
- * Reads user information.
75
- */
76
- user = new UsersSdk(this);
77
- /**
78
- * The HTTP(S) requests.
79
- */
80
- http = new HttpClient(this);
81
- /**
82
- * The web socket requests.
83
- */
84
- ws = new WsClient(this);
85
-
86
-
87
- [baseUriSymbol] = '';
88
-
89
- /**
90
- * @returns The base URI to the store.
91
- */
92
- get baseUri(): string {
93
- return this[baseUriSymbol];
94
- }
95
-
96
- /**
97
- * @param value The base URI to the store.
98
- */
99
- set baseUri(value: string) {
100
- if (!value) {
101
- throw new Error(`Invalid value for the baseUri`);
102
- }
103
- let url: URL;
104
- try {
105
- url = new URL(value);
106
- } catch (e) {
107
- throw new Error(`Invalid baseUri: Not an URL`);
108
- }
109
- this[baseUriSymbol] = url.origin;
110
- const { pathname } = url;
111
- if (pathname && pathname !== '/') {
112
- this.basePath = pathname;
113
- } else {
114
- this.basePath = undefined;
115
- }
116
- }
117
-
118
- /**
119
- * @param baseUri The base URI to the store.
120
- * @param basePath The base path (the router prefix) for the server.
121
- */
122
- constructor(baseUri: string, public basePath?: string) {
123
- this.baseUri = baseUri;
124
- }
125
-
126
- /**
127
- * Creates a full URL for a path.
128
- * This adds the server's base path and the passed path to the configured base URL.
129
- * @param path The path to the endpoint, without the base path.
130
- */
131
- getUrl(path='/'): URL {
132
- const { baseUri, basePath='' } = this;
133
- let userPath = path;
134
- if (basePath && userPath.startsWith(basePath)) {
135
- userPath = userPath.substring(basePath.length);
136
- }
137
- return new URL(`${basePath}${userPath}`, baseUri);
138
- }
139
-
140
- /**
141
- * Appends list options to the query parameters.
142
- * @param url The URL object to the API endpoint
143
- * @param options The list options, if any
144
- */
145
- appendListOptions(url: URL, options: IListOptions = {}): void {
146
- const { searchParams } = url;
147
- if (options.cursor) {
148
- searchParams.set('cursor', options.cursor);
149
- } else {
150
- if (typeof options.limit === 'number') {
151
- searchParams.set('limit', String(options.limit));
152
- }
153
- if (options.query) {
154
- searchParams.set('query', options.query);
155
- }
156
- if (Array.isArray(options.queryField)) {
157
- options.queryField.forEach((field) => {
158
- searchParams.append('queryField', field);
159
- });
160
- }
161
- }
162
- }
163
- }
164
-
165
- class SdkBase {
166
- constructor(public sdk: StoreSdk) {}
167
-
168
- protected logInvalidResponse(response: IStoreResponse): void {
169
- if (response.body) {
170
- try {
171
- const data = JSON.parse(response.body);
172
- if (data.message) {
173
- console.warn(`[Store message]: ${data.message}`);
174
- }
175
- } catch (e) {
176
- // .
177
- }
178
- }
179
- }
180
-
181
- /**
182
- * Throws unified message for a common error status codes.
183
- * It handles 404, 403, and 401 status codes.
184
- */
185
- protected inspectCommonStatusCodes(status: number): void {
186
- if (status === 404) {
187
- throw new Error(`Not found.`);
188
- }
189
- if (status === 403) {
190
- throw new Error(`You have no access to this resource.`);
191
- }
192
- if (status === 401) {
193
- throw new Error(`Not authorized.`);
194
- }
195
- }
196
- }
197
-
198
- class AuthSdk extends SdkBase {
199
- protected getExpires(headers: http.IncomingHttpHeaders): number | undefined {
200
- if (!headers.expires) {
201
- return undefined;
202
- }
203
- const d = new Date(headers.expires);
204
- const time = d.getTime();
205
- if (Number.isNaN(time)) {
206
- console.warn(`Invalid session response: the expires header cannot be parsed.`);
207
- return undefined;
208
- }
209
- return time;
210
- }
211
-
212
- /**
213
- * Creates unauthenticated session in the backend.
214
- * @returns The JWT for unauthenticated user.
215
- */
216
- async createSession(): Promise<IStoreTokenInfo> {
217
- const url = this.sdk.getUrl('/sessions');
218
- // console.log('Create session: ', url);
219
- const result = await this.sdk.http.post(url.toString());
220
- this.inspectCommonStatusCodes(result.status);
221
- if (result.status !== 200) {
222
- throw new Error(`Unable to create the session. Invalid response status: ${result.status}`);
223
- }
224
- if (!result.body) {
225
- throw new Error(`Unable to create the session. Response has no token.`);
226
- }
227
- const info: IStoreTokenInfo = {
228
- token: result.body,
229
- };
230
- const expires = this.getExpires(result.headers);
231
- if (expires) {
232
- info.expires = expires;
233
- }
234
- return info;
235
- }
236
-
237
- /**
238
- * Initializes the authentication session.
239
- * @param token The unauthenticated session JWT. Required when not set on the class.
240
- * @returns The location of the authorization endpoint.
241
- */
242
- async createAuthSession(token?: string, loginPath = '/auth/login'): Promise<string> {
243
- const url = this.sdk.getUrl(loginPath);
244
- const result = await this.sdk.http.post(url.toString(), { token });
245
- this.inspectCommonStatusCodes(result.status);
246
- const loc = result.headers.location;
247
- if (!loc) {
248
- throw new Error(`The location header not returned by the server.`);
249
- }
250
- return loc;
251
- }
252
-
253
- /**
254
- * Listens to the first message coming to the client from the auth endpoint.
255
- * @param authPath The authorization path returned by the server info or 401 response.
256
- * @param token Optional token to use.
257
- */
258
- async listenAuth(authPath: string, token?: string): Promise<void> {
259
- const url = this.sdk.getUrl(authPath);
260
- const client = await this.sdk.ws.createAndConnect(url.toString(), token);
261
- return new Promise((resolve, reject) => {
262
- client.on('message', (data: Buffer) => {
263
- const message = JSON.parse(data.toString());
264
- this.sdk.ws.disconnect(client)
265
- .then(() => {
266
- if (message.status === 'OK') {
267
- resolve();
268
- } else {
269
- reject(new Error(message.message || 'Unknown error'));
270
- }
271
- }).catch(cause => reject(cause));
272
- });
273
- });
274
- }
275
-
276
- /**
277
- * Renews authenticated token to a new one when the token expires.
278
- * @param token Optional token to use.
279
- * @returns
280
- */
281
- async renewToken(token = this.sdk.token): Promise<IStoreTokenInfo> {
282
- const authPath = '/sessions/renew';
283
- const url = this.sdk.getUrl(authPath);
284
- const result = await this.sdk.http.post(url.toString(), { token });
285
- this.inspectCommonStatusCodes(result.status);
286
- if (result.status !== 200) {
287
- throw new Error(`Unable to renew the token. Invalid response status: ${result.status}`);
288
- }
289
- if (!result.body) {
290
- throw new Error(`Unable to create the session. Response has no token.`);
291
- }
292
- const info: IStoreTokenInfo = {
293
- token: result.body,
294
- };
295
- const expires = this.getExpires(result.headers);
296
- if (expires) {
297
- info.expires = expires;
298
- }
299
- return info;
300
- }
301
- }
302
-
303
- class BackendSdk extends SdkBase {
304
- /**
305
- * @returns Client information about the store configuration.
306
- */
307
- async getInfo(): Promise<IBackendInfo> {
308
- const url = this.sdk.getUrl('/store');
309
- const result = await this.sdk.http.get(url.toString());
310
- this.inspectCommonStatusCodes(result.status);
311
- if (result.status !== 200) {
312
- throw new Error(`Invalid store response. Expected 200 status and ${result.status} received.`);
313
- }
314
- const body = result.body as string;
315
- let data;
316
- try {
317
- data = JSON.parse(body);
318
- } catch (e) {
319
- throw new Error(`The server returned invalid response. Unable to read store status.`);
320
- }
321
- return data as IBackendInfo;
322
- }
323
- }
324
-
325
- class SpacesSdk extends SdkBase {
326
- /**
327
- * Lists spaces in the store.
328
- * @param options Optional query options.
329
- */
330
- async list(options?: IListOptions): Promise<IListResponse> {
331
- const { token } = this.sdk;
332
- const url = this.sdk.getUrl('/spaces');
333
- this.sdk.appendListOptions(url, options);
334
- const result = await this.sdk.http.get(url.toString(), { token });
335
- this.inspectCommonStatusCodes(result.status);
336
- const E_PREFIX = 'Unable to list spaces. ';
337
- if (result.status !== 200) {
338
- this.logInvalidResponse(result);
339
- throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
340
- }
341
- if (!result.body) {
342
- throw new Error(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`);
343
- }
344
- let data: IListResponse;
345
- try {
346
- data = JSON.parse(result.body);
347
- } catch (e) {
348
- throw new Error(`${E_PREFIX}${E_INVALID_JSON}.`);
349
- }
350
- if (!Array.isArray(data.data)) {
351
- throw new Error(`${E_PREFIX}${E_RESPONSE_UNKNOWN}.`);
352
- }
353
- return data;
354
- }
355
-
356
- /**
357
- * Creates a workspace in the store.
358
- * @param space The workspace definition.
359
- * @returns The key of the creates space.
360
- */
361
- async create(space: IWorkspace | Workspace): Promise<string> {
362
- const { token } = this.sdk;
363
- const url = this.sdk.getUrl('/spaces');
364
- const body = JSON.stringify(space);
365
- const result = await this.sdk.http.post(url.toString(), { token, body });
366
- this.inspectCommonStatusCodes(result.status);
367
- const E_PREFIX = 'Unable to create a user space. ';
368
- if (result.status !== 204) {
369
- this.logInvalidResponse(result);
370
- throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
371
- }
372
- const { location } = result.headers;
373
- if (!location) {
374
- throw new Error(`${E_PREFIX}${E_RESPONSE_LOCATION}`);
375
- }
376
- const id = location.split('/').pop();
377
- return id as string;
378
- }
379
-
380
- /**
381
- * Reads a user space definition from the store.
382
- * @param key The user space key
383
- * @returns The definition of the user space.
384
- */
385
- async read(key: string): Promise<IUserWorkspace> {
386
- const { token } = this.sdk;
387
- const url = this.sdk.getUrl(`/spaces/${key}`);
388
- const result = await this.sdk.http.get(url.toString(), { token });
389
- this.inspectCommonStatusCodes(result.status);
390
- const E_PREFIX = 'Unable to read a user space. ';
391
- if (result.status !== 200) {
392
- this.logInvalidResponse(result);
393
- throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
394
- }
395
- if (!result.body) {
396
- throw new Error(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`);
397
- }
398
- let data: IUserWorkspace;
399
- try {
400
- data = JSON.parse(result.body);
401
- } catch (e) {
402
- throw new Error(`${E_PREFIX}${E_INVALID_JSON}.`);
403
- }
404
- if (data.kind !== 'ARC#Space') {
405
- throw new Error(`${E_PREFIX}${E_RESPONSE_UNKNOWN}.`);
406
- }
407
- return data;
408
- }
409
-
410
- /**
411
- * Patches a user space in the store.
412
- * @param key The key of the user space to patch
413
- * @param value The JSON patch to be processed.
414
- * @returns The JSON patch to revert the change using the `json8-patch` library
415
- */
416
- async patch(key: string, value: JsonPatch): Promise<JsonPatch> {
417
- const { token } = this.sdk;
418
- const url = this.sdk.getUrl(`/spaces/${key}`);
419
- const body = JSON.stringify(value);
420
- const result = await this.sdk.http.patch(url.toString(), { token, body });
421
- this.inspectCommonStatusCodes(result.status);
422
- const E_PREFIX = 'Unable to patch a user space. ';
423
- if (result.status !== 200) {
424
- this.logInvalidResponse(result);
425
- throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
426
- }
427
- if (!result.body) {
428
- throw new Error(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`);
429
- }
430
- let data: any;
431
- try {
432
- data = JSON.parse(result.body);
433
- } catch (e) {
434
- throw new Error(`${E_PREFIX}${E_INVALID_JSON}.`);
435
- }
436
- if (!data.revert) {
437
- throw new Error(`${E_PREFIX}${E_RESPONSE_UNKNOWN}.`);
438
- }
439
- return data.revert as JsonPatch;
440
- }
441
-
442
- /**
443
- * Deletes the space in the store.
444
- *
445
- * @param key The key of the space to delete.
446
- */
447
- async delete(key: string): Promise<void> {
448
- const { token } = this.sdk;
449
- const url = this.sdk.getUrl(`/spaces/${key}`);
450
- const result = await this.sdk.http.delete(url.toString(), { token });
451
- this.inspectCommonStatusCodes(result.status);
452
- const E_PREFIX = 'Unable to delete a user space. ';
453
- if (result.status !== 204) {
454
- this.logInvalidResponse(result);
455
- throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
456
- }
457
- }
458
-
459
- /**
460
- * Updates the sharing options of the space.
461
- *
462
- * @param key The user space key
463
- * @param value The patch operation on the space's ACL
464
- */
465
- async patchUsers(key: string, value: UserAccessOperation[]): Promise<void> {
466
- const { token } = this.sdk;
467
- const url = this.sdk.getUrl(`/spaces/${key}/users`);
468
- const body = JSON.stringify(value);
469
- const result = await this.sdk.http.patch(url.toString(), { token, body });
470
- this.inspectCommonStatusCodes(result.status);
471
- const E_PREFIX = 'Unable to patch a user space. ';
472
- if (result.status !== 204) {
473
- this.logInvalidResponse(result);
474
- throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
475
- }
476
- }
477
-
478
- /**
479
- * Lists uses having access to the user space.
480
- *
481
- * @param key The user space key
482
- */
483
- async listUsers(key: string): Promise<IListResponse> {
484
- const { token } = this.sdk;
485
- const url = this.sdk.getUrl(`/spaces/${key}/users`);
486
- const result = await this.sdk.http.get(url.toString(), { token });
487
- this.inspectCommonStatusCodes(result.status);
488
- const E_PREFIX = 'Unable to list users in the space. ';
489
- if (result.status !== 200) {
490
- this.logInvalidResponse(result);
491
- throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
492
- }
493
- if (!result.body) {
494
- throw new Error(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`);
495
- }
496
- let data: IListResponse;
497
- try {
498
- data = JSON.parse(result.body);
499
- } catch (e) {
500
- throw new Error(`${E_PREFIX}${E_INVALID_JSON}.`);
501
- }
502
- if (!Array.isArray(data.data)) {
503
- throw new Error(`${E_PREFIX}${E_RESPONSE_UNKNOWN}.`);
504
- }
505
- return data;
506
- }
507
- }
508
-
509
- class HttpClient extends SdkBase {
510
- /**
511
- * Performs the GET request.
512
- *
513
- * @param url The request URL
514
- * @param opts The request options
515
- * @returns The response info.
516
- */
517
- async get(url: string, opts: IStoreRequestOptions = {}): Promise<IStoreResponse> {
518
- const result = await this._get(url, opts);
519
- if ([301, 302, 308, 307].includes(result.status)) {
520
- const loc = result.headers.location;
521
- if (!loc) {
522
- throw new Error('Expected redirection but no "location" header.')
523
- }
524
- const newUrl = new URL(loc, url);
525
- return this.get(newUrl.toString(), opts);
526
- }
527
- return result;
528
- }
529
-
530
- private _get(url: string, opts: IStoreRequestOptions = {}): Promise<IStoreResponse> {
531
- return new Promise((resolve, reject) => {
532
- const { method='GET', headers={}, token = this.sdk.token } = opts;
533
- if (token) {
534
- headers.authorization = `Bearer ${token}`;
535
- }
536
- const isSsl = url.startsWith('https:');
537
- const lib = isSsl ? https : http;
538
- // console.log(`${method} ${url}`);
539
- const request = lib.request(url, {
540
- method,
541
- headers,
542
- rejectUnauthorized: false,
543
- });
544
- request.on('response', (response) => {
545
- const ro: IStoreResponse = {
546
- status: response.statusCode as number,
547
- headers: response.headers,
548
- body: '',
549
- };
550
- response.on('data', (chunk) => {
551
- ro.body += chunk;
552
- });
553
- response.on('end', () => resolve(ro));
554
- });
555
- request.on('error', (error) => reject(error));
556
- request.end();
557
- });
558
- }
559
-
560
- post(url: string, opts: IStoreRequestOptions = {}): Promise<IStoreResponse> {
561
- return new Promise((resolve, reject) => {
562
- const { method='POST', headers={}, token = this.sdk.token } = opts;
563
- if (token) {
564
- headers.authorization = `Bearer ${token}`;
565
- }
566
- const isSsl = url.startsWith('https:');
567
- const lib = isSsl ? https : http;
568
- const request = lib.request(url, {
569
- method,
570
- headers,
571
- });
572
- request.on('response', (response) => {
573
- const ro: IStoreResponse = {
574
- status: response.statusCode as number,
575
- headers: response.headers,
576
- body: '',
577
- };
578
- response.on('data', (chunk) => {
579
- ro.body += chunk;
580
- });
581
- response.on('end', () => resolve(ro));
582
- });
583
- request.on('error', (error) => reject(error));
584
- if (opts.body) {
585
- request.write(opts.body);
586
- }
587
- request.end();
588
- });
589
- }
590
-
591
- patch(url: string, opts: IStoreRequestOptions = {}): Promise<IStoreResponse> {
592
- const options = { ...opts };
593
- options.method = 'PATCH';
594
- return this.post(url, options);
595
- }
596
-
597
- delete(url: string, opts: IStoreRequestOptions = {}): Promise<IStoreResponse> {
598
- const options = { ...opts };
599
- options.method = 'DELETE';
600
- return this.post(url, options);
601
- }
602
- }
603
-
604
- class WsClient extends SdkBase {
605
- /**
606
- * Creates a WS client with optional token
607
- * @param addr The ws:// address
608
- * @param token Optional token to add.
609
- */
610
- getClient(addr: string, token = this.sdk.token): WebSocket {
611
- let url = addr;
612
- if (token) {
613
- url += url.includes('?') ? '&' : '?';
614
- url += 'token=';
615
- url += token;
616
- }
617
- if (url.startsWith('http:')) {
618
- url = `ws:${url.substring(5)}`;
619
- } else if (url.startsWith('https:')) {
620
- url = `wss:${url.substring(6)}`;
621
- }
622
- return new WebSocket(url);
623
- }
624
-
625
- /**
626
- * Connect to the WS server
627
- *
628
- * @param client The client to wait for connection.
629
- */
630
- connect(client: WebSocket): Promise<void> {
631
- return new Promise((resolve, reject) => {
632
- client.once('open', () => {
633
- client.removeAllListeners('error');
634
- resolve();
635
- });
636
- client.once('error', (err) => {
637
- client.removeAllListeners('open');
638
- reject(err);
639
- });
640
- });
641
- }
642
-
643
- /**
644
- * Disconnects from the WS server.
645
- */
646
- disconnect(client: WebSocket): Promise<void> {
647
- return new Promise((resolve, reject) => {
648
- client.once('close', () => {
649
- client.removeAllListeners('error');
650
- resolve();
651
- });
652
- client.once('error', (err) => {
653
- client.removeAllListeners('close');
654
- reject(err);
655
- });
656
- client.close();
657
- });
658
- }
659
-
660
- /**
661
- * The combination of `getClient()` and `connect()`.
662
- */
663
- async createAndConnect(addr: string, token?: string): Promise<WebSocket> {
664
- const client = this.getClient(addr, token);
665
- await this.connect(client);
666
- return client;
667
- }
668
- }
669
-
670
- class ProjectsSdk extends SdkBase {
671
- /**
672
- * Creates a project in a user space.
673
- *
674
- * @param key The user space key
675
- * @param project THe project to create
676
- * @returns The key of the created project.
677
- */
678
- async create(key: string, project: IHttpProject | HttpProject): Promise<string> {
679
- const { token } = this.sdk;
680
- const url = this.sdk.getUrl(`/spaces/${key}/projects`);
681
- const body = JSON.stringify(project);
682
- const result = await this.sdk.http.post(url.toString(), { token, body });
683
- this.inspectCommonStatusCodes(result.status);
684
- const E_PREFIX = 'Unable to create a project. ';
685
- if (result.status !== 204) {
686
- this.logInvalidResponse(result);
687
- throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
688
- }
689
- const { location } = result.headers;
690
- if (!location) {
691
- throw new Error(`${E_PREFIX}${E_RESPONSE_LOCATION}`);
692
- }
693
- const id = location.split('/').pop();
694
- return id as string;
695
- }
696
-
697
- /**
698
- * Reads a project definition from the store.
699
- * @param space The user space key
700
- * @param project The project key
701
- * @returns The definition of the project.
702
- */
703
- async read(space: string, project: string): Promise<IHttpProject> {
704
- const { token } = this.sdk;
705
- const url = this.sdk.getUrl(`/spaces/${space}/projects/${project}`);
706
- const result = await this.sdk.http.get(url.toString(), { token });
707
- this.inspectCommonStatusCodes(result.status);
708
- const E_PREFIX = 'Unable to read a project. ';
709
- if (result.status !== 200) {
710
- this.logInvalidResponse(result);
711
- throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
712
- }
713
- if (!result.body) {
714
- throw new Error(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`);
715
- }
716
- let data: IHttpProject;
717
- try {
718
- data = JSON.parse(result.body);
719
- } catch (e) {
720
- throw new Error(`${E_PREFIX}${E_INVALID_JSON}.`);
721
- }
722
- if (data.kind !== 'ARC#HttpProject') {
723
- throw new Error(`${E_PREFIX}${E_RESPONSE_UNKNOWN}.`);
724
- }
725
- return data;
726
- }
727
-
728
- /**
729
- * Lists projects in the space
730
- *
731
- * @param space The user space key
732
- * @param options Optional query options.
733
- */
734
- async list(space: string, options?: IListOptions): Promise<IListResponse> {
735
- const { token } = this.sdk;
736
- const url = this.sdk.getUrl(`/spaces/${space}/projects`);
737
- this.sdk.appendListOptions(url, options);
738
- const result = await this.sdk.http.get(url.toString(), { token });
739
- this.inspectCommonStatusCodes(result.status);
740
- const E_PREFIX = 'Unable to list projects. ';
741
- if (result.status !== 200) {
742
- this.logInvalidResponse(result);
743
- throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
744
- }
745
- if (!result.body) {
746
- throw new Error(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`);
747
- }
748
- let data: IListResponse;
749
- try {
750
- data = JSON.parse(result.body);
751
- } catch (e) {
752
- throw new Error(`${E_PREFIX}${E_INVALID_JSON}.`);
753
- }
754
- if (!Array.isArray(data.data)) {
755
- throw new Error(`${E_PREFIX}${E_RESPONSE_UNKNOWN}.`);
756
- }
757
- return data;
758
- }
759
-
760
- /**
761
- * Deletes a project form the store.
762
- *
763
- * @param space The key of the parent space.
764
- * @param project The key of the project to delete.
765
- */
766
- async delete(space: string, project: string): Promise<void> {
767
- const { token } = this.sdk;
768
- const url = this.sdk.getUrl(`/spaces/${space}/projects/${project}`);
769
- const result = await this.sdk.http.delete(url.toString(), { token });
770
- this.inspectCommonStatusCodes(result.status);
771
- const E_PREFIX = 'Unable to delete a project. ';
772
- if (result.status !== 204) {
773
- this.logInvalidResponse(result);
774
- throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
775
- }
776
- }
777
-
778
- /**
779
- * Patches a project in the store.
780
- * @param space The key of the parent user space
781
- * @param project The key of project to patch.
782
- * @param value The JSON patch to be processed.
783
- * @returns The JSON patch to revert the change using the `json8-patch` library
784
- */
785
- async patch(space: string, project: string, value: JsonPatch): Promise<JsonPatch> {
786
- const { token } = this.sdk;
787
- const url = this.sdk.getUrl(`/spaces/${space}/projects/${project}`);
788
- const body = JSON.stringify(value);
789
- const result = await this.sdk.http.patch(url.toString(), { token, body });
790
- this.inspectCommonStatusCodes(result.status);
791
- const E_PREFIX = 'Unable to patch a project. ';
792
- if (result.status !== 200) {
793
- this.logInvalidResponse(result);
794
- throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
795
- }
796
- if (!result.body) {
797
- throw new Error(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`);
798
- }
799
- let data: any;
800
- try {
801
- data = JSON.parse(result.body);
802
- } catch (e) {
803
- throw new Error(`${E_PREFIX}${E_INVALID_JSON}.`);
804
- }
805
- if (!data.revert) {
806
- throw new Error(`${E_PREFIX}${E_RESPONSE_UNKNOWN}.`);
807
- }
808
- return data.revert as JsonPatch;
809
- }
810
- }
811
-
812
- class UsersSdk extends SdkBase {
813
- async me(): Promise<IUser> {
814
- const { token } = this.sdk;
815
- const url = this.sdk.getUrl(`/users/me`);
816
- const result = await this.sdk.http.get(url.toString(), { token });
817
- this.inspectCommonStatusCodes(result.status);
818
- const E_PREFIX = 'Unable to read a user. ';
819
- if (result.status !== 200) {
820
- this.logInvalidResponse(result);
821
- throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
822
- }
823
- if (!result.body) {
824
- throw new Error(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`);
825
- }
826
- let data: IUser;
827
- try {
828
- data = JSON.parse(result.body);
829
- } catch (e) {
830
- throw new Error(`${E_PREFIX}${E_INVALID_JSON}.`);
831
- }
832
- if (!data.key) {
833
- throw new Error(`${E_PREFIX}${E_RESPONSE_UNKNOWN}.`);
834
- }
835
- return data;
836
- }
837
-
838
- /**
839
- * Lists users in the store
840
- *
841
- * @param options Optional query options.
842
- */
843
- async list(options?: IListOptions): Promise<IListResponse> {
844
- const { token } = this.sdk;
845
- const url = this.sdk.getUrl(`/users`);
846
- this.sdk.appendListOptions(url, options);
847
- const result = await this.sdk.http.get(url.toString(), { token });
848
- this.inspectCommonStatusCodes(result.status);
849
- const E_PREFIX = 'Unable to list projects. ';
850
- if (result.status !== 200) {
851
- this.logInvalidResponse(result);
852
- throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
853
- }
854
- if (!result.body) {
855
- throw new Error(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`);
856
- }
857
- let data: IListResponse;
858
- try {
859
- data = JSON.parse(result.body);
860
- } catch (e) {
861
- throw new Error(`${E_PREFIX}${E_INVALID_JSON}.`);
862
- }
863
- if (!Array.isArray(data.data)) {
864
- throw new Error(`${E_PREFIX}${E_RESPONSE_UNKNOWN}.`);
865
- }
866
- return data;
867
- }
868
-
869
- /**
870
- * Reads a user information from the store.
871
- * @param key The user key.
872
- * @returns The user object
873
- */
874
- async read(key: string): Promise<IUser> {
875
- const { token } = this.sdk;
876
- const url = this.sdk.getUrl(`/users/${key}`);
877
- const result = await this.sdk.http.get(url.toString(), { token });
878
- this.inspectCommonStatusCodes(result.status);
879
- const E_PREFIX = 'Unable to read the user info. ';
880
- if (result.status !== 200) {
881
- this.logInvalidResponse(result);
882
- throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
883
- }
884
- if (!result.body) {
885
- throw new Error(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`);
886
- }
887
- let data: IUser;
888
- try {
889
- data = JSON.parse(result.body);
890
- } catch (e) {
891
- throw new Error(`${E_PREFIX}${E_INVALID_JSON}.`);
892
- }
893
- return data;
894
- }
895
- }