@warp-drive/legacy 5.8.0-alpha.9 → 5.8.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (200) hide show
  1. package/README.md +14 -27
  2. package/declarations/adapter/error.d.ts +7 -7
  3. package/declarations/adapter/json-api.d.ts +2 -2
  4. package/declarations/adapter/rest.d.ts +21 -104
  5. package/declarations/adapter.d.ts +2 -2
  6. package/declarations/compat/builders/find-all.d.ts +6 -6
  7. package/declarations/compat/builders/find-record.d.ts +8 -8
  8. package/declarations/compat/builders/query.d.ts +12 -12
  9. package/declarations/compat/legacy-network-handler/minimum-serializer-interface.d.ts +16 -24
  10. package/declarations/compat/utils.d.ts +13 -13
  11. package/declarations/compat.d.ts +31 -5
  12. package/declarations/index.d.ts +25 -8
  13. package/declarations/model/-private/legacy-relationships-support.d.ts +2 -2
  14. package/declarations/model/-private/model.d.ts +10 -51
  15. package/declarations/model/-private/promise-many-array.d.ts +0 -18
  16. package/declarations/model/-private/references/belongs-to.d.ts +18 -28
  17. package/declarations/model/-private/references/has-many.d.ts +13 -15
  18. package/declarations/model/migration-support.d.ts +42 -19
  19. package/declarations/model-fragments/extensions/fragment-array.d.ts +16 -0
  20. package/declarations/model-fragments/extensions/fragment.d.ts +15 -0
  21. package/declarations/model-fragments/hooks/model-for.d.ts +20 -0
  22. package/declarations/model-fragments/index.d.ts +5 -0
  23. package/declarations/model-fragments/instance-initializers/fragment-extensions.d.ts +9 -0
  24. package/declarations/model-fragments/utilities/with-array-defaults.d.ts +15 -0
  25. package/declarations/model-fragments/utilities/with-fragment-array-defaults.d.ts +20 -0
  26. package/declarations/model-fragments/utilities/with-fragment-defaults.d.ts +19 -0
  27. package/declarations/model-fragments/utilities/with-legacy.d.ts +3 -0
  28. package/declarations/model-fragments.d.ts +9 -0
  29. package/declarations/model.d.ts +2 -2
  30. package/declarations/serializer/-private/embedded-records-mixin.d.ts +1 -1
  31. package/declarations/serializer/json-api.d.ts +7 -6
  32. package/declarations/serializer/json.d.ts +3 -3
  33. package/declarations/serializer.d.ts +5 -6
  34. package/dist/{-private-B1pSSN52.js → -private-BG3bMiKp.js} +2 -1
  35. package/dist/adapter/-private.js +1 -1
  36. package/dist/adapter/error.js +9 -9
  37. package/dist/adapter/json-api.js +1 -1
  38. package/dist/adapter/rest.js +33 -117
  39. package/dist/adapter.js +2 -2
  40. package/dist/compat/-private.js +1 -1
  41. package/dist/compat/builders.js +26 -26
  42. package/dist/compat/utils.js +13 -14
  43. package/dist/compat.js +28 -8
  44. package/dist/{errors-COviC59J.js → errors-Cz5KrzBk.js} +113 -120
  45. package/dist/{hooks-Bp8SIQBU.js → hooks-D6diaM34.js} +1 -1
  46. package/dist/index.js +25 -8
  47. package/dist/{json-ksWOHRfq.js → json-ChdEfB0X.js} +12 -27
  48. package/dist/model/-private.js +1 -1
  49. package/dist/model/migration-support.js +55 -25
  50. package/dist/model-for-CqXsIKws.js +221 -0
  51. package/dist/model-fragments.js +76 -0
  52. package/dist/model.js +3 -3
  53. package/dist/{schema-provider-JlCneqZH.js → schema-provider-DJCV_6AF.js} +44 -87
  54. package/dist/{serialize-into-hash-BnYvPex3.js → serialize-into-hash-DPZYoF-i.js} +1 -1
  55. package/dist/serializer/json-api.js +18 -45
  56. package/dist/serializer/json.js +1 -1
  57. package/dist/serializer/rest.js +10 -10
  58. package/dist/serializer.js +5 -6
  59. package/dist/store.js +2 -1
  60. package/dist/unpkg/dev/-private-DtjBbEgy.js +1206 -0
  61. package/dist/unpkg/dev/adapter/-private.js +1 -0
  62. package/dist/unpkg/dev/adapter/error.js +335 -0
  63. package/dist/unpkg/dev/adapter/json-api.js +271 -0
  64. package/dist/unpkg/dev/adapter/rest.js +1171 -0
  65. package/dist/unpkg/dev/adapter.js +1252 -0
  66. package/dist/unpkg/dev/compat/-private.js +1 -0
  67. package/dist/unpkg/dev/compat/builders.js +275 -0
  68. package/dist/unpkg/dev/compat/extensions.js +242 -0
  69. package/dist/unpkg/dev/compat/utils.js +223 -0
  70. package/dist/unpkg/dev/compat.js +1147 -0
  71. package/dist/unpkg/dev/errors-DmGGJr3T.js +2562 -0
  72. package/dist/unpkg/dev/hooks-CkYiE6Ud.js +73 -0
  73. package/dist/unpkg/dev/index.js +197 -0
  74. package/dist/unpkg/dev/json-Cu1LNgmQ.js +1256 -0
  75. package/dist/unpkg/dev/model/-private.js +1 -0
  76. package/dist/unpkg/dev/model/migration-support.js +553 -0
  77. package/dist/unpkg/dev/model-for-CqXsIKws.js +221 -0
  78. package/dist/unpkg/dev/model-fragments.js +76 -0
  79. package/dist/unpkg/dev/model.js +678 -0
  80. package/dist/unpkg/dev/runtime-BPCpkOf1-BKOwiRJp.js +65 -0
  81. package/dist/unpkg/dev/schema-provider-DDVYxmUV.js +2186 -0
  82. package/dist/unpkg/dev/serialize-into-hash-B2xDbuo5.js +259 -0
  83. package/dist/unpkg/dev/serializer/json-api.js +649 -0
  84. package/dist/unpkg/dev/serializer/json.js +4 -0
  85. package/dist/unpkg/dev/serializer/rest.js +1242 -0
  86. package/dist/unpkg/dev/serializer/transform.js +278 -0
  87. package/dist/unpkg/dev/serializer.js +248 -0
  88. package/dist/unpkg/dev/store.js +637 -0
  89. package/dist/unpkg/dev/util-DvanW33H.js +20 -0
  90. package/dist/unpkg/dev/utils-BhvS1iTS.js +8 -0
  91. package/dist/unpkg/dev-deprecated/-private-DtjBbEgy.js +1206 -0
  92. package/dist/unpkg/dev-deprecated/adapter/-private.js +1 -0
  93. package/dist/unpkg/dev-deprecated/adapter/error.js +335 -0
  94. package/dist/unpkg/dev-deprecated/adapter/json-api.js +271 -0
  95. package/dist/unpkg/dev-deprecated/adapter/rest.js +1171 -0
  96. package/dist/unpkg/dev-deprecated/adapter.js +1252 -0
  97. package/dist/unpkg/dev-deprecated/compat/-private.js +1 -0
  98. package/dist/unpkg/dev-deprecated/compat/builders.js +275 -0
  99. package/dist/unpkg/dev-deprecated/compat/extensions.js +242 -0
  100. package/dist/unpkg/dev-deprecated/compat/utils.js +223 -0
  101. package/dist/unpkg/dev-deprecated/compat.js +1147 -0
  102. package/dist/unpkg/dev-deprecated/errors-Spt6ubMd.js +2565 -0
  103. package/dist/unpkg/dev-deprecated/hooks-DOXegvhL.js +73 -0
  104. package/dist/unpkg/dev-deprecated/index.js +196 -0
  105. package/dist/unpkg/dev-deprecated/json-Cu1LNgmQ.js +1256 -0
  106. package/dist/unpkg/dev-deprecated/model/-private.js +1 -0
  107. package/dist/unpkg/dev-deprecated/model/migration-support.js +570 -0
  108. package/dist/unpkg/dev-deprecated/model-for-CqXsIKws.js +221 -0
  109. package/dist/unpkg/dev-deprecated/model-fragments.js +76 -0
  110. package/dist/unpkg/dev-deprecated/model.js +682 -0
  111. package/dist/unpkg/dev-deprecated/runtime-BPCpkOf1-BKOwiRJp.js +65 -0
  112. package/dist/unpkg/dev-deprecated/schema-provider-BP6_8N-V.js +2211 -0
  113. package/dist/unpkg/dev-deprecated/serialize-into-hash-B2xDbuo5.js +259 -0
  114. package/dist/unpkg/dev-deprecated/serializer/json-api.js +649 -0
  115. package/dist/unpkg/dev-deprecated/serializer/json.js +4 -0
  116. package/dist/unpkg/dev-deprecated/serializer/rest.js +1242 -0
  117. package/dist/unpkg/dev-deprecated/serializer/transform.js +278 -0
  118. package/dist/unpkg/dev-deprecated/serializer.js +248 -0
  119. package/dist/unpkg/dev-deprecated/store.js +637 -0
  120. package/dist/unpkg/dev-deprecated/util-CWr5WQOT.js +24 -0
  121. package/dist/unpkg/dev-deprecated/utils-C9PJehtL.js +12 -0
  122. package/dist/unpkg/prod/-private-BdyZaGEh.js +971 -0
  123. package/dist/unpkg/prod/adapter/-private.js +1 -0
  124. package/dist/unpkg/prod/adapter/error.js +330 -0
  125. package/dist/unpkg/prod/adapter/json-api.js +266 -0
  126. package/dist/unpkg/prod/adapter/rest.js +1134 -0
  127. package/dist/unpkg/prod/adapter.js +1219 -0
  128. package/dist/unpkg/prod/compat/-private.js +1 -0
  129. package/dist/unpkg/prod/compat/builders.js +210 -0
  130. package/dist/unpkg/prod/compat/extensions.js +232 -0
  131. package/dist/unpkg/prod/compat/utils.js +218 -0
  132. package/dist/unpkg/prod/compat.js +727 -0
  133. package/dist/unpkg/prod/errors-BGVFCBmi.js +2314 -0
  134. package/dist/unpkg/prod/hooks-BztVA_x0.js +41 -0
  135. package/dist/unpkg/prod/index.js +151 -0
  136. package/dist/unpkg/prod/json-BWrZ5546.js +1243 -0
  137. package/dist/unpkg/prod/model/-private.js +1 -0
  138. package/dist/unpkg/prod/model/migration-support.js +546 -0
  139. package/dist/unpkg/prod/model-for-CqXsIKws.js +221 -0
  140. package/dist/unpkg/prod/model-fragments.js +76 -0
  141. package/dist/unpkg/prod/model.js +593 -0
  142. package/dist/unpkg/prod/runtime-BPCpkOf1-BKOwiRJp.js +65 -0
  143. package/dist/unpkg/prod/schema-provider-DJtD_8jZ.js +1861 -0
  144. package/dist/unpkg/prod/serialize-into-hash-DGlzQteF.js +215 -0
  145. package/dist/unpkg/prod/serializer/json-api.js +592 -0
  146. package/dist/unpkg/prod/serializer/json.js +4 -0
  147. package/dist/unpkg/prod/serializer/rest.js +1210 -0
  148. package/dist/unpkg/prod/serializer/transform.js +278 -0
  149. package/dist/unpkg/prod/serializer.js +248 -0
  150. package/dist/unpkg/prod/store.js +505 -0
  151. package/dist/unpkg/prod/util-DvanW33H.js +20 -0
  152. package/dist/unpkg/prod/utils-BhvS1iTS.js +8 -0
  153. package/dist/unpkg/prod-deprecated/-private-BdyZaGEh.js +971 -0
  154. package/dist/unpkg/prod-deprecated/adapter/-private.js +1 -0
  155. package/dist/unpkg/prod-deprecated/adapter/error.js +330 -0
  156. package/dist/unpkg/prod-deprecated/adapter/json-api.js +266 -0
  157. package/dist/unpkg/prod-deprecated/adapter/rest.js +1134 -0
  158. package/dist/unpkg/prod-deprecated/adapter.js +1219 -0
  159. package/dist/unpkg/prod-deprecated/compat/-private.js +1 -0
  160. package/dist/unpkg/prod-deprecated/compat/builders.js +210 -0
  161. package/dist/unpkg/prod-deprecated/compat/extensions.js +232 -0
  162. package/dist/unpkg/prod-deprecated/compat/utils.js +218 -0
  163. package/dist/unpkg/prod-deprecated/compat.js +727 -0
  164. package/dist/unpkg/prod-deprecated/errors-CdDaK81x.js +2317 -0
  165. package/dist/unpkg/prod-deprecated/hooks-yId87yyG.js +41 -0
  166. package/dist/unpkg/prod-deprecated/index.js +150 -0
  167. package/dist/unpkg/prod-deprecated/json-BWrZ5546.js +1243 -0
  168. package/dist/unpkg/prod-deprecated/model/-private.js +1 -0
  169. package/dist/unpkg/prod-deprecated/model/migration-support.js +563 -0
  170. package/dist/unpkg/prod-deprecated/model-for-CqXsIKws.js +221 -0
  171. package/dist/unpkg/prod-deprecated/model-fragments.js +76 -0
  172. package/dist/unpkg/prod-deprecated/model.js +596 -0
  173. package/dist/unpkg/prod-deprecated/runtime-BPCpkOf1-BKOwiRJp.js +65 -0
  174. package/dist/unpkg/prod-deprecated/schema-provider-CjX55uSY.js +1904 -0
  175. package/dist/unpkg/prod-deprecated/serialize-into-hash-DGlzQteF.js +215 -0
  176. package/dist/unpkg/prod-deprecated/serializer/json-api.js +592 -0
  177. package/dist/unpkg/prod-deprecated/serializer/json.js +4 -0
  178. package/dist/unpkg/prod-deprecated/serializer/rest.js +1210 -0
  179. package/dist/unpkg/prod-deprecated/serializer/transform.js +278 -0
  180. package/dist/unpkg/prod-deprecated/serializer.js +248 -0
  181. package/dist/unpkg/prod-deprecated/store.js +505 -0
  182. package/dist/unpkg/prod-deprecated/util-B6cn-i93.js +23 -0
  183. package/dist/unpkg/prod-deprecated/utils-BUWwQwCh.js +11 -0
  184. package/logos/README.md +2 -2
  185. package/logos/logo-yellow-slab.svg +1 -0
  186. package/logos/word-mark-black.svg +1 -0
  187. package/logos/word-mark-white.svg +1 -0
  188. package/package.json +14 -6
  189. package/logos/NCC-1701-a-blue.svg +0 -4
  190. package/logos/NCC-1701-a-gold.svg +0 -4
  191. package/logos/NCC-1701-a-gold_100.svg +0 -1
  192. package/logos/NCC-1701-a-gold_base-64.txt +0 -1
  193. package/logos/NCC-1701-a.svg +0 -4
  194. package/logos/docs-badge.svg +0 -2
  195. package/logos/ember-data-logo-dark.svg +0 -12
  196. package/logos/ember-data-logo-light.svg +0 -12
  197. package/logos/social1.png +0 -0
  198. package/logos/social2.png +0 -0
  199. package/logos/warp-drive-logo-dark.svg +0 -4
  200. package/logos/warp-drive-logo-gold.svg +0 -4
