@warp-drive/legacy 5.9.0-alpha.21 → 5.9.0-alpha.22

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 (467) hide show
  1. package/dist/-private-DQ6Iap2t.js +193 -0
  2. package/dist/-private-DQ6Iap2t.js.map +1 -0
  3. package/dist/-private-DccRZQsm.js +904 -0
  4. package/dist/-private-DccRZQsm.js.map +1 -0
  5. package/dist/adapter/-private.d.ts +33 -0
  6. package/dist/adapter/-private.d.ts.map +1 -0
  7. package/dist/adapter/-private.js +3 -1
  8. package/dist/adapter/error.d.ts +2 -0
  9. package/dist/adapter/error.js +422 -453
  10. package/dist/adapter/error.js.map +1 -0
  11. package/{declarations → dist}/adapter/json-api.d.ts +30 -29
  12. package/dist/adapter/json-api.d.ts.map +1 -0
  13. package/dist/adapter/json-api.js +265 -143
  14. package/dist/adapter/json-api.js.map +1 -0
  15. package/dist/adapter/rest.d.ts +2 -0
  16. package/dist/adapter/rest.js +937 -1136
  17. package/dist/adapter/rest.js.map +1 -0
  18. package/dist/adapter-CARrK3-8.d.ts +784 -0
  19. package/dist/adapter-CARrK3-8.d.ts.map +1 -0
  20. package/dist/adapter-XgLAkYVs.js +1130 -0
  21. package/dist/adapter-XgLAkYVs.js.map +1 -0
  22. package/dist/adapter.d.ts +2 -0
  23. package/dist/adapter.js +1 -1248
  24. package/dist/compat/-private.d.ts +2 -0
  25. package/dist/compat/-private.js +3 -1
  26. package/dist/compat/builders.d.ts +169 -0
  27. package/dist/compat/builders.d.ts.map +1 -0
  28. package/dist/compat/builders.js +205 -245
  29. package/dist/compat/builders.js.map +1 -0
  30. package/dist/compat/extensions.d.ts +2 -0
  31. package/dist/compat/extensions.js +265 -304
  32. package/dist/compat/extensions.js.map +1 -0
  33. package/dist/compat/utils.d.ts +151 -0
  34. package/dist/compat/utils.d.ts.map +1 -0
  35. package/dist/compat/utils.js +187 -206
  36. package/dist/compat/utils.js.map +1 -0
  37. package/dist/compat-BbNNmTM-.d.ts +1386 -0
  38. package/dist/compat-BbNNmTM-.d.ts.map +1 -0
  39. package/dist/compat-g__Pfy2S.js +734 -0
  40. package/dist/compat-g__Pfy2S.js.map +1 -0
  41. package/dist/compat.d.ts +2 -0
  42. package/dist/compat.js +1 -1036
  43. package/dist/error-CzkyXr1Q.d.ts +435 -0
  44. package/dist/error-CzkyXr1Q.d.ts.map +1 -0
  45. package/dist/errors-LQhmDVdd.js +2134 -0
  46. package/dist/errors-LQhmDVdd.js.map +1 -0
  47. package/dist/extensions-DumNm4dA.d.ts +102 -0
  48. package/dist/extensions-DumNm4dA.d.ts.map +1 -0
  49. package/dist/index.d.ts +243 -0
  50. package/dist/index.d.ts.map +1 -0
  51. package/dist/index.js +267 -339
  52. package/dist/index.js.map +1 -0
  53. package/dist/json-BXNPTlcH.js +1106 -0
  54. package/dist/json-BXNPTlcH.js.map +1 -0
  55. package/dist/model/-private.d.ts +3 -0
  56. package/dist/model/-private.js +3 -1
  57. package/dist/model/migration-support.d.ts +335 -0
  58. package/dist/model/migration-support.d.ts.map +1 -0
  59. package/dist/model/migration-support.js +504 -575
  60. package/dist/model/migration-support.js.map +1 -0
  61. package/dist/model-62hn5_PD.d.ts +3107 -0
  62. package/dist/model-62hn5_PD.d.ts.map +1 -0
  63. package/dist/model-BtIEHqYZ.js +683 -0
  64. package/dist/model-BtIEHqYZ.js.map +1 -0
  65. package/dist/model-fragments-Dpn9285r.js +334 -0
  66. package/dist/model-fragments-Dpn9285r.js.map +1 -0
  67. package/dist/model-fragments.d.ts +294 -0
  68. package/dist/model-fragments.d.ts.map +1 -0
  69. package/dist/model-fragments.js +2 -94
  70. package/dist/model.d.ts +404 -0
  71. package/dist/model.d.ts.map +1 -0
  72. package/dist/model.js +3 -670
  73. package/{declarations/adapter/rest.d.ts → dist/rest-0NhtexIq.d.ts} +183 -173
  74. package/dist/rest-0NhtexIq.d.ts.map +1 -0
  75. package/dist/runtime-BPCpkOf1-BgO3Lyjx.js +53 -0
  76. package/dist/runtime-BPCpkOf1-BgO3Lyjx.js.map +1 -0
  77. package/dist/schema-provider-DsKuB5Wb.js +1929 -0
  78. package/dist/schema-provider-DsKuB5Wb.js.map +1 -0
  79. package/{declarations → dist}/serializer/json-api.d.ts +3 -0
  80. package/dist/serializer/json-api.d.ts.map +1 -0
  81. package/dist/serializer/json-api.js +527 -519
  82. package/dist/serializer/json-api.js.map +1 -0
  83. package/{declarations → dist}/serializer/json.d.ts +3 -0
  84. package/dist/serializer/json.d.ts.map +1 -0
  85. package/dist/serializer/json.js +3 -6
  86. package/{declarations/serializer/-private/embedded-records-mixin.d.ts → dist/serializer/rest.d.ts} +56 -1
  87. package/dist/serializer/rest.d.ts.map +1 -0
  88. package/dist/serializer/rest.js +958 -1199
  89. package/dist/serializer/rest.js.map +1 -0
  90. package/dist/serializer/transform.d.ts +313 -0
  91. package/dist/serializer/transform.d.ts.map +1 -0
  92. package/dist/serializer/transform.js +283 -308
  93. package/dist/serializer/transform.js.map +1 -0
  94. package/dist/serializer.d.ts +151 -0
  95. package/dist/serializer.d.ts.map +1 -0
  96. package/dist/serializer.js +205 -205
  97. package/dist/serializer.js.map +1 -0
  98. package/dist/store-C4qYzXei.js +473 -0
  99. package/dist/store-C4qYzXei.js.map +1 -0
  100. package/dist/store.d.ts +13 -0
  101. package/dist/store.d.ts.map +1 -0
  102. package/dist/store.js +1 -641
  103. package/dist/unpkg/dev/-private-CrEyBE-B.js +189 -0
  104. package/dist/unpkg/dev/-private-CrEyBE-B.js.map +1 -0
  105. package/dist/unpkg/dev/-private-vOhxVExH.js +896 -0
  106. package/dist/unpkg/dev/-private-vOhxVExH.js.map +1 -0
  107. package/dist/unpkg/dev/adapter/-private.js +3 -1
  108. package/dist/unpkg/dev/adapter/error.js +421 -452
  109. package/dist/unpkg/dev/adapter/error.js.map +1 -0
  110. package/dist/unpkg/dev/adapter/json-api.js +239 -257
  111. package/dist/unpkg/dev/adapter/json-api.js.map +1 -0
  112. package/dist/unpkg/dev/adapter/rest.js +935 -1134
  113. package/dist/unpkg/dev/adapter/rest.js.map +1 -0
  114. package/dist/unpkg/dev/adapter-Cw0uSl8d.js +1129 -0
  115. package/dist/unpkg/dev/adapter-Cw0uSl8d.js.map +1 -0
  116. package/dist/unpkg/dev/adapter.js +1 -1247
  117. package/dist/unpkg/dev/compat/-private.js +3 -1
  118. package/dist/unpkg/dev/compat/builders.js +204 -247
  119. package/dist/unpkg/dev/compat/builders.js.map +1 -0
  120. package/dist/unpkg/dev/compat/extensions.js +264 -303
  121. package/dist/unpkg/dev/compat/extensions.js.map +1 -0
  122. package/dist/unpkg/dev/compat/utils.js +186 -205
  123. package/dist/unpkg/dev/compat/utils.js.map +1 -0
  124. package/dist/unpkg/dev/compat-CeyxXB7K.js +825 -0
  125. package/dist/unpkg/dev/compat-CeyxXB7K.js.map +1 -0
  126. package/dist/unpkg/dev/compat.js +1 -1178
  127. package/dist/unpkg/dev/errors-1javxsPo.js +2127 -0
  128. package/dist/unpkg/dev/errors-1javxsPo.js.map +1 -0
  129. package/dist/unpkg/dev/index.js +266 -344
  130. package/dist/unpkg/dev/index.js.map +1 -0
  131. package/dist/unpkg/dev/json-DDJji3V3.js +1104 -0
  132. package/dist/unpkg/dev/json-DDJji3V3.js.map +1 -0
  133. package/dist/unpkg/dev/model/-private.js +3 -1
  134. package/dist/unpkg/dev/model/migration-support.js +481 -549
  135. package/dist/unpkg/dev/model/migration-support.js.map +1 -0
  136. package/dist/unpkg/dev/model-C9Xb1R1x.js +655 -0
  137. package/dist/unpkg/dev/model-C9Xb1R1x.js.map +1 -0
  138. package/dist/unpkg/dev/model-fragments-Dpn9285r.js +334 -0
  139. package/dist/unpkg/dev/model-fragments-Dpn9285r.js.map +1 -0
  140. package/dist/unpkg/dev/model-fragments.js +2 -94
  141. package/dist/unpkg/dev/model.js +3 -681
  142. package/dist/unpkg/dev/runtime-BPCpkOf1-BgO3Lyjx.js +53 -0
  143. package/dist/unpkg/dev/runtime-BPCpkOf1-BgO3Lyjx.js.map +1 -0
  144. package/dist/unpkg/dev/schema-provider-Dy6c1J5f.js +1834 -0
  145. package/dist/unpkg/dev/schema-provider-Dy6c1J5f.js.map +1 -0
  146. package/dist/unpkg/dev/serializer/json-api.js +514 -634
  147. package/dist/unpkg/dev/serializer/json-api.js.map +1 -0
  148. package/dist/unpkg/dev/serializer/json.js +3 -4
  149. package/dist/unpkg/dev/serializer/rest.js +954 -1196
  150. package/dist/unpkg/dev/serializer/rest.js.map +1 -0
  151. package/dist/unpkg/dev/serializer/transform.js +283 -308
  152. package/dist/unpkg/dev/serializer/transform.js.map +1 -0
  153. package/dist/unpkg/dev/serializer.js +205 -205
  154. package/dist/unpkg/dev/serializer.js.map +1 -0
  155. package/dist/unpkg/dev/store-D_V8-HQl.js +472 -0
  156. package/dist/unpkg/dev/store-D_V8-HQl.js.map +1 -0
  157. package/dist/unpkg/dev/store.js +1 -649
  158. package/dist/unpkg/dev/util-dSKEgQDp.js +12 -0
  159. package/dist/unpkg/dev/util-dSKEgQDp.js.map +1 -0
  160. package/dist/unpkg/dev/utils-DYC9utVJ.js +11 -0
  161. package/dist/unpkg/dev/utils-DYC9utVJ.js.map +1 -0
  162. package/dist/unpkg/dev-deprecated/-private-CrEyBE-B.js +189 -0
  163. package/dist/unpkg/dev-deprecated/-private-CrEyBE-B.js.map +1 -0
  164. package/dist/unpkg/dev-deprecated/-private-vOhxVExH.js +896 -0
  165. package/dist/unpkg/dev-deprecated/-private-vOhxVExH.js.map +1 -0
  166. package/dist/unpkg/dev-deprecated/adapter/-private.js +3 -1
  167. package/dist/unpkg/dev-deprecated/adapter/error.js +421 -452
  168. package/dist/unpkg/dev-deprecated/adapter/error.js.map +1 -0
  169. package/dist/unpkg/dev-deprecated/adapter/json-api.js +239 -257
  170. package/dist/unpkg/dev-deprecated/adapter/json-api.js.map +1 -0
  171. package/dist/unpkg/dev-deprecated/adapter/rest.js +935 -1134
  172. package/dist/unpkg/dev-deprecated/adapter/rest.js.map +1 -0
  173. package/dist/unpkg/dev-deprecated/adapter-Cw0uSl8d.js +1129 -0
  174. package/dist/unpkg/dev-deprecated/adapter-Cw0uSl8d.js.map +1 -0
  175. package/dist/unpkg/dev-deprecated/adapter.js +1 -1247
  176. package/dist/unpkg/dev-deprecated/compat/-private.js +3 -1
  177. package/dist/unpkg/dev-deprecated/compat/builders.js +204 -247
  178. package/dist/unpkg/dev-deprecated/compat/builders.js.map +1 -0
  179. package/dist/unpkg/dev-deprecated/compat/extensions.js +264 -303
  180. package/dist/unpkg/dev-deprecated/compat/extensions.js.map +1 -0
  181. package/dist/unpkg/dev-deprecated/compat/utils.js +186 -205
  182. package/dist/unpkg/dev-deprecated/compat/utils.js.map +1 -0
  183. package/dist/unpkg/dev-deprecated/compat-DmAgB2NS.js +825 -0
  184. package/dist/unpkg/dev-deprecated/compat-DmAgB2NS.js.map +1 -0
  185. package/dist/unpkg/dev-deprecated/compat.js +1 -1178
  186. package/dist/unpkg/dev-deprecated/errors-C4_aOhY5.js +2128 -0
  187. package/dist/unpkg/dev-deprecated/errors-C4_aOhY5.js.map +1 -0
  188. package/dist/unpkg/dev-deprecated/index.js +266 -343
  189. package/dist/unpkg/dev-deprecated/index.js.map +1 -0
  190. package/dist/unpkg/dev-deprecated/json-DDJji3V3.js +1104 -0
  191. package/dist/unpkg/dev-deprecated/json-DDJji3V3.js.map +1 -0
  192. package/dist/unpkg/dev-deprecated/model/-private.js +3 -1
  193. package/dist/unpkg/dev-deprecated/model/migration-support.js +492 -566
  194. package/dist/unpkg/dev-deprecated/model/migration-support.js.map +1 -0
  195. package/dist/unpkg/dev-deprecated/model-CvSMYkgs.js +660 -0
  196. package/dist/unpkg/dev-deprecated/model-CvSMYkgs.js.map +1 -0
  197. package/dist/unpkg/dev-deprecated/model-fragments-Dpn9285r.js +334 -0
  198. package/dist/unpkg/dev-deprecated/model-fragments-Dpn9285r.js.map +1 -0
  199. package/dist/unpkg/dev-deprecated/model-fragments.js +2 -94
  200. package/dist/unpkg/dev-deprecated/model.js +3 -685
  201. package/dist/unpkg/dev-deprecated/runtime-BPCpkOf1-BgO3Lyjx.js +53 -0
  202. package/dist/unpkg/dev-deprecated/runtime-BPCpkOf1-BgO3Lyjx.js.map +1 -0
  203. package/dist/unpkg/dev-deprecated/schema-provider-Cqttza94.js +1835 -0
  204. package/dist/unpkg/dev-deprecated/schema-provider-Cqttza94.js.map +1 -0
  205. package/dist/unpkg/dev-deprecated/serializer/json-api.js +514 -634
  206. package/dist/unpkg/dev-deprecated/serializer/json-api.js.map +1 -0
  207. package/dist/unpkg/dev-deprecated/serializer/json.js +3 -4
  208. package/dist/unpkg/dev-deprecated/serializer/rest.js +954 -1196
  209. package/dist/unpkg/dev-deprecated/serializer/rest.js.map +1 -0
  210. package/dist/unpkg/dev-deprecated/serializer/transform.js +283 -308
  211. package/dist/unpkg/dev-deprecated/serializer/transform.js.map +1 -0
  212. package/dist/unpkg/dev-deprecated/serializer.js +205 -205
  213. package/dist/unpkg/dev-deprecated/serializer.js.map +1 -0
  214. package/dist/unpkg/dev-deprecated/store-BX7GvXC_.js +472 -0
  215. package/dist/unpkg/dev-deprecated/store-BX7GvXC_.js.map +1 -0
  216. package/dist/unpkg/dev-deprecated/store.js +1 -649
  217. package/dist/unpkg/dev-deprecated/util-9mgEBJ3W.js +18 -0
  218. package/dist/unpkg/dev-deprecated/util-9mgEBJ3W.js.map +1 -0
  219. package/dist/unpkg/dev-deprecated/utils-Cl86-e7F.js +17 -0
  220. package/dist/unpkg/dev-deprecated/utils-Cl86-e7F.js.map +1 -0
  221. package/dist/unpkg/prod/-private-B4bxoTuK.js +160 -0
  222. package/dist/unpkg/prod/-private-B4bxoTuK.js.map +1 -0
  223. package/dist/unpkg/prod/-private-SfXkkHKd.js +745 -0
  224. package/dist/unpkg/prod/-private-SfXkkHKd.js.map +1 -0
  225. package/dist/unpkg/prod/adapter/-private.js +3 -1
  226. package/dist/unpkg/prod/adapter/error.js +418 -447
  227. package/dist/unpkg/prod/adapter/error.js.map +1 -0
  228. package/dist/unpkg/prod/adapter/json-api.js +236 -252
  229. package/dist/unpkg/prod/adapter/json-api.js.map +1 -0
  230. package/dist/unpkg/prod/adapter/rest.js +911 -1097
  231. package/dist/unpkg/prod/adapter/rest.js.map +1 -0
  232. package/dist/unpkg/prod/adapter-Wnk7jizX.js +1111 -0
  233. package/dist/unpkg/prod/adapter-Wnk7jizX.js.map +1 -0
  234. package/dist/unpkg/prod/adapter.js +1 -1214
  235. package/dist/unpkg/prod/compat/-private.js +3 -1
  236. package/dist/unpkg/prod/compat/builders.js +165 -182
  237. package/dist/unpkg/prod/compat/builders.js.map +1 -0
  238. package/dist/unpkg/prod/compat/extensions.js +252 -283
  239. package/dist/unpkg/prod/compat/extensions.js.map +1 -0
  240. package/dist/unpkg/prod/compat/utils.js +183 -200
  241. package/dist/unpkg/prod/compat/utils.js.map +1 -0
  242. package/dist/unpkg/prod/compat-CJ87MzDn.js +547 -0
  243. package/dist/unpkg/prod/compat-CJ87MzDn.js.map +1 -0
  244. package/dist/unpkg/prod/compat.js +1 -742
  245. package/dist/unpkg/prod/errors-Co7YWHoA.js +1977 -0
  246. package/dist/unpkg/prod/errors-Co7YWHoA.js.map +1 -0
  247. package/dist/unpkg/prod/index.js +245 -308
  248. package/dist/unpkg/prod/index.js.map +1 -0
  249. package/dist/unpkg/prod/json-BD1kRTfB.js +1080 -0
  250. package/dist/unpkg/prod/json-BD1kRTfB.js.map +1 -0
  251. package/dist/unpkg/prod/model/-private.js +3 -1
  252. package/dist/unpkg/prod/model/migration-support.js +477 -542
  253. package/dist/unpkg/prod/model/migration-support.js.map +1 -0
  254. package/dist/unpkg/prod/model-BzoOmtka.js +603 -0
  255. package/dist/unpkg/prod/model-BzoOmtka.js.map +1 -0
  256. package/dist/unpkg/prod/model-fragments-Dpn9285r.js +334 -0
  257. package/dist/unpkg/prod/model-fragments-Dpn9285r.js.map +1 -0
  258. package/dist/unpkg/prod/model-fragments.js +2 -94
  259. package/dist/unpkg/prod/model.js +3 -596
  260. package/dist/unpkg/prod/runtime-BPCpkOf1-BgO3Lyjx.js +53 -0
  261. package/dist/unpkg/prod/runtime-BPCpkOf1-BgO3Lyjx.js.map +1 -0
  262. package/dist/unpkg/prod/schema-provider-CKZcqWyP.js +1622 -0
  263. package/dist/unpkg/prod/schema-provider-CKZcqWyP.js.map +1 -0
  264. package/dist/unpkg/prod/serializer/json-api.js +497 -592
  265. package/dist/unpkg/prod/serializer/json-api.js.map +1 -0
  266. package/dist/unpkg/prod/serializer/json.js +3 -4
  267. package/dist/unpkg/prod/serializer/rest.js +938 -1166
  268. package/dist/unpkg/prod/serializer/rest.js.map +1 -0
  269. package/dist/unpkg/prod/serializer/transform.js +283 -308
  270. package/dist/unpkg/prod/serializer/transform.js.map +1 -0
  271. package/dist/unpkg/prod/serializer.js +205 -205
  272. package/dist/unpkg/prod/serializer.js.map +1 -0
  273. package/dist/unpkg/prod/store-RGGc1ilN.js +392 -0
  274. package/dist/unpkg/prod/store-RGGc1ilN.js.map +1 -0
  275. package/dist/unpkg/prod/store.js +1 -517
  276. package/dist/unpkg/prod/util-dSKEgQDp.js +12 -0
  277. package/dist/unpkg/prod/util-dSKEgQDp.js.map +1 -0
  278. package/dist/unpkg/prod/utils-DYC9utVJ.js +11 -0
  279. package/dist/unpkg/prod/utils-DYC9utVJ.js.map +1 -0
  280. package/dist/unpkg/prod-deprecated/-private-B4bxoTuK.js +160 -0
  281. package/dist/unpkg/prod-deprecated/-private-B4bxoTuK.js.map +1 -0
  282. package/dist/unpkg/prod-deprecated/-private-SfXkkHKd.js +745 -0
  283. package/dist/unpkg/prod-deprecated/-private-SfXkkHKd.js.map +1 -0
  284. package/dist/unpkg/prod-deprecated/adapter/-private.js +3 -1
  285. package/dist/unpkg/prod-deprecated/adapter/error.js +418 -447
  286. package/dist/unpkg/prod-deprecated/adapter/error.js.map +1 -0
  287. package/dist/unpkg/prod-deprecated/adapter/json-api.js +236 -252
  288. package/dist/unpkg/prod-deprecated/adapter/json-api.js.map +1 -0
  289. package/dist/unpkg/prod-deprecated/adapter/rest.js +911 -1097
  290. package/dist/unpkg/prod-deprecated/adapter/rest.js.map +1 -0
  291. package/{declarations/adapter.d.ts → dist/unpkg/prod-deprecated/adapter-Wnk7jizX.js} +445 -110
  292. package/dist/unpkg/prod-deprecated/adapter-Wnk7jizX.js.map +1 -0
  293. package/dist/unpkg/prod-deprecated/adapter.js +1 -1214
  294. package/dist/unpkg/prod-deprecated/compat/-private.js +3 -1
  295. package/dist/unpkg/prod-deprecated/compat/builders.js +165 -182
  296. package/dist/unpkg/prod-deprecated/compat/builders.js.map +1 -0
  297. package/dist/unpkg/prod-deprecated/compat/extensions.js +252 -283
  298. package/dist/unpkg/prod-deprecated/compat/extensions.js.map +1 -0
  299. package/dist/unpkg/prod-deprecated/compat/utils.js +183 -200
  300. package/dist/unpkg/prod-deprecated/compat/utils.js.map +1 -0
  301. package/dist/unpkg/prod-deprecated/compat-CJ87MzDn.js +547 -0
  302. package/dist/unpkg/prod-deprecated/compat-CJ87MzDn.js.map +1 -0
  303. package/dist/unpkg/prod-deprecated/compat.js +1 -742
  304. package/dist/unpkg/prod-deprecated/errors-C-opde61.js +1978 -0
  305. package/dist/unpkg/prod-deprecated/errors-C-opde61.js.map +1 -0
  306. package/dist/unpkg/prod-deprecated/index.js +245 -307
  307. package/dist/unpkg/prod-deprecated/index.js.map +1 -0
  308. package/dist/unpkg/prod-deprecated/json-BD1kRTfB.js +1080 -0
  309. package/dist/unpkg/prod-deprecated/json-BD1kRTfB.js.map +1 -0
  310. package/dist/unpkg/prod-deprecated/model/-private.js +3 -1
  311. package/dist/unpkg/prod-deprecated/model/migration-support.js +488 -559
  312. package/dist/unpkg/prod-deprecated/model/migration-support.js.map +1 -0
  313. package/dist/unpkg/prod-deprecated/model-C0bTXzGn.js +604 -0
  314. package/dist/unpkg/prod-deprecated/model-C0bTXzGn.js.map +1 -0
  315. package/dist/unpkg/prod-deprecated/model-fragments-Dpn9285r.js +334 -0
  316. package/dist/unpkg/prod-deprecated/model-fragments-Dpn9285r.js.map +1 -0
  317. package/dist/unpkg/prod-deprecated/model-fragments.js +2 -94
  318. package/dist/unpkg/prod-deprecated/model.js +3 -599
  319. package/dist/unpkg/prod-deprecated/runtime-BPCpkOf1-BgO3Lyjx.js +53 -0
  320. package/dist/unpkg/prod-deprecated/runtime-BPCpkOf1-BgO3Lyjx.js.map +1 -0
  321. package/dist/unpkg/prod-deprecated/schema-provider-lH3qP5tw.js +1635 -0
  322. package/dist/unpkg/prod-deprecated/schema-provider-lH3qP5tw.js.map +1 -0
  323. package/dist/unpkg/prod-deprecated/serializer/json-api.js +497 -592
  324. package/dist/unpkg/prod-deprecated/serializer/json-api.js.map +1 -0
  325. package/dist/unpkg/prod-deprecated/serializer/json.js +3 -4
  326. package/dist/unpkg/prod-deprecated/serializer/rest.js +938 -1166
  327. package/dist/unpkg/prod-deprecated/serializer/rest.js.map +1 -0
  328. package/dist/unpkg/prod-deprecated/serializer/transform.js +283 -308
  329. package/dist/unpkg/prod-deprecated/serializer/transform.js.map +1 -0
  330. package/dist/unpkg/prod-deprecated/serializer.js +205 -205
  331. package/dist/unpkg/prod-deprecated/serializer.js.map +1 -0
  332. package/dist/unpkg/prod-deprecated/store-0L2CkYzh.js +392 -0
  333. package/dist/unpkg/prod-deprecated/store-0L2CkYzh.js.map +1 -0
  334. package/dist/unpkg/prod-deprecated/store.js +1 -517
  335. package/dist/unpkg/prod-deprecated/util-CQQdHX3n.js +14 -0
  336. package/dist/unpkg/prod-deprecated/util-CQQdHX3n.js.map +1 -0
  337. package/dist/unpkg/prod-deprecated/utils-BMI_JwFP.js +13 -0
  338. package/dist/unpkg/prod-deprecated/utils-BMI_JwFP.js.map +1 -0
  339. package/dist/util-Bnx8nm-D.js +29 -0
  340. package/dist/util-Bnx8nm-D.js.map +1 -0
  341. package/dist/utils-ZAytYep8.js +28 -0
  342. package/dist/utils-ZAytYep8.js.map +1 -0
  343. package/package.json +13 -13
  344. package/declarations/adapter/-private/build-url-mixin.d.ts +0 -189
  345. package/declarations/adapter/-private/fastboot-interface.d.ts +0 -8
  346. package/declarations/adapter/-private/utils/continue-on-reject.d.ts +0 -1
  347. package/declarations/adapter/-private/utils/determine-body-promise.d.ts +0 -4
  348. package/declarations/adapter/-private/utils/fetch.d.ts +0 -8
  349. package/declarations/adapter/-private/utils/parse-response-headers.d.ts +0 -1
  350. package/declarations/adapter/-private/utils/serialize-into-hash.d.ts +0 -6
  351. package/declarations/adapter/-private/utils/serialize-query-params.d.ts +0 -5
  352. package/declarations/adapter/-private.d.ts +0 -5
  353. package/declarations/adapter/error.d.ts +0 -431
  354. package/declarations/compat/-private.d.ts +0 -13
  355. package/declarations/compat/builders/find-all.d.ts +0 -34
  356. package/declarations/compat/builders/find-record.d.ts +0 -56
  357. package/declarations/compat/builders/query.d.ts +0 -66
  358. package/declarations/compat/builders/save-record.d.ts +0 -34
  359. package/declarations/compat/builders/utils.d.ts +0 -3
  360. package/declarations/compat/builders.d.ts +0 -14
  361. package/declarations/compat/extensions.d.ts +0 -99
  362. package/declarations/compat/legacy-network-handler/fetch-manager.d.ts +0 -55
  363. package/declarations/compat/legacy-network-handler/identifier-has-id.d.ts +0 -2
  364. package/declarations/compat/legacy-network-handler/legacy-data-fetch.d.ts +0 -11
  365. package/declarations/compat/legacy-network-handler/legacy-data-utils.d.ts +0 -5
  366. package/declarations/compat/legacy-network-handler/legacy-network-handler.d.ts +0 -9
  367. package/declarations/compat/legacy-network-handler/minimum-adapter-interface.d.ts +0 -528
  368. package/declarations/compat/legacy-network-handler/minimum-serializer-interface.d.ts +0 -220
  369. package/declarations/compat/legacy-network-handler/serializer-response.d.ts +0 -6
  370. package/declarations/compat/legacy-network-handler/snapshot-record-array.d.ts +0 -110
  371. package/declarations/compat/legacy-network-handler/snapshot.d.ts +0 -255
  372. package/declarations/compat/utils.d.ts +0 -148
  373. package/declarations/compat.d.ts +0 -189
  374. package/declarations/index.d.ts +0 -244
  375. package/declarations/model/-private/attr.d.ts +0 -191
  376. package/declarations/model/-private/belongs-to.d.ts +0 -195
  377. package/declarations/model/-private/debug/assert-polymorphic-type.d.ts +0 -5
  378. package/declarations/model/-private/errors.d.ts +0 -289
  379. package/declarations/model/-private/has-many.d.ts +0 -162
  380. package/declarations/model/-private/hooks.d.ts +0 -36
  381. package/declarations/model/-private/legacy-relationships-support.d.ts +0 -47
  382. package/declarations/model/-private/model-for-mixin.d.ts +0 -3
  383. package/declarations/model/-private/model-methods.d.ts +0 -39
  384. package/declarations/model/-private/model.d.ts +0 -1279
  385. package/declarations/model/-private/notify-changes.d.ts +0 -4
  386. package/declarations/model/-private/promise-belongs-to.d.ts +0 -40
  387. package/declarations/model/-private/promise-many-array.d.ts +0 -130
  388. package/declarations/model/-private/record-state.d.ts +0 -58
  389. package/declarations/model/-private/references/belongs-to.d.ts +0 -488
  390. package/declarations/model/-private/references/has-many.d.ts +0 -498
  391. package/declarations/model/-private/schema-provider.d.ts +0 -66
  392. package/declarations/model/-private/type-utils.d.ts +0 -57
  393. package/declarations/model/-private/util.d.ts +0 -5
  394. package/declarations/model/-private.d.ts +0 -8
  395. package/declarations/model/migration-support.d.ts +0 -306
  396. package/declarations/model-fragments/extensions/fragment-array.d.ts +0 -54
  397. package/declarations/model-fragments/extensions/fragment.d.ts +0 -50
  398. package/declarations/model-fragments/hooks/model-for.d.ts +0 -27
  399. package/declarations/model-fragments/index.d.ts +0 -13
  400. package/declarations/model-fragments/instance-initializers/fragment-extensions.d.ts +0 -15
  401. package/declarations/model-fragments/utilities/with-array-defaults.d.ts +0 -56
  402. package/declarations/model-fragments/utilities/with-fragment-array-defaults.d.ts +0 -38
  403. package/declarations/model-fragments/utilities/with-fragment-defaults.d.ts +0 -34
  404. package/declarations/model-fragments/utilities/with-legacy.d.ts +0 -11
  405. package/declarations/model-fragments.d.ts +0 -9
  406. package/declarations/model.d.ts +0 -49
  407. package/declarations/serializer/-private/transforms/boolean.d.ts +0 -60
  408. package/declarations/serializer/-private/transforms/date.d.ts +0 -42
  409. package/declarations/serializer/-private/transforms/number.d.ts +0 -42
  410. package/declarations/serializer/-private/transforms/string.d.ts +0 -42
  411. package/declarations/serializer/-private/transforms/transform.d.ts +0 -118
  412. package/declarations/serializer/-private/utils.d.ts +0 -27
  413. package/declarations/serializer/rest.d.ts +0 -51
  414. package/declarations/serializer/transform.d.ts +0 -5
  415. package/declarations/serializer.d.ts +0 -252
  416. package/declarations/store/-private.d.ts +0 -223
  417. package/declarations/store.d.ts +0 -10
  418. package/dist/-private-BG3bMiKp.js +0 -1211
  419. package/dist/errors-DZKA206g.js +0 -2589
  420. package/dist/hooks-BvOqJBei.js +0 -105
  421. package/dist/json-DU8m1pGf.js +0 -1336
  422. package/dist/model-for-CXkJw59V.js +0 -297
  423. package/dist/runtime-BPCpkOf1-BKOwiRJp.js +0 -65
  424. package/dist/schema-provider-DtPOp0d7.js +0 -2382
  425. package/dist/serialize-into-hash-DPZYoF-i.js +0 -261
  426. package/dist/unpkg/dev/-private-DtjBbEgy.js +0 -1206
  427. package/dist/unpkg/dev/errors-DngRU-If.js +0 -2585
  428. package/dist/unpkg/dev/hooks-UuiLxWxS.js +0 -104
  429. package/dist/unpkg/dev/json-K2Y9z6MN.js +0 -1333
  430. package/dist/unpkg/dev/model-for-CXkJw59V.js +0 -297
  431. package/dist/unpkg/dev/runtime-BPCpkOf1-BKOwiRJp.js +0 -65
  432. package/dist/unpkg/dev/schema-provider-0D7f-QOn.js +0 -2272
  433. package/dist/unpkg/dev/serialize-into-hash-B2xDbuo5.js +0 -259
  434. package/dist/unpkg/dev/util-DvanW33H.js +0 -20
  435. package/dist/unpkg/dev/utils-BhvS1iTS.js +0 -8
  436. package/dist/unpkg/dev-deprecated/-private-DtjBbEgy.js +0 -1206
  437. package/dist/unpkg/dev-deprecated/errors-Bzidntpi.js +0 -2588
  438. package/dist/unpkg/dev-deprecated/hooks-DVXhMW14.js +0 -104
  439. package/dist/unpkg/dev-deprecated/json-K2Y9z6MN.js +0 -1333
  440. package/dist/unpkg/dev-deprecated/model-for-CXkJw59V.js +0 -297
  441. package/dist/unpkg/dev-deprecated/runtime-BPCpkOf1-BKOwiRJp.js +0 -65
  442. package/dist/unpkg/dev-deprecated/schema-provider-ZwO5_fZw.js +0 -2292
  443. package/dist/unpkg/dev-deprecated/serialize-into-hash-B2xDbuo5.js +0 -259
  444. package/dist/unpkg/dev-deprecated/util-CWr5WQOT.js +0 -24
  445. package/dist/unpkg/dev-deprecated/utils-C9PJehtL.js +0 -12
  446. package/dist/unpkg/prod/-private-BdyZaGEh.js +0 -971
  447. package/dist/unpkg/prod/errors-B-PWeYlW.js +0 -2337
  448. package/dist/unpkg/prod/hooks-Bx-zBziU.js +0 -72
  449. package/dist/unpkg/prod/json-Qewu-UN9.js +0 -1300
  450. package/dist/unpkg/prod/model-for-CXkJw59V.js +0 -297
  451. package/dist/unpkg/prod/runtime-BPCpkOf1-BKOwiRJp.js +0 -65
  452. package/dist/unpkg/prod/schema-provider-BgRqk8i9.js +0 -1951
  453. package/dist/unpkg/prod/serialize-into-hash-DGlzQteF.js +0 -215
  454. package/dist/unpkg/prod/util-DvanW33H.js +0 -20
  455. package/dist/unpkg/prod/utils-BhvS1iTS.js +0 -8
  456. package/dist/unpkg/prod-deprecated/-private-BdyZaGEh.js +0 -971
  457. package/dist/unpkg/prod-deprecated/errors-Dl2J9d1D.js +0 -2340
  458. package/dist/unpkg/prod-deprecated/hooks-Cm60wWTf.js +0 -72
  459. package/dist/unpkg/prod-deprecated/json-Qewu-UN9.js +0 -1300
  460. package/dist/unpkg/prod-deprecated/model-for-CXkJw59V.js +0 -297
  461. package/dist/unpkg/prod-deprecated/runtime-BPCpkOf1-BKOwiRJp.js +0 -65
  462. package/dist/unpkg/prod-deprecated/schema-provider-Dh6tRqVI.js +0 -1980
  463. package/dist/unpkg/prod-deprecated/serialize-into-hash-DGlzQteF.js +0 -215
  464. package/dist/unpkg/prod-deprecated/util-B6cn-i93.js +0 -23
  465. package/dist/unpkg/prod-deprecated/utils-BUWwQwCh.js +0 -11
  466. package/dist/util-Dul6TZts.js +0 -35
  467. package/dist/utils-Cqw9eRj5.js +0 -23
