aspernaturalias 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (370) hide show
  1. package/.prettierignore +2 -0
  2. package/.travis.yml +31 -0
  3. package/.vscode/launch.json +24 -0
  4. package/.vscode/settings.json +3 -0
  5. package/codecov.yml +3 -0
  6. package/demos/ago-node-cli/README.md +29 -0
  7. package/demos/ago-node-cli/ago.js +33 -0
  8. package/demos/ago-node-cli/index.js +11 -0
  9. package/demos/ago-node-cli/lib/item-export-command.js +48 -0
  10. package/demos/ago-node-cli/lib/item-search-command.js +35 -0
  11. package/demos/ago-node-cli/package-lock.json +152 -0
  12. package/demos/ago-node-cli/package.json +30 -0
  13. package/demos/attachments/README.md +5 -0
  14. package/demos/attachments/index.html +164 -0
  15. package/demos/attachments/package-lock.json +182 -0
  16. package/demos/attachments/package.json +18 -0
  17. package/demos/batch-geocoder-node/NYC_Restaurant_Inspection_Results.csv +100 -0
  18. package/demos/batch-geocoder-node/README.md +15 -0
  19. package/demos/batch-geocoder-node/batch-geocode.js +115 -0
  20. package/demos/batch-geocoder-node/config-template.js +18 -0
  21. package/demos/batch-geocoder-node/package-lock.json +94 -0
  22. package/demos/batch-geocoder-node/package.json +38 -0
  23. package/demos/express/README.md +15 -0
  24. package/demos/express/config.json.template +3 -0
  25. package/demos/express/package-lock.json +413 -0
  26. package/demos/express/package.json +18 -0
  27. package/demos/express/server.js +33 -0
  28. package/demos/feature-service-browser/README.md +6 -0
  29. package/demos/feature-service-browser/index.html +122 -0
  30. package/demos/feature-service-browser/package-lock.json +182 -0
  31. package/demos/feature-service-browser/package.json +18 -0
  32. package/demos/geocoder-browser/README.md +10 -0
  33. package/demos/geocoder-browser/config.js.template +1 -0
  34. package/demos/geocoder-browser/index.html +131 -0
  35. package/demos/geocoder-browser/package-lock.json +182 -0
  36. package/demos/geocoder-browser/package.json +19 -0
  37. package/demos/geocoder-browser/post-sign-in.html +25 -0
  38. package/demos/jsapi-integration/README.md +25 -0
  39. package/demos/jsapi-integration/config.js +6 -0
  40. package/demos/jsapi-integration/index.html +85 -0
  41. package/demos/jsapi-integration/package-lock.json +184 -0
  42. package/demos/jsapi-integration/package.json +19 -0
  43. package/demos/node-cli-item-management/README.md +10 -0
  44. package/demos/node-cli-item-management/index.js +238 -0
  45. package/demos/node-cli-item-management/package-lock.json +152 -0
  46. package/demos/node-cli-item-management/package.json +27 -0
  47. package/demos/node-cli-item-management/screenshot.png +0 -0
  48. package/demos/oauth2-browser/README.md +14 -0
  49. package/demos/oauth2-browser/authenticate.html +30 -0
  50. package/demos/oauth2-browser/config.js.template +6 -0
  51. package/demos/oauth2-browser/index.html +202 -0
  52. package/demos/oauth2-browser/logo.svg +4 -0
  53. package/demos/oauth2-browser/package-lock.json +163 -0
  54. package/demos/oauth2-browser/package.json +18 -0
  55. package/demos/oauth2-browser/style.css +36 -0
  56. package/demos/oauth2-browser-retry/README.md +25 -0
  57. package/demos/oauth2-browser-retry/authenticate.html +22 -0
  58. package/demos/oauth2-browser-retry/index.html +116 -0
  59. package/demos/oauth2-browser-retry/logo.svg +4 -0
  60. package/demos/stream-response-to-file/README.md +7 -0
  61. package/demos/stream-response-to-file/index.js +36 -0
  62. package/demos/stream-response-to-file/output/.gitkeep +0 -0
  63. package/demos/stream-response-to-file/package-lock.json +60 -0
  64. package/demos/stream-response-to-file/package.json +33 -0
  65. package/demos/tree-shaking-rollup/.babelrc +3 -0
  66. package/demos/tree-shaking-rollup/README.md +9 -0
  67. package/demos/tree-shaking-rollup/index.html +11 -0
  68. package/demos/tree-shaking-rollup/package-lock.json +2225 -0
  69. package/demos/tree-shaking-rollup/package.json +25 -0
  70. package/demos/tree-shaking-rollup/rollup.config.js +17 -0
  71. package/demos/tree-shaking-rollup/src/index.js +8 -0
  72. package/demos/tree-shaking-webpack/README.md +9 -0
  73. package/demos/tree-shaking-webpack/index.html +11 -0
  74. package/demos/tree-shaking-webpack/package-lock.json +4595 -0
  75. package/demos/tree-shaking-webpack/package.json +24 -0
  76. package/demos/tree-shaking-webpack/src/index.js +10 -0
  77. package/demos/tree-shaking-webpack/webpack.config.js +27 -0
  78. package/demos/vue/.env.example +11 -0
  79. package/demos/vue/.eslintrc.js +17 -0
  80. package/demos/vue/.postcssrc.js +5 -0
  81. package/demos/vue/README.md +17 -0
  82. package/demos/vue/babel.config.js +3 -0
  83. package/demos/vue/package-lock.json +11323 -0
  84. package/demos/vue/package.json +33 -0
  85. package/demos/vue/public/favicon.ico +0 -0
  86. package/demos/vue/public/index.html +24 -0
  87. package/demos/vue/src/assets/logo.svg +29 -0
  88. package/demos/vue/src/components/App.vue +305 -0
  89. package/demos/vue/src/components/Authenticate.vue +65 -0
  90. package/demos/vue/src/components/Loader.vue +230 -0
  91. package/demos/vue/src/main.js +92 -0
  92. package/demos/webmap-checker-sapper/.env.example +5 -0
  93. package/demos/webmap-checker-sapper/README.md +123 -0
  94. package/demos/webmap-checker-sapper/appveyor.yml +18 -0
  95. package/demos/webmap-checker-sapper/cypress/fixtures/example.json +5 -0
  96. package/demos/webmap-checker-sapper/cypress/integration/spec.js +19 -0
  97. package/demos/webmap-checker-sapper/cypress/plugins/index.js +17 -0
  98. package/demos/webmap-checker-sapper/cypress/support/commands.js +25 -0
  99. package/demos/webmap-checker-sapper/cypress/support/index.js +20 -0
  100. package/demos/webmap-checker-sapper/cypress.json +4 -0
  101. package/demos/webmap-checker-sapper/package-lock.json +3851 -0
  102. package/demos/webmap-checker-sapper/package.json +50 -0
  103. package/demos/webmap-checker-sapper/rollup.config.js +87 -0
  104. package/demos/webmap-checker-sapper/src/client.js +20 -0
  105. package/demos/webmap-checker-sapper/src/components/LayerStatus.html +108 -0
  106. package/demos/webmap-checker-sapper/src/components/Nav.html +21 -0
  107. package/demos/webmap-checker-sapper/src/components/WebMap.html +62 -0
  108. package/demos/webmap-checker-sapper/src/routes/_error.html +41 -0
  109. package/demos/webmap-checker-sapper/src/routes/_layout.html +21 -0
  110. package/demos/webmap-checker-sapper/src/routes/auth/authorize.js +18 -0
  111. package/demos/webmap-checker-sapper/src/routes/auth/exchange-token.js +20 -0
  112. package/demos/webmap-checker-sapper/src/routes/auth/post-sign-in.js +24 -0
  113. package/demos/webmap-checker-sapper/src/routes/auth/sign-out.js +10 -0
  114. package/demos/webmap-checker-sapper/src/routes/index.html +20 -0
  115. package/demos/webmap-checker-sapper/src/routes/webmaps/[webmapId].html +83 -0
  116. package/demos/webmap-checker-sapper/src/routes/webmaps/index.html +59 -0
  117. package/demos/webmap-checker-sapper/src/server.js +101 -0
  118. package/demos/webmap-checker-sapper/src/service-worker.js +82 -0
  119. package/demos/webmap-checker-sapper/src/template.html +33 -0
  120. package/demos/webmap-checker-sapper/src/userInfoMiddleware.js +21 -0
  121. package/demos/webmap-checker-sapper/src/utils.js +33 -0
  122. package/demos/webmap-checker-sapper/static/favicon.png +0 -0
  123. package/demos/webmap-checker-sapper/static/global.css +36 -0
  124. package/demos/webmap-checker-sapper/static/manifest.json +20 -0
  125. package/demos/webmap-checker-sapper/static/svelte-logo-192.png +0 -0
  126. package/demos/webmap-checker-sapper/static/svelte-logo-512.png +0 -0
  127. package/docs/FAQ.md +48 -0
  128. package/docs/HISTORY.md +62 -0
  129. package/docs/acetate.config.js +262 -0
  130. package/docs/build-typedoc.js +434 -0
  131. package/docs/generate-srihashes.js +53 -0
  132. package/docs/src/_layout.html +86 -0
  133. package/docs/src/api/_declaration.html +600 -0
  134. package/docs/src/api/_layout.html +204 -0
  135. package/docs/src/api/_package.html +38 -0
  136. package/docs/src/api/index.html +16 -0
  137. package/docs/src/guides/_layout.html +24 -0
  138. package/docs/src/guides/amd-requirejs-dojo.md +40 -0
  139. package/docs/src/guides/browser-authentication.md +41 -0
  140. package/docs/src/guides/bundlers.md +52 -0
  141. package/docs/src/guides/cli-authentication.md +9 -0
  142. package/docs/src/guides/client-server-authentication.md +9 -0
  143. package/docs/src/guides/from-a-cdn.md +38 -0
  144. package/docs/src/guides/index.md +59 -0
  145. package/docs/src/guides/node.md +87 -0
  146. package/docs/src/guides/package-overview.md +111 -0
  147. package/docs/src/guides/server-authentication.md +9 -0
  148. package/docs/src/guides/whats-new-v2-0.md +305 -0
  149. package/docs/src/img/icons.png +0 -0
  150. package/docs/src/img/icons@2x.png +0 -0
  151. package/docs/src/index.html +12 -0
  152. package/docs/src/js/api-search.js +112 -0
  153. package/docs/src/js/nav-toggle.js +41 -0
  154. package/docs/src/sass/_highlight.scss +96 -0
  155. package/docs/src/sass/_icons.scss +157 -0
  156. package/docs/src/sass/style.scss +242 -0
  157. package/docs/src/srihashes.json +12 -0
  158. package/jasmine.json +7 -0
  159. package/karma.conf.js +106 -0
  160. package/lerna.json +8 -0
  161. package/notes/README.md +88 -0
  162. package/package.json +129 -0
  163. package/packages/arcgis-rest-auth/README.md +71 -0
  164. package/packages/arcgis-rest-auth/package-lock.json +11 -0
  165. package/packages/arcgis-rest-auth/package.json +69 -0
  166. package/packages/arcgis-rest-auth/src/ApplicationSession.ts +122 -0
  167. package/packages/arcgis-rest-auth/src/UserSession.ts +997 -0
  168. package/packages/arcgis-rest-auth/src/authenticated-request-options.ts +24 -0
  169. package/packages/arcgis-rest-auth/src/fetch-token.ts +50 -0
  170. package/packages/arcgis-rest-auth/src/generate-token.ts +35 -0
  171. package/packages/arcgis-rest-auth/src/index.ts +9 -0
  172. package/packages/arcgis-rest-auth/test/ApplicationSession.test.ts +124 -0
  173. package/packages/arcgis-rest-auth/test/UserSession.test.ts +1315 -0
  174. package/packages/arcgis-rest-auth/test/fetchToken.test.ts +112 -0
  175. package/packages/arcgis-rest-auth/test/generateToken.test.ts +102 -0
  176. package/packages/arcgis-rest-auth/test/utils.ts +14 -0
  177. package/packages/arcgis-rest-auth/tsconfig.json +6 -0
  178. package/packages/arcgis-rest-feature-layer/README.md +77 -0
  179. package/packages/arcgis-rest-feature-layer/package-lock.json +11 -0
  180. package/packages/arcgis-rest-feature-layer/package.json +64 -0
  181. package/packages/arcgis-rest-feature-layer/src/add.ts +56 -0
  182. package/packages/arcgis-rest-feature-layer/src/addAttachment.ts +53 -0
  183. package/packages/arcgis-rest-feature-layer/src/decodeValues.ts +124 -0
  184. package/packages/arcgis-rest-feature-layer/src/delete.ts +61 -0
  185. package/packages/arcgis-rest-feature-layer/src/deleteAttachments.ts +52 -0
  186. package/packages/arcgis-rest-feature-layer/src/getAttachments.ts +55 -0
  187. package/packages/arcgis-rest-feature-layer/src/getLayer.ts +24 -0
  188. package/packages/arcgis-rest-feature-layer/src/helpers.ts +55 -0
  189. package/packages/arcgis-rest-feature-layer/src/index.ts +28 -0
  190. package/packages/arcgis-rest-feature-layer/src/query.ts +203 -0
  191. package/packages/arcgis-rest-feature-layer/src/queryRelated.ts +89 -0
  192. package/packages/arcgis-rest-feature-layer/src/update.ts +56 -0
  193. package/packages/arcgis-rest-feature-layer/src/updateAttachment.ts +59 -0
  194. package/packages/arcgis-rest-feature-layer/test/attachments.test.ts +202 -0
  195. package/packages/arcgis-rest-feature-layer/test/crud.test.ts +130 -0
  196. package/packages/arcgis-rest-feature-layer/test/decodeValues.test.ts +67 -0
  197. package/packages/arcgis-rest-feature-layer/test/getLayer.test.ts +31 -0
  198. package/packages/arcgis-rest-feature-layer/test/mocks/cvdQueryResponse.ts +225 -0
  199. package/packages/arcgis-rest-feature-layer/test/mocks/feature.ts +281 -0
  200. package/packages/arcgis-rest-feature-layer/test/mocks/fields.ts +779 -0
  201. package/packages/arcgis-rest-feature-layer/test/mocks/foo.txt +1 -0
  202. package/packages/arcgis-rest-feature-layer/test/mocks/service.ts +259 -0
  203. package/packages/arcgis-rest-feature-layer/test/query.test.ts +168 -0
  204. package/packages/arcgis-rest-feature-layer/tsconfig.json +6 -0
  205. package/packages/arcgis-rest-geocoding/README.md +86 -0
  206. package/packages/arcgis-rest-geocoding/package-lock.json +11 -0
  207. package/packages/arcgis-rest-geocoding/package.json +64 -0
  208. package/packages/arcgis-rest-geocoding/src/bulk.ts +105 -0
  209. package/packages/arcgis-rest-geocoding/src/geocode.ts +130 -0
  210. package/packages/arcgis-rest-geocoding/src/helpers.ts +54 -0
  211. package/packages/arcgis-rest-geocoding/src/index.ts +15 -0
  212. package/packages/arcgis-rest-geocoding/src/reverse.ts +84 -0
  213. package/packages/arcgis-rest-geocoding/src/suggest.ts +45 -0
  214. package/packages/arcgis-rest-geocoding/test/bulk.test.ts +151 -0
  215. package/packages/arcgis-rest-geocoding/test/geocode.test.ts +212 -0
  216. package/packages/arcgis-rest-geocoding/test/helpers.test.ts +85 -0
  217. package/packages/arcgis-rest-geocoding/test/mocks/responses.ts +637 -0
  218. package/packages/arcgis-rest-geocoding/test/reverse.test.ts +126 -0
  219. package/packages/arcgis-rest-geocoding/test/suggest.test.ts +53 -0
  220. package/packages/arcgis-rest-geocoding/tsconfig.json +6 -0
  221. package/packages/arcgis-rest-portal/README.md +73 -0
  222. package/packages/arcgis-rest-portal/package-lock.json +11 -0
  223. package/packages/arcgis-rest-portal/package.json +64 -0
  224. package/packages/arcgis-rest-portal/src/groups/create.ts +43 -0
  225. package/packages/arcgis-rest-portal/src/groups/get.ts +99 -0
  226. package/packages/arcgis-rest-portal/src/groups/helpers.ts +14 -0
  227. package/packages/arcgis-rest-portal/src/groups/join.ts +57 -0
  228. package/packages/arcgis-rest-portal/src/groups/notification.ts +77 -0
  229. package/packages/arcgis-rest-portal/src/groups/protect.ts +56 -0
  230. package/packages/arcgis-rest-portal/src/groups/remove.ts +32 -0
  231. package/packages/arcgis-rest-portal/src/groups/search.ts +25 -0
  232. package/packages/arcgis-rest-portal/src/groups/update.ts +39 -0
  233. package/packages/arcgis-rest-portal/src/index.ts +48 -0
  234. package/packages/arcgis-rest-portal/src/items/add.ts +137 -0
  235. package/packages/arcgis-rest-portal/src/items/create.ts +128 -0
  236. package/packages/arcgis-rest-portal/src/items/get.ts +177 -0
  237. package/packages/arcgis-rest-portal/src/items/helpers.ts +193 -0
  238. package/packages/arcgis-rest-portal/src/items/protect.ts +39 -0
  239. package/packages/arcgis-rest-portal/src/items/remove.ts +131 -0
  240. package/packages/arcgis-rest-portal/src/items/search.ts +25 -0
  241. package/packages/arcgis-rest-portal/src/items/update.ts +145 -0
  242. package/packages/arcgis-rest-portal/src/sharing/access.ts +84 -0
  243. package/packages/arcgis-rest-portal/src/sharing/group-sharing.ts +210 -0
  244. package/packages/arcgis-rest-portal/src/sharing/helpers.ts +84 -0
  245. package/packages/arcgis-rest-portal/src/users/get-user-url.ts +18 -0
  246. package/packages/arcgis-rest-portal/src/users/get-user.ts +58 -0
  247. package/packages/arcgis-rest-portal/src/users/invitation.ts +162 -0
  248. package/packages/arcgis-rest-portal/src/users/notification.ts +70 -0
  249. package/packages/arcgis-rest-portal/src/users/update.ts +66 -0
  250. package/packages/arcgis-rest-portal/src/util/SearchQueryBuilder.ts +374 -0
  251. package/packages/arcgis-rest-portal/src/util/generic-search.ts +67 -0
  252. package/packages/arcgis-rest-portal/src/util/get-portal-url.ts +27 -0
  253. package/packages/arcgis-rest-portal/src/util/get-portal.ts +52 -0
  254. package/packages/arcgis-rest-portal/src/util/search.ts +38 -0
  255. package/packages/arcgis-rest-portal/test/groups/crud.test.ts +180 -0
  256. package/packages/arcgis-rest-portal/test/groups/get.test.ts +101 -0
  257. package/packages/arcgis-rest-portal/test/groups/join.test.ts +72 -0
  258. package/packages/arcgis-rest-portal/test/groups/notification.test.ts +112 -0
  259. package/packages/arcgis-rest-portal/test/groups/protect.test.ts +72 -0
  260. package/packages/arcgis-rest-portal/test/groups/search.test.ts +110 -0
  261. package/packages/arcgis-rest-portal/test/items/add.test.ts +321 -0
  262. package/packages/arcgis-rest-portal/test/items/create.test.ts +375 -0
  263. package/packages/arcgis-rest-portal/test/items/get.test.ts +309 -0
  264. package/packages/arcgis-rest-portal/test/items/protect.test.ts +122 -0
  265. package/packages/arcgis-rest-portal/test/items/remove.test.ts +238 -0
  266. package/packages/arcgis-rest-portal/test/items/search.test.ts +272 -0
  267. package/packages/arcgis-rest-portal/test/items/update.test.ts +515 -0
  268. package/packages/arcgis-rest-portal/test/mocks/groups/responses.ts +144 -0
  269. package/packages/arcgis-rest-portal/test/mocks/items/foo.zip +0 -0
  270. package/packages/arcgis-rest-portal/test/mocks/items/item.ts +122 -0
  271. package/packages/arcgis-rest-portal/test/mocks/items/resources.ts +31 -0
  272. package/packages/arcgis-rest-portal/test/mocks/items/search.ts +121 -0
  273. package/packages/arcgis-rest-portal/test/mocks/portal/response.ts +112 -0
  274. package/packages/arcgis-rest-portal/test/mocks/sharing/sharing.ts +18 -0
  275. package/packages/arcgis-rest-portal/test/mocks/users/invitation.ts +70 -0
  276. package/packages/arcgis-rest-portal/test/mocks/users/notification.ts +34 -0
  277. package/packages/arcgis-rest-portal/test/mocks/users/user.ts +173 -0
  278. package/packages/arcgis-rest-portal/test/sharing/access.test.ts +162 -0
  279. package/packages/arcgis-rest-portal/test/sharing/group-sharing.test.ts +650 -0
  280. package/packages/arcgis-rest-portal/test/sharing/helpers.test.ts +55 -0
  281. package/packages/arcgis-rest-portal/test/users/get-user-url.test.ts +40 -0
  282. package/packages/arcgis-rest-portal/test/users/get-user.test.ts +90 -0
  283. package/packages/arcgis-rest-portal/test/users/invitation.test.ts +127 -0
  284. package/packages/arcgis-rest-portal/test/users/notification.test.ts +77 -0
  285. package/packages/arcgis-rest-portal/test/users/update.test.ts +151 -0
  286. package/packages/arcgis-rest-portal/test/util/SearchQueryBuilder.test.ts +340 -0
  287. package/packages/arcgis-rest-portal/test/util/get-portal-url.test.ts +37 -0
  288. package/packages/arcgis-rest-portal/test/util/portal.test.ts +97 -0
  289. package/packages/arcgis-rest-portal/tsconfig.json +6 -0
  290. package/packages/arcgis-rest-request/README.md +72 -0
  291. package/packages/arcgis-rest-request/package-lock.json +11 -0
  292. package/packages/arcgis-rest-request/package.json +60 -0
  293. package/packages/arcgis-rest-request/src/index.ts +24 -0
  294. package/packages/arcgis-rest-request/src/request.ts +389 -0
  295. package/packages/arcgis-rest-request/src/utils/ArcGISRequestError.ts +76 -0
  296. package/packages/arcgis-rest-request/src/utils/ErrorTypes.ts +29 -0
  297. package/packages/arcgis-rest-request/src/utils/GrantTypes.ts +5 -0
  298. package/packages/arcgis-rest-request/src/utils/HTTPMethods.ts +6 -0
  299. package/packages/arcgis-rest-request/src/utils/IAuthenticationManager.ts +21 -0
  300. package/packages/arcgis-rest-request/src/utils/IFetchTokenParams.ts +11 -0
  301. package/packages/arcgis-rest-request/src/utils/IGenerateTokenParams.ts +9 -0
  302. package/packages/arcgis-rest-request/src/utils/IParamBuilder.ts +3 -0
  303. package/packages/arcgis-rest-request/src/utils/IParams.ts +6 -0
  304. package/packages/arcgis-rest-request/src/utils/IParamsBuilder.ts +5 -0
  305. package/packages/arcgis-rest-request/src/utils/IRequestOptions.ts +43 -0
  306. package/packages/arcgis-rest-request/src/utils/ITokenRequestOptions.ts +9 -0
  307. package/packages/arcgis-rest-request/src/utils/ResponseFormats.ts +10 -0
  308. package/packages/arcgis-rest-request/src/utils/append-custom-params.ts +49 -0
  309. package/packages/arcgis-rest-request/src/utils/clean-url.ts +16 -0
  310. package/packages/arcgis-rest-request/src/utils/encode-form-data.ts +38 -0
  311. package/packages/arcgis-rest-request/src/utils/encode-query-string.ts +23 -0
  312. package/packages/arcgis-rest-request/src/utils/process-params.ts +109 -0
  313. package/packages/arcgis-rest-request/src/utils/retryAuthError.ts +10 -0
  314. package/packages/arcgis-rest-request/src/utils/warn.ts +11 -0
  315. package/packages/arcgis-rest-request/src/utils/with-options.ts +48 -0
  316. package/packages/arcgis-rest-request/test/mocks/errors.ts +76 -0
  317. package/packages/arcgis-rest-request/test/mocks/geojson-feature-collection.ts +13 -0
  318. package/packages/arcgis-rest-request/test/mocks/param-builder.ts +7 -0
  319. package/packages/arcgis-rest-request/test/mocks/sharing-rest-info.ts +41 -0
  320. package/packages/arcgis-rest-request/test/mocks/webmap.ts +41 -0
  321. package/packages/arcgis-rest-request/test/request.test.ts +496 -0
  322. package/packages/arcgis-rest-request/test/utils/ArcGISAuthError.test.ts +191 -0
  323. package/packages/arcgis-rest-request/test/utils/ArcGISRequestError.test.ts +51 -0
  324. package/packages/arcgis-rest-request/test/utils/check-for-errors.test.ts +111 -0
  325. package/packages/arcgis-rest-request/test/utils/clean-url.test.ts +47 -0
  326. package/packages/arcgis-rest-request/test/utils/encode-form-data.test.ts +133 -0
  327. package/packages/arcgis-rest-request/test/utils/process-params.test.ts +193 -0
  328. package/packages/arcgis-rest-request/test/utils/with-options.test.ts +133 -0
  329. package/packages/arcgis-rest-request/tsconfig.json +4 -0
  330. package/packages/arcgis-rest-routing/README.md +75 -0
  331. package/packages/arcgis-rest-routing/package-lock.json +11 -0
  332. package/packages/arcgis-rest-routing/package.json +63 -0
  333. package/packages/arcgis-rest-routing/src/helpers.ts +16 -0
  334. package/packages/arcgis-rest-routing/src/index.ts +11 -0
  335. package/packages/arcgis-rest-routing/src/solveRoute.ts +124 -0
  336. package/packages/arcgis-rest-routing/test/mocks/responses.ts +825 -0
  337. package/packages/arcgis-rest-routing/test/solveRoute.test.ts +509 -0
  338. package/packages/arcgis-rest-routing/tsconfig.json +6 -0
  339. package/packages/arcgis-rest-service-admin/README.md +73 -0
  340. package/packages/arcgis-rest-service-admin/package-lock.json +11 -0
  341. package/packages/arcgis-rest-service-admin/package.json +65 -0
  342. package/packages/arcgis-rest-service-admin/src/addTo.ts +70 -0
  343. package/packages/arcgis-rest-service-admin/src/create.ts +213 -0
  344. package/packages/arcgis-rest-service-admin/src/index.ts +9 -0
  345. package/packages/arcgis-rest-service-admin/test/addTo.test.ts +350 -0
  346. package/packages/arcgis-rest-service-admin/test/create.test.ts +377 -0
  347. package/packages/arcgis-rest-service-admin/test/mocks/layerDefinition.ts +79 -0
  348. package/packages/arcgis-rest-service-admin/test/mocks/move.ts +11 -0
  349. package/packages/arcgis-rest-service-admin/test/mocks/service.ts +69 -0
  350. package/packages/arcgis-rest-service-admin/tsconfig.json +6 -0
  351. package/packages/arcgis-rest-types/README.md +66 -0
  352. package/packages/arcgis-rest-types/package.json +54 -0
  353. package/packages/arcgis-rest-types/src/feature.ts +39 -0
  354. package/packages/arcgis-rest-types/src/geometry.ts +272 -0
  355. package/packages/arcgis-rest-types/src/group.ts +72 -0
  356. package/packages/arcgis-rest-types/src/index.ts +7 -0
  357. package/packages/arcgis-rest-types/src/item.ts +65 -0
  358. package/packages/arcgis-rest-types/src/symbol.ts +170 -0
  359. package/packages/arcgis-rest-types/src/user.ts +48 -0
  360. package/packages/arcgis-rest-types/src/webmap.ts +1345 -0
  361. package/packages/arcgis-rest-types/tsconfig.json +9 -0
  362. package/support/changelog.js +393 -0
  363. package/support/deploy-doc-site.js +16 -0
  364. package/support/dev.sh +6 -0
  365. package/support/publish.sh +47 -0
  366. package/support/test-helpers.js +9 -0
  367. package/tsconfig.json +63 -0
  368. package/tslint.json +16 -0
  369. package/umd-base-profile.js +81 -0
  370. package/umd-production-profile.js +13 -0
