@warp-drive/legacy 5.6.0-alpha.15 → 5.6.0-alpha.18

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 (193) hide show
  1. package/declarations/adapter/-private/build-url-mixin.d.ts +41 -29
  2. package/declarations/adapter/-private/fastboot-interface.d.ts +4 -5
  3. package/declarations/adapter/-private/utils/continue-on-reject.d.ts +5 -6
  4. package/declarations/adapter/-private/utils/determine-body-promise.d.ts +1 -2
  5. package/declarations/adapter/-private/utils/fetch.d.ts +2 -3
  6. package/declarations/adapter/-private/utils/parse-response-headers.d.ts +0 -1
  7. package/declarations/adapter/-private/utils/serialize-into-hash.d.ts +5 -6
  8. package/declarations/adapter/-private/utils/serialize-query-params.d.ts +5 -2
  9. package/declarations/adapter/-private.d.ts +5 -6
  10. package/declarations/adapter/error.d.ts +206 -178
  11. package/declarations/adapter/json-api.d.ts +206 -207
  12. package/declarations/adapter/rest.d.ts +805 -808
  13. package/declarations/adapter.d.ts +742 -766
  14. package/declarations/compat/-private.d.ts +10 -11
  15. package/declarations/compat/builders/find-all.d.ts +26 -24
  16. package/declarations/compat/builders/find-record.d.ts +41 -39
  17. package/declarations/compat/builders/query.d.ts +50 -45
  18. package/declarations/compat/builders/save-record.d.ts +26 -24
  19. package/declarations/compat/builders/utils.d.ts +1 -2
  20. package/declarations/compat/builders.d.ts +10 -11
  21. package/declarations/compat/extensions.d.ts +52 -106
  22. package/declarations/compat/legacy-network-handler/fetch-manager.d.ts +39 -38
  23. package/declarations/compat/legacy-network-handler/identifier-has-id.d.ts +1 -2
  24. package/declarations/compat/legacy-network-handler/legacy-data-fetch.d.ts +9 -10
  25. package/declarations/compat/legacy-network-handler/legacy-data-utils.d.ts +1 -2
  26. package/declarations/compat/legacy-network-handler/legacy-network-handler.d.ts +1 -2
  27. package/declarations/compat/legacy-network-handler/minimum-adapter-interface.d.ts +521 -517
  28. package/declarations/compat/legacy-network-handler/minimum-serializer-interface.d.ts +214 -215
  29. package/declarations/compat/legacy-network-handler/serializer-response.d.ts +5 -6
  30. package/declarations/compat/legacy-network-handler/snapshot-record-array.d.ts +83 -84
  31. package/declarations/compat/legacy-network-handler/snapshot.d.ts +228 -229
  32. package/declarations/compat/utils.d.ts +118 -119
  33. package/declarations/compat.d.ts +106 -104
  34. package/declarations/model/-private/attr.d.ts +164 -145
  35. package/declarations/model/-private/belongs-to.d.ts +135 -122
  36. package/declarations/model/-private/debug/assert-polymorphic-type.d.ts +15 -4
  37. package/declarations/model/-private/errors.d.ts +272 -267
  38. package/declarations/model/-private/has-many.d.ts +119 -116
  39. package/declarations/model/-private/hooks.d.ts +5 -6
  40. package/declarations/model/-private/legacy-relationships-support.d.ts +51 -52
  41. package/declarations/model/-private/model-for-mixin.d.ts +18 -3
  42. package/declarations/model/-private/model-methods.d.ts +29 -24
  43. package/declarations/model/-private/model.d.ts +1306 -1254
  44. package/declarations/model/-private/notify-changes.d.ts +3 -4
  45. package/declarations/model/-private/promise-belongs-to.d.ts +33 -27
  46. package/declarations/model/-private/promise-many-array.d.ts +125 -116
  47. package/declarations/model/-private/record-state.d.ts +63 -60
  48. package/declarations/model/-private/references/belongs-to.d.ts +491 -487
  49. package/declarations/model/-private/references/has-many.d.ts +495 -492
  50. package/declarations/model/-private/schema-provider.d.ts +53 -56
  51. package/declarations/model/-private/type-utils.d.ts +41 -41
  52. package/declarations/model/-private/util.d.ts +1 -2
  53. package/declarations/model/-private.d.ts +8 -8
  54. package/declarations/model/migration-support.d.ts +281 -279
  55. package/declarations/model.d.ts +48 -49
  56. package/declarations/serializer/-private/embedded-records-mixin.d.ts +70 -71
  57. package/declarations/serializer/-private/transforms/boolean.d.ts +37 -37
  58. package/declarations/serializer/-private/transforms/date.d.ts +22 -22
  59. package/declarations/serializer/-private/transforms/number.d.ts +22 -22
  60. package/declarations/serializer/-private/transforms/string.d.ts +22 -22
  61. package/declarations/serializer/-private/transforms/transform.d.ts +109 -110
  62. package/declarations/serializer/-private/utils.d.ts +0 -1
  63. package/declarations/serializer/json-api.d.ts +124 -494
  64. package/declarations/serializer/json.d.ts +62 -1031
  65. package/declarations/serializer/rest.d.ts +52 -552
  66. package/declarations/serializer/transform.d.ts +5 -6
  67. package/declarations/serializer.d.ts +217 -218
  68. package/dist/{-private-DFfBszo5.js → -private-CKrP0ogQ.js} +1 -1
  69. package/dist/adapter/-private.js +1 -1
  70. package/dist/adapter/error.js +17 -0
  71. package/dist/adapter/json-api.js +1 -1
  72. package/dist/adapter/rest.js +5 -7
  73. package/dist/adapter.js +6 -35
  74. package/dist/compat/-private.js +1 -1
  75. package/dist/compat/extensions.js +0 -2
  76. package/dist/compat.js +3 -4
  77. package/dist/{errors-_QQ7xpSn.js → errors-BX5wowuz.js} +11 -10
  78. package/dist/{json-DSOlH9A8.js → json-Et4mt_LM.js} +84 -157
  79. package/dist/model/-private.js +1 -1
  80. package/dist/model/migration-support.js +3 -4
  81. package/dist/model.js +3 -3
  82. package/dist/{schema-provider-D_P8ReX6.js → schema-provider-BdQhkT-Q.js} +20 -13
  83. package/dist/{serialize-into-hash-BxfqWC8u.js → serialize-into-hash-Bp58npke.js} +1 -1
  84. package/dist/serializer/json-api.js +42 -2
  85. package/dist/serializer/json.js +1 -1
  86. package/dist/serializer/rest.js +46 -41
  87. package/dist/serializer/transform.js +0 -44
  88. package/package.json +7 -7
  89. package/declarations/adapter/-private/build-url-mixin.d.ts.map +0 -1
  90. package/declarations/adapter/-private/fastboot-interface.d.ts.map +0 -1
  91. package/declarations/adapter/-private/utils/continue-on-reject.d.ts.map +0 -1
  92. package/declarations/adapter/-private/utils/determine-body-promise.d.ts.map +0 -1
  93. package/declarations/adapter/-private/utils/fetch.d.ts.map +0 -1
  94. package/declarations/adapter/-private/utils/parse-response-headers.d.ts.map +0 -1
  95. package/declarations/adapter/-private/utils/serialize-into-hash.d.ts.map +0 -1
  96. package/declarations/adapter/-private/utils/serialize-query-params.d.ts.map +0 -1
  97. package/declarations/adapter/-private.d.ts.map +0 -1
  98. package/declarations/adapter/error.d.ts.map +0 -1
  99. package/declarations/adapter/json-api.d.ts.map +0 -1
  100. package/declarations/adapter/rest.d.ts.map +0 -1
  101. package/declarations/adapter.d.ts.map +0 -1
  102. package/declarations/compat/-private.d.ts.map +0 -1
  103. package/declarations/compat/builders/find-all.d.ts.map +0 -1
  104. package/declarations/compat/builders/find-record.d.ts.map +0 -1
  105. package/declarations/compat/builders/query.d.ts.map +0 -1
  106. package/declarations/compat/builders/save-record.d.ts.map +0 -1
  107. package/declarations/compat/builders/utils.d.ts.map +0 -1
  108. package/declarations/compat/builders.d.ts.map +0 -1
  109. package/declarations/compat/extensions.d.ts.map +0 -1
  110. package/declarations/compat/legacy-network-handler/fetch-manager.d.ts.map +0 -1
  111. package/declarations/compat/legacy-network-handler/identifier-has-id.d.ts.map +0 -1
  112. package/declarations/compat/legacy-network-handler/legacy-data-fetch.d.ts.map +0 -1
  113. package/declarations/compat/legacy-network-handler/legacy-data-utils.d.ts.map +0 -1
  114. package/declarations/compat/legacy-network-handler/legacy-network-handler.d.ts.map +0 -1
  115. package/declarations/compat/legacy-network-handler/minimum-adapter-interface.d.ts.map +0 -1
  116. package/declarations/compat/legacy-network-handler/minimum-serializer-interface.d.ts.map +0 -1
  117. package/declarations/compat/legacy-network-handler/serializer-response.d.ts.map +0 -1
  118. package/declarations/compat/legacy-network-handler/snapshot-record-array.d.ts.map +0 -1
  119. package/declarations/compat/legacy-network-handler/snapshot.d.ts.map +0 -1
  120. package/declarations/compat/utils.d.ts.map +0 -1
  121. package/declarations/compat.d.ts.map +0 -1
  122. package/declarations/model/-private/attr.d.ts.map +0 -1
  123. package/declarations/model/-private/attr.type-test.d.ts +0 -2
  124. package/declarations/model/-private/attr.type-test.d.ts.map +0 -1
  125. package/declarations/model/-private/belongs-to.d.ts.map +0 -1
  126. package/declarations/model/-private/belongs-to.type-test.d.ts +0 -2
  127. package/declarations/model/-private/belongs-to.type-test.d.ts.map +0 -1
  128. package/declarations/model/-private/debug/assert-polymorphic-type.d.ts.map +0 -1
  129. package/declarations/model/-private/errors.d.ts.map +0 -1
  130. package/declarations/model/-private/has-many.d.ts.map +0 -1
  131. package/declarations/model/-private/has-many.type-test.d.ts +0 -2
  132. package/declarations/model/-private/has-many.type-test.d.ts.map +0 -1
  133. package/declarations/model/-private/hooks.d.ts.map +0 -1
  134. package/declarations/model/-private/legacy-relationships-support.d.ts.map +0 -1
  135. package/declarations/model/-private/model-for-mixin.d.ts.map +0 -1
  136. package/declarations/model/-private/model-methods.d.ts.map +0 -1
  137. package/declarations/model/-private/model.d.ts.map +0 -1
  138. package/declarations/model/-private/model.type-test.d.ts +0 -2
  139. package/declarations/model/-private/model.type-test.d.ts.map +0 -1
  140. package/declarations/model/-private/notify-changes.d.ts.map +0 -1
  141. package/declarations/model/-private/promise-belongs-to.d.ts.map +0 -1
  142. package/declarations/model/-private/promise-many-array.d.ts.map +0 -1
  143. package/declarations/model/-private/promise-proxy-base.d.ts +0 -3
  144. package/declarations/model/-private/promise-proxy-base.d.ts.map +0 -1
  145. package/declarations/model/-private/record-state.d.ts.map +0 -1
  146. package/declarations/model/-private/references/belongs-to.d.ts.map +0 -1
  147. package/declarations/model/-private/references/has-many.d.ts.map +0 -1
  148. package/declarations/model/-private/schema-provider.d.ts.map +0 -1
  149. package/declarations/model/-private/type-utils.d.ts.map +0 -1
  150. package/declarations/model/-private/util.d.ts.map +0 -1
  151. package/declarations/model/-private.d.ts.map +0 -1
  152. package/declarations/model/migration-support.d.ts.map +0 -1
  153. package/declarations/model/migration-support.type-test.d.ts +0 -2
  154. package/declarations/model/migration-support.type-test.d.ts.map +0 -1
  155. package/declarations/model.d.ts.map +0 -1
  156. package/declarations/serializer/-private/embedded-records-mixin.d.ts.map +0 -1
  157. package/declarations/serializer/-private/transforms/boolean.d.ts.map +0 -1
  158. package/declarations/serializer/-private/transforms/boolean.type-test.d.ts +0 -2
  159. package/declarations/serializer/-private/transforms/boolean.type-test.d.ts.map +0 -1
  160. package/declarations/serializer/-private/transforms/date.d.ts.map +0 -1
  161. package/declarations/serializer/-private/transforms/number.d.ts.map +0 -1
  162. package/declarations/serializer/-private/transforms/string.d.ts.map +0 -1
  163. package/declarations/serializer/-private/transforms/transform.d.ts.map +0 -1
  164. package/declarations/serializer/-private/utils.d.ts.map +0 -1
  165. package/declarations/serializer/json-api.d.ts.map +0 -1
  166. package/declarations/serializer/json.d.ts.map +0 -1
  167. package/declarations/serializer/rest.d.ts.map +0 -1
  168. package/declarations/serializer/transform.d.ts.map +0 -1
  169. package/declarations/serializer.d.ts.map +0 -1
  170. package/dist/-private-DFfBszo5.js.map +0 -1
  171. package/dist/adapter/-private.js.map +0 -1
  172. package/dist/adapter/error.js.map +0 -1
  173. package/dist/adapter/json-api.js.map +0 -1
  174. package/dist/adapter/rest.js.map +0 -1
  175. package/dist/adapter.js.map +0 -1
  176. package/dist/compat/-private.js.map +0 -1
  177. package/dist/compat/builders.js.map +0 -1
  178. package/dist/compat/extensions.js.map +0 -1
  179. package/dist/compat/utils.js.map +0 -1
  180. package/dist/compat.js.map +0 -1
  181. package/dist/errors-_QQ7xpSn.js.map +0 -1
  182. package/dist/json-DSOlH9A8.js.map +0 -1
  183. package/dist/model/-private.js.map +0 -1
  184. package/dist/model/migration-support.js.map +0 -1
  185. package/dist/model.js.map +0 -1
  186. package/dist/runtime-BPCpkOf1-BKOwiRJp.js.map +0 -1
  187. package/dist/schema-provider-D_P8ReX6.js.map +0 -1
  188. package/dist/serialize-into-hash-BxfqWC8u.js.map +0 -1
  189. package/dist/serializer/json-api.js.map +0 -1
  190. package/dist/serializer/json.js.map +0 -1
  191. package/dist/serializer/rest.js.map +0 -1
  192. package/dist/serializer/transform.js.map +0 -1
  193. package/dist/serializer.js.map +0 -1
