@ember-data/serializer 5.5.0-alpha.9 → 5.5.0-beta.0

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 (78) hide show
  1. package/LICENSE.md +19 -7
  2. package/README.md +16 -10
  3. package/addon-main.cjs +5 -0
  4. package/blueprints/serializer/index.js +71 -5
  5. package/blueprints/serializer-test/index.js +13 -7
  6. package/blueprints/serializer-test/qunit-files/__root__/__path__/__test__.js +8 -9
  7. package/blueprints/transform/index.js +14 -4
  8. package/blueprints/transform-test/index.js +13 -7
  9. package/blueprints/transform-test/qunit-files/__root__/__path__/__test__.js +3 -4
  10. package/dist/index.js +306 -0
  11. package/dist/index.js.map +1 -0
  12. package/{addon/json.js → dist/json-CVTR4xWv.js} +86 -66
  13. package/dist/json-CVTR4xWv.js.map +1 -0
  14. package/{addon → dist}/json-api.js +88 -64
  15. package/dist/json-api.js.map +1 -0
  16. package/dist/json.js +6 -0
  17. package/dist/json.js.map +1 -0
  18. package/dist/rest.js +1274 -0
  19. package/dist/rest.js.map +1 -0
  20. package/{addon/string-22572f80.js → dist/transform.js} +143 -20
  21. package/dist/transform.js.map +1 -0
  22. package/logos/NCC-1701-a-blue.svg +4 -0
  23. package/logos/NCC-1701-a-gold.svg +4 -0
  24. package/logos/NCC-1701-a-gold_100.svg +1 -0
  25. package/logos/NCC-1701-a-gold_base-64.txt +1 -0
  26. package/logos/NCC-1701-a.svg +4 -0
  27. package/logos/README.md +4 -0
  28. package/logos/docs-badge.svg +2 -0
  29. package/logos/github-header.svg +444 -0
  30. package/logos/social1.png +0 -0
  31. package/logos/social2.png +0 -0
  32. package/logos/warp-drive-logo-dark.svg +4 -0
  33. package/logos/warp-drive-logo-gold.svg +4 -0
  34. package/package.json +60 -45
  35. package/unstable-preview-types/-private/embedded-records-mixin.d.ts +102 -0
  36. package/unstable-preview-types/-private/embedded-records-mixin.d.ts.map +1 -0
  37. package/unstable-preview-types/-private/transforms/boolean.d.ts +52 -0
  38. package/unstable-preview-types/-private/transforms/boolean.d.ts.map +1 -0
  39. package/unstable-preview-types/-private/transforms/boolean.type-test.d.ts +4 -0
  40. package/unstable-preview-types/-private/transforms/boolean.type-test.d.ts.map +1 -0
  41. package/unstable-preview-types/-private/transforms/date.d.ts +33 -0
  42. package/unstable-preview-types/-private/transforms/date.d.ts.map +1 -0
  43. package/unstable-preview-types/-private/transforms/number.d.ts +34 -0
  44. package/unstable-preview-types/-private/transforms/number.d.ts.map +1 -0
  45. package/unstable-preview-types/-private/transforms/string.d.ts +34 -0
  46. package/unstable-preview-types/-private/transforms/string.d.ts.map +1 -0
  47. package/unstable-preview-types/-private/transforms/transform.d.ts +126 -0
  48. package/unstable-preview-types/-private/transforms/transform.d.ts.map +1 -0
  49. package/unstable-preview-types/-private/utils.d.ts +6 -0
  50. package/unstable-preview-types/-private/utils.d.ts.map +1 -0
  51. package/unstable-preview-types/index.d.ts +277 -0
  52. package/unstable-preview-types/index.d.ts.map +1 -0
  53. package/unstable-preview-types/json-api.d.ts +514 -0
  54. package/unstable-preview-types/json-api.d.ts.map +1 -0
  55. package/unstable-preview-types/json.d.ts +1093 -0
  56. package/unstable-preview-types/json.d.ts.map +1 -0
  57. package/unstable-preview-types/rest.d.ts +570 -0
  58. package/unstable-preview-types/rest.d.ts.map +1 -0
  59. package/unstable-preview-types/transform.d.ts +11 -0
  60. package/unstable-preview-types/transform.d.ts.map +1 -0
  61. package/addon/-private.js +0 -4
  62. package/addon/-private.js.map +0 -1
  63. package/addon/embedded-records-mixin-d75385ff.js +0 -575
  64. package/addon/embedded-records-mixin-d75385ff.js.map +0 -1
  65. package/addon/index.js +0 -180
  66. package/addon/index.js.map +0 -1
  67. package/addon/json-api.js.map +0 -1
  68. package/addon/json.js.map +0 -1
  69. package/addon/rest.js +0 -684
  70. package/addon/rest.js.map +0 -1
  71. package/addon/string-22572f80.js.map +0 -1
  72. package/addon/transform.js +0 -2
  73. package/addon/transform.js.map +0 -1
  74. package/addon/utils-075c5b79.js +0 -12
  75. package/addon/utils-075c5b79.js.map +0 -1
  76. package/addon-main.js +0 -93
  77. /package/{ember-data-logo-dark.svg → logos/ember-data-logo-dark.svg} +0 -0
  78. /package/{ember-data-logo-light.svg → logos/ember-data-logo-light.svg} +0 -0
