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

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 (214) hide show
  1. package/README.md +14 -27
  2. package/declarations/adapter/error.d.ts +7 -7
  3. package/declarations/adapter/json-api.d.ts +6 -8
  4. package/declarations/adapter/rest.d.ts +26 -112
  5. package/declarations/adapter.d.ts +6 -8
  6. package/declarations/compat/-private.d.ts +1 -1
  7. package/declarations/compat/builders/find-all.d.ts +6 -6
  8. package/declarations/compat/builders/find-record.d.ts +8 -8
  9. package/declarations/compat/builders/query.d.ts +12 -12
  10. package/declarations/compat/extensions.d.ts +1 -1
  11. package/declarations/compat/legacy-network-handler/minimum-adapter-interface.d.ts +7 -9
  12. package/declarations/compat/legacy-network-handler/minimum-serializer-interface.d.ts +20 -30
  13. package/declarations/compat/utils.d.ts +17 -17
  14. package/declarations/compat.d.ts +35 -11
  15. package/declarations/index.d.ts +70 -0
  16. package/declarations/model/-private/attr.d.ts +5 -6
  17. package/declarations/model/-private/belongs-to.d.ts +4 -5
  18. package/declarations/model/-private/has-many.d.ts +4 -5
  19. package/declarations/model/-private/hooks.d.ts +1 -1
  20. package/declarations/model/-private/legacy-relationships-support.d.ts +2 -2
  21. package/declarations/model/-private/model.d.ts +18 -59
  22. package/declarations/model/-private/promise-many-array.d.ts +0 -18
  23. package/declarations/model/-private/record-state.d.ts +1 -1
  24. package/declarations/model/-private/references/belongs-to.d.ts +19 -29
  25. package/declarations/model/-private/references/has-many.d.ts +14 -16
  26. package/declarations/model/migration-support.d.ts +46 -21
  27. package/declarations/model-fragments/extensions/fragment-array.d.ts +16 -0
  28. package/declarations/model-fragments/extensions/fragment.d.ts +15 -0
  29. package/declarations/model-fragments/hooks/model-for.d.ts +20 -0
  30. package/declarations/model-fragments/index.d.ts +5 -0
  31. package/declarations/model-fragments/instance-initializers/fragment-extensions.d.ts +9 -0
  32. package/declarations/model-fragments/utilities/with-array-defaults.d.ts +15 -0
  33. package/declarations/model-fragments/utilities/with-fragment-array-defaults.d.ts +20 -0
  34. package/declarations/model-fragments/utilities/with-fragment-defaults.d.ts +19 -0
  35. package/declarations/model-fragments/utilities/with-legacy.d.ts +3 -0
  36. package/declarations/model-fragments.d.ts +9 -0
  37. package/declarations/model.d.ts +2 -2
  38. package/declarations/serializer/-private/embedded-records-mixin.d.ts +1 -6
  39. package/declarations/serializer/-private/transforms/boolean.d.ts +2 -2
  40. package/declarations/serializer/-private/transforms/date.d.ts +2 -2
  41. package/declarations/serializer/-private/transforms/number.d.ts +1 -1
  42. package/declarations/serializer/-private/transforms/string.d.ts +1 -1
  43. package/declarations/serializer/json-api.d.ts +11 -12
  44. package/declarations/serializer/json.d.ts +9 -11
  45. package/declarations/serializer/rest.d.ts +4 -6
  46. package/declarations/serializer.d.ts +9 -12
  47. package/dist/{-private-8UmnAf9J.js → -private-BG3bMiKp.js} +3 -2
  48. package/dist/adapter/-private.js +1 -1
  49. package/dist/adapter/error.js +14 -15
  50. package/dist/adapter/json-api.js +4 -1
  51. package/dist/adapter/rest.js +38 -125
  52. package/dist/adapter.js +6 -8
  53. package/dist/compat/-private.js +1 -1
  54. package/dist/compat/builders.js +26 -26
  55. package/dist/compat/utils.js +17 -18
  56. package/dist/compat.js +61 -43
  57. package/dist/{errors-8kD2mSe_.js → errors-Cz5KrzBk.js} +115 -122
  58. package/dist/hooks-D6diaM34.js +74 -0
  59. package/dist/index.js +195 -0
  60. package/dist/{json-DziiodPf.js → json-ChdEfB0X.js} +18 -35
  61. package/dist/model/-private.js +1 -1
  62. package/dist/model/migration-support.js +57 -25
  63. package/dist/model-for-CqXsIKws.js +221 -0
  64. package/dist/model-fragments.js +76 -0
  65. package/dist/model.js +18 -90
  66. package/dist/{schema-provider-DQu4Rjco.js → schema-provider-DJCV_6AF.js} +50 -95
  67. package/dist/{serialize-into-hash-CS0MIv4F.js → serialize-into-hash-DPZYoF-i.js} +1 -1
  68. package/dist/serializer/json-api.js +18 -45
  69. package/dist/serializer/json.js +1 -1
  70. package/dist/serializer/rest.js +14 -21
  71. package/dist/serializer/transform.js +15 -6
  72. package/dist/serializer.js +9 -13
  73. package/dist/store.js +5 -1
  74. package/dist/unpkg/dev/-private-DtjBbEgy.js +1206 -0
  75. package/dist/unpkg/dev/adapter/-private.js +1 -0
  76. package/dist/unpkg/dev/adapter/error.js +335 -0
  77. package/dist/unpkg/dev/adapter/json-api.js +271 -0
  78. package/dist/unpkg/dev/adapter/rest.js +1171 -0
  79. package/dist/unpkg/dev/adapter.js +1252 -0
  80. package/dist/unpkg/dev/compat/-private.js +1 -0
  81. package/dist/unpkg/dev/compat/builders.js +275 -0
  82. package/dist/unpkg/dev/compat/extensions.js +242 -0
  83. package/dist/unpkg/dev/compat/utils.js +223 -0
  84. package/dist/unpkg/dev/compat.js +1147 -0
  85. package/dist/unpkg/dev/errors-DmGGJr3T.js +2562 -0
  86. package/dist/unpkg/dev/hooks-CkYiE6Ud.js +73 -0
  87. package/dist/unpkg/dev/index.js +197 -0
  88. package/dist/unpkg/dev/json-Cu1LNgmQ.js +1256 -0
  89. package/dist/unpkg/dev/model/-private.js +1 -0
  90. package/dist/unpkg/dev/model/migration-support.js +553 -0
  91. package/dist/unpkg/dev/model-for-CqXsIKws.js +221 -0
  92. package/dist/unpkg/dev/model-fragments.js +76 -0
  93. package/dist/unpkg/dev/model.js +678 -0
  94. package/dist/unpkg/dev/runtime-BPCpkOf1-BKOwiRJp.js +65 -0
  95. package/dist/unpkg/dev/schema-provider-DDVYxmUV.js +2186 -0
  96. package/dist/unpkg/dev/serialize-into-hash-B2xDbuo5.js +259 -0
  97. package/dist/unpkg/dev/serializer/json-api.js +649 -0
  98. package/dist/unpkg/dev/serializer/json.js +4 -0
  99. package/dist/unpkg/dev/serializer/rest.js +1242 -0
  100. package/dist/unpkg/dev/serializer/transform.js +278 -0
  101. package/dist/unpkg/dev/serializer.js +248 -0
  102. package/dist/unpkg/dev/store.js +637 -0
  103. package/dist/unpkg/dev/util-DvanW33H.js +20 -0
  104. package/dist/unpkg/dev/utils-BhvS1iTS.js +8 -0
  105. package/dist/unpkg/dev-deprecated/-private-DtjBbEgy.js +1206 -0
  106. package/dist/unpkg/dev-deprecated/adapter/-private.js +1 -0
  107. package/dist/unpkg/dev-deprecated/adapter/error.js +335 -0
  108. package/dist/unpkg/dev-deprecated/adapter/json-api.js +271 -0
  109. package/dist/unpkg/dev-deprecated/adapter/rest.js +1171 -0
  110. package/dist/unpkg/dev-deprecated/adapter.js +1252 -0
  111. package/dist/unpkg/dev-deprecated/compat/-private.js +1 -0
  112. package/dist/unpkg/dev-deprecated/compat/builders.js +275 -0
  113. package/dist/unpkg/dev-deprecated/compat/extensions.js +242 -0
  114. package/dist/unpkg/dev-deprecated/compat/utils.js +223 -0
  115. package/dist/unpkg/dev-deprecated/compat.js +1147 -0
  116. package/dist/unpkg/dev-deprecated/errors-Spt6ubMd.js +2565 -0
  117. package/dist/unpkg/dev-deprecated/hooks-DOXegvhL.js +73 -0
  118. package/dist/unpkg/dev-deprecated/index.js +196 -0
  119. package/dist/unpkg/dev-deprecated/json-Cu1LNgmQ.js +1256 -0
  120. package/dist/unpkg/dev-deprecated/model/-private.js +1 -0
  121. package/dist/unpkg/dev-deprecated/model/migration-support.js +570 -0
  122. package/dist/unpkg/dev-deprecated/model-for-CqXsIKws.js +221 -0
  123. package/dist/unpkg/dev-deprecated/model-fragments.js +76 -0
  124. package/dist/unpkg/dev-deprecated/model.js +682 -0
  125. package/dist/unpkg/dev-deprecated/runtime-BPCpkOf1-BKOwiRJp.js +65 -0
  126. package/dist/unpkg/dev-deprecated/schema-provider-BP6_8N-V.js +2211 -0
  127. package/dist/unpkg/dev-deprecated/serialize-into-hash-B2xDbuo5.js +259 -0
  128. package/dist/unpkg/dev-deprecated/serializer/json-api.js +649 -0
  129. package/dist/unpkg/dev-deprecated/serializer/json.js +4 -0
  130. package/dist/unpkg/dev-deprecated/serializer/rest.js +1242 -0
  131. package/dist/unpkg/dev-deprecated/serializer/transform.js +278 -0
  132. package/dist/unpkg/dev-deprecated/serializer.js +248 -0
  133. package/dist/unpkg/dev-deprecated/store.js +637 -0
  134. package/dist/unpkg/dev-deprecated/util-CWr5WQOT.js +24 -0
  135. package/dist/unpkg/dev-deprecated/utils-C9PJehtL.js +12 -0
  136. package/dist/unpkg/prod/-private-BdyZaGEh.js +971 -0
  137. package/dist/unpkg/prod/adapter/-private.js +1 -0
  138. package/dist/unpkg/prod/adapter/error.js +330 -0
  139. package/dist/unpkg/prod/adapter/json-api.js +266 -0
  140. package/dist/unpkg/prod/adapter/rest.js +1134 -0
  141. package/dist/unpkg/prod/adapter.js +1219 -0
  142. package/dist/unpkg/prod/compat/-private.js +1 -0
  143. package/dist/unpkg/prod/compat/builders.js +210 -0
  144. package/dist/unpkg/prod/compat/extensions.js +232 -0
  145. package/dist/unpkg/prod/compat/utils.js +218 -0
  146. package/dist/unpkg/prod/compat.js +727 -0
  147. package/dist/unpkg/prod/errors-BGVFCBmi.js +2314 -0
  148. package/dist/unpkg/prod/hooks-BztVA_x0.js +41 -0
  149. package/dist/unpkg/prod/index.js +151 -0
  150. package/dist/unpkg/prod/json-BWrZ5546.js +1243 -0
  151. package/dist/unpkg/prod/model/-private.js +1 -0
  152. package/dist/unpkg/prod/model/migration-support.js +546 -0
  153. package/dist/unpkg/prod/model-for-CqXsIKws.js +221 -0
  154. package/dist/unpkg/prod/model-fragments.js +76 -0
  155. package/dist/unpkg/prod/model.js +593 -0
  156. package/dist/unpkg/prod/runtime-BPCpkOf1-BKOwiRJp.js +65 -0
  157. package/dist/unpkg/prod/schema-provider-DJtD_8jZ.js +1861 -0
  158. package/dist/unpkg/prod/serialize-into-hash-DGlzQteF.js +215 -0
  159. package/dist/unpkg/prod/serializer/json-api.js +592 -0
  160. package/dist/unpkg/prod/serializer/json.js +4 -0
  161. package/dist/unpkg/prod/serializer/rest.js +1210 -0
  162. package/dist/unpkg/prod/serializer/transform.js +278 -0
  163. package/dist/unpkg/prod/serializer.js +248 -0
  164. package/dist/unpkg/prod/store.js +505 -0
  165. package/dist/unpkg/prod/util-DvanW33H.js +20 -0
  166. package/dist/unpkg/prod/utils-BhvS1iTS.js +8 -0
  167. package/dist/unpkg/prod-deprecated/-private-BdyZaGEh.js +971 -0
  168. package/dist/unpkg/prod-deprecated/adapter/-private.js +1 -0
  169. package/dist/unpkg/prod-deprecated/adapter/error.js +330 -0
  170. package/dist/unpkg/prod-deprecated/adapter/json-api.js +266 -0
  171. package/dist/unpkg/prod-deprecated/adapter/rest.js +1134 -0
  172. package/dist/unpkg/prod-deprecated/adapter.js +1219 -0
  173. package/dist/unpkg/prod-deprecated/compat/-private.js +1 -0
  174. package/dist/unpkg/prod-deprecated/compat/builders.js +210 -0
  175. package/dist/unpkg/prod-deprecated/compat/extensions.js +232 -0
  176. package/dist/unpkg/prod-deprecated/compat/utils.js +218 -0
  177. package/dist/unpkg/prod-deprecated/compat.js +727 -0
  178. package/dist/unpkg/prod-deprecated/errors-CdDaK81x.js +2317 -0
  179. package/dist/unpkg/prod-deprecated/hooks-yId87yyG.js +41 -0
  180. package/dist/unpkg/prod-deprecated/index.js +150 -0
  181. package/dist/unpkg/prod-deprecated/json-BWrZ5546.js +1243 -0
  182. package/dist/unpkg/prod-deprecated/model/-private.js +1 -0
  183. package/dist/unpkg/prod-deprecated/model/migration-support.js +563 -0
  184. package/dist/unpkg/prod-deprecated/model-for-CqXsIKws.js +221 -0
  185. package/dist/unpkg/prod-deprecated/model-fragments.js +76 -0
  186. package/dist/unpkg/prod-deprecated/model.js +596 -0
  187. package/dist/unpkg/prod-deprecated/runtime-BPCpkOf1-BKOwiRJp.js +65 -0
  188. package/dist/unpkg/prod-deprecated/schema-provider-CjX55uSY.js +1904 -0
  189. package/dist/unpkg/prod-deprecated/serialize-into-hash-DGlzQteF.js +215 -0
  190. package/dist/unpkg/prod-deprecated/serializer/json-api.js +592 -0
  191. package/dist/unpkg/prod-deprecated/serializer/json.js +4 -0
  192. package/dist/unpkg/prod-deprecated/serializer/rest.js +1210 -0
  193. package/dist/unpkg/prod-deprecated/serializer/transform.js +278 -0
  194. package/dist/unpkg/prod-deprecated/serializer.js +248 -0
  195. package/dist/unpkg/prod-deprecated/store.js +505 -0
  196. package/dist/unpkg/prod-deprecated/util-B6cn-i93.js +23 -0
  197. package/dist/unpkg/prod-deprecated/utils-BUWwQwCh.js +11 -0
  198. package/logos/README.md +2 -2
  199. package/logos/logo-yellow-slab.svg +1 -0
  200. package/logos/word-mark-black.svg +1 -0
  201. package/logos/word-mark-white.svg +1 -0
  202. package/package.json +14 -6
  203. package/logos/NCC-1701-a-blue.svg +0 -4
  204. package/logos/NCC-1701-a-gold.svg +0 -4
  205. package/logos/NCC-1701-a-gold_100.svg +0 -1
  206. package/logos/NCC-1701-a-gold_base-64.txt +0 -1
  207. package/logos/NCC-1701-a.svg +0 -4
  208. package/logos/docs-badge.svg +0 -2
  209. package/logos/ember-data-logo-dark.svg +0 -12
  210. package/logos/ember-data-logo-light.svg +0 -12
  211. package/logos/social1.png +0 -0
  212. package/logos/social2.png +0 -0
  213. package/logos/warp-drive-logo-dark.svg +0 -4
  214. package/logos/warp-drive-logo-gold.svg +0 -4