@@ -0,0 +1,997 @@
1
+ /* Copyright (c) 2017-2019 Environmental Systems Research Institute, Inc.
2
+ * Apache-2.0 */
3
+
4
+ /**
5
+ * /generateToken returns a token that cannot be refreshed.
6
+ *
7
+ * oauth2/token can return a token *and* a refreshToken.
8
+ * up until the refreshToken expires, you can use it (and a clientId)
9
+ * to fetch fresh credentials without a username and password.
10
+ *
11
+ * the catch is that this 'authorization_code' flow is only utilized
12
+ * by server based OAuth 2 Node.js applications that call /authorize first.
13
+ */
14
+
15
+ import * as http from "http";
16
+ import {
17
+ request,
18
+ IRequestOptions,
19
+ ArcGISAuthError,
20
+ IAuthenticationManager,
21
+ ITokenRequestOptions,
22
+ cleanUrl
23
+ } from "@esri/arcgis-rest-request";
24
+ import { IUser } from "@esri/arcgis-rest-types";
25
+ import { generateToken } from "./generate-token";
26
+ import { fetchToken, IFetchTokenResponse } from "./fetch-token";
27
+
28
+ /**
29
+ * Internal utility for resolving a Promise from outside its constructor.
30
+ *
31
+ * See: http://lea.verou.me/2016/12/resolve-promises-externally-with-this-one-weird-trick/
32
+ */
33
+ interface IDeferred<T> {
34
+ promise: Promise<T>;
35
+ resolve: (v: T) => void;
36
+ reject: (v: any) => void;
37
+ }
38
+
39
+ export type AuthenticationProvider = "arcgis" | "facebook" | "google";
40
+
41
+ /**
42
+ * Represents a [credential](https://developers.arcgis.com/javascript/latest/api-reference/esri-identity-Credential.html)
43
+ * object used to access a secure ArcGIS resource.
44
+ */
45
+ export interface ICredential {
46
+ expires: number;
47
+ server: string;
48
+ ssl: boolean;
49
+ token: string;
50
+ userId: string;
51
+ }
52
+
53
+ function defer<T>(): IDeferred<T> {
54
+ const deferred: any = {
55
+ promise: null,
56
+ resolve: null,
57
+ reject: null
58
+ };
59
+
60
+ deferred.promise = new Promise((resolve, reject) => {
61
+ deferred.resolve = resolve;
62
+ deferred.reject = reject;
63
+ });
64
+
65
+ return deferred as IDeferred<T>;
66
+ }
67
+
68
+ /**
69
+ * Used to test if a URL is an ArcGIS Online URL
70
+ */
71
+ const arcgisOnlineUrlRegex = /^https?:\/\/\S+\.arcgis\.com.+/;
72
+
73
+ /**
74
+ * Used to test if a URL is production ArcGIS Online Portal
75
+ */
76
+ const arcgisOnlinePortalRegex = /^https?:\/\/www\.arcgis\.com\/sharing\/rest+/;
77
+
78
+ /**
79
+ * Used to test if a URL is an ArcGIS Online Organization Portal
80
+ */
81
+ const arcgisOnlineOrgPortalRegex = /^https?:\/\/(?:[a-z0-9-]+\.maps)?.\arcgis\.com\/sharing\/rest/;
82
+
83
+ /**
84
+ * Options for static OAuth 2.0 helper methods on `UserSession`.
85
+ */
86
+ export interface IOAuth2Options {
87
+ /**
88
+ * Client ID of your application. Can be obtained by registering an application
89
+ * on [ArcGIS for Developers](https://developers.arcgis.com/documentation/core-concepts/security-and-authentication/signing-in-arcgis-online-users/#registering-your-application),
90
+ * [ArcGIS Online](http://doc.arcgis.com/en/arcgis-online/share-maps/add-items.htm#ESRI_SECTION1_0D1B620254F745AE84F394289F8AF44B) or on your instance of ArcGIS Enterprise.
91
+ */
92
+ clientId: string;
93
+
94
+ /**
95
+ * A valid URL to redirect to after a user authorizes your application. Can be set on [ArcGIS for Developers](https://developers.arcgis.com/documentation/core-concepts/security-and-authentication/signing-in-arcgis-online-users/#registering-your-application),
96
+ * [ArcGIS Online](http://doc.arcgis.com/en/arcgis-online/share-maps/add-items.htm#ESRI_SECTION1_0D1B620254F745AE84F394289F8AF44B) or on your instance of ArcGIS Enterprise.
97
+ */
98
+ redirectUri: string;
99
+
100
+ /**
101
+ * The ArcGIS Online or ArcGIS Enterprise portal you want to use for authentication. Defaults to `https://www.arcgis.com/sharing/rest` for the ArcGIS Online portal.
102
+ */
103
+ portal?: string;
104
+
105
+ /**
106
+ * ArcGIS Authentication is used by default. Specifying an alternative will take users directly to the corresponding provider's OAuth page.
107
+ */
108
+
109
+ provider?: AuthenticationProvider;
110
+
111
+ /**
112
+ * Duration (in minutes) that a token will be valid. Defaults to 20160 (two weeks).
113
+ */
114
+ duration?: number;
115
+
116
+ /**
117
+ * Determines whether to open the authorization window in a new tab/window or in the current window.
118
+ *
119
+ * @browserOnly
120
+ */
121
+ popup?: boolean;
122
+
123
+ /**
124
+ * Duration (in minutes) that a refresh token will be valid.
125
+ *
126
+ * @nodeOnly
127
+ */
128
+ refreshTokenTTL?: number;
129
+
130
+ /**
131
+ * The locale assumed to render the login page.
132
+ *
133
+ * @browserOnly
134
+ */
135
+ locale?: string;
136
+
137
+ /**
138
+ * Applications can specify an opaque value for this parameter to correlate the authorization request sent with the received response. By default, clientId is used.
139
+ *
140
+ * @browserOnly
141
+ */
142
+ state?: string;
143
+ }
144
+
145
+ /**
146
+ * Options for the `UserSession` constructor.
147
+ */
148
+ export interface IUserSessionOptions {
149
+ /**
150
+ * Client ID of your application. Can be obtained by registering an application
151
+ * on [ArcGIS for Developers](https://developers.arcgis.com/documentation/core-concepts/security-and-authentication/signing-in-arcgis-online-users/#registering-your-application),
152
+ * [ArcGIS Online](http://doc.arcgis.com/en/arcgis-online/share-maps/add-items.htm#ESRI_SECTION1_0D1B620254F745AE84F394289F8AF44B) or on your instance of ArcGIS Enterprise.
153
+ */
154
+ clientId?: string;
155
+
156
+ /**
157
+ * A valid URL to redirect to after a user authorizes your application. Can be set on [ArcGIS for Developers](https://developers.arcgis.com/documentation/core-concepts/security-and-authentication/signing-in-arcgis-online-users/#registering-your-application),
158
+ * [ArcGIS Online](http://doc.arcgis.com/en/arcgis-online/share-maps/add-items.htm#ESRI_SECTION1_0D1B620254F745AE84F394289F8AF44B) or on your instance of ArcGIS Enterprise.
159
+ */
160
+ redirectUri?: string;
161
+
162
+ /**
163
+ * OAuth 2.0 refresh token from a previous user session.
164
+ */
165
+ refreshToken?: string;
166
+
167
+ /**
168
+ * Expiration date of the `refreshToken`
169
+ */
170
+ refreshTokenExpires?: Date;
171
+
172
+ /**
173
+ * The authenticated user's username. Guaranteed to be unique across ArcGIS Online or your instance of ArcGIS Enterprise.
174
+ */
175
+ username?: string;
176
+
177
+ /**
178
+ * Password for this user. Used in CLI apps where users cannot do OAuth 2.0.
179
+ */
180
+ password?: string;
181
+
182
+ /**
183
+ * OAuth 2.0 access token from a previous user session.
184
+ */
185
+ token?: string;
186
+
187
+ /**
188
+ * Expiration date for the `token`
189
+ */
190
+ tokenExpires?: Date;
191
+
192
+ /**
193
+ * The ArcGIS Online or ArcGIS Enterprise portal you want to use for authentication. Defaults to `https://www.arcgis.com/sharing/rest` for the ArcGIS Online portal.
194
+ */
195
+ portal?: string;
196
+
197
+ /**
198
+ * This value is set to true automatically if the ArcGIS Organization requires that requests be made over https.
199
+ */
200
+ ssl?: boolean;
201
+
202
+ /**
203
+ * ArcGIS Authentication is used by default. Specifying an alternative will take users directly to the corresponding provider's OAuth page.
204
+ */
205
+ provider?: AuthenticationProvider;
206
+
207
+ /**
208
+ * Duration of requested token validity in minutes. Used when requesting tokens with `username` and `password` or when validating the identity of unknown servers. Defaults to two weeks.
209
+ */
210
+ tokenDuration?: number;
211
+
212
+ /**
213
+ * Duration (in minutes) that a refresh token will be valid.
214
+ */
215
+ refreshTokenTTL?: number;
216
+
217
+ /**
218
+ * An unfederated ArcGIS Server instance known to recognize credentials supplied manually.
219
+ * ```js
220
+ * {
221
+ * server: "https://sampleserver6.arcgisonline.com/arcgis",
222
+ * token: "SOSlV3v..",
223
+ * tokenExpires: new Date(1545415669763)
224
+ * }
225
+ * ```
226
+ */
227
+ server?: string;
228
+ }
229
+
230
+ /**
231
+ * ```js
232
+ * import { UserSession } from '@esri/arcgis-rest-auth';
233
+ * UserSession.beginOAuth2({
234
+ * // register an app of your own to create a unique clientId
235
+ * clientId: "abc123",
236
+ * redirectUri: 'https://yourapp.com/authenticate.html'
237
+ * })
238
+ * .then(session)
239
+ * // or
240
+ * new UserSession({
241
+ * username: "jsmith",
242
+ * password: "123456"
243
+ * })
244
+ * // or
245
+ * UserSession.deserialize(cache)
246
+ * ```
247
+ * Used to authenticate both ArcGIS Online and ArcGIS Enterprise users. `UserSession` includes helper methods for [OAuth 2.0](/arcgis-rest-js/guides/browser-authentication/) in both browser and server applications.
248
+ */
249
+ export class UserSession implements IAuthenticationManager {
250
+ /**
251
+ * Begins a new browser-based OAuth 2.0 sign in. If `options.popup` is true the
252
+ * authentication window will open in a new tab/window otherwise the user will
253
+ * be redirected to the authorization page in their current tab.
254
+ *
255
+ * @browserOnly
256
+ */
257
+ /* istanbul ignore next */
258
+ public static beginOAuth2(options: IOAuth2Options, win: any = window) {
259
+ const {
260
+ portal,
261
+ provider,
262
+ clientId,
263
+ duration,
264
+ redirectUri,
265
+ popup,
266
+ state,
267
+ locale
268
+ }: IOAuth2Options = {
269
+ ...{
270
+ portal: "https://www.arcgis.com/sharing/rest",
271
+ provider: "arcgis",
272
+ duration: 20160,
273
+ popup: true,
274
+ state: options.clientId,
275
+ locale: ""
276
+ },
277
+ ...options
278
+ };
279
+ let url: string;
280
+ if (provider === "arcgis") {
281
+ url = `${portal}/oauth2/authorize?client_id=${clientId}&response_type=token&expiration=${duration}&redirect_uri=${encodeURIComponent(
282
+ redirectUri
283
+ )}&state=${state}&locale=${locale}`;
284
+ } else {
285
+ url = `${portal}/oauth2/social/authorize?client_id=${clientId}&socialLoginProviderName=${provider}&autoAccountCreateForSocial=true&response_type=token&expiration=${duration}&redirect_uri=${encodeURIComponent(
286
+ redirectUri
287
+ )}&state=${state}&locale=${locale}`;
288
+ }
289
+
290
+ if (!popup) {
291
+ win.location.href = url;
292
+ return undefined;
293
+ }
294
+
295
+ const session = defer<UserSession>();
296
+
297
+ win[`__ESRI_REST_AUTH_HANDLER_${clientId}`] = function(
298
+ errorString: any,
299
+ oauthInfoString: string
300
+ ) {
301
+ if (errorString) {
302
+ const error = JSON.parse(errorString);
303
+ session.reject(new ArcGISAuthError(error.errorMessage, error.error));
304
+ return;
305
+ }
306
+
307
+ if (oauthInfoString) {
308
+ const oauthInfo = JSON.parse(oauthInfoString);
309
+ session.resolve(
310
+ new UserSession({
311
+ clientId,
312
+ portal,
313
+ ssl: oauthInfo.ssl,
314
+ token: oauthInfo.token,
315
+ tokenExpires: new Date(oauthInfo.expires),
316
+ username: oauthInfo.username
317
+ })
318
+ );
319
+ }
320
+ };
321
+
322
+ win.open(
323
+ url,
324
+ "oauth-window",
325
+ "height=400,width=600,menubar=no,location=yes,resizable=yes,scrollbars=yes,status=yes"
326
+ );
327
+
328
+ return session.promise;
329
+ }
330
+
331
+ /**
332
+ * Completes a browser-based OAuth 2.0 sign if `options.popup` is true the user
333
+ * will be returned to the previous window. Otherwise a new `UserSession`
334
+ * will be returned.
335
+ *
336
+ * @browserOnly
337
+ */
338
+ /* istanbul ignore next */
339
+ public static completeOAuth2(options: IOAuth2Options, win: any = window) {
340
+ const { portal, clientId }: IOAuth2Options = {
341
+ ...{ portal: "https://www.arcgis.com/sharing/rest" },
342
+ ...options
343
+ };
344
+
345
+ function completeSignIn(error: any, oauthInfo?: IFetchTokenResponse) {
346
+ if (win.opener && win.opener.parent) {
347
+ win.opener.parent[`__ESRI_REST_AUTH_HANDLER_${clientId}`](
348
+ error ? JSON.stringify(error) : undefined,
349
+ JSON.stringify(oauthInfo)
350
+ );
351
+ win.close();
352
+ return undefined;
353
+ }
354
+
355
+ if (win !== win.parent) {
356
+ win.parent[`__ESRI_REST_AUTH_HANDLER_${clientId}`](
357
+ error ? JSON.stringify(error) : undefined,
358
+ JSON.stringify(oauthInfo)
359
+ );
360
+ win.close();
361
+ return undefined;
362
+ }
363
+
364
+ if (error) {
365
+ throw new ArcGISAuthError(error.errorMessage, error.error);
366
+ }
367
+
368
+ return new UserSession({
369
+ clientId,
370
+ portal,
371
+ ssl: oauthInfo.ssl,
372
+ token: oauthInfo.token,
373
+ tokenExpires: oauthInfo.expires,
374
+ username: oauthInfo.username
375
+ });
376
+ }
377
+
378
+ const match = win.location.href.match(
379
+ /access_token=(.+)&expires_in=(.+)&username=([^&]+)/
380
+ );
381
+
382
+ if (!match) {
383
+ const errorMatch = win.location.href.match(
384
+ /error=(.+)&error_description=(.+)/
385
+ );
386
+
387
+ const error = errorMatch[1];
388
+ const errorMessage = decodeURIComponent(errorMatch[2]);
389
+
390
+ return completeSignIn({ error, errorMessage });
391
+ }
392
+
393
+ const token = match[1];
394
+ const expires = new Date(
395
+ Date.now() + parseInt(match[2], 10) * 1000 - 60 * 1000
396
+ );
397
+ const username = decodeURIComponent(match[3]);
398
+ const ssl =
399
+ win.location.href.indexOf("&ssl=true") > -1 ||
400
+ win.location.href.indexOf("#ssl=true") > -1;
401
+
402
+ return completeSignIn(undefined, {
403
+ token,
404
+ expires,
405
+ ssl,
406
+ username
407
+ });
408
+ }
409
+
410
+ /**
411
+ * Begins a new server-based OAuth 2.0 sign in. This will redirect the user to
412
+ * the ArcGIS Online or ArcGIS Enterprise authorization page.
413
+ *
414
+ * @nodeOnly
415
+ */
416
+ public static authorize(
417
+ options: IOAuth2Options,
418
+ response: http.ServerResponse
419
+ ) {
420
+ const { portal, clientId, duration, redirectUri }: IOAuth2Options = {
421
+ ...{ portal: "https://arcgis.com/sharing/rest", duration: 20160 },
422
+ ...options
423
+ };
424
+
425
+ response.writeHead(301, {
426
+ Location: `${portal}/oauth2/authorize?client_id=${clientId}&duration=${duration}&response_type=code&redirect_uri=${encodeURIComponent(
427
+ redirectUri
428
+ )}`
429
+ });
430
+
431
+ response.end();
432
+ }
433
+
434
+ /**
435
+ * Completes the server-based OAuth 2.0 sign in process by exchanging the `authorizationCode`
436
+ * for a `access_token`.
437
+ *
438
+ * @nodeOnly
439
+ */
440
+ public static exchangeAuthorizationCode(
441
+ options: IOAuth2Options,
442
+ authorizationCode: string
443
+ ): Promise<UserSession> {
444
+ const { portal, clientId, redirectUri, refreshTokenTTL }: IOAuth2Options = {
445
+ ...{
446
+ portal: "https://www.arcgis.com/sharing/rest",
447
+ refreshTokenTTL: 1440
448
+ },
449
+ ...options
450
+ };
451
+
452
+ return fetchToken(`${portal}/oauth2/token`, {
453
+ params: {
454
+ grant_type: "authorization_code",
455
+ client_id: clientId,
456
+ redirect_uri: redirectUri,
457
+ code: authorizationCode
458
+ }
459
+ }).then(response => {
460
+ return new UserSession({
461
+ clientId,
462
+ portal,
463
+ ssl: response.ssl,
464
+ redirectUri,
465
+ refreshToken: response.refreshToken,
466
+ refreshTokenTTL,
467
+ refreshTokenExpires: new Date(
468
+ Date.now() + (refreshTokenTTL - 1) * 1000
469
+ ),
470
+ token: response.token,
471
+ tokenExpires: response.expires,
472
+ username: response.username
473
+ });
474
+ });
475
+ }
476
+
477
+ public static deserialize(str: string) {
478
+ const options = JSON.parse(str);
479
+ return new UserSession({
480
+ clientId: options.clientId,
481
+ refreshToken: options.refreshToken,
482
+ refreshTokenExpires: new Date(options.refreshTokenExpires),
483
+ username: options.username,
484
+ password: options.password,
485
+ token: options.token,
486
+ tokenExpires: new Date(options.tokenExpires),
487
+ portal: options.portal,
488
+ ssl: options.ssl,
489
+ tokenDuration: options.tokenDuration,
490
+ redirectUri: options.redirectUri,
491
+ refreshTokenTTL: options.refreshTokenTTL
492
+ });
493
+ }
494
+
495
+ /**
496
+ * Translates authentication from the format used in the [ArcGIS API for JavaScript](https://developers.arcgis.com/javascript/).
497
+ *
498
+ * ```js
499
+ * UserSession.fromCredential({
500
+ * userId: "jsmith",
501
+ * token: "secret"
502
+ * });
503
+ * ```
504
+ *
505
+ * @returns UserSession
506
+ */
507
+ public static fromCredential(credential: ICredential) {
508
+ return new UserSession({
509
+ portal: credential.server.includes("sharing/rest")
510
+ ? credential.server
511
+ : credential.server + `/sharing/rest`,
512
+ ssl: credential.ssl,
513
+ token: credential.token,
514
+ username: credential.userId,
515
+ tokenExpires: new Date(credential.expires)
516
+ });
517
+ }
518
+
519
+ /**
520
+ * Client ID being used for authentication if provided in the `constructor`.
521
+ */
522
+ public readonly clientId: string;
523
+
524
+ /**
525
+ * The currently authenticated user if provided in the `constructor`.
526
+ */
527
+ public readonly username: string;
528
+
529
+ /**
530
+ * The currently authenticated user's password if provided in the `constructor`.
531
+ */
532
+ public readonly password: string;
533
+
534
+ /**
535
+ * The current portal the user is authenticated with.
536
+ */
537
+ public readonly portal: string;
538
+
539
+ /**
540
+ * This value is set to true automatically if the ArcGIS Organization requires that requests be made over https.
541
+ */
542
+ public readonly ssl: boolean;
543
+
544
+ /**
545
+ * The authentication provider to use.
546
+ */
547
+ public readonly provider: AuthenticationProvider;
548
+
549
+ /**
550
+ * Determines how long new tokens requested are valid.
551
+ */
552
+ public readonly tokenDuration: number;
553
+
554
+ /**
555
+ * A valid redirect URI for this application if provided in the `constructor`.
556
+ */
557
+ public readonly redirectUri: string;
558
+
559
+ /**
560
+ * Duration of new OAuth 2.0 refresh token validity.
561
+ */
562
+ public readonly refreshTokenTTL: number;
563
+
564
+ /**
565
+ * An unfederated ArcGIS Server instance known to recognize credentials supplied manually.
566
+ * ```js
567
+ * {
568
+ * server: "https://sampleserver6.arcgisonline.com/arcgis",
569
+ * token: "SOSlV3v..",
570
+ * tokenExpires: new Date(1545415669763)
571
+ * }
572
+ * ```
573
+ */
574
+ public readonly server: string;
575
+
576
+ /**
577
+ * Hydrated by a call to [getUser()](#getUser-summary).
578
+ */
579
+ private _user: IUser;
580
+
581
+ private _token: string;
582
+ private _tokenExpires: Date;
583
+ private _refreshToken: string;
584
+ private _refreshTokenExpires: Date;
585
+
586
+ /**
587
+ * Internal object to keep track of pending token requests. Used to prevent
588
+ * duplicate token requests.
589
+ */
590
+ private _pendingTokenRequests: {
591
+ [key: string]: Promise<string>;
592
+ };
593
+
594
+ /**
595
+ * Internal list of trusted 3rd party servers (federated servers) that have
596
+ * been validated with `generateToken`.
597
+ */
598
+ private trustedServers: {
599
+ [key: string]: {
600
+ token: string;
601
+ expires: Date;
602
+ };
603
+ };
604
+
605
+ /**
606
+ * The current ArcGIS Online or ArcGIS Enterprise `token`.
607
+ */
608
+ get token() {
609
+ return this._token;
610
+ }
611
+
612
+ /**
613
+ * The expiration time of the current `token`.
614
+ */
615
+ get tokenExpires() {
616
+ return this._tokenExpires;
617
+ }
618
+
619
+ /**
620
+ * The current token to ArcGIS Online or ArcGIS Enterprise.
621
+ */
622
+ get refreshToken() {
623
+ return this._refreshToken;
624
+ }
625
+
626
+ /**
627
+ * The expiration time of the current `refreshToken`.
628
+ */
629
+ get refreshTokenExpires() {
630
+ return this._refreshTokenExpires;
631
+ }
632
+
633
+ constructor(options: IUserSessionOptions) {
634
+ this.clientId = options.clientId;
635
+ this._refreshToken = options.refreshToken;
636
+ this._refreshTokenExpires = options.refreshTokenExpires;
637
+ this.username = options.username;
638
+ this.password = options.password;
639
+ this._token = options.token;
640
+ this._tokenExpires = options.tokenExpires;
641
+ this.portal = options.portal
642
+ ? cleanUrl(options.portal)
643
+ : "https://www.arcgis.com/sharing/rest";
644
+ this.ssl = options.ssl;
645
+ this.provider = options.provider || "arcgis";
646
+ this.tokenDuration = options.tokenDuration || 20160;
647
+ this.redirectUri = options.redirectUri;
648
+ this.refreshTokenTTL = options.refreshTokenTTL || 1440;
649
+
650
+ this.trustedServers = {};
651
+ // if a non-federated server was passed explicitly, it should be trusted.
652
+ if (options.server) {
653
+ // if the url includes more than '/arcgis/', trim the rest
654
+ const root = this.getServerRootUrl(options.server);
655
+
656
+ this.trustedServers[root] = {
657
+ token: options.token,
658
+ expires: options.tokenExpires
659
+ };
660
+ }
661
+ this._pendingTokenRequests = {};
662
+ }
663
+
664
+ /**
665
+ * Returns authentication in a format useable in the [ArcGIS API for JavaScript](https://developers.arcgis.com/javascript/).
666
+ *
667
+ * ```js
668
+ * esriId.registerToken(session.toCredential());
669
+ * ```
670
+ *
671
+ * @returns ICredential
672
+ */
673
+ public toCredential(): ICredential {
674
+ return {
675
+ expires: this.tokenExpires.getTime(),
676
+ server: this.portal,
677
+ ssl: this.ssl,
678
+ token: this.token,
679
+ userId: this.username
680
+ };
681
+ }
682
+
683
+ /**
684
+ * Returns information about the currently logged in [user](https://developers.arcgis.com/rest/users-groups-and-items/user.htm). Subsequent calls will *not* result in additional web traffic.
685
+ *
686
+ * ```js
687
+ * session.getUser()
688
+ * .then(response => {
689
+ * console.log(response.role); // "org_admin"
690
+ * })
691
+ * ```
692
+ *
693
+ * @param requestOptions - Options for the request. NOTE: `rawResponse` is not supported by this operation.
694
+ * @returns A Promise that will resolve with the data from the response.
695
+ */
696
+ public getUser(requestOptions?: IRequestOptions): Promise<IUser> {
697
+ if (this._user && this._user.username === this.username) {
698
+ return Promise.resolve(this._user);
699
+ } else {
700
+ const url = `${this.portal}/community/users/${encodeURIComponent(
701
+ this.username
702
+ )}`;
703
+
704
+ const options = {
705
+ httpMethod: "GET",
706
+ authentication: this,
707
+ ...requestOptions,
708
+ rawResponse: false
709
+ } as IRequestOptions;
710
+ return request(url, options).then(response => {
711
+ this._user = response;
712
+ return response;
713
+ });
714
+ }
715
+ }
716
+
717
+ /**
718
+ * Gets an appropriate token for the given URL. If `portal` is ArcGIS Online and
719
+ * the request is to an ArcGIS Online domain `token` will be used. If the request
720
+ * is to the current `portal` the current `token` will also be used. However if
721
+ * the request is to an unknown server we will validate the server with a request
722
+ * to our current `portal`.
723
+ */
724
+ public getToken(url: string, requestOptions?: ITokenRequestOptions) {
725
+ if (
726
+ (arcgisOnlinePortalRegex.test(this.portal) ||
727
+ arcgisOnlineOrgPortalRegex.test(this.portal)) &&
728
+ arcgisOnlineUrlRegex.test(url)
729
+ ) {
730
+ return this.getFreshToken(requestOptions);
731
+ } else if (new RegExp(this.portal, "i").test(url)) {
732
+ return this.getFreshToken(requestOptions);
733
+ } else {
734
+ return this.getTokenForServer(url, requestOptions);
735
+ }
736
+ }
737
+
738
+ public toJSON(): IUserSessionOptions {
739
+ return {
740
+ clientId: this.clientId,
741
+ refreshToken: this.refreshToken,
742
+ refreshTokenExpires: this.refreshTokenExpires,
743
+ username: this.username,
744
+ password: this.password,
745
+ token: this.token,
746
+ tokenExpires: this.tokenExpires,
747
+ portal: this.portal,
748
+ ssl: this.ssl,
749
+ tokenDuration: this.tokenDuration,
750
+ redirectUri: this.redirectUri,
751
+ refreshTokenTTL: this.refreshTokenTTL
752
+ };
753
+ }
754
+
755
+ public serialize() {
756
+ return JSON.stringify(this);
757
+ }
758
+
759
+ /**
760
+ * Manually refreshes the current `token` and `tokenExpires`.
761
+ */
762
+ public refreshSession(
763
+ requestOptions?: ITokenRequestOptions
764
+ ): Promise<UserSession> {
765
+ // make sure subsequent calls to getUser() don't returned cached metadata
766
+ this._user = null;
767
+ if (this.username && this.password) {
768
+ return this.refreshWithUsernameAndPassword(requestOptions);
769
+ }
770
+
771
+ if (this.clientId && this.refreshToken) {
772
+ return this.refreshWithRefreshToken();
773
+ }
774
+
775
+ return Promise.reject(new ArcGISAuthError("Unable to refresh token."));
776
+ }
777
+
778
+ /**
779
+ * Determines the root of the ArcGIS Server or Portal for a given URL.
780
+ *
781
+ * @param url the URl to determine the root url for.
782
+ */
783
+ public getServerRootUrl(url: string) {
784
+ const [root] = url.split(/\/rest(\/admin)?\/services(?:\/|#|\?|$)/);
785
+ const [match, protocol, domainAndPath] = root.match(/(https?:\/\/)(.+)/);
786
+ const [domain, ...path] = domainAndPath.split("/");
787
+
788
+ // only the domain is lowercased becasue in some cases an org id might be
789
+ // in the path which cannot be lowercased.
790
+ return `${protocol}${domain.toLowerCase()}/${path.join("/")}`;
791
+ }
792
+
793
+ /**
794
+ * Validates that a given URL is properly federated with our current `portal`.
795
+ * Attempts to use the internal `trustedServers` cache first.
796
+ */
797
+ private getTokenForServer(
798
+ url: string,
799
+ requestOptions?: ITokenRequestOptions
800
+ ) {
801
+ // requests to /rest/services/ and /rest/admin/services/ are both valid
802
+ // Federated servers may have inconsistent casing, so lowerCase it
803
+ const root = this.getServerRootUrl(url);
804
+ const existingToken = this.trustedServers[root];
805
+
806
+ if (
807
+ existingToken &&
808
+ existingToken.expires &&
809
+ existingToken.expires.getTime() > Date.now()
810
+ ) {
811
+ return Promise.resolve(existingToken.token);
812
+ }
813
+
814
+ if (this._pendingTokenRequests[root]) {
815
+ return this._pendingTokenRequests[root];
816
+ }
817
+
818
+ this._pendingTokenRequests[root] = request(`${root}/rest/info`)
819
+ .then(response => {
820
+ if (response.owningSystemUrl) {
821
+ /**
822
+ * if this server is not owned by this portal
823
+ * bail out with an error since we know we wont
824
+ * be able to generate a token
825
+ */
826
+ if (!new RegExp(response.owningSystemUrl, "i").test(this.portal)) {
827
+ throw new ArcGISAuthError(
828
+ `${url} is not federated with ${this.portal}.`,
829
+ "NOT_FEDERATED"
830
+ );
831
+ } else {
832
+ /**
833
+ * if the server is federated, use the relevant token endpoint.
834
+ */
835
+ return request(
836
+ `${response.owningSystemUrl}/sharing/rest/info`,
837
+ requestOptions
838
+ );
839
+ }
840
+ } else if (
841
+ response.authInfo &&
842
+ this.trustedServers[root] !== undefined
843
+ ) {
844
+ /**
845
+ * if its a stand-alone instance of ArcGIS Server that doesn't advertise
846
+ * federation, but the root server url is recognized, use its built in token endpoint.
847
+ */
848
+ return Promise.resolve({ authInfo: response.authInfo });
849
+ } else {
850
+ throw new ArcGISAuthError(
851
+ `${url} is not federated with any portal and is not explicitly trusted.`,
852
+ "NOT_FEDERATED"
853
+ );
854
+ }
855
+ })
856
+ .then((response: any) => {
857
+ return response.authInfo.tokenServicesUrl;
858
+ })
859
+ .then((tokenServicesUrl: string) => {
860
+ // an expired token cant be used to generate a new token
861
+ if (this.token && this.tokenExpires.getTime() > Date.now()) {
862
+ return generateToken(tokenServicesUrl, {
863
+ params: {
864
+ token: this.token,
865
+ serverUrl: url,
866
+ expiration: this.tokenDuration,
867
+ client: "referer"
868
+ }
869
+ });
870
+ // generate an entirely fresh token if necessary
871
+ } else {
872
+ return generateToken(tokenServicesUrl, {
873
+ params: {
874
+ username: this.username,
875
+ password: this.password,
876
+ expiration: this.tokenDuration,
877
+ client: "referer"
878
+ }
879
+ }).then((response: any) => {
880
+ this._token = response.token;
881
+ this._tokenExpires = new Date(response.expires);
882
+ return response;
883
+ });
884
+ }
885
+ })
886
+ .then(response => {
887
+ this.trustedServers[root] = {
888
+ expires: new Date(response.expires),
889
+ token: response.token
890
+ };
891
+ return response.token;
892
+ });
893
+
894
+ return this._pendingTokenRequests[root];
895
+ }
896
+
897
+ /**
898
+ * Returns an unexpired token for the current `portal`.
899
+ */
900
+ private getFreshToken(requestOptions?: ITokenRequestOptions) {
901
+ if (
902
+ this.token &&
903
+ this.tokenExpires &&
904
+ this.tokenExpires.getTime() > Date.now()
905
+ ) {
906
+ return Promise.resolve(this.token);
907
+ }
908
+
909
+ if (!this._pendingTokenRequests[this.portal]) {
910
+ this._pendingTokenRequests[this.portal] = this.refreshSession(
911
+ requestOptions
912
+ ).then(session => {
913
+ this._pendingTokenRequests[this.portal] = null;
914
+ return session.token;
915
+ });
916
+ }
917
+
918
+ return this._pendingTokenRequests[this.portal];
919
+ }
920
+
921
+ /**
922
+ * Refreshes the current `token` and `tokenExpires` with `username` and
923
+ * `password`.
924
+ */
925
+ private refreshWithUsernameAndPassword(
926
+ requestOptions?: ITokenRequestOptions
927
+ ) {
928
+ const options = {
929
+ params: {
930
+ username: this.username,
931
+ password: this.password,
932
+ expiration: this.tokenDuration
933
+ },
934
+ ...requestOptions
935
+ };
936
+ return generateToken(`${this.portal}/generateToken`, options).then(
937
+ (response: any) => {
938
+ this._token = response.token;
939
+ this._tokenExpires = new Date(response.expires);
940
+ return this;
941
+ }
942
+ );
943
+ }
944
+
945
+ /**
946
+ * Refreshes the current `token` and `tokenExpires` with `refreshToken`.
947
+ */
948
+ private refreshWithRefreshToken(requestOptions?: ITokenRequestOptions) {
949
+ if (
950
+ this.refreshToken &&
951
+ this.refreshTokenExpires &&
952
+ this.refreshTokenExpires.getTime() < Date.now()
953
+ ) {
954
+ return this.refreshRefreshToken(requestOptions);
955
+ }
956
+
957
+ const options: ITokenRequestOptions = {
958
+ params: {
959
+ client_id: this.clientId,
960
+ refresh_token: this.refreshToken,
961
+ grant_type: "refresh_token"
962
+ },
963
+ ...requestOptions
964
+ };
965
+ return fetchToken(`${this.portal}/oauth2/token`, options).then(response => {
966
+ this._token = response.token;
967
+ this._tokenExpires = response.expires;
968
+ return this;
969
+ });
970
+ }
971
+
972
+ /**
973
+ * Exchanges an unexpired `refreshToken` for a new one, also updates `token` and
974
+ * `tokenExpires`.
975
+ */
976
+ private refreshRefreshToken(requestOptions?: ITokenRequestOptions) {
977
+ const options: ITokenRequestOptions = {
978
+ params: {
979
+ client_id: this.clientId,
980
+ refresh_token: this.refreshToken,
981
+ redirect_uri: this.redirectUri,
982
+ grant_type: "exchange_refresh_token"
983
+ },
984
+ ...requestOptions
985
+ };
986
+
987
+ return fetchToken(`${this.portal}/oauth2/token`, options).then(response => {
988
+ this._token = response.token;
989
+ this._tokenExpires = response.expires;
990
+ this._refreshToken = response.refreshToken;
991
+ this._refreshTokenExpires = new Date(
992
+ Date.now() + (this.refreshTokenTTL - 1) * 60 * 1000
993
+ );
994
+ return this;
995
+ });
996
+ }
997
+ }