package/LICENSE.md CHANGED
@@ -1,11 +1,23 @@
1
- The MIT License (MIT)
1
+ MIT License
2
2
 
3
- Copyright (C) 2017-2023 Ember.js contributors
4
- Portions Copyright (C) 2011-2017 Tilde, Inc. and contributors.
5
- Portions Copyright (C) 2011 LivingSocial Inc.
3
+ Copyright (c) 2017-2025 Ember.js and contributors
4
+ Copyright (c) 2011-2017 Tilde, Inc. and contributors
5
+ Copyright (c) 2011 LivingSocial Inc.
6
6
 
7
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
7
+ Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ of this software and associated documentation files (the "Software"), to deal
9
+ in the Software without restriction, including without limitation the rights
10
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ copies of the Software, and to permit persons to whom the Software is
12
+ furnished to do so, subject to the following conditions:
8
13
 
9
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
14
+ The above copyright notice and this permission notice shall be included in all
15
+ copies or substantial portions of the Software.
10
16
 
11
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ SOFTWARE.
package/README.md CHANGED
@@ -1,14 +1,14 @@
1
1
  <p align="center">
2
2
  <img
3
3
  class="project-logo"
4
- src="./ember-data-logo-dark.svg#gh-dark-mode-only"
4
+ src="./logos/ember-data-logo-dark.svg#gh-dark-mode-only"
5
5
  alt="EmberData Serializer"
6
6
  width="240px"
7
7
  title="EmberData Serializer"
8
8
  />
9
9
  <img
10
10
  class="project-logo"
11
- src="./ember-data-logo-light.svg#gh-light-mode-only"
11
+ src="./logos/ember-data-logo-light.svg#gh-light-mode-only"
12
12
  alt="EmberData Serializer"
13
13
  width="240px"
14
14
  title="EmberData Serializer"
@@ -31,11 +31,21 @@ If installing `@ember-data/` packages individually install using your javascript
31
31
  pnpm add @ember-data/serializer
32
32
  ```
33
33
 
34
+ **Tagged Releases**
35
+
36
+ - ![NPM Canary Version](https://img.shields.io/npm/v/%40ember-data/serializer/canary?label=%40canary&color=FFBF00)
37
+ - ![NPM Beta Version](https://img.shields.io/npm/v/%40ember-data/serializer/beta?label=%40beta&color=ff00ff)
38
+ - ![NPM Stable Version](https://img.shields.io/npm/v/%40ember-data/serializer/latest?label=%40latest&color=90EE90)
39
+ - ![NPM LTS Version](https://img.shields.io/npm/v/%40ember-data/serializer/lts?label=%40lts&color=0096FF)
40
+ - ![NPM LTS 4.12 Version](https://img.shields.io/npm/v/%40ember-data/serializer/lts-4-12?label=%40lts-4-12&color=bbbbbb)
41
+
42
+
34
43
  ## 🚀 Setup
35
44
 
36
45
  If using `ember-data` no additional setup is necesssary.
37
46
 
38
- > **Note** When using [ember-data](https://github.com/emberjs/data/blob/main/packages/-ember-data) the below
47
+ > **Note**
48
+ > When using [ember-data](https://github.com/emberjs/data/blob/main/packages/-ember-data) the below
39
49
  > configuration is handled for you automatically.
40
50
 
41
51
  To use legacy serializers you will need to have installed and configured the LegacyNetworkHandler from [@ember-data/legacy-compat](https://github.com/emberjs/data/blob/main/packages/-ember-data)
@@ -50,13 +60,9 @@ import RequestManager from '@ember-data/request';
50
60
  import { LegacyNetworkHandler } from '@ember-data/legacy-compat';
51
61
 
52
62
  export default class extends Store {
53
- requestManager = new RequestManager();
54
-
55
- constructor(args) {
56
- super(args);
57
- this.requestManager.use([LegacyNetworkHandler]);
58
- this.requestManager.useCache(CacheHandler);
59
- }
63
+ requestManager = new RequestManager()
64
+ .use([LegacyNetworkHandler])
65
+ .useCache(CacheHandler);
60
66
  }
61
67
  ```
