@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,1218 @@
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-DYU2egXl.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
+ url = this.urlPrefix(url, this.buildURL(type, id, snapshot, 'findHasMany'));
571
+ return this.ajax(url, 'GET');
572
+ }
573
+
574
+ /**
575
+ Called by the store in order to fetch the JSON for the unloaded record in a
576
+ belongs-to relationship that was originally specified as a URL (inside of
577
+ `links`).
578
+ For example, if your original payload looks like this:
579
+ ```js
580
+ {
581
+ "person": {
582
+ "id": 1,
583
+ "name": "Tom Dale",
584
+ "links": { "group": "/people/1/group" }
585
+ }
586
+ }
587
+ ```
588
+ This method will be called with the parent record and `/people/1/group`.
589
+ The `findBelongsTo` method will make an Ajax (HTTP GET) request to the originally specified URL.
590
+ The format of your `links` value will influence the final request URL via the `urlPrefix` method:
591
+ * Links beginning with `//`, `http://`, `https://`, will be used as is, with no further manipulation.
592
+ * Links beginning with a single `/` will have the current adapter's `host` value prepended to it.
593
+ * Links with no beginning `/` will have a parentURL prepended to it, via the current adapter's `buildURL`.
594
+ @public
595
+ */
596
+ findBelongsTo(store, snapshot, url, relationship) {
597
+ const id = snapshot.id;
598
+ const type = snapshot.modelName;
599
+ url = this.urlPrefix(url, this.buildURL(type, id, snapshot, 'findBelongsTo'));
600
+ return this.ajax(url, 'GET');
601
+ }
602
+
603
+ /**
604
+ Called by the store when a newly created record is
605
+ saved via the `save` method on a model record instance.
606
+ The `createRecord` method serializes the record and makes an Ajax (HTTP POST) request
607
+ to a URL computed by `buildURL`.
608
+ See `serialize` for information on how to customize the serialized form
609
+ of a record.
610
+ @public
611
+ @param {Store} store
612
+ @param {Model} type
613
+ @param {Snapshot} snapshot
614
+ @return {Promise} promise
615
+ */
616
+ createRecord(store, type, snapshot) {
617
+ const url = this.buildURL(type.modelName, null, snapshot, 'createRecord');
618
+ const data = serializeIntoHash(store, type, snapshot);
619
+ return this.ajax(url, 'POST', {
620
+ data
621
+ });
622
+ }
623
+
624
+ /**
625
+ Called by the store when an existing record is saved
626
+ via the `save` method on a model record instance.
627
+ The `updateRecord` method serializes the record and makes an Ajax (HTTP PUT) request
628
+ to a URL computed by `buildURL`.
629
+ See `serialize` for information on how to customize the serialized form
630
+ of a record.
631
+ @public
632
+ @param {Store} store
633
+ @param {Model} schema
634
+ @param {Snapshot} snapshot
635
+ @return {Promise} promise
636
+ */
637
+ updateRecord(store, schema, snapshot) {
638
+ const data = serializeIntoHash(store, schema, snapshot, {});
639
+ const type = snapshot.modelName;
640
+ const id = snapshot.id;
641
+ const url = this.buildURL(type, id, snapshot, 'updateRecord');
642
+ return this.ajax(url, 'PUT', {
643
+ data
644
+ });
645
+ }
646
+
647
+ /**
648
+ Called by the store when a record is deleted.
649
+ The `deleteRecord` method makes an Ajax (HTTP DELETE) request to a URL computed by `buildURL`.
650
+ @public
651
+ @param {Store} store
652
+ @param {Model} type
653
+ @param {Snapshot} snapshot
654
+ @return {Promise} promise
655
+ */
656
+ deleteRecord(store, schema, snapshot) {
657
+ const type = snapshot.modelName;
658
+ const id = snapshot.id;
659
+ return this.ajax(this.buildURL(type, id, snapshot, 'deleteRecord'), 'DELETE');
660
+ }
661
+ _stripIDFromURL(store, snapshot) {
662
+ const type = snapshot.modelName;
663
+ const id = snapshot.id;
664
+ const url = this.buildURL(type, id, snapshot);
665
+ const expandedURL = url.split('/');
666
+ // Case when the url is of the format ...something/:id
667
+ // We are decodeURIComponent-ing the lastSegment because if it represents
668
+ // the id, it has been encodeURIComponent-ified within `buildURL`. If we
669
+ // don't do this, then records with id having special characters are not
670
+ // coalesced correctly (see GH #4190 for the reported bug)
671
+ const lastSegment = expandedURL[expandedURL.length - 1];
672
+ if (decodeURIComponent(lastSegment) === id) {
673
+ expandedURL[expandedURL.length - 1] = '';
674
+ } else if (id && endsWith(lastSegment, '?id=' + id)) {
675
+ //Case when the url is of the format ...something?id=:id
676
+ expandedURL[expandedURL.length - 1] = lastSegment.substring(0, lastSegment.length - id.length - 1);
677
+ }
678
+ return expandedURL.join('/');
679
+ }
680
+
681
+ // http://stackoverflow.com/questions/417142/what-is-the-maximum-length-of-a-url-in-different-browsers
682
+ maxURLLength = 2048;
683
+
684
+ /**
685
+ Organize records into groups, each of which is to be passed to separate
686
+ calls to `findMany`.
687
+ This implementation groups together records that have the same base URL but
688
+ differing ids. For example `/comments/1` and `/comments/2` will be grouped together
689
+ because we know findMany can coalesce them together as `/comments?ids[]=1&ids[]=2`
690
+ It also supports urls where ids are passed as a query param, such as `/comments?id=1`
691
+ but not those where there is more than 1 query param such as `/comments?id=2&name=David`
692
+ Currently only the query param of `id` is supported. If you need to support others, please
693
+ override this or the `_stripIDFromURL` method.
694
+ It does not group records that have differing base urls, such as for example: `/posts/1/comments/2`
695
+ and `/posts/2/comments/3`
696
+ @public
697
+ @param {Store} store
698
+ @param {Array} snapshots
699
+ @return {Array} an array of arrays of records, each of which is to be
700
+ loaded separately by `findMany`.
701
+ */
702
+ groupRecordsForFindMany(store, snapshots) {
703
+ const groups = new Map();
704
+ const maxURLLength = this.maxURLLength;
705
+ snapshots.forEach(snapshot => {
706
+ const baseUrl = this._stripIDFromURL(store, snapshot);
707
+ if (!groups.has(baseUrl)) {
708
+ groups.set(baseUrl, []);
709
+ }
710
+ groups.get(baseUrl).push(snapshot);
711
+ });
712
+ const groupsArray = [];
713
+ groups.forEach((group, key) => {
714
+ const paramNameLength = '&ids%5B%5D='.length;
715
+ const splitGroups = splitGroupToFitInUrl(store, this, group, maxURLLength, paramNameLength);
716
+ splitGroups.forEach(splitGroup => groupsArray.push(splitGroup));
717
+ });
718
+ return groupsArray;
719
+ }
720
+
721
+ /**
722
+ Takes an ajax response, and returns the json payload or an error.
723
+ By default this hook just returns the json payload passed to it.
724
+ You might want to override it in two cases:
725
+ 1. Your API might return useful results in the response headers.
726
+ Response headers are passed in as the second argument.
727
+ 2. Your API might return errors as successful responses with status code
728
+ 200 and an Errors text or object. You can return a `InvalidError` or a
729
+ `AdapterError` (or a sub class) from this hook and it will automatically
730
+ reject the promise and put your record into the invalid or error state.
731
+ Returning a `InvalidError` from this method will cause the
732
+ record to transition into the `invalid` state and make the
733
+ `errors` object available on the record. When returning an
734
+ `InvalidError` the store will attempt to normalize the error data
735
+ returned from the server using the serializer's `extractErrors`
736
+ method.
737
+ @since 1.13.0
738
+ @public
739
+ @param {Number} status
740
+ @param {Object} headers
741
+ @param {Object} payload
742
+ @param {Object} requestData - the original request information
743
+ @return {Object | AdapterError} response
744
+ */
745
+ handleResponse(status, headers, payload, requestData) {
746
+ if (this.isSuccess(status, headers, payload)) {
747
+ return payload;
748
+ } else if (this.isInvalid(status, headers, payload)) {
749
+ // @ts-expect-error needs cast to ApiError
750
+ return new InvalidError(typeof payload === 'object' && 'errors' in payload ? payload.errors : undefined);
751
+ }
752
+ const errors = this.normalizeErrorResponse(status, headers, payload);
753
+ const detailedMessage = this.generatedDetailedMessage(status, headers, payload, requestData);
754
+ switch (status) {
755
+ case 401:
756
+ return new UnauthorizedError(errors, detailedMessage);
757
+ case 403:
758
+ return new ForbiddenError(errors, detailedMessage);
759
+ case 404:
760
+ return new NotFoundError(errors, detailedMessage);
761
+ case 409:
762
+ return new ConflictError(errors, detailedMessage);
763
+ default:
764
+ if (status >= 500) {
765
+ return new ServerError(errors, detailedMessage);
766
+ }
767
+ }
768
+ return new AdapterError(errors, detailedMessage);
769
+ }
770
+
771
+ /**
772
+ Default `handleResponse` implementation uses this hook to decide if the
773
+ response is a success.
774
+ @since 1.13.0
775
+ @public
776
+ @param {Number} status
777
+ @param {Object} headers
778
+ @param {Object} payload
779
+ @return {Boolean}
780
+ */
781
+ isSuccess(status, _headers, _payload) {
782
+ return status >= 200 && status < 300 || status === 304;
783
+ }
784
+
785
+ /**
786
+ Default `handleResponse` implementation uses this hook to decide if the
787
+ response is an invalid error.
788
+ @since 1.13.0
789
+ @public
790
+ @param {Number} status
791
+ @param {Object} headers
792
+ @param {Object} payload
793
+ @return {Boolean}
794
+ */
795
+ isInvalid(status, _headers, _payload) {
796
+ return status === 422;
797
+ }
798
+
799
+ /**
800
+ Takes a URL, an HTTP method and a hash of data, and makes an
801
+ HTTP request.
802
+ When the server responds with a payload, Ember Data will call into `extractSingle`
803
+ or `extractArray` (depending on whether the original query was for one record or
804
+ many records).
805
+ By default, `ajax` method has the following behavior:
806
+ * It sets the response `dataType` to `"json"`
807
+ * If the HTTP method is not `"GET"`, it sets the `Content-Type` to be
808
+ `application/json; charset=utf-8`
809
+ * If the HTTP method is not `"GET"`, it stringifies the data passed in. The
810
+ data is the serialized record in the case of a save.
811
+ * Registers success and failure handlers.
812
+ @private
813
+ @param {String} url
814
+ @param {String} type The request type GET, POST, PUT, DELETE etc.
815
+ @param {Object} options
816
+ @return {Promise} promise
817
+ */
818
+ async ajax(url, type, options = {}) {
819
+ const requestData = {
820
+ url: url,
821
+ method: type
822
+ };
823
+ if (this.useFetch) {
824
+ // @ts-expect-error poorly typed
825
+ const hash = this.ajaxOptions(url, type, options);
826
+ const response = await this._fetchRequest(hash);
827
+ const payload = await determineBodyPromise(response, requestData);
828
+ if (response.ok && !(payload instanceof Error)) {
829
+ return fetchSuccessHandler(this, payload, response, requestData);
830
+ } else {
831
+ // eslint-disable-next-line @typescript-eslint/only-throw-error
832
+ throw fetchErrorHandler(this, payload, response, null, requestData);
833
+ }
834
+ } else {
835
+ return execjQAjax(this, requestData, options);
836
+ }
837
+ }
838
+
839
+ /**
840
+ @private
841
+ @param {Object} options jQuery ajax options to be used for the ajax request
842
+ */
843
+ _ajaxRequest(options) {
844
+ void jQuery.ajax(options);
845
+ }
846
+ _fetchRequest(options) {
847
+ const fetchFunction = getFetchFunction();
848
+ return fetchFunction(options.url, options);
849
+ }
850
+ _ajax(options) {
851
+ if (this.useFetch) {
852
+ void this._fetchRequest(options);
853
+ } else {
854
+ this._ajaxRequest(options);
855
+ }
856
+ }
857
+
858
+ /**
859
+ @private
860
+ @param {String} url
861
+ @param {String} type The request type GET, POST, PUT, DELETE etc.
862
+ @param {Object} options
863
+ @return {Object}
864
+ */
865
+ ajaxOptions(url, method, options) {
866
+ let reqOptions = Object.assign({
867
+ url,
868
+ method,
869
+ type: method
870
+ }, options);
871
+ if (this.headers !== undefined) {
872
+ // @ts-expect-error poorly typed
873
+ reqOptions.headers = {
874
+ ...this.headers,
875
+ ...reqOptions.headers
876
+ };
877
+ } else if (!options.headers) {
878
+ reqOptions.headers = {};
879
+ }
880
+
881
+ // @ts-expect-error poorly typed
882
+ const contentType = reqOptions.contentType || this._defaultContentType;
883
+ if (this.useFetch) {
884
+ // @ts-expect-error poorly typed
885
+ if (reqOptions.data && reqOptions.type !== 'GET' && reqOptions.headers) {
886
+ if (!reqOptions.headers['Content-Type'] && !reqOptions.headers['content-type']) {
887
+ reqOptions.headers['content-type'] = contentType;
888
+ }
889
+ }
890
+ // @ts-expect-error poorly typed
891
+ reqOptions = fetchOptions(reqOptions);
892
+ } else {
893
+ // GET requests without a body should not have a content-type header
894
+ // and may be unexpected by a server
895
+ // @ts-expect-error poorly typed
896
+ if (reqOptions.data && reqOptions.type !== 'GET') {
897
+ reqOptions = {
898
+ ...reqOptions,
899
+ contentType
900
+ };
901
+ }
902
+ // @ts-expect-error poorly typed
903
+ reqOptions = ajaxOptions(reqOptions, this);
904
+ }
905
+ reqOptions.url = this._ajaxURL(reqOptions.url);
906
+ return reqOptions;
907
+ }
908
+ _ajaxURL(url) {
909
+ if (this.fastboot?.isFastBoot) {
910
+ const httpRegex = /^https?:\/\//;
911
+ const protocolRelativeRegex = /^\/\//;
912
+ const protocol = this.fastboot.request.protocol;
913
+ const host = this.fastboot.request.host;
914
+ if (protocolRelativeRegex.test(url)) {
915
+ return `${protocol}${url}`;
916
+ } else if (!httpRegex.test(url)) {
917
+ try {
918
+ return `${protocol}//${host}${url}`;
919
+ } catch (fbError) {
920
+ 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);
921
+ }
922
+ }
923
+ }
924
+ return url;
925
+ }
926
+
927
+ /**
928
+ @private
929
+ @param {String} responseText
930
+ @return {Object}
931
+ */
932
+ parseErrorResponse(responseText) {
933
+ let json = responseText;
934
+ try {
935
+ json = JSON.parse(responseText);
936
+ } catch {
937
+ // ignored
938
+ }
939
+ return json;
940
+ }
941
+
942
+ /**
943
+ @private
944
+ @param {Number} status
945
+ @param {Object} headers
946
+ @param {Object} payload
947
+ @return {Array} errors payload
948
+ */
949
+ normalizeErrorResponse(status, _headers, payload) {
950
+ if (payload && typeof payload === 'object' && 'errors' in payload && Array.isArray(payload.errors)) {
951
+ return payload.errors;
952
+ } else {
953
+ return [{
954
+ status: `${status}`,
955
+ // Set to a string per the JSON API spec: https://jsonapi.org/format/#errors
956
+ title: 'The backend responded with an error',
957
+ // Detail is intended to be a string, but backends be non-compliant.
958
+ // stringifying gives the user a more friendly error in this situation, whereas
959
+ // they'd instead receive [object Object].
960
+ // JSON.stringify will convert *anything* to a string without erroring.
961
+ detail: typeof payload === 'string' ? payload : JSON.stringify(payload)
962
+ }];
963
+ }
964
+ }
965
+
966
+ /**
967
+ Generates a detailed ("friendly") error message, with plenty
968
+ of information for debugging (good luck!)
969
+ @private
970
+ @param {Number} status
971
+ @param {Object} headers
972
+ @param {Object} payload
973
+ @param {Object} requestData
974
+ @return {String} detailed error message
975
+ */
976
+ generatedDetailedMessage(status, headers, payload, requestData) {
977
+ let shortenedPayload;
978
+ const payloadContentType = headers['content-type'] || 'Empty Content-Type';
979
+ if (payloadContentType === 'text/html' && typeof payload === 'string' && payload.length > 250) {
980
+ shortenedPayload = '[Omitted Lengthy HTML]';
981
+ } else if (typeof payload === 'object' && payload !== null) {
982
+ shortenedPayload = JSON.stringify(payload, null, 2);
983
+ } else {
984
+ shortenedPayload = payload;
985
+ }
986
+ const requestDescription = requestData.method + ' ' + requestData.url;
987
+ const payloadDescription = 'Payload (' + payloadContentType + ')';
988
+ return ['Ember Data Request ' + requestDescription + ' returned a ' + status, payloadDescription, shortenedPayload].join('\n');
989
+ }
990
+
991
+ /**
992
+ Used by `findAll` and `findRecord` to build the query's `data` hash
993
+ supplied to the ajax method.
994
+ @since 2.5.0
995
+ @public
996
+ @param {Snapshot} snapshot
997
+ @return {Object}
998
+ */
999
+ buildQuery(snapshot) {
1000
+ const query = {};
1001
+ if (snapshot) {
1002
+ const {
1003
+ include
1004
+ } = snapshot;
1005
+ if (include) {
1006
+ // note: if user passed in an array, this will serialize like `?include[]=foo&include[]=bar`
1007
+ // but if user passed in a string, this will serialize like `?include=foo,bar`
1008
+ // users that want consistent behavior should override this method
1009
+ query.include = include;
1010
+ }
1011
+ }
1012
+ return query;
1013
+ }
1014
+ }
1015
+
1016
+ // eslint-disable-next-line @typescript-eslint/no-empty-object-type
1017
+
1018
+ function ajaxSuccess(adapter, payload, requestData, responseData) {
1019
+ let response;
1020
+ try {
1021
+ response = adapter.handleResponse(responseData.status, responseData.headers, payload, requestData);
1022
+ } catch (error) {
1023
+ // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
1024
+ return Promise.reject(error);
1025
+ }
1026
+ if (response && response.isAdapterError) {
1027
+ // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
1028
+ return Promise.reject(response);
1029
+ } else {
1030
+ return response;
1031
+ }
1032
+ }
1033
+ function ajaxError(adapter, payload, requestData, responseData) {
1034
+ let error;
1035
+ if (responseData.errorThrown instanceof Error && payload !== '') {
1036
+ error = responseData.errorThrown;
1037
+ } else if (responseData.textStatus === 'timeout') {
1038
+ error = new TimeoutError();
1039
+ } else if (responseData.textStatus === 'abort' || responseData.status === 0) {
1040
+ error = handleAbort(requestData, responseData);
1041
+ } else {
1042
+ try {
1043
+ error = adapter.handleResponse(responseData.status, responseData.headers, payload || responseData.errorThrown, requestData);
1044
+ } catch (e) {
1045
+ error = e;
1046
+ }
1047
+ }
1048
+ return error;
1049
+ }
1050
+
1051
+ // Adapter abort error to include any relevent info, e.g. request/response:
1052
+ function handleAbort(requestData, responseData) {
1053
+ const {
1054
+ method,
1055
+ url,
1056
+ errorThrown
1057
+ } = requestData;
1058
+ const {
1059
+ status
1060
+ } = responseData;
1061
+ const msg = `Request failed: ${method} ${url} ${String(errorThrown ?? '')}`;
1062
+ const errors = [{
1063
+ title: 'Adapter Error',
1064
+ detail: msg.trim(),
1065
+ status
1066
+ }];
1067
+ return new AbortError(errors);
1068
+ }
1069
+
1070
+ //From http://stackoverflow.com/questions/280634/endswith-in-javascript
1071
+ function endsWith(string, suffix) {
1072
+ if (typeof String.prototype.endsWith !== 'function') {
1073
+ return string.includes(suffix, string.length - suffix.length);
1074
+ } else {
1075
+ return string.endsWith(suffix);
1076
+ }
1077
+ }
1078
+ function fetchSuccessHandler(adapter, payload, response, requestData) {
1079
+ const responseData = fetchResponseData(response);
1080
+ return ajaxSuccess(adapter, payload, requestData, responseData);
1081
+ }
1082
+ function fetchErrorHandler(adapter, payload, response, errorThrown, requestData) {
1083
+ const responseData = fetchResponseData(response);
1084
+ if (responseData.status === 200 && payload instanceof Error) {
1085
+ responseData.errorThrown = payload;
1086
+ // @ts-expect-error poorly typed
1087
+ payload = responseData.errorThrown.payload;
1088
+ } else {
1089
+ responseData.errorThrown = errorThrown;
1090
+ if (typeof payload === 'string') {
1091
+ payload = adapter.parseErrorResponse(payload);
1092
+ }
1093
+ }
1094
+ return ajaxError(adapter, payload, requestData, responseData);
1095
+ }
1096
+ function ajaxSuccessHandler(adapter, payload, jqXHR, requestData) {
1097
+ const responseData = ajaxResponseData(jqXHR);
1098
+ return ajaxSuccess(adapter, payload, requestData, responseData);
1099
+ }
1100
+ function ajaxErrorHandler(adapter, jqXHR, errorThrown, requestData) {
1101
+ const responseData = ajaxResponseData(jqXHR);
1102
+ responseData.errorThrown = errorThrown;
1103
+ const payload = adapter.parseErrorResponse(jqXHR.responseText);
1104
+ return ajaxError(adapter, payload, requestData, responseData);
1105
+ }
1106
+ function fetchResponseData(response) {
1107
+ return {
1108
+ status: response.status,
1109
+ textStatus: response.statusText,
1110
+ headers: headersToObject(response.headers)
1111
+ };
1112
+ }
1113
+ function ajaxResponseData(jqXHR) {
1114
+ return {
1115
+ status: jqXHR.status,
1116
+ textStatus: jqXHR.statusText,
1117
+ headers: parseResponseHeaders(jqXHR.getAllResponseHeaders())
1118
+ };
1119
+ }
1120
+ function headersToObject(headers) {
1121
+ const headersObject = {};
1122
+ if (headers) {
1123
+ headers.forEach((value, key) => headersObject[key] = value);
1124
+ }
1125
+ return headersObject;
1126
+ }
1127
+
1128
+ /**
1129
+ * Helper function that translates the options passed to `jQuery.ajax` into a format that `fetch` expects.
1130
+ *
1131
+ * @param {Object} _options
1132
+ * @param {Adapter} adapter
1133
+ * @private
1134
+ * @return {Object}
1135
+ */
1136
+ function fetchOptions(options, adapter) {
1137
+ options.credentials = options.credentials || 'same-origin';
1138
+ if (options.data) {
1139
+ // GET and HEAD requests can't have a `body`
1140
+ if (options.method === 'GET' || options.method === 'HEAD') {
1141
+ // If no options are passed, Ember Data sets `data` to an empty object, which we test for.
1142
+ if (Object.keys(options.data).length && options.url) {
1143
+ // Test if there are already query params in the url (mimics jQuey.ajax).
1144
+ const queryParamDelimiter = options.url.includes('?') ? '&' : '?';
1145
+ options.url += `${queryParamDelimiter}${serializeQueryParams(options.data)}`;
1146
+ }
1147
+ } else {
1148
+ // NOTE: a request's body cannot be an object, so we stringify it if it is.
1149
+ // JSON.stringify removes keys with values of `undefined` (mimics jQuery.ajax).
1150
+ // If the data is not a POJO (it's a String, FormData, etc), we just set it.
1151
+ // If the data is a string, we assume it's a stringified object.
1152
+
1153
+ /* We check for Objects this way because we want the logic inside the consequent to run
1154
+ * if `options.data` is a POJO, not if it is a data structure whose `typeof` returns "object"
1155
+ * when it's not (Array, FormData, etc). The reason we don't use `options.data.constructor`
1156
+ * to check is in case `data` is an object with no prototype (e.g. created with null).
1157
+ */
1158
+ if (Object.prototype.toString.call(options.data) === '[object Object]') {
1159
+ options.body = JSON.stringify(options.data);
1160
+ } else {
1161
+ // @ts-expect-error poorly typed
1162
+ options.body = options.data;
1163
+ }
1164
+ }
1165
+ }
1166
+ return options;
1167
+ }
1168
+ function ajaxOptions(options, adapter) {
1169
+ options.dataType = 'json';
1170
+ options.context = adapter;
1171
+ if (options.data && options.type !== 'GET') {
1172
+ options.data = JSON.stringify(options.data);
1173
+ }
1174
+ options.beforeSend = function (xhr) {
1175
+ if (options.headers) {
1176
+ Object.keys(options.headers).forEach(key => {
1177
+ const headerValue = options.headers && options.headers[key];
1178
+ const isString = value => typeof value === 'string';
1179
+ if (isString(headerValue)) {
1180
+ xhr.setRequestHeader(key, headerValue);
1181
+ }
1182
+ });
1183
+ }
1184
+ };
1185
+ return options;
1186
+ }
1187
+ function execjQAjax(adapter, requestData, options) {
1188
+ const hash = adapter.ajaxOptions(requestData.url, requestData.method, options);
1189
+ return new Promise((resolve, reject) => {
1190
+ hash.success = function (payload, textStatus, jqXHR) {
1191
+ const response = ajaxSuccessHandler(adapter, payload, jqXHR, requestData);
1192
+ resolve(response);
1193
+ };
1194
+ hash.error = function (jqXHR, textStatus, errorThrown) {
1195
+ const error = ajaxErrorHandler(adapter, jqXHR, errorThrown, requestData);
1196
+ // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
1197
+ reject(error);
1198
+ };
1199
+ adapter._ajax(hash);
1200
+ });
1201
+ }
1202
+ function splitGroupToFitInUrl(store, adapter, group, maxURLLength, paramNameLength) {
1203
+ let idsSize = 0;
1204
+ const baseUrl = adapter._stripIDFromURL(store, group[0]);
1205
+ const splitGroups = [[]];
1206
+ group.forEach(snapshot => {
1207
+ const additionalLength = encodeURIComponent(snapshot.id).length + paramNameLength;
1208
+ if (baseUrl.length + idsSize + additionalLength >= maxURLLength) {
1209
+ idsSize = 0;
1210
+ splitGroups.push([]);
1211
+ }
1212
+ idsSize += additionalLength;
1213
+ const lastGroupIndex = splitGroups.length - 1;
1214
+ splitGroups[lastGroupIndex].push(snapshot);
1215
+ });
1216
+ return splitGroups;
1217
+ }
1218
+ export { RESTAdapter, fetchOptions };