@ember-data/model 4.5.0-beta.0 → 4.7.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.
@@ -0,0 +1,92 @@
1
+ import { assert } from '@ember/debug';
2
+ import { DEBUG } from '@glimmer/env';
3
+
4
+ import type Store from '@ember-data/store';
5
+ import type ShimModelClass from '@ember-data/store/-private/model/shim-model-class';
6
+ import type { JsonApiDocument } from '@ember-data/types/q/ember-data-json-api';
7
+ import type { StableExistingRecordIdentifier, StableRecordIdentifier } from '@ember-data/types/q/identifier';
8
+ import type { AdapterPayload } from '@ember-data/types/q/minimum-adapter-interface';
9
+ import type { MinimumSerializerInterface, RequestType } from '@ember-data/types/q/minimum-serializer-interface';
10
+
11
+ export function iterateData<T>(data: T[] | T, fn: (o: T, index?: number) => T) {
12
+ if (Array.isArray(data)) {
13
+ return data.map(fn);
14
+ } else {
15
+ return fn(data);
16
+ }
17
+ }
18
+
19
+ export function assertIdentifierHasId(
20
+ identifier: StableRecordIdentifier
21
+ ): asserts identifier is StableExistingRecordIdentifier {
22
+ assert(`Attempted to schedule a fetch for a record without an id.`, identifier.id !== null);
23
+ }
24
+
25
+ export function normalizeResponseHelper(
26
+ serializer: MinimumSerializerInterface | null,
27
+ store: Store,
28
+ modelClass: ShimModelClass,
29
+ payload: AdapterPayload,
30
+ id: string | null,
31
+ requestType: RequestType
32
+ ): JsonApiDocument {
33
+ let normalizedResponse = serializer
34
+ ? serializer.normalizeResponse(store, modelClass, payload, id, requestType)
35
+ : payload;
36
+
37
+ validateDocumentStructure(normalizedResponse);
38
+
39
+ return normalizedResponse;
40
+ }
41
+
42
+ export function validateDocumentStructure(doc?: AdapterPayload | JsonApiDocument): asserts doc is JsonApiDocument {
43
+ if (DEBUG) {
44
+ let errors: string[] = [];
45
+ if (!doc || typeof doc !== 'object') {
46
+ errors.push('Top level of a JSON API document must be an object');
47
+ } else {
48
+ if (!('data' in doc) && !('errors' in doc) && !('meta' in doc)) {
49
+ errors.push('One or more of the following keys must be present: "data", "errors", "meta".');
50
+ } else {
51
+ if ('data' in doc && 'errors' in doc) {
52
+ errors.push('Top level keys "errors" and "data" cannot both be present in a JSON API document');
53
+ }
54
+ }
55
+ if ('data' in doc) {
56
+ if (!(doc.data === null || Array.isArray(doc.data) || typeof doc.data === 'object')) {
57
+ errors.push('data must be null, an object, or an array');
58
+ }
59
+ }
60
+ if ('meta' in doc) {
61
+ if (typeof doc.meta !== 'object') {
62
+ errors.push('meta must be an object');
63
+ }
64
+ }
65
+ if ('errors' in doc) {
66
+ if (!Array.isArray(doc.errors)) {
67
+ errors.push('errors must be an array');
68
+ }
69
+ }
70
+ if ('links' in doc) {
71
+ if (typeof doc.links !== 'object') {
72
+ errors.push('links must be an object');
73
+ }
74
+ }
75
+ if ('jsonapi' in doc) {
76
+ if (typeof doc.jsonapi !== 'object') {
77
+ errors.push('jsonapi must be an object');
78
+ }
79
+ }
80
+ if ('included' in doc) {
81
+ if (typeof doc.included !== 'object') {
82
+ errors.push('included must be an array');
83
+ }
84
+ }
85
+ }
86
+
87
+ assert(
88
+ `Response must be normalized to a valid JSON API document:\n\t* ${errors.join('\n\t* ')}`,
89
+ errors.length === 0
90
+ );
91
+ }
92
+ }