@@ -0,0 +1,1252 @@
1
+ import EmberObject from '@ember/object';
2
+ import * as s from '@ember/service';
3
+ import { a as decorateFieldV2, i as initializeDeferredDecorator } from "./runtime-BPCpkOf1-BKOwiRJp.js";
4
+ import Mixin from '@ember/object/mixin';
5
+ import { camelize, pluralize } from '@warp-drive/utilities/string';
6
+
7
+ /*
8
+ The structure of this file is such because typing Mixins is hard. Here we've structured it in
9
+ such a way as to maximize the type information that a consumer can utilize. There are simpler
10
+ ways to type a mixin but we would not be able to provide the nice overload signature for buildURL
11
+ */
12
+ // the interface must fully declare the function signatures that the individual functions
13
+ // will also declare. If instead we try to keep them in sync by doing something like
14
+ // `interface BuildURLMixin { buildURL: typeof buildURL }`
15
+ // then an extending class overwriting one of the methods will break because typescript
16
+ // thinks it is a switch from an instance prop (that is a method) to an instance method.
17
+
18
+ // prevents the final constructed object from needing to add
19
+ // host and namespace which are provided by the final consuming
20
+ // class to the prototype which can result in overwrite errors
21
+
22
+ /**
23
+ ## Using BuildURLMixin
24
+
25
+ To use URL building, include the mixin when extending an adapter, and call `buildURL` where needed.
26
+ The default behaviour is designed for RESTAdapter.
27
+
28
+ ### Example
29
+
30
+ ```javascript
31
+ import Adapter, { BuildURLMixin } from '@ember-data/adapter';
32
+
33
+ export default class ApplicationAdapter extends Adapter.extend(BuildURLMixin) {
34
+ findRecord(store, type, id, snapshot) {
35
+ var url = this.buildURL(type.modelName, id, snapshot, 'findRecord');
36
+ return this.ajax(url, 'GET');
37
+ }
38
+ }
39
+ ```
40
+
41
+ ### Attributes
42
+
43
+ The `host` and `namespace` attributes will be used if defined, and are optional.
44
+
45
+ @class BuildURLMixin
46
+ @public
47
+ */
48
+ /**
49
+ Builds a URL for a given type and optional ID.
50
+
51
+ By default, it pluralizes the type's name (for example, 'post'
52
+ becomes 'posts' and 'person' becomes 'people'). To override the
53
+ pluralization see [pathForType](./pathForType?anchor=pathForType).
54
+
55
+ If an ID is specified, it adds the ID to the path generated
56
+ for the type, separated by a `/`.
57
+
58
+ When called by `RESTAdapter.findMany()` the `id` and `snapshot` parameters
59
+ will be arrays of ids and snapshots.
60
+
61
+ @public
62
+ @param {String} modelName
63
+ @param {(String|Array|Object)} id single id or array of ids or query
64
+ @param {(Snapshot|SnapshotRecordArray)} snapshot single snapshot or array of snapshots
65
+ @param {String} requestType
66
+ @param {Object} query object of query parameters to send for query requests.
67
+ @return {String} url
68
+ */
69
+
70
+ function buildURL(modelName, id, snapshot, requestType, query) {
71
+ /*
72
+ Switch statements in typescript don't currently narrow even when the function is implemented
73
+ with overloads.
74
+ We still extract this to stand alone so that we can provide nice overloads for calling signatures,
75
+ but we will still require all of this casting (or a ridiculous number of assertsthat narrow it down
76
+ for us).
77
+ */
78
+ switch (requestType) {
79
+ case 'findRecord':
80
+ return this.urlForFindRecord(id, modelName, snapshot);
81
+ case 'findAll':
82
+ return this.urlForFindAll(modelName, snapshot);
83
+ case 'query':
84
+ return this.urlForQuery(query || {}, modelName);
85
+ case 'queryRecord':
86
+ return this.urlForQueryRecord(query || {}, modelName);
87
+ case 'findMany':
88
+ return this.urlForFindMany(id, modelName, snapshot);
89
+ case 'findHasMany':
90
+ return this.urlForFindHasMany(id, modelName, snapshot);
91
+ case 'findBelongsTo':
92
+ return this.urlForFindBelongsTo(id, modelName, snapshot);
93
+ case 'createRecord':
94
+ return this.urlForCreateRecord(modelName, snapshot);
95
+ case 'updateRecord':
96
+ return this.urlForUpdateRecord(id, modelName, snapshot);
97
+ case 'deleteRecord':
98
+ return this.urlForDeleteRecord(id, modelName, snapshot);
99
+ default:
100
+ // this is the 'never' case but someone may call `buildURL` manually so
101
+ // we try to do something for them.
102
+ return this._buildURL(modelName, id);
103
+ }
104
+ }
105
+
106
+ /**
107
+ @private
108
+ @param {String} modelName
109
+ @param {String} id
110
+ @return {String} url
111
+ */
112
+ function _buildURL(modelName, id) {
113
+ let path;
114
+ const url = [];
115
+ const {
116
+ host
117
+ } = this;
118
+ const prefix = this.urlPrefix();
119
+ if (modelName) {
120
+ path = this.pathForType(modelName);
121
+ if (path) {
122
+ url.push(path);
123
+ }
124
+ }
125
+ if (id) {
126
+ url.push(encodeURIComponent(id));
127
+ }
128
+ if (prefix) {
129
+ url.unshift(prefix);
130
+ }
131
+ let urlString = url.join('/');
132
+ if (!host && urlString && urlString.charAt(0) !== '/') {
133
+ urlString = '/' + urlString;
134
+ }
135
+ return urlString;
136
+ }
137
+
138
+ /**
139
+ Builds a URL for a `store.findRecord(type, id)` call.
140
+
141
+ Example:
142
+
143
+ ```js [app/adapters/user.js]
144
+ import { JSONAPIAdapter } from '@warp-drive/legacy/adapter/json-api';
145
+
146
+ export default class ApplicationAdapter extends JSONAPIAdapter {
147
+ urlForFindRecord(id, modelName, snapshot) {
148
+ let baseUrl = this.buildURL(modelName, id, snapshot);
149
+ return `${baseUrl}/users/${snapshot.adapterOptions.user_id}/playlists/${id}`;
150
+ }
151
+ }
152
+ ```
153
+
154
+ @public
155
+ @param {String} id
156
+ @param {String} modelName
157
+ @param {Snapshot} snapshot
158
+ @return {String} url
159
+
160
+ */
161
+ function urlForFindRecord(id, modelName, snapshot) {
162
+ return this._buildURL(modelName, id);
163
+ }
164
+
165
+ /**
166
+ Builds a URL for a `store.findAll(type)` call.
167
+
168
+ Example:
169
+
170
+ ```js [app/adapters/comment.js]
171
+ import { JSONAPIAdapter } from '@warp-drive/legacy/adapter/json-api';
172
+
173
+ export default class ApplicationAdapter extends JSONAPIAdapter {
174
+ urlForFindAll(modelName, snapshot) {
175
+ let baseUrl = this.buildURL(modelName);
176
+ return `${baseUrl}/data/comments.json`;
177
+ }
178
+ }
179
+ ```
180
+
181
+ @public
182
+ @param {String} modelName
183
+ @param {SnapshotRecordArray} snapshot
184
+ @return {String} url
185
+ */
186
+ function urlForFindAll(modelName, snapshots) {
187
+ return this._buildURL(modelName);
188
+ }
189
+
190
+ /**
191
+ Builds a URL for a `store.query(type, query)` call.
192
+
193
+ Example:
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
+ host = 'https://api.github.com';
200
+ urlForQuery (query, modelName) {
201
+ switch(modelName) {
202
+ case 'repo':
203
+ return `https://api.github.com/orgs/${query.orgId}/repos`;
204
+ default:
205
+ return super.urlForQuery(...arguments);
206
+ }
207
+ }
208
+ }
209
+ ```
210
+
211
+ @public
212
+ @param {Object} query
213
+ @param {String} modelName
214
+ @return {String} url
215
+ */
216
+ function urlForQuery(query, modelName) {
217
+ return this._buildURL(modelName);
218
+ }
219
+
220
+ /**
221
+ Builds a URL for a `store.queryRecord(type, query)` call.
222
+
223
+ Example:
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
+ urlForQueryRecord({ slug }, modelName) {
230
+ let baseUrl = this.buildURL();
231
+ return `${baseUrl}/${encodeURIComponent(slug)}`;
232
+ }
233
+ }
234
+ ```
235
+
236
+ @public
237
+ @param {Object} query
238
+ @param {String} modelName
239
+ @return {String} url
240
+ */
241
+ function urlForQueryRecord(query, modelName) {
242
+ return this._buildURL(modelName);
243
+ }
244
+
245
+ /**
246
+ Builds a URL for coalescing multiple `store.findRecord(type, id)`
247
+ records into 1 request when the adapter's `coalesceFindRequests`
248
+ property is `true`.
249
+
250
+ Example:
251
+
252
+ ```js [app/adapters/application.js]
253
+ import { RESTAdapter } from '@warp-drive/legacy/adapter/rest';
254
+
255
+ export default class ApplicationAdapter extends RESTAdapter {
256
+ urlForFindMany(ids, modelName) {
257
+ let baseUrl = this.buildURL();
258
+ return `${baseUrl}/coalesce`;
259
+ }
260
+ }
261
+ ```
262
+
263
+ @public
264
+ @param {Array} ids
265
+ @param {String} modelName
266
+ @param {Array} snapshots
267
+ @return {String} url
268
+ */
269
+ function urlForFindMany(ids, modelName, snapshots) {
270
+ return this._buildURL(modelName);
271
+ }
272
+
273
+ /**
274
+ Builds a URL for fetching an async `hasMany` relationship when a URL
275
+ is not provided by the server.
276
+
277
+ Example:
278
+
279
+ ```js [app/adapters/application.js]
280
+ import { JSONAPIAdapter } from '@warp-drive/legacy/adapter/json-api';
281
+
282
+ export default class ApplicationAdapter extends JSONAPIAdapter {
283
+ urlForFindHasMany(id, modelName, snapshot) {
284
+ let baseUrl = this.buildURL(modelName, id);
285
+ return `${baseUrl}/relationships`;
286
+ }
287
+ }
288
+ ```
289
+
290
+ @public
291
+ @param {String} id
292
+ @param {String} modelName
293
+ @param {Snapshot} snapshot
294
+ @return {String} url
295
+ */
296
+ function urlForFindHasMany(id, modelName, snapshot) {
297
+ return this._buildURL(modelName, id);
298
+ }
299
+
300
+ /**
301
+ Builds a URL for fetching an async `belongsTo` relationship when a url
302
+ is not provided by the server.
303
+
304
+ Example:
305
+
306
+ ```js [app/adapters/application.js]
307
+ import { JSONAPIAdapter } from '@warp-drive/legacy/adapter/json-api';
308
+
309
+ export default class ApplicationAdapter extends JSONAPIAdapter {
310
+ urlForFindBelongsTo(id, modelName, snapshot) {
311
+ let baseUrl = this.buildURL(modelName, id);
312
+ return `${baseUrl}/relationships`;
313
+ }
314
+ }
315
+ ```
316
+
317
+ @public
318
+ @param {String} id
319
+ @param {String} modelName
320
+ @param {Snapshot} snapshot
321
+ @return {String} url
322
+ */
323
+ function urlForFindBelongsTo(id, modelName, snapshot) {
324
+ return this._buildURL(modelName, id);
325
+ }
326
+
327
+ /**
328
+ Builds a URL for a `record.save()` call when the record was created
329
+ locally using `store.createRecord()`.
330
+
331
+ Example:
332
+
333
+ ```js [app/adapters/application.js]
334
+ import { RESTAdapter } from '@warp-drive/legacy/adapter/rest';
335
+
336
+ export default class ApplicationAdapter extends RESTAdapter {
337
+ urlForCreateRecord(modelName, snapshot) {
338
+ return super.urlForCreateRecord(...arguments) + '/new';
339
+ }
340
+ }
341
+ ```
342
+
343
+ @public
344
+ @param {String} modelName
345
+ @param {Snapshot} snapshot
346
+ @return {String} url
347
+ */
348
+ function urlForCreateRecord(modelName, snapshot) {
349
+ return this._buildURL(modelName);
350
+ }
351
+
352
+ /**
353
+ Builds a URL for a `record.save()` call when the record has been updated locally.
354
+
355
+ Example:
356
+
357
+ ```js [app/adapters/application.js]
358
+ import { RESTAdapter } from '@warp-drive/legacy/adapter/rest';
359
+
360
+ export default class ApplicationAdapter extends RESTAdapter {
361
+ urlForUpdateRecord(id, modelName, snapshot) {
362
+ return `/${id}/feed?access_token=${snapshot.adapterOptions.token}`;
363
+ }
364
+ }
365
+ ```
366
+
367
+ @public
368
+ @param {String} id
369
+ @param {String} modelName
370
+ @param {Snapshot} snapshot
371
+ @return {String} url
372
+ */
373
+ function urlForUpdateRecord(id, modelName, snapshot) {
374
+ return this._buildURL(modelName, id);
375
+ }
376
+
377
+ /**
378
+ Builds a URL for a `record.save()` call when the record has been deleted locally.
379
+
380
+ Example:
381
+
382
+ ```js [app/adapters/application.js]
383
+ import { RESTAdapter } from '@warp-drive/legacy/adapter/rest';
384
+
385
+ export default class ApplicationAdapter extends RESTAdapter {
386
+ urlForDeleteRecord(id, modelName, snapshot) {
387
+ return super.urlForDeleteRecord(...arguments) + '/destroy';
388
+ }
389
+ }
390
+ ```
391
+
392
+ @public
393
+ @param {String} id
394
+ @param {String} modelName
395
+ @param {Snapshot} snapshot
396
+ @return {String} url
397
+ */
398
+ function urlForDeleteRecord(id, modelName, snapshot) {
399
+ return this._buildURL(modelName, id);
400
+ }
401
+
402
+ /**
403
+ @private
404
+ @param {String} path
405
+ @param {String} parentURL
406
+ @return {String} urlPrefix
407
+ */
408
+ function urlPrefix(path, parentURL) {
409
+ const {
410
+ namespace
411
+ } = this;
412
+ let {
413
+ host
414
+ } = this;
415
+ if (!host || host === '/') {
416
+ host = '';
417
+ }
418
+ if (path) {
419
+ // Protocol relative url
420
+ if (/^\/\//.test(path) || /http(s)?:\/\//.test(path)) {
421
+ // Do nothing, the full host is already included.
422
+ return path;
423
+
424
+ // Absolute path
425
+ } else if (path.charAt(0) === '/') {
426
+ return `${host}${path}`;
427
+ // Relative path
428
+ } else {
429
+ return `${parentURL}/${path}`;
430
+ }
431
+ }
432
+
433
+ // No path provided
434
+ const url = [];
435
+ if (host) {
436
+ url.push(host);
437
+ }
438
+ if (namespace) {
439
+ url.push(namespace);
440
+ }
441
+ return url.join('/');
442
+ }
443
+
444
+ /**
445
+ Determines the pathname for a given type.
446
+
447
+ By default, it pluralizes the type's name (for example,
448
+ 'post' becomes 'posts' and 'person' becomes 'people').
449
+
450
+ ### Pathname customization
451
+
452
+ For example, if you have an object `LineItem` with an
453
+ endpoint of `/line_items/`.
454
+
455
+ ```js [app/adapters/application.js]
456
+ import { RESTAdapter } from '@warp-drive/legacy/adapter/rest';
457
+ import { undesrcore, pluralize } from '<app-name>/utils/string-utils';
458
+
459
+ export default class ApplicationAdapter extends RESTAdapter {
460
+ pathForType(modelName) {
461
+ return pluralize(underscore(modelName));
462
+ }
463
+ }
464
+ ```
465
+
466
+ @public
467
+ @param {String} modelName
468
+ @return {String} path
469
+ **/
470
+ function pathForType(modelName) {
471
+ const camelized = camelize(modelName);
472
+ return pluralize(camelized);
473
+ }
474
+
475
+ // we build it this way vs casting to BuildURLMixin so that any
476
+ // changes to the interface surface as errors here.
477
+ const mixinProps = {
478
+ buildURL,
479
+ _buildURL,
480
+ urlForFindRecord,
481
+ urlForFindAll,
482
+ urlForQueryRecord,
483
+ urlForQuery,
484
+ urlForFindMany,
485
+ urlForFindHasMany,
486
+ urlForFindBelongsTo,
487
+ urlForCreateRecord,
488
+ urlForDeleteRecord,
489
+ urlForUpdateRecord,
490
+ urlPrefix,
491
+ pathForType
492
+ };
493
+ const BuildURLMixin = Mixin.create(mixinProps);
494
+
495
+ /**
496
+ * ## Overview
497
+ *
498
+ * :::danger
499
+ * ⚠️ **This is LEGACY documentation** for a feature that is no longer encouraged to be used.
500
+ * If starting a new app or thinking of implementing a new adapter, consider writing a
501
+ * {@link Handler} instead to be used with the {@link RequestManager}
502
+ * ::::
503
+ *
504
+ * In order to properly fetch and update data, @warp-drive/legacy
505
+ * needs to understand how to connect to your API.
506
+ *
507
+ * `Adapters` accept various kinds of requests from the store
508
+ * and manage fulfillment of the request from your API.
509
+ *
510
+ * ### Request Flow
511
+ *
512
+ * When the store decides it needs to issue a request it uses the following flow to manage the request and process the data.
513
+ *
514
+ * - find the appropriate adapter
515
+ * - issue the request to the adapter
516
+ * - await the adapter's response
517
+ * - if an error occurs reject with the error
518
+ * - if no error
519
+ * - if there is response data
520
+ * - pass the response data to the appropriate serializer
521
+ * - update the cache using the JSON:API formatted data from the serializer's response
522
+ * - return the primary record(s) associated with the request
523
+ *
524
+ * ### Request Errors
525
+ *
526
+ * When a request errors and your adapter does not have the ability to recover from the error,
527
+ * you may either reject the promise returned by your adapter method with the error or simply
528
+ * throw the error.
529
+ *
530
+ * If the request was for a `createRecord` `updateRecord` or `deleteRecord` special rules
531
+ * apply to how this error will affect the state of the store and additional properties on
532
+ * the `Error` class may be used. See the documentation for these methods in
533
+ * {@link MinimumAdapterInterface} for more information.
534
+ *
535
+ * ### Implementing an Adapter
536
+ *
537
+ * There are seven required adapter methods, one for each of
538
+ * the primary request types that @warp-drive/legacy issues.
539
+ *
540
+ * They are:
541
+ *
542
+ * - findRecord
543
+ * - findAll
544
+ * - queryRecord
545
+ * - query
546
+ * - createRecord
547
+ * - updateRecord
548
+ * - deleteRecord
549
+ *
550
+ * Each of these request types has a matching store method that triggers it
551
+ * and matching `requestType` that is passed to the serializer's
552
+ * `normalizeResponse` method.
553
+ *
554
+ * If your app only reads data but never writes data, it is not necessary
555
+ * to implement the methods for create, update, and delete. This extends to
556
+ * all of the store's find methods with the exception of `findRecord` (`findAll`,
557
+ * `query`, `queryRecord`): if you do not use the store method in your app then
558
+ * your Adapter does not need the method.
559
+ *
560
+ * ```ts
561
+ * async function fetchData(url, options = {}) {
562
+ * let response = await fetch(url, options);
563
+ * return response.toJSON();
564
+ * }
565
+ *
566
+ * export default class ApplicationAdapter {
567
+ * findRecord(_, { modelName }, id) {
568
+ * return fetchData(`./${modelName}s/${id}`);
569
+ * }
570
+ *
571
+ * static create() {
572
+ * return new this();
573
+ * }
574
+ * }
575
+ * ```
576
+ *
577
+ * ### Adapter Resolution
578
+ *
579
+ * `store.adapterFor(name)` will lookup adapters defined in `app/adapters/` and
580
+ * return an instance.
581
+ *
582
+ * `adapterFor` first attempts to find an adapter with an exact match on `name`,
583
+ then falls back to checking for the presence of an adapter named `application`.
584
+
585
+ If no adapter is found, an error will be thrown.
586
+
587
+ ```ts
588
+ store.adapterFor('author');
589
+
590
+ // lookup paths (in order) =>
591
+ // app/adapters/author.js
592
+ // app/adapters/application.js
593
+ ```
594
+
595
+ Most requests in @warp-drive/legacy are made with respect to a particular `type` (or `modelName`)
596
+ (e.g., "get me the full collection of **books**" or "get me the **employee** whose id is 37"). We
597
+ refer to this as the **primary** resource `type`.
598
+
599
+ `adapterFor` is used by the store to find an adapter with a name matching that of the primary
600
+ resource `type` for the request, which then falls back to the `application` adapter.
601
+
602
+ It is recommended that applications define only a single `application` adapter and serializer
603
+ where possible, only implementing an adapter specific to the `type` when absolutely necessary.
604
+
605
+ If you need to support multiple API versions for the same type, the per-type strategy for
606
+ defining adapters might not be adequate.
607
+
608
+ If you have multiple APIs or multiple API versions and the single application adapter and per-type
609
+ strategy does not suite your needs, one strategy is to write an `application` adapter and serializer
610
+ that make use of `options` to specify the desired format when making a request, then forwards to the
611
+ request to the desired adapter or serializer as needed.
612
+
613
+ ```js [app/adapters/application.js]
614
+ export default class Adapter extends EmberObject {
615
+ findRecord(store, schema, id, snapshot) {
616
+ let { apiVersion } = snapshot.adapterOptions;
617
+ return this.adapterFor(`-api-${apiVersion}`).findRecord(store, schema, id, snapshot);
618
+ }
619
+ }
620
+ ```
621
+
622
+ ### Overriding `Store.adapterFor`
623
+
624
+ ```js
625
+ import Store from '@ember-data/store';
626
+ import Adapter from '@ember-data/adapter/json-api';
627
+
628
+ class extends Store {
629
+ #adapter = new Adapter();
630
+
631
+ adapterFor() {
632
+ return this.#adapter;
633
+ }
634
+ }
635
+ ```
636
+
637
+
638
+ Note: If you are using Ember and would like to make use of `service` injections in your adapter, you will want to additionally `setOwner` for the Adapter.
639
+
640
+ ```js
641
+ import Store from '@ember-data/store';
642
+ import Adapter from '@ember-data/adapter/json-api';
643
+ import { getOwner, setOwner } from '@ember/owner';
644
+
645
+ class extends Store {
646
+ #adapter = null;
647
+
648
+ adapterFor() {
649
+ let adapter = this.#adapter;
650
+ if (!adapter) {
651
+ const owner = getOwner(this);
652
+ adapter = new Adapter();
653
+ setOwner(adapter, owner);
654
+ this.#adapter = adapter;
655
+ }
656
+
657
+ return adapter;
658
+ }
659
+ }
660
+ ```
661
+
662
+ By default when using with Ember you only need to implement this hook if you want your adapter usage to be statically analyzeable. *Ember***Data** will attempt to resolve adapters using Ember's resolver. To provide a single Adapter for your application like the above you would provide it as the default export of the file `app/adapters/application.{js/ts}`
663
+
664
+ ### Using an Adapter
665
+
666
+ Any adapter in `app/adapters/` can be looked up by `name` using `store.adapterFor(name)`.
667
+
668
+ ### Default Adapters
669
+
670
+ Applications whose API's structure endpoint URLs *very close to* or *exactly* the **REST**
671
+ or **JSON:API** convention, the `@ember-data/adapter` package contains implementations
672
+ these applications can extend.
673
+
674
+ Many applications will find writing their own adapter to be allow greater flexibility,
675
+ customization, and maintenance than attempting to override methods in these adapters.
676
+
677
+ @module
678
+ */
679
+
680
+ const service = s.service ?? s.inject;
681
+ /**
682
+ An adapter is an object that receives requests from a store and
683
+ translates them into the appropriate action to take against your
684
+ persistence layer. The persistence layer is usually an HTTP API but
685
+ may be anything, such as the browser's local storage. Typically the
686
+ adapter is not invoked directly instead its functionality is accessed
687
+ through the `store`.
688
+
689
+ > ⚠️ CAUTION you likely want the docs for {@link MinimumAdapterInterface}
690
+ > as extending this abstract class is unnecessary.
691
+
692
+ ### Creating an Adapter
693
+
694
+ Create a new subclass of `Adapter` in the `app/adapters` folder:
695
+
696
+ ```js [app/adapters/application.js]
697
+ import { Adapter } from '@warp-drive/legacy/adapter';
698
+
699
+ export default class extends Adapter {
700
+ // ...your code here
701
+ }
702
+ ```
703
+
704
+ Model-specific adapters can be created by putting your adapter
705
+ class in an `app/adapters/` + `model-name` + `.js` file of the application.
706
+
707
+ ```js [app/adapters/post.js]
708
+ import { Adapter } from '@warp-drive/legacy/adapter';
709
+
710
+ export default class extends Adapter {
711
+ // ...Post-specific adapter code goes here
712
+ }
713
+ ```
714
+
715
+ `Adapter` is an abstract base class that you should override in your
716
+ application to customize it for your backend. The minimum set of methods
717
+ that you should implement is:
718
+
719
+ * `findRecord()`
720
+ * `createRecord()`
721
+ * `updateRecord()`
722
+ * `deleteRecord()`
723
+ * `findAll()`
724
+ * `query()`
725
+
726
+ To improve the network performance of your application, you can optimize
727
+ your adapter by overriding these lower-level methods:
728
+
729
+ * `findMany()`
730
+
731
+
732
+ For an example of the implementation, see `RESTAdapter`, the
733
+ included REST adapter.
734
+
735
+ @public
736
+ */
737
+ class Adapter extends EmberObject {
738
+ static {
739
+ decorateFieldV2(this.prototype, "store", [service]);
740
+ }
741
+ #store = (initializeDeferredDecorator(this, "store"), void 0);
742
+ /**
743
+ The `findRecord()` method is invoked when the store is asked for a record that
744
+ has not previously been loaded. In response to `findRecord()` being called, you
745
+ should query your persistence layer for a record with the given ID. The `findRecord`
746
+ method should return a promise that will resolve to a JavaScript object that will be
747
+ normalized by the serializer.
748
+ Here is an example of the `findRecord` implementation:
749
+ ```js [app/adapters/application.js]
750
+ import { Adapter } from '@warp-drive/legacy/adapter';
751
+ import RSVP from 'RSVP';
752
+ import $ from 'jquery';
753
+ export default class ApplicationAdapter extends Adapter {
754
+ findRecord(store, type, id, snapshot) {
755
+ return new RSVP.Promise(function(resolve, reject) {
756
+ $.getJSON(`/${type.modelName}/${id}`).then(function(data) {
757
+ resolve(data);
758
+ }, function(jqXHR) {
759
+ reject(jqXHR);
760
+ });
761
+ });
762
+ }
763
+ }
764
+ ```
765
+ @public
766
+ */
767
+ // @ts-expect-error
768
+ findRecord(store, type, id, snapshot) {
769
+ {
770
+ throw new Error('You subclassed the Adapter class but missing a findRecord override');
771
+ }
772
+ }
773
+
774
+ /**
775
+ The `findAll()` method is used to retrieve all records for a given type.
776
+ Example
777
+ ```js [app/adapters/application.js]
778
+ import { Adapter } from '@warp-drive/legacy/adapter';
779
+ import RSVP from 'RSVP';
780
+ import $ from 'jquery';
781
+ export default class ApplicationAdapter extends Adapter {
782
+ findAll(store, type) {
783
+ return new RSVP.Promise(function(resolve, reject) {
784
+ $.getJSON(`/${type.modelName}`).then(function(data) {
785
+ resolve(data);
786
+ }, function(jqXHR) {
787
+ reject(jqXHR);
788
+ });
789
+ });
790
+ }
791
+ }
792
+ ```
793
+ @param neverSet a value is never provided to this argument
794
+ @public
795
+ */
796
+ findAll(store, type, neverSet, snapshotRecordArray) {
797
+ {
798
+ throw new Error('You subclassed the Adapter class but missing a findAll override');
799
+ }
800
+ }
801
+
802
+ /**
803
+ This method is called when you call `query` on the store.
804
+ Example
805
+ ```js [app/adapters/application.js]
806
+ import { Adapter } from '@warp-drive/legacy/adapter';
807
+ import RSVP from 'RSVP';
808
+ import $ from 'jquery';
809
+ export default class ApplicationAdapter extends Adapter {
810
+ query(store, type, query) {
811
+ return new RSVP.Promise(function(resolve, reject) {
812
+ $.getJSON(`/${type.modelName}`, query).then(function(data) {
813
+ resolve(data);
814
+ }, function(jqXHR) {
815
+ reject(jqXHR);
816
+ });
817
+ });
818
+ }
819
+ }
820
+ ```
821
+ @public
822
+ */
823
+ // @ts-expect-error
824
+ query(store, type, query) {
825
+ {
826
+ throw new Error('You subclassed the Adapter class but missing a query override');
827
+ }
828
+ }
829
+
830
+ /**
831
+ The `queryRecord()` method is invoked when the store is asked for a single
832
+ record through a query object.
833
+ In response to `queryRecord()` being called, you should always fetch fresh
834
+ data. Once found, you can asynchronously call the store's `push()` method
835
+ to push the record into the store.
836
+ Here is an example `queryRecord` implementation:
837
+ Example
838
+ ```js [app/adapters/application.js]
839
+ import { Adapter, BuildURLMixin } from '@warp-drive/legacy/adapter';
840
+ export default class ApplicationAdapter extends Adapter.extend(BuildURLMixin) {
841
+ queryRecord(store, type, query) {
842
+ return fetch(`/${type.modelName}`, { body: JSON.stringify(query) })
843
+ .then((response) => response.json());
844
+ }
845
+ }
846
+ ```
847
+ @public
848
+ */
849
+ queryRecord(store, type, query, adapterOptions) {
850
+ {
851
+ throw new Error('You subclassed the Adapter class but missing a queryRecord override');
852
+ }
853
+ }
854
+
855
+ /**
856
+ If the globally unique IDs for your records should be generated on the client,
857
+ implement the `generateIdForRecord()` method. This method will be invoked
858
+ each time you create a new record, and the value returned from it will be
859
+ assigned to the record's `primaryKey`.
860
+ Most traditional REST-like HTTP APIs will not use this method. Instead, the ID
861
+ of the record will be set by the server, and your adapter will update the store
862
+ with the new ID when it calls `didCreateRecord()`. Only implement this method if
863
+ you intend to generate record IDs on the client-side.
864
+ The `generateIdForRecord()` method will be invoked with the requesting store as
865
+ the first parameter and the newly created record as the second parameter:
866
+ ```javascript
867
+ import { Adapter } from '@warp-drive/legacy/adapter';
868
+ import { v4 } from 'uuid';
869
+ export default class ApplicationAdapter extends Adapter {
870
+ generateIdForRecord(store, type, inputProperties) {
871
+ return v4();
872
+ }
873
+ }
874
+ ```
875
+ @param {Store} store
876
+ @param {Model} type the Model class of the record
877
+ @param {Object} inputProperties a hash of properties to set on the
878
+ newly created record.
879
+ @return {(String|Number)} id
880
+ @public
881
+ */
882
+
883
+ /**
884
+ Proxies to the serializer's `serialize` method.
885
+ Example
886
+ ```js [app/adapters/application.js]
887
+ import { Adapter } from '@warp-drive/legacy/adapter';
888
+ export default class ApplicationAdapter extends Adapter {
889
+ createRecord(store, type, snapshot) {
890
+ let data = this.serialize(snapshot, { includeId: true });
891
+ let url = `/${type.modelName}`;
892
+ // ...
893
+ }
894
+ }
895
+ ```
896
+ @public
897
+ */
898
+ serialize(snapshot, options) {
899
+ const serialized = snapshot.serialize(options);
900
+ (test => {
901
+ if (!test) {
902
+ throw new Error(`Your adapter's serialize method must return an object, but it returned ${typeof serialized}`);
903
+ }
904
+ })(serialized && typeof serialized === 'object');
905
+ return serialized;
906
+ }
907
+
908
+ /**
909
+ Implement this method in a subclass to handle the creation of
910
+ new records.
911
+ Serializes the record and sends it to the server.
912
+ Example
913
+ ```js [app/adapters/application.js]
914
+ import { Adapter } from '@warp-drive/legacy/adapter';
915
+ import RSVP from 'RSVP';
916
+ import $ from 'jquery';
917
+ export default class ApplicationAdapter extends Adapter {
918
+ createRecord(store, type, snapshot) {
919
+ let data = this.serialize(snapshot, { includeId: true });
920
+ return new RSVP.Promise(function (resolve, reject) {
921
+ $.ajax({
922
+ type: 'POST',
923
+ url: `/${type.modelName}`,
924
+ dataType: 'json',
925
+ data: data
926
+ }).then(function (data) {
927
+ resolve(data);
928
+ }, function (jqXHR) {
929
+ jqXHR.then = null; // tame jQuery's ill mannered promises
930
+ reject(jqXHR);
931
+ });
932
+ });
933
+ }
934
+ }
935
+ ```
936
+ @public
937
+ */
938
+ // @ts-expect-error
939
+ createRecord(store, type, snapshot) {
940
+ {
941
+ throw new Error('You subclassed the Adapter class but missing a createRecord override');
942
+ }
943
+ }
944
+
945
+ /**
946
+ Implement this method in a subclass to handle the updating of
947
+ a record.
948
+ Serializes the record update and sends it to the server.
949
+ The updateRecord method is expected to return a promise that will
950
+ resolve with the serialized record. This allows the backend to
951
+ inform the WarpDrive store the current state of this record after
952
+ the update. If it is not possible to return a serialized record
953
+ the updateRecord promise can also resolve with `undefined` and the
954
+ WarpDrive store will assume all of the updates were successfully
955
+ applied on the backend.
956
+ Example
957
+ ```js [app/adapters/application.js]
958
+ import { Adapter } from '@warp-drive/legacy/adapter';
959
+ import RSVP from 'RSVP';
960
+ import $ from 'jquery';
961
+ export default class ApplicationAdapter extends Adapter {
962
+ updateRecord(store, type, snapshot) {
963
+ let data = this.serialize(snapshot, { includeId: true });
964
+ let id = snapshot.id;
965
+ return new RSVP.Promise(function(resolve, reject) {
966
+ $.ajax({
967
+ type: 'PUT',
968
+ url: `/${type.modelName}/${id}`,
969
+ dataType: 'json',
970
+ data: data
971
+ }).then(function(data) {
972
+ resolve(data);
973
+ }, function(jqXHR) {
974
+ jqXHR.then = null; // tame jQuery's ill mannered promises
975
+ reject(jqXHR);
976
+ });
977
+ });
978
+ }
979
+ }
980
+ ```
981
+ @param {Store} store
982
+ @param {Model} type the Model class of the record
983
+ @param {Snapshot} snapshot
984
+ @return {Promise} promise
985
+ @public
986
+ */
987
+ // @ts-expect-error
988
+ updateRecord(store, type, snapshot) {
989
+ {
990
+ throw new Error('You subclassed the Adapter class but missing a updateRecord override');
991
+ }
992
+ }
993
+
994
+ /**
995
+ Implement this method in a subclass to handle the deletion of
996
+ a record.
997
+ Sends a delete request for the record to the server.
998
+ Example
999
+ ```js [app/adapters/application.js]
1000
+ import { Adapter } from '@warp-drive/legacy/adapter';
1001
+ import RSVP from 'RSVP';
1002
+ import $ from 'jquery';
1003
+ export default class ApplicationAdapter extends Adapter {
1004
+ deleteRecord(store, type, snapshot) {
1005
+ let data = this.serialize(snapshot, { includeId: true });
1006
+ let id = snapshot.id;
1007
+ return new RSVP.Promise(function(resolve, reject) {
1008
+ $.ajax({
1009
+ type: 'DELETE',
1010
+ url: `/${type.modelName}/${id}`,
1011
+ dataType: 'json',
1012
+ data: data
1013
+ }).then(function(data) {
1014
+ resolve(data)
1015
+ }, function(jqXHR) {
1016
+ jqXHR.then = null; // tame jQuery's ill mannered promises
1017
+ reject(jqXHR);
1018
+ });
1019
+ });
1020
+ }
1021
+ }
1022
+ ```
1023
+ @param {Store} store
1024
+ @param {Model} type the Model class of the record
1025
+ @param {Snapshot} snapshot
1026
+ @return {Promise} promise
1027
+ @public
1028
+ */
1029
+ // @ts-expect-error
1030
+ deleteRecord(store, type, snapshot) {
1031
+ {
1032
+ throw new Error('You subclassed the Adapter class but missing a deleteRecord override');
1033
+ }
1034
+ }
1035
+
1036
+ /**
1037
+ By default the store will try to coalesce all `findRecord` calls within the same runloop
1038
+ into as few requests as possible by calling groupRecordsForFindMany and passing it into a findMany call.
1039
+ You can opt out of this behaviour by either not implementing the findMany hook or by setting
1040
+ coalesceFindRequests to false.
1041
+ @property coalesceFindRequests
1042
+ @public
1043
+ @type {Boolean}
1044
+ */
1045
+ get coalesceFindRequests() {
1046
+ const coalesceFindRequests = this._coalesceFindRequests;
1047
+ if (typeof coalesceFindRequests === 'boolean') {
1048
+ return coalesceFindRequests;
1049
+ }
1050
+ return this._coalesceFindRequests = true;
1051
+ }
1052
+ set coalesceFindRequests(value) {
1053
+ this._coalesceFindRequests = value;
1054
+ }
1055
+
1056
+ /**
1057
+ The store will call `findMany` instead of multiple `findRecord`
1058
+ requests to find multiple records at once if coalesceFindRequests
1059
+ is true.
1060
+ ```js [app/adapters/application.js]
1061
+ import { Adapter } from '@warp-drive/legacy/adapter';
1062
+ import RSVP from 'RSVP';
1063
+ import $ from 'jquery';
1064
+ export default class ApplicationAdapter extends Adapter {
1065
+ findMany(store, type, ids, snapshots) {
1066
+ return new RSVP.Promise(function(resolve, reject) {
1067
+ $.ajax({
1068
+ type: 'GET',
1069
+ url: `/${type.modelName}/`,
1070
+ dataType: 'json',
1071
+ data: { filter: { id: ids.join(',') } }
1072
+ }).then(function(data) {
1073
+ resolve(data);
1074
+ }, function(jqXHR) {
1075
+ jqXHR.then = null; // tame jQuery's ill mannered promises
1076
+ reject(jqXHR);
1077
+ });
1078
+ });
1079
+ }
1080
+ }
1081
+ ```
1082
+ @param {Store} store
1083
+ @param {Model} type the Model class of the records
1084
+ @param {Array} ids
1085
+ @param {Array} snapshots
1086
+ @return {Promise} promise
1087
+ @public
1088
+ */
1089
+
1090
+ /**
1091
+ Organize records into groups, each of which is to be passed to separate
1092
+ calls to `findMany`.
1093
+ For example, if your API has nested URLs that depend on the parent, you will
1094
+ want to group records by their parent.
1095
+ The default implementation returns the records as a single group.
1096
+ @public
1097
+ @param {Store} store
1098
+ @param {Array} snapshots
1099
+ @return {Array} an array of arrays of records, each of which is to be
1100
+ loaded separately by `findMany`.
1101
+ */
1102
+ groupRecordsForFindMany(store, snapshots) {
1103
+ return [snapshots];
1104
+ }
1105
+
1106
+ /**
1107
+ This method is used by the store to determine if the store should
1108
+ reload a record from the adapter when a record is requested by
1109
+ `store.findRecord`.
1110
+ If this method returns `true`, the store will re-fetch a record from
1111
+ the adapter. If this method returns `false`, the store will resolve
1112
+ immediately using the cached record.
1113
+ For example, if you are building an events ticketing system, in which users
1114
+ can only reserve tickets for 20 minutes at a time, and want to ensure that
1115
+ in each route you have data that is no more than 20 minutes old you could
1116
+ write:
1117
+ ```javascript
1118
+ shouldReloadRecord(store, ticketSnapshot) {
1119
+ let lastAccessedAt = ticketSnapshot.attr('lastAccessedAt');
1120
+ let timeDiff = moment().diff(lastAccessedAt, 'minutes');
1121
+ if (timeDiff > 20) {
1122
+ return true;
1123
+ } else {
1124
+ return false;
1125
+ }
1126
+ }
1127
+ ```
1128
+ This method would ensure that whenever you do `store.findRecord('ticket',
1129
+ id)` you will always get a ticket that is no more than 20 minutes old. In
1130
+ case the cached version is more than 20 minutes old, `findRecord` will not
1131
+ resolve until you fetched the latest version.
1132
+ By default this hook returns `false`, as most UIs should not block user
1133
+ interactions while waiting on data update.
1134
+ Note that, with default settings, `shouldBackgroundReloadRecord` will always
1135
+ re-fetch the records in the background even if `shouldReloadRecord` returns
1136
+ `false`. You can override `shouldBackgroundReloadRecord` if this does not
1137
+ suit your use case.
1138
+ @since 1.13.0
1139
+ @param {Store} store
1140
+ @param {Snapshot} snapshot
1141
+ @return {Boolean}
1142
+ @public
1143
+ */
1144
+ shouldReloadRecord(store, snapshot) {
1145
+ return false;
1146
+ }
1147
+
1148
+ /**
1149
+ This method is used by the store to determine if the store should
1150
+ reload all records from the adapter when records are requested by
1151
+ `store.findAll`.
1152
+ If this method returns `true`, the store will re-fetch all records from
1153
+ the adapter. If this method returns `false`, the store will resolve
1154
+ immediately using the cached records.
1155
+ For example, if you are building an events ticketing system, in which users
1156
+ can only reserve tickets for 20 minutes at a time, and want to ensure that
1157
+ in each route you have data that is no more than 20 minutes old you could
1158
+ write:
1159
+ ```javascript
1160
+ shouldReloadAll(store, snapshotArray) {
1161
+ let snapshots = snapshotArray.snapshots();
1162
+ return snapshots.any((ticketSnapshot) => {
1163
+ let lastAccessedAt = ticketSnapshot.attr('lastAccessedAt');
1164
+ let timeDiff = moment().diff(lastAccessedAt, 'minutes');
1165
+ if (timeDiff > 20) {
1166
+ return true;
1167
+ } else {
1168
+ return false;
1169
+ }
1170
+ });
1171
+ }
1172
+ ```
1173
+ This method would ensure that whenever you do `store.findAll('ticket')` you
1174
+ will always get a list of tickets that are no more than 20 minutes old. In
1175
+ case a cached version is more than 20 minutes old, `findAll` will not
1176
+ resolve until you fetched the latest versions.
1177
+ By default, this method returns `true` if the passed `snapshotRecordArray`
1178
+ is empty (meaning that there are no records locally available yet),
1179
+ otherwise, it returns `false`.
1180
+ Note that, with default settings, `shouldBackgroundReloadAll` will always
1181
+ re-fetch all the records in the background even if `shouldReloadAll` returns
1182
+ `false`. You can override `shouldBackgroundReloadAll` if this does not suit
1183
+ your use case.
1184
+ @since 1.13.0
1185
+ @param {Store} store
1186
+ @param {SnapshotRecordArray} snapshotRecordArray
1187
+ @return {Boolean}
1188
+ @public
1189
+ */
1190
+ shouldReloadAll(store, snapshotRecordArray) {
1191
+ return !snapshotRecordArray.length;
1192
+ }
1193
+
1194
+ /**
1195
+ This method is used by the store to determine if the store should
1196
+ reload a record after the `store.findRecord` method resolves a
1197
+ cached record.
1198
+ This method is *only* checked by the store when the store is
1199
+ returning a cached record.
1200
+ If this method returns `true` the store will re-fetch a record from
1201
+ the adapter.
1202
+ For example, if you do not want to fetch complex data over a mobile
1203
+ connection, or if the network is down, you can implement
1204
+ `shouldBackgroundReloadRecord` as follows:
1205
+ ```javascript
1206
+ shouldBackgroundReloadRecord(store, snapshot) {
1207
+ let { downlink, effectiveType } = navigator.connection;
1208
+ return downlink > 0 && effectiveType === '4g';
1209
+ }
1210
+ ```
1211
+ By default, this hook returns `true` so the data for the record is updated
1212
+ in the background.
1213
+ @since 1.13.0
1214
+ @param {Store} store
1215
+ @param {Snapshot} snapshot
1216
+ @return {Boolean}
1217
+ @public
1218
+ */
1219
+ shouldBackgroundReloadRecord(store, snapshot) {
1220
+ return true;
1221
+ }
1222
+
1223
+ /**
1224
+ This method is used by the store to determine if the store should
1225
+ reload a record array after the `store.findAll` method resolves
1226
+ with a cached record array.
1227
+ This method is *only* checked by the store when the store is
1228
+ returning a cached record array.
1229
+ If this method returns `true` the store will re-fetch all records
1230
+ from the adapter.
1231
+ For example, if you do not want to fetch complex data over a mobile
1232
+ connection, or if the network is down, you can implement
1233
+ `shouldBackgroundReloadAll` as follows:
1234
+ ```javascript
1235
+ shouldBackgroundReloadAll(store, snapshotArray) {
1236
+ let { downlink, effectiveType } = navigator.connection;
1237
+ return downlink > 0 && effectiveType === '4g';
1238
+ }
1239
+ ```
1240
+ By default this method returns `true`, indicating that a background reload
1241
+ should always be triggered.
1242
+ @since 1.13.0
1243
+ @param {Store} store
1244
+ @param {SnapshotRecordArray} snapshotRecordArray
1245
+ @return {Boolean}
1246
+ @public
1247
+ */
1248
+ shouldBackgroundReloadAll(store, snapshotRecordArray) {
1249
+ return true;
1250
+ }
1251
+ }
1252
+ export { Adapter, BuildURLMixin };