@warp-drive/legacy 5.8.0-alpha.4 → 5.8.0-alpha.40

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