@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,1219 @@
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
+ /**
771
+ The `findAll()` method is used to retrieve all records for a given type.
772
+ Example
773
+ ```js [app/adapters/application.js]
774
+ import { Adapter } from '@warp-drive/legacy/adapter';
775
+ import RSVP from 'RSVP';
776
+ import $ from 'jquery';
777
+ export default class ApplicationAdapter extends Adapter {
778
+ findAll(store, type) {
779
+ return new RSVP.Promise(function(resolve, reject) {
780
+ $.getJSON(`/${type.modelName}`).then(function(data) {
781
+ resolve(data);
782
+ }, function(jqXHR) {
783
+ reject(jqXHR);
784
+ });
785
+ });
786
+ }
787
+ }
788
+ ```
789
+ @param neverSet a value is never provided to this argument
790
+ @public
791
+ */
792
+ findAll(store, type, neverSet, snapshotRecordArray) {}
793
+
794
+ /**
795
+ This method is called when you call `query` on the store.
796
+ Example
797
+ ```js [app/adapters/application.js]
798
+ import { Adapter } from '@warp-drive/legacy/adapter';
799
+ import RSVP from 'RSVP';
800
+ import $ from 'jquery';
801
+ export default class ApplicationAdapter extends Adapter {
802
+ query(store, type, query) {
803
+ return new RSVP.Promise(function(resolve, reject) {
804
+ $.getJSON(`/${type.modelName}`, query).then(function(data) {
805
+ resolve(data);
806
+ }, function(jqXHR) {
807
+ reject(jqXHR);
808
+ });
809
+ });
810
+ }
811
+ }
812
+ ```
813
+ @public
814
+ */
815
+ // @ts-expect-error
816
+ query(store, type, query) {}
817
+
818
+ /**
819
+ The `queryRecord()` method is invoked when the store is asked for a single
820
+ record through a query object.
821
+ In response to `queryRecord()` being called, you should always fetch fresh
822
+ data. Once found, you can asynchronously call the store's `push()` method
823
+ to push the record into the store.
824
+ Here is an example `queryRecord` implementation:
825
+ Example
826
+ ```js [app/adapters/application.js]
827
+ import { Adapter, BuildURLMixin } from '@warp-drive/legacy/adapter';
828
+ export default class ApplicationAdapter extends Adapter.extend(BuildURLMixin) {
829
+ queryRecord(store, type, query) {
830
+ return fetch(`/${type.modelName}`, { body: JSON.stringify(query) })
831
+ .then((response) => response.json());
832
+ }
833
+ }
834
+ ```
835
+ @public
836
+ */
837
+ queryRecord(store, type, query, adapterOptions) {}
838
+
839
+ /**
840
+ If the globally unique IDs for your records should be generated on the client,
841
+ implement the `generateIdForRecord()` method. This method will be invoked
842
+ each time you create a new record, and the value returned from it will be
843
+ assigned to the record's `primaryKey`.
844
+ Most traditional REST-like HTTP APIs will not use this method. Instead, the ID
845
+ of the record will be set by the server, and your adapter will update the store
846
+ with the new ID when it calls `didCreateRecord()`. Only implement this method if
847
+ you intend to generate record IDs on the client-side.
848
+ The `generateIdForRecord()` method will be invoked with the requesting store as
849
+ the first parameter and the newly created record as the second parameter:
850
+ ```javascript
851
+ import { Adapter } from '@warp-drive/legacy/adapter';
852
+ import { v4 } from 'uuid';
853
+ export default class ApplicationAdapter extends Adapter {
854
+ generateIdForRecord(store, type, inputProperties) {
855
+ return v4();
856
+ }
857
+ }
858
+ ```
859
+ @param {Store} store
860
+ @param {Model} type the Model class of the record
861
+ @param {Object} inputProperties a hash of properties to set on the
862
+ newly created record.
863
+ @return {(String|Number)} id
864
+ @public
865
+ */
866
+
867
+ /**
868
+ Proxies to the serializer's `serialize` method.
869
+ Example
870
+ ```js [app/adapters/application.js]
871
+ import { Adapter } from '@warp-drive/legacy/adapter';
872
+ export default class ApplicationAdapter extends Adapter {
873
+ createRecord(store, type, snapshot) {
874
+ let data = this.serialize(snapshot, { includeId: true });
875
+ let url = `/${type.modelName}`;
876
+ // ...
877
+ }
878
+ }
879
+ ```
880
+ @public
881
+ */
882
+ serialize(snapshot, options) {
883
+ const serialized = snapshot.serialize(options);
884
+ return serialized;
885
+ }
886
+
887
+ /**
888
+ Implement this method in a subclass to handle the creation of
889
+ new records.
890
+ Serializes the record and sends it to the server.
891
+ Example
892
+ ```js [app/adapters/application.js]
893
+ import { Adapter } from '@warp-drive/legacy/adapter';
894
+ import RSVP from 'RSVP';
895
+ import $ from 'jquery';
896
+ export default class ApplicationAdapter extends Adapter {
897
+ createRecord(store, type, snapshot) {
898
+ let data = this.serialize(snapshot, { includeId: true });
899
+ return new RSVP.Promise(function (resolve, reject) {
900
+ $.ajax({
901
+ type: 'POST',
902
+ url: `/${type.modelName}`,
903
+ dataType: 'json',
904
+ data: data
905
+ }).then(function (data) {
906
+ resolve(data);
907
+ }, function (jqXHR) {
908
+ jqXHR.then = null; // tame jQuery's ill mannered promises
909
+ reject(jqXHR);
910
+ });
911
+ });
912
+ }
913
+ }
914
+ ```
915
+ @public
916
+ */
917
+ // @ts-expect-error
918
+ createRecord(store, type, snapshot) {}
919
+
920
+ /**
921
+ Implement this method in a subclass to handle the updating of
922
+ a record.
923
+ Serializes the record update and sends it to the server.
924
+ The updateRecord method is expected to return a promise that will
925
+ resolve with the serialized record. This allows the backend to
926
+ inform the WarpDrive store the current state of this record after
927
+ the update. If it is not possible to return a serialized record
928
+ the updateRecord promise can also resolve with `undefined` and the
929
+ WarpDrive store will assume all of the updates were successfully
930
+ applied on the backend.
931
+ Example
932
+ ```js [app/adapters/application.js]
933
+ import { Adapter } from '@warp-drive/legacy/adapter';
934
+ import RSVP from 'RSVP';
935
+ import $ from 'jquery';
936
+ export default class ApplicationAdapter extends Adapter {
937
+ updateRecord(store, type, snapshot) {
938
+ let data = this.serialize(snapshot, { includeId: true });
939
+ let id = snapshot.id;
940
+ return new RSVP.Promise(function(resolve, reject) {
941
+ $.ajax({
942
+ type: 'PUT',
943
+ url: `/${type.modelName}/${id}`,
944
+ dataType: 'json',
945
+ data: data
946
+ }).then(function(data) {
947
+ resolve(data);
948
+ }, function(jqXHR) {
949
+ jqXHR.then = null; // tame jQuery's ill mannered promises
950
+ reject(jqXHR);
951
+ });
952
+ });
953
+ }
954
+ }
955
+ ```
956
+ @param {Store} store
957
+ @param {Model} type the Model class of the record
958
+ @param {Snapshot} snapshot
959
+ @return {Promise} promise
960
+ @public
961
+ */
962
+ // @ts-expect-error
963
+ updateRecord(store, type, snapshot) {}
964
+
965
+ /**
966
+ Implement this method in a subclass to handle the deletion of
967
+ a record.
968
+ Sends a delete request for the record to the server.
969
+ Example
970
+ ```js [app/adapters/application.js]
971
+ import { Adapter } from '@warp-drive/legacy/adapter';
972
+ import RSVP from 'RSVP';
973
+ import $ from 'jquery';
974
+ export default class ApplicationAdapter extends Adapter {
975
+ deleteRecord(store, type, snapshot) {
976
+ let data = this.serialize(snapshot, { includeId: true });
977
+ let id = snapshot.id;
978
+ return new RSVP.Promise(function(resolve, reject) {
979
+ $.ajax({
980
+ type: 'DELETE',
981
+ url: `/${type.modelName}/${id}`,
982
+ dataType: 'json',
983
+ data: data
984
+ }).then(function(data) {
985
+ resolve(data)
986
+ }, function(jqXHR) {
987
+ jqXHR.then = null; // tame jQuery's ill mannered promises
988
+ reject(jqXHR);
989
+ });
990
+ });
991
+ }
992
+ }
993
+ ```
994
+ @param {Store} store
995
+ @param {Model} type the Model class of the record
996
+ @param {Snapshot} snapshot
997
+ @return {Promise} promise
998
+ @public
999
+ */
1000
+ // @ts-expect-error
1001
+ deleteRecord(store, type, snapshot) {}
1002
+
1003
+ /**
1004
+ By default the store will try to coalesce all `findRecord` calls within the same runloop
1005
+ into as few requests as possible by calling groupRecordsForFindMany and passing it into a findMany call.
1006
+ You can opt out of this behaviour by either not implementing the findMany hook or by setting
1007
+ coalesceFindRequests to false.
1008
+ @property coalesceFindRequests
1009
+ @public
1010
+ @type {Boolean}
1011
+ */
1012
+ get coalesceFindRequests() {
1013
+ const coalesceFindRequests = this._coalesceFindRequests;
1014
+ if (typeof coalesceFindRequests === 'boolean') {
1015
+ return coalesceFindRequests;
1016
+ }
1017
+ return this._coalesceFindRequests = true;
1018
+ }
1019
+ set coalesceFindRequests(value) {
1020
+ this._coalesceFindRequests = value;
1021
+ }
1022
+
1023
+ /**
1024
+ The store will call `findMany` instead of multiple `findRecord`
1025
+ requests to find multiple records at once if coalesceFindRequests
1026
+ is true.
1027
+ ```js [app/adapters/application.js]
1028
+ import { Adapter } from '@warp-drive/legacy/adapter';
1029
+ import RSVP from 'RSVP';
1030
+ import $ from 'jquery';
1031
+ export default class ApplicationAdapter extends Adapter {
1032
+ findMany(store, type, ids, snapshots) {
1033
+ return new RSVP.Promise(function(resolve, reject) {
1034
+ $.ajax({
1035
+ type: 'GET',
1036
+ url: `/${type.modelName}/`,
1037
+ dataType: 'json',
1038
+ data: { filter: { id: ids.join(',') } }
1039
+ }).then(function(data) {
1040
+ resolve(data);
1041
+ }, function(jqXHR) {
1042
+ jqXHR.then = null; // tame jQuery's ill mannered promises
1043
+ reject(jqXHR);
1044
+ });
1045
+ });
1046
+ }
1047
+ }
1048
+ ```
1049
+ @param {Store} store
1050
+ @param {Model} type the Model class of the records
1051
+ @param {Array} ids
1052
+ @param {Array} snapshots
1053
+ @return {Promise} promise
1054
+ @public
1055
+ */
1056
+
1057
+ /**
1058
+ Organize records into groups, each of which is to be passed to separate
1059
+ calls to `findMany`.
1060
+ For example, if your API has nested URLs that depend on the parent, you will
1061
+ want to group records by their parent.
1062
+ The default implementation returns the records as a single group.
1063
+ @public
1064
+ @param {Store} store
1065
+ @param {Array} snapshots
1066
+ @return {Array} an array of arrays of records, each of which is to be
1067
+ loaded separately by `findMany`.
1068
+ */
1069
+ groupRecordsForFindMany(store, snapshots) {
1070
+ return [snapshots];
1071
+ }
1072
+
1073
+ /**
1074
+ This method is used by the store to determine if the store should
1075
+ reload a record from the adapter when a record is requested by
1076
+ `store.findRecord`.
1077
+ If this method returns `true`, the store will re-fetch a record from
1078
+ the adapter. If this method returns `false`, the store will resolve
1079
+ immediately using the cached record.
1080
+ For example, if you are building an events ticketing system, in which users
1081
+ can only reserve tickets for 20 minutes at a time, and want to ensure that
1082
+ in each route you have data that is no more than 20 minutes old you could
1083
+ write:
1084
+ ```javascript
1085
+ shouldReloadRecord(store, ticketSnapshot) {
1086
+ let lastAccessedAt = ticketSnapshot.attr('lastAccessedAt');
1087
+ let timeDiff = moment().diff(lastAccessedAt, 'minutes');
1088
+ if (timeDiff > 20) {
1089
+ return true;
1090
+ } else {
1091
+ return false;
1092
+ }
1093
+ }
1094
+ ```
1095
+ This method would ensure that whenever you do `store.findRecord('ticket',
1096
+ id)` you will always get a ticket that is no more than 20 minutes old. In
1097
+ case the cached version is more than 20 minutes old, `findRecord` will not
1098
+ resolve until you fetched the latest version.
1099
+ By default this hook returns `false`, as most UIs should not block user
1100
+ interactions while waiting on data update.
1101
+ Note that, with default settings, `shouldBackgroundReloadRecord` will always
1102
+ re-fetch the records in the background even if `shouldReloadRecord` returns
1103
+ `false`. You can override `shouldBackgroundReloadRecord` if this does not
1104
+ suit your use case.
1105
+ @since 1.13.0
1106
+ @param {Store} store
1107
+ @param {Snapshot} snapshot
1108
+ @return {Boolean}
1109
+ @public
1110
+ */
1111
+ shouldReloadRecord(store, snapshot) {
1112
+ return false;
1113
+ }
1114
+
1115
+ /**
1116
+ This method is used by the store to determine if the store should
1117
+ reload all records from the adapter when records are requested by
1118
+ `store.findAll`.
1119
+ If this method returns `true`, the store will re-fetch all records from
1120
+ the adapter. If this method returns `false`, the store will resolve
1121
+ immediately using the cached records.
1122
+ For example, if you are building an events ticketing system, in which users
1123
+ can only reserve tickets for 20 minutes at a time, and want to ensure that
1124
+ in each route you have data that is no more than 20 minutes old you could
1125
+ write:
1126
+ ```javascript
1127
+ shouldReloadAll(store, snapshotArray) {
1128
+ let snapshots = snapshotArray.snapshots();
1129
+ return snapshots.any((ticketSnapshot) => {
1130
+ let lastAccessedAt = ticketSnapshot.attr('lastAccessedAt');
1131
+ let timeDiff = moment().diff(lastAccessedAt, 'minutes');
1132
+ if (timeDiff > 20) {
1133
+ return true;
1134
+ } else {
1135
+ return false;
1136
+ }
1137
+ });
1138
+ }
1139
+ ```
1140
+ This method would ensure that whenever you do `store.findAll('ticket')` you
1141
+ will always get a list of tickets that are no more than 20 minutes old. In
1142
+ case a cached version is more than 20 minutes old, `findAll` will not
1143
+ resolve until you fetched the latest versions.
1144
+ By default, this method returns `true` if the passed `snapshotRecordArray`
1145
+ is empty (meaning that there are no records locally available yet),
1146
+ otherwise, it returns `false`.
1147
+ Note that, with default settings, `shouldBackgroundReloadAll` will always
1148
+ re-fetch all the records in the background even if `shouldReloadAll` returns
1149
+ `false`. You can override `shouldBackgroundReloadAll` if this does not suit
1150
+ your use case.
1151
+ @since 1.13.0
1152
+ @param {Store} store
1153
+ @param {SnapshotRecordArray} snapshotRecordArray
1154
+ @return {Boolean}
1155
+ @public
1156
+ */
1157
+ shouldReloadAll(store, snapshotRecordArray) {
1158
+ return !snapshotRecordArray.length;
1159
+ }
1160
+
1161
+ /**
1162
+ This method is used by the store to determine if the store should
1163
+ reload a record after the `store.findRecord` method resolves a
1164
+ cached record.
1165
+ This method is *only* checked by the store when the store is
1166
+ returning a cached record.
1167
+ If this method returns `true` the store will re-fetch a record from
1168
+ the adapter.
1169
+ For example, if you do not want to fetch complex data over a mobile
1170
+ connection, or if the network is down, you can implement
1171
+ `shouldBackgroundReloadRecord` as follows:
1172
+ ```javascript
1173
+ shouldBackgroundReloadRecord(store, snapshot) {
1174
+ let { downlink, effectiveType } = navigator.connection;
1175
+ return downlink > 0 && effectiveType === '4g';
1176
+ }
1177
+ ```
1178
+ By default, this hook returns `true` so the data for the record is updated
1179
+ in the background.
1180
+ @since 1.13.0
1181
+ @param {Store} store
1182
+ @param {Snapshot} snapshot
1183
+ @return {Boolean}
1184
+ @public
1185
+ */
1186
+ shouldBackgroundReloadRecord(store, snapshot) {
1187
+ return true;
1188
+ }
1189
+
1190
+ /**
1191
+ This method is used by the store to determine if the store should
1192
+ reload a record array after the `store.findAll` method resolves
1193
+ with a cached record array.
1194
+ This method is *only* checked by the store when the store is
1195
+ returning a cached record array.
1196
+ If this method returns `true` the store will re-fetch all records
1197
+ from the adapter.
1198
+ For example, if you do not want to fetch complex data over a mobile
1199
+ connection, or if the network is down, you can implement
1200
+ `shouldBackgroundReloadAll` as follows:
1201
+ ```javascript
1202
+ shouldBackgroundReloadAll(store, snapshotArray) {
1203
+ let { downlink, effectiveType } = navigator.connection;
1204
+ return downlink > 0 && effectiveType === '4g';
1205
+ }
1206
+ ```
1207
+ By default this method returns `true`, indicating that a background reload
1208
+ should always be triggered.
1209
+ @since 1.13.0
1210
+ @param {Store} store
1211
+ @param {SnapshotRecordArray} snapshotRecordArray
1212
+ @return {Boolean}
1213
+ @public
1214
+ */
1215
+ shouldBackgroundReloadAll(store, snapshotRecordArray) {
1216
+ return true;
1217
+ }
1218
+ }
1219
+ export { Adapter, BuildURLMixin };