@ember-data/serializer 5.4.0-alpha.30 → 5.4.0-alpha.31

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 (27) hide show
  1. package/package.json +10 -9
  2. package/unstable-preview-types/-private/embedded-records-mixin.d.ts +5 -0
  3. package/unstable-preview-types/-private/embedded-records-mixin.d.ts.map +1 -0
  4. package/unstable-preview-types/-private/transforms/boolean.d.ts +48 -0
  5. package/unstable-preview-types/-private/transforms/boolean.d.ts.map +1 -0
  6. package/unstable-preview-types/-private/transforms/date.d.ts +29 -0
  7. package/unstable-preview-types/-private/transforms/date.d.ts.map +1 -0
  8. package/unstable-preview-types/-private/transforms/number.d.ts +30 -0
  9. package/unstable-preview-types/-private/transforms/number.d.ts.map +1 -0
  10. package/unstable-preview-types/-private/transforms/string.d.ts +30 -0
  11. package/unstable-preview-types/-private/transforms/string.d.ts.map +1 -0
  12. package/unstable-preview-types/-private/transforms/transform.d.ts +126 -0
  13. package/unstable-preview-types/-private/transforms/transform.d.ts.map +1 -0
  14. package/unstable-preview-types/-private/utils.d.ts +4 -0
  15. package/unstable-preview-types/-private/utils.d.ts.map +1 -0
  16. package/unstable-preview-types/-private.d.ts +11 -0
  17. package/unstable-preview-types/-private.d.ts.map +1 -0
  18. package/unstable-preview-types/index.d.ts +263 -0
  19. package/unstable-preview-types/index.d.ts.map +1 -0
  20. package/unstable-preview-types/json-api.d.ts +513 -0
  21. package/unstable-preview-types/json-api.d.ts.map +1 -0
  22. package/unstable-preview-types/json.d.ts +1092 -0
  23. package/unstable-preview-types/json.d.ts.map +1 -0
  24. package/unstable-preview-types/rest.d.ts +600 -0
  25. package/unstable-preview-types/rest.d.ts.map +1 -0
  26. package/unstable-preview-types/transform.d.ts +5 -0
  27. package/unstable-preview-types/transform.d.ts.map +1 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ember-data/serializer",
3
- "version": "5.4.0-alpha.30",
3
+ "version": "5.4.0-alpha.31",
4
4
  "description": "Provides Legacy JSON, JSON:API and REST Implementations of the Serializer Interface for use with @ember-data/store",
5
5
  "keywords": [
6
6
  "ember-addon"
@@ -19,6 +19,7 @@
19
19
  "version": 1
20
20
  },
21
21
  "files": [
22
+ "unstable-preview-types",
22
23
  "blueprints",
23
24
  "addon-main.js",
24
25
  "addon",
@@ -29,7 +30,7 @@
29
30
  ],
30
31
  "peerDependencies": {
31
32
  "@ember/string": "^3.1.1",
32
- "@warp-drive/core-types": "0.0.0-alpha.16",
33
+ "@warp-drive/core-types": "0.0.0-alpha.17",
33
34
  "ember-inflector": "^4.0.2"
34
35
  },
35
36
  "dependenciesMeta": {
@@ -56,7 +57,7 @@
56
57
  }
57
58
  },