@@ -1,1242 +1,1001 @@
1
- import { warn } from '@ember/debug';
2
- import { camelize, dasherize, singularize } from '@warp-drive/utilities/string';
3
- import { i as inverseForRelationship, J as JSONSerializer, c as coerceId } from "../json-DU8m1pGf.js";
4
- import { macroCondition, getGlobalConfig } from '@embroider/macros';
5
- import Mixin from '@ember/object/mixin';
6
-
7
- /* eslint-disable @typescript-eslint/no-unsafe-argument */
8
- /* eslint-disable @typescript-eslint/no-unsafe-return */
9
- /* eslint-disable @typescript-eslint/no-unsafe-call */
10
- /* eslint-disable @typescript-eslint/no-unsafe-member-access */
11
- /* eslint-disable @typescript-eslint/no-unsafe-assignment */
1
+ import { n as coerceId, r as inverseForRelationship, t as JSONSerializer } from "../json-BXNPTlcH.js";
2
+ import { warn } from "@ember/debug";
3
+ import { getGlobalConfig, macroCondition } from "@embroider/macros";
4
+ import { camelize, dasherize, singularize } from "@warp-drive/utilities/string";
5
+ import Mixin from "@ember/object/mixin";
12
6
 
7
+ //#region src/serializer/-private/embedded-records-mixin.ts
13
8
  /**
14
- ## Using Embedded Records
15
-
16
- `EmbeddedRecordsMixin` supports serializing embedded records.
17
-
18
- To set up embedded records, include the mixin when extending a serializer,
19
- then define and configure embedded (model) relationships.
9
+ ## Using Embedded Records
20
10
 
21
- Note that embedded records will serialize with the serializer for their model instead of the serializer in which they are defined.
11
+ `EmbeddedRecordsMixin` supports serializing embedded records.
22
12
 
23
- Note also that this mixin does not work with JSONAPISerializer because the JSON:API specification does not describe how to format embedded resources.
13
+ To set up embedded records, include the mixin when extending a serializer,
14
+ then define and configure embedded (model) relationships.
24
15
 
25
- Below is an example of a per-type serializer (`post` type).
16
+ Note that embedded records will serialize with the serializer for their model instead of the serializer in which they are defined.
26
17
 
27
- ```js [app/serializers/post.js]
28
- import RESTSerializer, { EmbeddedRecordsMixin } from '@warp-drive/legacy/serializer/rest';
18
+ Note also that this mixin does not work with JSONAPISerializer because the JSON:API specification does not describe how to format embedded resources.
29
19
 
30
- export default class PostSerializer extends RESTSerializer.extend(EmbeddedRecordsMixin) {
31
- attrs = {
32
- author: { embedded: 'always' },
33
- comments: { serialize: 'ids' }
34
- }
35
- }
36
- ```
37
- Note that this use of `{ embedded: 'always' }` is unrelated to
38
- the `{ embedded: 'always' }` that is defined as an option on `attr` as part of
39
- defining a model while working with the `ActiveModelSerializer`. Nevertheless,
40
- using `{ embedded: 'always' }` as an option to `attr` is not a valid way to set up
41
- embedded records.
20
+ Below is an example of a per-type serializer (`post` type).
42
21
 
43
- The `attrs` option for a resource `{ embedded: 'always' }` is shorthand for:
22
+ ```js [app/serializers/post.js]
23
+ import RESTSerializer, { EmbeddedRecordsMixin } from '@warp-drive/legacy/serializer/rest';
44
24
 
45
- ```js
46
- {
47
- serialize: 'records',
48
- deserialize: 'records'
49
- }
50
- ```
25
+ export default class PostSerializer extends RESTSerializer.extend(EmbeddedRecordsMixin) {
26
+ attrs = {
27
+ author: { embedded: 'always' },
28
+ comments: { serialize: 'ids' }
29
+ }
30
+ }
31
+ ```
32
+ Note that this use of `{ embedded: 'always' }` is unrelated to
33
+ the `{ embedded: 'always' }` that is defined as an option on `attr` as part of
34
+ defining a model while working with the `ActiveModelSerializer`. Nevertheless,
35
+ using `{ embedded: 'always' }` as an option to `attr` is not a valid way to set up
36
+ embedded records.
37
+
38
+ The `attrs` option for a resource `{ embedded: 'always' }` is shorthand for:
39
+
40
+ ```js
41
+ {
42
+ serialize: 'records',
43
+ deserialize: 'records'
44
+ }
45
+ ```
51
46
 
52
- ### Configuring Attrs
47
+ ### Configuring Attrs
53
48
 
54
- A resource's `attrs` option may be set to use `ids`, `records` or false for the
55
- `serialize` and `deserialize` settings.
49
+ A resource's `attrs` option may be set to use `ids`, `records` or false for the
50
+ `serialize` and `deserialize` settings.
56
51
 
57
- The `attrs` property can be set on the `ApplicationSerializer` or a per-type
58
- serializer.
52
+ The `attrs` property can be set on the `ApplicationSerializer` or a per-type
53
+ serializer.
59
54
 
60
- In the case where embedded JSON is expected while extracting a payload (reading)
61
- the setting is `deserialize: 'records'`, there is no need to use `ids` when
62
- extracting as that is the default behaviour without this mixin if you are using
63
- the vanilla `EmbeddedRecordsMixin`. Likewise, to embed JSON in the payload while
64
- serializing `serialize: 'records'` is the setting to use. There is an option of
65
- not embedding JSON in the serialized payload by using `serialize: 'ids'`. If you
66
- do not want the relationship sent at all, you can use `serialize: false`.
55
+ In the case where embedded JSON is expected while extracting a payload (reading)
56
+ the setting is `deserialize: 'records'`, there is no need to use `ids` when
57
+ extracting as that is the default behaviour without this mixin if you are using
58
+ the vanilla `EmbeddedRecordsMixin`. Likewise, to embed JSON in the payload while
59
+ serializing `serialize: 'records'` is the setting to use. There is an option of
60
+ not embedding JSON in the serialized payload by using `serialize: 'ids'`. If you
61
+ do not want the relationship sent at all, you can use `serialize: false`.
67
62
 
68
63
 
69
- ### EmbeddedRecordsMixin defaults
70
- If you do not overwrite `attrs` for a specific relationship, the `EmbeddedRecordsMixin`
71
- will behave in the following way:
64
+ ### EmbeddedRecordsMixin defaults
65
+ If you do not overwrite `attrs` for a specific relationship, the `EmbeddedRecordsMixin`
66
+ will behave in the following way:
72
67
 
73
- BelongsTo: `{ serialize: 'id', deserialize: 'id' }`
74
- HasMany: `{ serialize: false, deserialize: 'ids' }`
68
+ BelongsTo: `{ serialize: 'id', deserialize: 'id' }`
69
+ HasMany: `{ serialize: false, deserialize: 'ids' }`
75
70
 
76
- ### Model Relationships
71
+ ### Model Relationships
77
72
 
78
- Embedded records must have a model defined to be extracted and serialized. Note that
79
- when defining any relationships on your model such as `belongsTo` and `hasMany`, you
80
- should not both specify `async: true` and also indicate through the serializer's
81
- `attrs` attribute that the related model should be embedded for deserialization.
82
- If a model is declared embedded for deserialization (`embedded: 'always'` or `deserialize: 'records'`),
83
- then do not use `async: true`.
73
+ Embedded records must have a model defined to be extracted and serialized. Note that
74
+ when defining any relationships on your model such as `belongsTo` and `hasMany`, you
75
+ should not both specify `async: true` and also indicate through the serializer's
76
+ `attrs` attribute that the related model should be embedded for deserialization.
77
+ If a model is declared embedded for deserialization (`embedded: 'always'` or `deserialize: 'records'`),
78
+ then do not use `async: true`.
84
79
 
85
- To successfully extract and serialize embedded records the model relationships
86
- must be set up correctly. See the
87
- [defining relationships](https://guides.emberjs.com/current/models/relationships)
88
- section of the **Defining Models** guide page.
80
+ To successfully extract and serialize embedded records the model relationships
81
+ must be set up correctly. See the
82
+ [defining relationships](https://guides.emberjs.com/current/models/relationships)
83
+ section of the **Defining Models** guide page.
89
84
 
90
- Records without an `id` property are not considered embedded records, model
91
- instances must have an `id` property to be used with Ember Data.
85
+ Records without an `id` property are not considered embedded records, model
86
+ instances must have an `id` property to be used with Ember Data.
92
87
 
93
- ### Example JSON payloads, Models and Serializers
88
+ ### Example JSON payloads, Models and Serializers
94
89
 
95
- **When customizing a serializer it is important to grok what the customizations
96
- are. Please read the docs for the methods this mixin provides, in case you need
97
- to modify it to fit your specific needs.**
90
+ **When customizing a serializer it is important to grok what the customizations
91
+ are. Please read the docs for the methods this mixin provides, in case you need
92
+ to modify it to fit your specific needs.**
98
93
 
99
- @class EmbeddedRecordsMixin
100
- @public
94
+ @class EmbeddedRecordsMixin
95
+ @public
101
96
  */