@@ -1,823 +1,820 @@
1
- import type { Store } from '@warp-drive/core';
2
- import type { ModelSchema } from '@warp-drive/core/types';
3
- import type { HTTPMethod } from '@warp-drive/core/types/request';
4
- import { Adapter } from '../adapter.ts';
5
- import type { AdapterPayload } from '../compat.ts';
6
- import type { Snapshot, SnapshotRecordArray } from '../compat/-private.ts';
7
- import type { MixtBuildURLMixin } from './-private/build-url-mixin.ts';
8
- import type { FastBoot } from './-private/fastboot-interface.ts';
9
- import { AdapterError } from './error.js';
1
+ import type Mixin from "@ember/object/mixin";
2
+ import type Owner from "@ember/owner";
3
+ import type { Store } from "@warp-drive/core";
4
+ import type { ModelSchema } from "@warp-drive/core/types";
5
+ import type { HTTPMethod } from "@warp-drive/core/types/request";
6
+ import { Adapter } from "../adapter.js";
7
+ import type { AdapterPayload } from "../compat.js";
8
+ import type { Snapshot, SnapshotRecordArray } from "../compat/-private.js";
9
+ import type { MixtBuildURLMixin } from "./-private/build-url-mixin.js";
10
+ import type { FastBoot } from "./-private/fastboot-interface.js";
11
+ import { AdapterError } from "./error.js";
10
12
  type Payload = Error | Record<string, unknown> | unknown[] | string | undefined;
11
13
  export type QueryState = {
12
- include?: unknown;
13
- since?: unknown;
14
+ include?: unknown;
15
+ since?: unknown;
14
16
  };
15
17
  export interface FetchRequestInit extends RequestInit {
16
- url: string;
17
- method: HTTPMethod;
18
- type: HTTPMethod;
18
+ url: string;
19
+ method: HTTPMethod;
20
+ type: HTTPMethod;
19
21
  }
20
22
  export interface JQueryRequestInit extends JQueryAjaxSettings {
21
- url: string;
22
- method: HTTPMethod;
23
- type: HTTPMethod;
23
+ url: string;
24
+ method: HTTPMethod;
25
+ type: HTTPMethod;
24
26
  }
25
27
  export type RequestData = {
26
- url: string;
27
- method: HTTPMethod;
28
- [key: string]: unknown;
28
+ url: string;
29
+ method: HTTPMethod;
30
+ [key: string]: unknown;
29
31
  };