@@ -0,0 +1,1171 @@
1
+ import { getOwner } from '@ember/application';
2
+ import { computed } from '@ember/object';
3
+ import { Adapter, BuildURLMixin } from '../adapter.js';
4
+ import { b as serializeIntoHash, d as determineBodyPromise, g as getFetchFunction, s as serializeQueryParams, p as parseResponseHeaders } from "../serialize-into-hash-B2xDbuo5.js";
5
+ import { InvalidError, ServerError, ConflictError, NotFoundError, ForbiddenError, UnauthorizedError, AdapterError, TimeoutError, AbortError } from './error.js';
6
+ import { d as decorateMethodV2 } from "../runtime-BPCpkOf1-BKOwiRJp.js";
7
+
8
+ /* eslint-disable @typescript-eslint/no-unsafe-member-access */
9
+ /* eslint-disable @typescript-eslint/no-unsafe-assignment */
10
+ /* eslint-disable @typescript-eslint/no-unsafe-return */
11
+
12
+ const AdapterWithBuildURLMixin = Adapter.extend(BuildURLMixin);
13
+
14
+ /**
15
+ * :::danger
16
+ ⚠️ **This is LEGACY documentation** for a feature that is no longer encouraged to be used.
17
+ If starting a new app or thinking of implementing a new adapter, consider writing a
18
+ {@link Handler} instead to be used with the {@link RequestManager}
19
+ :::
20
+
21
+ The REST adapter allows your store to communicate with an HTTP server by
22
+ transmitting JSON via XHR.
23
+
24
+ This adapter is designed around the idea that the JSON exchanged with
25
+ the server should be conventional. It builds URLs in a manner that follows
26
+ the structure of most common REST-style web services.
27
+
28
+ ## Success and failure
29
+
30
+ The REST adapter will consider a success any response with a status code
31
+ of the 2xx family ("Success"), as well as 304 ("Not Modified"). Any other
32
+ status code will be considered a failure.
33
+
34
+ On success, the request promise will be resolved with the full response
35
+ payload.
36
+
37
+ Failed responses with status code 422 ("Unprocessable Entity") will be
38
+ considered "invalid". The response will be discarded, except for the
39
+ `errors` key. The request promise will be rejected with a `InvalidError`.
40
+ This error object will encapsulate the saved `errors` value.
41
+
42
+ Any other status codes will be treated as an "adapter error". The request
43
+ promise will be rejected, similarly to the "invalid" case, but with
44
+ an instance of `AdapterError` instead.
45
+
46
+ ## JSON Structure
47
+
48
+ The REST adapter expects the JSON returned from your server to follow
49
+ these conventions.
50
+
51
+ ### Object Root
52
+
53
+ The JSON payload should be an object that contains the record inside a
54
+ root property. For example, in response to a `GET` request for
55
+ `/posts/1`, the JSON should look like this:
56
+
57
+ ```js
58
+ {
59
+ "posts": {
60
+ "id": 1,
61
+ "title": "I'm Running to Reform the W3C",
62
+ "author": "Yehuda Katz"
63
+ }
64
+ }
65
+ ```
66
+
67
+ Similarly, in response to a `GET` request for `/posts`, the JSON should
68
+ look like this:
69
+
70
+ ```js
71
+ {
72
+ "posts": [
73
+ {
74
+ "id": 1,
75
+ "title": "I'm Running to Reform the W3C",
76
+ "author": "Yehuda Katz"
77
+ },
78
+ {
79
+ "id": 2,
80
+ "title": "Rails is omakase",
81
+ "author": "D2H"
82
+ }
83
+ ]
84
+ }
85
+ ```
86
+
87
+ Note that the object root can be pluralized for both a single-object response
88
+ and an array response: the REST adapter is not strict on this. Further, if the
89
+ HTTP server responds to a `GET` request to `/posts/1` (e.g. the response to a
90
+ `findRecord` query) with more than one object in the array, WarpDrive will
91
+ only display the object with the matching ID.
92
+
93
+ ### Conventional Names
94
+
95
+ Attribute names in your JSON payload should be the camelCased versions of
96
+ the attributes in your Ember.js models.
97
+
98
+ For example, if you have a `Person` model:
99
+
100
+ ```js [app/models/person.js]
101
+ import { Model, attr } from '@warp-drive/legacy/model';
102
+
103
+ export default Model.extend({
104
+ firstName: attr('string'),
105
+ lastName: attr('string'),
106
+ occupation: attr('string')
107
+ });
108
+ ```
109
+
110
+ The JSON returned should look like this:
111
+
112
+ ```js
113
+ {
114
+ "people": {
115
+ "id": 5,
116
+ "firstName": "Zaphod",
117
+ "lastName": "Beeblebrox",
118
+ "occupation": "President"
119
+ }
120
+ }
121
+ ```
122
+
123
+ #### Relationships
124
+
125
+ Relationships are usually represented by ids to the record in the
126
+ relationship. The related records can then be sideloaded in the
127
+ response under a key for the type.
128
+
129
+ ```js
130
+ {
131
+ "posts": {
132
+ "id": 5,
133
+ "title": "I'm Running to Reform the W3C",
134
+ "author": "Yehuda Katz",
135
+ "comments": [1, 2]
136
+ },
137
+ "comments": [{
138
+ "id": 1,
139
+ "author": "User 1",
140
+ "message": "First!",
141
+ }, {
142
+ "id": 2,
143
+ "author": "User 2",
144
+ "message": "Good Luck!",
145
+ }]
146
+ }
147
+ ```
148
+
149
+ If the records in the relationship are not known when the response
150
+ is serialized it's also possible to represent the relationship as a
151
+ URL using the `links` key in the response. WarpDrive will fetch
152
+ this URL to resolve the relationship when it is accessed for the
153
+ first time.
154
+
155
+ ```js
156
+ {
157
+ "posts": {
158
+ "id": 5,
159
+ "title": "I'm Running to Reform the W3C",
160
+ "author": "Yehuda Katz",
161
+ "links": {
162
+ "comments": "/posts/5/comments"
163
+ }
164
+ }
165
+ }
166
+ ```
167
+
168
+ ### Errors
169
+
170
+ If a response is considered a failure, the JSON payload is expected to include
171
+ a top-level key `errors`, detailing any specific issues. For example:
172
+
173
+ ```js
174
+ {
175
+ "errors": {
176
+ "msg": "Something went wrong"
177
+ }
178
+ }
179
+ ```
180
+
181
+ This adapter does not make any assumptions as to the format of the `errors`
182
+ object. It will simply be passed along as is, wrapped in an instance
183
+ of `InvalidError` or `AdapterError`. The serializer can interpret it
184
+ afterwards.
185
+
186
+ ## Customization
187
+
188
+ ### Endpoint path customization
189
+
190
+ Endpoint paths can be prefixed with a `namespace` by setting the namespace
191
+ property on the adapter:
192
+
193
+ ```js [app/adapters/application.js]
194
+ import { RESTAdapter } from '@warp-drive/legacy/adapter/rest';
195
+
196
+ export default class ApplicationAdapter extends RESTAdapter {
197
+ namespace = 'api/1';
198
+ }
199
+ ```
200
+ Requests for the `Person` model would now target `/api/1/people/1`.
201
+
202
+ ### Host customization
203
+
204
+ An adapter can target other hosts by setting the `host` property.
205
+
206
+ ```js [app/adapters/application.js]
207
+ import { RESTAdapter } from '@warp-drive/legacy/adapter/rest';
208
+
209
+ export default class ApplicationAdapter extends RESTAdapter {
210
+ host = 'https://api.example.com';
211
+ }
212
+ ```
213
+
214
+ ### Headers customization
215
+
216
+ Some APIs require HTTP headers, e.g. to provide an API key. Arbitrary
217
+ headers can be set as key/value pairs on the `RESTAdapter`'s `headers`
218
+ object and WarpDrive will send them along with each ajax request.
219
+
220
+
221
+ ```js [app/adapters/application.js]
222
+ import { RESTAdapter } from '@warp-drive/legacy/adapter/rest';
223
+
224
+ export default class ApplicationAdapter extends RESTAdapter {
225
+ get headers() {
226
+ return {
227
+ 'API_KEY': 'secret key',
228
+ 'ANOTHER_HEADER': 'Some header value'
229
+ };
230
+ }
231
+ }
232
+ ```
233
+
234
+ @public
235
+ */
236
+ class RESTAdapter extends AdapterWithBuildURLMixin {
237
+ /**
238
+ This property allows ajax to still be used instead when `false`.
239
+ @default true
240
+ @public
241
+ */
242
+ useFetch = true;
243
+ _defaultContentType = 'application/json; charset=utf-8';
244
+ get fastboot() {
245
+ // Avoid computed property override deprecation in fastboot as suggested by:
246
+ // https://deprecations.emberjs.com/v3.x/#toc_computed-property-override
247
+ const fastboot = this._fastboot;
248
+ if (fastboot) {
249
+ return fastboot;
250
+ }
251
+ return this._fastboot = getOwner(this).lookup('service:fastboot');
252
+ }
253
+ static {
254
+ decorateMethodV2(this.prototype, "fastboot", [computed()]);
255
+ }
256
+ set fastboot(value) {
257
+ this._fastboot = value;
258
+ }
259
+
260
+ /**
261
+ By default, the RESTAdapter will send the query params sorted alphabetically to the
262
+ server.
263
+ For example:
264
+ ```js
265
+ store.query('posts', { sort: 'price', category: 'pets' });
266
+ ```
267
+ will generate a requests like this `/posts?category=pets&sort=price`, even if the
268
+ parameters were specified in a different order.
269
+ That way the generated URL will be deterministic and that simplifies caching mechanisms
270
+ in the backend.
271
+ Setting `sortQueryParams` to a falsey value will respect the original order.
272
+ In case you want to sort the query parameters with a different criteria, set
273
+ `sortQueryParams` to your custom sort function.
274
+ ```js [app/adapters/application.js]
275
+ import { RESTAdapter } from '@warp-drive/legacy/adapter/rest';
276
+ export default class ApplicationAdapter extends RESTAdapter {
277
+ sortQueryParams(params) {
278
+ let sortedKeys = Object.keys(params).sort().reverse();
279
+ let len = sortedKeys.length, newParams = {};
280
+ for (let i = 0; i < len; i++) {
281
+ newParams[sortedKeys[i]] = params[sortedKeys[i]];
282
+ }
283
+ return newParams;
284
+ }
285
+ }
286
+ ```
287
+ @public
288
+ */
289
+ sortQueryParams(obj) {
290
+ const keys = Object.keys(obj);
291
+ const len = keys.length;
292
+ if (len < 2) {
293
+ return obj;
294
+ }
295
+ const newQueryParams = {};
296
+ const sortedKeys = keys.sort();
297
+ for (let i = 0; i < len; i++) {
298
+ newQueryParams[sortedKeys[i]] = obj[sortedKeys[i]];
299
+ }
300
+ return newQueryParams;
301
+ }
302
+
303
+ /**
304
+ By default the RESTAdapter will send each find request coming from a `store.find`
305
+ or from accessing a relationship separately to the server. If your server supports passing
306
+ ids as a query string, you can set coalesceFindRequests to true to coalesce all find requests
307
+ within a single runloop.
308
+ For example, if you have an initial payload of:
309
+ ```javascript
310
+ {
311
+ post: {
312
+ id: 1,
313
+ comments: [1, 2]
314
+ }
315
+ }
316
+ ```
317
+ By default calling `post.comments` will trigger the following requests(assuming the
318
+ comments haven't been loaded before):
319
+ ```
320
+ GET /comments/1
321
+ GET /comments/2
322
+ ```
323
+ If you set coalesceFindRequests to `true` it will instead trigger the following request:
324
+ ```
325
+ GET /comments?ids[]=1&ids[]=2
326
+ ```
327
+ Setting coalesceFindRequests to `true` also works for `store.find` requests and `belongsTo`
328
+ relationships accessed within the same runloop. If you set `coalesceFindRequests: true`
329
+ ```javascript
330
+ store.findRecord('comment', 1);
331
+ store.findRecord('comment', 2);
332
+ ```
333
+ will also send a request to: `GET /comments?ids[]=1&ids[]=2`
334
+ Note: Requests coalescing rely on URL building strategy. So if you override `buildURL` in your app
335
+ `groupRecordsForFindMany` more likely should be overridden as well in order for coalescing to work.
336
+ @public
337
+ */
338
+ get coalesceFindRequests() {
339
+ const coalesceFindRequests = this._coalesceFindRequests;
340
+ if (typeof coalesceFindRequests === 'boolean') {
341
+ return coalesceFindRequests;
342
+ }
343
+ return this._coalesceFindRequests = false;
344
+ }
345
+ set coalesceFindRequests(value) {
346
+ this._coalesceFindRequests = value;
347
+ }
348
+
349
+ /**
350
+ Endpoint paths can be prefixed with a `namespace` by setting the namespace
351
+ property on the adapter:
352
+ ```js [app/adapters/application.js]
353
+ import { RESTAdapter } from '@warp-drive/legacy/adapter/rest';
354
+ export default class ApplicationAdapter extends RESTAdapter {
355
+ namespace = 'api/1';
356
+ }
357
+ ```
358
+ Requests for the `Post` model would now target `/api/1/post/`.
359
+ @public
360
+ */
361
+
362
+ /**
363
+ An adapter can target other hosts by setting the `host` property.
364
+ ```js [app/adapters/application.js]
365
+ import { RESTAdapter } from '@warp-drive/legacy/adapter/rest';
366
+ export default class ApplicationAdapter extends RESTAdapter {
367
+ host = 'https://api.example.com';
368
+ }
369
+ ```
370
+ Requests for the `Post` model would now target `https://api.example.com/post/`.
371
+ @public
372
+ */
373
+
374
+ /**
375
+ Some APIs require HTTP headers, e.g. to provide an API
376
+ key. Arbitrary headers can be set as key/value pairs on the
377
+ `RESTAdapter`'s `headers` object and WarpDrive will send them
378
+ along with each ajax request..
379
+ ```js [app/adapters/application.js]
380
+ import { RESTAdapter } from '@warp-drive/legacy/adapter/rest';
381
+ export default class ApplicationAdapter extends RESTAdapter {
382
+ get headers() {
383
+ return {
384
+ 'API_KEY': 'secret key',
385
+ 'ANOTHER_HEADER': 'Some header value'
386
+ };
387
+ }
388
+ }
389
+ ```
390
+ @public
391
+ */
392
+
393
+ /**
394
+ Called by the store in order to fetch the JSON for a given
395
+ type and ID.
396
+ The `findRecord` method makes an Ajax request to a URL computed by
397
+ `buildURL`, and returns a promise for the resulting payload.
398
+ This method performs an HTTP `GET` request with the id provided as part of the query string.
399
+ @since 1.13.0
400
+ @public
401
+ */
402
+ findRecord(store, type, id, snapshot) {
403
+ const url = this.buildURL(type.modelName, id, snapshot, 'findRecord');
404
+ const query = this.buildQuery(snapshot);
405
+ return this.ajax(url, 'GET', {
406
+ data: query
407
+ });
408
+ }
409
+
410
+ /**
411
+ Called by the store in order to fetch a JSON array for all
412
+ of the records for a given type.
413
+ The `findAll` method makes an Ajax (HTTP GET) request to a URL computed by `buildURL`, and returns a
414
+ promise for the resulting payload.
415
+ @public
416
+ */
417
+ findAll(store, type, neverUsed, snapshotRecordArray) {
418
+ const query = this.buildQuery(snapshotRecordArray);
419
+ const url = this.buildURL(type.modelName, null, snapshotRecordArray, 'findAll');
420
+ return this.ajax(url, 'GET', {
421
+ data: query
422
+ });
423
+ }
424
+
425
+ /**
426
+ Called by the store in order to fetch a JSON array for
427
+ the records that match a particular query.
428
+ The `query` method makes an Ajax (HTTP GET) request to a URL
429
+ computed by `buildURL`, and returns a promise for the resulting
430
+ payload.
431
+ The `query` argument is a simple JavaScript object that will be passed directly
432
+ to the server as parameters.
433
+ @public
434
+ */
435
+ query(store, type, query) {
436
+ const url = this.buildURL(type.modelName, null, null, 'query', query);
437
+ if (this.sortQueryParams) {
438
+ query = this.sortQueryParams(query);
439
+ }
440
+ return this.ajax(url, 'GET', {
441
+ data: query
442
+ });
443
+ }
444
+
445
+ /**
446
+ Called by the store in order to fetch a JSON object for
447
+ the record that matches a particular query.
448
+ The `queryRecord` method makes an Ajax (HTTP GET) request to a URL
449
+ computed by `buildURL`, and returns a promise for the resulting
450
+ payload.
451
+ The `query` argument is a simple JavaScript object that will be passed directly
452
+ to the server as parameters.
453
+ @since 1.13.0
454
+ @public
455
+ */
456
+ queryRecord(store, type, query, adapterOptions) {
457
+ const url = this.buildURL(type.modelName, null, null, 'queryRecord', query);
458
+ if (this.sortQueryParams) {
459
+ query = this.sortQueryParams(query);
460
+ }
461
+ return this.ajax(url, 'GET', {
462
+ data: query
463
+ });
464
+ }
465
+
466
+ /**
467
+ Called by the store in order to fetch several records together if `coalesceFindRequests` is true
468
+ For example, if the original payload looks like:
469
+ ```js
470
+ {
471
+ "id": 1,
472
+ "title": "Rails is omakase",
473
+ "comments": [ 1, 2, 3 ]
474
+ }
475
+ ```
476
+ The IDs will be passed as a URL-encoded Array of IDs, in this form:
477
+ ```
478
+ ids[]=1&ids[]=2&ids[]=3
479
+ ```
480
+ Many servers, such as Rails and PHP, will automatically convert this URL-encoded array
481
+ into an Array for you on the server-side. If you want to encode the
482
+ IDs, differently, just override this (one-line) method.
483
+ The `findMany` method makes an Ajax (HTTP GET) request to a URL computed by `buildURL`, and returns a
484
+ promise for the resulting payload.
485
+ @public
486
+ */
487
+ findMany(store, type, ids, snapshots) {
488
+ const url = this.buildURL(type.modelName, ids, snapshots, 'findMany');
489
+ return this.ajax(url, 'GET', {
490
+ data: {
491
+ ids: ids
492
+ }
493
+ });
494
+ }
495
+
496
+ /**
497
+ Called by the store in order to fetch a JSON array for
498
+ the unloaded records in a has-many relationship that were originally
499
+ specified as a URL (inside of `links`).
500
+ For example, if your original payload looks like this:
501
+ ```js
502
+ {
503
+ "post": {
504
+ "id": 1,
505
+ "title": "Rails is omakase",
506
+ "links": { "comments": "/posts/1/comments" }
507
+ }
508
+ }
509
+ ```
510
+ This method will be called with the parent record and `/posts/1/comments`.
511
+ The `findHasMany` method will make an Ajax (HTTP GET) request to the originally specified URL.
512
+ The format of your `links` value will influence the final request URL via the `urlPrefix` method:
513
+ * Links beginning with `//`, `http://`, `https://`, will be used as is, with no further manipulation.
514
+ * Links beginning with a single `/` will have the current adapter's `host` value prepended to it.
515
+ * Links with no beginning `/` will have a parentURL prepended to it, via the current adapter's `buildURL`.
516
+ @public
517
+ */
518
+ findHasMany(store, snapshot, url, relationship) {
519
+ const id = snapshot.id;
520
+ const type = snapshot.modelName;
521
+ (test => {
522
+ if (!test) {
523
+ throw new Error(`Attempted to fetch the hasMany relationship for ${type}, but the record has no id`);
524
+ }
525
+ })(typeof id === 'string' && id.length > 0);
526
+ url = this.urlPrefix(url, this.buildURL(type, id, snapshot, 'findHasMany'));
527
+ return this.ajax(url, 'GET');
528
+ }
529
+
530
+ /**
531
+ Called by the store in order to fetch the JSON for the unloaded record in a
532
+ belongs-to relationship that was originally specified as a URL (inside of
533
+ `links`).
534
+ For example, if your original payload looks like this:
535
+ ```js
536
+ {
537
+ "person": {
538
+ "id": 1,
539
+ "name": "Tom Dale",
540
+ "links": { "group": "/people/1/group" }
541
+ }
542
+ }
543
+ ```
544
+ This method will be called with the parent record and `/people/1/group`.
545
+ The `findBelongsTo` method will make an Ajax (HTTP GET) request to the originally specified URL.
546
+ The format of your `links` value will influence the final request URL via the `urlPrefix` method:
547
+ * Links beginning with `//`, `http://`, `https://`, will be used as is, with no further manipulation.
548
+ * Links beginning with a single `/` will have the current adapter's `host` value prepended to it.
549
+ * Links with no beginning `/` will have a parentURL prepended to it, via the current adapter's `buildURL`.
550
+ @public
551
+ */
552
+ findBelongsTo(store, snapshot, url, relationship) {
553
+ const id = snapshot.id;
554
+ const type = snapshot.modelName;
555
+ (test => {
556
+ if (!test) {
557
+ throw new Error(`Attempted to fetch the belongsTo relationship for ${type}, but the record has no id`);
558
+ }
559
+ })(typeof id === 'string' && id.length > 0);
560
+ url = this.urlPrefix(url, this.buildURL(type, id, snapshot, 'findBelongsTo'));
561
+ return this.ajax(url, 'GET');
562
+ }
563
+
564
+ /**
565
+ Called by the store when a newly created record is
566
+ saved via the `save` method on a model record instance.
567
+ The `createRecord` method serializes the record and makes an Ajax (HTTP POST) request
568
+ to a URL computed by `buildURL`.
569
+ See `serialize` for information on how to customize the serialized form
570
+ of a record.
571
+ @public
572
+ */
573
+ createRecord(store, type, snapshot) {
574
+ const url = this.buildURL(type.modelName, null, snapshot, 'createRecord');
575
+ const data = serializeIntoHash(store, type, snapshot);
576
+ return this.ajax(url, 'POST', {
577
+ data
578
+ });
579
+ }
580
+
581
+ /**
582
+ Called by the store when an existing record is saved
583
+ via the `save` method on a model record instance.
584
+ The `updateRecord` method serializes the record and makes an Ajax (HTTP PUT) request
585
+ to a URL computed by `buildURL`.
586
+ See `serialize` for information on how to customize the serialized form
587
+ of a record.
588
+ @public
589
+ */
590
+ updateRecord(store, schema, snapshot) {
591
+ const data = serializeIntoHash(store, schema, snapshot, {});
592
+ const type = snapshot.modelName;
593
+ const id = snapshot.id;
594
+ (test => {
595
+ if (!test) {
596
+ throw new Error(`Attempted to update the ${type} record, but the record has no id`);
597
+ }
598
+ })(typeof id === 'string' && id.length > 0);
599
+ const url = this.buildURL(type, id, snapshot, 'updateRecord');
600
+ return this.ajax(url, 'PUT', {
601
+ data
602
+ });
603
+ }
604
+
605
+ /**
606
+ Called by the store when a record is deleted.
607
+ The `deleteRecord` method makes an Ajax (HTTP DELETE) request to a URL computed by `buildURL`.
608
+ @public
609
+ */
610
+ deleteRecord(store, schema, snapshot) {
611
+ const type = snapshot.modelName;
612
+ const id = snapshot.id;
613
+ (test => {
614
+ if (!test) {
615
+ throw new Error(`Attempted to delete the ${type} record, but the record has no id`);
616
+ }
617
+ })(typeof id === 'string' && id.length > 0);
618
+ return this.ajax(this.buildURL(type, id, snapshot, 'deleteRecord'), 'DELETE');
619
+ }
620
+
621
+ /**
622
+ @private
623
+ */
624
+ _stripIDFromURL(store, snapshot) {
625
+ const type = snapshot.modelName;
626
+ const id = snapshot.id;
627
+ (test => {
628
+ if (!test) {
629
+ throw new Error(`Attempted to strip the url from the ${type} record for coalescing, but the record has no id`);
630
+ }
631
+ })(typeof id === 'string' && id.length > 0);
632
+ const url = this.buildURL(type, id, snapshot);
633
+ const expandedURL = url.split('/');
634
+ // Case when the url is of the format ...something/:id
635
+ // We are decodeURIComponent-ing the lastSegment because if it represents
636
+ // the id, it has been encodeURIComponent-ified within `buildURL`. If we
637
+ // don't do this, then records with id having special characters are not
638
+ // coalesced correctly (see GH #4190 for the reported bug)
639
+ const lastSegment = expandedURL[expandedURL.length - 1];
640
+ if (decodeURIComponent(lastSegment) === id) {
641
+ expandedURL[expandedURL.length - 1] = '';
642
+ } else if (id && endsWith(lastSegment, '?id=' + id)) {
643
+ //Case when the url is of the format ...something?id=:id
644
+ expandedURL[expandedURL.length - 1] = lastSegment.substring(0, lastSegment.length - id.length - 1);
645
+ }
646
+ return expandedURL.join('/');
647
+ }
648
+
649
+ // http://stackoverflow.com/questions/417142/what-is-the-maximum-length-of-a-url-in-different-browsers
650
+ maxURLLength = 2048;
651
+
652
+ /**
653
+ Organize records into groups, each of which is to be passed to separate
654
+ calls to `findMany`.
655
+ This implementation groups together records that have the same base URL but
656
+ differing ids. For example `/comments/1` and `/comments/2` will be grouped together
657
+ because we know findMany can coalesce them together as `/comments?ids[]=1&ids[]=2`
658
+ It also supports urls where ids are passed as a query param, such as `/comments?id=1`
659
+ but not those where there is more than 1 query param such as `/comments?id=2&name=David`
660
+ Currently only the query param of `id` is supported. If you need to support others, please
661
+ override this or the `_stripIDFromURL` method.
662
+ It does not group records that have differing base urls, such as for example: `/posts/1/comments/2`
663
+ and `/posts/2/comments/3`
664
+ @public
665
+ @return an array of arrays of records, each of which is to be
666
+ loaded separately by `findMany`.
667
+ */
668
+ groupRecordsForFindMany(store, snapshots) {
669
+ const groups = new Map();
670
+ const maxURLLength = this.maxURLLength;
671
+ snapshots.forEach(snapshot => {
672
+ const baseUrl = this._stripIDFromURL(store, snapshot);
673
+ if (!groups.has(baseUrl)) {
674
+ groups.set(baseUrl, []);
675
+ }
676
+ groups.get(baseUrl).push(snapshot);
677
+ });
678
+ const groupsArray = [];
679
+ groups.forEach((group, key) => {
680
+ const paramNameLength = '&ids%5B%5D='.length;
681
+ const splitGroups = splitGroupToFitInUrl(store, this, group, maxURLLength, paramNameLength);
682
+ splitGroups.forEach(splitGroup => groupsArray.push(splitGroup));
683
+ });
684
+ return groupsArray;
685
+ }
686
+
687
+ /**
688
+ Takes an ajax response, and returns the json payload or an error.
689
+ By default this hook just returns the json payload passed to it.
690
+ You might want to override it in two cases:
691
+ 1. Your API might return useful results in the response headers.
692
+ Response headers are passed in as the second argument.
693
+ 2. Your API might return errors as successful responses with status code
694
+ 200 and an Errors text or object. You can return a `InvalidError` or a
695
+ `AdapterError` (or a sub class) from this hook and it will automatically
696
+ reject the promise and put your record into the invalid or error state.
697
+ Returning a `InvalidError` from this method will cause the
698
+ record to transition into the `invalid` state and make the
699
+ `errors` object available on the record. When returning an
700
+ `InvalidError` the store will attempt to normalize the error data
701
+ returned from the server using the serializer's `extractErrors`
702
+ method.
703
+ @since 1.13.0
704
+ @public
705
+ */
706
+ handleResponse(status, headers, payload, requestData) {
707
+ if (this.isSuccess(status, headers, payload)) {
708
+ return payload;
709
+ } else if (this.isInvalid(status, headers, payload)) {
710
+ // @ts-expect-error needs cast to ApiError
711
+ return new InvalidError(typeof payload === 'object' && 'errors' in payload ? payload.errors : undefined);
712
+ }
713
+ const errors = this.normalizeErrorResponse(status, headers, payload);
714
+ const detailedMessage = this.generatedDetailedMessage(status, headers, payload, requestData);
715
+ switch (status) {
716
+ case 401:
717
+ return new UnauthorizedError(errors, detailedMessage);
718
+ case 403:
719
+ return new ForbiddenError(errors, detailedMessage);
720
+ case 404:
721
+ return new NotFoundError(errors, detailedMessage);
722
+ case 409:
723
+ return new ConflictError(errors, detailedMessage);
724
+ default:
725
+ if (status >= 500) {
726
+ return new ServerError(errors, detailedMessage);
727
+ }
728
+ }
729
+ return new AdapterError(errors, detailedMessage);
730
+ }
731
+
732
+ /**
733
+ Default `handleResponse` implementation uses this hook to decide if the
734
+ response is a success.
735
+ @since 1.13.0
736
+ @public
737
+ */
738
+ isSuccess(status, _headers, _payload) {
739
+ return status >= 200 && status < 300 || status === 304;
740
+ }
741
+
742
+ /**
743
+ Default `handleResponse` implementation uses this hook to decide if the
744
+ response is an invalid error.
745
+ @since 1.13.0
746
+ @public
747
+ */
748
+ isInvalid(status, _headers, _payload) {
749
+ return status === 422;
750
+ }
751
+
752
+ /**
753
+ Takes a URL, an HTTP method and a hash of data, and makes an
754
+ HTTP request.
755
+ When the server responds with a payload, WarpDrive will call into `extractSingle`
756
+ or `extractArray` (depending on whether the original query was for one record or
757
+ many records).
758
+ By default, `ajax` method has the following behavior:
759
+ * It sets the response `dataType` to `"json"`
760
+ * If the HTTP method is not `"GET"`, it sets the `Content-Type` to be
761
+ `application/json; charset=utf-8`
762
+ * If the HTTP method is not `"GET"`, it stringifies the data passed in. The
763
+ data is the serialized record in the case of a save.
764
+ * Registers success and failure handlers.
765
+ @private
766
+ */
767
+ async ajax(url, type, options = {}) {
768
+ const requestData = {
769
+ url: url,
770
+ method: type
771
+ };
772
+ if (this.useFetch) {
773
+ // @ts-expect-error poorly typed
774
+ const hash = this.ajaxOptions(url, type, options);
775
+ const response = await this._fetchRequest(hash);
776
+ const payload = await determineBodyPromise(response, requestData);
777
+ if (response.ok && !(payload instanceof Error)) {
778
+ return fetchSuccessHandler(this, payload, response, requestData);
779
+ } else {
780
+ // eslint-disable-next-line @typescript-eslint/only-throw-error
781
+ throw fetchErrorHandler(this, payload, response, null, requestData);
782
+ }
783
+ } else {
784
+ return execjQAjax(this, requestData, options);
785
+ }
786
+ }
787
+
788
+ /**
789
+ @private
790
+ @param options jQuery ajax options to be used for the ajax request
791
+ */
792
+ _ajaxRequest(options) {
793
+ (test => {
794
+ if (!test) {
795
+ throw new Error('You must install jQuery globally when `useFetch` is false');
796
+ }
797
+ })(typeof jQuery !== 'undefined');
798
+ void jQuery.ajax(options);
799
+ }
800
+ /**
801
+ @private
802
+ */
803
+ _fetchRequest(options) {
804
+ const fetchFunction = getFetchFunction();
805
+ return fetchFunction(options.url, options);
806
+ }
807
+ /**
808
+ @private
809
+ */
810
+ _ajax(options) {
811
+ if (this.useFetch) {
812
+ void this._fetchRequest(options);
813
+ } else {
814
+ this._ajaxRequest(options);
815
+ }
816
+ }
817
+
818
+ /**
819
+ @private
820
+ */
821
+ ajaxOptions(url, method, options) {
822
+ let reqOptions = Object.assign({
823
+ url,
824
+ method,
825
+ type: method
826
+ }, options);
827
+ if (this.headers !== undefined) {
828
+ // @ts-expect-error poorly typed
829
+ reqOptions.headers = {
830
+ ...this.headers,
831
+ ...reqOptions.headers
832
+ };
833
+ } else if (!options.headers) {
834
+ reqOptions.headers = {};
835
+ }
836
+
837
+ // @ts-expect-error poorly typed
838
+ const contentType = reqOptions.contentType || this._defaultContentType;
839
+ if (this.useFetch) {
840
+ // @ts-expect-error poorly typed
841
+ if (reqOptions.data && reqOptions.type !== 'GET' && reqOptions.headers) {
842
+ if (!reqOptions.headers['Content-Type'] && !reqOptions.headers['content-type']) {
843
+ reqOptions.headers['content-type'] = contentType;
844
+ }
845
+ }
846
+ // @ts-expect-error poorly typed
847
+ reqOptions = fetchOptions(reqOptions);
848
+ } else {
849
+ // GET requests without a body should not have a content-type header
850
+ // and may be unexpected by a server
851
+ // @ts-expect-error poorly typed
852
+ if (reqOptions.data && reqOptions.type !== 'GET') {
853
+ reqOptions = {
854
+ ...reqOptions,
855
+ contentType
856
+ };
857
+ }
858
+ // @ts-expect-error poorly typed
859
+ reqOptions = ajaxOptions(reqOptions, this);
860
+ }
861
+ reqOptions.url = this._ajaxURL(reqOptions.url);
862
+ return reqOptions;
863
+ }
864
+
865
+ /**
866
+ * @private
867
+ */
868
+ _ajaxURL(url) {
869
+ if (this.fastboot?.isFastBoot) {
870
+ const httpRegex = /^https?:\/\//;
871
+ const protocolRelativeRegex = /^\/\//;
872
+ const protocol = this.fastboot.request.protocol;
873
+ const host = this.fastboot.request.host;
874
+ if (protocolRelativeRegex.test(url)) {
875
+ return `${protocol}${url}`;
876
+ } else if (!httpRegex.test(url)) {
877
+ try {
878
+ return `${protocol}//${host}${url}`;
879
+ } catch (fbError) {
880
+ throw new Error('You are using WarpDrive with no host defined in your adapter. This will attempt to use the host of the FastBoot request, which is not configured for the current host of this request. Please set the hostWhitelist property for in your environment.js. FastBoot Error: ' + fbError.message);
881
+ }
882
+ }
883
+ }
884
+ return url;
885
+ }
886
+
887
+ /**
888
+ @private
889
+ */
890
+ parseErrorResponse(responseText) {
891
+ let json = responseText;
892
+ try {
893
+ json = JSON.parse(responseText);
894
+ } catch {
895
+ // ignored
896
+ }
897
+ return json;
898
+ }
899
+
900
+ /**
901
+ @private
902
+ @return errors payload
903
+ */
904
+ normalizeErrorResponse(status, _headers, payload) {
905
+ if (payload && typeof payload === 'object' && 'errors' in payload && Array.isArray(payload.errors)) {
906
+ return payload.errors;
907
+ } else {
908
+ return [{
909
+ status: `${status}`,
910
+ // Set to a string per the JSON API spec: https://jsonapi.org/format/#errors
911
+ title: 'The backend responded with an error',
912
+ // Detail is intended to be a string, but backends be non-compliant.
913
+ // stringifying gives the user a more friendly error in this situation, whereas
914
+ // they'd instead receive [object Object].
915
+ // JSON.stringify will convert *anything* to a string without erroring.
916
+ detail: typeof payload === 'string' ? payload : JSON.stringify(payload)
917
+ }];
918
+ }
919
+ }
920
+
921
+ /**
922
+ Generates a detailed ("friendly") error message, with plenty
923
+ of information for debugging (good luck!)
924
+ @private
925
+ @return detailed error message
926
+ */
927
+ generatedDetailedMessage(status, headers, payload, requestData) {
928
+ let shortenedPayload;
929
+ const payloadContentType = headers['content-type'] || 'Empty Content-Type';
930
+ if (payloadContentType === 'text/html' && typeof payload === 'string' && payload.length > 250) {
931
+ shortenedPayload = '[Omitted Lengthy HTML]';
932
+ } else if (typeof payload === 'object' && payload !== null) {
933
+ shortenedPayload = JSON.stringify(payload, null, 2);
934
+ } else {
935
+ shortenedPayload = payload;
936
+ }
937
+ const requestDescription = requestData.method + ' ' + requestData.url;
938
+ const payloadDescription = 'Payload (' + payloadContentType + ')';
939
+ return ['Ember Data Request ' + requestDescription + ' returned a ' + status, payloadDescription, shortenedPayload].join('\n');
940
+ }
941
+
942
+ /**
943
+ Used by `findAll` and `findRecord` to build the query's `data` hash
944
+ supplied to the ajax method.
945
+ @since 2.5.0
946
+ @public
947
+ */
948
+ buildQuery(snapshot) {
949
+ const query = {};
950
+ if (snapshot) {
951
+ const {
952
+ include
953
+ } = snapshot;
954
+ if (include) {
955
+ // note: if user passed in an array, this will serialize like `?include[]=foo&include[]=bar`
956
+ // but if user passed in a string, this will serialize like `?include=foo,bar`
957
+ // users that want consistent behavior should override this method
958
+ query.include = include;
959
+ }
960
+ }
961
+ return query;
962
+ }
963
+ }
964
+
965
+ // eslint-disable-next-line @typescript-eslint/no-empty-object-type
966
+
967
+ function ajaxSuccess(adapter, payload, requestData, responseData) {
968
+ let response;
969
+ try {
970
+ response = adapter.handleResponse(responseData.status, responseData.headers, payload, requestData);
971
+ } catch (error) {
972
+ // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
973
+ return Promise.reject(error);
974
+ }
975
+ if (response && response.isAdapterError) {
976
+ // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
977
+ return Promise.reject(response);
978
+ } else {
979
+ return response;
980
+ }
981
+ }
982
+ function ajaxError(adapter, payload, requestData, responseData) {
983
+ let error;
984
+ if (responseData.errorThrown instanceof Error && payload !== '') {
985
+ error = responseData.errorThrown;
986
+ } else if (responseData.textStatus === 'timeout') {
987
+ error = new TimeoutError();
988
+ } else if (responseData.textStatus === 'abort' || responseData.status === 0) {
989
+ error = handleAbort(requestData, responseData);
990
+ } else {
991
+ try {
992
+ error = adapter.handleResponse(responseData.status, responseData.headers, payload || responseData.errorThrown, requestData);
993
+ } catch (e) {
994
+ error = e;
995
+ }
996
+ }
997
+ return error;
998
+ }
999
+
1000
+ // Adapter abort error to include any relevent info, e.g. request/response:
1001
+ function handleAbort(requestData, responseData) {
1002
+ const {
1003
+ method,
1004
+ url,
1005
+ errorThrown
1006
+ } = requestData;
1007
+ const {
1008
+ status
1009
+ } = responseData;
1010
+ const msg = `Request failed: ${method} ${url} ${String(errorThrown ?? '')}`;
1011
+ const errors = [{
1012
+ title: 'Adapter Error',
1013
+ detail: msg.trim(),
1014
+ status
1015
+ }];
1016
+ return new AbortError(errors);
1017
+ }
1018
+
1019
+ //From http://stackoverflow.com/questions/280634/endswith-in-javascript
1020
+ function endsWith(string, suffix) {
1021
+ if (typeof String.prototype.endsWith !== 'function') {
1022
+ return string.includes(suffix, string.length - suffix.length);
1023
+ } else {
1024
+ return string.endsWith(suffix);
1025
+ }
1026
+ }
1027
+ function fetchSuccessHandler(adapter, payload, response, requestData) {
1028
+ const responseData = fetchResponseData(response);
1029
+ return ajaxSuccess(adapter, payload, requestData, responseData);
1030
+ }
1031
+ function fetchErrorHandler(adapter, payload, response, errorThrown, requestData) {
1032
+ const responseData = fetchResponseData(response);
1033
+ if (responseData.status === 200 && payload instanceof Error) {
1034
+ responseData.errorThrown = payload;
1035
+ // @ts-expect-error poorly typed
1036
+ payload = responseData.errorThrown.payload;
1037
+ } else {
1038
+ responseData.errorThrown = errorThrown;
1039
+ if (typeof payload === 'string') {
1040
+ payload = adapter.parseErrorResponse(payload);
1041
+ }
1042
+ }
1043
+ return ajaxError(adapter, payload, requestData, responseData);
1044
+ }
1045
+ function ajaxSuccessHandler(adapter, payload, jqXHR, requestData) {
1046
+ const responseData = ajaxResponseData(jqXHR);
1047
+ return ajaxSuccess(adapter, payload, requestData, responseData);
1048
+ }
1049
+ function ajaxErrorHandler(adapter, jqXHR, errorThrown, requestData) {
1050
+ const responseData = ajaxResponseData(jqXHR);
1051
+ responseData.errorThrown = errorThrown;
1052
+ const payload = adapter.parseErrorResponse(jqXHR.responseText);
1053
+ {
1054
+ const message = `The server returned an empty string for ${requestData.method} ${requestData.url}, which cannot be parsed into a valid JSON. Return either null or {}.`;
1055
+ const validJSONString = !(responseData.textStatus === 'parsererror' && payload === '');
1056
+ console.warn(message, validJSONString, {
1057
+ id: 'ds.adapter.returned-empty-string-as-JSON'
1058
+ });
1059
+ }
1060
+ return ajaxError(adapter, payload, requestData, responseData);
1061
+ }
1062
+ function fetchResponseData(response) {
1063
+ return {
1064
+ status: response.status,
1065
+ textStatus: response.statusText,
1066
+ headers: headersToObject(response.headers)
1067
+ };
1068
+ }
1069
+ function ajaxResponseData(jqXHR) {
1070
+ return {
1071
+ status: jqXHR.status,
1072
+ textStatus: jqXHR.statusText,
1073
+ headers: parseResponseHeaders(jqXHR.getAllResponseHeaders())
1074
+ };
1075
+ }
1076
+ function headersToObject(headers) {
1077
+ const headersObject = {};
1078
+ if (headers) {
1079
+ headers.forEach((value, key) => headersObject[key] = value);
1080
+ }
1081
+ return headersObject;
1082
+ }
1083
+
1084
+ /**
1085
+ * Helper function that translates the options passed to `jQuery.ajax` into a format that `fetch` expects.
1086
+ *
1087
+ * @private
1088
+ */
1089
+ function fetchOptions(options, adapter) {
1090
+ options.credentials = options.credentials || 'same-origin';
1091
+ if (options.data) {
1092
+ // GET and HEAD requests can't have a `body`
1093
+ if (options.method === 'GET' || options.method === 'HEAD') {
1094
+ // If no options are passed, WarpDrive sets `data` to an empty object, which we test for.
1095
+ if (Object.keys(options.data).length && options.url) {
1096
+ // Test if there are already query params in the url (mimics jQuey.ajax).
1097
+ const queryParamDelimiter = options.url.includes('?') ? '&' : '?';
1098
+ options.url += `${queryParamDelimiter}${serializeQueryParams(options.data)}`;
1099
+ }
1100
+ } else {
1101
+ // NOTE: a request's body cannot be an object, so we stringify it if it is.
1102
+ // JSON.stringify removes keys with values of `undefined` (mimics jQuery.ajax).
1103
+ // If the data is not a POJO (it's a String, FormData, etc), we just set it.
1104
+ // If the data is a string, we assume it's a stringified object.
1105
+
1106
+ /* We check for Objects this way because we want the logic inside the consequent to run
1107
+ * if `options.data` is a POJO, not if it is a data structure whose `typeof` returns "object"
1108
+ * when it's not (Array, FormData, etc). The reason we don't use `options.data.constructor`
1109
+ * to check is in case `data` is an object with no prototype (e.g. created with null).
1110
+ */
1111
+ if (Object.prototype.toString.call(options.data) === '[object Object]') {
1112
+ options.body = JSON.stringify(options.data);
1113
+ } else {
1114
+ // @ts-expect-error poorly typed
1115
+ options.body = options.data;
1116
+ }
1117
+ }
1118
+ }
1119
+ return options;
1120
+ }
1121
+ function ajaxOptions(options, adapter) {
1122
+ options.dataType = 'json';
1123
+ options.context = adapter;
1124
+ if (options.data && options.type !== 'GET') {
1125
+ options.data = JSON.stringify(options.data);
1126
+ }
1127
+ options.beforeSend = function (xhr) {
1128
+ if (options.headers) {
1129
+ Object.keys(options.headers).forEach(key => {
1130
+ const headerValue = options.headers && options.headers[key];
1131
+ const isString = value => typeof value === 'string';
1132
+ if (isString(headerValue)) {
1133
+ xhr.setRequestHeader(key, headerValue);
1134
+ }
1135
+ });
1136
+ }
1137
+ };
1138
+ return options;
1139
+ }
1140
+ function execjQAjax(adapter, requestData, options) {
1141
+ const hash = adapter.ajaxOptions(requestData.url, requestData.method, options);
1142
+ return new Promise((resolve, reject) => {
1143
+ hash.success = function (payload, textStatus, jqXHR) {
1144
+ const response = ajaxSuccessHandler(adapter, payload, jqXHR, requestData);
1145
+ resolve(response);
1146
+ };
1147
+ hash.error = function (jqXHR, textStatus, errorThrown) {
1148
+ const error = ajaxErrorHandler(adapter, jqXHR, errorThrown, requestData);
1149
+ // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
1150
+ reject(error);
1151
+ };
1152
+ adapter._ajax(hash);
1153
+ });
1154
+ }
1155
+ function splitGroupToFitInUrl(store, adapter, group, maxURLLength, paramNameLength) {
1156
+ let idsSize = 0;
1157
+ const baseUrl = adapter._stripIDFromURL(store, group[0]);
1158
+ const splitGroups = [[]];
1159
+ group.forEach(snapshot => {
1160
+ const additionalLength = encodeURIComponent(snapshot.id).length + paramNameLength;
1161
+ if (baseUrl.length + idsSize + additionalLength >= maxURLLength) {
1162
+ idsSize = 0;
1163
+ splitGroups.push([]);
1164
+ }
1165
+ idsSize += additionalLength;
1166
+ const lastGroupIndex = splitGroups.length - 1;
1167
+ splitGroups[lastGroupIndex].push(snapshot);
1168
+ });
1169
+ return splitGroups;
1170
+ }
1171
+ export { RESTAdapter, fetchOptions };