102
97
  const EmbeddedRecordsMixin = Mixin.create({
103
- /**
104
- Normalize the record and recursively normalize/extract all the embedded records
105
- while pushing them into the store as they are encountered
106
- A payload with an attr configured for embedded records needs to be extracted:
107
- ```js
108
- {
109
- "post": {
110
- "id": "1"
111
- "title": "Rails is omakase",
112
- "comments": [{
113
- "id": "1",
114
- "body": "Rails is unagi"
115
- }, {
116
- "id": "2",
117
- "body": "Omakase O_o"
118
- }]
119
- }
120
- }
121
- ```
122
- @public
123
- @param {Model} typeClass
124
- @param {Object} hash to be normalized
125
- @param {String} prop the hash has been referenced by
126
- @return {Object} the normalized hash
127
- **/
128
- normalize(typeClass, hash, prop) {
129
- const normalizedHash = this._super(typeClass, hash, prop);
130
- return this._extractEmbeddedRecords(this, this.store, typeClass, normalizedHash);
131
- },
132
- keyForRelationship(key, typeClass, method) {
133
- if (method === 'serialize' && this.hasSerializeRecordsOption(key) || method === 'deserialize' && this.hasDeserializeRecordsOption(key)) {
134
- return this.keyForAttribute(key, method);
135
- } else {
136
- return this._super(key, typeClass, method) || key;
137
- }
138
- },
139
- /**
140
- Serialize `belongsTo` relationship when it is configured as an embedded object.
141
- This example of an author model belongs to a post model:
142
- ```js
143
- import Model, { attr, belongsTo } from '@warp-drive/legacy/model';
144
- Post = Model.extend({
145
- title: attr('string'),
146
- body: attr('string'),
147
- author: belongsTo('author')
148
- });
149
- Author = Model.extend({
150
- name: attr('string'),
151
- post: belongsTo('post')
152
- });
153
- ```
154
- Use a custom (type) serializer for the post model to configure embedded author
155
- ```js [app/serializers/post.js]
156
- import RESTSerializer, { EmbeddedRecordsMixin } from '@warp-drive/legacy/serializer/rest';
157
- export default class PostSerializer extends RESTSerializer.extend(EmbeddedRecordsMixin) {
158
- attrs = {
159
- author: { embedded: 'always' }
160
- }
161
- }
162
- ```
163
- A payload with an attribute configured for embedded records can serialize
164
- the records together under the root attribute's payload:
165
- ```js
166
- {
167
- "post": {
168
- "id": "1"
169
- "title": "Rails is omakase",
170
- "author": {
171
- "id": "2"
172
- "name": "dhh"
173
- }
174
- }
175
- }
176
- ```
177
- @public
178
- @param {Snapshot} snapshot
179
- @param {Object} json
180
- @param {Object} relationship
181
- */
182
- serializeBelongsTo(snapshot, json, relationship) {
183
- const attr = relationship.name;
184
- if (this.noSerializeOptionSpecified(attr)) {
185
- this._super(snapshot, json, relationship);
186
- return;
187
- }
188
- const includeIds = this.hasSerializeIdsOption(attr);
189
- const includeRecords = this.hasSerializeRecordsOption(attr);
190
- const embeddedSnapshot = snapshot.belongsTo(attr);
191
- if (includeIds) {
192
- const schema = this.store.modelFor(snapshot.modelName);
193
- let serializedKey = this._getMappedKey(relationship.name, schema);
194
- if (serializedKey === relationship.name && this.keyForRelationship) {
195
- serializedKey = this.keyForRelationship(relationship.name, relationship.kind, 'serialize');
196
- }
197
- if (!embeddedSnapshot) {
198
- json[serializedKey] = null;
199
- } else {
200
- json[serializedKey] = embeddedSnapshot.id;
201
- if (relationship.options.polymorphic) {
202
- this.serializePolymorphicType(snapshot, json, relationship);
203
- }
204
- }
205
- } else if (includeRecords) {
206
- this._serializeEmbeddedBelongsTo(snapshot, json, relationship);
207
- }
208
- },
209
- _serializeEmbeddedBelongsTo(snapshot, json, relationship) {
210
- const embeddedSnapshot = snapshot.belongsTo(relationship.name);
211
- const schema = this.store.modelFor(snapshot.modelName);
212
- let serializedKey = this._getMappedKey(relationship.name, schema);
213
- if (serializedKey === relationship.name && this.keyForRelationship) {
214
- serializedKey = this.keyForRelationship(relationship.name, relationship.kind, 'serialize');
215
- }
216
- if (!embeddedSnapshot) {
217
- json[serializedKey] = null;
218
- } else {
219
- json[serializedKey] = embeddedSnapshot.serialize({
220
- includeId: true
221
- });
222
- this.removeEmbeddedForeignKey(snapshot, embeddedSnapshot, relationship, json[serializedKey]);
223
- if (relationship.options.polymorphic) {
224
- this.serializePolymorphicType(snapshot, json, relationship);
225
- }
226
- }
227
- },
228
- /**
229
- Serializes `hasMany` relationships when it is configured as embedded objects.
230
- This example of a post model has many comments:
231
- ```js
232
- import Model, { attr, belongsTo, hasMany } from '@warp-drive/legacy/model';
233
- Post = Model.extend({
234
- title: attr('string'),
235
- body: attr('string'),
236
- comments: hasMany('comment')
237
- });
238
- Comment = Model.extend({
239
- body: attr('string'),
240
- post: belongsTo('post')
241
- });
242
- ```
243
- Use a custom (type) serializer for the post model to configure embedded comments
244
- ```js [app/serializers/post.js]
245
- import RESTSerializer, { EmbeddedRecordsMixin } from '@warp-drive/legacy/serializer/rest';
246
- export default class PostSerializer extends RESTSerializer.extend(EmbeddedRecordsMixin) {
247
- attrs = {
248
- comments: { embedded: 'always' }
249
- }
250
- }
251
- ```
252
- A payload with an attribute configured for embedded records can serialize
253
- the records together under the root attribute's payload:
254
- ```js
255
- {
256
- "post": {
257
- "id": "1"
258
- "title": "Rails is omakase",
259
- "body": "I want this for my ORM, I want that for my template language..."
260
- "comments": [{
261
- "id": "1",
262
- "body": "Rails is unagi"
263
- }, {
264
- "id": "2",
265
- "body": "Omakase O_o"
266
- }]
267
- }
268
- }
269
- ```
270
- The attrs options object can use more specific instruction for extracting and
271
- serializing. When serializing, an option to embed `ids`, `ids-and-types` or `records` can be set.
272
- When extracting the only option is `records`.
273
- So `{ embedded: 'always' }` is shorthand for:
274
- `{ serialize: 'records', deserialize: 'records' }`
275
- To embed the `ids` for a related object (using a hasMany relationship):
276
- ```js [app/serializers/post.js]
277
- import RESTSerializer, { EmbeddedRecordsMixin } from '@warp-drive/legacy/serializer/rest';
278
- export default class PostSerializer extends RESTSerializer.extend(EmbeddedRecordsMixin) {
279
- attrs = {
280
- comments: { serialize: 'ids', deserialize: 'records' }
281
- }
282
- }
283
- ```
284
- ```js
285
- {
286
- "post": {
287
- "id": "1"
288
- "title": "Rails is omakase",
289
- "body": "I want this for my ORM, I want that for my template language..."
290
- "comments": ["1", "2"]
291
- }
292
- }
293
- ```
294
- To embed the relationship as a collection of objects with `id` and `type` keys, set
295
- `ids-and-types` for the related object.
296
- This is particularly useful for polymorphic relationships where records don't share
297
- the same table and the `id` is not enough information.
298
- For example having a user that has many pets:
299
- ```js
300
- User = Model.extend({
301
- name: attr('string'),
302
- pets: hasMany('pet', { polymorphic: true })
303
- });
304
- Pet = Model.extend({
305
- name: attr('string'),
306
- });
307
- Cat = Pet.extend({
308
- // ...
309
- });
310
- Parrot = Pet.extend({
311
- // ...
312
- });
313
- ```
314
- ```js [app/serializers/user.js]
315
- import RESTSerializer, { EmbeddedRecordsMixin } from '@warp-drive/legacy/serializer/rest';
316
- export default class UserSerializer extends RESTSerializer.extend(EmbeddedRecordsMixin) {
317
- attrs = {
318
- pets: { serialize: 'ids-and-types', deserialize: 'records' }
319
- }
320
- }
321
- ```
322
- ```js
323
- {
324
- "user": {
325
- "id": "1"
326
- "name": "Bertin Osborne",
327
- "pets": [
328
- { "id": "1", "type": "Cat" },
329
- { "id": "1", "type": "Parrot"}
330
- ]
331
- }
332
- }
333
- ```
334
- @public
335
- @param {Snapshot} snapshot
336
- @param {Object} json
337
- @param {Object} relationship
338
- */
339
- serializeHasMany(snapshot, json, relationship) {
340
- const attr = relationship.name;
341
- if (this.noSerializeOptionSpecified(attr)) {
342
- this._super(snapshot, json, relationship);
343
- return;
344
- }
345
- if (this.hasSerializeIdsOption(attr)) {
346
- const schema = this.store.modelFor(snapshot.modelName);
347
- let serializedKey = this._getMappedKey(relationship.name, schema);
348
- if (serializedKey === relationship.name && this.keyForRelationship) {
349
- serializedKey = this.keyForRelationship(relationship.name, relationship.kind, 'serialize');
350
- }
351
- json[serializedKey] = snapshot.hasMany(attr, {
352
- ids: true
353
- });
354
- } else if (this.hasSerializeRecordsOption(attr)) {
355
- this._serializeEmbeddedHasMany(snapshot, json, relationship);
356
- } else {
357
- if (this.hasSerializeIdsAndTypesOption(attr)) {
358
- this._serializeHasManyAsIdsAndTypes(snapshot, json, relationship);
359
- }
360
- }
361
- },
362
- /*
363
- Serializes a hasMany relationship as an array of objects containing only `id` and `type`
364
- keys.
365
- This has its use case on polymorphic hasMany relationships where the server is not storing
366
- all records in the same table using STI, and therefore the `id` is not enough information
367
- TODO: Make the default in Ember-data 3.0??
368
- */
369
- _serializeHasManyAsIdsAndTypes(snapshot, json, relationship) {
370
- const serializedKey = this.keyForAttribute(relationship.name, 'serialize');
371
- const hasMany = snapshot.hasMany(relationship.name) || [];
372
- json[serializedKey] = hasMany.map(function (recordSnapshot) {
373
- //
374
- // I'm sure I'm being utterly naive here. Probably id is a configurable property and
375
- // type too, and the modelName has to be normalized somehow.
376
- //
377
- return {
378
- id: recordSnapshot.id,
379
- type: recordSnapshot.modelName
380
- };
381
- });
382
- },
383
- _serializeEmbeddedHasMany(snapshot, json, relationship) {
384
- const schema = this.store.modelFor(snapshot.modelName);
385
- let serializedKey = this._getMappedKey(relationship.name, schema);
386
- if (serializedKey === relationship.name && this.keyForRelationship) {
387
- serializedKey = this.keyForRelationship(relationship.name, relationship.kind, 'serialize');
388
- }
389
- warn(`The embedded relationship '${serializedKey}' is undefined for '${snapshot.modelName}' with id '${snapshot.id}'. Please include it in your original payload.`, typeof snapshot.hasMany(relationship.name) !== 'undefined', {
390
- id: 'ds.serializer.embedded-relationship-undefined'
391
- });
392
- json[serializedKey] = this._generateSerializedHasMany(snapshot, relationship);
393
- },
394
- /*
395
- Returns an array of embedded records serialized to JSON
396
- */
397
- _generateSerializedHasMany(snapshot, relationship) {
398
- const hasMany = snapshot.hasMany(relationship.name) || [];
399
- const ret = new Array(hasMany.length);
400
- for (let i = 0; i < hasMany.length; i++) {
401
- const embeddedSnapshot = hasMany[i];
402
- const embeddedJson = embeddedSnapshot.serialize({
403
- includeId: true
404
- });
405
- this.removeEmbeddedForeignKey(snapshot, embeddedSnapshot, relationship, embeddedJson);
406
- ret[i] = embeddedJson;
407
- }
408
- return ret;
409
- },
410
- /**
411
- When serializing an embedded record, modify the property (in the `JSON` payload)
412
- that refers to the parent record (foreign key for the relationship).
413
- Serializing a `belongsTo` relationship removes the property that refers to the
414
- parent record
415
- Serializing a `hasMany` relationship does not remove the property that refers to
416
- the parent record.
417
- @public
418
- @param {Snapshot} snapshot
419
- @param {Snapshot} embeddedSnapshot
420
- @param {Object} relationship
421
- @param {Object} json
422
- */
423
- removeEmbeddedForeignKey(snapshot, embeddedSnapshot, relationship, json) {
424
- if (relationship.kind === 'belongsTo') {
425
- const parentRecord = inverseForRelationship(this.store, snapshot.modelName, relationship.name);
426
- if (parentRecord) {
427
- const name = parentRecord.name;
428
- const embeddedSerializer = this.store.serializerFor(embeddedSnapshot.modelName);
429
- const parentKey = embeddedSerializer.keyForRelationship(name, parentRecord.kind, 'deserialize');
430
- if (parentKey) {
431
- delete json[parentKey];
432
- }
433
- }
434
- } /*else if (relationship.kind === 'hasMany') {
435
- return;
436
- }*/
437
- },
438
- // checks config for attrs option to embedded (always) - serialize and deserialize
439
- hasEmbeddedAlwaysOption(attr) {
440
- const option = this.attrsOption(attr);
441
- return option && option.embedded === 'always';
442
- },
443
- // checks config for attrs option to serialize ids
444
- hasSerializeRecordsOption(attr) {
445
- const alwaysEmbed = this.hasEmbeddedAlwaysOption(attr);
446
- const option = this.attrsOption(attr);
447
- return alwaysEmbed || option && option.serialize === 'records';
448
- },
449
- // checks config for attrs option to serialize records
450
- hasSerializeIdsOption(attr) {
451
- const option = this.attrsOption(attr);
452
- return option && (option.serialize === 'ids' || option.serialize === 'id');
453
- },
454
- // checks config for attrs option to serialize records as objects containing id and types
455
- hasSerializeIdsAndTypesOption(attr) {
456
- const option = this.attrsOption(attr);
457
- return option && (option.serialize === 'ids-and-types' || option.serialize === 'id-and-type');
458
- },
459
- // checks config for attrs option to serialize records
460
- noSerializeOptionSpecified(attr) {
461
- const option = this.attrsOption(attr);
462
- return !(option && (option.serialize || option.embedded));
463
- },
464
- // checks config for attrs option to deserialize records
465
- // a defined option object for a resource is treated the same as
466
- // `deserialize: 'records'`
467
- hasDeserializeRecordsOption(attr) {
468
- const alwaysEmbed = this.hasEmbeddedAlwaysOption(attr);
469
- const option = this.attrsOption(attr);
470
- return alwaysEmbed || option && option.deserialize === 'records';
471
- },
472
- attrsOption(attr) {
473
- const attrs = this.attrs;
474
- return attrs && (attrs[camelize(attr)] || attrs[attr]);
475
- },
476
- /**
477
- @private
478
- */
479
- _extractEmbeddedRecords(serializer, store, typeClass, partial) {
480
- typeClass.eachRelationship((key, relationship) => {
481
- if (serializer.hasDeserializeRecordsOption(key)) {
482
- if (relationship.kind === 'hasMany') {
483
- this._extractEmbeddedHasMany(store, key, partial, relationship);
484
- }
485
- if (relationship.kind === 'belongsTo') {
486
- this._extractEmbeddedBelongsTo(store, key, partial, relationship);
487
- }
488
- }
489
- });
490
- return partial;
491
- },
492
- /**
493
- @private
494
- */
495
- _extractEmbeddedHasMany(store, key, hash, relationshipMeta) {
496
- const relationshipHash = hash.data?.relationships?.[key]?.data;
497
- if (!relationshipHash) {
498
- return;
499
- }
500
- const hasMany = new Array(relationshipHash.length);
501
- for (let i = 0; i < relationshipHash.length; i++) {
502
- const item = relationshipHash[i];
503
- const {
504
- data,
505
- included
506
- } = this._normalizeEmbeddedRelationship(store, relationshipMeta, item);
507
- hash.included = hash.included || [];
508
- hash.included.push(data);
509
- if (included) {
510
- hash.included = hash.included.concat(included);
511
- }
512
- hasMany[i] = {
513
- id: data.id,
514
- type: data.type
515
- };
516
- if (data.lid) {
517
- hasMany[i].lid = data.lid;
518
- }
519
- }
520
- const relationship = {
521
- data: hasMany
522
- };
523
- hash.data.relationships[key] = relationship;
524
- },
525
- /**
526
- @private
527
- */
528
- _extractEmbeddedBelongsTo(store, key, hash, relationshipMeta) {
529
- const relationshipHash = hash.data?.relationships?.[key]?.data;
530
- if (!relationshipHash) {
531
- return;
532
- }
533
- const {
534
- data,
535
- included
536
- } = this._normalizeEmbeddedRelationship(store, relationshipMeta, relationshipHash);
537
- hash.included = hash.included || [];
538
- hash.included.push(data);
539
- if (included) {
540
- hash.included = hash.included.concat(included);
541
- }
542
- const belongsTo = {
543
- id: data.id,
544
- type: data.type
545
- };
546
- const relationship = {
547
- data: belongsTo
548
- };
549
- if (data.lid) {
550
- // @ts-expect-error
551
- belongsTo.lid = data.lid;
552
- }
553
- hash.data.relationships[key] = relationship;
554
- },
555
- /**
556
- @private
557
- */
558
- _normalizeEmbeddedRelationship(store, relationshipMeta, relationshipHash) {
559
- let modelName = relationshipMeta.type;
560
- if (relationshipMeta.options.polymorphic) {
561
- modelName = relationshipHash.type;
562
- }
563
- const modelClass = store.modelFor(modelName);
564
- const serializer = store.serializerFor(modelName);
565
- return serializer.normalize(modelClass, relationshipHash, null);
566
- },
567
- isEmbeddedRecordsMixin: true
98
+ /**
99
+ Normalize the record and recursively normalize/extract all the embedded records
100
+ while pushing them into the store as they are encountered
101
+ A payload with an attr configured for embedded records needs to be extracted:
102
+ ```js
103
+ {
104
+ "post": {
105
+ "id": "1"
106
+ "title": "Rails is omakase",
107
+ "comments": [{
108
+ "id": "1",
109
+ "body": "Rails is unagi"
110
+ }, {
111
+ "id": "2",
112
+ "body": "Omakase O_o"
113
+ }]
114
+ }
115
+ }
116
+ ```
117
+ @public
118
+ @param {Model} typeClass
119
+ @param {Object} hash to be normalized
120
+ @param {String} prop the hash has been referenced by
121
+ @return {Object} the normalized hash
122
+ **/
123
+ normalize(typeClass, hash, prop) {
124
+ const normalizedHash = this._super(typeClass, hash, prop);
125
+ return this._extractEmbeddedRecords(this, this.store, typeClass, normalizedHash);
126
+ },
127
+ keyForRelationship(key, typeClass, method) {
128
+ if (method === "serialize" && this.hasSerializeRecordsOption(key) || method === "deserialize" && this.hasDeserializeRecordsOption(key)) return this.keyForAttribute(key, method);
129
+ else return this._super(key, typeClass, method) || key;
130
+ },
131
+ /**
132
+ Serialize `belongsTo` relationship when it is configured as an embedded object.
133
+ This example of an author model belongs to a post model:
134
+ ```js
135
+ import Model, { attr, belongsTo } from '@warp-drive/legacy/model';
136
+ Post = Model.extend({
137
+ title: attr('string'),
138
+ body: attr('string'),
139
+ author: belongsTo('author')
140
+ });
141
+ Author = Model.extend({
142
+ name: attr('string'),
143
+ post: belongsTo('post')
144
+ });
145
+ ```
146
+ Use a custom (type) serializer for the post model to configure embedded author
147
+ ```js [app/serializers/post.js]
148
+ import RESTSerializer, { EmbeddedRecordsMixin } from '@warp-drive/legacy/serializer/rest';
149
+ export default class PostSerializer extends RESTSerializer.extend(EmbeddedRecordsMixin) {
150
+ attrs = {
151
+ author: { embedded: 'always' }
152
+ }
153
+ }
154
+ ```
155
+ A payload with an attribute configured for embedded records can serialize
156
+ the records together under the root attribute's payload:
157
+ ```js
158
+ {
159
+ "post": {
160
+ "id": "1"
161
+ "title": "Rails is omakase",
162
+ "author": {
163
+ "id": "2"
164
+ "name": "dhh"
165
+ }
166
+ }
167
+ }
168
+ ```
169
+ @public
170
+ @param {Snapshot} snapshot
171
+ @param {Object} json
172
+ @param {Object} relationship
173
+ */
174
+ serializeBelongsTo(snapshot, json, relationship) {
175
+ const attr = relationship.name;
176
+ if (this.noSerializeOptionSpecified(attr)) {
177
+ this._super(snapshot, json, relationship);
178
+ return;
179
+ }
180
+ const includeIds = this.hasSerializeIdsOption(attr);
181
+ const includeRecords = this.hasSerializeRecordsOption(attr);
182
+ const embeddedSnapshot = snapshot.belongsTo(attr);
183
+ if (includeIds) {
184
+ const schema = this.store.modelFor(snapshot.modelName);
185
+ let serializedKey = this._getMappedKey(relationship.name, schema);
186
+ if (serializedKey === relationship.name && this.keyForRelationship) serializedKey = this.keyForRelationship(relationship.name, relationship.kind, "serialize");
187
+ if (!embeddedSnapshot) json[serializedKey] = null;
188
+ else {
189
+ json[serializedKey] = embeddedSnapshot.id;
190
+ if (relationship.options.polymorphic) this.serializePolymorphicType(snapshot, json, relationship);
191
+ }
192
+ } else if (includeRecords) this._serializeEmbeddedBelongsTo(snapshot, json, relationship);
193
+ },
194
+ _serializeEmbeddedBelongsTo(snapshot, json, relationship) {
195
+ const embeddedSnapshot = snapshot.belongsTo(relationship.name);
196
+ const schema = this.store.modelFor(snapshot.modelName);
197
+ let serializedKey = this._getMappedKey(relationship.name, schema);
198
+ if (serializedKey === relationship.name && this.keyForRelationship) serializedKey = this.keyForRelationship(relationship.name, relationship.kind, "serialize");
199
+ if (!embeddedSnapshot) json[serializedKey] = null;
200
+ else {
201
+ json[serializedKey] = embeddedSnapshot.serialize({ includeId: true });
202
+ this.removeEmbeddedForeignKey(snapshot, embeddedSnapshot, relationship, json[serializedKey]);
203
+ if (relationship.options.polymorphic) this.serializePolymorphicType(snapshot, json, relationship);
204
+ }
205
+ },
206
+ /**
207
+ Serializes `hasMany` relationships when it is configured as embedded objects.
208
+ This example of a post model has many comments:
209
+ ```js
210
+ import Model, { attr, belongsTo, hasMany } from '@warp-drive/legacy/model';
211
+ Post = Model.extend({
212
+ title: attr('string'),
213
+ body: attr('string'),
214
+ comments: hasMany('comment')
215
+ });
216
+ Comment = Model.extend({
217
+ body: attr('string'),
218
+ post: belongsTo('post')
219
+ });
220
+ ```
221
+ Use a custom (type) serializer for the post model to configure embedded comments
222
+ ```js [app/serializers/post.js]
223
+ import RESTSerializer, { EmbeddedRecordsMixin } from '@warp-drive/legacy/serializer/rest';
224
+ export default class PostSerializer extends RESTSerializer.extend(EmbeddedRecordsMixin) {
225
+ attrs = {
226
+ comments: { embedded: 'always' }
227
+ }
228
+ }
229
+ ```
230
+ A payload with an attribute configured for embedded records can serialize
231
+ the records together under the root attribute's payload:
232
+ ```js
233
+ {
234
+ "post": {
235
+ "id": "1"
236
+ "title": "Rails is omakase",
237
+ "body": "I want this for my ORM, I want that for my template language..."
238
+ "comments": [{
239
+ "id": "1",
240
+ "body": "Rails is unagi"
241
+ }, {
242
+ "id": "2",
243
+ "body": "Omakase O_o"
244
+ }]
245
+ }
246
+ }
247
+ ```
248
+ The attrs options object can use more specific instruction for extracting and
249
+ serializing. When serializing, an option to embed `ids`, `ids-and-types` or `records` can be set.
250
+ When extracting the only option is `records`.
251
+ So `{ embedded: 'always' }` is shorthand for:
252
+ `{ serialize: 'records', deserialize: 'records' }`
253
+ To embed the `ids` for a related object (using a hasMany relationship):
254
+ ```js [app/serializers/post.js]
255
+ import RESTSerializer, { EmbeddedRecordsMixin } from '@warp-drive/legacy/serializer/rest';
256
+ export default class PostSerializer extends RESTSerializer.extend(EmbeddedRecordsMixin) {
257
+ attrs = {
258
+ comments: { serialize: 'ids', deserialize: 'records' }
259
+ }
260
+ }
261
+ ```
262
+ ```js
263
+ {
264
+ "post": {
265
+ "id": "1"
266
+ "title": "Rails is omakase",
267
+ "body": "I want this for my ORM, I want that for my template language..."
268
+ "comments": ["1", "2"]
269
+ }
270
+ }
271
+ ```
272
+ To embed the relationship as a collection of objects with `id` and `type` keys, set
273
+ `ids-and-types` for the related object.
274
+ This is particularly useful for polymorphic relationships where records don't share
275
+ the same table and the `id` is not enough information.
276
+ For example having a user that has many pets:
277
+ ```js
278
+ User = Model.extend({
279
+ name: attr('string'),
280
+ pets: hasMany('pet', { polymorphic: true })
281
+ });
282
+ Pet = Model.extend({
283
+ name: attr('string'),
284
+ });
285
+ Cat = Pet.extend({
286
+ // ...
287
+ });
288
+ Parrot = Pet.extend({
289
+ // ...
290
+ });
291
+ ```
292
+ ```js [app/serializers/user.js]
293
+ import RESTSerializer, { EmbeddedRecordsMixin } from '@warp-drive/legacy/serializer/rest';
294
+ export default class UserSerializer extends RESTSerializer.extend(EmbeddedRecordsMixin) {
295
+ attrs = {
296
+ pets: { serialize: 'ids-and-types', deserialize: 'records' }
297
+ }
298
+ }
299
+ ```
300
+ ```js
301
+ {
302
+ "user": {
303
+ "id": "1"
304
+ "name": "Bertin Osborne",
305
+ "pets": [
306
+ { "id": "1", "type": "Cat" },
307
+ { "id": "1", "type": "Parrot"}
308
+ ]
309
+ }
310
+ }
311
+ ```
312
+ @public
313
+ @param {Snapshot} snapshot
314
+ @param {Object} json
315
+ @param {Object} relationship
316
+ */
317
+ serializeHasMany(snapshot, json, relationship) {
318
+ const attr = relationship.name;
319
+ if (this.noSerializeOptionSpecified(attr)) {
320
+ this._super(snapshot, json, relationship);
321
+ return;
322
+ }
323
+ if (this.hasSerializeIdsOption(attr)) {
324
+ const schema = this.store.modelFor(snapshot.modelName);
325
+ let serializedKey = this._getMappedKey(relationship.name, schema);
326
+ if (serializedKey === relationship.name && this.keyForRelationship) serializedKey = this.keyForRelationship(relationship.name, relationship.kind, "serialize");
327
+ json[serializedKey] = snapshot.hasMany(attr, { ids: true });
328
+ } else if (this.hasSerializeRecordsOption(attr)) this._serializeEmbeddedHasMany(snapshot, json, relationship);
329
+ else if (this.hasSerializeIdsAndTypesOption(attr)) this._serializeHasManyAsIdsAndTypes(snapshot, json, relationship);
330
+ },
331
+ _serializeHasManyAsIdsAndTypes(snapshot, json, relationship) {
332
+ const serializedKey = this.keyForAttribute(relationship.name, "serialize");
333
+ json[serializedKey] = (snapshot.hasMany(relationship.name) || []).map(function(recordSnapshot) {
334
+ return {
335
+ id: recordSnapshot.id,
336
+ type: recordSnapshot.modelName
337
+ };
338
+ });
339
+ },
340
+ _serializeEmbeddedHasMany(snapshot, json, relationship) {
341
+ const schema = this.store.modelFor(snapshot.modelName);
342
+ let serializedKey = this._getMappedKey(relationship.name, schema);
343
+ if (serializedKey === relationship.name && this.keyForRelationship) serializedKey = this.keyForRelationship(relationship.name, relationship.kind, "serialize");
344
+ warn(`The embedded relationship '${serializedKey}' is undefined for '${snapshot.modelName}' with id '${snapshot.id}'. Please include it in your original payload.`, typeof snapshot.hasMany(relationship.name) !== "undefined", { id: "ds.serializer.embedded-relationship-undefined" });
345
+ json[serializedKey] = this._generateSerializedHasMany(snapshot, relationship);
346
+ },
347
+ _generateSerializedHasMany(snapshot, relationship) {
348
+ const hasMany = snapshot.hasMany(relationship.name) || [];
349
+ const ret = new Array(hasMany.length);
350
+ for (let i = 0; i < hasMany.length; i++) {
351
+ const embeddedSnapshot = hasMany[i];
352
+ const embeddedJson = embeddedSnapshot.serialize({ includeId: true });
353
+ this.removeEmbeddedForeignKey(snapshot, embeddedSnapshot, relationship, embeddedJson);
354
+ ret[i] = embeddedJson;
355
+ }
356
+ return ret;
357
+ },
358
+ /**
359
+ When serializing an embedded record, modify the property (in the `JSON` payload)
360
+ that refers to the parent record (foreign key for the relationship).
361
+ Serializing a `belongsTo` relationship removes the property that refers to the
362
+ parent record
363
+ Serializing a `hasMany` relationship does not remove the property that refers to
364
+ the parent record.
365
+ @public
366
+ @param {Snapshot} snapshot
367
+ @param {Snapshot} embeddedSnapshot
368
+ @param {Object} relationship
369
+ @param {Object} json
370
+ */
371
+ removeEmbeddedForeignKey(snapshot, embeddedSnapshot, relationship, json) {
372
+ if (relationship.kind === "belongsTo") {
373
+ const parentRecord = inverseForRelationship(this.store, snapshot.modelName, relationship.name);
374
+ if (parentRecord) {
375
+ const name = parentRecord.name;
376
+ const parentKey = this.store.serializerFor(embeddedSnapshot.modelName).keyForRelationship(name, parentRecord.kind, "deserialize");
377
+ if (parentKey) delete json[parentKey];
378
+ }
379
+ }
380
+ },
381
+ hasEmbeddedAlwaysOption(attr) {
382
+ const option = this.attrsOption(attr);
383
+ return option && option.embedded === "always";
384
+ },
385
+ hasSerializeRecordsOption(attr) {
386
+ const alwaysEmbed = this.hasEmbeddedAlwaysOption(attr);
387
+ const option = this.attrsOption(attr);
388
+ return alwaysEmbed || option && option.serialize === "records";
389
+ },
390
+ hasSerializeIdsOption(attr) {
391
+ const option = this.attrsOption(attr);
392
+ return option && (option.serialize === "ids" || option.serialize === "id");
393
+ },
394
+ hasSerializeIdsAndTypesOption(attr) {
395
+ const option = this.attrsOption(attr);
396
+ return option && (option.serialize === "ids-and-types" || option.serialize === "id-and-type");
397
+ },
398
+ noSerializeOptionSpecified(attr) {
399
+ const option = this.attrsOption(attr);
400
+ return !(option && (option.serialize || option.embedded));
401
+ },
402
+ hasDeserializeRecordsOption(attr) {
403
+ const alwaysEmbed = this.hasEmbeddedAlwaysOption(attr);
404
+ const option = this.attrsOption(attr);
405
+ return alwaysEmbed || option && option.deserialize === "records";
406
+ },
407
+ attrsOption(attr) {
408
+ const attrs = this.attrs;
409
+ return attrs && (attrs[camelize(attr)] || attrs[attr]);
410
+ },
411
+ /**
412
+ @private
413
+ */
414
+ _extractEmbeddedRecords(serializer, store, typeClass, partial) {
415
+ typeClass.eachRelationship((key, relationship) => {
416
+ if (serializer.hasDeserializeRecordsOption(key)) {
417
+ if (relationship.kind === "hasMany") this._extractEmbeddedHasMany(store, key, partial, relationship);
418
+ if (relationship.kind === "belongsTo") this._extractEmbeddedBelongsTo(store, key, partial, relationship);
419
+ }
420
+ });
421
+ return partial;
422
+ },
423
+ /**
424
+ @private
425
+ */
426
+ _extractEmbeddedHasMany(store, key, hash, relationshipMeta) {
427
+ const relationshipHash = hash.data?.relationships?.[key]?.data;
428
+ if (!relationshipHash) return;
429
+ const hasMany = new Array(relationshipHash.length);
430
+ for (let i = 0; i < relationshipHash.length; i++) {
431
+ const item = relationshipHash[i];
432
+ const { data, included } = this._normalizeEmbeddedRelationship(store, relationshipMeta, item);
433
+ hash.included = hash.included || [];
434
+ hash.included.push(data);
435
+ if (included) hash.included = hash.included.concat(included);
436
+ hasMany[i] = {
437
+ id: data.id,
438
+ type: data.type
439
+ };
440
+ if (data.lid) hasMany[i].lid = data.lid;
441
+ }
442
+ const relationship = { data: hasMany };
443
+ hash.data.relationships[key] = relationship;
444
+ },
445
+ /**
446
+ @private
447
+ */
448
+ _extractEmbeddedBelongsTo(store, key, hash, relationshipMeta) {
449
+ const relationshipHash = hash.data?.relationships?.[key]?.data;
450
+ if (!relationshipHash) return;
451
+ const { data, included } = this._normalizeEmbeddedRelationship(store, relationshipMeta, relationshipHash);
452
+ hash.included = hash.included || [];
453
+ hash.included.push(data);
454
+ if (included) hash.included = hash.included.concat(included);
455
+ const belongsTo = {
456
+ id: data.id,
457
+ type: data.type
458
+ };
459
+ const relationship = { data: belongsTo };
460
+ if (data.lid) belongsTo.lid = data.lid;
461
+ hash.data.relationships[key] = relationship;
462
+ },
463
+ /**
464
+ @private
465
+ */
466
+ _normalizeEmbeddedRelationship(store, relationshipMeta, relationshipHash) {
467
+ let modelName = relationshipMeta.type;
468
+ if (relationshipMeta.options.polymorphic) modelName = relationshipHash.type;
469
+ const modelClass = store.modelFor(modelName);
470
+ return store.serializerFor(modelName).normalize(modelClass, relationshipHash, null);
471
+ },
472
+ isEmbeddedRecordsMixin: true
568
473
  });
