@ember-data-mirror/serializer 4.13.0-alpha.1
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.
- package/LICENSE.md +11 -0
- package/README.md +107 -0
- package/addon-main.cjs +5 -0
- package/blueprints/serializer/files/__root__/__path__/__name__.js +4 -0
- package/blueprints/serializer/index.js +80 -0
- package/blueprints/serializer/native-files/__root__/__path__/__name__.js +4 -0
- package/blueprints/serializer-test/index.js +35 -0
- package/blueprints/serializer-test/qunit-files/__root__/__path__/__test__.js +23 -0
- package/blueprints/transform/files/__root__/__path__/__name__.js +13 -0
- package/blueprints/transform/index.js +17 -0
- package/blueprints/transform/native-files/__root__/__path__/__name__.js +13 -0
- package/blueprints/transform-test/index.js +35 -0
- package/blueprints/transform-test/qunit-files/__root__/__path__/__test__.js +12 -0
- package/dist/index.js +304 -0
- package/dist/index.js.map +1 -0
- package/dist/json-BwMH6O_R.js +1396 -0
- package/dist/json-BwMH6O_R.js.map +1 -0
- package/dist/json-api.js +529 -0
- package/dist/json-api.js.map +1 -0
- package/dist/json.js +6 -0
- package/dist/json.js.map +1 -0
- package/dist/rest.js +1270 -0
- package/dist/rest.js.map +1 -0
- package/dist/transform.js +336 -0
- package/dist/transform.js.map +1 -0
- package/package.json +116 -0
- package/unstable-preview-types/-private/embedded-records-mixin.d.ts +102 -0
- package/unstable-preview-types/-private/embedded-records-mixin.d.ts.map +1 -0
- package/unstable-preview-types/-private/transforms/boolean.d.ts +52 -0
- package/unstable-preview-types/-private/transforms/boolean.d.ts.map +1 -0
- package/unstable-preview-types/-private/transforms/boolean.type-test.d.ts +4 -0
- package/unstable-preview-types/-private/transforms/boolean.type-test.d.ts.map +1 -0
- package/unstable-preview-types/-private/transforms/date.d.ts +33 -0
- package/unstable-preview-types/-private/transforms/date.d.ts.map +1 -0
- package/unstable-preview-types/-private/transforms/number.d.ts +34 -0
- package/unstable-preview-types/-private/transforms/number.d.ts.map +1 -0
- package/unstable-preview-types/-private/transforms/string.d.ts +34 -0
- package/unstable-preview-types/-private/transforms/string.d.ts.map +1 -0
- package/unstable-preview-types/-private/transforms/transform.d.ts +126 -0
- package/unstable-preview-types/-private/transforms/transform.d.ts.map +1 -0
- package/unstable-preview-types/-private/utils.d.ts +6 -0
- package/unstable-preview-types/-private/utils.d.ts.map +1 -0
- package/unstable-preview-types/index.d.ts +277 -0
- package/unstable-preview-types/index.d.ts.map +1 -0
- package/unstable-preview-types/json-api.d.ts +527 -0
- package/unstable-preview-types/json-api.d.ts.map +1 -0
- package/unstable-preview-types/json.d.ts +1093 -0
- package/unstable-preview-types/json.d.ts.map +1 -0
- package/unstable-preview-types/rest.d.ts +570 -0
- package/unstable-preview-types/rest.d.ts.map +1 -0
- package/unstable-preview-types/transform.d.ts +11 -0
- package/unstable-preview-types/transform.d.ts.map +1 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
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.
|
|
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:
|
|
8
|
+
|
|
9
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
10
|
+
|
|
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.
|
package/README.md
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img
|
|
3
|
+
class="project-logo"
|
|
4
|
+
src="./ember-data-logo-dark.svg#gh-dark-mode-only"
|
|
5
|
+
alt="EmberData Serializer"
|
|
6
|
+
width="240px"
|
|
7
|
+
title="EmberData Serializer"
|
|
8
|
+
/>
|
|
9
|
+
<img
|
|
10
|
+
class="project-logo"
|
|
11
|
+
src="./ember-data-logo-light.svg#gh-light-mode-only"
|
|
12
|
+
alt="EmberData Serializer"
|
|
13
|
+
width="240px"
|
|
14
|
+
title="EmberData Serializer"
|
|
15
|
+
/>
|
|
16
|
+
</p>
|
|
17
|
+
|
|
18
|
+
<p align="center">Provides JSON, REST and JSON:API Implementations of the legacy <a href="https://api.emberjs.com/ember-data/release/classes/%3CInterface%3E%20Serializer">Serializer Interface</a></p>
|
|
19
|
+
|
|
20
|
+
> **Caution** ⚠️ **This is LEGACY documentation** for a feature that is no longer encouraged to be used.
|
|
21
|
+
> If starting a new app or thinking of implementing a new serializer, consider writing a [Handler](https://api.emberjs.com/ember-data/release/classes/%3CInterface%3E%20Handler)
|
|
22
|
+
> instead to be used with the [RequestManager](https://github.com/emberjs/data/tree/main/packages/request#readme)
|
|
23
|
+
|
|
24
|
+
## Installation
|
|
25
|
+
|
|
26
|
+
This package is currently installed when installing `ember-data`.
|
|
27
|
+
|
|
28
|
+
If installing `@ember-data/` packages individually install using your javascript package manager of choice. For instance with [pnpm](https://pnpm.io/)
|
|
29
|
+
|
|
30
|
+
```no-highlight
|
|
31
|
+
pnpm add @ember-data-mirror/serializer
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
**Tagged Releases**
|
|
35
|
+
|
|
36
|
+
- 
|
|
37
|
+
- 
|
|
38
|
+
- 
|
|
39
|
+
- 
|
|
40
|
+
- 
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
## 🚀 Setup
|
|
44
|
+
|
|
45
|
+
If using `ember-data` no additional setup is necesssary.
|
|
46
|
+
|
|
47
|
+
> **Note**
|
|
48
|
+
> When using [ember-data](https://github.com/emberjs/data/blob/main/packages/-ember-data) the below
|
|
49
|
+
> configuration is handled for you automatically.
|
|
50
|
+
|
|
51
|
+
To use legacy serializers you will need to have installed and configured the LegacyNetworkHandler from [@ember-data-mirror/legacy-compat](https://github.com/emberjs/data/blob/main/packages/-ember-data)
|
|
52
|
+
|
|
53
|
+
```no-highlight
|
|
54
|
+
pnpm add @ember-data-mirror/legacy-compat
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
import Store, { CacheHandler } from '@ember-data-mirror/store';
|
|
59
|
+
import RequestManager from '@ember-data-mirror/request';
|
|
60
|
+
import { LegacyNetworkHandler } from '@ember-data-mirror/legacy-compat';
|
|
61
|
+
|
|
62
|
+
export default class extends Store {
|
|
63
|
+
requestManager = new RequestManager();
|
|
64
|
+
|
|
65
|
+
constructor(args) {
|
|
66
|
+
super(args);
|
|
67
|
+
this.requestManager.use([LegacyNetworkHandler]);
|
|
68
|
+
this.requestManager.useCache(CacheHandler);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
## Usage
|
|
75
|
+
|
|
76
|
+
To use as either a per-type or application serializer, export one of the
|
|
77
|
+
implementations within the `serializers/` directory of your app as appropriate.
|
|
78
|
+
|
|
79
|
+
For instance, to configure an application serializer to use `JSON:API`
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
*app/serializers/application.ts*
|
|
83
|
+
```ts
|
|
84
|
+
export { default } from '@ember-data-mirror/serializer/json-api';
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
By default serializers are resolved by looking for a serializer with the same name in the `serializers/` folder as the `type` given to `store.serializerFor(<type>)`, falling back to looking for a serializer named `application`.
|
|
88
|
+
|
|
89
|
+
**Overriding Resolution**
|
|
90
|
+
|
|
91
|
+
If you would like to avoid using resolver semantics and your application has only one or a few serializers, you may ovveride the `serializerFor` hook on the store.
|
|
92
|
+
|
|
93
|
+
```ts
|
|
94
|
+
import Store from '@ember-data-mirror/store';
|
|
95
|
+
import Serializer from '@ember-data-mirror/serializer/json-api';
|
|
96
|
+
|
|
97
|
+
class extends Store {
|
|
98
|
+
#serializer = new Serializer();
|
|
99
|
+
|
|
100
|
+
serializerFor() {
|
|
101
|
+
return this.#serializer;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
For the full list of APIs available read the code documentation for [@ember-data-mirror/serializer](https://api.emberjs.com/ember-data/release/modules/@ember-data%2Fserializer). You may also be interested in learning more about *Ember***Data**'s [Serializer Interface](https://api.emberjs.com/ember-data/release/classes/%3CInterface%3E%20Serializer).
|
package/addon-main.cjs
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
const path = require('path');
|
|
2
|
+
const fs = require('fs');
|
|
3
|
+
|
|
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.',
|
|
11
|
+
|
|
12
|
+
availableOptions: [{ name: 'base-class', type: String }],
|
|
13
|
+
|
|
14
|
+
root: __dirname,
|
|
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
|
+
|
|
25
|
+
locals(options) {
|
|
26
|
+
return extendFromApplicationEntity('serializer', 'JSONAPISerializer', options);
|
|
27
|
+
},
|
|
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
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
const path = require('path');
|
|
2
|
+
|
|
3
|
+
const testInfo = require('ember-cli-test-info');
|
|
4
|
+
const { dasherize } = require('ember-cli-string-utils');
|
|
5
|
+
|
|
6
|
+
module.exports = {
|
|
7
|
+
description: 'Generates an EmberData Serializer unit test',
|
|
8
|
+
supportsAddon() { return false; },
|
|
9
|
+
|
|
10
|
+
root: __dirname,
|
|
11
|
+
|
|
12
|
+
fileMapTokens() {
|
|
13
|
+
return {
|
|
14
|
+
__root__() {
|
|
15
|
+
return 'tests';
|
|
16
|
+
},
|
|
17
|
+
__path__() {
|
|
18
|
+
return path.join('unit', 'serializers');
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
},
|
|
22
|
+
|
|
23
|
+
locals(options) {
|
|
24
|
+
const modulePrefix = dasherize(options.project.config().modulePrefix);
|
|
25
|
+
return {
|
|
26
|
+
friendlyTestDescription: testInfo.description(options.entity.name, 'Unit', 'Serializer'),
|
|
27
|
+
modulePrefix,
|
|
28
|
+
};
|
|
29
|
+
},
|
|
30
|
+
|
|
31
|
+
filesPath() {
|
|
32
|
+
return path.join(__dirname, 'qunit-files')
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { setupTest } from '<%= modulePrefix %>/tests/helpers';
|
|
2
|
+
import { module, test } from 'qunit';
|
|
3
|
+
|
|
4
|
+
module('<%= friendlyTestDescription %>', function (hooks) {
|
|
5
|
+
setupTest(hooks);
|
|
6
|
+
|
|
7
|
+
// Replace this with your real tests.
|
|
8
|
+
test('it exists', function (assert) {
|
|
9
|
+
const store = this.owner.lookup('service:store');
|
|
10
|
+
const serializer = store.serializerFor('<%= dasherizedModuleName %>');
|
|
11
|
+
|
|
12
|
+
assert.ok(serializer, 'serializer exists');
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
test('it serializes records', function (assert) {
|
|
16
|
+
const store = this.owner.lookup('service:store');
|
|
17
|
+
const record = store.createRecord('<%= dasherizedModuleName %>', {});
|
|
18
|
+
|
|
19
|
+
const serializedRecord = record.serialize();
|
|
20
|
+
|
|
21
|
+
assert.ok(serializedRecord, 'it serializes records');
|
|
22
|
+
});
|
|
23
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
const path = require('path');
|
|
2
|
+
|
|
3
|
+
const { has } = require('@ember/edition-utils');
|
|
4
|
+
|
|
5
|
+
module.exports = {
|
|
6
|
+
description: 'Generates an ember-data Transform.',
|
|
7
|
+
root: __dirname,
|
|
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
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
const path = require('path');
|
|
2
|
+
|
|
3
|
+
const testInfo = require('ember-cli-test-info');
|
|
4
|
+
const { dasherize } = require('ember-cli-string-utils');
|
|
5
|
+
|
|
6
|
+
module.exports = {
|
|
7
|
+
description: 'Generates an EmberData Transform unit test',
|
|
8
|
+
supportsAddon() { return false; },
|
|
9
|
+
|
|
10
|
+
root: __dirname,
|
|
11
|
+
|
|
12
|
+
fileMapTokens() {
|
|
13
|
+
return {
|
|
14
|
+
__root__() {
|
|
15
|
+
return 'tests';
|
|
16
|
+
},
|
|
17
|
+
__path__() {
|
|
18
|
+
return path.join('unit', 'transforms');
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
},
|
|
22
|
+
|
|
23
|
+
locals(options) {
|
|
24
|
+
const modulePrefix = dasherize(options.project.config().modulePrefix);
|
|
25
|
+
return {
|
|
26
|
+
friendlyTestDescription: testInfo.description(options.entity.name, 'Unit', 'Transform'),
|
|
27
|
+
modulePrefix,
|
|
28
|
+
};
|
|
29
|
+
},
|
|
30
|
+
|
|
31
|
+
filesPath() {
|
|
32
|
+
return path.join(__dirname, 'qunit-files')
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { setupTest } from '<%= modulePrefix %>/tests/helpers';
|
|
2
|
+
import { module, test } from 'qunit';
|
|
3
|
+
|
|
4
|
+
module('<%= friendlyTestDescription %>', function (hooks) {
|
|
5
|
+
setupTest(hooks);
|
|
6
|
+
|
|
7
|
+
// Replace this with your real tests.
|
|
8
|
+
test('it exists', function (assert) {
|
|
9
|
+
const transform = this.owner.lookup('transform:<%= dasherizedModuleName %>');
|
|
10
|
+
assert.ok(transform, 'transform exists');
|
|
11
|
+
});
|
|
12
|
+
});
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
import EmberObject from '@ember/object';
|
|
2
|
+
import { inject } 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-mirror/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-mirror/serializer
|
|
160
|
+
@main @ember-data-mirror/serializer
|
|
161
|
+
*/
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
> ⚠️ CAUTION you likely want the docs for [<Interface> Serializer](/ember-data/release/classes/%3CInterface%3E%20Serializer)
|
|
165
|
+
> as extending this abstract class is unnecessary.
|
|
166
|
+
|
|
167
|
+
`Serializer` is an abstract base class that you may override in your
|
|
168
|
+
application to customize it for your backend. The minimum set of methods
|
|
169
|
+
that you should implement is:
|
|
170
|
+
|
|
171
|
+
* `normalizeResponse()`
|
|
172
|
+
* `serialize()`
|
|
173
|
+
|
|
174
|
+
And you can optionally override the following methods:
|
|
175
|
+
|
|
176
|
+
* `normalize()`
|
|
177
|
+
|
|
178
|
+
For an example implementation, see
|
|
179
|
+
[JSONSerializer](JSONSerializer), the included JSON serializer.
|
|
180
|
+
|
|
181
|
+
@class Serializer
|
|
182
|
+
@public
|
|
183
|
+
@extends Ember.EmberObject
|
|
184
|
+
*/
|
|
185
|
+
|
|
186
|
+
class Serializer extends EmberObject {
|
|
187
|
+
static {
|
|
188
|
+
decorateFieldV2(this.prototype, "store", [inject]);
|
|
189
|
+
}
|
|
190
|
+
#store = (initializeDeferredDecorator(this, "store"), void 0);
|
|
191
|
+
/**
|
|
192
|
+
The `store` property is the application's `store` that contains
|
|
193
|
+
all records. It can be used to look up serializers for other model
|
|
194
|
+
types that may be nested inside the payload response.
|
|
195
|
+
Example:
|
|
196
|
+
```js
|
|
197
|
+
Serializer.extend({
|
|
198
|
+
extractRelationship(relationshipModelName, relationshipHash) {
|
|
199
|
+
let modelClass = this.store.modelFor(relationshipModelName);
|
|
200
|
+
let relationshipSerializer = this.store.serializerFor(relationshipModelName);
|
|
201
|
+
return relationshipSerializer.normalize(modelClass, relationshipHash);
|
|
202
|
+
}
|
|
203
|
+
});
|
|
204
|
+
```
|
|
205
|
+
@property store
|
|
206
|
+
@type {Store}
|
|
207
|
+
@public
|
|
208
|
+
*/
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
The `normalizeResponse` method is used to normalize a payload from the
|
|
212
|
+
server to a JSON-API Document.
|
|
213
|
+
http://jsonapi.org/format/#document-structure
|
|
214
|
+
Example:
|
|
215
|
+
```js
|
|
216
|
+
Serializer.extend({
|
|
217
|
+
normalizeResponse(store, primaryModelClass, payload, id, requestType) {
|
|
218
|
+
if (requestType === 'findRecord') {
|
|
219
|
+
return this.normalize(primaryModelClass, payload);
|
|
220
|
+
} else {
|
|
221
|
+
return payload.reduce(function(documentHash, item) {
|
|
222
|
+
let { data, included } = this.normalize(primaryModelClass, item);
|
|
223
|
+
documentHash.included.push(...included);
|
|
224
|
+
documentHash.data.push(data);
|
|
225
|
+
return documentHash;
|
|
226
|
+
}, { data: [], included: [] })
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
});
|
|
230
|
+
```
|
|
231
|
+
@since 1.13.0
|
|
232
|
+
@method normalizeResponse
|
|
233
|
+
@public
|
|
234
|
+
@param {Store} store
|
|
235
|
+
@param {Model} primaryModelClass
|
|
236
|
+
@param {Object} payload
|
|
237
|
+
@param {String|Number} id
|
|
238
|
+
@param {String} requestType
|
|
239
|
+
@return {Object} JSON-API Document
|
|
240
|
+
*/
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
The `serialize` method is used when a record is saved in order to convert
|
|
244
|
+
the record into the form that your external data source expects.
|
|
245
|
+
`serialize` takes an optional `options` hash with a single option:
|
|
246
|
+
- `includeId`: If this is `true`, `serialize` should include the ID
|
|
247
|
+
in the serialized object it builds.
|
|
248
|
+
Example:
|
|
249
|
+
```js
|
|
250
|
+
Serializer.extend({
|
|
251
|
+
serialize(snapshot, options) {
|
|
252
|
+
let json = {
|
|
253
|
+
id: snapshot.id
|
|
254
|
+
};
|
|
255
|
+
snapshot.eachAttribute((key, attribute) => {
|
|
256
|
+
json[key] = snapshot.attr(key);
|
|
257
|
+
});
|
|
258
|
+
snapshot.eachRelationship((key, relationship) => {
|
|
259
|
+
if (relationship.kind === 'belongsTo') {
|
|
260
|
+
json[key] = snapshot.belongsTo(key, { id: true });
|
|
261
|
+
} else if (relationship.kind === 'hasMany') {
|
|
262
|
+
json[key] = snapshot.hasMany(key, { ids: true });
|
|
263
|
+
}
|
|
264
|
+
});
|
|
265
|
+
return json;
|
|
266
|
+
},
|
|
267
|
+
});
|
|
268
|
+
```
|
|
269
|
+
@method serialize
|
|
270
|
+
@public
|
|
271
|
+
@param {Snapshot} snapshot
|
|
272
|
+
@param {Object} [options]
|
|
273
|
+
@return {Object}
|
|
274
|
+
*/
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
The `normalize` method is used to convert a payload received from your
|
|
278
|
+
external data source into the normalized form `store.push()` expects. You
|
|
279
|
+
should override this method, munge the hash and return the normalized
|
|
280
|
+
payload.
|
|
281
|
+
Example:
|
|
282
|
+
```js
|
|
283
|
+
Serializer.extend({
|
|
284
|
+
normalize(modelClass, resourceHash) {
|
|
285
|
+
let data = {
|
|
286
|
+
id: resourceHash.id,
|
|
287
|
+
type: modelClass.modelName,
|
|
288
|
+
attributes: resourceHash
|
|
289
|
+
};
|
|
290
|
+
return { data: data };
|
|
291
|
+
}
|
|
292
|
+
})
|
|
293
|
+
```
|
|
294
|
+
@method normalize
|
|
295
|
+
@public
|
|
296
|
+
@param {Model} typeClass
|
|
297
|
+
@param {Object} hash
|
|
298
|
+
@return {Object}
|
|
299
|
+
*/
|
|
300
|
+
normalize(_typeClass, hash) {
|
|
301
|
+
return hash;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
export { Serializer as default };
|