@esri/solution-common 4.1.2 → 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (261) hide show
  1. package/dist/cjs/completeItem.d.ts +29 -29
  2. package/dist/cjs/completeItem.js +81 -81
  3. package/dist/cjs/create-hub-request-options.d.ts +29 -29
  4. package/dist/cjs/create-hub-request-options.js +63 -63
  5. package/dist/cjs/deleteHelpers/deleteEmptyGroups.d.ts +24 -24
  6. package/dist/cjs/deleteHelpers/deleteEmptyGroups.js +41 -41
  7. package/dist/cjs/deleteHelpers/deleteGroupIfEmpty.d.ts +27 -27
  8. package/dist/cjs/deleteHelpers/deleteGroupIfEmpty.js +96 -96
  9. package/dist/cjs/deleteHelpers/deleteSolutionContents.d.ts +38 -38
  10. package/dist/cjs/deleteHelpers/deleteSolutionContents.js +129 -129
  11. package/dist/cjs/deleteHelpers/deleteSolutionFolder.d.ts +29 -29
  12. package/dist/cjs/deleteHelpers/deleteSolutionFolder.js +78 -78
  13. package/dist/cjs/deleteHelpers/deleteSolutionItem.d.ts +30 -30
  14. package/dist/cjs/deleteHelpers/deleteSolutionItem.js +53 -53
  15. package/dist/cjs/deleteHelpers/index.d.ts +22 -22
  16. package/dist/cjs/deleteHelpers/index.js +25 -25
  17. package/dist/cjs/deleteHelpers/reconstructBuildOrderIds.d.ts +27 -27
  18. package/dist/cjs/deleteHelpers/reconstructBuildOrderIds.js +33 -33
  19. package/dist/cjs/deleteHelpers/removeItems.d.ts +34 -34
  20. package/dist/cjs/deleteHelpers/removeItems.js +111 -111
  21. package/dist/cjs/deleteHelpers/reportProgress.d.ts +27 -27
  22. package/dist/cjs/deleteHelpers/reportProgress.js +45 -45
  23. package/dist/cjs/deleteSolution.d.ts +55 -55
  24. package/dist/cjs/deleteSolution.js +106 -106
  25. package/dist/cjs/dependencies.d.ts +26 -26
  26. package/dist/cjs/dependencies.js +170 -170
  27. package/dist/cjs/featureServiceHelpers.d.ts +791 -791
  28. package/dist/cjs/featureServiceHelpers.js +2420 -2420
  29. package/dist/cjs/generalHelpers.d.ts +392 -385
  30. package/dist/cjs/generalHelpers.js +857 -854
  31. package/dist/cjs/generalHelpers.js.map +1 -1
  32. package/dist/cjs/get-subscription-info.d.ts +27 -27
  33. package/dist/cjs/get-subscription-info.js +38 -38
  34. package/dist/cjs/getDeletableSolutionInfo.d.ts +29 -29
  35. package/dist/cjs/getDeletableSolutionInfo.js +52 -52
  36. package/dist/cjs/getItemTypeAbbrev.d.ts +19 -19
  37. package/dist/cjs/getItemTypeAbbrev.js +184 -184
  38. package/dist/cjs/getSolutionSummary.d.ts +27 -27
  39. package/dist/cjs/getSolutionSummary.js +100 -100
  40. package/dist/cjs/index.d.ts +43 -44
  41. package/dist/cjs/index.js +46 -47
  42. package/dist/cjs/index.js.map +1 -1
  43. package/dist/cjs/interfaces.d.ts +1334 -1334
  44. package/dist/cjs/interfaces.js +74 -74
  45. package/dist/cjs/interfaces.js.map +1 -1
  46. package/dist/cjs/libConnectors.d.ts +73 -73
  47. package/dist/cjs/libConnectors.js +114 -114
  48. package/dist/cjs/migrations/apply-schema.d.ts +24 -24
  49. package/dist/cjs/migrations/apply-schema.js +35 -35
  50. package/dist/cjs/migrations/is-legacy-solution.d.ts +24 -24
  51. package/dist/cjs/migrations/is-legacy-solution.js +39 -39
  52. package/dist/cjs/migrations/upgrade-three-dot-one.d.ts +27 -27
  53. package/dist/cjs/migrations/upgrade-three-dot-one.js +48 -48
  54. package/dist/cjs/migrations/upgrade-three-dot-zero.d.ts +27 -27
  55. package/dist/cjs/migrations/upgrade-three-dot-zero.js +42 -42
  56. package/dist/cjs/migrations/upgrade-two-dot-five.d.ts +24 -24
  57. package/dist/cjs/migrations/upgrade-two-dot-five.js +72 -72
  58. package/dist/cjs/migrations/upgrade-two-dot-four.d.ts +24 -24
  59. package/dist/cjs/migrations/upgrade-two-dot-four.js +71 -71
  60. package/dist/cjs/migrations/upgrade-two-dot-one.d.ts +7 -7
  61. package/dist/cjs/migrations/upgrade-two-dot-one.js +38 -38
  62. package/dist/cjs/migrations/upgrade-two-dot-seven.d.ts +23 -23
  63. package/dist/cjs/migrations/upgrade-two-dot-seven.js +57 -57
  64. package/dist/cjs/migrations/upgrade-two-dot-six.d.ts +27 -27
  65. package/dist/cjs/migrations/upgrade-two-dot-six.js +60 -60
  66. package/dist/cjs/migrations/upgrade-two-dot-three.d.ts +23 -23
  67. package/dist/cjs/migrations/upgrade-two-dot-three.js +54 -54
  68. package/dist/cjs/migrations/upgrade-two-dot-two.d.ts +23 -23
  69. package/dist/cjs/migrations/upgrade-two-dot-two.js +57 -57
  70. package/dist/cjs/migrations/upgrade-two-dot-zero.d.ts +44 -44
  71. package/dist/cjs/migrations/upgrade-two-dot-zero.js +94 -94
  72. package/dist/cjs/migrator.d.ts +25 -25
  73. package/dist/cjs/migrator.js +76 -76
  74. package/dist/cjs/resourceHelpers.d.ts +191 -191
  75. package/dist/cjs/resourceHelpers.js +383 -390
  76. package/dist/cjs/resourceHelpers.js.map +1 -1
  77. package/dist/cjs/resources/add-resource-from-blob.d.ts +26 -26
  78. package/dist/cjs/resources/add-resource-from-blob.js +51 -51
  79. package/dist/cjs/resources/addMetadataFromBlob.d.ts +25 -25
  80. package/dist/cjs/resources/addMetadataFromBlob.js +42 -42
  81. package/dist/cjs/resources/convert-item-resource-to-storage-resource.d.ts +32 -32
  82. package/dist/cjs/resources/convert-item-resource-to-storage-resource.js +69 -69
  83. package/dist/cjs/resources/convert-storage-resource-to-item-resource.d.ts +29 -29
  84. package/dist/cjs/resources/convert-storage-resource-to-item-resource.js +69 -69
  85. package/dist/cjs/resources/copyAssociatedFiles.d.ts +67 -67
  86. package/dist/cjs/resources/copyAssociatedFiles.js +301 -301
  87. package/dist/cjs/resources/copyDataIntoItem.d.ts +33 -33
  88. package/dist/cjs/resources/copyDataIntoItem.js +61 -62
  89. package/dist/cjs/resources/copyDataIntoItem.js.map +1 -1
  90. package/dist/cjs/resources/copyMetadataIntoItem.d.ts +26 -26
  91. package/dist/cjs/resources/copyMetadataIntoItem.js +45 -45
  92. package/dist/cjs/resources/copyResourceIntoZip.d.ts +33 -33
  93. package/dist/cjs/resources/copyResourceIntoZip.js +77 -77
  94. package/dist/cjs/resources/copyZipIntoItem.d.ts +25 -25
  95. package/dist/cjs/resources/copyZipIntoItem.js +53 -53
  96. package/dist/cjs/resources/createCopyResults.d.ts +25 -25
  97. package/dist/cjs/resources/createCopyResults.js +35 -35
  98. package/dist/cjs/resources/get-blob.d.ts +26 -26
  99. package/dist/cjs/resources/get-blob.js +26 -26
  100. package/dist/cjs/resources/getItemResourcesFilesFromPaths.d.ts +24 -24
  101. package/dist/cjs/resources/getItemResourcesFilesFromPaths.js +48 -48
  102. package/dist/cjs/resources/getItemResourcesPaths.d.ts +26 -26
  103. package/dist/cjs/resources/getItemResourcesPaths.js +72 -72
  104. package/dist/cjs/resources/index.d.ts +29 -29
  105. package/dist/cjs/resources/index.js +32 -32
  106. package/dist/cjs/resources/solution-resource.d.ts +35 -35
  107. package/dist/cjs/resources/solution-resource.js +30 -30
  108. package/dist/cjs/resources/solution-resource.js.map +1 -1
  109. package/dist/cjs/resources/transform-resource-paths-to-solution-resources.d.ts +56 -56
  110. package/dist/cjs/resources/transform-resource-paths-to-solution-resources.js +145 -145
  111. package/dist/cjs/restHelpers.d.ts +586 -585
  112. package/dist/cjs/restHelpers.js +1888 -1883
  113. package/dist/cjs/restHelpers.js.map +1 -1
  114. package/dist/cjs/restHelpersGet.d.ts +288 -288
  115. package/dist/cjs/restHelpersGet.js +803 -803
  116. package/dist/cjs/sharing/index.d.ts +16 -16
  117. package/dist/cjs/sharing/index.js +19 -19
  118. package/dist/cjs/sharing/share-item-to-groups.d.ts +26 -26
  119. package/dist/cjs/sharing/share-item-to-groups.js +43 -43
  120. package/dist/cjs/templatization.d.ts +139 -139
  121. package/dist/cjs/templatization.js +313 -313
  122. package/dist/cjs/trackingHelpers.d.ts +116 -116
  123. package/dist/cjs/trackingHelpers.js +216 -216
  124. package/dist/cjs/velocityHelpers.d.ts +57 -57
  125. package/dist/cjs/velocityHelpers.js +134 -134
  126. package/dist/cjs/workforceHelpers.d.ts +115 -115
  127. package/dist/cjs/workforceHelpers.js +746 -746
  128. package/dist/cjs/workforceHelpers.js.map +1 -1
  129. package/dist/esm/completeItem.d.ts +29 -29
  130. package/dist/esm/completeItem.js +76 -76
  131. package/dist/esm/create-hub-request-options.d.ts +29 -29
  132. package/dist/esm/create-hub-request-options.js +59 -59
  133. package/dist/esm/deleteHelpers/deleteEmptyGroups.d.ts +24 -24
  134. package/dist/esm/deleteHelpers/deleteEmptyGroups.js +37 -37
  135. package/dist/esm/deleteHelpers/deleteGroupIfEmpty.d.ts +27 -27
  136. package/dist/esm/deleteHelpers/deleteGroupIfEmpty.js +91 -91
  137. package/dist/esm/deleteHelpers/deleteSolutionContents.d.ts +38 -38
  138. package/dist/esm/deleteHelpers/deleteSolutionContents.js +124 -124
  139. package/dist/esm/deleteHelpers/deleteSolutionFolder.d.ts +29 -29
  140. package/dist/esm/deleteHelpers/deleteSolutionFolder.js +73 -73
  141. package/dist/esm/deleteHelpers/deleteSolutionItem.d.ts +30 -30
  142. package/dist/esm/deleteHelpers/deleteSolutionItem.js +48 -48
  143. package/dist/esm/deleteHelpers/index.d.ts +22 -22
  144. package/dist/esm/deleteHelpers/index.js +22 -22
  145. package/dist/esm/deleteHelpers/reconstructBuildOrderIds.d.ts +27 -27
  146. package/dist/esm/deleteHelpers/reconstructBuildOrderIds.js +28 -28
  147. package/dist/esm/deleteHelpers/removeItems.d.ts +34 -34
  148. package/dist/esm/deleteHelpers/removeItems.js +106 -106
  149. package/dist/esm/deleteHelpers/reportProgress.d.ts +27 -27
  150. package/dist/esm/deleteHelpers/reportProgress.js +41 -41
  151. package/dist/esm/deleteSolution.d.ts +55 -55
  152. package/dist/esm/deleteSolution.js +100 -100
  153. package/dist/esm/dependencies.d.ts +26 -26
  154. package/dist/esm/dependencies.js +166 -166
  155. package/dist/esm/featureServiceHelpers.d.ts +791 -791
  156. package/dist/esm/featureServiceHelpers.js +2336 -2336
  157. package/dist/esm/generalHelpers.d.ts +392 -385
  158. package/dist/esm/generalHelpers.js +810 -808
  159. package/dist/esm/generalHelpers.js.map +1 -1
  160. package/dist/esm/get-subscription-info.d.ts +27 -27
  161. package/dist/esm/get-subscription-info.js +34 -34
  162. package/dist/esm/getDeletableSolutionInfo.d.ts +29 -29
  163. package/dist/esm/getDeletableSolutionInfo.js +47 -47
  164. package/dist/esm/getItemTypeAbbrev.d.ts +19 -19
  165. package/dist/esm/getItemTypeAbbrev.js +180 -180
  166. package/dist/esm/getSolutionSummary.d.ts +27 -27
  167. package/dist/esm/getSolutionSummary.js +95 -95
  168. package/dist/esm/index.d.ts +43 -44
  169. package/dist/esm/index.js +43 -44
  170. package/dist/esm/index.js.map +1 -1
  171. package/dist/esm/interfaces.d.ts +1334 -1334
  172. package/dist/esm/interfaces.js +70 -70
  173. package/dist/esm/libConnectors.d.ts +73 -73
  174. package/dist/esm/libConnectors.js +104 -104
  175. package/dist/esm/migrations/apply-schema.d.ts +24 -24
  176. package/dist/esm/migrations/apply-schema.js +31 -31
  177. package/dist/esm/migrations/is-legacy-solution.d.ts +24 -24
  178. package/dist/esm/migrations/is-legacy-solution.js +35 -35
  179. package/dist/esm/migrations/upgrade-three-dot-one.d.ts +27 -27
  180. package/dist/esm/migrations/upgrade-three-dot-one.js +44 -44
  181. package/dist/esm/migrations/upgrade-three-dot-zero.d.ts +27 -27
  182. package/dist/esm/migrations/upgrade-three-dot-zero.js +38 -38
  183. package/dist/esm/migrations/upgrade-two-dot-five.d.ts +24 -24
  184. package/dist/esm/migrations/upgrade-two-dot-five.js +68 -68
  185. package/dist/esm/migrations/upgrade-two-dot-four.d.ts +24 -24
  186. package/dist/esm/migrations/upgrade-two-dot-four.js +67 -67
  187. package/dist/esm/migrations/upgrade-two-dot-one.d.ts +7 -7
  188. package/dist/esm/migrations/upgrade-two-dot-one.js +34 -34
  189. package/dist/esm/migrations/upgrade-two-dot-seven.d.ts +23 -23
  190. package/dist/esm/migrations/upgrade-two-dot-seven.js +53 -53
  191. package/dist/esm/migrations/upgrade-two-dot-six.d.ts +27 -27
  192. package/dist/esm/migrations/upgrade-two-dot-six.js +56 -56
  193. package/dist/esm/migrations/upgrade-two-dot-three.d.ts +23 -23
  194. package/dist/esm/migrations/upgrade-two-dot-three.js +50 -50
  195. package/dist/esm/migrations/upgrade-two-dot-two.d.ts +23 -23
  196. package/dist/esm/migrations/upgrade-two-dot-two.js +53 -53
  197. package/dist/esm/migrations/upgrade-two-dot-zero.d.ts +44 -44
  198. package/dist/esm/migrations/upgrade-two-dot-zero.js +87 -87
  199. package/dist/esm/migrator.d.ts +25 -25
  200. package/dist/esm/migrator.js +72 -72
  201. package/dist/esm/resourceHelpers.d.ts +191 -191
  202. package/dist/esm/resourceHelpers.js +364 -371
  203. package/dist/esm/resourceHelpers.js.map +1 -1
  204. package/dist/esm/resources/add-resource-from-blob.d.ts +26 -26
  205. package/dist/esm/resources/add-resource-from-blob.js +47 -47
  206. package/dist/esm/resources/addMetadataFromBlob.d.ts +25 -25
  207. package/dist/esm/resources/addMetadataFromBlob.js +38 -38
  208. package/dist/esm/resources/convert-item-resource-to-storage-resource.d.ts +32 -32
  209. package/dist/esm/resources/convert-item-resource-to-storage-resource.js +65 -65
  210. package/dist/esm/resources/convert-storage-resource-to-item-resource.d.ts +29 -29
  211. package/dist/esm/resources/convert-storage-resource-to-item-resource.js +65 -65
  212. package/dist/esm/resources/copyAssociatedFiles.d.ts +67 -67
  213. package/dist/esm/resources/copyAssociatedFiles.js +293 -293
  214. package/dist/esm/resources/copyDataIntoItem.d.ts +33 -33
  215. package/dist/esm/resources/copyDataIntoItem.js +56 -57
  216. package/dist/esm/resources/copyDataIntoItem.js.map +1 -1
  217. package/dist/esm/resources/copyMetadataIntoItem.d.ts +26 -26
  218. package/dist/esm/resources/copyMetadataIntoItem.js +41 -41
  219. package/dist/esm/resources/copyResourceIntoZip.d.ts +33 -33
  220. package/dist/esm/resources/copyResourceIntoZip.js +72 -72
  221. package/dist/esm/resources/copyZipIntoItem.d.ts +25 -25
  222. package/dist/esm/resources/copyZipIntoItem.js +49 -49
  223. package/dist/esm/resources/createCopyResults.d.ts +25 -25
  224. package/dist/esm/resources/createCopyResults.js +31 -31
  225. package/dist/esm/resources/get-blob.d.ts +26 -26
  226. package/dist/esm/resources/get-blob.js +22 -22
  227. package/dist/esm/resources/getItemResourcesFilesFromPaths.d.ts +24 -24
  228. package/dist/esm/resources/getItemResourcesFilesFromPaths.js +44 -44
  229. package/dist/esm/resources/getItemResourcesPaths.d.ts +26 -26
  230. package/dist/esm/resources/getItemResourcesPaths.js +68 -68
  231. package/dist/esm/resources/index.d.ts +29 -29
  232. package/dist/esm/resources/index.js +29 -29
  233. package/dist/esm/resources/solution-resource.d.ts +35 -35
  234. package/dist/esm/resources/solution-resource.js +27 -27
  235. package/dist/esm/resources/transform-resource-paths-to-solution-resources.d.ts +56 -56
  236. package/dist/esm/resources/transform-resource-paths-to-solution-resources.js +137 -137
  237. package/dist/esm/restHelpers.d.ts +586 -585
  238. package/dist/esm/restHelpers.js +1827 -1823
  239. package/dist/esm/restHelpers.js.map +1 -1
  240. package/dist/esm/restHelpersGet.d.ts +288 -288
  241. package/dist/esm/restHelpersGet.js +763 -763
  242. package/dist/esm/sharing/index.d.ts +16 -16
  243. package/dist/esm/sharing/index.js +16 -16
  244. package/dist/esm/sharing/share-item-to-groups.d.ts +26 -26
  245. package/dist/esm/sharing/share-item-to-groups.js +39 -39
  246. package/dist/esm/templatization.d.ts +139 -139
  247. package/dist/esm/templatization.js +293 -293
  248. package/dist/esm/trackingHelpers.d.ts +116 -116
  249. package/dist/esm/trackingHelpers.js +204 -204
  250. package/dist/esm/velocityHelpers.d.ts +57 -57
  251. package/dist/esm/velocityHelpers.js +128 -128
  252. package/dist/esm/workforceHelpers.d.ts +115 -115
  253. package/dist/esm/workforceHelpers.js +717 -717
  254. package/dist/esm/workforceHelpers.js.map +1 -1
  255. package/package.json +2 -2
  256. package/dist/cjs/polyfills.d.ts +0 -40
  257. package/dist/cjs/polyfills.js +0 -98
  258. package/dist/cjs/polyfills.js.map +0 -1
  259. package/dist/esm/polyfills.d.ts +0 -40
  260. package/dist/esm/polyfills.js +0 -93
  261. package/dist/esm/polyfills.js.map +0 -1