569
474
 
570
- /* eslint-disable @typescript-eslint/no-unsafe-argument */
571
- /* eslint-disable @typescript-eslint/no-unsafe-member-access */
572
- /* eslint-disable @typescript-eslint/no-unsafe-assignment */
573
- /* eslint-disable @typescript-eslint/no-unsafe-call */
574
- /* eslint-disable @typescript-eslint/no-unsafe-return */
475
+ //#endregion
476
+ //#region src/serializer/rest.ts
575
477
  function makeArray(value) {
576
- return Array.isArray(value) ? value : [value];
478
+ return Array.isArray(value) ? value : [value];
577
479
  }
578
-
579
480
  /**
580
- * :::danger
581
- ⚠️ **This is LEGACY documentation** for a feature that is no longer encouraged to be used.
582
- If starting a new app or thinking of implementing a new adapter, consider writing a
583
- {@link Handler} instead to be used with the {@link RequestManager}
584
- :::
481
+ * :::danger
482
+ ⚠️ **This is LEGACY documentation** for a feature that is no longer encouraged to be used.
483
+ If starting a new app or thinking of implementing a new adapter, consider writing a
484
+ {@link Handler} instead to be used with the {@link RequestManager}
485
+ :::
585
486
 
586
- Normally, applications will use the `RESTSerializer` by implementing
587
- the `normalize` method.
487
+ Normally, applications will use the `RESTSerializer` by implementing
488
+ the `normalize` method.
588
489
 
589
- This allows you to do whatever kind of munging you need and is
590
- especially useful if your server is inconsistent and you need to
591
- do munging differently for many different kinds of responses.
490
+ This allows you to do whatever kind of munging you need and is
491
+ especially useful if your server is inconsistent and you need to
492
+ do munging differently for many different kinds of responses.
592
493
 
593
- See the `normalize` documentation for more information.
494
+ See the `normalize` documentation for more information.
594
495
 
595
- ## Across the Board Normalization
496
+ ## Across the Board Normalization
596
497
 
597
- There are also a number of hooks that you might find useful to define
598
- across-the-board rules for your payload. These rules will be useful
599
- if your server is consistent, or if you're building an adapter for
600
- an infrastructure service, like Firebase, and want to encode service
601
- conventions.
498
+ There are also a number of hooks that you might find useful to define
499
+ across-the-board rules for your payload. These rules will be useful
500
+ if your server is consistent, or if you're building an adapter for
501
+ an infrastructure service, like Firebase, and want to encode service
502
+ conventions.
602
503
 
603
- For example, if all of your keys are underscored and all-caps, but
604
- otherwise consistent with the names you use in your models, you
605
- can implement across-the-board rules for how to convert an attribute
606
- name in your model to a key in your JSON.
504
+ For example, if all of your keys are underscored and all-caps, but
505
+ otherwise consistent with the names you use in your models, you
506
+ can implement across-the-board rules for how to convert an attribute
507
+ name in your model to a key in your JSON.
607
508
 
608
- ```js [app/serializers/application.js]
609
- import { RESTSerializer } from '@warp-drive/legacy/serializer/rest';
610
- import { underscore } from '<app-name>/utils/string-utils';
509
+ ```js [app/serializers/application.js]
510
+ import { RESTSerializer } from '@warp-drive/legacy/serializer/rest';
511
+ import { underscore } from '<app-name>/utils/string-utils';
611
512
 
612
- export default class ApplicationSerializer extends RESTSerializer {
613
- keyForAttribute(attr, method) {
614
- return underscore(attr).toUpperCase();
615
- }
616
- }
617
- ```
513
+ export default class ApplicationSerializer extends RESTSerializer {
514
+ keyForAttribute(attr, method) {
515
+ return underscore(attr).toUpperCase();
516
+ }
517
+ }
518
+ ```
618
519
 
619
- You can also implement `keyForRelationship`, which takes the name
620
- of the relationship as the first parameter, the kind of
621
- relationship (`hasMany` or `belongsTo`) as the second parameter, and
622
- the method (`serialize` or `deserialize`) as the third parameter.
520
+ You can also implement `keyForRelationship`, which takes the name
521
+ of the relationship as the first parameter, the kind of
522
+ relationship (`hasMany` or `belongsTo`) as the second parameter, and
523
+ the method (`serialize` or `deserialize`) as the third parameter.
623
524
 
624
- @class RESTSerializer
625
- @public
525
+ @class RESTSerializer
526
+ @public
626
527
  */
