@ember-data/serializer 4.10.0-alpha.2 → 4.10.0-alpha.21

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 (39) hide show
  1. package/addon/-private.js +210 -0
  2. package/addon/-private.js.map +1 -0
  3. package/addon/{-private/embedded-records-mixin.js → embedded-records-mixin-0a9e9148.js} +87 -146
  4. package/addon/embedded-records-mixin-0a9e9148.js.map +1 -0
  5. package/addon/index.js +178 -0
  6. package/addon/index.js.map +1 -0
  7. package/addon/json-api.js +96 -233
  8. package/addon/json-api.js.map +1 -0
  9. package/addon/json.js +198 -432
  10. package/addon/json.js.map +1 -0
  11. package/addon/rest.js +133 -270
  12. package/addon/rest.js.map +1 -0
  13. package/addon/{-private/transforms/transform.js → transform-63fba437.js} +9 -15
  14. package/addon/transform-63fba437.js.map +1 -0
  15. package/addon/transform.js +3 -4
  16. package/addon/transform.js.map +1 -0
  17. package/addon-main.js +90 -0
  18. package/package.json +39 -8
  19. package/addon/-private/index.js +0 -11
  20. package/addon/-private/transforms/boolean.js +0 -70
  21. package/addon/-private/transforms/date.js +0 -59
  22. package/addon/-private/transforms/number.js +0 -57
  23. package/addon/-private/transforms/string.js +0 -38
  24. package/addon/index.ts +0 -259
  25. package/blueprints/serializer/files/__root__/__path__/__name__.js +0 -4
  26. package/blueprints/serializer/index.js +0 -14
  27. package/blueprints/serializer/native-files/__root__/__path__/__name__.js +0 -4
  28. package/blueprints/serializer-test/index.js +0 -29
  29. package/blueprints/serializer-test/mocha-files/__root__/__path__/__test__.js +0 -20
  30. package/blueprints/serializer-test/mocha-rfc-232-files/__root__/__path__/__test__.js +0 -25
  31. package/blueprints/serializer-test/qunit-files/__root__/__path__/__test__.js +0 -24
  32. package/blueprints/transform/files/__root__/__path__/__name__.js +0 -11
  33. package/blueprints/transform/index.js +0 -7
  34. package/blueprints/transform/native-files/__root__/__path__/__name__.js +0 -11
  35. package/blueprints/transform-test/index.js +0 -29
  36. package/blueprints/transform-test/mocha-files/__root__/__path__/__test__.js +0 -17
  37. package/blueprints/transform-test/mocha-rfc-232-files/__root__/__path__/__test__.js +0 -14
  38. package/blueprints/transform-test/qunit-files/__root__/__path__/__test__.js +0 -13
  39. package/index.js +0 -25