@@ -1,764 +1,764 @@
1
- /** @license
2
- * Copyright 2018 Esri
3
- *
4
- * Licensed under the Apache License, Version 2.0 (the "License");
5
- * you may not use this file except in compliance with the License.
6
- * You may obtain a copy of the License at
7
- *
8
- * http://www.apache.org/licenses/LICENSE-2.0
9
- *
10
- * Unless required by applicable law or agreed to in writing, software
11
- * distributed under the License is distributed on an "AS IS" BASIS,
12
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- * See the License for the specific language governing permissions and
14
- * limitations under the License.
15
- */
16
- /**
17
- * Provides common item fetch functions involving the arcgis-rest-js library.
18
- *
19
- * @module restHelpersGet
20
- */
21
- import { blobToFile, blobToJson, blobToText, checkUrlPathTermination, getProp } from "./generalHelpers";
22
- import { getGroup, getGroupCategorySchema as portalGetGroupCategorySchema, getGroupContent, getItem, getItemResources as portalGetItemResources, getPortal as portalGetPortal, getRelatedItems } from "@esri/arcgis-rest-portal";
23
- import { request } from "@esri/arcgis-rest-request";
24
- import { getBlob } from "./resources/get-blob";
25
- import { searchGroups, searchGroupContents } from "./restHelpers";
26
- // ------------------------------------------------------------------------------------------------------------------ //
27
- const ZIP_FILE_HEADER_SIGNATURE = "PK";
28
- export function checkJsonForError(json) {
29
- return typeof json?.error !== "undefined";
30
- }
31
- export function getPortal(id, authentication) {
32
- const requestOptions = {
33
- authentication: authentication
34
- };
35
- return portalGetPortal(id, requestOptions);
36
- }
37
- export function getUser(authentication) {
38
- return authentication.getUser();
39
- }
40
- export function getUsername(authentication) {
41
- return new Promise((resolve, reject) => {
42
- getUser(authentication).then((user) => resolve(user.username), reject);
43
- });
44
- }
45
- export function getFoldersAndGroups(authentication) {
46
- return new Promise((resolve, reject) => {
47
- const requestOptions = {
48
- httpMethod: "GET",
49
- authentication: authentication,
50
- rawResponse: false
51
- };
52
- // Folders
53
- const foldersUrl = `${authentication.portal}/content/users/${encodeURIComponent(authentication.username)}`;
54
- // Groups
55
- const groupsUrl = `${authentication.portal}/community/users/${encodeURIComponent(authentication.username)}`;
56
- Promise.all([
57
- request(foldersUrl, requestOptions),
58
- request(groupsUrl, requestOptions)
59
- ]).then(responses => {
60
- resolve({
61
- folders: responses[0].folders || [],
62
- groups: responses[1].groups || []
63
- });
64
- }, e => reject(e));
65
- });
66
- }
67
- /**
68
- * Gets a Blob from a web site and casts it as a file using the supplied name.
69
- *
70
- * @param url Address of Blob
71
- * @param filename Name to use for file
72
- * @param authentication Credentials for the request
73
- * @returns Promise that will resolve with a File, undefined if the Blob is null, or an AGO-style JSON failure response
74
- */
75
- export function getBlobAsFile(url, filename, authentication, ignoreErrors = [], mimeType) {
76
- return new Promise((resolve, reject) => {
77
- // Get the blob from the URL
78
- getBlobCheckForError(url, authentication, ignoreErrors).then(blob => !blob ? resolve(null) : resolve(blobToFile(blob, filename, mimeType)), reject);
79
- });
80
- }
81
- /**
82
- * Gets a Blob from a web site and checks for a JSON error packet in the Blob.
83
- *
84
- * @param url Address of Blob
85
- * @param authentication Credentials for the request
86
- * @param ignoreErrors List of HTTP error codes that should be ignored
87
- * @returns Promise that will resolve with Blob or an AGO-REST JSON failure response
88
- */
89
- export function getBlobCheckForError(url, authentication, ignoreErrors = []) {
90
- return new Promise((resolve, reject) => {
91
- // Get the blob from the URL
92
- getBlob(url, authentication).then(blob => {
93
- // Reclassify text/plain blobs as needed
94
- _fixTextBlobType(blob).then(adjustedBlob => {
95
- if (adjustedBlob.type === "application/json") {
96
- // Blob may be an error
97
- // eslint-disable-next-line @typescript-eslint/no-floating-promises
98
- blobToJson(adjustedBlob).then((json) => {
99
- // Check for valid JSON with an error
100
- if (json?.error) {
101
- const code = json.error.code;
102
- if (code !== undefined && ignoreErrors.indexOf(code) >= 0) {
103
- resolve(null); // Error, but ignored
104
- }
105
- else {
106
- reject(json); // Other error; fail with error
107
- }
108
- }
109
- else {
110
- resolve(adjustedBlob);
111
- }
112
- });
113
- }
114
- else {
115
- resolve(adjustedBlob);
116
- }
117
- }, reject);
118
- }, reject);
119
- });
120
- }
121
- /**
122
- * Extracts the text in a url between the last forward slash and the beginning of the url's parameters.
123
- *
124
- * @param url URL to work with
125
- * @returns Text extracted; empty if url ends with a forward slash or has a "?" immediately after the last
126
- * forward slash
127
- */
128
- export function getFilenameFromUrl(url) {
129
- if (!url) {
130
- return "";
131
- }
132
- let iParamsStart = url.indexOf("?");
133
- /* istanbul ignore else */
134
- if (iParamsStart < 0) {
135
- iParamsStart = url.length;
136
- }
137
- const iFilenameStart = url.lastIndexOf("/", iParamsStart) + 1;
138
- return iFilenameStart < iParamsStart
139
- ? url.substring(iFilenameStart, iParamsStart)
140
- : "";
141
- }
142
- /**
143
- * Gets the primary information of an AGO group.
144
- *
145
- * @param groupId Id of an group whose primary information is sought
146
- * @param authentication Credentials for the request to AGO
147
- * @returns A promise that will resolve with group's JSON or error JSON or throws ArcGISRequestError in case of HTTP error
148
- * or response error code
149
- */
150
- export function getGroupBase(groupId, authentication) {
151
- const requestOptions = {
152
- authentication: authentication
153
- };
154
- return getGroup(groupId, requestOptions);
155
- }
156
- /**
157
- * Gets the category schema set on a group.
158
- *
159
- * @param groupId Id of an group whose category schema information is sought
160
- * @param authentication Credentials for the request to AGO
161
- * @returns A promise that will resolve with JSON of group's category schema
162
- * @see https://developers.arcgis.com/rest/users-groups-and-items/group-category-schema.htm
163
- */
164
- export function getGroupCategorySchema(groupId, authentication) {
165
- const requestOptions = {
166
- authentication: authentication
167
- };
168
- return portalGetGroupCategorySchema(groupId, requestOptions);
169
- }
170
- /**
171
- * Gets the ids of the dependencies (contents) of an AGO group.
172
- *
173
- * @param groupId Id of a group whose contents are sought
174
- * @param authentication Credentials for the request to AGO
175
- * @returns A promise that will resolve with list of dependent ids or an empty list
176
- */
177
- export function getGroupContents(groupId, authentication) {
178
- return new Promise((resolve, reject) => {
179
- const pagingParams = {
180
- start: 1,
181
- num: 100 // max allowed by REST API
182
- };
183
- // Fetch group items
184
- _getGroupContentsTranche(groupId, pagingParams, authentication).then(contents => {
185
- resolve(contents);
186
- }, reject);
187
- });
188
- }
189
- /**
190
- * Gets the primary information of an AGO item.
191
- *
192
- * @param itemId Id of an item whose primary information is sought
193
- * @param authentication Credentials for the request to AGO
194
- * @returns A promise that will resolve with item's JSON or error JSON or throws ArcGISRequestError in case of HTTP error
195
- * or response error code
196
- */
197
- export function getItemBase(itemId, authentication) {
198
- const itemParam = {
199
- authentication: authentication
200
- };
201
- return getItem(itemId, itemParam);
202
- }
203
- /**
204
- * Gets the data information of an AGO item in its raw (Blob) form and casts it as a file using the supplied name.
205
- *
206
- * @param itemId Id of an item whose data information is sought
207
- * @param filename Name to use for file
208
- * @param authentication Credentials for the request to AGO
209
- * @returns Promise that will resolve with a File, undefined if the Blob is null, or an AGO-style JSON failure response
210
- */
211
- export function getItemDataAsFile(itemId, filename, authentication) {
212
- return new Promise(resolve => {
213
- getItemDataBlob(itemId, authentication).then(blob => resolve(blobToFile(blob, filename)), () => resolve(null));
214
- });
215
- }
216
- /**
217
- * Gets the data information of an AGO item in its JSON form.
218
- *
219
- * @param itemId Id of an item whose data information is sought
220
- * @param filename Name to use for file
221
- * @param authentication Credentials for the request to AGO
222
- * @returns Promise that will resolve with JSON, or an AGO-style JSON failure response
223
- */
224
- export function getItemDataAsJson(itemId, authentication) {
225
- return new Promise(resolve => {
226
- getItemDataBlob(itemId, authentication).then(blob => resolve(blobToJson(blob)), () => resolve(null));
227
- });
228
- }
229
- /**
230
- * Gets the data information of an AGO item in its raw (Blob) form.
231
- *
232
- * @param itemId Id of an item whose data information is sought
233
- * @param authentication Credentials for the request to AGO
234
- * @returns A promise that will resolve with the data Blob or null if the item doesn't have a data section
235
- */
236
- export function getItemDataBlob(itemId, authentication) {
237
- return new Promise(resolve => {
238
- const url = getItemDataBlobUrl(itemId, authentication);
239
- getBlobCheckForError(url, authentication, [400, 500]).then(blob => resolve(_fixTextBlobType(blob)), () => resolve(null));
240
- });
241
- }
242
- /**
243
- * Gets the URL to the data information of an AGO item in its raw (Blob) form.
244
- *
245
- * @param itemId Id of an item whose data information is sought
246
- * @param authentication Credentials for the request to AGO
247
- * @returns URL string
248
- */
249
- export function getItemDataBlobUrl(itemId, authentication) {
250
- return `${getPortalSharingUrlFromAuth(authentication)}/content/items/${itemId}/data`;
251
- }
252
- /**
253
- * Gets the URL to an information item in an AGO item.
254
- *
255
- * @param itemId Id of an item whose data information is sought
256
- * @param authentication Credentials for the request to AGO
257
- * @returns URL string
258
- */
259
- export function getItemInfoFileUrlPrefix(itemId, authentication) {
260
- return `${getPortalSharingUrlFromAuth(authentication)}/content/items/${itemId}/info/`;
261
- }
262
- /**
263
- * Gets the metadata information of an AGO item.
264
- *
265
- * @param itemId Id of an item whose data information is sought
266
- * @param authentication Credentials for the request to AGO
267
- * @returns Promise that will resolve with `undefined` or a File containing the metadata
268
- */
269
- export function getItemMetadataAsFile(itemId, authentication) {
270
- return new Promise(resolve => {
271
- getItemMetadataBlob(itemId, authentication).then(blob => {
272
- if (!blob || (blob && blob.type.startsWith("application/json"))) {
273
- resolve(null); // JSON error
274
- }
275
- else {
276
- resolve(blobToFile(blob, "metadata.xml"));
277
- }
278
- }, () => resolve(null));
279
- });
280
- }
281
- /**
282
- * Gets the metadata information of an AGO item.
283
- *
284
- * @param itemId Id of an item whose data information is sought
285
- * @param authentication Credentials for the request to AGO
286
- * @returns A promise that will resolve with the metadata Blob or null if the item doesn't have a metadata file
287
- */
288
- export function getItemMetadataBlob(itemId, authentication) {
289
- return new Promise((resolve, reject) => {
290
- const url = getItemMetadataBlobUrl(itemId, authentication);
291
- getBlobCheckForError(url, authentication, [400]).then(resolve, reject);
292
- });
293
- }
294
- /**
295
- * Gets the URL to the metadata information of an AGO item.
296
- *
297
- * @param itemId Id of an item whose data information is sought
298
- * @param authentication Credentials for the request to AGO
299
- * @returns URL string
300
- */
301
- export function getItemMetadataBlobUrl(itemId, authentication) {
302
- return (getItemInfoFileUrlPrefix(itemId, authentication) + "metadata/metadata.xml");
303
- }
304
- /**
305
- * Gets the related items of an AGO item.
306
- *
307
- * @param itemId Id of an item whose related items are sought
308
- * @param relationshipType Tag for relationship type, e.g., "Solution2Item"
309
- * @param direction Relationship direction
310
- * @param authentication Credentials for the request to AGO
311
- * @param num Number of related items to request per batch; maximum is 100
312
- * @returns A promise that will resolve with the list of related items
313
- */
314
- export function getItemRelatedItems(itemId, relationshipType, direction, authentication, num = 100) {
315
- return new Promise(resolve => {
316
- const itemRelatedItemsParam = {
317
- id: itemId,
318
- relationshipType,
319
- authentication: authentication,
320
- params: {
321
- direction,
322
- start: 1,
323
- num
324
- }
325
- };
326
- // Fetch related items
327
- // eslint-disable-next-line @typescript-eslint/no-floating-promises
328
- _getItemRelatedItemsTranche(itemRelatedItemsParam).then(response => {
329
- resolve(response);
330
- });
331
- });
332
- }
333
- /**
334
- * Gets all of the related items of an AGO item in the specified direction.
335
- *
336
- * @param itemId Id of an item whose related items are sought
337
- * @param direction
338
- * @param authentication Credentials for the request to AGO
339
- * @returns A promise that will resolve with a list of IRelatedItems
340
- */
341
- export function getItemRelatedItemsInSameDirection(itemId, direction, authentication) {
342
- return new Promise(resolve => {
343
- const relationshipTypes = [
344
- // from ItemRelationshipType
345
- "APIKey2Item",
346
- "Area2CustomPackage",
347
- "Area2Package",
348
- "Item2Attachment",
349
- "Item2Report",
350
- "Listed2Provisioned",
351
- "Map2AppConfig",
352
- "Map2Area",
353
- "Map2FeatureCollection",
354
- "Map2Service",
355
- "MobileApp2Code",
356
- "Service2Data",
357
- "Service2Layer",
358
- "Service2Route",
359
- "Service2Service",
360
- "Service2Style",
361
- "Solution2Item",
362
- "Style2Style",
363
- "Survey2Data",
364
- "Survey2Service",
365
- "SurveyAddIn2Data",
366
- "Theme2Story",
367
- "TrackView2Map",
368
- "WebStyle2DesktopStyle",
369
- "WMA2Code",
370
- "WorkforceMap2FeatureService"
371
- ];
372
- const relatedItemDefs = relationshipTypes.map(relationshipType => getItemRelatedItems(itemId, relationshipType, direction, authentication));
373
- // eslint-disable-next-line @typescript-eslint/no-floating-promises
374
- Promise.all(relatedItemDefs).then((relationshipResponses) => {
375
- const relatedItems = [];
376
- for (let i = 0; i < relationshipTypes.length; ++i) {
377
- if (relationshipResponses[i].total > 0) {
378
- relatedItems.push({
379
- relationshipType: relationshipTypes[i],
380
- relatedItemIds: relationshipResponses[i].relatedItems.map(item => item.id)
381
- });
382
- }
383
- }
384
- resolve(relatedItems);
385
- });
386
- });
387
- }
388
- export function getItemResources(id, authentication) {
389
- return new Promise(resolve => {
390
- const requestOptions = {
391
- authentication: authentication
392
- };
393
- portalGetItemResources(id, requestOptions).then(resolve, () => {
394
- resolve({
395
- total: 0,
396
- start: 1,
397
- num: 0,
398
- nextStart: -1,
399
- resources: []
400
- });
401
- });
402
- });
403
- }
404
- /**
405
- * Gets the resources of an AGO item.
406
- *
407
- * @param itemId Id of an item whose resources are sought
408
- * @param authentication Credentials for the request to AGO
409
- * @returns Promise that will resolve with a list of Files or an AGO-style JSON failure response
410
- */
411
- export function getItemResourcesFiles(itemId, authentication) {
412
- return new Promise((resolve, reject) => {
413
- const pagingParams = {
414
- start: 1,
415
- num: 100 // max allowed by REST API
416
- };
417
- // Fetch resources
418
- _getItemResourcesTranche(itemId, pagingParams, authentication).then(itemResourcesDef => {
419
- Promise.all(itemResourcesDef).then(resolve, reject);
420
- }, reject);
421
- });
422
- }
423
- /**
424
- * Gets all of the items associated with a Solution via a Solution2Item relationship.
425
- *
426
- * @param solutionItemId Id of a deployed Solution
427
- * @param authentication Credentials for the request
428
- * @returns Promise resolving to a list of detailed item information
429
- */
430
- export function getItemsRelatedToASolution(solutionItemId, authentication) {
431
- // eslint-disable-next-line @typescript-eslint/no-floating-promises
432
- return getItemRelatedItems(solutionItemId, "Solution2Item", "forward", authentication).then((relationshipResponse) => {
433
- return relationshipResponse.relatedItems;
434
- });
435
- }
436
- /**
437
- * Gets the thumbnail of an AGO item.
438
- *
439
- * @param itemId Id of an item whose resources are sought
440
- * @param thumbnailUrlPart The partial name of the item's thumbnail as reported by the `thumbnail` property
441
- * in the item's base section
442
- * @param isGroup Switch indicating if the item is a group
443
- * @param authentication Credentials for the request to AGO
444
- * @returns Promise that will resolve with an image Blob or an AGO-style JSON failure response
445
- */
446
- export function getItemThumbnail(itemId, thumbnailUrlPart, isGroup, authentication) {
447
- return new Promise((resolve, reject) => {
448
- if (!thumbnailUrlPart) {
449
- resolve(null);
450
- return;
451
- }
452
- const url = getItemThumbnailUrl(itemId, thumbnailUrlPart, isGroup, authentication);
453
- getBlobCheckForError(url, authentication, [500]).then(blob => resolve(_fixTextBlobType(blob)), reject);
454
- });
455
- }
456
- /**
457
- * Gets the thumbnail of an AGO item.
458
- *
459
- * @param itemId Id of an item whose resources are sought
460
- * @param thumbnailUrlPart The partial name of the item's thumbnail as reported by the `thumbnail` property
461
- * in the item's base section
462
- * @param isGroup Switch indicating if the item is a group
463
- * @param authentication Credentials for the request to AGO
464
- * @returns Promise that will resolve with an image Blob or an AGO-style JSON failure response
465
- */
466
- export function getItemThumbnailAsFile(itemId, thumbnailUrlPart, isGroup, authentication) {
467
- return new Promise((resolve, reject) => {
468
- /* istanbul ignore else */
469
- if (!thumbnailUrlPart) {
470
- resolve(null);
471
- return;
472
- }
473
- const url = getItemThumbnailUrl(itemId, thumbnailUrlPart, isGroup, authentication);
474
- const iFilenameStart = thumbnailUrlPart.lastIndexOf("/") + 1;
475
- const filename = thumbnailUrlPart.substring(iFilenameStart);
476
- getBlobAsFile(url, filename, authentication, [400, 500]).then(resolve, reject);
477
- });
478
- }
479
- /**
480
- * Gets the URL to the thumbnail of an AGO item.
481
- *
482
- * @param itemId Id of an item whose resources are sought
483
- * @param thumbnailUrlPart The partial name of the item's thumbnail as reported by the `thumbnail` property
484
- * in the item's base section
485
- * @param isGroup Switch indicating if the item is a group
486
- * @param authentication Credentials for the request to AGO
487
- * @returns URL string
488
- */
489
- export function getItemThumbnailUrl(itemId, thumbnailUrlPart, isGroup, authentication) {
490
- return (checkUrlPathTermination(getPortalSharingUrlFromAuth(authentication)) +
491
- (isGroup ? "community/groups/" : "content/items/") +
492
- itemId +
493
- "/info/" +
494
- thumbnailUrlPart);
495
- }
496
- /**
497
- * Gets a JSON from a web site.
498
- *
499
- * @param url Address of JSON
500
- * @param authentication Credentials for the request
501
- * @returns Promise that will resolve with JSON
502
- */
503
- export function getJson(url, authentication) {
504
- // Get the blob from the URL
505
- const requestOptions = { httpMethod: "GET" };
506
- return getBlob(url, authentication, requestOptions)
507
- .then(blob => {
508
- // Reclassify text/plain blobs as needed
509
- return _fixTextBlobType(blob);
510
- })
511
- .then(adjustedBlob => {
512
- if (adjustedBlob.type === "application/json") {
513
- // Blob may be an error
514
- // eslint-disable-next-line @typescript-eslint/no-floating-promises
515
- return blobToJson(adjustedBlob);
516
- }
517
- else {
518
- return Promise.resolve(null);
519
- }
520
- });
521
- }
522
- /**
523
- * Extracts the portal sharing url from a supplied authentication.
524
- *
525
- * @param authentication Credentials for the request to AGO
526
- * @returns Portal sharing url to be used in API requests, defaulting to `https://www.arcgis.com/sharing/rest`
527
- */
528
- export function getPortalSharingUrlFromAuth(authentication) {
529
- // If auth was passed, use that portal
530
- return getProp(authentication, "portal") || "https://www.arcgis.com/sharing/rest";
531
- }
532
- /**
533
- * Extracts the portal url from a supplied authentication.
534
- *
535
- * @param authentication Credentials for the request to AGO
536
- * @returns Portal url to be used in API requests, defaulting to `https://www.arcgis.com`
537
- */
538
- export function getPortalUrlFromAuth(authentication) {
539
- return getPortalSharingUrlFromAuth(authentication).replace("/sharing/rest", "");
540
- }
541
- /**
542
- * Gets the ids of all Solution items associated with an AGO item via a Solution2Item relationship.
543
- *
544
- * @param itemId Id of an AGO item to query
545
- * @param authentication Credentials for the request
546
- * @returns Promise resolving to a list of Solution item ids
547
- */
548
- export function getSolutionsRelatedToAnItem(itemId, authentication) {
549
- // eslint-disable-next-line @typescript-eslint/no-floating-promises
550
- return getItemRelatedItems(itemId, "Solution2Item", "reverse", authentication).then((relationshipResponse) => {
551
- return relationshipResponse.relatedItems.map(item => item.id);
552
- });
553
- }
554
- export function getThumbnailFile(url, filename, authentication) {
555
- return new Promise(resolve => {
556
- getBlobAsFile(url, filename, authentication, [500]).then(resolve, () => resolve(null));
557
- });
558
- }
559
- // ------------------------------------------------------------------------------------------------------------------ //
560
- /**
561
- * Fixes the types of Blobs incorrectly typed as text/plain.
562
- *
563
- * @param blob Blob to check
564
- * @returns Promise resolving to original Blob, unless it's originally typed as text/plain but is
565
- * really JSON, ZIP, or XML
566
- * @private
567
- */
568
- export function _fixTextBlobType(blob) {
569
- return new Promise((resolve, reject) => {
570
- if (blob &&
571
- blob.size > 0 &&
572
- (blob.type.startsWith("text/plain") ||
573
- blob.type.startsWith("application/json"))) {
574
- blobToText(blob).then(blobText => {
575
- // Convertible to JSON?
576
- try {
577
- JSON.parse(blobText);
578
- // Yes; reclassify as JSON
579
- resolve(new Blob([blob], { type: "application/json" }));
580
- }
581
- catch (ignored) {
582
- // Nope; test for ZIP file
583
- if (blobText.length > 4 &&
584
- blobText.substr(0, 4) === ZIP_FILE_HEADER_SIGNATURE) {
585
- // Yes; reclassify as ZIP
586
- resolve(new Blob([blob], { type: "application/zip" }));
587
- }
588
- else if (blobText.startsWith("<")) {
589
- // Reclassify as XML; since the blob started out as text/plain, it's more likely that is
590
- // meant to be human-readable, so we'll use text/xml instead of application/xml
591
- resolve(new Blob([blob], { type: "text/xml" }));
592
- }
593
- else {
594
- // Leave as text
595
- resolve(blob);
596
- }
597
- }
598
- },
599
- // Faulty blob
600
- reject);
601
- }
602
- else {
603
- // Empty or not typed as plain text, so simply return
604
- if (blob) {
605
- resolve(blob);
606
- }
607
- else {
608
- reject();
609
- }
610
- }
611
- });
612
- }
613
- /**
614
- * Gets some of the ids of the dependencies (contents) of an AGO group.
615
- *
616
- * @param groupId Id of a group whose contents are sought
617
- * @param pagingParams Structure with start and num properties for the tranche to fetch
618
- * @param authentication Credentials for the request to AGO
619
- * @returns A promise that will resolve with list of dependent ids or an empty list
620
- * @private
621
- */
622
- export function _getGroupContentsTranche(groupId, pagingParams, authentication) {
623
- return new Promise((resolve, reject) => {
624
- // Fetch group items
625
- const pagingRequest = {
626
- paging: pagingParams,
627
- authentication: authentication
628
- };
629
- getGroupContent(groupId, pagingRequest).then(contents => {
630
- if (contents.num > 0) {
631
- // Extract the list of content ids from the JSON returned
632
- const trancheIds = contents.items.map((item) => item.id);
633
- // Are there more contents to fetch?
634
- if (contents.nextStart > 0) {
635
- pagingRequest.paging.start = contents.nextStart;
636
- _getGroupContentsTranche(groupId, pagingParams, authentication).then((allSubsequentTrancheIds) => {
637
- // Append all of the following tranches to the current tranche and return it
638
- resolve(trancheIds.concat(allSubsequentTrancheIds));
639
- }, reject);
640
- }
641
- else {
642
- resolve(trancheIds);
643
- }
644
- }
645
- else {
646
- resolve([]);
647
- }
648
- }, reject);
649
- });
650
- }
651
- /**
652
- * Gets some of the related items of an AGO item.
653
- *
654
- * @param requestOptions Item id, relationship type, relationship direction, authentication, and paging
655
- * @returns A promise that will resolve with the list of related items
656
- * @private
657
- */
658
- function _getItemRelatedItemsTranche(requestOptions) {
659
- return new Promise(resolve => {
660
- const response = {
661
- total: 0,
662
- relatedItems: []
663
- };
664
- getRelatedItems(requestOptions).then(
665
- // Have to use `any` because `IGetRelatedItemsResponse` doesn't include all of the response properties
666
- (results) => {
667
- // Are there any results?
668
- if (results.aggregations.total.count > 0) {
669
- response.total = results.aggregations.total.count;
670
- response.relatedItems = results.relatedItems;
671
- // Are there more items to fetch?
672
- if (results.nextkey) {
673
- requestOptions.params.start += requestOptions.params.num;
674
- requestOptions.params.nextkey = results.nextkey;
675
- // eslint-disable-next-line @typescript-eslint/no-floating-promises
676
- _getItemRelatedItemsTranche(requestOptions).then((allSubsequentResults) => {
677
- // Append all of the following tranches to the current tranche and return it
678
- response.total += allSubsequentResults.total;
679
- response.relatedItems = response.relatedItems.concat(allSubsequentResults.relatedItems);
680
- resolve(response);
681
- });
682
- }
683
- else {
684
- resolve(response);
685
- }
686
- }
687
- else {
688
- resolve(response);
689
- }
690
- }, () => {
691
- resolve(response);
692
- });
693
- });
694
- }
695
- /**
696
- * Gets some of the resources of an AGO item.
697
- *
698
- * @param itemId Id of an item whose resources are sought
699
- * @param pagingParams Structure with start and num properties for the tranche to fetch
700
- * @param authentication Credentials for the request to AGO
701
- * @returns Promise that will resolve with a list of File promises or an AGO-style JSON failure response
702
- * @private
703
- */
704
- export function _getItemResourcesTranche(itemId, pagingParams, authentication) {
705
- return new Promise((resolve, reject) => {
706
- // Fetch resources
707
- const portalSharingUrl = getPortalSharingUrlFromAuth(authentication);
708
- const trancheUrl = `${portalSharingUrl}/content/items/${itemId}/resources`;
709
- const itemResourcesDef = [];
710
- const options = {
711
- params: {
712
- ...pagingParams
713
- },
714
- authentication: authentication
715
- };
716
- request(trancheUrl, options).then(contents => {
717
- if (contents.num > 0) {
718
- // Extract the list of resource filenames from the JSON returned
719
- contents.resources.forEach((resource) => {
720
- const itemResourceUrl = `${portalSharingUrl}/content/items/${itemId}/resources/${resource.resource}`;
721
- itemResourcesDef.push(getBlobAsFile(itemResourceUrl, resource.resource, authentication));
722
- });
723
- // Are there more resources to fetch?
724
- if (contents.nextStart > 0) {
725
- pagingParams.start = contents.nextStart;
726
- _getItemResourcesTranche(itemId, pagingParams, authentication).then((allSubsequentTrancheDefs) => {
727
- // Append all of the following tranches to the current tranche and return it
728
- resolve(itemResourcesDef.concat(allSubsequentTrancheDefs));
729
- }, reject);
730
- }
731
- else {
732
- resolve(itemResourcesDef);
733
- }
734
- }
735
- else {
736
- resolve([]);
737
- }
738
- }, reject);
739
- });
740
- }
741
- /**
742
- * Retrieves the default basemap for the given & basemapGalleryGroupQuery, basemapTitle
743
- *
744
- * @param {string} basemapGalleryGroupQuery The default basemap group query
745
- * @param {string} basemapTitle The default basemap title
746
- * @param {UserSession} authentication The session info
747
- * @returns {IItem}
748
- */
749
- export function getPortalDefaultBasemap(basemapGalleryGroupQuery, basemapTitle, authentication) {
750
- return searchGroups(basemapGalleryGroupQuery, authentication, { num: 1 })
751
- .then(({ results: [basemapGroup] }) => {
752
- if (!basemapGroup) {
753
- throw new Error("No basemap group found");
754
- }
755
- return searchGroupContents(basemapGroup.id, `title:${basemapTitle}`, authentication, { num: 1 });
756
- })
757
- .then(({ results: [defaultBasemap] }) => {
758
- if (!defaultBasemap) {
759
- throw new Error("No basemap found");
760
- }
761
- return defaultBasemap;
762
- });
763
- }
1
+ /** @license
2
+ * Copyright 2018 Esri
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ /**
17
+ * Provides common item fetch functions involving the arcgis-rest-js library.
18
+ *
19
+ * @module restHelpersGet
20
+ */
21
+ import { blobToFile, blobToJson, blobToText, checkUrlPathTermination, getProp } from "./generalHelpers";
22
+ import { getGroup, getGroupCategorySchema as portalGetGroupCategorySchema, getGroupContent, getItem, getItemResources as portalGetItemResources, getPortal as portalGetPortal, getRelatedItems } from "@esri/arcgis-rest-portal";
23
+ import { request } from "@esri/arcgis-rest-request";
24
+ import { getBlob } from "./resources/get-blob";
25
+ import { searchGroups, searchGroupContents } from "./restHelpers";
26
+ // ------------------------------------------------------------------------------------------------------------------ //
27
+ const ZIP_FILE_HEADER_SIGNATURE = "PK";
28
+ export function checkJsonForError(json) {
29
+ return typeof json?.error !== "undefined";
30
+ }
31
+ export function getPortal(id, authentication) {
32
+ const requestOptions = {
33
+ authentication: authentication
34
+ };
35
+ return portalGetPortal(id, requestOptions);
36
+ }
37
+ export function getUser(authentication) {
38
+ return authentication.getUser();
39
+ }
40
+ export function getUsername(authentication) {
41
+ return new Promise((resolve, reject) => {
42
+ getUser(authentication).then((user) => resolve(user.username), reject);
43
+ });
44
+ }
45
+ export function getFoldersAndGroups(authentication) {
46
+ return new Promise((resolve, reject) => {
47
+ const requestOptions = {
48
+ httpMethod: "GET",
49
+ authentication: authentication,
50
+ rawResponse: false
51
+ };
52
+ // Folders
53
+ const foldersUrl = `${authentication.portal}/content/users/${encodeURIComponent(authentication.username)}`;
54
+ // Groups
55
+ const groupsUrl = `${authentication.portal}/community/users/${encodeURIComponent(authentication.username)}`;
56
+ Promise.all([
57
+ request(foldersUrl, requestOptions),
58
+ request(groupsUrl, requestOptions)
59
+ ]).then(responses => {
60
+ resolve({
61
+ folders: responses[0].folders || [],
62
+ groups: responses[1].groups || []
63
+ });
64
+ }, e => reject(e));
65
+ });
66
+ }
67
+ /**
68
+ * Gets a Blob from a web site and casts it as a file using the supplied name.
69
+ *
70
+ * @param url Address of Blob
71
+ * @param filename Name to use for file
72
+ * @param authentication Credentials for the request
73
+ * @returns Promise that will resolve with a File, undefined if the Blob is null, or an AGO-style JSON failure response
74
+ */
75
+ export function getBlobAsFile(url, filename, authentication, ignoreErrors = [], mimeType) {
76
+ return new Promise((resolve, reject) => {
77
+ // Get the blob from the URL
78
+ getBlobCheckForError(url, authentication, ignoreErrors).then(blob => !blob ? resolve(null) : resolve(blobToFile(blob, filename, mimeType)), reject);
79
+ });
80
+ }
81
+ /**
82
+ * Gets a Blob from a web site and checks for a JSON error packet in the Blob.
83
+ *
84
+ * @param url Address of Blob
85
+ * @param authentication Credentials for the request
86
+ * @param ignoreErrors List of HTTP error codes that should be ignored
87
+ * @returns Promise that will resolve with Blob or an AGO-REST JSON failure response
88
+ */
89
+ export function getBlobCheckForError(url, authentication, ignoreErrors = []) {
90
+ return new Promise((resolve, reject) => {
91
+ // Get the blob from the URL
92
+ getBlob(url, authentication).then(blob => {
93
+ // Reclassify text/plain blobs as needed
94
+ _fixTextBlobType(blob).then(adjustedBlob => {
95
+ if (adjustedBlob.type === "application/json") {
96
+ // Blob may be an error
97
+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
98
+ blobToJson(adjustedBlob).then((json) => {
99
+ // Check for valid JSON with an error
100
+ if (json?.error) {
101
+ const code = json.error.code;
102
+ if (code !== undefined && ignoreErrors.indexOf(code) >= 0) {
103
+ resolve(null); // Error, but ignored
104
+ }
105
+ else {
106
+ reject(json); // Other error; fail with error
107
+ }
108
+ }
109
+ else {
110
+ resolve(adjustedBlob);
111
+ }
112
+ });
113
+ }
114
+ else {
115
+ resolve(adjustedBlob);
116
+ }
117
+ }, reject);
118
+ }, reject);
119
+ });
120
+ }
121
+ /**
122
+ * Extracts the text in a url between the last forward slash and the beginning of the url's parameters.
123
+ *
124
+ * @param url URL to work with
125
+ * @returns Text extracted; empty if url ends with a forward slash or has a "?" immediately after the last
126
+ * forward slash
127
+ */
128
+ export function getFilenameFromUrl(url) {
129
+ if (!url) {
130
+ return "";
131
+ }
132
+ let iParamsStart = url.indexOf("?");
133
+ /* istanbul ignore else */
134
+ if (iParamsStart < 0) {
135
+ iParamsStart = url.length;
136
+ }
137
+ const iFilenameStart = url.lastIndexOf("/", iParamsStart) + 1;
138
+ return iFilenameStart < iParamsStart
139
+ ? url.substring(iFilenameStart, iParamsStart)
140
+ : "";
141
+ }
142
+ /**
143
+ * Gets the primary information of an AGO group.
144
+ *
145
+ * @param groupId Id of an group whose primary information is sought
146
+ * @param authentication Credentials for the request to AGO
147
+ * @returns A promise that will resolve with group's JSON or error JSON or throws ArcGISRequestError in case of HTTP error
148
+ * or response error code
149
+ */
150
+ export function getGroupBase(groupId, authentication) {
151
+ const requestOptions = {
152
+ authentication: authentication
153
+ };
154
+ return getGroup(groupId, requestOptions);
155
+ }
156
+ /**
157
+ * Gets the category schema set on a group.
158
+ *
159
+ * @param groupId Id of an group whose category schema information is sought
160
+ * @param authentication Credentials for the request to AGO
161
+ * @returns A promise that will resolve with JSON of group's category schema
162
+ * @see https://developers.arcgis.com/rest/users-groups-and-items/group-category-schema.htm
163
+ */
164
+ export function getGroupCategorySchema(groupId, authentication) {
165
+ const requestOptions = {
166
+ authentication: authentication
167
+ };
168
+ return portalGetGroupCategorySchema(groupId, requestOptions);
169
+ }
170
+ /**
171
+ * Gets the ids of the dependencies (contents) of an AGO group.
172
+ *
173
+ * @param groupId Id of a group whose contents are sought
174
+ * @param authentication Credentials for the request to AGO
175
+ * @returns A promise that will resolve with list of dependent ids or an empty list
176
+ */
177
+ export function getGroupContents(groupId, authentication) {
178
+ return new Promise((resolve, reject) => {
179
+ const pagingParams = {
180
+ start: 1,
181
+ num: 100 // max allowed by REST API
182
+ };
183
+ // Fetch group items
184
+ _getGroupContentsTranche(groupId, pagingParams, authentication).then(contents => {
185
+ resolve(contents);
186
+ }, reject);
187
+ });
188
+ }
189
+ /**
190
+ * Gets the primary information of an AGO item.
191
+ *
192
+ * @param itemId Id of an item whose primary information is sought
193
+ * @param authentication Credentials for the request to AGO
194
+ * @returns A promise that will resolve with item's JSON or error JSON or throws ArcGISRequestError in case of HTTP error
195
+ * or response error code
196
+ */
197
+ export function getItemBase(itemId, authentication) {
198
+ const itemParam = {
199
+ authentication: authentication
200
+ };
201
+ return getItem(itemId, itemParam);
202
+ }
203
+ /**
204
+ * Gets the data information of an AGO item in its raw (Blob) form and casts it as a file using the supplied name.
205
+ *
206
+ * @param itemId Id of an item whose data information is sought
207
+ * @param filename Name to use for file
208
+ * @param authentication Credentials for the request to AGO
209
+ * @returns Promise that will resolve with a File, undefined if the Blob is null, or an AGO-style JSON failure response
210
+ */
211
+ export function getItemDataAsFile(itemId, filename, authentication) {
212
+ return new Promise(resolve => {
213
+ getItemDataBlob(itemId, authentication).then(blob => resolve(blobToFile(blob, filename)), () => resolve(null));
214
+ });
215
+ }
216
+ /**
217
+ * Gets the data information of an AGO item in its JSON form.
218
+ *
219
+ * @param itemId Id of an item whose data information is sought
220
+ * @param filename Name to use for file
221
+ * @param authentication Credentials for the request to AGO
222
+ * @returns Promise that will resolve with JSON, or an AGO-style JSON failure response
223
+ */
224
+ export function getItemDataAsJson(itemId, authentication) {
225
+ return new Promise(resolve => {
226
+ getItemDataBlob(itemId, authentication).then(blob => resolve(blobToJson(blob)), () => resolve(null));
227
+ });
228
+ }
229
+ /**
230
+ * Gets the data information of an AGO item in its raw (Blob) form.
231
+ *
232
+ * @param itemId Id of an item whose data information is sought
233
+ * @param authentication Credentials for the request to AGO
234
+ * @returns A promise that will resolve with the data Blob or null if the item doesn't have a data section
235
+ */
236
+ export function getItemDataBlob(itemId, authentication) {
237
+ return new Promise(resolve => {
238
+ const url = getItemDataBlobUrl(itemId, authentication);
239
+ getBlobCheckForError(url, authentication, [400, 500]).then(blob => resolve(_fixTextBlobType(blob)), () => resolve(null));
240
+ });
241
+ }
242
+ /**
243
+ * Gets the URL to the data information of an AGO item in its raw (Blob) form.
244
+ *
245
+ * @param itemId Id of an item whose data information is sought
246
+ * @param authentication Credentials for the request to AGO
247
+ * @returns URL string
248
+ */
249
+ export function getItemDataBlobUrl(itemId, authentication) {
250
+ return `${getPortalSharingUrlFromAuth(authentication)}/content/items/${itemId}/data`;
251
+ }
252
+ /**
253
+ * Gets the URL to an information item in an AGO item.
254
+ *
255
+ * @param itemId Id of an item whose data information is sought
256
+ * @param authentication Credentials for the request to AGO
257
+ * @returns URL string
258
+ */
259
+ export function getItemInfoFileUrlPrefix(itemId, authentication) {
260
+ return `${getPortalSharingUrlFromAuth(authentication)}/content/items/${itemId}/info/`;
261
+ }
262
+ /**
263
+ * Gets the metadata information of an AGO item.
264
+ *
265
+ * @param itemId Id of an item whose data information is sought
266
+ * @param authentication Credentials for the request to AGO
267
+ * @returns Promise that will resolve with `undefined` or a File containing the metadata
268
+ */
269
+ export function getItemMetadataAsFile(itemId, authentication) {
270
+ return new Promise(resolve => {
271
+ getItemMetadataBlob(itemId, authentication).then(blob => {
272
+ if (!blob || (blob && blob.type.startsWith("application/json"))) {
273
+ resolve(null); // JSON error
274
+ }
275
+ else {
276
+ resolve(blobToFile(blob, "metadata.xml"));
277
+ }
278
+ }, () => resolve(null));
279
+ });
280
+ }
281
+ /**
282
+ * Gets the metadata information of an AGO item.
283
+ *
284
+ * @param itemId Id of an item whose data information is sought
285
+ * @param authentication Credentials for the request to AGO
286
+ * @returns A promise that will resolve with the metadata Blob or null if the item doesn't have a metadata file
287
+ */
288
+ export function getItemMetadataBlob(itemId, authentication) {
289
+ return new Promise((resolve, reject) => {
290
+ const url = getItemMetadataBlobUrl(itemId, authentication);
291
+ getBlobCheckForError(url, authentication, [400]).then(resolve, reject);
292
+ });
293
+ }
294
+ /**
295
+ * Gets the URL to the metadata information of an AGO item.
296
+ *
297
+ * @param itemId Id of an item whose data information is sought
298
+ * @param authentication Credentials for the request to AGO
299
+ * @returns URL string
300
+ */
301
+ export function getItemMetadataBlobUrl(itemId, authentication) {
302
+ return (getItemInfoFileUrlPrefix(itemId, authentication) + "metadata/metadata.xml");
303
+ }
304
+ /**
305
+ * Gets the related items of an AGO item.
306
+ *
307
+ * @param itemId Id of an item whose related items are sought
308
+ * @param relationshipType Tag for relationship type, e.g., "Solution2Item"
309
+ * @param direction Relationship direction
310
+ * @param authentication Credentials for the request to AGO
311
+ * @param num Number of related items to request per batch; maximum is 100
312
+ * @returns A promise that will resolve with the list of related items
313
+ */
314
+ export function getItemRelatedItems(itemId, relationshipType, direction, authentication, num = 100) {
315
+ return new Promise(resolve => {
316
+ const itemRelatedItemsParam = {
317
+ id: itemId,
318
+ relationshipType,
319
+ authentication: authentication,
320
+ params: {
321
+ direction,
322
+ start: 1,
323
+ num
324
+ }
325
+ };
326
+ // Fetch related items
327
+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
328
+ _getItemRelatedItemsTranche(itemRelatedItemsParam).then(response => {
329
+ resolve(response);
330
+ });
331
+ });
332
+ }
333
+ /**
334
+ * Gets all of the related items of an AGO item in the specified direction.
335
+ *
336
+ * @param itemId Id of an item whose related items are sought
337
+ * @param direction
338
+ * @param authentication Credentials for the request to AGO
339
+ * @returns A promise that will resolve with a list of IRelatedItems
340
+ */
341
+ export function getItemRelatedItemsInSameDirection(itemId, direction, authentication) {
342
+ return new Promise(resolve => {
343
+ const relationshipTypes = [
344
+ // from ItemRelationshipType
345
+ "APIKey2Item",
346
+ "Area2CustomPackage",
347
+ "Area2Package",
348
+ "Item2Attachment",
349
+ "Item2Report",
350
+ "Listed2Provisioned",
351
+ "Map2AppConfig",
352
+ "Map2Area",
353
+ "Map2FeatureCollection",
354
+ "Map2Service",
355
+ "MobileApp2Code",
356
+ "Service2Data",
357
+ "Service2Layer",
358
+ "Service2Route",
359
+ "Service2Service",
360
+ "Service2Style",
361
+ "Solution2Item",
362
+ "Style2Style",
363
+ "Survey2Data",
364
+ "Survey2Service",
365
+ "SurveyAddIn2Data",
366
+ "Theme2Story",
367
+ "TrackView2Map",
368
+ "WebStyle2DesktopStyle",
369
+ "WMA2Code",
370
+ "WorkforceMap2FeatureService"
371
+ ];
372
+ const relatedItemDefs = relationshipTypes.map(relationshipType => getItemRelatedItems(itemId, relationshipType, direction, authentication));
373
+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
374
+ Promise.all(relatedItemDefs).then((relationshipResponses) => {
375
+ const relatedItems = [];
376
+ for (let i = 0; i < relationshipTypes.length; ++i) {
377
+ if (relationshipResponses[i].total > 0) {
378
+ relatedItems.push({
379
+ relationshipType: relationshipTypes[i],
380
+ relatedItemIds: relationshipResponses[i].relatedItems.map(item => item.id)
381
+ });
382
+ }
383
+ }
384
+ resolve(relatedItems);
385
+ });
386
+ });
387
+ }
388
+ export function getItemResources(id, authentication) {
389
+ return new Promise(resolve => {
390
+ const requestOptions = {
391
+ authentication: authentication
392
+ };
393
+ portalGetItemResources(id, requestOptions).then(resolve, () => {
394
+ resolve({
395
+ total: 0,
396
+ start: 1,
397
+ num: 0,
398
+ nextStart: -1,
399
+ resources: []
400
+ });
401
+ });
402
+ });
403
+ }
404
+ /**
405
+ * Gets the resources of an AGO item.
406
+ *
407
+ * @param itemId Id of an item whose resources are sought
408
+ * @param authentication Credentials for the request to AGO
409
+ * @returns Promise that will resolve with a list of Files or an AGO-style JSON failure response
410
+ */
411
+ export function getItemResourcesFiles(itemId, authentication) {
412
+ return new Promise((resolve, reject) => {
413
+ const pagingParams = {
414
+ start: 1,
415
+ num: 100 // max allowed by REST API
416
+ };
417
+ // Fetch resources
418
+ _getItemResourcesTranche(itemId, pagingParams, authentication).then(itemResourcesDef => {
419
+ Promise.all(itemResourcesDef).then(resolve, reject);
420
+ }, reject);
421
+ });
422
+ }
423
+ /**
424
+ * Gets all of the items associated with a Solution via a Solution2Item relationship.
425
+ *
426
+ * @param solutionItemId Id of a deployed Solution
427
+ * @param authentication Credentials for the request
428
+ * @returns Promise resolving to a list of detailed item information
429
+ */
430
+ export function getItemsRelatedToASolution(solutionItemId, authentication) {
431
+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
432
+ return getItemRelatedItems(solutionItemId, "Solution2Item", "forward", authentication).then((relationshipResponse) => {
433
+ return relationshipResponse.relatedItems;
434
+ });
435
+ }
436
+ /**
437
+ * Gets the thumbnail of an AGO item.
438
+ *
439
+ * @param itemId Id of an item whose resources are sought
440
+ * @param thumbnailUrlPart The partial name of the item's thumbnail as reported by the `thumbnail` property
441
+ * in the item's base section
442
+ * @param isGroup Switch indicating if the item is a group
443
+ * @param authentication Credentials for the request to AGO
444
+ * @returns Promise that will resolve with an image Blob or an AGO-style JSON failure response
445
+ */
446
+ export function getItemThumbnail(itemId, thumbnailUrlPart, isGroup, authentication) {
447
+ return new Promise((resolve, reject) => {
448
+ if (!thumbnailUrlPart) {
449
+ resolve(null);
450
+ return;
451
+ }
452
+ const url = getItemThumbnailUrl(itemId, thumbnailUrlPart, isGroup, authentication);
453
+ getBlobCheckForError(url, authentication, [500]).then(blob => resolve(_fixTextBlobType(blob)), reject);
454
+ });
455
+ }
456
+ /**
457
+ * Gets the thumbnail of an AGO item.
458
+ *
459
+ * @param itemId Id of an item whose resources are sought
460
+ * @param thumbnailUrlPart The partial name of the item's thumbnail as reported by the `thumbnail` property
461
+ * in the item's base section
462
+ * @param isGroup Switch indicating if the item is a group
463
+ * @param authentication Credentials for the request to AGO
464
+ * @returns Promise that will resolve with an image Blob or an AGO-style JSON failure response
465
+ */
466
+ export function getItemThumbnailAsFile(itemId, thumbnailUrlPart, isGroup, authentication) {
467
+ return new Promise((resolve, reject) => {
468
+ /* istanbul ignore else */
469
+ if (!thumbnailUrlPart) {
470
+ resolve(null);
471
+ return;
472
+ }
473
+ const url = getItemThumbnailUrl(itemId, thumbnailUrlPart, isGroup, authentication);
474
+ const iFilenameStart = thumbnailUrlPart.lastIndexOf("/") + 1;
475
+ const filename = thumbnailUrlPart.substring(iFilenameStart);
476
+ getBlobAsFile(url, filename, authentication, [400, 500]).then(resolve, reject);
477
+ });
478
+ }
479
+ /**
480
+ * Gets the URL to the thumbnail of an AGO item.
481
+ *
482
+ * @param itemId Id of an item whose resources are sought
483
+ * @param thumbnailUrlPart The partial name of the item's thumbnail as reported by the `thumbnail` property
484
+ * in the item's base section
485
+ * @param isGroup Switch indicating if the item is a group
486
+ * @param authentication Credentials for the request to AGO
487
+ * @returns URL string
488
+ */
489
+ export function getItemThumbnailUrl(itemId, thumbnailUrlPart, isGroup, authentication) {
490
+ return (checkUrlPathTermination(getPortalSharingUrlFromAuth(authentication)) +
491
+ (isGroup ? "community/groups/" : "content/items/") +
492
+ itemId +
493
+ "/info/" +
494
+ thumbnailUrlPart);
495
+ }
496
+ /**
497
+ * Gets a JSON from a web site.
498
+ *
499
+ * @param url Address of JSON
500
+ * @param authentication Credentials for the request
501
+ * @returns Promise that will resolve with JSON
502
+ */
503
+ export function getJson(url, authentication) {
504
+ // Get the blob from the URL
505
+ const requestOptions = { httpMethod: "GET" };
506
+ return getBlob(url, authentication, requestOptions)
507
+ .then(blob => {
508
+ // Reclassify text/plain blobs as needed
509
+ return _fixTextBlobType(blob);
510
+ })
511
+ .then(adjustedBlob => {
512
+ if (adjustedBlob.type === "application/json") {
513
+ // Blob may be an error
514
+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
515
+ return blobToJson(adjustedBlob);
516
+ }
517
+ else {
518
+ return Promise.resolve(null);
519
+ }
520
+ });
521
+ }
522
+ /**
523
+ * Extracts the portal sharing url from a supplied authentication.
524
+ *
525
+ * @param authentication Credentials for the request to AGO
526
+ * @returns Portal sharing url to be used in API requests, defaulting to `https://www.arcgis.com/sharing/rest`
527
+ */
528
+ export function getPortalSharingUrlFromAuth(authentication) {
529
+ // If auth was passed, use that portal
530
+ return getProp(authentication, "portal") || "https://www.arcgis.com/sharing/rest";
531
+ }
532
+ /**
533
+ * Extracts the portal url from a supplied authentication.
534
+ *
535
+ * @param authentication Credentials for the request to AGO
536
+ * @returns Portal url to be used in API requests, defaulting to `https://www.arcgis.com`
537
+ */
538
+ export function getPortalUrlFromAuth(authentication) {
539
+ return getPortalSharingUrlFromAuth(authentication).replace("/sharing/rest", "");
540
+ }
541
+ /**
542
+ * Gets the ids of all Solution items associated with an AGO item via a Solution2Item relationship.
543
+ *
544
+ * @param itemId Id of an AGO item to query
545
+ * @param authentication Credentials for the request
546
+ * @returns Promise resolving to a list of Solution item ids
547
+ */
548
+ export function getSolutionsRelatedToAnItem(itemId, authentication) {
549
+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
550
+ return getItemRelatedItems(itemId, "Solution2Item", "reverse", authentication).then((relationshipResponse) => {
551
+ return relationshipResponse.relatedItems.map(item => item.id);
552
+ });
553
+ }
554
+ export function getThumbnailFile(url, filename, authentication) {
555
+ return new Promise(resolve => {
556
+ getBlobAsFile(url, filename, authentication, [500]).then(resolve, () => resolve(null));
557
+ });
558
+ }
559
+ // ------------------------------------------------------------------------------------------------------------------ //
560
+ /**
561
+ * Fixes the types of Blobs incorrectly typed as text/plain.
562
+ *
563
+ * @param blob Blob to check
564
+ * @returns Promise resolving to original Blob, unless it's originally typed as text/plain but is
565
+ * really JSON, ZIP, or XML
566
+ * @private
567
+ */
568
+ export function _fixTextBlobType(blob) {
569
+ return new Promise((resolve, reject) => {
570
+ if (blob &&
571
+ blob.size > 0 &&
572
+ (blob.type.startsWith("text/plain") ||
573
+ blob.type.startsWith("application/json"))) {
574
+ blobToText(blob).then(blobText => {
575
+ // Convertible to JSON?
576
+ try {
577
+ JSON.parse(blobText);
578
+ // Yes; reclassify as JSON
579
+ resolve(new Blob([blob], { type: "application/json" }));
580
+ }
581
+ catch (ignored) {
582
+ // Nope; test for ZIP file
583
+ if (blobText.length > 4 &&
584
+ blobText.substr(0, 4) === ZIP_FILE_HEADER_SIGNATURE) {
585
+ // Yes; reclassify as ZIP
586
+ resolve(new Blob([blob], { type: "application/zip" }));
587
+ }
588
+ else if (blobText.startsWith("<")) {
589
+ // Reclassify as XML; since the blob started out as text/plain, it's more likely that is
590
+ // meant to be human-readable, so we'll use text/xml instead of application/xml
591
+ resolve(new Blob([blob], { type: "text/xml" }));
592
+ }
593
+ else {
594
+ // Leave as text
595
+ resolve(blob);
596
+ }
597
+ }
598
+ },
599
+ // Faulty blob
600
+ reject);
601
+ }
602
+ else {
603
+ // Empty or not typed as plain text, so simply return
604
+ if (blob) {
605
+ resolve(blob);
606
+ }
607
+ else {
608
+ reject();
609
+ }
610
+ }
611
+ });
612
+ }
613
+ /**
614
+ * Gets some of the ids of the dependencies (contents) of an AGO group.
615
+ *
616
+ * @param groupId Id of a group whose contents are sought
617
+ * @param pagingParams Structure with start and num properties for the tranche to fetch
618
+ * @param authentication Credentials for the request to AGO
619
+ * @returns A promise that will resolve with list of dependent ids or an empty list
620
+ * @private
621
+ */
622
+ export function _getGroupContentsTranche(groupId, pagingParams, authentication) {
623
+ return new Promise((resolve, reject) => {
624
+ // Fetch group items
625
+ const pagingRequest = {
626
+ paging: pagingParams,
627
+ authentication: authentication
628
+ };
629
+ getGroupContent(groupId, pagingRequest).then(contents => {
630
+ if (contents.num > 0) {
631
+ // Extract the list of content ids from the JSON returned
632
+ const trancheIds = contents.items.map((item) => item.id);
633
+ // Are there more contents to fetch?
634
+ if (contents.nextStart > 0) {
635
+ pagingRequest.paging.start = contents.nextStart;
636
+ _getGroupContentsTranche(groupId, pagingParams, authentication).then((allSubsequentTrancheIds) => {
637
+ // Append all of the following tranches to the current tranche and return it
638
+ resolve(trancheIds.concat(allSubsequentTrancheIds));
639
+ }, reject);
640
+ }
641
+ else {
642
+ resolve(trancheIds);
643
+ }
644
+ }
645
+ else {
646
+ resolve([]);
647
+ }
648
+ }, reject);
649
+ });
650
+ }
651
+ /**
652
+ * Gets some of the related items of an AGO item.
653
+ *
654
+ * @param requestOptions Item id, relationship type, relationship direction, authentication, and paging
655
+ * @returns A promise that will resolve with the list of related items
656
+ * @private
657
+ */
658
+ function _getItemRelatedItemsTranche(requestOptions) {
659
+ return new Promise(resolve => {
660
+ const response = {
661
+ total: 0,
662
+ relatedItems: []
663
+ };
664
+ getRelatedItems(requestOptions).then(
665
+ // Have to use `any` because `IGetRelatedItemsResponse` doesn't include all of the response properties
666
+ (results) => {
667
+ // Are there any results?
668
+ if (results.aggregations.total.count > 0) {
669
+ response.total = results.aggregations.total.count;
670
+ response.relatedItems = results.relatedItems;
671
+ // Are there more items to fetch?
672
+ if (results.nextkey) {
673
+ requestOptions.params.start += requestOptions.params.num;
674
+ requestOptions.params.nextkey = results.nextkey;
675
+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
676
+ _getItemRelatedItemsTranche(requestOptions).then((allSubsequentResults) => {
677
+ // Append all of the following tranches to the current tranche and return it
678
+ response.total += allSubsequentResults.total;
679
+ response.relatedItems = response.relatedItems.concat(allSubsequentResults.relatedItems);
680
+ resolve(response);
681
+ });
682
+ }
683
+ else {
684
+ resolve(response);
685
+ }
686
+ }
687
+ else {
688
+ resolve(response);
689
+ }
690
+ }, () => {
691
+ resolve(response);
692
+ });
693
+ });
694
+ }
695
+ /**
696
+ * Gets some of the resources of an AGO item.
697
+ *
698
+ * @param itemId Id of an item whose resources are sought
699
+ * @param pagingParams Structure with start and num properties for the tranche to fetch
700
+ * @param authentication Credentials for the request to AGO
701
+ * @returns Promise that will resolve with a list of File promises or an AGO-style JSON failure response
702
+ * @private
703
+ */
704
+ export function _getItemResourcesTranche(itemId, pagingParams, authentication) {
705
+ return new Promise((resolve, reject) => {
706
+ // Fetch resources
707
+ const portalSharingUrl = getPortalSharingUrlFromAuth(authentication);
708
+ const trancheUrl = `${portalSharingUrl}/content/items/${itemId}/resources`;
709
+ const itemResourcesDef = [];
710
+ const options = {
711
+ params: {
712
+ ...pagingParams
713
+ },
714
+ authentication: authentication
715
+ };
716
+ request(trancheUrl, options).then(contents => {
717
+ if (contents.num > 0) {
718
+ // Extract the list of resource filenames from the JSON returned
719
+ contents.resources.forEach((resource) => {
720
+ const itemResourceUrl = `${portalSharingUrl}/content/items/${itemId}/resources/${resource.resource}`;
721
+ itemResourcesDef.push(getBlobAsFile(itemResourceUrl, resource.resource, authentication));
722
+ });
723
+ // Are there more resources to fetch?
724
+ if (contents.nextStart > 0) {
725
+ pagingParams.start = contents.nextStart;
726
+ _getItemResourcesTranche(itemId, pagingParams, authentication).then((allSubsequentTrancheDefs) => {
727
+ // Append all of the following tranches to the current tranche and return it
728
+ resolve(itemResourcesDef.concat(allSubsequentTrancheDefs));
729
+ }, reject);
730
+ }
731
+ else {
732
+ resolve(itemResourcesDef);
733
+ }
734
+ }
735
+ else {
736
+ resolve([]);
737
+ }
738
+ }, reject);
739
+ });
740
+ }
741
+ /**
742
+ * Retrieves the default basemap for the given & basemapGalleryGroupQuery, basemapTitle
743
+ *
744
+ * @param {string} basemapGalleryGroupQuery The default basemap group query
745
+ * @param {string} basemapTitle The default basemap title
746
+ * @param {UserSession} authentication The session info
747
+ * @returns {IItem}
748
+ */
749
+ export function getPortalDefaultBasemap(basemapGalleryGroupQuery, basemapTitle, authentication) {
750
+ return searchGroups(basemapGalleryGroupQuery, authentication, { num: 1 })
751
+ .then(({ results: [basemapGroup] }) => {
752
+ if (!basemapGroup) {
753
+ throw new Error("No basemap group found");
754
+ }
755
+ return searchGroupContents(basemapGroup.id, `title:${basemapTitle}`, authentication, { num: 1 });
756
+ })
757
+ .then(({ results: [defaultBasemap] }) => {
758
+ if (!defaultBasemap) {
759
+ throw new Error("No basemap found");
760
+ }
761
+ return defaultBasemap;
762
+ });
763
+ }
764
764
  //# sourceMappingURL=restHelpersGet.js.map