30
- declare const RESTAdapter_base: Readonly<typeof Adapter> & (new (owner?: import("@ember/-internals/owner").default) => Adapter) & import("@ember/object/mixin").default;
32
+ declare const AdapterWithBuildURLMixin: Readonly<typeof Adapter> & (new (owner?: Owner) => Adapter) & Mixin;
31
33
  /**
32
- * <blockquote style="margin: 1em; padding: .1em 1em .1em 1em; border-left: solid 1em #E34C32; background: #e0e0e0;">
33
- <p>
34
- ⚠️ <strong>This is LEGACY documentation</strong> for a feature that is no longer encouraged to be used.
35
- If starting a new app or thinking of implementing a new adapter, consider writing a
36
- <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>
37
- </p>
38
- </blockquote>
39
-
40
- The REST adapter allows your store to communicate with an HTTP server by
41
- transmitting JSON via XHR.
42
-
43
- This adapter is designed around the idea that the JSON exchanged with
44
- the server should be conventional. It builds URLs in a manner that follows
45
- the structure of most common REST-style web services.
46
-
47
- ## Success and failure
48
-
49
- The REST adapter will consider a success any response with a status code
50
- of the 2xx family ("Success"), as well as 304 ("Not Modified"). Any other
51
- status code will be considered a failure.
52
-
53
- On success, the request promise will be resolved with the full response
54
- payload.
55
-
56
- Failed responses with status code 422 ("Unprocessable Entity") will be
57
- considered "invalid". The response will be discarded, except for the
58
- `errors` key. The request promise will be rejected with a `InvalidError`.
59
- This error object will encapsulate the saved `errors` value.
60
-
61
- Any other status codes will be treated as an "adapter error". The request
62
- promise will be rejected, similarly to the "invalid" case, but with
63
- an instance of `AdapterError` instead.
64
-
65
- ## JSON Structure
66
-
67
- The REST adapter expects the JSON returned from your server to follow
68
- these conventions.
69
-
70
- ### Object Root
71
-
72
- The JSON payload should be an object that contains the record inside a
73
- root property. For example, in response to a `GET` request for
74
- `/posts/1`, the JSON should look like this:
75
-
76
- ```js
77
- {
78
- "posts": {
79
- "id": 1,
80
- "title": "I'm Running to Reform the W3C",
81
- "author": "Yehuda Katz"
82
- }
83
- }
84
- ```
85
-
86
- Similarly, in response to a `GET` request for `/posts`, the JSON should
87
- look like this:
88
-
89
- ```js
90
- {
91
- "posts": [
92
- {
93
- "id": 1,
94
- "title": "I'm Running to Reform the W3C",
95
- "author": "Yehuda Katz"
96
- },
97
- {
98
- "id": 2,
99
- "title": "Rails is omakase",
100
- "author": "D2H"
101
- }
102
- ]
103
- }
104
- ```
105
-
106
- Note that the object root can be pluralized for both a single-object response
107
- and an array response: the REST adapter is not strict on this. Further, if the
108
- HTTP server responds to a `GET` request to `/posts/1` (e.g. the response to a
109
- `findRecord` query) with more than one object in the array, Ember Data will
110
- only display the object with the matching ID.
111
-
112
- ### Conventional Names
113
-
114
- Attribute names in your JSON payload should be the camelCased versions of
115
- the attributes in your Ember.js models.
116
-
117
- For example, if you have a `Person` model:
118
-
119
- ```js [app/models/person.js]
120
- import { Model, attr } from '@warp-drive/legacy/model';
121
-
122
- export default Model.extend({
123
- firstName: attr('string'),
124
- lastName: attr('string'),
125
- occupation: attr('string')
126
- });
127
- ```
128
-
129
- The JSON returned should look like this:
130
-
131
- ```js
132
- {
133
- "people": {
134
- "id": 5,
135
- "firstName": "Zaphod",
136
- "lastName": "Beeblebrox",
137
- "occupation": "President"
138
- }
139
- }
140
- ```
141
-
142
- #### Relationships
143
-
144
- Relationships are usually represented by ids to the record in the
145
- relationship. The related records can then be sideloaded in the
146
- response under a key for the type.
147
-
148
- ```js
149
- {
150
- "posts": {
151
- "id": 5,
152
- "title": "I'm Running to Reform the W3C",
153
- "author": "Yehuda Katz",
154
- "comments": [1, 2]
155
- },
156
- "comments": [{
157
- "id": 1,
158
- "author": "User 1",
159
- "message": "First!",
160
- }, {
161
- "id": 2,
162
- "author": "User 2",
163
- "message": "Good Luck!",
164
- }]
165
- }
166
- ```
167
-
168
- If the records in the relationship are not known when the response
169
- is serialized it's also possible to represent the relationship as a
170
- URL using the `links` key in the response. Ember Data will fetch
171
- this URL to resolve the relationship when it is accessed for the
172
- first time.
173
-
174
- ```js
175
- {
176
- "posts": {
177
- "id": 5,
178
- "title": "I'm Running to Reform the W3C",
179
- "author": "Yehuda Katz",
180
- "links": {
181
- "comments": "/posts/5/comments"
182
- }
183
- }
184
- }
185
- ```
186
-
187
- ### Errors
188
-
189
- If a response is considered a failure, the JSON payload is expected to include
190
- a top-level key `errors`, detailing any specific issues. For example:
191
-
192
- ```js
193
- {
194
- "errors": {
195
- "msg": "Something went wrong"
196
- }
197
- }
198
- ```
199
-
200
- This adapter does not make any assumptions as to the format of the `errors`
201
- object. It will simply be passed along as is, wrapped in an instance
202
- of `InvalidError` or `AdapterError`. The serializer can interpret it
203
- afterwards.
204
-
205
- ## Customization
206
-
207
- ### Endpoint path customization
208
-
209
- Endpoint paths can be prefixed with a `namespace` by setting the namespace
210
- property on the adapter:
211
-
212
- ```js [app/adapters/application.js]
213
- import { RESTAdapter } from '@warp-drive/legacy/adapter/rest';
214
-
215
- export default class ApplicationAdapter extends RESTAdapter {
216
- namespace = 'api/1';
217
- }
218
- ```
219
- Requests for the `Person` model would now target `/api/1/people/1`.
220
-
221
- ### Host customization
222
-
223
- An adapter can target other hosts by setting the `host` property.
224
-
225
- ```js [app/adapters/application.js]
226
- import { RESTAdapter } from '@warp-drive/legacy/adapter/rest';
227
-
228
- export default class ApplicationAdapter extends RESTAdapter {
229
- host = 'https://api.example.com';
230
- }
231
- ```
232
-
233
- ### Headers customization
234
-
235
- Some APIs require HTTP headers, e.g. to provide an API key. Arbitrary
236
- headers can be set as key/value pairs on the `RESTAdapter`'s `headers`
237
- object and WarpDrive will send them along with each ajax request.
238
-
239
-
240
- ```js [app/adapters/application.js]
241
- import { RESTAdapter } from '@warp-drive/legacy/adapter/rest';
242
-
243
- export default class ApplicationAdapter extends RESTAdapter {
244
- get headers() {
245
- return {
246
- 'API_KEY': 'secret key',
247
- 'ANOTHER_HEADER': 'Some header value'
248
- };
249
- }
250
- }
251
- ```
252
-
253
- @class RESTAdapter
254
- @public
255
- @constructor
256
- @uses BuildURLMixin
257
- */
258
- declare class RESTAdapter extends RESTAdapter_base {
259
- _fastboot: FastBoot;
260
- _coalesceFindRequests: boolean;
261
- host: string | null;
262
- namespace: string | null;
263
- /**
264
- This property allows ajax to still be used instead when `false`.
265
-
266
- @property useFetch
267
- @type {Boolean}
268
- @default true
269
- @public
270
- */
271
- useFetch: boolean;
272
- _defaultContentType: string;
273
- get fastboot(): FastBoot;
274
- set fastboot(value: FastBoot);
275
- /**
276
- By default, the RESTAdapter will send the query params sorted alphabetically to the
277
- server.
278
-
279
- For example:
280
-
281
- ```js
282
- store.query('posts', { sort: 'price', category: 'pets' });
283
- ```
284
-
285
- will generate a requests like this `/posts?category=pets&sort=price`, even if the
286
- parameters were specified in a different order.
287
-
288
- That way the generated URL will be deterministic and that simplifies caching mechanisms
289
- in the backend.
290
-
291
- Setting `sortQueryParams` to a falsey value will respect the original order.
292
-
293
- In case you want to sort the query parameters with a different criteria, set
294
- `sortQueryParams` to your custom sort function.
295
-
296
- ```js [app/adapters/application.js]
297
- import { RESTAdapter } from '@warp-drive/legacy/adapter/rest';
298
-
299
- export default class ApplicationAdapter extends RESTAdapter {
300
- sortQueryParams(params) {
301
- let sortedKeys = Object.keys(params).sort().reverse();
302
- let len = sortedKeys.length, newParams = {};
303
-
304
- for (let i = 0; i < len; i++) {
305
- newParams[sortedKeys[i]] = params[sortedKeys[i]];
306
- }
307
-
308
- return newParams;
309
- }
310
- }
311
- ```
312
-
313
- @param {Object} obj
314
- @return {Object}
315
- @public
316
- */
317
- sortQueryParams(obj: Record<string, unknown>): Record<string, unknown>;
318
- /**
319
- By default the RESTAdapter will send each find request coming from a `store.find`
320
- or from accessing a relationship separately to the server. If your server supports passing
321
- ids as a query string, you can set coalesceFindRequests to true to coalesce all find requests
322
- within a single runloop.
323
-
324
- For example, if you have an initial payload of:
325
-
326
- ```javascript
327
- {
328
- post: {
329
- id: 1,
330
- comments: [1, 2]
331
- }
332
- }
333
- ```
334
-
335
- By default calling `post.comments` will trigger the following requests(assuming the
336
- comments haven't been loaded before):
337
-
338
- ```
339
- GET /comments/1
340
- GET /comments/2
341
- ```
342
-
343
- If you set coalesceFindRequests to `true` it will instead trigger the following request:
344
-
345
- ```
346
- GET /comments?ids[]=1&ids[]=2
347
- ```
348
-
349
- Setting coalesceFindRequests to `true` also works for `store.find` requests and `belongsTo`
350
- relationships accessed within the same runloop. If you set `coalesceFindRequests: true`
351
-
352
- ```javascript
353
- store.findRecord('comment', 1);
354
- store.findRecord('comment', 2);
355
- ```
356
-
357
- will also send a request to: `GET /comments?ids[]=1&ids[]=2`
358
-
359
- Note: Requests coalescing rely on URL building strategy. So if you override `buildURL` in your app
360
- `groupRecordsForFindMany` more likely should be overridden as well in order for coalescing to work.
361
-
362
- @property coalesceFindRequests
363
- @public
364
- @type {Boolean}
365
- */
366
- get coalesceFindRequests(): boolean;
367
- set coalesceFindRequests(value: boolean);
368
- /**
369
- Endpoint paths can be prefixed with a `namespace` by setting the namespace
370
- property on the adapter:
371
-
372
- ```js [app/adapters/application.js]
373
- import { RESTAdapter } from '@warp-drive/legacy/adapter/rest';
374
-
375
- export default class ApplicationAdapter extends RESTAdapter {
376
- namespace = 'api/1';
377
- }
378
- ```
379
-
380
- Requests for the `Post` model would now target `/api/1/post/`.
381
-
382
- @property namespace
383
- @public
384
- @type {String}
385
- */
386
- /**
387
- An adapter can target other hosts by setting the `host` property.
388
-
389
- ```js [app/adapters/application.js]
390
- import { RESTAdapter } from '@warp-drive/legacy/adapter/rest';
391
-
392
- export default class ApplicationAdapter extends RESTAdapter {
393
- host = 'https://api.example.com';
394
- }
395
- ```
396
-
397
- Requests for the `Post` model would now target `https://api.example.com/post/`.
398
-
399
- @property host
400
- @public
401
- @type {String}
402
- */
403
- /**
404
- Some APIs require HTTP headers, e.g. to provide an API
405
- key. Arbitrary headers can be set as key/value pairs on the
406
- `RESTAdapter`'s `headers` object and Ember Data will send them
407
- along with each ajax request. For dynamic headers see [headers
408
- customization](/ember-data/release/classes/RESTAdapter).
409
-
410
- ```js [app/adapters/application.js]
411
- import { RESTAdapter } from '@warp-drive/legacy/adapter/rest';
412
-
413
- export default class ApplicationAdapter extends RESTAdapter {
414
- get headers() {
415
- return {
416
- 'API_KEY': 'secret key',
417
- 'ANOTHER_HEADER': 'Some header value'
418
- };
419
- }
420
- }
421
- ```
422
-
423
- @property headers
424
- @public
425
- @type {Object}
426
- */
427
- headers: Record<string, unknown> | undefined;
428
- /**
429
- Called by the store in order to fetch the JSON for a given
430
- type and ID.
431
-
432
- The `findRecord` method makes an Ajax request to a URL computed by
433
- `buildURL`, and returns a promise for the resulting payload.
434
-
435
- This method performs an HTTP `GET` request with the id provided as part of the query string.
436
-
437
- @since 1.13.0
438
- @public
439
- @param {Store} store
440
- @param {Model} type
441
- @param {String} id
442
- @param {Snapshot} snapshot
443
- @return {Promise} promise
444
- */
445
- findRecord(store: Store, type: ModelSchema, id: string, snapshot: Snapshot): Promise<AdapterPayload>;
446
- /**
447
- Called by the store in order to fetch a JSON array for all
448
- of the records for a given type.
449
-
450
- The `findAll` method makes an Ajax (HTTP GET) request to a URL computed by `buildURL`, and returns a
451
- promise for the resulting payload.
452
-
453
- @public
454
- @param {Store} store
455
- @param {Model} type
456
- @param {undefined} neverSet a value is never provided to this argument
457
- @param {SnapshotRecordArray} snapshotRecordArray
458
- @return {Promise} promise
459
- */
460
- findAll(store: Store, type: ModelSchema, sinceToken: null, snapshotRecordArray: SnapshotRecordArray): Promise<AdapterPayload>;
461
- /**
462
- Called by the store in order to fetch a JSON array for
463
- the records that match a particular query.
464
-
465
- The `query` method makes an Ajax (HTTP GET) request to a URL
466
- computed by `buildURL`, and returns a promise for the resulting
467
- payload.
468
-
469
- The `query` argument is a simple JavaScript object that will be passed directly
470
- to the server as parameters.
471
-
472
- @public
473
- @param {Store} store
474
- @param {Model} type
475
- @param {Object} query
476
- @param {Collection} recordArray
477
- @param {Object} adapterOptions
478
- @return {Promise} promise
479
- */
480
- query(store: Store, type: ModelSchema, query: Record<string, unknown>): Promise<AdapterPayload>;
481
- /**
482
- Called by the store in order to fetch a JSON object for
483
- the record that matches a particular query.
484
-
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
-
489
- The `query` argument is a simple JavaScript object that will be passed directly
490
- to the server as parameters.
491
-
492
- @since 1.13.0
493
- @public
494
- @param {Store} store
495
- @param {Model} type
496
- @param {Object} query
497
- @param {Object} adapterOptions
498
- @return {Promise} promise
499
- */
500
- queryRecord(store: Store, type: ModelSchema, query: Record<string, unknown>, adapterOptions: Record<string, unknown>): Promise<AdapterPayload>;
501
- /**
502
- Called by the store in order to fetch several records together if `coalesceFindRequests` is true
503
-
504
- For example, if the original payload looks like:
505
-
506
- ```js
507
- {
508
- "id": 1,
509
- "title": "Rails is omakase",
510
- "comments": [ 1, 2, 3 ]
511
- }
512
- ```
513
-
514
- The IDs will be passed as a URL-encoded Array of IDs, in this form:
515
-
516
- ```
517
- ids[]=1&ids[]=2&ids[]=3
518
- ```
519
-
520
- Many servers, such as Rails and PHP, will automatically convert this URL-encoded array
521
- into an Array for you on the server-side. If you want to encode the
522
- IDs, differently, just override this (one-line) method.
523
-
524
- The `findMany` method makes an Ajax (HTTP GET) request to a URL computed by `buildURL`, and returns a
525
- promise for the resulting payload.
526
-
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: Store, type: ModelSchema, ids: string[], snapshots: Snapshot[]): Promise<AdapterPayload>;
535
- /**
536
- Called by the store in order to fetch a JSON array for
537
- the unloaded records in a has-many relationship that were originally
538
- specified as a URL (inside of `links`).
539
-
540
- For example, if your original payload looks like this:
541
-
542
- ```js
543
- {
544
- "post": {
545
- "id": 1,
546
- "title": "Rails is omakase",
547
- "links": { "comments": "/posts/1/comments" }
548
- }
549
- }
550
- ```
551
-
552
- This method will be called with the parent record and `/posts/1/comments`.
553
-
554
- The `findHasMany` method will make an Ajax (HTTP GET) request to the originally specified URL.
555
-
556
- The format of your `links` value will influence the final request URL via the `urlPrefix` method:
557
-
558
- * Links beginning with `//`, `http://`, `https://`, will be used as is, with no further manipulation.
559
-
560
- * Links beginning with a single `/` will have the current adapter's `host` value prepended to it.
561
-
562
- * Links with no beginning `/` will have a parentURL prepended to it, via the current adapter's `buildURL`.
563
-
564
- @public
565
- @param {Store} store
566
- @param {Snapshot} snapshot
567
- @param {String} url
568
- @param {Object} relationship meta object describing the relationship
569
- @return {Promise} promise
570
- */
571
- findHasMany(store: Store, snapshot: Snapshot, url: string, relationship: Record<string, unknown>): Promise<AdapterPayload>;
572
- /**
573
- Called by the store in order to fetch the JSON for the unloaded record in a
574
- belongs-to relationship that was originally specified as a URL (inside of
575
- `links`).
576
-
577
- For example, if your original payload looks like this:
578
-
579
- ```js
580
- {
581
- "person": {
582
- "id": 1,
583
- "name": "Tom Dale",
584
- "links": { "group": "/people/1/group" }
585
- }
586
- }
587
- ```
588
-
589
- This method will be called with the parent record and `/people/1/group`.
590
-
591
- The `findBelongsTo` method will make an Ajax (HTTP GET) request to the originally specified URL.
592
-
593
- The format of your `links` value will influence the final request URL via the `urlPrefix` method:
594
-
595
- * Links beginning with `//`, `http://`, `https://`, will be used as is, with no further manipulation.
596
-
597
- * Links beginning with a single `/` will have the current adapter's `host` value prepended to it.
598
-
599
- * Links with no beginning `/` will have a parentURL prepended to it, via the current adapter's `buildURL`.
600
-
601
- @public
602
- @param {Store} store
603
- @param {Snapshot} snapshot
604
- @param {String} url
605
- @param {Object} relationship meta object describing the relationship
606
- @return {Promise} promise
607
- */
608
- findBelongsTo(store: Store, snapshot: Snapshot, url: string, relationship: any): Promise<AdapterPayload>;
609
- /**
610
- Called by the store when a newly created record is
611
- saved via the `save` method on a model record instance.
612
-
613
- The `createRecord` method serializes the record and makes an Ajax (HTTP POST) request
614
- to a URL computed by `buildURL`.
615
-
616
- See `serialize` for information on how to customize the serialized form
617
- of a record.
618
-
619
- @public
620
- @param {Store} store
621
- @param {Model} type
622
- @param {Snapshot} snapshot
623
- @return {Promise} promise
624
- */
625
- createRecord(store: Store, type: ModelSchema, snapshot: Snapshot): Promise<AdapterPayload>;
626
- /**
627
- Called by the store when an existing record is saved
628
- via the `save` method on a model record instance.
629
-
630
- The `updateRecord` method serializes the record and makes an Ajax (HTTP PUT) request
631
- to a URL computed by `buildURL`.
632
-
633
- See `serialize` for information on how to customize the serialized form
634
- of a record.
635
-
636
- @public
637
- @param {Store} store
638
- @param {Model} schema
639
- @param {Snapshot} snapshot
640
- @return {Promise} promise
641
- */
642
- updateRecord(store: Store, schema: ModelSchema, snapshot: Snapshot): Promise<AdapterPayload>;
643
- /**
644
- Called by the store when a record is deleted.
645
-
646
- The `deleteRecord` method makes an Ajax (HTTP DELETE) request to a URL computed by `buildURL`.
647
-
648
- @public
649
- @param {Store} store
650
- @param {Model} type
651
- @param {Snapshot} snapshot
652
- @return {Promise} promise
653
- */
654
- deleteRecord(store: Store, schema: ModelSchema, snapshot: Snapshot): Promise<AdapterPayload>;
655
- _stripIDFromURL(store: Store, snapshot: Snapshot): string;
656
- maxURLLength: number;
657
- /**
658
- Organize records into groups, each of which is to be passed to separate
659
- calls to `findMany`.
660
-
661
- This implementation groups together records that have the same base URL but
662
- differing ids. For example `/comments/1` and `/comments/2` will be grouped together
663
- because we know findMany can coalesce them together as `/comments?ids[]=1&ids[]=2`
664
-
665
- It also supports urls where ids are passed as a query param, such as `/comments?id=1`
666
- but not those where there is more than 1 query param such as `/comments?id=2&name=David`
667
- Currently only the query param of `id` is supported. If you need to support others, please
668
- override this or the `_stripIDFromURL` method.
669
-
670
- It does not group records that have differing base urls, such as for example: `/posts/1/comments/2`
671
- and `/posts/2/comments/3`
672
-
673
- @public
674
- @param {Store} store
675
- @param {Array} snapshots
676
- @return {Array} an array of arrays of records, each of which is to be
677
- loaded separately by `findMany`.
678
- */
679
- groupRecordsForFindMany(store: Store, snapshots: Snapshot[]): Snapshot[][];
680
- /**
681
- Takes an ajax response, and returns the json payload or an error.
682
-
683
- By default this hook just returns the json payload passed to it.
684
- You might want to override it in two cases:
685
-
686
- 1. Your API might return useful results in the response headers.
687
- Response headers are passed in as the second argument.
688
-
689
- 2. Your API might return errors as successful responses with status code
690
- 200 and an Errors text or object. You can return a `InvalidError` or a
691
- `AdapterError` (or a sub class) from this hook and it will automatically
692
- reject the promise and put your record into the invalid or error state.
693
-
694
- Returning a `InvalidError` from this method will cause the
695
- record to transition into the `invalid` state and make the
696
- `errors` object available on the record. When returning an
697
- `InvalidError` the store will attempt to normalize the error data
698
- returned from the server using the serializer's `extractErrors`
699
- method.
700
-
701
- @since 1.13.0
702
- @public
703
- @param {Number} status
704
- @param {Object} headers
705
- @param {Object} payload
706
- @param {Object} requestData - the original request information
707
- @return {Object | AdapterError} response
708
- */
709
- handleResponse(status: number, headers: Record<string, string>, payload: Payload, requestData: RequestData): Payload | typeof AdapterError;
710
- /**
711
- Default `handleResponse` implementation uses this hook to decide if the
712
- response is a success.
713
-
714
- @since 1.13.0
715
- @public
716
- @param {Number} status
717
- @param {Object} headers
718
- @param {Object} payload
719
- @return {Boolean}
720
- */
721
- isSuccess(status: number, _headers: Record<string, unknown>, _payload: Payload): boolean;
722
- /**
723
- Default `handleResponse` implementation uses this hook to decide if the
724
- response is an invalid error.
725
-
726
- @since 1.13.0
727
- @public
728
- @param {Number} status
729
- @param {Object} headers
730
- @param {Object} payload
731
- @return {Boolean}
732
- */
733
- isInvalid(status: number, _headers: Record<string, unknown>, _payload: Payload): boolean;
734
- /**
735
- Takes a URL, an HTTP method and a hash of data, and makes an
736
- HTTP request.
737
-
738
- When the server responds with a payload, Ember Data will call into `extractSingle`
739
- or `extractArray` (depending on whether the original query was for one record or
740
- many records).
741
-
742
- By default, `ajax` method has the following behavior:
743
-
744
- * It sets the response `dataType` to `"json"`
745
- * If the HTTP method is not `"GET"`, it sets the `Content-Type` to be
746
- `application/json; charset=utf-8`
747
- * If the HTTP method is not `"GET"`, it stringifies the data passed in. The
748
- data is the serialized record in the case of a save.
749
- * Registers success and failure handlers.
750
-
751
- @private
752
- @param {String} url
753
- @param {String} type The request type GET, POST, PUT, DELETE etc.
754
- @param {Object} options
755
- @return {Promise} promise
756
- */
757
- ajax(url: string, type: HTTPMethod, options?: JQueryAjaxSettings | RequestInit): Promise<AdapterPayload>;
758
- /**
759
- @private
760
- @param {Object} options jQuery ajax options to be used for the ajax request
761
- */
762
- _ajaxRequest(options: JQueryRequestInit): void;
763
- _fetchRequest(options: FetchRequestInit): Promise<Response>;
764
- _ajax(options: FetchRequestInit | JQueryRequestInit): void;
765
- /**
766
- @private
767
- @param {String} url
768
- @param {String} type The request type GET, POST, PUT, DELETE etc.
769
- @param {Object} options
770
- @return {Object}
771
- */
772
- ajaxOptions(url: string, method: HTTPMethod, options: JQueryAjaxSettings | RequestInit): JQueryRequestInit | FetchRequestInit;
773
- _ajaxURL(url: string): string;
774
- /**
775
- @private
776
- @param {String} responseText
777
- @return {Object}
778
- */
779
- parseErrorResponse(responseText: string): Record<string, unknown> | string;
780
- /**
781
- @private
782
- @param {Number} status
783
- @param {Object} headers
784
- @param {Object} payload
785
- @return {Array} errors payload
786
- */
787
- normalizeErrorResponse(status: number, _headers: Record<string, unknown>, payload: Payload): Record<string, unknown>[];
788
- /**
789
- Generates a detailed ("friendly") error message, with plenty
790
- of information for debugging (good luck!)
791
-
792
- @private
793
- @param {Number} status
794
- @param {Object} headers
795
- @param {Object} payload
796
- @param {Object} requestData
797
- @return {String} detailed error message
798
- */
799
- generatedDetailedMessage(status: number, headers: Record<string, string>, payload: Payload, requestData: RequestData): string;
800
- /**
801
- Used by `findAll` and `findRecord` to build the query's `data` hash
802
- supplied to the ajax method.
803
-
804
- @since 2.5.0
805
- @public
806
- @param {Snapshot} snapshot
807
- @return {Object}
808
- */
809
- buildQuery(snapshot: Snapshot | SnapshotRecordArray): QueryState;
34
+ * <blockquote style="margin: 1em; padding: .1em 1em .1em 1em; border-left: solid 1em #E34C32; background: #e0e0e0;">
35
+ <p>
36
+ ⚠️ <strong>This is LEGACY documentation</strong> for a feature that is no longer encouraged to be used.
37
+ If starting a new app or thinking of implementing a new adapter, consider writing a
38
+ <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>
39
+ </p>
40
+ </blockquote>
41
+
42
+ The REST adapter allows your store to communicate with an HTTP server by
43
+ transmitting JSON via XHR.
44
+
45
+ This adapter is designed around the idea that the JSON exchanged with
46
+ the server should be conventional. It builds URLs in a manner that follows
47
+ the structure of most common REST-style web services.
48
+
49
+ ## Success and failure
50
+
51
+ The REST adapter will consider a success any response with a status code
52
+ of the 2xx family ("Success"), as well as 304 ("Not Modified"). Any other
53
+ status code will be considered a failure.
54
+
55
+ On success, the request promise will be resolved with the full response
56
+ payload.
57
+
58
+ Failed responses with status code 422 ("Unprocessable Entity") will be
59
+ considered "invalid". The response will be discarded, except for the
60
+ `errors` key. The request promise will be rejected with a `InvalidError`.
61
+ This error object will encapsulate the saved `errors` value.
62
+
63
+ Any other status codes will be treated as an "adapter error". The request
64
+ promise will be rejected, similarly to the "invalid" case, but with
65
+ an instance of `AdapterError` instead.
66
+
67
+ ## JSON Structure
68
+
69
+ The REST adapter expects the JSON returned from your server to follow
70
+ these conventions.
71
+
72
+ ### Object Root
73
+
74
+ The JSON payload should be an object that contains the record inside a
75
+ root property. For example, in response to a `GET` request for
76
+ `/posts/1`, the JSON should look like this:
77
+
78
+ ```js
79
+ {
80
+ "posts": {
81
+ "id": 1,
82
+ "title": "I'm Running to Reform the W3C",
83
+ "author": "Yehuda Katz"
84
+ }
85
+ }
86
+ ```
87
+
88
+ Similarly, in response to a `GET` request for `/posts`, the JSON should
89
+ look like this:
90
+
91
+ ```js
92
+ {
93
+ "posts": [
94
+ {
95
+ "id": 1,
96
+ "title": "I'm Running to Reform the W3C",
97
+ "author": "Yehuda Katz"
98
+ },
99
+ {
100
+ "id": 2,
101
+ "title": "Rails is omakase",
102
+ "author": "D2H"
103
+ }
104
+ ]
105
+ }
106
+ ```
107
+
108
+ Note that the object root can be pluralized for both a single-object response
109
+ and an array response: the REST adapter is not strict on this. Further, if the
110
+ HTTP server responds to a `GET` request to `/posts/1` (e.g. the response to a
111
+ `findRecord` query) with more than one object in the array, Ember Data will
112
+ only display the object with the matching ID.
113
+
114
+ ### Conventional Names
115
+
116
+ Attribute names in your JSON payload should be the camelCased versions of
117
+ the attributes in your Ember.js models.
118
+
119
+ For example, if you have a `Person` model:
120
+
121
+ ```js [app/models/person.js]
122
+ import { Model, attr } from '@warp-drive/legacy/model';
123
+
124
+ export default Model.extend({
125
+ firstName: attr('string'),
126
+ lastName: attr('string'),
127
+ occupation: attr('string')
128
+ });
129
+ ```
130
+
131
+ The JSON returned should look like this:
132
+
133
+ ```js
134
+ {
135
+ "people": {
136
+ "id": 5,
137
+ "firstName": "Zaphod",
138
+ "lastName": "Beeblebrox",
139
+ "occupation": "President"
140
+ }
141
+ }
142
+ ```
143
+
144
+ #### Relationships
145
+
146
+ Relationships are usually represented by ids to the record in the
147
+ relationship. The related records can then be sideloaded in the
148
+ response under a key for the type.
149
+
150
+ ```js
151
+ {
152
+ "posts": {
153
+ "id": 5,
154
+ "title": "I'm Running to Reform the W3C",
155
+ "author": "Yehuda Katz",
156
+ "comments": [1, 2]
157
+ },
158
+ "comments": [{
159
+ "id": 1,
160
+ "author": "User 1",
161
+ "message": "First!",
162
+ }, {
163
+ "id": 2,
164
+ "author": "User 2",
165
+ "message": "Good Luck!",
166
+ }]
167
+ }
168
+ ```
169
+
170
+ If the records in the relationship are not known when the response
171
+ is serialized it's also possible to represent the relationship as a
172
+ URL using the `links` key in the response. Ember Data will fetch
173
+ this URL to resolve the relationship when it is accessed for the
174
+ first time.
175
+
176
+ ```js
177
+ {
178
+ "posts": {
179
+ "id": 5,
180
+ "title": "I'm Running to Reform the W3C",
181
+ "author": "Yehuda Katz",
182
+ "links": {
183
+ "comments": "/posts/5/comments"
184
+ }
810
185
  }
811
- interface RESTAdapter extends MixtBuildURLMixin {
812
186
  }
187
+ ```
188
+
189
+ ### Errors
190
+
191
+ If a response is considered a failure, the JSON payload is expected to include
192
+ a top-level key `errors`, detailing any specific issues. For example:
193
+
194
+ ```js
195
+ {
196
+ "errors": {
197
+ "msg": "Something went wrong"
198
+ }
199
+ }
200
+ ```
201
+
202
+ This adapter does not make any assumptions as to the format of the `errors`
203
+ object. It will simply be passed along as is, wrapped in an instance
204
+ of `InvalidError` or `AdapterError`. The serializer can interpret it
205
+ afterwards.
206
+
207
+ ## Customization
208
+
209
+ ### Endpoint path customization
210
+
211
+ Endpoint paths can be prefixed with a `namespace` by setting the namespace
212
+ property on the adapter:
213
+
214
+ ```js [app/adapters/application.js]
215
+ import { RESTAdapter } from '@warp-drive/legacy/adapter/rest';
216
+
217
+ export default class ApplicationAdapter extends RESTAdapter {
218
+ namespace = 'api/1';
219
+ }
220
+ ```
221
+ Requests for the `Person` model would now target `/api/1/people/1`.
222
+
223
+ ### Host customization
224
+
225
+ An adapter can target other hosts by setting the `host` property.
226
+
227
+ ```js [app/adapters/application.js]
228
+ import { RESTAdapter } from '@warp-drive/legacy/adapter/rest';
229
+
230
+ export default class ApplicationAdapter extends RESTAdapter {
231
+ host = 'https://api.example.com';
232
+ }
233
+ ```
234
+
235
+ ### Headers customization
236
+
237
+ Some APIs require HTTP headers, e.g. to provide an API key. Arbitrary
238
+ headers can be set as key/value pairs on the `RESTAdapter`'s `headers`
239
+ object and WarpDrive will send them along with each ajax request.
240
+
241
+
242
+ ```js [app/adapters/application.js]
243
+ import { RESTAdapter } from '@warp-drive/legacy/adapter/rest';
244
+
245
+ export default class ApplicationAdapter extends RESTAdapter {
246
+ get headers() {
247
+ return {
248
+ 'API_KEY': 'secret key',
249
+ 'ANOTHER_HEADER': 'Some header value'
250
+ };
251
+ }
252
+ }
253
+ ```
254
+
255
+ @class RESTAdapter
256
+ @public
257
+ @constructor
258
+ @uses BuildURLMixin
259
+ */
260
+ declare class RESTAdapter extends AdapterWithBuildURLMixin {
261
+ _fastboot: FastBoot;
262
+ _coalesceFindRequests: boolean;
263
+ host: string | null;
264
+ namespace: string | null;
265
+ /**
266
+ This property allows ajax to still be used instead when `false`.
267
+
268
+ @property useFetch
269
+ @type {Boolean}
270
+ @default true
271
+ @public
272
+ */
273
+ useFetch: boolean;
274
+ _defaultContentType: string;
275
+ get fastboot(): FastBoot;
276
+ set fastboot(value: FastBoot);
277
+ /**
278
+ By default, the RESTAdapter will send the query params sorted alphabetically to the
279
+ server.
280
+
281
+ For example:
282
+
283
+ ```js
284
+ store.query('posts', { sort: 'price', category: 'pets' });
285
+ ```
286
+
287
+ will generate a requests like this `/posts?category=pets&sort=price`, even if the
288
+ parameters were specified in a different order.
289
+
290
+ That way the generated URL will be deterministic and that simplifies caching mechanisms
291
+ in the backend.
292
+
293
+ Setting `sortQueryParams` to a falsey value will respect the original order.
294
+
295
+ In case you want to sort the query parameters with a different criteria, set
296
+ `sortQueryParams` to your custom sort function.
297
+
298
+ ```js [app/adapters/application.js]
299
+ import { RESTAdapter } from '@warp-drive/legacy/adapter/rest';
300
+
301
+ export default class ApplicationAdapter extends RESTAdapter {
302
+ sortQueryParams(params) {
303
+ let sortedKeys = Object.keys(params).sort().reverse();
304
+ let len = sortedKeys.length, newParams = {};
305
+
306
+ for (let i = 0; i < len; i++) {
307
+ newParams[sortedKeys[i]] = params[sortedKeys[i]];
308
+ }
309
+
310
+ return newParams;
311
+ }
312
+ }
313
+ ```
314
+
315
+ @param {Object} obj
316
+ @return {Object}
317
+ @public
318
+ */
319
+ sortQueryParams(obj: Record<string, unknown>): Record<string, unknown>;
320
+ /**
321
+ By default the RESTAdapter will send each find request coming from a `store.find`
322
+ or from accessing a relationship separately to the server. If your server supports passing
323
+ ids as a query string, you can set coalesceFindRequests to true to coalesce all find requests
324
+ within a single runloop.
325
+
326
+ For example, if you have an initial payload of:
327
+
328
+ ```javascript
329
+ {
330
+ post: {
331
+ id: 1,
332
+ comments: [1, 2]
333
+ }
334
+ }
335
+ ```
336
+
337
+ By default calling `post.comments` will trigger the following requests(assuming the
338
+ comments haven't been loaded before):
339
+
340
+ ```
341
+ GET /comments/1
342
+ GET /comments/2
343
+ ```
344
+
345
+ If you set coalesceFindRequests to `true` it will instead trigger the following request:
346
+
347
+ ```
348
+ GET /comments?ids[]=1&ids[]=2
349
+ ```
350
+
351
+ Setting coalesceFindRequests to `true` also works for `store.find` requests and `belongsTo`
352
+ relationships accessed within the same runloop. If you set `coalesceFindRequests: true`
353
+
354
+ ```javascript
355
+ store.findRecord('comment', 1);
356
+ store.findRecord('comment', 2);
357
+ ```
358
+
359
+ will also send a request to: `GET /comments?ids[]=1&ids[]=2`
360
+
361
+ Note: Requests coalescing rely on URL building strategy. So if you override `buildURL` in your app
362
+ `groupRecordsForFindMany` more likely should be overridden as well in order for coalescing to work.
363
+
364
+ @property coalesceFindRequests
365
+ @public
366
+ @type {Boolean}
367
+ */
368
+ get coalesceFindRequests(): boolean;
369
+ set coalesceFindRequests(value: boolean);
370
+ /**
371
+ Endpoint paths can be prefixed with a `namespace` by setting the namespace
372
+ property on the adapter:
373
+
374
+ ```js [app/adapters/application.js]
375
+ import { RESTAdapter } from '@warp-drive/legacy/adapter/rest';
376
+
377
+ export default class ApplicationAdapter extends RESTAdapter {
378
+ namespace = 'api/1';
379
+ }
380
+ ```
381
+
382
+ Requests for the `Post` model would now target `/api/1/post/`.
383
+
384
+ @property namespace
385
+ @public
386
+ @type {String}
387
+ */
388
+ /**
389
+ An adapter can target other hosts by setting the `host` property.
390
+
391
+ ```js [app/adapters/application.js]
392
+ import { RESTAdapter } from '@warp-drive/legacy/adapter/rest';
393
+
394
+ export default class ApplicationAdapter extends RESTAdapter {
395
+ host = 'https://api.example.com';
396
+ }
397
+ ```
398
+
399
+ Requests for the `Post` model would now target `https://api.example.com/post/`.
400
+
401
+ @property host
402
+ @public
403
+ @type {String}
404
+ */
405
+ /**
406
+ Some APIs require HTTP headers, e.g. to provide an API
407
+ key. Arbitrary headers can be set as key/value pairs on the
408
+ `RESTAdapter`'s `headers` object and Ember Data will send them
409
+ along with each ajax request. For dynamic headers see [headers
410
+ customization](/ember-data/release/classes/RESTAdapter).
411
+
412
+ ```js [app/adapters/application.js]
413
+ import { RESTAdapter } from '@warp-drive/legacy/adapter/rest';
414
+
415
+ export default class ApplicationAdapter extends RESTAdapter {
416
+ get headers() {
417
+ return {
418
+ 'API_KEY': 'secret key',
419
+ 'ANOTHER_HEADER': 'Some header value'
420
+ };
421
+ }
422
+ }
423
+ ```
424
+
425
+ @property headers
426
+ @public
427
+ @type {Object}
428
+ */
429
+ headers: Record<string, unknown> | undefined;
430
+ /**
431
+ Called by the store in order to fetch the JSON for a given
432
+ type and ID.
433
+
434
+ The `findRecord` method makes an Ajax request to a URL computed by
435
+ `buildURL`, and returns a promise for the resulting payload.
436
+
437
+ This method performs an HTTP `GET` request with the id provided as part of the query string.
438
+
439
+ @since 1.13.0
440
+ @public
441
+ @param {Store} store
442
+ @param {Model} type
443
+ @param {String} id
444
+ @param {Snapshot} snapshot
445
+ @return {Promise} promise
446
+ */
447
+ findRecord(store: Store, type: ModelSchema, id: string, snapshot: Snapshot): Promise<AdapterPayload>;
448
+ /**
449
+ Called by the store in order to fetch a JSON array for all
450
+ of the records for a given type.
451
+
452
+ The `findAll` method makes an Ajax (HTTP GET) request to a URL computed by `buildURL`, and returns a
453
+ promise for the resulting payload.
454
+
455
+ @public
456
+ @param {Store} store
457
+ @param {Model} type
458
+ @param {undefined} neverSet a value is never provided to this argument
459
+ @param {SnapshotRecordArray} snapshotRecordArray
460
+ @return {Promise} promise
461
+ */
462
+ findAll(store: Store, type: ModelSchema, sinceToken: null, snapshotRecordArray: SnapshotRecordArray): Promise<AdapterPayload>;
463
+ /**
464
+ Called by the store in order to fetch a JSON array for
465
+ the records that match a particular query.
466
+
467
+ The `query` method makes an Ajax (HTTP GET) request to a URL
468
+ computed by `buildURL`, and returns a promise for the resulting
469
+ payload.
470
+
471
+ The `query` argument is a simple JavaScript object that will be passed directly
472
+ to the server as parameters.
473
+
474
+ @public
475
+ @param {Store} store
476
+ @param {Model} type
477
+ @param {Object} query
478
+ @param {Collection} recordArray
479
+ @param {Object} adapterOptions
480
+ @return {Promise} promise
481
+ */
482
+ query(store: Store, type: ModelSchema, query: Record<string, unknown>): Promise<AdapterPayload>;
483
+ /**
484
+ Called by the store in order to fetch a JSON object for
485
+ the record that matches a particular query.
486
+
487
+ The `queryRecord` method makes an Ajax (HTTP GET) request to a URL
488
+ computed by `buildURL`, and returns a promise for the resulting
489
+ payload.
490
+
491
+ The `query` argument is a simple JavaScript object that will be passed directly
492
+ to the server as parameters.
493
+
494
+ @since 1.13.0
495
+ @public
496
+ @param {Store} store
497
+ @param {Model} type
498
+ @param {Object} query
499
+ @param {Object} adapterOptions
500
+ @return {Promise} promise
501
+ */
502
+ queryRecord(store: Store, type: ModelSchema, query: Record<string, unknown>, adapterOptions: Record<string, unknown>): Promise<AdapterPayload>;
503
+ /**
504
+ Called by the store in order to fetch several records together if `coalesceFindRequests` is true
505
+
506
+ For example, if the original payload looks like:
507
+
508
+ ```js
509
+ {
510
+ "id": 1,
511
+ "title": "Rails is omakase",
512
+ "comments": [ 1, 2, 3 ]
513
+ }
514
+ ```
515
+
516
+ The IDs will be passed as a URL-encoded Array of IDs, in this form:
517
+
518
+ ```
519
+ ids[]=1&ids[]=2&ids[]=3
520
+ ```
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
+
526
+ The `findMany` method makes an Ajax (HTTP GET) request to a URL computed by `buildURL`, and returns a
527
+ promise for the resulting payload.
528
+
529
+ @public
530
+ @param {Store} store
531
+ @param {Model} type
532
+ @param {Array} ids
533
+ @param {Array} snapshots
534
+ @return {Promise} promise
535
+ */
536
+ findMany(store: Store, type: ModelSchema, ids: string[], snapshots: Snapshot[]): Promise<AdapterPayload>;
537
+ /**
538
+ Called by the store in order to fetch a JSON array for
539
+ the unloaded records in a has-many relationship that were originally
540
+ specified as a URL (inside of `links`).
541
+
542
+ For example, if your original payload looks like this:
543
+
544
+ ```js
545
+ {
546
+ "post": {
547
+ "id": 1,
548
+ "title": "Rails is omakase",
549
+ "links": { "comments": "/posts/1/comments" }
550
+ }
551
+ }
552
+ ```
553
+
554
+ This method will be called with the parent record and `/posts/1/comments`.
555
+
556
+ The `findHasMany` method will make an Ajax (HTTP GET) request to the originally specified URL.
557
+
558
+ The format of your `links` value will influence the final request URL via the `urlPrefix` method:
559
+
560
+ * Links beginning with `//`, `http://`, `https://`, will be used as is, with no further manipulation.
561
+
562
+ * Links beginning with a single `/` will have the current adapter's `host` value prepended to it.
563
+
564
+ * Links with no beginning `/` will have a parentURL prepended to it, via the current adapter's `buildURL`.
565
+
566
+ @public
567
+ @param {Store} store
568
+ @param {Snapshot} snapshot
569
+ @param {String} url
570
+ @param {Object} relationship meta object describing the relationship
571
+ @return {Promise} promise
572
+ */
573
+ findHasMany(store: Store, snapshot: Snapshot, url: string, relationship: Record<string, unknown>): Promise<AdapterPayload>;
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
+
579
+ For example, if your original payload looks like this:
580
+
581
+ ```js
582
+ {
583
+ "person": {
584
+ "id": 1,
585
+ "name": "Tom Dale",
586
+ "links": { "group": "/people/1/group" }
587
+ }
588
+ }
589
+ ```
590
+
591
+ This method will be called with the parent record and `/people/1/group`.
592
+
593
+ The `findBelongsTo` method will make an Ajax (HTTP GET) request to the originally specified URL.
594
+
595
+ The format of your `links` value will influence the final request URL via the `urlPrefix` method:
596
+
597
+ * Links beginning with `//`, `http://`, `https://`, will be used as is, with no further manipulation.
598
+
599
+ * Links beginning with a single `/` will have the current adapter's `host` value prepended to it.
600
+
601
+ * Links with no beginning `/` will have a parentURL prepended to it, via the current adapter's `buildURL`.
602
+
603
+ @public
604
+ */
605
+ findBelongsTo(store: Store, snapshot: Snapshot, url: string, relationship: unknown): Promise<AdapterPayload>;
606
+ /**
607
+ Called by the store when a newly created record is
608
+ saved via the `save` method on a model record instance.
609
+
610
+ The `createRecord` method serializes the record and makes an Ajax (HTTP POST) request
611
+ to a URL computed by `buildURL`.
612
+
613
+ See `serialize` for information on how to customize the serialized form
614
+ of a record.
615
+
616
+ @public
617
+ @param {Store} store
618
+ @param {Model} type
619
+ @param {Snapshot} snapshot
620
+ @return {Promise} promise
621
+ */
622
+ createRecord(store: Store, type: ModelSchema, snapshot: Snapshot): Promise<AdapterPayload>;
623
+ /**
624
+ Called by the store when an existing record is saved
625
+ via the `save` method on a model record instance.
626
+
627
+ The `updateRecord` method serializes the record and makes an Ajax (HTTP PUT) request
628
+ to a URL computed by `buildURL`.
629
+
630
+ See `serialize` for information on how to customize the serialized form
631
+ of a record.
632
+
633
+ @public
634
+ @param {Store} store
635
+ @param {Model} schema
636
+ @param {Snapshot} snapshot
637
+ @return {Promise} promise
638
+ */
639
+ updateRecord(store: Store, schema: ModelSchema, snapshot: Snapshot): Promise<AdapterPayload>;
640
+ /**
641
+ Called by the store when a record is deleted.
642
+
643
+ The `deleteRecord` method makes an Ajax (HTTP DELETE) request to a URL computed by `buildURL`.
644
+
645
+ @public
646
+ @param {Store} store
647
+ @param {Model} type
648
+ @param {Snapshot} snapshot
649
+ @return {Promise} promise
650
+ */
651
+ deleteRecord(store: Store, schema: ModelSchema, snapshot: Snapshot): Promise<AdapterPayload>;
652
+ _stripIDFromURL(store: Store, snapshot: Snapshot): string;
653
+ // http://stackoverflow.com/questions/417142/what-is-the-maximum-length-of-a-url-in-different-browsers
654
+ maxURLLength: number;
655
+ /**
656
+ Organize records into groups, each of which is to be passed to separate
657
+ calls to `findMany`.
658
+
659
+ This implementation groups together records that have the same base URL but
660
+ differing ids. For example `/comments/1` and `/comments/2` will be grouped together
661
+ because we know findMany can coalesce them together as `/comments?ids[]=1&ids[]=2`
662
+
663
+ It also supports urls where ids are passed as a query param, such as `/comments?id=1`
664
+ but not those where there is more than 1 query param such as `/comments?id=2&name=David`
665
+ Currently only the query param of `id` is supported. If you need to support others, please
666
+ override this or the `_stripIDFromURL` method.
667
+
668
+ It does not group records that have differing base urls, such as for example: `/posts/1/comments/2`
669
+ and `/posts/2/comments/3`
670
+
671
+ @public
672
+ @param {Store} store
673
+ @param {Array} snapshots
674
+ @return {Array} an array of arrays of records, each of which is to be
675
+ loaded separately by `findMany`.
676
+ */
677
+ groupRecordsForFindMany(store: Store, snapshots: Snapshot[]): Snapshot[][];
678
+ /**
679
+ Takes an ajax response, and returns the json payload or an error.
680
+
681
+ By default this hook just returns the json payload passed to it.
682
+ You might want to override it in two cases:
683
+
684
+ 1. Your API might return useful results in the response headers.
685
+ Response headers are passed in as the second argument.
686
+
687
+ 2. Your API might return errors as successful responses with status code
688
+ 200 and an Errors text or object. You can return a `InvalidError` or a
689
+ `AdapterError` (or a sub class) from this hook and it will automatically
690
+ reject the promise and put your record into the invalid or error state.
691
+
692
+ Returning a `InvalidError` from this method will cause the
693
+ record to transition into the `invalid` state and make the
694
+ `errors` object available on the record. When returning an
695
+ `InvalidError` the store will attempt to normalize the error data
696
+ returned from the server using the serializer's `extractErrors`
697
+ method.
698
+
699
+ @since 1.13.0
700
+ @public
701
+ @param {Number} status
702
+ @param {Object} headers
703
+ @param {Object} payload
704
+ @param {Object} requestData - the original request information
705
+ @return {Object | AdapterError} response
706
+ */
707
+ handleResponse(status: number, headers: Record<string, string>, payload: Payload, requestData: RequestData): Payload | typeof AdapterError;
708
+ /**
709
+ Default `handleResponse` implementation uses this hook to decide if the
710
+ response is a success.
711
+
712
+ @since 1.13.0
713
+ @public
714
+ @param {Number} status
715
+ @param {Object} headers
716
+ @param {Object} payload
717
+ @return {Boolean}
718
+ */
719
+ isSuccess(status: number, _headers: Record<string, unknown>, _payload: Payload): boolean;
720
+ /**
721
+ Default `handleResponse` implementation uses this hook to decide if the
722
+ response is an invalid error.
723
+
724
+ @since 1.13.0
725
+ @public
726
+ @param {Number} status
727
+ @param {Object} headers
728
+ @param {Object} payload
729
+ @return {Boolean}
730
+ */
731
+ isInvalid(status: number, _headers: Record<string, unknown>, _payload: Payload): boolean;
732
+ /**
733
+ Takes a URL, an HTTP method and a hash of data, and makes an
734
+ HTTP request.
735
+
736
+ When the server responds with a payload, Ember Data will call into `extractSingle`
737
+ or `extractArray` (depending on whether the original query was for one record or
738
+ many records).
739
+
740
+ By default, `ajax` method has the following behavior:
741
+
742
+ * It sets the response `dataType` to `"json"`
743
+ * If the HTTP method is not `"GET"`, it sets the `Content-Type` to be
744
+ `application/json; charset=utf-8`
745
+ * If the HTTP method is not `"GET"`, it stringifies the data passed in. The
746
+ data is the serialized record in the case of a save.
747
+ * Registers success and failure handlers.
748
+
749
+ @private
750
+ @param {String} url
751
+ @param {String} type The request type GET, POST, PUT, DELETE etc.
752
+ @param {Object} options
753
+ @return {Promise} promise
754
+ */
755
+ ajax(url: string, type: HTTPMethod, options?: JQueryAjaxSettings | RequestInit): Promise<AdapterPayload>;
756
+ /**
757
+ @private
758
+ @param {Object} options jQuery ajax options to be used for the ajax request
759
+ */
760
+ _ajaxRequest(options: JQueryRequestInit): void;
761
+ _fetchRequest(options: FetchRequestInit): Promise<Response>;
762
+ _ajax(options: FetchRequestInit | JQueryRequestInit): void;
763
+ /**
764
+ @private
765
+ @param {String} url
766
+ @param {String} type The request type GET, POST, PUT, DELETE etc.
767
+ @param {Object} options
768
+ @return {Object}
769
+ */
770
+ ajaxOptions(url: string, method: HTTPMethod, options: JQueryAjaxSettings | RequestInit): JQueryRequestInit | FetchRequestInit;
771
+ _ajaxURL(url: string): string;
772
+ /**
773
+ @private
774
+ @param {String} responseText
775
+ @return {Object}
776
+ */
777
+ parseErrorResponse(responseText: string): Record<string, unknown> | string;
778
+ /**
779
+ @private
780
+ @param {Number} status
781
+ @param {Object} headers
782
+ @param {Object} payload
783
+ @return {Array} errors payload
784
+ */
785
+ normalizeErrorResponse(status: number, _headers: Record<string, unknown>, payload: Payload): Record<string, unknown>[];
786
+ /**
787
+ Generates a detailed ("friendly") error message, with plenty
788
+ of information for debugging (good luck!)
789
+
790
+ @private
791
+ @param {Number} status
792
+ @param {Object} headers
793
+ @param {Object} payload
794
+ @param {Object} requestData
795
+ @return {String} detailed error message
796
+ */
797
+ generatedDetailedMessage(status: number, headers: Record<string, string>, payload: Payload, requestData: RequestData): string;
798
+ /**
799
+ Used by `findAll` and `findRecord` to build the query's `data` hash
800
+ supplied to the ajax method.
801
+
802
+ @since 2.5.0
803
+ @public
804
+ @param {Snapshot} snapshot
805
+ @return {Object}
806
+ */
807
+ buildQuery(snapshot: Snapshot | SnapshotRecordArray): QueryState;
808
+ }
809
+ // eslint-disable-next-line @typescript-eslint/no-empty-object-type
810
+ interface RESTAdapter extends MixtBuildURLMixin {}
813
811
  /**
814
- * Helper function that translates the options passed to `jQuery.ajax` into a format that `fetch` expects.
815
- *
816
- * @param {Object} _options
817
- * @param {Adapter} adapter
818
- * @private
819
- * @return {Object}
820
- */
812
+ * Helper function that translates the options passed to `jQuery.ajax` into a format that `fetch` expects.
813
+ *
814
+ * @param {Object} _options
815
+ * @param {Adapter} adapter
816
+ * @private
817
+ * @return {Object}
818
+ */
821
819
  export declare function fetchOptions(options: JQueryRequestInit & Partial<FetchRequestInit>, adapter: RESTAdapter): FetchRequestInit;
822
820
  export { RESTAdapter };
823
- //# sourceMappingURL=rest.d.ts.map