etincidunt 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (226) hide show
  1. package/.prettierignore +2 -0
  2. package/.travis.yml +29 -0
  3. package/.vscode/launch.json +24 -0
  4. package/.vscode/settings.json +3 -0
  5. package/demos/ago-node-cli/README.md +29 -0
  6. package/demos/ago-node-cli/ago.js +32 -0
  7. package/demos/ago-node-cli/index.js +11 -0
  8. package/demos/ago-node-cli/lib/item-export-command.js +48 -0
  9. package/demos/ago-node-cli/lib/item-search-command.js +35 -0
  10. package/demos/ago-node-cli/package-lock.json +172 -0
  11. package/demos/ago-node-cli/package.json +30 -0
  12. package/demos/attachments/README.md +5 -0
  13. package/demos/attachments/index.html +164 -0
  14. package/demos/attachments/package-lock.json +182 -0
  15. package/demos/attachments/package.json +18 -0
  16. package/demos/batch-geocoder-node/NYC_Restaurant_Inspection_Results.csv +100 -0
  17. package/demos/batch-geocoder-node/README.md +14 -0
  18. package/demos/batch-geocoder-node/batch-geocode.js +112 -0
  19. package/demos/batch-geocoder-node/config-template.js +18 -0
  20. package/demos/batch-geocoder-node/package-lock.json +109 -0
  21. package/demos/batch-geocoder-node/package.json +37 -0
  22. package/demos/express/README.md +10 -0
  23. package/demos/express/config.json.template +3 -0
  24. package/demos/express/package-lock.json +388 -0
  25. package/demos/express/package.json +18 -0
  26. package/demos/express/server.js +28 -0
  27. package/demos/feature-service-browser/README.md +6 -0
  28. package/demos/feature-service-browser/index.html +122 -0
  29. package/demos/feature-service-browser/package-lock.json +182 -0
  30. package/demos/feature-service-browser/package.json +18 -0
  31. package/demos/geocoder-browser/README.md +10 -0
  32. package/demos/geocoder-browser/config.js.template +1 -0
  33. package/demos/geocoder-browser/index.html +131 -0
  34. package/demos/geocoder-browser/package-lock.json +163 -0
  35. package/demos/geocoder-browser/package.json +19 -0
  36. package/demos/geocoder-browser/post-sign-in.html +25 -0
  37. package/demos/jsapi-integration/README.md +8 -0
  38. package/demos/jsapi-integration/config.js +6 -0
  39. package/demos/jsapi-integration/index.html +79 -0
  40. package/demos/jsapi-integration/package-lock.json +184 -0
  41. package/demos/jsapi-integration/package.json +19 -0
  42. package/demos/oauth2-browser/README.md +12 -0
  43. package/demos/oauth2-browser/authenticate.html +32 -0
  44. package/demos/oauth2-browser/config.js.template +6 -0
  45. package/demos/oauth2-browser/index.html +202 -0
  46. package/demos/oauth2-browser/logo.svg +4 -0
  47. package/demos/oauth2-browser/package-lock.json +163 -0
  48. package/demos/oauth2-browser/package.json +18 -0
  49. package/demos/oauth2-browser/style.css +36 -0
  50. package/demos/vue/.babelrc +6 -0
  51. package/demos/vue/.env.example +8 -0
  52. package/demos/vue/README.md +17 -0
  53. package/demos/vue/index.html +21 -0
  54. package/demos/vue/package-lock.json +7236 -0
  55. package/demos/vue/package.json +39 -0
  56. package/demos/vue/src/assets/logo.svg +29 -0
  57. package/demos/vue/src/components/App.vue +302 -0
  58. package/demos/vue/src/components/Authenticate.vue +68 -0
  59. package/demos/vue/src/components/Loader.vue +216 -0
  60. package/demos/vue/src/main.js +75 -0
  61. package/demos/vue/webpack.config.js +84 -0
  62. package/docs/FAQ.md +28 -0
  63. package/docs/HISTORY.md +62 -0
  64. package/docs/acetate.config.js +214 -0
  65. package/docs/build-typedoc.js +301 -0
  66. package/docs/src/_layout.html +82 -0
  67. package/docs/src/api/_declaration.html +496 -0
  68. package/docs/src/api/_layout.html +127 -0
  69. package/docs/src/api/_package.html +13 -0
  70. package/docs/src/api/index.html +23 -0
  71. package/docs/src/guides/_layout.html +24 -0
  72. package/docs/src/guides/amd-requirejs-dojo.md +40 -0
  73. package/docs/src/guides/babel-and-rollup.md +30 -0
  74. package/docs/src/guides/babel-and-webpack.md +30 -0
  75. package/docs/src/guides/browser-authentication.md +9 -0
  76. package/docs/src/guides/browserify.md +9 -0
  77. package/docs/src/guides/cli-authentication.md +9 -0
  78. package/docs/src/guides/client-server-authentication.md +9 -0
  79. package/docs/src/guides/from-a-cdn.md +36 -0
  80. package/docs/src/guides/index.md +52 -0
  81. package/docs/src/guides/node.md +30 -0
  82. package/docs/src/guides/package-overview.md +8 -0
  83. package/docs/src/guides/server-authentication.md +9 -0
  84. package/docs/src/guides/typescript-and-webpack.md +9 -0
  85. package/docs/src/img/icons.png +0 -0
  86. package/docs/src/img/icons@2x.png +0 -0
  87. package/docs/src/index.html +12 -0
  88. package/docs/src/js/api-search.js +113 -0
  89. package/docs/src/js/index.js +1 -0
  90. package/docs/src/js/nav-toggle.js +41 -0
  91. package/docs/src/sass/_highlight.scss +96 -0
  92. package/docs/src/sass/_icons.scss +157 -0
  93. package/docs/src/sass/style.scss +169 -0
  94. package/jasmine.json +7 -0
  95. package/karma.conf.js +100 -0
  96. package/lerna.json +8 -0
  97. package/notes/README.md +88 -0
  98. package/package.json +91 -0
  99. package/packages/arcgis-rest-auth/README.md +64 -0
  100. package/packages/arcgis-rest-auth/package-lock.json +11 -0
  101. package/packages/arcgis-rest-auth/package.json +51 -0
  102. package/packages/arcgis-rest-auth/src/ApplicationSession.ts +91 -0
  103. package/packages/arcgis-rest-auth/src/UserSession.ts +829 -0
  104. package/packages/arcgis-rest-auth/src/authenticated-request-options.ts +21 -0
  105. package/packages/arcgis-rest-auth/src/fetch-token.ts +55 -0
  106. package/packages/arcgis-rest-auth/src/generate-token.ts +36 -0
  107. package/packages/arcgis-rest-auth/src/index.ts +5 -0
  108. package/packages/arcgis-rest-auth/test/ApplicationSession.test.ts +121 -0
  109. package/packages/arcgis-rest-auth/test/UserSession.test.ts +883 -0
  110. package/packages/arcgis-rest-auth/test/fetchToken.test.ts +76 -0
  111. package/packages/arcgis-rest-auth/test/generateToken.test.ts +36 -0
  112. package/packages/arcgis-rest-auth/test/utils.ts +11 -0
  113. package/packages/arcgis-rest-auth/tsconfig.json +6 -0
  114. package/packages/arcgis-rest-common-types/README.md +61 -0
  115. package/packages/arcgis-rest-common-types/package.json +38 -0
  116. package/packages/arcgis-rest-common-types/src/group.ts +51 -0
  117. package/packages/arcgis-rest-common-types/src/index.ts +467 -0
  118. package/packages/arcgis-rest-common-types/src/item.ts +45 -0
  119. package/packages/arcgis-rest-common-types/src/webmap.ts +1225 -0
  120. package/packages/arcgis-rest-common-types/tsconfig.json +11 -0
  121. package/packages/arcgis-rest-feature-service/README.md +70 -0
  122. package/packages/arcgis-rest-feature-service/package-lock.json +11 -0
  123. package/packages/arcgis-rest-feature-service/package.json +50 -0
  124. package/packages/arcgis-rest-feature-service/src/add.ts +82 -0
  125. package/packages/arcgis-rest-feature-service/src/addAttachment.ts +65 -0
  126. package/packages/arcgis-rest-feature-service/src/delete.ts +85 -0
  127. package/packages/arcgis-rest-feature-service/src/deleteAttachments.ts +68 -0
  128. package/packages/arcgis-rest-feature-service/src/getAttachments.ts +64 -0
  129. package/packages/arcgis-rest-feature-service/src/helpers.ts +77 -0
  130. package/packages/arcgis-rest-feature-service/src/index.ts +8 -0
  131. package/packages/arcgis-rest-feature-service/src/query.ts +174 -0
  132. package/packages/arcgis-rest-feature-service/src/update.ts +81 -0
  133. package/packages/arcgis-rest-feature-service/src/updateAttachment.ts +74 -0
  134. package/packages/arcgis-rest-feature-service/test/attachments.test.ts +179 -0
  135. package/packages/arcgis-rest-feature-service/test/features.test.ts +172 -0
  136. package/packages/arcgis-rest-feature-service/test/mocks/feature.ts +220 -0
  137. package/packages/arcgis-rest-feature-service/test/mocks/foo.txt +1 -0
  138. package/packages/arcgis-rest-feature-service/tsconfig.json +6 -0
  139. package/packages/arcgis-rest-geocoder/README.md +73 -0
  140. package/packages/arcgis-rest-geocoder/package-lock.json +11 -0
  141. package/packages/arcgis-rest-geocoder/package.json +52 -0
  142. package/packages/arcgis-rest-geocoder/src/bulk.ts +102 -0
  143. package/packages/arcgis-rest-geocoder/src/geocode.ts +117 -0
  144. package/packages/arcgis-rest-geocoder/src/helpers.ts +81 -0
  145. package/packages/arcgis-rest-geocoder/src/index.ts +4 -0
  146. package/packages/arcgis-rest-geocoder/src/reverse.ts +84 -0
  147. package/packages/arcgis-rest-geocoder/src/suggest.ts +72 -0
  148. package/packages/arcgis-rest-geocoder/test/geocoder.test.ts +510 -0
  149. package/packages/arcgis-rest-geocoder/test/mocks/responses.ts +588 -0
  150. package/packages/arcgis-rest-geocoder/tsconfig.json +6 -0
  151. package/packages/arcgis-rest-groups/README.md +64 -0
  152. package/packages/arcgis-rest-groups/package-lock.json +11 -0
  153. package/packages/arcgis-rest-groups/package.json +52 -0
  154. package/packages/arcgis-rest-groups/src/groups.ts +272 -0
  155. package/packages/arcgis-rest-groups/src/index.ts +1 -0
  156. package/packages/arcgis-rest-groups/test/groups.test.ts +280 -0
  157. package/packages/arcgis-rest-groups/test/mocks/responses.ts +137 -0
  158. package/packages/arcgis-rest-groups/tsconfig.json +6 -0
  159. package/packages/arcgis-rest-items/README.md +66 -0
  160. package/packages/arcgis-rest-items/package-lock.json +11 -0
  161. package/packages/arcgis-rest-items/package.json +52 -0
  162. package/packages/arcgis-rest-items/src/index.ts +1 -0
  163. package/packages/arcgis-rest-items/src/items.ts +498 -0
  164. package/packages/arcgis-rest-items/test/items.test.ts +1153 -0
  165. package/packages/arcgis-rest-items/test/mocks/foo.zip +0 -0
  166. package/packages/arcgis-rest-items/test/mocks/item.ts +30 -0
  167. package/packages/arcgis-rest-items/test/mocks/resources.ts +28 -0
  168. package/packages/arcgis-rest-items/test/mocks/search.ts +60 -0
  169. package/packages/arcgis-rest-items/tsconfig.json +6 -0
  170. package/packages/arcgis-rest-request/README.md +65 -0
  171. package/packages/arcgis-rest-request/package-lock.json +11 -0
  172. package/packages/arcgis-rest-request/package.json +42 -0
  173. package/packages/arcgis-rest-request/src/index.ts +10 -0
  174. package/packages/arcgis-rest-request/src/request.ts +259 -0
  175. package/packages/arcgis-rest-request/src/utils/ArcGISAuthError.ts +67 -0
  176. package/packages/arcgis-rest-request/src/utils/ArcGISRequestError.ts +73 -0
  177. package/packages/arcgis-rest-request/src/utils/ErrorTypes.ts +29 -0
  178. package/packages/arcgis-rest-request/src/utils/check-for-errors.ts +65 -0
  179. package/packages/arcgis-rest-request/src/utils/encode-form-data.ts +29 -0
  180. package/packages/arcgis-rest-request/src/utils/encode-query-string.ts +23 -0
  181. package/packages/arcgis-rest-request/src/utils/get-portal-url.ts +25 -0
  182. package/packages/arcgis-rest-request/src/utils/get-portal.ts +45 -0
  183. package/packages/arcgis-rest-request/src/utils/process-params.ts +99 -0
  184. package/packages/arcgis-rest-request/test/mocks/errors.ts +59 -0
  185. package/packages/arcgis-rest-request/test/mocks/geojson-feature-collection.ts +10 -0
  186. package/packages/arcgis-rest-request/test/mocks/portal.ts +109 -0
  187. package/packages/arcgis-rest-request/test/mocks/sharing-rest-info.ts +38 -0
  188. package/packages/arcgis-rest-request/test/mocks/webmap.ts +38 -0
  189. package/packages/arcgis-rest-request/test/request.test.ts +296 -0
  190. package/packages/arcgis-rest-request/test/utils/ArcGISAuthError.test.ts +167 -0
  191. package/packages/arcgis-rest-request/test/utils/ArcGISRequestError.test.ts +40 -0
  192. package/packages/arcgis-rest-request/test/utils/check-for-errors.test.ts +101 -0
  193. package/packages/arcgis-rest-request/test/utils/encode-form-data.test.ts +112 -0
  194. package/packages/arcgis-rest-request/test/utils/get-portal-url.test.ts +34 -0
  195. package/packages/arcgis-rest-request/test/utils/portal.test.ts +94 -0
  196. package/packages/arcgis-rest-request/test/utils/process-params.test.ts +190 -0
  197. package/packages/arcgis-rest-request/tsconfig.json +6 -0
  198. package/packages/arcgis-rest-sharing/README.md +67 -0
  199. package/packages/arcgis-rest-sharing/package-lock.json +11 -0
  200. package/packages/arcgis-rest-sharing/package.json +55 -0
  201. package/packages/arcgis-rest-sharing/src/access.ts +91 -0
  202. package/packages/arcgis-rest-sharing/src/group-sharing.ts +212 -0
  203. package/packages/arcgis-rest-sharing/src/helpers.ts +92 -0
  204. package/packages/arcgis-rest-sharing/src/index.ts +2 -0
  205. package/packages/arcgis-rest-sharing/test/access.test.ts +153 -0
  206. package/packages/arcgis-rest-sharing/test/group-sharing.test.ts +436 -0
  207. package/packages/arcgis-rest-sharing/test/mocks/sharing.ts +15 -0
  208. package/packages/arcgis-rest-sharing/tsconfig.json +6 -0
  209. package/packages/arcgis-rest-users/README.md +71 -0
  210. package/packages/arcgis-rest-users/package-lock.json +11 -0
  211. package/packages/arcgis-rest-users/package.json +51 -0
  212. package/packages/arcgis-rest-users/src/index.ts +1 -0
  213. package/packages/arcgis-rest-users/src/users.ts +70 -0
  214. package/packages/arcgis-rest-users/test/mocks/responses.ts +170 -0
  215. package/packages/arcgis-rest-users/test/users.test.ts +97 -0
  216. package/packages/arcgis-rest-users/tsconfig.json +6 -0
  217. package/support/FormData.d.ts +1 -0
  218. package/support/changelog.js +388 -0
  219. package/support/commit-template.txt +19 -0
  220. package/support/deploy-doc-site.js +16 -0
  221. package/support/publish.sh +40 -0
  222. package/support/test-helpers.js +8 -0
  223. package/tsconfig.json +69 -0
  224. package/tslint.json +14 -0
  225. package/umd-base-profile.js +82 -0
  226. package/umd-production-profile.js +13 -0