62
68
 
package/addon-main.cjs ADDED
@@ -0,0 +1,5 @@
1
+ 'use strict';
2
+
3
+ const { addonShim } = require('@warp-drive/build-config/addon-shim.cjs');
4
+
5
+ module.exports = addonShim(__dirname);
@@ -1,14 +1,80 @@
1
- const extendFromApplicationEntity = require('@ember-data/private-build-infra/src/utilities/extend-from-application-entity');
2
- const useEditionDetector = require('@ember-data/private-build-infra/src/utilities/edition-detector');
1
+ const path = require('path');
2
+ const fs = require('fs');
3
3
 
4
- module.exports = useEditionDetector({
5
- description: 'Generates an ember-data serializer.',
4
+ const stringUtil = require('ember-cli-string-utils');
5
+ const pathUtil = require('ember-cli-path-utils');
6
+
7
+ const { has } = require('@ember/edition-utils');
8
+
9
+ module.exports = {
10
+ description: 'Generates an ember-data Serializer.',
6
11
 
7
12
  availableOptions: [{ name: 'base-class', type: String }],
8
13
 
9
14
  root: __dirname,
10
15
 
16
+ filesPath() {
17
+ let hasOctane = has('octane');
18
+ if (hasOctane && process.env.EMBER_EDITION === 'classic') {
19
+ hasOctane = false; //forcible override
20
+ }
21
+ let rootPath = hasOctane ? 'native-files' : 'files';
22
+ return path.join(__dirname, rootPath);
23
+ },
24
+
11
25
  locals(options) {
12
26
  return extendFromApplicationEntity('serializer', 'JSONAPISerializer', options);
13
27
  },
14
- });
28
+ };
29
+
30
+ function extendFromApplicationEntity(type, baseClass, options) {
31
+ let isAddon = options.inRepoAddon || options.project.isEmberCLIAddon();
32
+
33
+ let entityName = options.entity.name;
34
+ let relativePath = pathUtil.getRelativePath(options.entity.name);
35
+
36
+ if (options.pod && options.podPath) {
37
+ relativePath = pathUtil.getRelativePath(options.podPath + options.entity.name);
38
+ }
39
+
40
+ let applicationEntityPath = path.join(options.project.root, 'app', `${type}s`, 'application.js');
41
+
42
+ let hasApplicationEntity = fs.existsSync(applicationEntityPath);
43
+ if (!isAddon && !options.baseClass && entityName !== 'application' && hasApplicationEntity) {
44
+ options.baseClass = 'application';
45
+ }
46
+
47
+ if (options.baseClass === entityName) {
48
+ throw new Error(
49
+ stringUtil.classify(type) +
50
+ 's cannot extend from themself. To resolve this, remove the `--base-class` option or change to a different base-class.'
51
+ );
52
+ }
53
+
54
+ let importStatement;
55
+
56
+ if (options.baseClass) {
57
+ let baseClassPath = options.baseClass;
58
+ baseClass = stringUtil.classify(baseClassPath.replace('/', '-'));
59
+ baseClass = baseClass + stringUtil.classify(type);
60
+
61
+ importStatement = `import ${baseClass} from '${relativePath}${baseClassPath}';`;
62
+ } else {
63
+ let baseClassPath = `@ember-data/${type}`;
64
+
65
+ if (baseClass.startsWith('JSONAPI')) {
66
+ baseClassPath += '/json-api';
67
+ }
68
+
69
+ if (baseClass.startsWith('REST')) {
70
+ baseClassPath += '/rest';
71
+ }
72
+
73
+ importStatement = `import ${baseClass} from '${baseClassPath}';`;
74
+ }
75
+
76
+ return {
77
+ importStatement,
78
+ baseClass,
79
+ };
80
+ }
@@ -1,15 +1,15 @@
1
1
  const path = require('path');