627
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
628
528
  const RESTSerializer = JSONSerializer.extend({
629
- /**
630
- `keyForPolymorphicType` can be used to define a custom key when
631
- serializing and deserializing a polymorphic type. By default, the
632
- returned key is `${key}Type`.
633
- Example
634
- ```js [app/serializers/post.js]
635
- import { RESTSerializer } from '@warp-drive/legacy/serializer/rest';
636
- export default class ApplicationSerializer extends RESTSerializer {
637
- keyForPolymorphicType(key, relationship) {
638
- let relationshipKey = this.keyForRelationship(key);
639
- return 'type-' + relationshipKey;
640
- }
641
- }
642
- ```
643
- @public
644
- */
645
- keyForPolymorphicType(key, type, method) {
646
- // @ts-expect-error
647
- const relationshipKey = this.keyForRelationship(key);
648
- return `${relationshipKey}Type`;
649
- },
650
- /**
651
- Normalizes a part of the JSON payload returned by
652
- the server. You should override this method, munge the hash
653
- and call super if you have generic normalization to do.
654
- It takes the type of the record that is being normalized
655
- (as a Model class), the property where the hash was
656
- originally found, and the hash to normalize.
657
- For example, if you have a payload that looks like this:
658
- ```js
659
- {
660
- "post": {
661
- "id": 1,
662
- "title": "Rails is omakase",
663
- "comments": [ 1, 2 ]
664
- },
665
- "comments": [{
666
- "id": 1,
667
- "body": "FIRST"
668
- }, {
669
- "id": 2,
670
- "body": "Rails is unagi"
671
- }]
672
- }
673
- ```
674
- The `normalize` method will be called three times:
675
- * With `App.Post`, `"posts"` and `{ id: 1, title: "Rails is omakase", ... }`
676
- * With `App.Comment`, `"comments"` and `{ id: 1, body: "FIRST" }`
677
- * With `App.Comment`, `"comments"` and `{ id: 2, body: "Rails is unagi" }`
678
- You can use this method, for example, to normalize underscored keys to camelized
679
- or other general-purpose normalizations. You will only need to implement
680
- `normalize` and manipulate the payload as desired.
681
- For example, if the `IDs` under `"comments"` are provided as `_id` instead of
682
- `id`, you can specify how to normalize just the comments:
683
- ```js [app/serializers/post.js]
684
- import { RESTSerializer } from '@warp-drive/legacy/serializer/rest';
685
- export default class ApplicationSerializer extends RESTSerializer {
686
- normalize(model, hash, prop) {
687
- if (prop === 'comments') {
688
- hash.id = hash._id;
689
- delete hash._id;
690
- }
691
- return super.normalize(...arguments);
692
- }
693
- }
694
- ```
695
- On each call to the `normalize` method, the third parameter (`prop`) is always
696
- one of the keys that were in the original payload or in the result of another
697
- normalization as `normalizeResponse`.
698
- @public
699
- @param {Model} modelClass
700
- @param {Object} resourceHash
701
- @param {String} prop
702
- @return {Object}
703
- */
704
-
705
- /**
706
- Normalizes an array of resource payloads and returns a JSON-API Document
707
- with primary data and, if any, included data as `{ data, included }`.
708
- @private
709
- */
710
- _normalizeArray(store, modelName, arrayHash, prop) {
711
- const documentHash = {
712
- data: [],
713
- included: []
714
- };
715
- const modelClass = store.modelFor(modelName);
716
- const serializer = store.serializerFor(modelName);
717
- makeArray(arrayHash).forEach(hash => {
718
- // @ts-expect-error
719
- const {
720
- data,
721
- included
722
- } = this._normalizePolymorphicRecord(store, hash, prop, modelClass, serializer);
723
- // @ts-expect-error
724
- documentHash.data.push(data);
725
- if (included) {
726
- documentHash.included = documentHash.included.concat(included);
727
- }
728
- });
729
- return documentHash;
730
- },
731
- _normalizePolymorphicRecord(store, hash, prop, primaryModelClass, primarySerializer) {
732
- let serializer = primarySerializer;
733
- let modelClass = primaryModelClass;
734
- const primaryHasTypeAttribute = primaryModelClass.fields.has('type');
735
-
736
- // @ts-expect-error all the errors
737
- if (!primaryHasTypeAttribute && hash.type) {
738
- // Support polymorphic records in async relationships
739
- // @ts-expect-error all the errors
740
- const type = this.modelNameFromPayloadKey(hash.type);
741
- if (store.schema.hasResource({
742
- type
743
- })) {
744
- serializer = store.serializerFor(type);
745
- modelClass = store.modelFor(type);
746
- }
747
- }
748
-
749
- // @ts-expect-error all the errors
750
- return serializer.normalize(modelClass, hash, prop);
751
- },
752
- /**
753
- @private
754
- */
755
- _normalizeResponse(store, primaryModelClass, payload, id, requestType, isSingle) {
756
- const documentHash = {
757
- data: null,
758
- included: []
759
- };
760
- // @ts-expect-error
761
- const meta = this.extractMeta(store, primaryModelClass, payload);
762
- if (meta) {
763
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
764
- if (!test) {
765
- throw new Error('The `meta` returned from `extractMeta` has to be an object, not "' + typeof meta + '".');
766
- }
767
- })(typeof meta === 'object') : {}; // @ts-expect-error
768
- documentHash.meta = meta;
769
- }
770
- const keys = Object.keys(payload);
771
- for (let i = 0, length = keys.length; i < length; i++) {
772
- const prop = keys[i];
773
- let modelName = prop;
774
- let forcedSecondary = false;
775
-
776
- /*
777
- If you want to provide sideloaded records of the same type that the
778
- primary data you can do that by prefixing the key with `_`.
779
- Example
780
- ```
781
- {
782
- users: [
783
- { id: 1, title: 'Tom', manager: 3 },
784
- { id: 2, title: 'Yehuda', manager: 3 }
785
- ],
786
- _users: [
787
- { id: 3, title: 'Tomster' }
788
- ]
789
- }
790
- ```
791
- This forces `_users` to be added to `included` instead of `data`.
792
- */
793
- if (prop.charAt(0) === '_') {
794
- forcedSecondary = true;
795
- modelName = prop.substr(1);
796
- }
797
- const type = this.modelNameFromPayloadKey(modelName);
798
- if (!store.schema.hasResource({
799
- type
800
- })) {
801
- if (macroCondition(getGlobalConfig().WarpDrive.env.DEBUG)) {
802
- // @ts-expect-error
803
- warn(this.warnMessageNoModelForKey(modelName, type), false, {
804
- id: 'ds.serializer.model-for-key-missing'
805
- });
806
- }
807
- continue;
808
- }
809
-
810
- // eslint-disable-next-line no-var
811
- var isPrimary = !forcedSecondary && this.isPrimaryType(store, type, primaryModelClass);
812
- const value = payload[prop];
813
- if (value === null) {
814
- continue;
815
- }
816
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
817
- if (!test) {
818
- throw new Error('The adapter returned an array for the primary data of a `queryRecord` response. `queryRecord` should return a single record.');
819
- }
820
- })(!(requestType === 'queryRecord' && isPrimary && Array.isArray(value))) : {};
821
-
822
- /*
823
- Support primary data as an object instead of an array.
824
- Example
825
- ```
826
- {
827
- user: { id: 1, title: 'Tom', manager: 3 }
828
- }
829
- ```
830
- */
831
- if (isPrimary && !Array.isArray(value)) {
832
- // @ts-expect-error
833
- const {
834
- data,
835
- included
836
- } = this._normalizePolymorphicRecord(store, value, prop, primaryModelClass, this);
837
- documentHash.data = data;
838
- if (included) {
839
- documentHash.included = documentHash.included.concat(included);
840
- }
841
- continue;
842
- }
843
- // @ts-expect-error
844
- const {
845
- data,
846
- included
847
- } = this._normalizeArray(store, type, value, prop);
848
- if (included) {
849
- documentHash.included = documentHash.included.concat(included);
850
- }
851
- if (isSingle) {
852
- // eslint-disable-next-line @typescript-eslint/no-loop-func
853
- data.forEach(resource => {
854
- /*
855
- Figures out if this is the primary record or not.
856
- It's either:
857
- 1. The record with the same ID as the original request
858
- 2. If it's a newly created record without an ID, the first record
859
- in the array
860
- */
861
- const isUpdatedRecord = isPrimary && coerceId(resource.id) === id;
862
- const isFirstCreatedRecord = isPrimary && !id && !documentHash.data;
863
- if (isFirstCreatedRecord || isUpdatedRecord) {
864
- documentHash.data = resource;
865
- } else {
866
- // @ts-expect-error
867
- documentHash.included.push(resource);
868
- }
869
- });
870
- } else {
871
- if (isPrimary) {
872
- documentHash.data = data;
873
- } else {
874
- if (data) {
875
- documentHash.included = documentHash.included.concat(data);
876
- }
877
- }
878
- }
879
- }
880
- return documentHash;
881
- },
882
- isPrimaryType(store, modelName, primaryModelClass) {
883
- return dasherize(modelName) === primaryModelClass.modelName;
884
- },
885
- /**
886
- This method allows you to push a payload containing top-level
887
- collections of records organized per type.
888
- ```js
889
- {
890
- "posts": [{
891
- "id": "1",
892
- "title": "Rails is omakase",
893
- "author", "1",
894
- "comments": [ "1" ]
895
- }],
896
- "comments": [{
897
- "id": "1",
898
- "body": "FIRST"
899
- }],
900
- "users": [{
901
- "id": "1",
902
- "name": "@d2h"
903
- }]
904
- }
905
- ```
906
- It will first normalize the payload, so you can use this to push
907
- in data streaming in from your server structured the same way
908
- that fetches and saves are structured.
909
- @public
910
- */
911
- pushPayload(store, payload) {
912
- const documentHash = {
913
- data: [],
914
- included: []
915
- };
916
- for (const prop in payload) {
917
- const type = this.modelNameFromPayloadKey(prop);
918
- if (!store.schema.hasResource({
919
- type
920
- })) {
921
- if (macroCondition(getGlobalConfig().WarpDrive.env.DEBUG)) {
922
- // @ts-expect-error
923
- warn(this.warnMessageNoModelForKey(prop, type), false, {
924
- id: 'ds.serializer.model-for-key-missing'
925
- });
926
- }
927
- continue;
928
- }
929
- const ModelSchema = store.modelFor(type);
930
- const typeSerializer = store.serializerFor(ModelSchema.modelName);
931
- makeArray(payload[prop]).forEach(hash => {
932
- const {
933
- data,
934
- included
935
- } = typeSerializer.normalize(ModelSchema, hash, prop);
936
- // @ts-expect-error
937
- documentHash.data.push(data);
938
- if (included) {
939
- documentHash.included = documentHash.included.concat(included);
940
- }
941
- });
942
- }
943
- store.push(documentHash);
944
- },
945
- /**
946
- This method is used to convert each JSON root key in the payload
947
- into a modelName that it can use to look up the appropriate model for
948
- that part of the payload.
949
- For example, your server may send a model name that does not correspond with
950
- the name of the model in your app. Let's take a look at an example model,
951
- and an example payload:
952
- ```js [app/models/post.js]
953
- import Model from '@warp-drive/legacy/model';
954
- export default class Post extends Model {}
955
- ```
956
- ```javascript
957
- {
958
- "blog/post": {
959
- "id": "1
960
- }
961
- }
962
- ```
963
- Ember Data is going to normalize the payload's root key for the modelName. As a result,
964
- it will try to look up the "blog/post" model. Since we don't have a model called "blog/post"
965
- (or a file called app/models/blog/post.js in ember-cli), Ember Data will throw an error
966
- because it cannot find the "blog/post" model.
967
- Since we want to remove this namespace, we can define a serializer for the application that will
968
- remove "blog/" from the payload key whenver it's encountered by Ember Data:
969
- ```js [app/serializers/application.js]
970
- import { RESTSerializer } from '@warp-drive/legacy/serializer/rest';
971
- export default class ApplicationSerializer extends RESTSerializer {
972
- modelNameFromPayloadKey(payloadKey) {
973
- if (payloadKey === 'blog/post') {
974
- return super.modelNameFromPayloadKey(payloadKey.replace('blog/', ''));
975
- } else {
976
- return super.modelNameFromPayloadKey(payloadKey);
977
- }
978
- }
979
- }
980
- ```
981
- After refreshing, Ember Data will appropriately look up the "post" model.
982
- By default the modelName for a model is its
983
- name in dasherized form. This means that a payload key like "blogPost" would be
984
- normalized to "blog-post" when Ember Data looks up the model. Usually, Ember Data
985
- can use the correct inflection to do this for you. Most of the time, you won't
986
- need to override `modelNameFromPayloadKey` for this purpose.
987
- @public
988
- @return the model's modelName
989
- */
990
- modelNameFromPayloadKey(key) {
991
- return dasherize(singularize(key));
992
- },
993
- // SERIALIZE
994
-
995
- /**
996
- Called when a record is saved in order to convert the
997
- record into JSON.
998
- By default, it creates a JSON object with a key for
999
- each attribute and belongsTo relationship.
1000
- For example, consider this model:
1001
- ```js [app/models/comment.js]
1002
- import Model, { attr, belongsTo } from '@warp-drive/legacy/model';
1003
- export default class Comment extends Model {
1004
- @attr title
1005
- @attr body
1006
- @belongsTo('user') author
1007
- }
1008
- ```
1009
- The default serialization would create a JSON object like:
1010
- ```js
1011
- {
1012
- "title": "Rails is unagi",
1013
- "body": "Rails? Omakase? O_O",
1014
- "author": 12
1015
- }
1016
- ```
1017
- By default, attributes are passed through as-is, unless
1018
- you specified an attribute type (`attr('date')`). If
1019
- you specify a transform, the JavaScript value will be
1020
- serialized when inserted into the JSON hash.
1021
- By default, belongs-to relationships are converted into
1022
- IDs when inserted into the JSON hash.
1023
- ## IDs
1024
- `serialize` takes an options hash with a single option:
1025
- `includeId`. If this option is `true`, `serialize` will,
1026
- by default include the ID in the JSON object it builds.
1027
- The RESTAdapter passes in `includeId: true` when serializing
1028
- a record for `createRecord`, but not for `updateRecord`.
1029
- ## Customization
1030
- Your server may expect a different JSON format than the
1031
- built-in serialization format.
1032
- In that case, you can implement `serialize` yourself and
1033
- return a JSON hash of your choosing.
1034
- ```js [app/serializers/post.js]
1035
- import { RESTSerializer } from '@warp-drive/legacy/serializer/rest';
1036
- export default class ApplicationSerializer extends RESTSerializer {
1037
- serialize(snapshot, options) {
1038
- let json = {
1039
- POST_TTL: snapshot.attr('title'),
1040
- POST_BDY: snapshot.attr('body'),
1041
- POST_CMS: snapshot.hasMany('comments', { ids: true })
1042
- };
1043
- if (options.includeId) {
1044
- json.POST_ID_ = snapshot.id;
1045
- }
1046
- return json;
1047
- }
1048
- }
1049
- ```
1050
- ## Customizing an App-Wide Serializer
1051
- If you want to define a serializer for your entire
1052
- application, you'll probably want to use `eachAttribute`
1053
- and `eachRelationship` on the record.
1054
- ```js [app/serializers/application.js]
1055
- import { RESTSerializer } from '@warp-drive/legacy/serializer/rest';
1056
- import { pluralize } from '<app-name>/utils/string-utils';
1057
- export default class ApplicationSerializer extends RESTSerializer {
1058
- serialize(snapshot, options) {
1059
- let json = {};
1060
- snapshot.eachAttribute(function(name) {
1061
- json[serverAttributeName(name)] = snapshot.attr(name);
1062
- });
1063
- snapshot.eachRelationship(function(name, relationship) {
1064
- if (relationship.kind === 'hasMany') {
1065
- json[serverHasManyName(name)] = snapshot.hasMany(name, { ids: true });
1066
- }
1067
- });
1068
- if (options.includeId) {
1069
- json.ID_ = snapshot.id;
1070
- }
1071
- return json;
1072
- }
1073
- }
1074
- function serverAttributeName(attribute) {
1075
- return attribute.underscore().toUpperCase();
1076
- }
1077
- function serverHasManyName(name) {
1078
- return serverAttributeName(singularize(name)) + "_IDS";
1079
- }
1080
- ```
1081
- This serializer will generate JSON that looks like this:
1082
- ```js
1083
- {
1084
- "TITLE": "Rails is omakase",
1085
- "BODY": "Yep. Omakase.",
1086
- "COMMENT_IDS": [ 1, 2, 3 ]
1087
- }
1088
- ```
1089
- ## Tweaking the Default JSON
1090
- If you just want to do some small tweaks on the default JSON,
1091
- you can call super first and make the tweaks on the returned
1092
- JSON.
1093
- ```js [app/serializers/post.js]
1094
- import { RESTSerializer } from '@warp-drive/legacy/serializer/rest';
1095
- export default class ApplicationSerializer extends RESTSerializer {
1096
- serialize(snapshot, options) {
1097
- let json = super.serialize(snapshot, options);
1098
- json.subject = json.title;
1099
- delete json.title;
1100
- return json;
1101
- }
1102
- }
1103
- ```
1104
- @public
1105
- */
1106
- serialize(snapshot, options) {
1107
- // @ts-expect-error
1108
- return this._super(...arguments);
1109
- },
1110
- /**
1111
- You can use this method to customize the root keys serialized into the JSON.
1112
- The hash property should be modified by reference (possibly using something like _.extend)
1113
- By default the REST Serializer sends the modelName of a model, which is a camelized
1114
- version of the name.
1115
- For example, your server may expect underscored root objects.
1116
- ```js [app/serializers/application.js]
1117
- import { RESTSerializer } from '@warp-drive/legacy/serializer/rest';
1118
- import { underscore } from '<app-name>/utils/string-utils';
1119
- export default class ApplicationSerializer extends RESTSerializer {
1120
- serializeIntoHash(data, type, record, options) {
1121
- let root = underscore(type.modelName);
1122
- data[root] = this.serialize(record, options);
1123
- }
1124
- }
1125
- ```
1126
- @public
1127
- */
1128
- serializeIntoHash(hash, typeClass, snapshot, options) {
1129
- const normalizedRootKey = this.payloadKeyFromModelName(typeClass.modelName);
1130
- hash[normalizedRootKey] = this.serialize(snapshot, options);
1131
- },
1132
- /**
1133
- You can use `payloadKeyFromModelName` to override the root key for an outgoing
1134
- request. By default, the RESTSerializer returns a camelized version of the
1135
- model's name.
1136
- For a model called TacoParty, its `modelName` would be the string `taco-party`. The RESTSerializer
1137
- will send it to the server with `tacoParty` as the root key in the JSON payload:
1138
- ```js
1139
- {
1140
- "tacoParty": {
1141
- "id": "1",
1142
- "location": "Matthew Beale's House"
1143
- }
1144
- }
1145
- ```
1146
- For example, your server may expect dasherized root objects:
1147
- ```js [app/serializers/application.js]
1148
- import { RESTSerializer } from '@warp-drive/legacy/serializer/rest';
1149
- import { dasherize } from '<app-name>/utils/string-utils';
1150
- export default class ApplicationSerializer extends RESTSerializer {
1151
- payloadKeyFromModelName(modelName) {
1152
- return dasherize(modelName);
1153
- }
1154
- }
1155
- ```
1156
- Given a `TacoParty` model, calling `save` on it would produce an outgoing
1157
- request like:
1158
- ```js
1159
- {
1160
- "taco-party": {
1161
- "id": "1",
1162
- "location": "Matthew Beale's House"
1163
- }
1164
- }
1165
- ```
1166
- @public
1167
- */
1168
- payloadKeyFromModelName(modelName) {
1169
- return camelize(modelName);
1170
- },
1171
- /**
1172
- You can use this method to customize how polymorphic objects are serialized.
1173
- By default the REST Serializer creates the key by appending `Type` to
1174
- the attribute and value from the model's camelcased model name.
1175
- @public
1176
- */
1177
- serializePolymorphicType(snapshot, json, relationship) {
1178
- const name = relationship.name;
1179
- const typeKey = this.keyForPolymorphicType(name, relationship.type, 'serialize');
1180
- const belongsTo = snapshot.belongsTo(name);
1181
- if (!belongsTo) {
1182
- json[typeKey] = null;
1183
- } else {
1184
- // @ts-expect-error
1185
- json[typeKey] = camelize(belongsTo.modelName);
1186
- }
1187
- },
1188
- /**
1189
- You can use this method to customize how a polymorphic relationship should
1190
- be extracted.
1191
- @public
1192
- @param {Object} relationshipType
1193
- @param {Object} relationshipHash
1194
- @param {Object} relationshipOptions
1195
- @return {Object}
1196
- */
1197
- extractPolymorphicRelationship(relationshipType, relationshipHash, relationshipOptions) {
1198
- // @ts-expect-error
1199
- const {
1200
- key,
1201
- resourceHash,
1202
- relationshipMeta
1203
- } = relationshipOptions;
1204
-
1205
- // A polymorphic belongsTo relationship can be present in the payload
1206
- // either in the form where the `id` and the `type` are given:
1207
- //
1208
- // {
1209
- // message: { id: 1, type: 'post' }
1210
- // }
1211
- //
1212
- // or by the `id` and a `<relationship>Type` attribute:
1213
- //
1214
- // {
1215
- // message: 1,
1216
- // messageType: 'post'
1217
- // }
1218
- //
1219
- // The next code checks if the latter case is present and returns the
1220
- // corresponding JSON-API representation. The former case is handled within
1221
- // the base class JSONSerializer.
1222
- const isPolymorphic = relationshipMeta.options.polymorphic;
1223
- const typeProperty = this.keyForPolymorphicType(key, relationshipType, 'deserialize');
1224
- if (isPolymorphic && resourceHash[typeProperty] !== undefined && typeof relationshipHash !== 'object') {
1225
- const type = this.modelNameFromPayloadKey(resourceHash[typeProperty]);
1226
- return {
1227
- id: coerceId(relationshipHash),
1228
- type: type
1229
- };
1230
- }
1231
- // @ts-expect-error
1232
- return this._super(...arguments);
1233
- }
529
+ /**
530
+ `keyForPolymorphicType` can be used to define a custom key when
531
+ serializing and deserializing a polymorphic type. By default, the
532
+ returned key is `${key}Type`.
533
+ Example
534
+ ```js [app/serializers/post.js]
535
+ import { RESTSerializer } from '@warp-drive/legacy/serializer/rest';
536
+ export default class ApplicationSerializer extends RESTSerializer {
537
+ keyForPolymorphicType(key, relationship) {
538
+ let relationshipKey = this.keyForRelationship(key);
539
+ return 'type-' + relationshipKey;
540
+ }
541
+ }
542
+ ```
543
+ @public
544
+ */
545
+ keyForPolymorphicType(key, type, method) {
546
+ return `${this.keyForRelationship(key)}Type`;
547
+ },
548
+ /**
549
+ Normalizes a part of the JSON payload returned by
550
+ the server. You should override this method, munge the hash
551
+ and call super if you have generic normalization to do.
552
+ It takes the type of the record that is being normalized
553
+ (as a Model class), the property where the hash was
554
+ originally found, and the hash to normalize.
555
+ For example, if you have a payload that looks like this:
556
+ ```js
557
+ {
558
+ "post": {
559
+ "id": 1,
560
+ "title": "Rails is omakase",
561
+ "comments": [ 1, 2 ]
562
+ },
563
+ "comments": [{
564
+ "id": 1,
565
+ "body": "FIRST"
566
+ }, {
567
+ "id": 2,
568
+ "body": "Rails is unagi"
569
+ }]
570
+ }
571
+ ```
572
+ The `normalize` method will be called three times:
573
+ * With `App.Post`, `"posts"` and `{ id: 1, title: "Rails is omakase", ... }`
574
+ * With `App.Comment`, `"comments"` and `{ id: 1, body: "FIRST" }`
575
+ * With `App.Comment`, `"comments"` and `{ id: 2, body: "Rails is unagi" }`
576
+ You can use this method, for example, to normalize underscored keys to camelized
577
+ or other general-purpose normalizations. You will only need to implement
578
+ `normalize` and manipulate the payload as desired.
579
+ For example, if the `IDs` under `"comments"` are provided as `_id` instead of
580
+ `id`, you can specify how to normalize just the comments:
581
+ ```js [app/serializers/post.js]
582
+ import { RESTSerializer } from '@warp-drive/legacy/serializer/rest';
583
+ export default class ApplicationSerializer extends RESTSerializer {
584
+ normalize(model, hash, prop) {
585
+ if (prop === 'comments') {
586
+ hash.id = hash._id;
587
+ delete hash._id;
588
+ }
589
+ return super.normalize(...arguments);
590
+ }
591
+ }
592
+ ```
593
+ On each call to the `normalize` method, the third parameter (`prop`) is always
594
+ one of the keys that were in the original payload or in the result of another
595
+ normalization as `normalizeResponse`.
596
+ @public
597
+ @param {Model} modelClass
598
+ @param {Object} resourceHash
599
+ @param {String} prop
600
+ @return {Object}
601
+ */
602
+ /**
603
+ Normalizes an array of resource payloads and returns a JSON-API Document
604
+ with primary data and, if any, included data as `{ data, included }`.
605
+ @private
606
+ */
607
+ _normalizeArray(store, modelName, arrayHash, prop) {
608
+ const documentHash = {
609
+ data: [],
610
+ included: []
611
+ };
612
+ const modelClass = store.modelFor(modelName);
613
+ const serializer = store.serializerFor(modelName);
614
+ makeArray(arrayHash).forEach((hash) => {
615
+ const { data, included } = this._normalizePolymorphicRecord(store, hash, prop, modelClass, serializer);
616
+ documentHash.data.push(data);
617
+ if (included) documentHash.included = documentHash.included.concat(included);
618
+ });
619
+ return documentHash;
620
+ },
621
+ _normalizePolymorphicRecord(store, hash, prop, primaryModelClass, primarySerializer) {
622
+ let serializer = primarySerializer;
623
+ let modelClass = primaryModelClass;
624
+ if (!primaryModelClass.fields.has("type") && hash.type) {
625
+ const type = this.modelNameFromPayloadKey(hash.type);
626
+ if (store.schema.hasResource({ type })) {
627
+ serializer = store.serializerFor(type);
628
+ modelClass = store.modelFor(type);
629
+ }
630
+ }
631
+ return serializer.normalize(modelClass, hash, prop);
632
+ },
633
+ /**
634
+ @private
635
+ */
636
+ _normalizeResponse(store, primaryModelClass, payload, id, requestType, isSingle) {
637
+ const documentHash = {
638
+ data: null,
639
+ included: []
640
+ };
641
+ const meta = this.extractMeta(store, primaryModelClass, payload);
642
+ if (meta) {
643
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) && ((test) => {
644
+ if (!test) throw new Error("The `meta` returned from `extractMeta` has to be an object, not \"" + typeof meta + "\".");
645
+ })(typeof meta === "object");
646
+ documentHash.meta = meta;
647
+ }
648
+ const keys = Object.keys(payload);
649
+ for (let i = 0, length = keys.length; i < length; i++) {
650
+ const prop = keys[i];
651
+ let modelName = prop;
652
+ let forcedSecondary = false;
653
+ if (prop.charAt(0) === "_") {
654
+ forcedSecondary = true;
655
+ modelName = prop.substr(1);
656
+ }
657
+ const type = this.modelNameFromPayloadKey(modelName);
658
+ if (!store.schema.hasResource({ type })) {
659
+ if (macroCondition(getGlobalConfig().WarpDrive.env.DEBUG)) warn(this.warnMessageNoModelForKey(modelName, type), false, { id: "ds.serializer.model-for-key-missing" });
660
+ continue;
661
+ }
662
+ var isPrimary = !forcedSecondary && this.isPrimaryType(store, type, primaryModelClass);
663
+ const value = payload[prop];
664
+ if (value === null) continue;
665
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) && ((test) => {
666
+ if (!test) throw new Error("The adapter returned an array for the primary data of a `queryRecord` response. `queryRecord` should return a single record.");
667
+ })(!(requestType === "queryRecord" && isPrimary && Array.isArray(value)));
668
+ if (isPrimary && !Array.isArray(value)) {
669
+ const { data, included } = this._normalizePolymorphicRecord(store, value, prop, primaryModelClass, this);
670
+ documentHash.data = data;
671
+ if (included) documentHash.included = documentHash.included.concat(included);
672
+ continue;
673
+ }
674
+ const { data, included } = this._normalizeArray(store, type, value, prop);
675
+ if (included) documentHash.included = documentHash.included.concat(included);
676
+ if (isSingle) data.forEach((resource) => {
677
+ const isUpdatedRecord = isPrimary && coerceId(resource.id) === id;
678
+ if (isPrimary && !id && !documentHash.data || isUpdatedRecord) documentHash.data = resource;
679
+ else documentHash.included.push(resource);
680
+ });
681
+ else if (isPrimary) documentHash.data = data;
682
+ else if (data) documentHash.included = documentHash.included.concat(data);
683
+ }
684
+ return documentHash;
685
+ },
686
+ isPrimaryType(store, modelName, primaryModelClass) {
687
+ return dasherize(modelName) === primaryModelClass.modelName;
688
+ },
689
+ /**
690
+ This method allows you to push a payload containing top-level
691
+ collections of records organized per type.
692
+ ```js
693
+ {
694
+ "posts": [{
695
+ "id": "1",
696
+ "title": "Rails is omakase",
697
+ "author", "1",
698
+ "comments": [ "1" ]
699
+ }],
700
+ "comments": [{
701
+ "id": "1",
702
+ "body": "FIRST"
703
+ }],
704
+ "users": [{
705
+ "id": "1",
706
+ "name": "@d2h"
707
+ }]
708
+ }
709
+ ```
710
+ It will first normalize the payload, so you can use this to push
711
+ in data streaming in from your server structured the same way
712
+ that fetches and saves are structured.
713
+ @public
714
+ */
715
+ pushPayload(store, payload) {
716
+ const documentHash = {
717
+ data: [],
718
+ included: []
719
+ };
720
+ for (const prop in payload) {
721
+ const type = this.modelNameFromPayloadKey(prop);
722
+ if (!store.schema.hasResource({ type })) {
723
+ if (macroCondition(getGlobalConfig().WarpDrive.env.DEBUG)) warn(this.warnMessageNoModelForKey(prop, type), false, { id: "ds.serializer.model-for-key-missing" });
724
+ continue;
725
+ }
726
+ const ModelSchema = store.modelFor(type);
727
+ const typeSerializer = store.serializerFor(ModelSchema.modelName);
728
+ makeArray(payload[prop]).forEach((hash) => {
729
+ const { data, included } = typeSerializer.normalize(ModelSchema, hash, prop);
730
+ documentHash.data.push(data);
731
+ if (included) documentHash.included = documentHash.included.concat(included);
732
+ });
733
+ }
734
+ store.push(documentHash);
735
+ },
736
+ /**
737
+ This method is used to convert each JSON root key in the payload
738
+ into a modelName that it can use to look up the appropriate model for
739
+ that part of the payload.
740
+ For example, your server may send a model name that does not correspond with
741
+ the name of the model in your app. Let's take a look at an example model,
742
+ and an example payload:
743
+ ```js [app/models/post.js]
744
+ import Model from '@warp-drive/legacy/model';
745
+ export default class Post extends Model {}
746
+ ```
747
+ ```javascript
748
+ {
749
+ "blog/post": {
750
+ "id": "1
751
+ }
752
+ }
753
+ ```
754
+ Ember Data is going to normalize the payload's root key for the modelName. As a result,
755
+ it will try to look up the "blog/post" model. Since we don't have a model called "blog/post"
756
+ (or a file called app/models/blog/post.js in ember-cli), Ember Data will throw an error
757
+ because it cannot find the "blog/post" model.
758
+ Since we want to remove this namespace, we can define a serializer for the application that will
759
+ remove "blog/" from the payload key whenver it's encountered by Ember Data:
760
+ ```js [app/serializers/application.js]
761
+ import { RESTSerializer } from '@warp-drive/legacy/serializer/rest';
762
+ export default class ApplicationSerializer extends RESTSerializer {
763
+ modelNameFromPayloadKey(payloadKey) {
764
+ if (payloadKey === 'blog/post') {
765
+ return super.modelNameFromPayloadKey(payloadKey.replace('blog/', ''));
766
+ } else {
767
+ return super.modelNameFromPayloadKey(payloadKey);
768
+ }
769
+ }
770
+ }
771
+ ```
772
+ After refreshing, Ember Data will appropriately look up the "post" model.
773
+ By default the modelName for a model is its
774
+ name in dasherized form. This means that a payload key like "blogPost" would be
775
+ normalized to "blog-post" when Ember Data looks up the model. Usually, Ember Data
776
+ can use the correct inflection to do this for you. Most of the time, you won't
777
+ need to override `modelNameFromPayloadKey` for this purpose.
778
+ @public
779
+ @return the model's modelName
780
+ */
781
+ modelNameFromPayloadKey(key) {
782
+ return dasherize(singularize(key));
783
+ },
784
+ /**
785
+ Called when a record is saved in order to convert the
786
+ record into JSON.
787
+ By default, it creates a JSON object with a key for
788
+ each attribute and belongsTo relationship.
789
+ For example, consider this model:
790
+ ```js [app/models/comment.js]
791
+ import Model, { attr, belongsTo } from '@warp-drive/legacy/model';
792
+ export default class Comment extends Model {
793
+ @attr title
794
+ @attr body
795
+ @belongsTo('user') author
796
+ }
797
+ ```
798
+ The default serialization would create a JSON object like:
799
+ ```js
800
+ {
801
+ "title": "Rails is unagi",
802
+ "body": "Rails? Omakase? O_O",
803
+ "author": 12
804
+ }
805
+ ```
806
+ By default, attributes are passed through as-is, unless
807
+ you specified an attribute type (`attr('date')`). If
808
+ you specify a transform, the JavaScript value will be
809
+ serialized when inserted into the JSON hash.
810
+ By default, belongs-to relationships are converted into
811
+ IDs when inserted into the JSON hash.
812
+ ## IDs
813
+ `serialize` takes an options hash with a single option:
814
+ `includeId`. If this option is `true`, `serialize` will,
815
+ by default include the ID in the JSON object it builds.
816
+ The RESTAdapter passes in `includeId: true` when serializing
817
+ a record for `createRecord`, but not for `updateRecord`.
818
+ ## Customization
819
+ Your server may expect a different JSON format than the
820
+ built-in serialization format.
821
+ In that case, you can implement `serialize` yourself and
822
+ return a JSON hash of your choosing.
823
+ ```js [app/serializers/post.js]
824
+ import { RESTSerializer } from '@warp-drive/legacy/serializer/rest';
825
+ export default class ApplicationSerializer extends RESTSerializer {
826
+ serialize(snapshot, options) {
827
+ let json = {
828
+ POST_TTL: snapshot.attr('title'),
829
+ POST_BDY: snapshot.attr('body'),
830
+ POST_CMS: snapshot.hasMany('comments', { ids: true })
831
+ };
832
+ if (options.includeId) {
833
+ json.POST_ID_ = snapshot.id;
834
+ }
835
+ return json;
836
+ }
837
+ }
838
+ ```
839
+ ## Customizing an App-Wide Serializer
840
+ If you want to define a serializer for your entire
841
+ application, you'll probably want to use `eachAttribute`
842
+ and `eachRelationship` on the record.
843
+ ```js [app/serializers/application.js]
844
+ import { RESTSerializer } from '@warp-drive/legacy/serializer/rest';
845
+ import { pluralize } from '<app-name>/utils/string-utils';
846
+ export default class ApplicationSerializer extends RESTSerializer {
847
+ serialize(snapshot, options) {
848
+ let json = {};
849
+ snapshot.eachAttribute(function(name) {
850
+ json[serverAttributeName(name)] = snapshot.attr(name);
851
+ });
852
+ snapshot.eachRelationship(function(name, relationship) {
853
+ if (relationship.kind === 'hasMany') {
854
+ json[serverHasManyName(name)] = snapshot.hasMany(name, { ids: true });
855
+ }
856
+ });
857
+ if (options.includeId) {
858
+ json.ID_ = snapshot.id;
859
+ }
860
+ return json;
861
+ }
862
+ }
863
+ function serverAttributeName(attribute) {
864
+ return attribute.underscore().toUpperCase();
865
+ }
866
+ function serverHasManyName(name) {
867
+ return serverAttributeName(singularize(name)) + "_IDS";
868
+ }
869
+ ```
870
+ This serializer will generate JSON that looks like this:
871
+ ```js
872
+ {
873
+ "TITLE": "Rails is omakase",
874
+ "BODY": "Yep. Omakase.",
875
+ "COMMENT_IDS": [ 1, 2, 3 ]
876
+ }
877
+ ```
878
+ ## Tweaking the Default JSON
879
+ If you just want to do some small tweaks on the default JSON,
880
+ you can call super first and make the tweaks on the returned
881
+ JSON.
882
+ ```js [app/serializers/post.js]
883
+ import { RESTSerializer } from '@warp-drive/legacy/serializer/rest';
884
+ export default class ApplicationSerializer extends RESTSerializer {
885
+ serialize(snapshot, options) {
886
+ let json = super.serialize(snapshot, options);
887
+ json.subject = json.title;
888
+ delete json.title;
889
+ return json;
890
+ }
891
+ }
892
+ ```
893
+ @public
894
+ */
895
+ serialize(snapshot, options) {
896
+ return this._super(...arguments);
897
+ },
898
+ /**
899
+ You can use this method to customize the root keys serialized into the JSON.
900
+ The hash property should be modified by reference (possibly using something like _.extend)
901
+ By default the REST Serializer sends the modelName of a model, which is a camelized
902
+ version of the name.
903
+ For example, your server may expect underscored root objects.
904
+ ```js [app/serializers/application.js]
905
+ import { RESTSerializer } from '@warp-drive/legacy/serializer/rest';
906
+ import { underscore } from '<app-name>/utils/string-utils';
907
+ export default class ApplicationSerializer extends RESTSerializer {
908
+ serializeIntoHash(data, type, record, options) {
909
+ let root = underscore(type.modelName);
910
+ data[root] = this.serialize(record, options);
911
+ }
912
+ }
913
+ ```
914
+ @public
915
+ */
916
+ serializeIntoHash(hash, typeClass, snapshot, options) {
917
+ const normalizedRootKey = this.payloadKeyFromModelName(typeClass.modelName);
918
+ hash[normalizedRootKey] = this.serialize(snapshot, options);
919
+ },
920
+ /**
921
+ You can use `payloadKeyFromModelName` to override the root key for an outgoing
922
+ request. By default, the RESTSerializer returns a camelized version of the
923
+ model's name.
924
+ For a model called TacoParty, its `modelName` would be the string `taco-party`. The RESTSerializer
925
+ will send it to the server with `tacoParty` as the root key in the JSON payload:
926
+ ```js
927
+ {
928
+ "tacoParty": {
929
+ "id": "1",
930
+ "location": "Matthew Beale's House"
931
+ }
932
+ }
933
+ ```
934
+ For example, your server may expect dasherized root objects:
935
+ ```js [app/serializers/application.js]
936
+ import { RESTSerializer } from '@warp-drive/legacy/serializer/rest';
937
+ import { dasherize } from '<app-name>/utils/string-utils';
938
+ export default class ApplicationSerializer extends RESTSerializer {
939
+ payloadKeyFromModelName(modelName) {
940
+ return dasherize(modelName);
941
+ }
942
+ }
943
+ ```
944
+ Given a `TacoParty` model, calling `save` on it would produce an outgoing
945
+ request like:
946
+ ```js
947
+ {
948
+ "taco-party": {
949
+ "id": "1",
950
+ "location": "Matthew Beale's House"
951
+ }
952
+ }
953
+ ```
954
+ @public
955
+ */
956
+ payloadKeyFromModelName(modelName) {
957
+ return camelize(modelName);
958
+ },
959
+ /**
960
+ You can use this method to customize how polymorphic objects are serialized.
961
+ By default the REST Serializer creates the key by appending `Type` to
962
+ the attribute and value from the model's camelcased model name.
963
+ @public
964
+ */
965
+ serializePolymorphicType(snapshot, json, relationship) {
966
+ const name = relationship.name;
967
+ const typeKey = this.keyForPolymorphicType(name, relationship.type, "serialize");
968
+ const belongsTo = snapshot.belongsTo(name);
969
+ if (!belongsTo) json[typeKey] = null;
970
+ else json[typeKey] = camelize(belongsTo.modelName);
971
+ },
972
+ /**
973
+ You can use this method to customize how a polymorphic relationship should
974
+ be extracted.
975
+ @public
976
+ @param {Object} relationshipType
977
+ @param {Object} relationshipHash
978
+ @param {Object} relationshipOptions
979
+ @return {Object}
980
+ */
981
+ extractPolymorphicRelationship(relationshipType, relationshipHash, relationshipOptions) {
982
+ const { key, resourceHash, relationshipMeta } = relationshipOptions;
983
+ const isPolymorphic = relationshipMeta.options.polymorphic;
984
+ const typeProperty = this.keyForPolymorphicType(key, relationshipType, "deserialize");
985
+ if (isPolymorphic && resourceHash[typeProperty] !== void 0 && typeof relationshipHash !== "object") {
986
+ const type = this.modelNameFromPayloadKey(resourceHash[typeProperty]);
987
+ return {
988
+ id: coerceId(relationshipHash),
989
+ type
990
+ };
991
+ }
992
+ return this._super(...arguments);
993
+ }
1234
994
  });
1235
- if (macroCondition(getGlobalConfig().WarpDrive.env.DEBUG)) {
1236
- RESTSerializer.reopen({
1237
- warnMessageNoModelForKey(prop, typeKey) {
1238
- return 'Encountered "' + prop + '" in payload, but no model was found for model name "' + typeKey + '" (resolved model name using ' + this.constructor.toString() + '.modelNameFromPayloadKey("' + prop + '"))';
1239
- }
1240
- });
1241
- }
1242
- export { EmbeddedRecordsMixin, RESTSerializer };
995
+ if (macroCondition(getGlobalConfig().WarpDrive.env.DEBUG)) RESTSerializer.reopen({ warnMessageNoModelForKey(prop, typeKey) {
996
+ return "Encountered \"" + prop + "\" in payload, but no model was found for model name \"" + typeKey + "\" (resolved model name using " + this.constructor.toString() + ".modelNameFromPayloadKey(\"" + prop + "\"))";
997
+ } });
998
+
999
+ //#endregion
1000
+ export { EmbeddedRecordsMixin, RESTSerializer };
1001
+ //# sourceMappingURL=rest.js.map