58
59
  "dependencies": {
59
- "@ember-data/private-build-infra": "5.4.0-alpha.30",
60
+ "@ember-data/private-build-infra": "5.4.0-alpha.31",
60
61
  "@embroider/macros": "^1.13.5",
61
62
  "ember-cli-babel": "^8.2.0",
62
63
  "ember-cli-test-info": "^1.0.0",
@@ -73,16 +74,16 @@
73
74
  "@babel/preset-env": "^7.23.9",
74
75
  "@babel/preset-typescript": "^7.23.3",
75
76
  "@babel/runtime": "^7.23.9",
76
- "@ember-data/request": "5.4.0-alpha.30",
77
- "@ember-data/store": "5.4.0-alpha.30",
78
- "@ember-data/tracking": "5.4.0-alpha.30",
77
+ "@ember-data/request": "5.4.0-alpha.31",
78
+ "@ember-data/store": "5.4.0-alpha.31",
79
+ "@ember-data/tracking": "5.4.0-alpha.31",
79
80
  "@ember/string": "^3.1.1",
80
81
  "@embroider/addon-dev": "^4.1.2",
81
82
  "@glimmer/component": "^1.1.2",
82
83
  "@rollup/plugin-babel": "^6.0.4",
83
84
  "@rollup/plugin-node-resolve": "^15.2.3",
84
- "@warp-drive/core-types": "0.0.0-alpha.16",
85
- "@warp-drive/internal-config": "5.4.0-alpha.30",
85
+ "@warp-drive/core-types": "0.0.0-alpha.17",
86
+ "@warp-drive/internal-config": "5.4.0-alpha.31",
86
87
  "ember-inflector": "^4.0.2",
87
88
  "ember-source": "~5.6.0",
88
89
  "rollup": "^4.9.6",
@@ -99,7 +100,7 @@
99
100
  "packageManager": "pnpm@8.6.0",
100
101
  "scripts": {
101
102
  "lint": "eslint . --quiet --cache --cache-strategy=content --ext .js,.ts,.mjs,.cjs --report-unused-disable-directives",
102
- "build:types": "echo \"Types are private\" && exit 0",
103
+ "build:types": "tsc --build",
103
104
  "build:client": "rollup --config && babel ./addon --out-dir addon --plugins=../private-build-infra/src/transforms/babel-plugin-transform-ext.js",
104
105
  "_build": "bun run build:client && bun run build:types",
105
106
  "_syncPnpm": "bun run sync-dependencies-meta-injected"
@@ -0,0 +1,5 @@
1
+ /// <reference types="ember-source/types" />
2
+ declare const _default: Mixin;
3
+ export default _default;
4
+ import Mixin from '@ember/object/mixin';
5
+ //# sourceMappingURL=embedded-records-mixin.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"embedded-records-mixin.d.ts","sourceRoot":"","sources":["../../src/-private/embedded-records-mixin.js"],"names":[],"mappings":";;;kBACkB,qBAAqB"}
@@ -0,0 +1,48 @@
1
+ /**
2
+ @module @ember-data/serializer
3
+ */
4
+ /**
5
+ The `BooleanTransform` class is used to serialize and deserialize
6
+ boolean attributes on Ember Data record objects. This transform is
7
+ used when `boolean` is passed as the type parameter to the
8
+ [attr](/ember-data/release/functions/@ember-data%2Fmodel/attr) function.
9
+
10
+ Usage
11
+
12
+ ```app/models/user.js
13
+ import Model, { attr } from '@ember-data/model';
14
+
15
+ export default class UserModel extends Model {
16
+ @attr('boolean') isAdmin;
17
+ @attr('string') name;
18
+ @attr('string') email;
19
+ }
20
+ ```
21
+
22
+ By default, the boolean transform only allows for values of `true` or
23
+ `false`. You can opt into allowing `null` values for
24
+ boolean attributes via `attr('boolean', { allowNull: true })`
25
+
26
+ ```app/models/user.js
27
+ import Model, { attr } from '@ember-data/model';
28
+
29
+ export default class UserModel extends Model {
30
+ @attr('string') email;
31
+ @attr('string') username;
32
+ @attr('boolean', { allowNull: true }) wantsWeeklyEmail;
33
+ }
34
+ ```
35
+
36
+ @class BooleanTransform
37
+ @public
38
+ */
39
+ export default class BooleanTransform {
40
+ deserialize(serialized: boolean | null | number | string, options?: {
41
+ allowNull?: boolean;
42
+ }): boolean | null;
43
+ serialize(deserialized: boolean | null, options?: {
44
+ allowNull?: boolean;
45
+ }): boolean | null;
46
+ static create(): BooleanTransform;
47
+ }
48
+ //# sourceMappingURL=boolean.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"boolean.d.ts","sourceRoot":"","sources":["../../../src/-private/transforms/boolean.ts"],"names":[],"mappings":"AAAA;;EAEE;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,MAAM,CAAC,OAAO,OAAO,gBAAgB;IACnC,WAAW,CAAC,UAAU,EAAE,OAAO,GAAG,IAAI,GAAG,MAAM,GAAG,MAAM,EAAE,OAAO,CAAC,EAAE;QAAE,SAAS,CAAC,EAAE,OAAO,CAAA;KAAE,GAAG,OAAO,GAAG,IAAI;IAgB5G,SAAS,CAAC,YAAY,EAAE,OAAO,GAAG,IAAI,EAAE,OAAO,CAAC,EAAE;QAAE,SAAS,CAAC,EAAE,OAAO,CAAA;KAAE,GAAG,OAAO,GAAG,IAAI;IAQ1F,MAAM,CAAC,MAAM;CAGd"}
@@ -0,0 +1,29 @@
1
+ /**
2
+ @module @ember-data/serializer
3
+ */
4
+ /**
5
+ The `DateTransform` class is used to serialize and deserialize
6
+ date attributes on Ember Data record objects. This transform is used
7
+ when `date` is passed as the type parameter to the
8
+ [attr](/ember-data/release/functions/@ember-data%2Fmodel/attr) function. It uses the [`ISO 8601`](https://en.wikipedia.org/wiki/ISO_8601)
9
+ standard.
10
+
11
+ ```app/models/score.js
12
+ import Model, { attr, belongsTo } from '@ember-data/model';
13
+
14
+ export default class ScoreModel extends Model {
15
+ @attr('number') value;
16
+ @belongsTo('player') player;
17
+ @attr('date') date;
18
+ }
19
+ ```
20
+
21
+ @class DateTransform
22
+ @public
23
+ */
24
+ export default class DateTransform {
25
+ deserialize(serialized: string | number | null, _options?: Record<string, unknown>): Date | null;
26
+ serialize(date: Date, _options?: Record<string, unknown>): string | null;
27
+ static create(): DateTransform;
28
+ }
29
+ //# sourceMappingURL=date.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"date.d.ts","sourceRoot":"","sources":["../../../src/-private/transforms/date.ts"],"names":[],"mappings":"AAAA;;EAEE;AAEF;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,MAAM,CAAC,OAAO,OAAO,aAAa;IAChC,WAAW,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,EAAE,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAoBlF,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,GAAG,IAAI;IASxE,MAAM,CAAC,MAAM;CAGd"}
@@ -0,0 +1,30 @@
1
+ /**
2
+ @module @ember-data/serializer
3
+ */
4
+ /**
5
+ The `NumberTransform` class is used to serialize and deserialize
6
+ numeric attributes on Ember Data record objects. This transform is
7
+ used when `number` is passed as the type parameter to the
8
+ [attr](/ember-data/release/functions/@ember-data%2Fmodel/attr) function.
9
+
10
+ Usage
11
+
12
+ ```app/models/score.js
13
+ import Model, { attr, belongsTo } from '@ember-data/model';
14
+
15
+ export default class ScoreModel extends Model {
16
+ @attr('number') value;
17
+ @belongsTo('player') player;
18
+ @attr('date') date;
19
+ }
20
+ ```
21
+
22
+ @class NumberTransform
23
+ @public
24
+ */
25
+ export default class NumberTransform {
26
+ deserialize(serialized: string | number | null | undefined, _options?: Record<string, unknown>): number | null;
27
+ serialize(deserialized: string | number | null | undefined, _options?: Record<string, unknown>): number | null;
28
+ static create(): NumberTransform;
29
+ }
30
+ //# sourceMappingURL=number.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"number.d.ts","sourceRoot":"","sources":["../../../src/-private/transforms/number.ts"],"names":[],"mappings":"AAAA;;EAEE;AAMF;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,CAAC,OAAO,OAAO,eAAe;IAClC,WAAW,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,GAAG,SAAS,EAAE,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,GAAG,IAAI;IAU9G,SAAS,CAAC,YAAY,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,GAAG,SAAS,EAAE,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,GAAG,IAAI;IAU9G,MAAM,CAAC,MAAM;CAGd"}
@@ -0,0 +1,30 @@
1
+ /**
2
+ @module @ember-data/serializer
3
+ */
4
+ /**
5
+ The `StringTransform` class is used to serialize and deserialize
6
+ string attributes on Ember Data record objects. This transform is
7
+ used when `string` is passed as the type parameter to the
8
+ [attr](/ember-data/release/functions/@ember-data%2Fmodel/attr) function.
9
+
10
+ Usage
11
+
12
+ ```app/models/user.js
13
+ import Model, { attr, belongsTo } from '@ember-data/model';
14
+
15
+ export default class UserModel extends Model {
16
+ @attr('boolean') isAdmin;
17
+ @attr('string') name;
18
+ @attr('string') email;
19
+ }
20
+ ```
21
+
22
+ @class StringTransform
23
+ @public
24
+ */
25
+ export default class StringTransform {
26
+ deserialize(serialized: unknown, _options?: Record<string, unknown>): string | null;
27
+ serialize(deserialized: unknown, _options?: Record<string, unknown>): string | null;
28
+ static create(): StringTransform;
29
+ }
30
+ //# sourceMappingURL=string.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"string.d.ts","sourceRoot":"","sources":["../../../src/-private/transforms/string.ts"],"names":[],"mappings":"AAAA;;EAEE;AAEF;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,CAAC,OAAO,OAAO,eAAe;IAClC,WAAW,CAAC,UAAU,EAAE,OAAO,EAAE,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,GAAG,IAAI;IAGnF,SAAS,CAAC,YAAY,EAAE,OAAO,EAAE,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,GAAG,IAAI;IAInF,MAAM,CAAC,MAAM;CAGd"}
@@ -0,0 +1,126 @@
1
+ /// <reference types="ember-source/types" />
2
+ /**
3
+ @module @ember-data/serializer
4
+ */
5
+ import EmberObject from '@ember/object';
6
+ import type { AttributeSchema } from '@warp-drive/core-types/schema';
7
+ /**
8
+ The `Transform` class is used to serialize and deserialize model
9
+ attributes when they are saved or loaded from an
10
+ adapter. Subclassing `Transform` is useful for creating custom
11
+ attributes. All subclasses of `Transform` must implement a
12
+ `serialize` and a `deserialize` method.
13
+
14
+ Example
15
+
16
+ ```app/transforms/temperature.js
17
+
18
+ // Converts centigrade in the JSON to fahrenheit in the app
19
+ export default class TemperatureTransform {
20
+ deserialize(serialized, options) {
21
+ return (serialized * 1.8) + 32;
22
+ }
23
+
24
+ serialize(deserialized, options) {
25
+ return (deserialized - 32) / 1.8;
26
+ }
27
+
28
+ static create() {
29
+ return new this();
30
+ }
31
+ }
32
+ ```
33
+
34
+ Usage
35
+
36
+ ```app/models/requirement.js
37
+ import Model, { attr } from '@ember-data/model';
38
+
39
+ export default class RequirementModel extends Model {
40
+ @attr('string') name;
41
+ @attr('temperature') temperature;
42
+ }
43
+ ```
44
+
45
+ The options passed into the `attr` function when the attribute is
46
+ declared on the model is also available in the transform.
47
+
48
+ ```app/models/post.js
49
+ import Model, { attr } from '@ember-data/model';
50
+
51
+ export default class PostModel extends Model {
52
+ @attr('string') title;
53
+ @attr('markdown', {
54
+ markdown: {
55
+ gfm: false,
56
+ sanitize: true
57
+ }
58
+ })
59
+ markdown;
60
+ }
61
+ ```
62
+
63
+ ```app/transforms/markdown.js
64
+ export default class MarkdownTransform {
65
+ serialize(deserialized, options) {
66
+ return deserialized.raw;
67
+ }
68
+
69
+ deserialize(serialized, options) {
70
+ let markdownOptions = options.markdown || {};
71
+
72
+ return marked(serialized, markdownOptions);
73
+ }
74
+
75
+ static create() {
76
+ return new this();
77
+ }
78
+ }
79
+ ```
80
+
81
+ @class Transform
82
+ @public
83
+ */
84
+ /**
85
+ When given a deserialized value from a record attribute this
86
+ method must return the serialized value.
87
+
88
+ Example
89
+
90
+ ```javascript
91
+ serialize(deserialized, options) {
92
+ return deserialized ? null : Number(deserialized);
93
+ }
94
+ ```
95
+
96
+ @method serialize
97
+ @public
98
+ @param deserialized The deserialized value
99
+ @param options hash of options passed to `attr`
100
+ @return The serialized value
101
+ */
102
+ /**
103
+ When given a serialized value from a JSON object this method must
104
+ return the deserialized value for the record attribute.
105
+
106
+ Example
107
+
108
+ ```javascript
109
+ deserialize(serialized, options) {
110
+ return empty(serialized) ? null : Number(serialized);
111
+ }
112
+ ```
113
+
114
+ @method deserialize
115
+ @public
116
+ @param serialized The serialized value
117
+ @param options hash of options passed to `attr`
118
+ @return The deserialized value
119
+ */
120
+ interface Transform {
121
+ serialize(value: unknown, options: AttributeSchema['options']): unknown;
122
+ deserialize(value: unknown, options: AttributeSchema['options']): unknown;
123
+ }
124
+ declare const Transform: typeof EmberObject;
125
+ export default Transform;
126
+ //# sourceMappingURL=transform.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transform.d.ts","sourceRoot":"","sources":["../../../src/-private/transforms/transform.ts"],"names":[],"mappings":";AAAA;;EAEE;AACF,OAAO,WAAW,MAAM,eAAe,CAAC;AAExC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAC;AAErE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4EG;AACH;;;;;;;;;;;;;;;;;EAiBE;AACF;;;;;;;;;;;;;;;;;EAiBE;AACF,UAAU,SAAS;IACjB,SAAS,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,eAAe,CAAC,SAAS,CAAC,GAAG,OAAO,CAAC;IACxE,WAAW,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,eAAe,CAAC,SAAS,CAAC,GAAG,OAAO,CAAC;CAC3E;AACD,QAAA,MAAM,SAAS,oBAAc,CAAC;AAE9B,eAAe,SAAS,CAAC"}
@@ -0,0 +1,4 @@
1
+ type Coercable = string | number | boolean | null | undefined | symbol;
2
+ export declare function coerceId(id: Coercable): string | null;
3
+ export {};
4
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/-private/utils.ts"],"names":[],"mappings":"AAAA,KAAK,SAAS,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,GAAG,SAAS,GAAG,MAAM,CAAC;AAEvE,wBAAgB,QAAQ,CAAC,EAAE,EAAE,SAAS,GAAG,MAAM,GAAG,IAAI,CAUrD"}
@@ -0,0 +1,11 @@
1
+ /**
2
+ @module @ember-data/serializer
3
+ */
4
+ export { default as EmbeddedRecordsMixin } from './-private/embedded-records-mixin';
5
+ export { default as Transform } from './-private/transforms/transform';
6
+ export { default as BooleanTransform } from './-private/transforms/boolean';
7
+ export { default as DateTransform } from './-private/transforms/date';
8
+ export { default as NumberTransform } from './-private/transforms/number';
9
+ export { default as StringTransform } from './-private/transforms/string';
10
+ export { coerceId } from './-private/utils';
11
+ //# sourceMappingURL=-private.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"-private.d.ts","sourceRoot":"","sources":["../src/-private.ts"],"names":[],"mappings":"AAAA;;EAEE;AAEF,OAAO,EAAE,OAAO,IAAI,oBAAoB,EAAE,MAAM,mCAAmC,CAAC;AAEpF,OAAO,EAAE,OAAO,IAAI,SAAS,EAAE,MAAM,iCAAiC,CAAC;AACvE,OAAO,EAAE,OAAO,IAAI,gBAAgB,EAAE,MAAM,+BAA+B,CAAC;AAC5E,OAAO,EAAE,OAAO,IAAI,aAAa,EAAE,MAAM,4BAA4B,CAAC;AACtE,OAAO,EAAE,OAAO,IAAI,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAC1E,OAAO,EAAE,OAAO,IAAI,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAC1E,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC"}
@@ -0,0 +1,263 @@
1
+ /**
2
+ ## Overview
3
+
4
+ <blockquote style="margin: 1em; padding: .1em 1em .1em 1em; border-left: solid 1em #E34C32; background: #e0e0e0;">
5
+ <p>
6
+ ⚠️ <strong>This is LEGACY documentation</strong> for a feature that is no longer encouraged to be used.
7
+ If starting a new app or thinking of implementing a new serializer, consider writing a
8
+ <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>
9
+ </p>
10
+ </blockquote>
11
+
12
+ In order to properly manage and present your data, EmberData
13
+ needs to understand the structure of data it receives.
14
+
15
+ `Serializers` convert data between the server's API format and
16
+ the format EmberData understands.
17
+
18
+ Data received from an API response is **normalized** into
19
+ [JSON:API](https://jsonapi.org/) (the format used internally
20
+ by EmberData), while data sent to an API is **serialized**
21
+ into the format the API expects.
22
+
23
+ ### Implementing a Serializer
24
+
25
+ There are only two required serializer methods, one for
26
+ normalizing data from the server API format into JSON:API, and
27
+ another for serializing records via `Snapshots` into the expected
28
+ server API format.
29
+
30
+ To implement a serializer, export a class that conforms to the structure
31
+ described by [<Interface> Serializer](/ember-data/release/classes/%3CInterface%3E%20Serializer)
32
+ from the `app/serializers/` directory. An example is below.
33
+
34
+ ```ts
35
+ import EmberObject from '@ember/object';
36
+
37
+ export default class ApplicationSerializer extends EmberObject {
38
+ normalizeResponse(store, schema, rawPayload) {
39
+ return rawPayload;
40
+ }
41
+
42
+ serialize(snapshot, options) {
43
+ const serializedResource = {
44
+ id: snapshot.id,
45
+ type: snapshot.modelName,
46
+ attributes: snapshot.attributes()
47
+ };
48
+
49
+ return serializedResource;
50
+ }
51
+ }
52
+ ```
53
+
54
+
55
+ ### Serializer Resolution
56
+
57
+ `store.serializerFor(name)` will lookup serializers defined in
58
+ `app/serializers/` and return an instance. If no serializer is found, an
59
+ error will be thrown.
60
+
61
+ `serializerFor` first attempts to find a serializer with an exact match on `name`,
62
+ then falls back to checking for the presence of a serializer named `application`.
63
+
64
+ ```ts
65
+ store.serializerFor('author');
66
+
67
+ // lookup paths (in order) =>
68
+ // app/serializers/author.js
69
+ // app/serializers/application.js
70
+ ```
71
+
72
+ Most requests in EmberData are made with respect to a particular `type` (or `modelName`)
73
+ (e.g., "get me the full collection of **books**" or "get me the **employee** whose id is 37"). We
74
+ refer to this as the **primary** resource `type`.
75
+
76
+ Typically `serializerFor` will be used to find a serializer with a name matching that of the primary
77
+ resource `type` for the request, falling back to the `application` serializer for those types that
78
+ do not have a defined serializer. This is often described as a `per-model` or `per-type` strategy
79
+ for defining serializers. However, because APIs rarely format payloads per-type but rather
80
+ per-API-version, this may not be a desired strategy.
81
+
82
+ It is recommended that applications define only a single `application` adapter and serializer
83
+ where possible.
84
+
85
+ If you have multiple API formats and the per-type strategy is not viable, one strategy is to
86
+ write an `application` adapter and serializer that make use of `options` to specify the desired
87
+ format when making a request.
88
+
89
+ ### Using a Serializer
90
+
91
+ Any serializer in `app/serializers/` can be looked up by `name` using `store.serializerFor(name)`.
92
+
93
+ ### Default Serializers
94
+
95
+ For applications whose APIs are *very close to* or *exactly* the **REST** format or **JSON:API**
96
+ format the `@ember-data/serializer` package contains implementations these applications can
97
+ extend. It also contains a simple `JSONSerializer` for serializing to/from very basic JSON objects.
98
+
99
+ Many applications will find writing their own serializer to be more performant and less
100
+ complex than extending these classes even when their API format is very close to that expected
101
+ by these serializers.
102
+
103
+ It is recommended that apps write their own serializer to best suit the needs of their API and
104
+ application.
105
+
106
+ @module @ember-data/serializer
107
+ @main @ember-data/serializer
108
+ */
109
+ /// <reference types="ember-source/types" />
110
+ import EmberObject from '@ember/object';
111
+ import type Store from '@ember-data/store';
112
+ import type { ModelSchema } from '@ember-data/store/-types/q/ds-model';
113
+ /**
114
+ > ⚠️ CAUTION you likely want the docs for [<Interface> Serializer](/ember-data/release/classes/%3CInterface%3E%20Serializer)
115
+ > as extending this abstract class is unnecessary.
116
+
117
+ `Serializer` is an abstract base class that you may override in your
118
+ application to customize it for your backend. The minimum set of methods
119
+ that you should implement is:
120
+
121
+ * `normalizeResponse()`
122
+ * `serialize()`
123
+
124
+ And you can optionally override the following methods:
125
+
126
+ * `normalize()`
127
+
128
+ For an example implementation, see
129
+ [JSONSerializer](JSONSerializer), the included JSON serializer.
130
+
131
+ @class Serializer
132
+ @public
133
+ @extends Ember.EmberObject
134
+ */
135
+ export default class extends EmberObject {
136
+ store: Store;
137
+ /**
138
+ The `store` property is the application's `store` that contains
139
+ all records. It can be used to look up serializers for other model
140
+ types that may be nested inside the payload response.
141
+
142
+ Example:
143
+
144
+ ```js
145
+ Serializer.extend({
146
+ extractRelationship(relationshipModelName, relationshipHash) {
147
+ let modelClass = this.store.modelFor(relationshipModelName);
148
+ let relationshipSerializer = this.store.serializerFor(relationshipModelName);
149
+ return relationshipSerializer.normalize(modelClass, relationshipHash);
150
+ }
151
+ });
152
+ ```
153
+
154
+ @property store
155
+ @type {Store}
156
+ @public
157
+ */
158
+ /**
159
+ The `normalizeResponse` method is used to normalize a payload from the
160
+ server to a JSON-API Document.
161
+
162
+ http://jsonapi.org/format/#document-structure
163
+
164
+ Example:
165
+
166
+ ```js
167
+ Serializer.extend({
168
+ normalizeResponse(store, primaryModelClass, payload, id, requestType) {
169
+ if (requestType === 'findRecord') {
170
+ return this.normalize(primaryModelClass, payload);
171
+ } else {
172
+ return payload.reduce(function(documentHash, item) {
173
+ let { data, included } = this.normalize(primaryModelClass, item);
174
+ documentHash.included.push(...included);
175
+ documentHash.data.push(data);
176
+ return documentHash;
177
+ }, { data: [], included: [] })
178
+ }
179
+ }
180
+ });
181
+ ```
182
+
183
+ @since 1.13.0
184
+ @method normalizeResponse
185
+ @public
186
+ @param {Store} store
187
+ @param {Model} primaryModelClass
188
+ @param {Object} payload
189
+ @param {String|Number} id
190
+ @param {String} requestType
191
+ @return {Object} JSON-API Document
192
+ */
193
+ /**
194
+ The `serialize` method is used when a record is saved in order to convert
195
+ the record into the form that your external data source expects.
196
+
197
+ `serialize` takes an optional `options` hash with a single option:
198
+
199
+ - `includeId`: If this is `true`, `serialize` should include the ID
200
+ in the serialized object it builds.
201
+
202
+ Example:
203
+
204
+ ```js
205
+ Serializer.extend({
206
+ serialize(snapshot, options) {
207
+ let json = {
208
+ id: snapshot.id
209
+ };
210
+
211
+ snapshot.eachAttribute((key, attribute) => {
212
+ json[key] = snapshot.attr(key);
213
+ });
214
+
215
+ snapshot.eachRelationship((key, relationship) => {
216
+ if (relationship.kind === 'belongsTo') {
217
+ json[key] = snapshot.belongsTo(key, { id: true });
218
+ } else if (relationship.kind === 'hasMany') {
219
+ json[key] = snapshot.hasMany(key, { ids: true });
220
+ }
221
+ });
222
+
223
+ return json;
224
+ },
225
+ });
226
+ ```
227
+
228
+ @method serialize
229
+ @public
230
+ @param {Snapshot} snapshot
231
+ @param {Object} [options]
232
+ @return {Object}
233
+ */
234
+ /**
235
+ The `normalize` method is used to convert a payload received from your
236
+ external data source into the normalized form `store.push()` expects. You
237
+ should override this method, munge the hash and return the normalized
238
+ payload.
239
+
240
+ Example:
241
+
242
+ ```js
243
+ Serializer.extend({
244
+ normalize(modelClass, resourceHash) {
245
+ let data = {
246
+ id: resourceHash.id,
247
+ type: modelClass.modelName,
248
+ attributes: resourceHash
249
+ };
250
+ return { data: data };
251
+ }
252
+ })
253
+ ```
254
+
255
+ @method normalize
256
+ @public
257
+ @param {Model} typeClass
258
+ @param {Object} hash
259
+ @return {Object}
260
+ */
261
+ normalize(_typeClass: ModelSchema, hash: Record<string, unknown>): Record<string, unknown>;
262
+ }
263
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EA2GE;;AAEF,OAAO,WAAW,MAAM,eAAe,CAAC;AAGxC,OAAO,KAAK,KAAK,MAAM,mBAAmB,CAAC;AAC3C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,qCAAqC,CAAC;AAEvE;;;;;;;;;;;;;;;;;;;;;EAqBE;AAEF,MAAM,CAAC,OAAO,MAAO,SAAQ,WAAW;IACrB,KAAK,EAAE,KAAK,CAAC;IAC9B;;;;;;;;;;;;;;;;;;;;MAoBE;IAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;MAkCE;IAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;MAwCE;IAEF;;;;;;;;;;;;;;;;;;;;;;;;;;MA0BE;IACF,SAAS,CAAC,UAAU,EAAE,WAAW,EAAE,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;CAG3F"}