2
2
 
3
3
  const testInfo = require('ember-cli-test-info');
4
- const useTestFrameworkDetector = require('@ember-data/private-build-infra/src/utilities/test-framework-detector');
5
- const modulePrefixForProject = require('@ember-data/private-build-infra/src/utilities/module-prefix-for-project');
4
+ const { dasherize } = require('ember-cli-string-utils');
6
5
 
7
- module.exports = useTestFrameworkDetector({
8
- description: 'Generates a serializer unit test.',
6
+ module.exports = {
7
+ description: 'Generates an EmberData Serializer unit test',
8
+ supportsAddon() { return false; },
9
9
 
10
10
  root: __dirname,
11
11
 
12
- fileMapTokens(options) {
12
+ fileMapTokens() {
13
13
  return {
14
14
  __root__() {
15
15
  return 'tests';
@@ -21,9 +21,15 @@ module.exports = useTestFrameworkDetector({
21
21
  },
22
22
 
23
23
  locals(options) {
24
+ const modulePrefix = dasherize(options.project.config().modulePrefix);
24
25
  return {
25
26
  friendlyTestDescription: testInfo.description(options.entity.name, 'Unit', 'Serializer'),
26
- modulePrefix: modulePrefixForProject(options.project),
27
+ modulePrefix,
27
28
  };
28
29
  },
29
- });
30
+
31
+ filesPath() {
32
+ return path.join(__dirname, 'qunit-files')
33
+ }
34
+ };
35
+
@@ -1,24 +1,23 @@
1
- import { module, test } from 'qunit';
2
-
3
1
  import { setupTest } from '<%= modulePrefix %>/tests/helpers';
2
+ import { module, test } from 'qunit';
4
3
 
5
4
  module('<%= friendlyTestDescription %>', function (hooks) {
6
5
  setupTest(hooks);
7
6
 
8
7
  // Replace this with your real tests.
9
8
  test('it exists', function (assert) {
10
- let store = this.owner.lookup('service:store');
11
- let serializer = store.serializerFor('<%= dasherizedModuleName %>');
9
+ const store = this.owner.lookup('service:store');
10
+ const serializer = store.serializerFor('<%= dasherizedModuleName %>');
12
11
 
13
- assert.ok(serializer);
12
+ assert.ok(serializer, 'serializer exists');
14
13
  });
15
14
 
16
15
  test('it serializes records', function (assert) {
17
- let store = this.owner.lookup('service:store');
18
- let record = store.createRecord('<%= dasherizedModuleName %>', {});
16
+ const store = this.owner.lookup('service:store');
17
+ const record = store.createRecord('<%= dasherizedModuleName %>', {});
19
18
 
20
- let serializedRecord = record.serialize();
19
+ const serializedRecord = record.serialize();
21
20
 
22
- assert.ok(serializedRecord);
21
+ assert.ok(serializedRecord, 'it serializes records');
23
22
  });
24
23
  });
@@ -1,7 +1,17 @@
1
- const useEditionDetector = require('@ember-data/private-build-infra/src/utilities/edition-detector');
1
+ const path = require('path');
2
2
 
3
- module.exports = useEditionDetector({
4
- description: 'Generates an ember-data value transform.',
3
+ const { has } = require('@ember/edition-utils');
5
4
 
5
+ module.exports = {
6
+ description: 'Generates an ember-data Transform.',
6
7
  root: __dirname,
7
- });
8
+
9
+ filesPath() {
10
+ let hasOctane = has('octane');
11
+ if (hasOctane && process.env.EMBER_EDITION === 'classic') {
12
+ hasOctane = false; //forcible override
13
+ }
14
+ let rootPath = hasOctane ? 'native-files' : 'files';
15
+ return path.join(__dirname, rootPath);
16
+ },
17
+ };
@@ -1,15 +1,15 @@
1
1
  const path = require('path');
2
2
 
3
3
  const testInfo = require('ember-cli-test-info');
4
- const useTestFrameworkDetector = require('@ember-data/private-build-infra/src/utilities/test-framework-detector');
5
- const modulePrefixForProject = require('@ember-data/private-build-infra/src/utilities/module-prefix-for-project');
4
+ const { dasherize } = require('ember-cli-string-utils');
6
5
 
7
- module.exports = useTestFrameworkDetector({
8
- description: 'Generates a transform unit test.',
6
+ module.exports = {
7
+ description: 'Generates an EmberData Transform unit test',
8
+ supportsAddon() { return false; },
9
9
 
10
10
  root: __dirname,
11
11
 
12
- fileMapTokens(options) {
12
+ fileMapTokens() {
13
13
  return {
14
14
  __root__() {
15
15
  return 'tests';
@@ -21,9 +21,15 @@ module.exports = useTestFrameworkDetector({
21
21
  },
22
22
 
23
23
  locals(options) {
24
+ const modulePrefix = dasherize(options.project.config().modulePrefix);
24
25
  return {
25
26
  friendlyTestDescription: testInfo.description(options.entity.name, 'Unit', 'Transform'),
26
- modulePrefix: modulePrefixForProject(options.project),
27
+ modulePrefix,
27
28
  };
28
29
  },
29
- });
30
+
31
+ filesPath() {
32
+ return path.join(__dirname, 'qunit-files')
33
+ }
34
+ };
35
+
@@ -1,13 +1,12 @@
1
- import { module, test } from 'qunit';
2
-
3
1
  import { setupTest } from '<%= modulePrefix %>/tests/helpers';
2
+ import { module, test } from 'qunit';
4
3
 
5
4
  module('<%= friendlyTestDescription %>', function (hooks) {
6
5
  setupTest(hooks);
7
6
 
8
7
  // Replace this with your real tests.
9
8
  test('it exists', function (assert) {
10
- let transform = this.owner.lookup('transform:<%= dasherizedModuleName %>');
11
- assert.ok(transform);
9
+ const transform = this.owner.lookup('transform:<%= dasherizedModuleName %>');
10
+ assert.ok(transform, 'transform exists');
12
11
  });
13
12
  });
package/dist/index.js ADDED
@@ -0,0 +1,306 @@
1
+ import EmberObject from '@ember/object';
2
+ import * as s from '@ember/service';
3
+ const deferred = /* @__PURE__ */new WeakMap();
4
+ function deferDecorator(proto, prop, desc) {
5
+ let map = deferred.get(proto);
6
+ if (!map) {
7
+ map = /* @__PURE__ */new Map();
8
+ deferred.set(proto, map);
9
+ }
10
+ map.set(prop, desc);
11
+ }
12
+ function findDeferredDecorator(target, prop) {
13
+ var _a;
14
+ let cursor = target.prototype;
15
+ while (cursor) {
16
+ let desc = (_a = deferred.get(cursor)) == null ? void 0 : _a.get(prop);
17
+ if (desc) {
18
+ return desc;
19
+ }
20
+ cursor = cursor.prototype;
21
+ }
22
+ }
23
+ function decorateFieldV2(prototype, prop, decorators, initializer) {
24
+ let desc = {
25
+ configurable: true,
26
+ enumerable: true,
27
+ writable: true,
28
+ initializer: null
29
+ };
30
+ if (initializer) {
31
+ desc.initializer = initializer;
32
+ }
33
+ for (let decorator of decorators) {
34
+ desc = decorator(prototype, prop, desc) || desc;
35
+ }
36
+ if (desc.initializer === void 0) {
37
+ Object.defineProperty(prototype, prop, desc);
38
+ } else {
39
+ deferDecorator(prototype, prop, desc);
40
+ }
41
+ }
42
+ function initializeDeferredDecorator(target, prop) {
43
+ let desc = findDeferredDecorator(target.constructor, prop);
44
+ if (desc) {
45
+ Object.defineProperty(target, prop, {
46
+ enumerable: desc.enumerable,
47
+ configurable: desc.configurable,
48
+ writable: desc.writable,
49
+ value: desc.initializer ? desc.initializer.call(target) : void 0
50
+ });
51
+ }
52
+ }
53
+
54
+ /**
55
+ ## Overview
56
+
57
+ <blockquote style="margin: 1em; padding: .1em 1em .1em 1em; border-left: solid 1em #E34C32; background: #e0e0e0;">
58
+ <p>
59
+ ⚠️ <strong>This is LEGACY documentation</strong> for a feature that is no longer encouraged to be used.
60
+ If starting a new app or thinking of implementing a new serializer, consider writing a
61
+ <a href="/ember-data/release/classes/%3CInterface%3E%20Handler">Handler</a> instead to be used with the <a href="https://github.com/emberjs/data/tree/main/packages/request#readme">RequestManager</a>
62
+ </p>
63
+ </blockquote>
64
+
65
+ In order to properly manage and present your data, EmberData
66
+ needs to understand the structure of data it receives.
67
+
68
+ `Serializers` convert data between the server's API format and
69
+ the format EmberData understands.
70
+
71
+ Data received from an API response is **normalized** into
72
+ [JSON:API](https://jsonapi.org/) (the format used internally
73
+ by EmberData), while data sent to an API is **serialized**
74
+ into the format the API expects.
75
+
76
+ ### Implementing a Serializer
77
+
78
+ There are only two required serializer methods, one for
79
+ normalizing data from the server API format into JSON:API, and
80
+ another for serializing records via `Snapshots` into the expected
81
+ server API format.
82
+
83
+ To implement a serializer, export a class that conforms to the structure
84
+ described by [<Interface> Serializer](/ember-data/release/classes/%3CInterface%3E%20Serializer)
85
+ from the `app/serializers/` directory. An example is below.
86
+
87
+ ```ts
88
+ import EmberObject from '@ember/object';
89
+
90
+ export default class ApplicationSerializer extends EmberObject {
91
+ normalizeResponse(store, schema, rawPayload) {
92
+ return rawPayload;
93
+ }
94
+
95
+ serialize(snapshot, options) {
96
+ const serializedResource = {
97
+ id: snapshot.id,
98
+ type: snapshot.modelName,
99
+ attributes: snapshot.attributes()
100
+ };
101
+
102
+ return serializedResource;
103
+ }
104
+ }
105
+ ```
106
+
107
+
108
+ ### Serializer Resolution
109
+
110
+ `store.serializerFor(name)` will lookup serializers defined in
111
+ `app/serializers/` and return an instance. If no serializer is found, an
112
+ error will be thrown.
113
+
114
+ `serializerFor` first attempts to find a serializer with an exact match on `name`,
115
+ then falls back to checking for the presence of a serializer named `application`.
116
+
117
+ ```ts
118
+ store.serializerFor('author');
119
+
120
+ // lookup paths (in order) =>
121
+ // app/serializers/author.js
122
+ // app/serializers/application.js
123
+ ```
124
+
125
+ Most requests in EmberData are made with respect to a particular `type` (or `modelName`)
126
+ (e.g., "get me the full collection of **books**" or "get me the **employee** whose id is 37"). We
127
+ refer to this as the **primary** resource `type`.
128
+
129
+ Typically `serializerFor` will be used to find a serializer with a name matching that of the primary
130
+ resource `type` for the request, falling back to the `application` serializer for those types that
131
+ do not have a defined serializer. This is often described as a `per-model` or `per-type` strategy
132
+ for defining serializers. However, because APIs rarely format payloads per-type but rather
133
+ per-API-version, this may not be a desired strategy.
134
+
135
+ It is recommended that applications define only a single `application` adapter and serializer
136
+ where possible.
137
+
138
+ If you have multiple API formats and the per-type strategy is not viable, one strategy is to
139
+ write an `application` adapter and serializer that make use of `options` to specify the desired
140
+ format when making a request.
141
+
142
+ ### Using a Serializer
143
+
144
+ Any serializer in `app/serializers/` can be looked up by `name` using `store.serializerFor(name)`.
145
+
146
+ ### Default Serializers
147
+
148
+ For applications whose APIs are *very close to* or *exactly* the **REST** format or **JSON:API**
149
+ format the `@ember-data/serializer` package contains implementations these applications can
150
+ extend. It also contains a simple `JSONSerializer` for serializing to/from very basic JSON objects.
151
+
152
+ Many applications will find writing their own serializer to be more performant and less
153
+ complex than extending these classes even when their API format is very close to that expected
154
+ by these serializers.
155
+
156
+ It is recommended that apps write their own serializer to best suit the needs of their API and
157
+ application.
158
+
159
+ @module @ember-data/serializer
160
+ @main @ember-data/serializer
161
+ */
162
+
163
+ const service = s.service ?? s.inject;
164
+
165
+ /**
166
+ > ⚠️ CAUTION you likely want the docs for [<Interface> Serializer](/ember-data/release/classes/%3CInterface%3E%20Serializer)
167
+ > as extending this abstract class is unnecessary.
168
+
169
+ `Serializer` is an abstract base class that you may override in your
170
+ application to customize it for your backend. The minimum set of methods
171
+ that you should implement is:
172
+
173
+ * `normalizeResponse()`
174
+ * `serialize()`
175
+
176
+ And you can optionally override the following methods:
177
+
178
+ * `normalize()`
179
+
180
+ For an example implementation, see
181
+ [JSONSerializer](JSONSerializer), the included JSON serializer.
182
+
183
+ @class Serializer
184
+ @public
185
+ @extends Ember.EmberObject
186
+ */
187
+
188
+ class Serializer extends EmberObject {
189
+ static {
190
+ decorateFieldV2(this.prototype, "store", [service]);
191
+ }
192
+ #store = (initializeDeferredDecorator(this, "store"), void 0);
193
+ /**
194
+ The `store` property is the application's `store` that contains
195
+ all records. It can be used to look up serializers for other model
196
+ types that may be nested inside the payload response.
197
+ Example:
198
+ ```js
199
+ Serializer.extend({
200
+ extractRelationship(relationshipModelName, relationshipHash) {
201
+ let modelClass = this.store.modelFor(relationshipModelName);
202
+ let relationshipSerializer = this.store.serializerFor(relationshipModelName);
203
+ return relationshipSerializer.normalize(modelClass, relationshipHash);
204
+ }
205
+ });
206
+ ```
207
+ @property store
208
+ @type {Store}
209
+ @public
210
+ */
211
+
212
+ /**
213
+ The `normalizeResponse` method is used to normalize a payload from the
214
+ server to a JSON-API Document.
215
+ http://jsonapi.org/format/#document-structure
216
+ Example:
217
+ ```js
218
+ Serializer.extend({
219
+ normalizeResponse(store, primaryModelClass, payload, id, requestType) {
220
+ if (requestType === 'findRecord') {
221
+ return this.normalize(primaryModelClass, payload);
222
+ } else {
223
+ return payload.reduce(function(documentHash, item) {
224
+ let { data, included } = this.normalize(primaryModelClass, item);
225
+ documentHash.included.push(...included);
226
+ documentHash.data.push(data);
227
+ return documentHash;
228
+ }, { data: [], included: [] })
229
+ }
230
+ }
231
+ });
232
+ ```
233
+ @since 1.13.0
234
+ @method normalizeResponse
235
+ @public
236
+ @param {Store} store
237
+ @param {Model} primaryModelClass
238
+ @param {Object} payload
239
+ @param {String|Number} id
240
+ @param {String} requestType
241
+ @return {Object} JSON-API Document
242
+ */
243
+
244
+ /**
245
+ The `serialize` method is used when a record is saved in order to convert
246
+ the record into the form that your external data source expects.
247
+ `serialize` takes an optional `options` hash with a single option:
248
+ - `includeId`: If this is `true`, `serialize` should include the ID
249
+ in the serialized object it builds.
250
+ Example:
251
+ ```js
252
+ Serializer.extend({
253
+ serialize(snapshot, options) {
254
+ let json = {
255
+ id: snapshot.id
256
+ };
257
+ snapshot.eachAttribute((key, attribute) => {
258
+ json[key] = snapshot.attr(key);
259
+ });
260
+ snapshot.eachRelationship((key, relationship) => {
261
+ if (relationship.kind === 'belongsTo') {
262
+ json[key] = snapshot.belongsTo(key, { id: true });
263
+ } else if (relationship.kind === 'hasMany') {
264
+ json[key] = snapshot.hasMany(key, { ids: true });
265
+ }
266
+ });
267
+ return json;
268
+ },
269
+ });
270
+ ```
271
+ @method serialize
272
+ @public
273
+ @param {Snapshot} snapshot
274
+ @param {Object} [options]
275
+ @return {Object}
276
+ */
277
+
278
+ /**
279
+ The `normalize` method is used to convert a payload received from your
280
+ external data source into the normalized form `store.push()` expects. You
281
+ should override this method, munge the hash and return the normalized
282
+ payload.
283
+ Example:
284
+ ```js
285
+ Serializer.extend({
286
+ normalize(modelClass, resourceHash) {
287
+ let data = {
288
+ id: resourceHash.id,
289
+ type: modelClass.modelName,
290
+ attributes: resourceHash
291
+ };
292
+ return { data: data };
293
+ }
294
+ })
295
+ ```
296
+ @method normalize
297
+ @public
298
+ @param {Model} typeClass
299
+ @param {Object} hash
300
+ @return {Object}
301
+ */
302
+ normalize(_typeClass, hash) {
303
+ return hash;
304
+ }
305
+ }
306
+ export { Serializer as default };