@@ -0,0 +1,296 @@
1
+ import { request, ErrorTypes } from "../src/index";
2
+ import * as fetchMock from "fetch-mock";
3
+ import {
4
+ SharingRestInfo,
5
+ SharingRestInfoHTML
6
+ } from "./mocks/sharing-rest-info";
7
+ import { ArcGISOnlineError } from "./mocks/errors";
8
+ import { WebMapAsText, WebMapAsJSON } from "./mocks/webmap";
9
+ import { GeoJSONFeatureCollection } from "./mocks/geojson-feature-collection";
10
+
11
+ describe("request()", () => {
12
+ afterEach(fetchMock.restore);
13
+
14
+ it("should make a basic POST request", done => {
15
+ fetchMock.once("*", SharingRestInfo);
16
+
17
+ request("https://www.arcgis.com/sharing/rest/info")
18
+ .then(response => {
19
+ const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
20
+ expect(url).toEqual("https://www.arcgis.com/sharing/rest/info");
21
+ expect(options.method).toBe("POST");
22
+ expect(response).toEqual(SharingRestInfo);
23
+ expect(options.body).toContain("f=json");
24
+ done();
25
+ })
26
+ .catch(e => {
27
+ fail(e);
28
+ });
29
+ });
30
+
31
+ it("should make a basic GET request", done => {
32
+ fetchMock.once("*", SharingRestInfo);
33
+
34
+ request("https://www.arcgis.com/sharing/rest/info", {
35
+ httpMethod: "GET"
36
+ })
37
+ .then(response => {
38
+ const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
39
+ expect(url).toEqual("https://www.arcgis.com/sharing/rest/info?f=json");
40
+ expect(options.method).toBe("GET");
41
+ expect(response).toEqual(SharingRestInfo);
42
+ done();
43
+ })
44
+ .catch(e => {
45
+ fail(e);
46
+ });
47
+ });
48
+
49
+ it("should make a basic GET request for text", done => {
50
+ fetchMock.once("*", WebMapAsText);
51
+
52
+ request(
53
+ "https://www.arcgis.com/sharing/rest/content/items/43a8e51789044d9480a20089a84129ad/data",
54
+ {
55
+ httpMethod: "GET",
56
+ params: { f: "text" }
57
+ }
58
+ )
59
+ .then(response => {
60
+ const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
61
+ expect(url).toEqual(
62
+ "https://www.arcgis.com/sharing/rest/content/items/43a8e51789044d9480a20089a84129ad/data?f=text"
63
+ );
64
+ expect(options.method).toBe("GET");
65
+ expect(response).toEqual(WebMapAsText);
66
+ done();
67
+ })
68
+ .catch(e => {
69
+ fail(e);
70
+ });
71
+ });
72
+
73
+ it("should make a basic GET request for html", done => {
74
+ fetchMock.once("*", SharingRestInfoHTML);
75
+
76
+ request("https://www.arcgis.com/sharing/rest/info", {
77
+ httpMethod: "GET",
78
+ params: { f: "html" }
79
+ })
80
+ .then(response => {
81
+ const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
82
+ expect(url).toEqual("https://www.arcgis.com/sharing/rest/info?f=html");
83
+ expect(options.method).toBe("GET");
84
+ expect(response).toEqual(SharingRestInfoHTML);
85
+ done();
86
+ })
87
+ .catch(e => {
88
+ fail(e);
89
+ });
90
+ });
91
+
92
+ it("should make a basic GET request for geojson", done => {
93
+ fetchMock.once("*", GeoJSONFeatureCollection);
94
+
95
+ request(
96
+ "https://services1.arcgis.com/ORG/arcgis/rest/services/FEATURE_SERVICE/FeatureServer/0/query",
97
+ {
98
+ httpMethod: "GET",
99
+ params: { where: "1=1", f: "geojson" }
100
+ }
101
+ )
102
+ .then(response => {
103
+ const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
104
+ expect(url).toEqual(
105
+ "https://services1.arcgis.com/ORG/arcgis/rest/services/FEATURE_SERVICE/FeatureServer/0/query?f=geojson&where=1%3D1"
106
+ );
107
+ expect(options.method).toBe("GET");
108
+ expect(response).toEqual(GeoJSONFeatureCollection);
109
+ done();
110
+ })
111
+ .catch(e => {
112
+ fail(e);
113
+ });
114
+ });
115
+
116
+ it("should switch from GET to POST when url is longer than specified", done => {
117
+ fetchMock.once("*", SharingRestInfo);
118
+ const restInfoUrl = "https://www.arcgis.com/sharing/rest/info";
119
+
120
+ request(restInfoUrl, {
121
+ httpMethod: "GET",
122
+ // typically consumers would base maxUrlLength on browser/server limits
123
+ // but for testing, we use an artificially low limit
124
+ // like this one that assumes no parameters will be added
125
+ maxUrlLength: restInfoUrl.length
126
+ })
127
+ .then(response => {
128
+ const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
129
+ expect(url).toEqual("https://www.arcgis.com/sharing/rest/info");
130
+ expect(options.method).toBe("POST");
131
+ expect(options.body).toContain("f=json");
132
+ expect(response).toEqual(SharingRestInfo);
133
+ done();
134
+ })
135
+ .catch(e => {
136
+ fail(e);
137
+ });
138
+ });
139
+
140
+ it("should use the `authentication` option to authenticate a request", done => {
141
+ fetchMock.once("*", WebMapAsText);
142
+
143
+ const MOCK_AUTH = {
144
+ portal: "https://www.arcgis.com/sharing/rest",
145
+ getToken() {
146
+ return Promise.resolve("token");
147
+ }
148
+ };
149
+
150
+ request(
151
+ "https://www.arcgis.com/sharing/rest/content/items/43a8e51789044d9480a20089a84129ad/data",
152
+ {
153
+ authentication: MOCK_AUTH
154
+ }
155
+ )
156
+ .then(response => {
157
+ const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
158
+ expect(url).toEqual(
159
+ "https://www.arcgis.com/sharing/rest/content/items/43a8e51789044d9480a20089a84129ad/data"
160
+ );
161
+ expect(options.body).toContain("token=token");
162
+ expect(response).toEqual(WebMapAsJSON);
163
+ done();
164
+ })
165
+ .catch(e => {
166
+ fail(e);
167
+ });
168
+ });
169
+
170
+ it("should re-throw HTTP errors (404, 500, etc)", done => {
171
+ fetchMock.once("*", 404);
172
+
173
+ request(
174
+ "https://www.arcgis.com/sharing/rest/content/items/43a8e51789044d9480a20089a84129ad/data"
175
+ ).catch(error => {
176
+ expect(error.name).toBe(ErrorTypes.ArcGISRequestError);
177
+ expect(error.message).toBe("HTTP 404: Not Found");
178
+ expect(error instanceof Error).toBeTruthy();
179
+ expect(error.url).toBe(
180
+ "https://www.arcgis.com/sharing/rest/content/items/43a8e51789044d9480a20089a84129ad/data"
181
+ );
182
+ expect(error.options.params).toEqual({ f: "json" });
183
+ expect(error.options.httpMethod).toEqual("POST");
184
+ expect(typeof error.options.fetch).toEqual("function");
185
+ expect(error.options.fetch.length).toEqual(2);
186
+ done();
187
+ });
188
+ });
189
+
190
+ it("should throw errors with information about the request", done => {
191
+ fetchMock.once("*", ArcGISOnlineError);
192
+
193
+ request(
194
+ "https://www.arcgis.com/sharing/rest/content/items/43a8e51789044d9480a20089a84129ad/data"
195
+ ).catch(error => {
196
+ expect(error.name).toBe(ErrorTypes.ArcGISRequestError);
197
+ expect(error.message).toBe("400: 'type' and 'title' property required.");
198
+ expect(error instanceof Error).toBeTruthy();
199
+ expect(error.url).toBe(
200
+ "https://www.arcgis.com/sharing/rest/content/items/43a8e51789044d9480a20089a84129ad/data"
201
+ );
202
+ expect(error.options.params).toEqual({ f: "json" });
203
+ expect(error.options.httpMethod).toEqual("POST");
204
+ expect(typeof error.options.fetch).toEqual("function");
205
+ expect(error.options.fetch.length).toEqual(2);
206
+ done();
207
+ });
208
+ });
209
+
210
+ it("should allow you to use custom implementations of `fetch`", done => {
211
+ const MockFetchResponse = {
212
+ ok: true,
213
+ json() {
214
+ return Promise.resolve(SharingRestInfo);
215
+ },
216
+ blob() {
217
+ return Promise.resolve(new Blob([JSON.stringify(SharingRestInfo)]));
218
+ },
219
+ text() {
220
+ return Promise.resolve(JSON.stringify(SharingRestInfo));
221
+ }
222
+ };
223
+
224
+ const MockFetch = function() {
225
+ return Promise.resolve(MockFetchResponse);
226
+ };
227
+
228
+ request("https://www.arcgis.com/sharing/rest/info", {
229
+ fetch: MockFetch as any
230
+ })
231
+ .then(response => {
232
+ expect(response).toEqual(SharingRestInfo);
233
+ done();
234
+ })
235
+ .catch(e => {
236
+ fail(e);
237
+ });
238
+ });
239
+
240
+ describe("should throw errors when required dependencies are missing", () => {
241
+ const oldPromise = Promise;
242
+ const oldFetch = fetch;
243
+ const oldFormData = FormData;
244
+ beforeEach(() => {
245
+ Promise = undefined;
246
+ FormData = undefined;
247
+ Function("return this")().fetch = undefined;
248
+ });
249
+
250
+ afterEach(() => {
251
+ Promise = oldPromise;
252
+ FormData = oldFormData;
253
+ Function("return this")().fetch = oldFetch;
254
+ });
255
+ it("should throw for missing dependencies", () => {
256
+ expect(() => {
257
+ request("https://www.arcgis.com/sharing/rest/info");
258
+ }).toThrowError(
259
+ "`arcgis-rest-request` requires global variables for `fetch`, `Promise` and `FormData` to be present in the global scope. You are missing `fetch`, `Promise`, `FormData`. We recommend installing the `isomorphic-fetch`, `es6-promise`, `isomorphic-form-data` modules at the root of your application to add these to the global scope. See https://bit.ly/2KNwWaJ for more info."
260
+ );
261
+ });
262
+
263
+ it("should not throw if fetch is not present but a custom fetch is defined", done => {
264
+ Promise = oldPromise;
265
+ FormData = oldFormData;
266
+
267
+ const MockFetchResponse = {
268
+ ok: true,
269
+ json() {
270
+ return Promise.resolve(SharingRestInfo);
271
+ },
272
+ blob() {
273
+ return Promise.resolve(new Blob([JSON.stringify(SharingRestInfo)]));
274
+ },
275
+ text() {
276
+ return Promise.resolve(JSON.stringify(SharingRestInfo));
277
+ }
278
+ };
279
+
280
+ const MockFetch = function() {
281
+ return Promise.resolve(MockFetchResponse);
282
+ };
283
+
284
+ request("https://www.arcgis.com/sharing/rest/info", {
285
+ fetch: MockFetch as any
286
+ })
287
+ .then(response => {
288
+ expect(response).toEqual(SharingRestInfo);
289
+ done();
290
+ })
291
+ .catch(e => {
292
+ fail(e);
293
+ });
294
+ });
295
+ });
296
+ });
@@ -0,0 +1,167 @@
1
+ import { ArcGISAuthError, IRetryAuthError, ErrorTypes } from "../../src/index";
2
+ import { ArcGISOnlineAuthError, ArcGISOnlineError } from "./../mocks/errors";
3
+ import * as fetchMock from "fetch-mock";
4
+
5
+ describe("ArcGISRequestError", () => {
6
+ afterEach(fetchMock.restore);
7
+
8
+ it("should be an instanceof Error", () => {
9
+ expect(new ArcGISAuthError() instanceof Error).toBe(true);
10
+ });
11
+
12
+ it("should expose basic error properties", () => {
13
+ const error = new ArcGISAuthError(
14
+ ArcGISOnlineAuthError.error.message,
15
+ ArcGISOnlineAuthError.error.code,
16
+ ArcGISOnlineAuthError
17
+ );
18
+
19
+ expect(error.name).toBe("ArcGISAuthError");
20
+ expect(error.message).toBe("498: Invalid token.");
21
+ expect(error.code).toBe(498);
22
+ expect(error.originalMessage).toBe("Invalid token.");
23
+ expect(error.response).toEqual(ArcGISOnlineAuthError);
24
+ });
25
+
26
+ it("should still format without a message, code or response", () => {
27
+ const error = new ArcGISAuthError();
28
+ expect(error.message).toBe("AUTHENTICATION_ERROR");
29
+ expect(error.code).toEqual("AUTHENTICATION_ERROR_CODE");
30
+ expect(error.originalMessage).toBe("AUTHENTICATION_ERROR");
31
+ expect(error.response).toEqual(undefined);
32
+ });
33
+
34
+ describe("retry", () => {
35
+ const MockAuth: {
36
+ portal: string;
37
+ getToken: any;
38
+ retryHandler: IRetryAuthError;
39
+ } = {
40
+ portal: "https://www.arcgis.com/sharing/rest",
41
+ getToken() {
42
+ return Promise.resolve("token");
43
+ },
44
+ retryHandler(url, options) {
45
+ return Promise.resolve(MockAuth);
46
+ }
47
+ };
48
+
49
+ it("should allow retrying a request with a new or updated session", done => {
50
+ const error = new ArcGISAuthError(
51
+ "Invalid token.",
52
+ 498,
53
+ ArcGISAuthError,
54
+ "http://www.arcgis.com/sharing/rest/content/users/caseyjones/addItem",
55
+ {
56
+ httpMethod: "POST",
57
+ params: {
58
+ title: "Test Map",
59
+ tags: "foo",
60
+ type: "Web Map",
61
+ f: "json"
62
+ }
63
+ }
64
+ );
65
+
66
+ fetchMock.once("*", {
67
+ success: true,
68
+ id: "abc",
69
+ folder: null
70
+ });
71
+
72
+ const retryHandlerSpy = spyOn(MockAuth, "retryHandler").and.callThrough();
73
+
74
+ error
75
+ .retry(MockAuth.retryHandler, 1)
76
+ .then((response: any) => {
77
+ const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
78
+ expect(url).toEqual(
79
+ "http://www.arcgis.com/sharing/rest/content/users/caseyjones/addItem"
80
+ );
81
+ expect(options.method).toEqual("POST");
82
+ expect(retryHandlerSpy).toHaveBeenCalledTimes(1);
83
+ expect(options.body).toContain("token=token");
84
+ expect(options.body).toContain("tags=foo");
85
+ expect(options.body).toContain("f=json");
86
+ expect(response.success).toBe(true);
87
+ expect(response.id).toBe("abc");
88
+ expect(response.folder).toBe(null);
89
+ done();
90
+ })
91
+ .catch((e: any) => {
92
+ fail(e);
93
+ });
94
+ });
95
+
96
+ it("should retry a request with a new or updated session up to the limit", done => {
97
+ const error = new ArcGISAuthError(
98
+ "Invalid token.",
99
+ 498,
100
+ ArcGISAuthError,
101
+ "http://www.arcgis.com/sharing/rest/content/users/caseyjones/addItem",
102
+ {
103
+ httpMethod: "POST",
104
+ params: {
105
+ title: "Test Map",
106
+ tags: "foo",
107
+ type: "Web Map",
108
+ f: "json"
109
+ }
110
+ }
111
+ );
112
+
113
+ fetchMock.post("*", ArcGISOnlineAuthError);
114
+
115
+ const retryHandlerSpy = spyOn(MockAuth, "retryHandler").and.callThrough();
116
+
117
+ error.retry(MockAuth.retryHandler).catch((e: any) => {
118
+ const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
119
+ expect(url).toEqual(
120
+ "http://www.arcgis.com/sharing/rest/content/users/caseyjones/addItem"
121
+ );
122
+ expect(options.method).toEqual("POST");
123
+ expect(retryHandlerSpy).toHaveBeenCalledTimes(3);
124
+ expect(options.body).toContain("token=token");
125
+ expect(options.body).toContain("tags=foo");
126
+ expect(options.body).toContain("f=json");
127
+ expect(e.name).toBe(ErrorTypes.ArcGISAuthError);
128
+ expect(e.message).toBe("498: Invalid token.");
129
+ done();
130
+ });
131
+ });
132
+
133
+ it("should throw an error if retrying throws a non-auth error", done => {
134
+ const requestUrl =
135
+ "http://www.arcgis.com/sharing/rest/content/users/caseyjones/addItem";
136
+ const error = new ArcGISAuthError(
137
+ "Invalid token.",
138
+ 498,
139
+ ArcGISAuthError,
140
+ requestUrl,
141
+ {
142
+ httpMethod: "POST",
143
+ params: {
144
+ type: "Web Map",
145
+ f: "json"
146
+ }
147
+ }
148
+ );
149
+
150
+ fetchMock.post("*", ArcGISOnlineError);
151
+
152
+ const retryHandlerSpy = spyOn(MockAuth, "retryHandler").and.callThrough();
153
+
154
+ error.retry(MockAuth.retryHandler).catch((e: any) => {
155
+ const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
156
+ expect(url).toEqual(requestUrl);
157
+ expect(options.method).toEqual("POST");
158
+ expect(retryHandlerSpy).toHaveBeenCalledTimes(1);
159
+ expect(options.body).toContain("token=token");
160
+ expect(options.body).toContain("f=json");
161
+ expect(e.name).toBe(ErrorTypes.ArcGISRequestError);
162
+ expect(e.message).toBe("400: 'type' and 'title' property required.");
163
+ done();
164
+ });
165
+ });
166
+ });
167
+ });
@@ -0,0 +1,40 @@
1
+ import { ArcGISRequestError } from "../../src/index";
2
+ import { ArcGISOnlineError } from "./../mocks/errors";
3
+
4
+ describe("ArcGISRequestError", () => {
5
+ it("should be an instanceof Error", () => {
6
+ expect(new ArcGISRequestError() instanceof Error).toBe(true);
7
+ });
8
+
9
+ it("should expose basic error properties", () => {
10
+ const error = new ArcGISRequestError(
11
+ ArcGISOnlineError.error.message,
12
+ ArcGISOnlineError.error.code,
13
+ ArcGISOnlineError,
14
+ "https://example.com",
15
+ {
16
+ params: {
17
+ f: "json"
18
+ },
19
+ httpMethod: "POST"
20
+ }
21
+ );
22
+
23
+ expect(error.name).toBe("ArcGISRequestError");
24
+ expect(error.message).toBe("400: 'type' and 'title' property required.");
25
+ expect(error.code).toBe(400);
26
+ expect(error.originalMessage).toBe("'type' and 'title' property required.");
27
+ expect(error.response).toEqual(ArcGISOnlineError);
28
+ expect(error.url).toBe("https://example.com");
29
+ expect(error.options.params).toEqual({ f: "json" });
30
+ expect(error.options.httpMethod).toEqual("POST");
31
+ });
32
+
33
+ it("should still format without a message, code or response", () => {
34
+ const error = new ArcGISRequestError();
35
+ expect(error.message).toBe("UNKNOWN_ERROR");
36
+ expect(error.code).toEqual("UNKNOWN_ERROR_CODE");
37
+ expect(error.originalMessage).toBe("UNKNOWN_ERROR");
38
+ expect(error.response).toEqual(undefined);
39
+ });
40
+ });
@@ -0,0 +1,101 @@
1
+ import {
2
+ checkForErrors,
3
+ warn,
4
+ ArcGISRequestError,
5
+ ArcGISAuthError
6
+ } from "../../src/index";
7
+ import { SharingRestInfo } from "./../mocks/sharing-rest-info";
8
+ import {
9
+ ArcGISOnlineError,
10
+ BillingError,
11
+ TaskErrorWithJSON,
12
+ TaskError,
13
+ ArcGISOnlineErrorNoMessageCode,
14
+ ArcGISOnlineErrorNoCode,
15
+ ArcGISServerTokenRequired,
16
+ ArcGISOnlineAuthError
17
+ } from "./../mocks/errors";
18
+
19
+ describe("checkForErrors", () => {
20
+ it("should pass if an error is not found", () => {
21
+ expect(checkForErrors(SharingRestInfo)).toBe(SharingRestInfo);
22
+ });
23
+
24
+ it("should throw an ArcGISRequestError for an error from the ArcGIS REST API", () => {
25
+ expect(() => {
26
+ checkForErrors(ArcGISOnlineError);
27
+ }).toThrowError(
28
+ ArcGISRequestError,
29
+ "400: 'type' and 'title' property required."
30
+ );
31
+ });
32
+
33
+ it("should throw an ArcGISOnlineAuthError for an error from the ArcGIS REST API", () => {
34
+ expect(() => {
35
+ checkForErrors(ArcGISOnlineAuthError);
36
+ }).toThrowError(ArcGISAuthError, "498: Invalid token.");
37
+ });
38
+
39
+ it("should throw an ArcGISRequestError for an error from the ArcGIS REST API that has no messageCode", () => {
40
+ expect(() => {
41
+ checkForErrors(ArcGISOnlineErrorNoMessageCode);
42
+ }).toThrowError(
43
+ ArcGISRequestError,
44
+ "403: You do not have permissions to access this resource or perform this operation."
45
+ );
46
+ });
47
+
48
+ it("should throw an ArcGISRequestError for an error from the ArcGIS REST API that has no code", () => {
49
+ expect(() => {
50
+ checkForErrors(ArcGISOnlineErrorNoCode);
51
+ }).toThrowError(
52
+ ArcGISRequestError,
53
+ "You do not have permissions to access this resource or perform this operation."
54
+ );
55
+ });
56
+
57
+ it("should throw an ArcGISRequestError for an error from the ArcGIS Online Billing Backend", () => {
58
+ expect(() => {
59
+ checkForErrors(BillingError);
60
+ }).toThrowError(ArcGISRequestError, "500: Error getting subscription info");
61
+ });
62
+
63
+ it("should throw an ArcGISRequestError for an error when checking long running tasks in ArcGIS REST API", () => {
64
+ expect(() => {
65
+ checkForErrors(TaskErrorWithJSON);
66
+ }).toThrowError(
67
+ ArcGISRequestError,
68
+ "400: Index was outside the bounds of the array."
69
+ );
70
+ });
71
+
72
+ it("should throw an ArcGISRequestError for an error when checking long running tasks in ArcGIS REST API without a JSON statusMessage", () => {
73
+ expect(() => {
74
+ checkForErrors(TaskError);
75
+ }).toThrowError(ArcGISRequestError, "failed");
76
+ });
77
+
78
+ it("should throw an ArcGISAuthError instead of ArcGISRequestError for messageCode=GWM_0003", () => {
79
+ expect(() => {
80
+ checkForErrors(ArcGISServerTokenRequired);
81
+ }).toThrowError(ArcGISAuthError, "GWM_0003: Token Required");
82
+ });
83
+ });
84
+
85
+ describe("warn", () => {
86
+ it("should bubble up deprecation warnings", () => {
87
+ console.warn = jasmine.createSpy("warning");
88
+ warn("Danger Will Robinson!");
89
+ expect(console.warn).toHaveBeenCalledWith("Danger Will Robinson!");
90
+ });
91
+ });
92
+
93
+ describe("warn", () => {
94
+ it("should carry on gracefully when no console is available", () => {
95
+ const realConsoleWarn = console.warn;
96
+ console.warn = undefined;
97
+ warn("Danger Will Robinson!");
98
+ expect(console.warn).toBe(undefined);
99
+ console.warn = realConsoleWarn;
100
+ });
101
+ });