@warp-drive/legacy 5.6.0-alpha.11

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