package/addon/index.ts DELETED
@@ -1,259 +0,0 @@
1
- /**
2
- ## Overview
3
-
4
- In order to properly manage and present your data, EmberData
5
- needs to understand the structure of data it receives.
6
-
7
- `Serializers` convert data between the server's API format and
8
- the format EmberData understands.
9
-
10
- Data received from an API response is **normalized** into
11
- [JSON:API](https://jsonapi.org/) (the format used internally
12
- by EmberData), while data sent to an API is **serialized**
13
- into the format the API expects.
14
-
15
- ### Implementing a Serializer
16
-
17
- There are only two required serializer methods, one for
18
- normalizing data from the server API format into JSON:API, and
19
- another for serializing records via `Snapshots` into the expected
20
- server API format.
21
-
22
- To implement a serializer, export a class that conforms to the structure
23
- described by the [MinimumSerializerInterface](/ember-data/release/classes/MinimumSerializerInterface)
24
- from the `app/serializers/` directory. An example is below.
25
-
26
- ```ts
27
- import EmberObject from '@ember/object';
28
-
29
- export default class ApplicationSerializer extends EmberObject {
30
- normalizeResponse(store, schema, rawPayload) {
31
- return rawPayload;
32
- }
33
-
34
- serialize(snapshot, options) {
35
- const serializedResource = {
36
- id: snapshot.id,
37
- type: snapshot.modelName,
38
- attributes: snapshot.attributes()
39
- };
40
-
41
- return serializedResource;
42
- }
43
- }
44
- ```
45
-
46
-
47
- ### Serializer Resolution
48
-
49
- `store.serializerFor(name)` will lookup serializers defined in
50
- `app/serializers/` and return an instance. If no serializer is found, an
51
- error will be thrown.
52
-
53
- `serializerFor` first attempts to find a serializer with an exact match on `name`,
54
- then falls back to checking for the presence of a serializer named `application`.
55
-
56
- ```ts
57
- store.serializerFor('author');
58
-
59
- // lookup paths (in order) =>
60
- // app/serializers/author.js
61
- // app/serializers/application.js
62
- ```
63
-
64
- Most requests in EmberData are made with respect to a particular `type` (or `modelName`)
65
- (e.g., "get me the full collection of **books**" or "get me the **employee** whose id is 37"). We
66
- refer to this as the **primary** resource `type`.
67
-
68
- Typically `serializerFor` will be used to find a serializer with a name matching that of the primary
69
- resource `type` for the request, falling back to the `application` serializer for those types that
70
- do not have a defined serializer. This is often described as a `per-model` or `per-type` strategy
71
- for defining serializers. However, because APIs rarely format payloads per-type but rather
72
- per-API-version, this may not be a desired strategy.
73
-
74
- It is recommended that applications define only a single `application` adapter and serializer
75
- where possible.
76
-
77
- If you have multiple API formats and the per-type strategy is not viable, one strategy is to
78
- write an `application` adapter and serializer that make use of `options` to specify the desired
79
- format when making a request.
80
-
81
- ### Using a Serializer
82
-
83
- Any serializer in `app/serializers/` can be looked up by `name` using `store.serializerFor(name)`.
84
-
85
- ### Default Serializers
86
-
87
- For applications whose APIs are *very close to* or *exactly* the **REST** format or **JSON:API**
88
- format the `@ember-data/serializer` package contains implementations these applications can
89
- extend. It also contains a simple `JSONSerializer` for serializing to/from very basic JSON objects.
90
-
91
- Many applications will find writing their own serializer to be more performant and less
92
- complex than extending these classes even when their API format is very close to that expected
93
- by these serializers.
94
-
95
- It is recommended that apps write their own serializer to best suit the needs of their API and
96
- application.
97
-
98
- @module @ember-data/serializer
99
- @main @ember-data/serializer
100
- */
101
-
102
- import EmberObject from '@ember/object';
103
- import { inject as service } from '@ember/service';
104
-
105
- import type Store from '@ember-data/store';
106
-
107
- /**
108
- `Serializer` is an abstract base class that you should override in your
109
- application to customize it for your backend. The minimum set of methods
110
- that you should implement is:
111
-
112
- * `normalizeResponse()`
113
- * `serialize()`
114
-
115
- And you can optionally override the following methods:
116
-
117
- * `normalize()`
118
-
119
- For an example implementation, see
120
- [JSONSerializer](JSONSerializer), the included JSON serializer.
121
-
122
- @class Serializer
123
- @public
124
- @extends Ember.EmberObject
125
- */
126
-
127
- export default class extends EmberObject {
128
- @service declare store: Store;
129
- /**
130
- The `store` property is the application's `store` that contains
131
- all records. It can be used to look up serializers for other model
132
- types that may be nested inside the payload response.
133
-
134
- Example:
135
-
136
- ```js
137
- Serializer.extend({
138
- extractRelationship(relationshipModelName, relationshipHash) {
139
- let modelClass = this.store.modelFor(relationshipModelName);
140
- let relationshipSerializer = this.store.serializerFor(relationshipModelName);
141
- return relationshipSerializer.normalize(modelClass, relationshipHash);
142
- }
143
- });
144
- ```
145
-
146
- @property store
147
- @type {Store}
148
- @public
149
- */
150
-
151
- /**
152
- The `normalizeResponse` method is used to normalize a payload from the
153
- server to a JSON-API Document.
154
-
155
- http://jsonapi.org/format/#document-structure
156
-
157
- Example:
158
-
159
- ```js
160
- Serializer.extend({
161
- normalizeResponse(store, primaryModelClass, payload, id, requestType) {
162
- if (requestType === 'findRecord') {
163
- return this.normalize(primaryModelClass, payload);
164
- } else {
165
- return payload.reduce(function(documentHash, item) {
166
- let { data, included } = this.normalize(primaryModelClass, item);
167
- documentHash.included.push(...included);
168
- documentHash.data.push(data);
169
- return documentHash;
170
- }, { data: [], included: [] })
171
- }
172
- }
173
- });
174
- ```
175
-
176
- @since 1.13.0
177
- @method normalizeResponse
178
- @public
179
- @param {Store} store
180
- @param {Model} primaryModelClass
181
- @param {Object} payload
182
- @param {String|Number} id
183
- @param {String} requestType
184
- @return {Object} JSON-API Document
185
- */
186
-
187
- /**
188
- The `serialize` method is used when a record is saved in order to convert
189
- the record into the form that your external data source expects.
190
-
191
- `serialize` takes an optional `options` hash with a single option:
192
-
193
- - `includeId`: If this is `true`, `serialize` should include the ID
194
- in the serialized object it builds.
195
-
196
- Example:
197
-
198
- ```js
199
- Serializer.extend({
200
- serialize(snapshot, options) {
201
- let json = {
202
- id: snapshot.id
203
- };
204
-
205
- snapshot.eachAttribute((key, attribute) => {
206
- json[key] = snapshot.attr(key);
207
- });
208
-
209
- snapshot.eachRelationship((key, relationship) => {
210
- if (relationship.kind === 'belongsTo') {
211
- json[key] = snapshot.belongsTo(key, { id: true });
212
- } else if (relationship.kind === 'hasMany') {
213
- json[key] = snapshot.hasMany(key, { ids: true });
214
- }
215
- });
216
-
217
- return json;
218
- },
219
- });
220
- ```
221
-
222
- @method serialize
223
- @public
224
- @param {Snapshot} snapshot
225
- @param {Object} [options]
226
- @return {Object}
227
- */
228
-
229
- /**
230
- The `normalize` method is used to convert a payload received from your
231
- external data source into the normalized form `store.push()` expects. You
232
- should override this method, munge the hash and return the normalized
233
- payload.
234
-
235
- Example:
236
-
237
- ```js
238
- Serializer.extend({
239
- normalize(modelClass, resourceHash) {
240
- let data = {
241
- id: resourceHash.id,
242
- type: modelClass.modelName,
243
- attributes: resourceHash
244
- };
245
- return { data: data };
246
- }
247
- })
248
- ```
249
-
250
- @method normalize
251
- @public
252
- @param {Model} typeClass
253
- @param {Object} hash
254
- @return {Object}
255
- */
256
- normalize(typeClass, hash) {
257
- return hash;
258
- }
259
- }
@@ -1,4 +0,0 @@
1
- <%= importStatement %>
2
-
3
- export default <%= baseClass %>.extend({
4
- });
@@ -1,14 +0,0 @@
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');
3
-
4
- module.exports = useEditionDetector({
5
- description: 'Generates an ember-data serializer.',
6
-
7
- availableOptions: [{ name: 'base-class', type: String }],
8
-
9
- root: __dirname,
10
-
11
- locals(options) {
12
- return extendFromApplicationEntity('serializer', 'JSONAPISerializer', options);
13
- },
14
- });
@@ -1,4 +0,0 @@
1
- <%= importStatement %>
2
-
3
- export default class <%= classifiedModuleName %>Serializer extends <%= baseClass %> {
4
- }
@@ -1,29 +0,0 @@
1
- const path = require('path');
2
-
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');
6
-
7
- module.exports = useTestFrameworkDetector({
8
- description: 'Generates a serializer unit test.',
9
-
10
- root: __dirname,
11
-
12
- fileMapTokens(options) {
13
- return {
14
- __root__() {
15
- return 'tests';
16
- },
17
- __path__() {
18
- return path.join('unit', 'serializers');
19
- },
20
- };
21
- },
22
-
23
- locals(options) {
24
- return {
25
- friendlyTestDescription: testInfo.description(options.entity.name, 'Unit', 'Serializer'),
26
- modulePrefix: modulePrefixForProject(options.project),
27
- };
28
- },
29
- });
@@ -1,20 +0,0 @@
1
- import { expect } from 'chai';
2
- import { describe, it } from 'mocha';
3
-
4
- import { setupModelTest } from 'ember-mocha';
5
-
6
- describe('<%= friendlyTestDescription %>', function () {
7
- setupModelTest('<%= dasherizedModuleName %>', {
8
- // Specify the other units that are required for this test.
9
- needs: ['serializer:<%= dasherizedModuleName %>'],
10
- });
11
-
12
- // Replace this with your real tests.
13
- it('serializes records', function () {
14
- let record = this.subject();
15
-
16
- let serializedRecord = record.serialize();
17
-
18
- expect(serializedRecord).to.be.ok;
19
- });
20
- });
@@ -1,25 +0,0 @@
1
- import { expect } from 'chai';
2
- import { describe, it } from 'mocha';
3
-
4
- import { setupTest } from '<%= modulePrefix %>/tests/helpers';
5
-
6
- describe('<%= friendlyTestDescription %>', function () {
7
- setupTest();
8
-
9
- // Replace this with your real tests.
10
- it('exists', function () {
11
- let store = this.owner.lookup('service:store');
12
- let serializer = store.serializerFor('<%= dasherizedModuleName %>');
13
-
14
- expect(serializer).to.be.ok;
15
- });
16
-
17
- it('serializes records', function () {
18
- let store = this.owner.lookup('service:store');
19
- let record = store.createRecord('<%= dasherizedModuleName %>', {});
20
-
21
- let serializedRecord = record.serialize();
22
-
23
- expect(serializedRecord).to.be.ok;
24
- });
25
- });
@@ -1,24 +0,0 @@
1
- import { module, test } from 'qunit';
2
-
3
- import { setupTest } from '<%= modulePrefix %>/tests/helpers';
4
-
5
- module('<%= friendlyTestDescription %>', function (hooks) {
6
- setupTest(hooks);
7
-
8
- // Replace this with your real tests.
9
- test('it exists', function (assert) {
10
- let store = this.owner.lookup('service:store');
11
- let serializer = store.serializerFor('<%= dasherizedModuleName %>');
12
-
13
- assert.ok(serializer);
14
- });
15
-
16
- test('it serializes records', function (assert) {
17
- let store = this.owner.lookup('service:store');
18
- let record = store.createRecord('<%= dasherizedModuleName %>', {});
19
-
20
- let serializedRecord = record.serialize();
21
-
22
- assert.ok(serializedRecord);
23
- });
24
- });
@@ -1,11 +0,0 @@
1
- import Transform from '@ember-data/serializer/transform';
2
-
3
- export default Transform.extend({
4
- deserialize(serialized) {
5
- return serialized;
6
- },
7
-
8
- serialize(deserialized) {
9
- return deserialized;
10
- }
11
- });
@@ -1,7 +0,0 @@
1
- const useEditionDetector = require('@ember-data/private-build-infra/src/utilities/edition-detector');
2
-
3
- module.exports = useEditionDetector({
4
- description: 'Generates an ember-data value transform.',
5
-
6
- root: __dirname,
7
- });
@@ -1,11 +0,0 @@
1
- import Transform from '@ember-data/serializer/transform';
2
-
3
- export default class <%= classifiedModuleName %>Transform extends Transform {
4
- deserialize(serialized) {
5
- return serialized;
6
- }
7
-
8
- serialize(deserialized) {
9
- return deserialized;
10
- }
11
- }
@@ -1,29 +0,0 @@
1
- const path = require('path');
2
-
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');
6
-
7
- module.exports = useTestFrameworkDetector({
8
- description: 'Generates a transform unit test.',
9
-
10
- root: __dirname,
11
-
12
- fileMapTokens(options) {
13
- return {
14
- __root__() {
15
- return 'tests';
16
- },
17
- __path__() {
18
- return path.join('unit', 'transforms');
19
- },
20
- };
21
- },
22
-
23
- locals(options) {
24
- return {
25
- friendlyTestDescription: testInfo.description(options.entity.name, 'Unit', 'Transform'),
26
- modulePrefix: modulePrefixForProject(options.project),
27
- };
28
- },
29
- });
@@ -1,17 +0,0 @@
1
- import { expect } from 'chai';
2
- import { describe, it } from 'mocha';
3
-
4
- import { setupTest } from 'ember-mocha';
5
-
6
- describe('<%= friendlyTestDescription %>', function () {
7
- setupTest('transform:<%= dasherizedModuleName %>', {
8
- // Specify the other units that are required for this test.
9
- // needs: ['transform:foo']
10
- });
11
-
12
- // Replace this with your real tests.
13
- it('exists', function () {
14
- let transform = this.subject();
15
- expect(transform).to.be.ok;
16
- });
17
- });
@@ -1,14 +0,0 @@
1
- import { expect } from 'chai';
2
- import { describe, it } from 'mocha';
3
-
4
- import { setupTest } from '<%= modulePrefix %>/tests/helpers';
5
-
6
- describe('<%= friendlyTestDescription %>', function () {
7
- setupTest();
8
-
9
- // Replace this with your real tests.
10
- it('exists', function () {
11
- let transform = this.owner.lookup('transform:<%= dasherizedModuleName %>');
12
- expect(transform).to.be.ok;
13
- });
14
- });
@@ -1,13 +0,0 @@
1
- import { module, test } from 'qunit';
2
-
3
- import { setupTest } from '<%= modulePrefix %>/tests/helpers';
4
-
5
- module('<%= friendlyTestDescription %>', function (hooks) {
6
- setupTest(hooks);
7
-
8
- // Replace this with your real tests.
9
- test('it exists', function (assert) {
10
- let transform = this.owner.lookup('transform:<%= dasherizedModuleName %>');
11
- assert.ok(transform);
12
- });
13
- });
package/index.js DELETED
@@ -1,25 +0,0 @@
1
- 'use strict';
2
-
3
- const addonBuildConfigForDataPackage = require('@ember-data/private-build-infra/src/addon-build-config-for-data-package');
4
-
5
- const name = require('./package').name;
6
-
7
- const addonBaseConfig = addonBuildConfigForDataPackage(name);
8
-
9
- module.exports = Object.assign({}, addonBaseConfig, {
10
- shouldRollupPrivate: true,
11
- externalDependenciesForPrivateModule() {
12
- return [
13
- '@ember/object',
14
- '@ember/application',
15
- '@ember/string',
16
- '@ember/utils',
17
- '@ember/debug',
18
- '@ember/polyfills',
19
- '@ember/array',
20
- '@ember/object/mixin',
21
- '@ember/string',
22
- 'ember-inflector',
23
- ];
24
- },
25
- });