UNPKG

86.4 kBTypeScriptView Raw
1// Type definitions for ember-data 4.0
2// Project: https://github.com/emberjs/data
3// Definitions by: Derek Wickern <https://github.com/dwickern>
4// Mike North <https://github.com/mike-north>
5// Chris Krycho <https://github.com/chriskrycho>
6// James C. Davis <https://github.com/jamescdavis>
7// Dan Freeman <https://github.com/dfreeman>
8// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
9// Minimum TypeScript Version: 4.4
10
11import Ember from 'ember';
12import Evented from '@ember/object/evented';
13import ObjectProxy from '@ember/object/proxy';
14import PromiseProxyMixin from '@ember/object/promise-proxy-mixin';
15import RSVP from 'rsvp';
16import TransformRegistry from 'ember-data/types/registries/transform';
17import ModelRegistry from 'ember-data/types/registries/model';
18import SerializerRegistry from 'ember-data/types/registries/serializer';
19import AdapterRegistry from 'ember-data/types/registries/adapter';
20
21/**
22 The keys from the actual Model class, removing all the keys which come from
23 the base class.
24 */
25type ModelKeys<Model extends DS.Model> = Exclude<keyof Model, keyof DS.Model>;
26
27type AttributesFor<Model extends DS.Model> = ModelKeys<Model>; // TODO: filter to attr properties only (TS 2.8)
28type RelationshipsFor<Model extends DS.Model> = ModelKeys<Model>; // TODO: filter to hasMany/belongsTo properties only (TS 2.8)
29
30export interface ChangedAttributes {
31 [key: string]: [any, any] | undefined;
32}
33interface AttributeMeta<Model extends DS.Model> {
34 type: keyof TransformRegistry;
35 options: object;
36 name: AttributesFor<Model>;
37 parentType: Model;
38 isAttribute: true;
39}
40
41interface RelationshipMetaOptions {
42 async?: boolean | undefined;
43 inverse?: string | undefined;
44 polymorphic?: boolean | undefined;
45 [k: string]: any;
46}
47interface RelationshipMeta<Model extends DS.Model> {
48 key: RelationshipsFor<Model>;
49 kind: 'belongsTo' | 'hasMany';
50 type: keyof ModelRegistry;
51 options: RelationshipMetaOptions;
52 name: string;
53 parentType: Model;
54 isRelationship: true;
55}
56
57export namespace DS {
58 /**
59 * Convert an hash of errors into an array with errors in JSON-API format.
60 */
61 function errorsHashToArray(errors: {}): any[];
62 /**
63 * Convert an array of errors in JSON-API format into an object.
64 */
65 function errorsArrayToHash(errors: any[]): {};
66
67 interface RelationshipOptions<M extends Model> {
68 async?: boolean | undefined;
69 inverse?: RelationshipsFor<M> | null | undefined;
70 polymorphic?: boolean | undefined;
71 }
72
73 interface Sync {
74 async: false;
75 }
76 interface Async {
77 async?: true | undefined;
78 }
79
80 type AsyncBelongsTo<T extends Model | null> = PromiseObject<T>;
81 /**
82 * `DS.belongsTo` is used to define One-To-One and One-To-Many
83 * relationships on a [DS.Model](/api/data/classes/DS.Model.html).
84 */
85 function belongsTo<K extends keyof ModelRegistry>(
86 modelName: K,
87 options: RelationshipOptions<ModelRegistry[K]> & Sync
88 ): Ember.ComputedProperty<
89 ModelRegistry[K] | null,
90 ModelRegistry[K] | PromiseObject<ModelRegistry[K] | null> | null
91 >;
92 function belongsTo<K extends keyof ModelRegistry>(
93 modelName: K,
94 options?: RelationshipOptions<ModelRegistry[K]> & Async
95 ): Ember.ComputedProperty<
96 AsyncBelongsTo<ModelRegistry[K] | null>,
97 ModelRegistry[K] | PromiseObject<ModelRegistry[K] | null> | null
98 >;
99 type AsyncHasMany<T extends Model> = PromiseManyArray<T>;
100 type SyncHasMany<T extends Model> = ManyArray<T>;
101 /**
102 * `DS.hasMany` is used to define One-To-Many and Many-To-Many
103 * relationships on a [DS.Model](/api/data/classes/DS.Model.html).
104 */
105 function hasMany<K extends keyof ModelRegistry>(
106 type: K,
107 options: RelationshipOptions<ModelRegistry[K]> & Sync
108 ): Ember.ComputedProperty<SyncHasMany<ModelRegistry[K]>>;
109 function hasMany<K extends keyof ModelRegistry>(
110 type: K,
111 options?: RelationshipOptions<ModelRegistry[K]> & Async
112 ): Ember.ComputedProperty<
113 AsyncHasMany<ModelRegistry[K]>,
114 Ember.Array<ModelRegistry[K]>
115 >;
116 /**
117 * This method normalizes a modelName into the format Ember Data uses
118 * internally.
119 */
120 function normalizeModelName<K extends keyof ModelRegistry>(
121 modelName: K
122 ): string;
123 const VERSION: string;
124
125 interface AttrOptions<T> {
126 defaultValue?: T extends Exclude<object, null> ? (() => T) : T | (() => T) | undefined;
127 allowNull?: boolean | undefined; // TODO: restrict to boolean transform (TS 2.8)
128 }
129
130 // The TransformRegistry should really only contain transforms, but historically people have just put the return type directly in.
131 type TransformType<K extends keyof TransformRegistry> = TransformRegistry[K] extends Transform ? ReturnType<TransformRegistry[K]['deserialize']> : TransformRegistry[K];
132
133 /**
134 * `DS.attr` defines an attribute on a [DS.Model](/api/data/classes/DS.Model.html).
135 * By default, attributes are passed through as-is, however you can specify an
136 * optional type to have the value automatically transformed.
137 * Ember Data ships with four basic transform types: `string`, `number`,
138 * `boolean` and `date`. You can define your own transforms by subclassing
139 * [DS.Transform](/api/data/classes/DS.Transform.html).
140 */
141 function attr<K extends keyof TransformRegistry>(
142 type: K,
143 options?: AttrOptions<TransformType<K>>
144 ): Ember.ComputedProperty<TransformType<K>>;
145 function attr(options?: AttrOptions<any>): Ember.ComputedProperty<any>;
146 function attr(target: any, propertyKey: string): void;
147
148 /**
149 * WARNING: This interface is likely to change in order to accomodate https://github.com/emberjs/rfcs/pull/4
150 * ## Using BuildURLMixin
151 * To use url building, include the mixin when extending an adapter, and call `buildURL` where needed.
152 * The default behaviour is designed for RESTAdapter.
153 * ### Example
154 * ```javascript
155 * export default DS.Adapter.extend(BuildURLMixin, {
156 * findRecord: function(store, type, id, snapshot) {
157 * var url = this.buildURL(type.modelName, id, snapshot, 'findRecord');
158 * return this.ajax(url, 'GET');
159 * }
160 * });
161 * ```
162 * ### Attributes
163 * The `host` and `namespace` attributes will be used if defined, and are optional.
164 */
165 class BuildURLMixin {
166 /**
167 * Builds a URL for a given type and optional ID.
168 */
169 buildURL<K extends keyof ModelRegistry>(
170 modelName?: K,
171 id?: string | any[] | {} | null,
172 snapshot?: Snapshot<K> | any[] | null,
173 requestType?: string,
174 query?: {}
175 ): string;
176 /**
177 * Used by `findAll` and `findRecord` to build the query's `data` hash supplied to the ajax method.
178 */
179 buildQuery<K extends keyof ModelRegistry>(
180 snapshot: Snapshot<K>
181 ): Record<string, unknown>;
182 /**
183 * Builds a URL for a `store.findRecord(type, id)` call.
184 */
185 urlForFindRecord<K extends keyof ModelRegistry>(
186 id: string,
187 modelName: K,
188 snapshot: Snapshot<K>
189 ): string;
190 /**
191 * Builds a URL for a `store.findAll(type)` call.
192 */
193 urlForFindAll<K extends keyof ModelRegistry>(
194 modelName: K,
195 snapshot: SnapshotRecordArray<K>
196 ): string;
197 /**
198 * Builds a URL for a `store.query(type, query)` call.
199 */
200 urlForQuery<K extends keyof ModelRegistry>(
201 query: {},
202 modelName: K
203 ): string;
204 /**
205 * Builds a URL for a `store.queryRecord(type, query)` call.
206 */
207 urlForQueryRecord<K extends keyof ModelRegistry>(
208 query: {},
209 modelName: K
210 ): string;
211 /**
212 * Builds a URL for coalesceing multiple `store.findRecord(type, id)`
213 * records into 1 request when the adapter's `coalesceFindRequests`
214 * property is true.
215 */
216 urlForFindMany<K extends keyof ModelRegistry>(
217 ids: any[],
218 modelName: K,
219 snapshots: any[]
220 ): string;
221 /**
222 * Builds a URL for fetching a async hasMany relationship when a url
223 * is not provided by the server.
224 */
225 urlForFindHasMany<K extends keyof ModelRegistry>(
226 id: string,
227 modelName: K,
228 snapshot: Snapshot<K>
229 ): string;
230 /**
231 * Builds a URL for fetching a async belongsTo relationship when a url
232 * is not provided by the server.
233 */
234 urlForFindBelongsTo<K extends keyof ModelRegistry>(
235 id: string,
236 modelName: K,
237 snapshot: Snapshot<K>
238 ): string;
239 /**
240 * Builds a URL for a `record.save()` call when the record was created
241 * locally using `store.createRecord()`.
242 */
243 urlForCreateRecord<K extends keyof ModelRegistry>(
244 modelName: K,
245 snapshot: Snapshot<K>
246 ): string;
247 /**
248 * Builds a URL for a `record.save()` call when the record has been update locally.
249 */
250 urlForUpdateRecord<K extends keyof ModelRegistry>(
251 id: string,
252 modelName: K,
253 snapshot: Snapshot<K>
254 ): string;
255 /**
256 * Builds a URL for a `record.save()` call when the record has been deleted locally.
257 */
258 urlForDeleteRecord<K extends keyof ModelRegistry>(
259 id: string,
260 modelName: K,
261 snapshot: Snapshot<K>
262 ): string;
263 /**
264 * Determines the pathname for a given type.
265 */
266 pathForType<K extends keyof ModelRegistry>(modelName: K): string;
267 }
268 /**
269 * A `DS.AdapterError` is used by an adapter to signal that an error occurred
270 * during a request to an external API. It indicates a generic error, and
271 * subclasses are used to indicate specific error states. The following
272 * subclasses are provided:
273 */
274 class AdapterError extends Ember.Object {}
275 /**
276 * A `DS.InvalidError` is used by an adapter to signal the external API
277 * was unable to process a request because the content was not
278 * semantically correct or meaningful per the API. Usually this means a
279 * record failed some form of server side validation. When a promise
280 * from an adapter is rejected with a `DS.InvalidError` the record will
281 * transition to the `invalid` state and the errors will be set to the
282 * `errors` property on the record.
283 */
284 class InvalidError extends AdapterError {
285 constructor(errors: any[]);
286 }
287 /**
288 * A `DS.TimeoutError` is used by an adapter to signal that a request
289 * to the external API has timed out. I.e. no response was received from
290 * the external API within an allowed time period.
291 */
292 class TimeoutError extends AdapterError {}
293 /**
294 * A `DS.AbortError` is used by an adapter to signal that a request to
295 * the external API was aborted. For example, this can occur if the user
296 * navigates away from the current page after a request to the external API
297 * has been initiated but before a response has been received.
298 */
299 class AbortError extends AdapterError {}
300 /**
301 * A `DS.UnauthorizedError` equates to a HTTP `401 Unauthorized` response
302 * status. It is used by an adapter to signal that a request to the external
303 * API was rejected because authorization is required and has failed or has not
304 * yet been provided.
305 */
306 class UnauthorizedError extends AdapterError {}
307 /**
308 * A `DS.ForbiddenError` equates to a HTTP `403 Forbidden` response status.
309 * It is used by an adapter to signal that a request to the external API was
310 * valid but the server is refusing to respond to it. If authorization was
311 * provided and is valid, then the authenticated user does not have the
312 * necessary permissions for the request.
313 */
314 class ForbiddenError extends AdapterError {}
315 /**
316 * A `DS.NotFoundError` equates to a HTTP `404 Not Found` response status.
317 * It is used by an adapter to signal that a request to the external API
318 * was rejected because the resource could not be found on the API.
319 */
320 class NotFoundError extends AdapterError {}
321 /**
322 * A `DS.ConflictError` equates to a HTTP `409 Conflict` response status.
323 * It is used by an adapter to indicate that the request could not be processed
324 * because of a conflict in the request. An example scenario would be when
325 * creating a record with a client generated id but that id is already known
326 * to the external API.
327 */
328 class ConflictError extends AdapterError {}
329 /**
330 * A `DS.ServerError` equates to a HTTP `500 Internal Server Error` response
331 * status. It is used by the adapter to indicate that a request has failed
332 * because of an error in the external API.
333 */
334 class ServerError extends AdapterError {}
335 /**
336 * Holds validation errors for a given record, organized by attribute names.
337 */
338 interface Errors extends Ember.Enumerable<any>, Evented {}
339 class Errors extends Ember.ArrayProxy<any> {
340 /**
341 * DEPRECATED:
342 * Register with target handler
343 */
344 registerHandlers(
345 target: {},
346 becameInvalid: Function,
347 becameValid: Function
348 ): any;
349 /**
350 * Returns errors for a given attribute
351 */
352 errorsFor(attribute: string): any[];
353 /**
354 * An array containing all of the error messages for this
355 * record. This is useful for displaying all errors to the user.
356 */
357 messages: Ember.ComputedProperty<any[]>;
358 /**
359 * Total number of errors.
360 */
361 length: Ember.ComputedProperty<number>;
362 isEmpty: Ember.ComputedProperty<boolean>;
363 /**
364 * DEPRECATED:
365 * Adds error messages to a given attribute and sends
366 * `becameInvalid` event to the record.
367 */
368 add(attribute: string, messages: any[] | string): any;
369 /**
370 * DEPRECATED:
371 * Removes all error messages from the given attribute and sends
372 * `becameValid` event to the record if there no more errors left.
373 */
374 remove(attribute: string): any;
375 /**
376 * DEPRECATED:
377 * Removes all error messages and sends `becameValid` event
378 * to the record.
379 */
380 clear(): any;
381 /**
382 * Checks if there is error messages for the given attribute.
383 */
384 has(attribute: string): boolean;
385 }
386 /**
387 * The model class that all Ember Data records descend from.
388 * This is the public API of Ember Data models. If you are using Ember Data
389 * in your application, this is the class you should use.
390 * If you are working on Ember Data internals, you most likely want to be dealing
391 * with `InternalModel`
392 */
393 class Model extends Ember.Object {
394 /**
395 * If this property is `true` the record is in the `empty`
396 * state. Empty is the first state all records enter after they have
397 * been created. Most records created by the store will quickly
398 * transition to the `loading` state if data needs to be fetched from
399 * the server or the `created` state if the record is created on the
400 * client. A record can also enter the empty state if the adapter is
401 * unable to locate the record.
402 */
403 isEmpty: Ember.ComputedProperty<boolean>;
404 /**
405 * If this property is `true` the record is in the `loading` state. A
406 * record enters this state when the store asks the adapter for its
407 * data. It remains in this state until the adapter provides the
408 * requested data.
409 */
410 isLoading: Ember.ComputedProperty<boolean>;
411 /**
412 * If this property is `true` the record is in the `loaded` state. A
413 * record enters this state when its data is populated. Most of a
414 * record's lifecycle is spent inside substates of the `loaded`
415 * state.
416 */
417 isLoaded: Ember.ComputedProperty<boolean>;
418 /**
419 * If this property is `true` the record is in the `dirty` state. The
420 * record has local changes that have not yet been saved by the
421 * adapter. This includes records that have been created (but not yet
422 * saved) or deleted.
423 */
424 hasDirtyAttributes: Ember.ComputedProperty<boolean>;
425 /**
426 * If this property is `true` the record is in the `saving` state. A
427 * record enters the saving state when `save` is called, but the
428 * adapter has not yet acknowledged that the changes have been
429 * persisted to the backend.
430 */
431 isSaving: Ember.ComputedProperty<boolean>;
432 /**
433 * If this property is `true` the record is in the `deleted` state
434 * and has been marked for deletion. When `isDeleted` is true and
435 * `hasDirtyAttributes` is true, the record is deleted locally but the deletion
436 * was not yet persisted. When `isSaving` is true, the change is
437 * in-flight. When both `hasDirtyAttributes` and `isSaving` are false, the
438 * change has persisted.
439 */
440 isDeleted: Ember.ComputedProperty<boolean>;
441 /**
442 * If this property is `true` the record is in the `new` state. A
443 * record will be in the `new` state when it has been created on the
444 * client and the adapter has not yet report that it was successfully
445 * saved.
446 */
447 isNew: Ember.ComputedProperty<boolean>;
448 /**
449 * If this property is `true` the record is in the `valid` state.
450 */
451 isValid: Ember.ComputedProperty<boolean>;
452 /**
453 * If the record is in the dirty state this property will report what
454 * kind of change has caused it to move into the dirty
455 * state. Possible values are:
456 */
457 dirtyType: Ember.ComputedProperty<string>;
458 /**
459 * If `true` the adapter reported that it was unable to save local
460 * changes to the backend for any reason other than a server-side
461 * validation error.
462 */
463 isError: boolean;
464 /**
465 * If `true` the store is attempting to reload the record from the adapter.
466 */
467 isReloading: boolean;
468 /**
469 * All ember models have an id property. This is an identifier
470 * managed by an external source. These are always coerced to be
471 * strings before being used internally. Note when declaring the
472 * attributes for a model it is an error to declare an id
473 * attribute.
474 */
475 id: string;
476 /**
477 * A reference to DS.Store service instance.
478 */
479 store: Store;
480 /**
481 * When the record is in the `invalid` state this object will contain
482 * any errors returned by the adapter. When present the errors hash
483 * contains keys corresponding to the invalid property names
484 * and values which are arrays of Javascript objects with two keys:
485 */
486 errors: Errors;
487 /**
488 * This property holds the `DS.AdapterError` object with which
489 * last adapter operation was rejected.
490 */
491 adapterError: AdapterError;
492 /**
493 * Create a JSON representation of the record, using the serialization
494 * strategy of the store's adapter.
495 */
496 serialize(options?: { includeId?: boolean | undefined }): object;
497 /**
498 * Use [DS.JSONSerializer](DS.JSONSerializer.html) to
499 * get the JSON representation of a record.
500 */
501 toJSON(options?: { includeId?: boolean | undefined }): object;
502 /**
503 * Fired when the record is ready to be interacted with,
504 * that is either loaded from the server or created locally.
505 */
506 ready(): void;
507 /**
508 * Fired when the record is loaded from the server.
509 */
510 didLoad(): void;
511 /**
512 * Fired when the record is updated.
513 */
514 didUpdate(): void;
515 /**
516 * Fired when a new record is commited to the server.
517 */
518 didCreate(): void;
519 /**
520 * Fired when the record is deleted.
521 */
522 didDelete(): void;
523 /**
524 * Fired when the record becomes invalid.
525 */
526 becameInvalid(): void;
527 /**
528 * Fired when the record enters the error state.
529 */
530 becameError(): void;
531 /**
532 * Fired when the record is rolled back.
533 */
534 rolledBack(): void;
535 /**
536 * Marks the record as deleted but does not save it. You must call
537 * `save` afterwards if you want to persist it. You might use this
538 * method if you want to allow the user to still `rollbackAttributes()`
539 * after a delete was made.
540 */
541 deleteRecord(): void;
542 /**
543 * Same as `deleteRecord`, but saves the record immediately.
544 */
545 destroyRecord(options?: { adapterOptions?: object | undefined }): RSVP.Promise<this>;
546 /**
547 * Unloads the record from the store. This will cause the record to be destroyed and freed up for garbage collection.
548 */
549 unloadRecord(): void;
550 /**
551 * Returns an object, whose keys are changed properties, and value is
552 * an [oldProp, newProp] array.
553 */
554 changedAttributes(): ChangedAttributes;
555 /**
556 * If the model `hasDirtyAttributes` this function will discard any unsaved
557 * changes. If the model `isNew` it will be removed from the store.
558 */
559 rollbackAttributes(): void;
560 /**
561 * Save the record and persist any changes to the record to an
562 * external source via the adapter.
563 */
564 save(options?: { adapterOptions?: object | undefined }): RSVP.Promise<this>;
565 /**
566 * Reload the record from the adapter.
567 */
568 reload(options?: { adapterOptions?: object | undefined }): RSVP.Promise<this>;
569 /**
570 * Get the reference for the specified belongsTo relationship.
571 */
572 belongsTo(name: RelationshipsFor<this>): BelongsToReference;
573 /**
574 * Get the reference for the specified hasMany relationship.
575 */
576 hasMany(name: RelationshipsFor<this>): HasManyReference<any>;
577 /**
578 * Given a callback, iterates over each of the relationships in the model,
579 * invoking the callback with the name of each relationship and its relationship
580 * descriptor.
581 */
582 eachRelationship<T extends Model>(
583 this: T,
584 callback: (name: ModelKeys<T>, details: RelationshipMeta<T>) => void,
585 binding?: any
586 ): void;
587 /**
588 * Represents the model's class name as a string. This can be used to look up the model's class name through
589 * `DS.Store`'s modelFor method.
590 */
591 static modelName: keyof ModelRegistry;
592 /**
593 * For a given relationship name, returns the model type of the relationship.
594 */
595 static typeForRelationship<K extends keyof ModelRegistry>(
596 name: K,
597 store: Store
598 ): ModelRegistry[K];
599 /**
600 * Find the relationship which is the inverse of the one asked for.
601 */
602 static inverseFor<K extends keyof ModelRegistry>(
603 name: K,
604 store: Store
605 ): {};
606 /**
607 * The model's relationships as a map, keyed on the type of the
608 * relationship. The value of each entry is an array containing a descriptor
609 * for each relationship with that type, describing the name of the relationship
610 * as well as the type.
611 */
612 static relationships: Ember.ComputedProperty<Map<string, unknown>>;
613 /**
614 * A hash containing lists of the model's relationships, grouped
615 * by the relationship kind. For example, given a model with this
616 * definition:
617 */
618 static relationshipNames: Ember.ComputedProperty<{}>;
619 /**
620 * An array of types directly related to a model. Each type will be
621 * included once, regardless of the number of relationships it has with
622 * the model.
623 */
624 static relatedTypes: Ember.ComputedProperty<Ember.NativeArray<string>>;
625 /**
626 * A map whose keys are the relationships of a model and whose values are
627 * relationship descriptors.
628 */
629 static relationshipsByName: Ember.ComputedProperty<Map<string, unknown>>;
630 /**
631 * A map whose keys are the fields of the model and whose values are strings
632 * describing the kind of the field. A model's fields are the union of all of its
633 * attributes and relationships.
634 */
635 static fields: Ember.ComputedProperty<Map<string, unknown>>;
636 /**
637 * Given a callback, iterates over each of the relationships in the model,
638 * invoking the callback with the name of each relationship and its relationship
639 * descriptor.
640 */
641 static eachRelationship<M extends Model = Model>(
642 callback: (name: ModelKeys<M>, details: RelationshipMeta<M>) => void,
643 binding?: any
644 ): void;
645 /**
646 * Given a callback, iterates over each of the types related to a model,
647 * invoking the callback with the related type's class. Each type will be
648 * returned just once, regardless of how many different relationships it has
649 * with a model.
650 */
651 static eachRelatedType(callback: (name: string) => void, binding?: any): void;
652 /**
653 * A map whose keys are the attributes of the model (properties
654 * described by DS.attr) and whose values are the meta object for the
655 * property.
656 */
657 static attributes: Ember.ComputedProperty<Map<string, unknown>>;
658 /**
659 * A map whose keys are the attributes of the model (properties
660 * described by DS.attr) and whose values are type of transformation
661 * applied to each attribute. This map does not include any
662 * attributes that do not have an transformation type.
663 */
664 static transformedAttributes: Ember.ComputedProperty<Map<string, unknown>>;
665 /**
666 * Iterates through the attributes of the model, calling the passed function on each
667 * attribute.
668 */
669 static eachAttribute<Class extends typeof Model, M extends InstanceType<Class>>(
670 this: Class,
671 callback: (
672 name: ModelKeys<M>,
673 meta: AttributeMeta<M>
674 ) => void,
675 binding?: any
676 ): void;
677 /**
678 * Iterates through the transformedAttributes of the model, calling
679 * the passed function on each attribute. Note the callback will not be
680 * called for any attributes that do not have an transformation type.
681 */
682 static eachTransformedAttribute<Class extends typeof Model>(
683 this: Class,
684 callback: (
685 name: ModelKeys<InstanceType<Class>>,
686 type: keyof TransformRegistry
687 ) => void,
688 binding?: any
689 ): void;
690 }
691 /**
692 * ### State
693 */
694 class RootState {}
695 /**
696 * Represents an ordered list of records whose order and membership is
697 * determined by the adapter. For example, a query sent to the adapter
698 * may trigger a search on the server, whose results would be loaded
699 * into an instance of the `AdapterPopulatedRecordArray`.
700 */
701 class AdapterPopulatedRecordArray<T> extends RecordArray<T> {}
702 /**
703 * Represents a list of records whose membership is determined by the
704 * store. As records are created, loaded, or modified, the store
705 * evaluates them to determine if they should be part of the record
706 * array.
707 */
708 class FilteredRecordArray<T> extends RecordArray<T> {
709 /**
710 * The filterFunction is a function used to test records from the store to
711 * determine if they should be part of the record array.
712 */
713 filterFunction(record: Model): boolean;
714 }
715 /**
716 * A record array is an array that contains records of a certain modelName. The record
717 * array materializes records as needed when they are retrieved for the first
718 * time. You should not create record arrays yourself. Instead, an instance of
719 * `DS.RecordArray` or its subclasses will be returned by your application's store
720 * in response to queries.
721 */
722 interface RecordArray<T> extends Ember.ArrayProxy<T>, Evented {}
723 class RecordArray<T> {
724 /**
725 * The flag to signal a `RecordArray` is finished loading data.
726 */
727 isLoaded: boolean;
728 /**
729 * The flag to signal a `RecordArray` is currently loading data.
730 */
731 isUpdating: boolean;
732 /**
733 * The modelClass represented by this record array.
734 */
735 type: Ember.ComputedProperty<Model>;
736 /**
737 * Used to get the latest version of all of the records in this array
738 * from the adapter.
739 */
740 update(): PromiseArray<T>;
741 /**
742 * Saves all of the records in the `RecordArray`.
743 */
744 save(): PromiseArray<T>;
745 }
746 /**
747 * A BelongsToReference is a low level API that allows users and
748 * addon author to perform meta-operations on a belongs-to
749 * relationship.
750 */
751 class BelongsToReference {
752 /**
753 * This returns a string that represents how the reference will be
754 * looked up when it is loaded. If the relationship has a link it will
755 * use the "link" otherwise it defaults to "id".
756 */
757 remoteType(): string;
758 /**
759 * The `id` of the record that this reference refers to. Together, the
760 * `type()` and `id()` methods form a composite key for the identity
761 * map. This can be used to access the id of an async relationship
762 * without triggering a fetch that would normally happen if you
763 * attempted to use `record.get('relationship.id')`.
764 */
765 id(): string;
766 /**
767 * The link Ember Data will use to fetch or reload this belongs-to
768 * relationship.
769 */
770 link(): string;
771 /**
772 * The meta data for the belongs-to relationship.
773 */
774 meta(): {};
775 /**
776 * `push` can be used to update the data in the relationship and Ember
777 * Data will treat the new data as the conanical value of this
778 * relationship on the backend.
779 */
780 push(objectOrPromise: {} | RSVP.Promise<any>): RSVP.Promise<any>;
781 /**
782 * `value()` synchronously returns the current value of the belongs-to
783 * relationship. Unlike `record.get('relationshipName')`, calling
784 * `value()` on a reference does not trigger a fetch if the async
785 * relationship is not yet loaded. If the relationship is not loaded
786 * it will always return `null`.
787 */
788 value(): Model | null;
789 /**
790 * Loads a record in a belongs to relationship if it is not already
791 * loaded. If the relationship is already loaded this method does not
792 * trigger a new load.
793 */
794 load(): RSVP.Promise<any>;
795 /**
796 * Triggers a reload of the value in this relationship. If the
797 * remoteType is `"link"` Ember Data will use the relationship link to
798 * reload the relationship. Otherwise it will reload the record by its
799 * id.
800 */
801 reload(): RSVP.Promise<any>;
802 }
803 /**
804 * A HasManyReference is a low level API that allows users and addon
805 * author to perform meta-operations on a has-many relationship.
806 */
807 class HasManyReference<T> {
808 /**
809 * This returns a string that represents how the reference will be
810 * looked up when it is loaded. If the relationship has a link it will
811 * use the "link" otherwise it defaults to "id".
812 */
813 remoteType(): string;
814 /**
815 * The link Ember Data will use to fetch or reload this has-many
816 * relationship.
817 */
818 link(): string;
819 /**
820 * `ids()` returns an array of the record ids in this relationship.
821 */
822 ids(): string[];
823 /**
824 * The meta data for the has-many relationship.
825 */
826 meta(): {};
827 /**
828 * `push` can be used to update the data in the relationship and Ember
829 * Data will treat the new data as the canonical value of this
830 * relationship on the backend.
831 */
832 push(objectOrPromise: T[] | RSVP.Promise<T[]>): ManyArray<T>;
833 /**
834 * `value()` synchronously returns the current value of the has-many
835 * relationship. Unlike `record.get('relationshipName')`, calling
836 * `value()` on a reference does not trigger a fetch if the async
837 * relationship is not yet loaded. If the relationship is not loaded
838 * it will always return `null`.
839 */
840 value(): ManyArray<T> | null;
841 /**
842 * Loads the relationship if it is not already loaded. If the
843 * relationship is already loaded this method does not trigger a new
844 * load.
845 */
846 load(): RSVP.Promise<any>;
847 /**
848 * Reloads this has-many relationship.
849 */
850 reload(): RSVP.Promise<any>;
851 }
852 /**
853 * An RecordReference is a low level API that allows users and
854 * addon author to perform meta-operations on a record.
855 */
856 class RecordReference<T extends Model> {
857 /**
858 * The `id` of the record that this reference refers to.
859 */
860 id(): string;
861 /**
862 * How the reference will be looked up when it is loaded: Currently
863 * this always return `identity` to signifying that a record will be
864 * loaded by the `type` and `id`.
865 */
866 remoteType(): string;
867 /**
868 * This API allows you to provide a reference with new data. The
869 * simplest usage of this API is similar to `store.push`: you provide a
870 * normalized hash of data and the object represented by the reference
871 * will update.
872 */
873 push(payload: RSVP.Promise<any> | {}): PromiseObject<T>;
874 /**
875 * If the entity referred to by the reference is already loaded, it is
876 * present as `reference.value`. Otherwise the value returned by this function
877 * is `null`.
878 */
879 value(): T | null;
880 /**
881 * Triggers a fetch for the backing entity based on its `remoteType`
882 * (see `remoteType` definitions per reference type).
883 */
884 load(): PromiseObject<T>;
885 /**
886 * Reloads the record if it is already loaded. If the record is not
887 * loaded it will load the record via `store.findRecord`
888 */
889 reload(): PromiseObject<T>;
890 }
891 /**
892 * A `ManyArray` is a `MutableArray` that represents the contents of a has-many
893 * relationship.
894 */
895 interface ManyArray<T> extends Ember.MutableArray<T> {}
896 class ManyArray<T> extends Ember.Object.extend(
897 Ember.MutableArray as {},
898 Ember.Evented
899 ) {
900 /**
901 * The loading state of this array
902 */
903 isLoaded: boolean;
904 /**
905 * Metadata associated with the request for async hasMany relationships.
906 */
907 meta: {};
908 /**
909 * Reloads all of the records in the manyArray. If the manyArray
910 * holds a relationship that was originally fetched using a links url
911 * Ember Data will revisit the original links url to repopulate the
912 * relationship.
913 */
914 reload(): PromiseArray<T>;
915 /**
916 * Saves all of the records in the `ManyArray`.
917 */
918 save(): PromiseArray<T>;
919 /**
920 * Create a child record within the owner
921 */
922 createRecord(inputProperties?: {}): T;
923 }
924 /**
925 * A `PromiseArray` is an object that acts like both an `Ember.Array`
926 * and a promise. When the promise is resolved the resulting value
927 * will be set to the `PromiseArray`'s `content` property. This makes
928 * it easy to create data bindings with the `PromiseArray` that will be
929 * updated when the promise resolves.
930 */
931 interface PromiseArray<T, ArrayType extends Ember.ArrayProxy<T>['content'] = Ember.Array<T>>
932 extends Ember.ArrayProxy<T>,
933 PromiseProxyMixin<ArrayType> {}
934 class PromiseArray<T> extends Ember.ArrayProxy<T> {}
935 /**
936 * A `PromiseObject` is an object that acts like both an `Ember.Object`
937 * and a promise. When the promise is resolved, then the resulting value
938 * will be set to the `PromiseObject`'s `content` property. This makes
939 * it easy to create data bindings with the `PromiseObject` that will
940 * be updated when the promise resolves.
941 */
942 interface PromiseObject<T extends object | null>
943 extends PromiseProxyMixin<T> {}
944 class PromiseObject<T> extends ObjectProxy<NonNullable<T>> {}
945 /**
946 * A PromiseManyArray is a PromiseArray that also proxies certain method calls
947 * to the underlying manyArray.
948 * Right now we proxy:
949 */
950 class PromiseManyArray<T extends Model> extends PromiseArray<T, Ember.ArrayProxy<T>> {
951 /**
952 * Reloads all of the records in the manyArray. If the manyArray
953 * holds a relationship that was originally fetched using a links url
954 * Ember Data will revisit the original links url to repopulate the
955 * relationship.
956 */
957 reload(): PromiseManyArray<T>;
958 /**
959 * Create a child record within the owner
960 */
961 createRecord(inputProperties?: {}): T;
962 }
963 class SnapshotRecordArray<K extends keyof ModelRegistry> {
964 /**
965 * Number of records in the array
966 */
967 length: number;
968 /**
969 * Meta objects for the record array.
970 */
971 meta: {};
972 /**
973 * A hash of adapter options passed into the store method for this request.
974 */
975 adapterOptions: {};
976 /**
977 * The relationships to include for this request.
978 */
979 include: string | any[];
980 /**
981 * The type of the underlying records for the snapshots in the array, as a DS.Model
982 */
983 type: ModelRegistry[K];
984 /**
985 * Get snapshots of the underlying record array
986 */
987 snapshots(): Snapshot[];
988 }
989 class Snapshot<K extends keyof ModelRegistry = keyof ModelRegistry> {
990 /**
991 * The underlying record for this snapshot. Can be used to access methods and
992 * properties defined on the record.
993 */
994 record: ModelRegistry[K];
995 /**
996 * The id of the snapshot's underlying record
997 */
998 id: string;
999 /**
1000 * A hash of adapter options
1001 */
1002 adapterOptions: {};
1003 /**
1004 * The name of the type of the underlying record for this snapshot, as a string.
1005 */
1006 modelName: K;
1007 /**
1008 * The type of the underlying record for this snapshot, as a DS.Model.
1009 */
1010 type: ModelRegistry[K];
1011 /**
1012 * Returns the value of an attribute.
1013 */
1014 attr<L extends AttributesFor<ModelRegistry[K]>>(
1015 keyName: L
1016 ): ModelRegistry[K][L];
1017 /**
1018 * Returns all attributes and their corresponding values.
1019 */
1020 attributes(): { [L in keyof ModelRegistry[K]]: ModelRegistry[K][L] };
1021 /**
1022 * Returns all changed attributes and their old and new values.
1023 */
1024 changedAttributes(): Partial<
1025 { [L in keyof ModelRegistry[K]]: ModelRegistry[K][L] }
1026 >;
1027 /**
1028 * Returns the current value of a belongsTo relationship.
1029 */
1030 belongsTo<L extends RelationshipsFor<ModelRegistry[K]>>(
1031 keyName: L,
1032 options?: {}
1033 ): Snapshot | null | undefined;
1034 belongsTo<L extends RelationshipsFor<ModelRegistry[K]>>(
1035 keyName: L,
1036 options: { id: true }
1037 ): string | null | undefined;
1038
1039 /**
1040 * Returns the current value of a hasMany relationship.
1041 */
1042 hasMany<L extends RelationshipsFor<ModelRegistry[K]>>(
1043 keyName: L,
1044 options?: { ids: false }
1045 ): Snapshot[] | undefined;
1046 hasMany<L extends RelationshipsFor<ModelRegistry[K]>>(
1047 keyName: L,
1048 options: { ids: true }
1049 ): string[] | undefined;
1050 /**
1051 * Iterates through all the attributes of the model, calling the passed
1052 * function on each attribute.
1053 */
1054 eachAttribute<M extends ModelRegistry[K]>(
1055 callback: (key: ModelKeys<M>, meta: AttributeMeta<M>) => void,
1056 binding?: {}
1057 ): void;
1058 /**
1059 * Iterates through all the relationships of the model, calling the passed
1060 * function on each relationship.
1061 */
1062 eachRelationship<M extends ModelRegistry[K]>(
1063 callback: (key: ModelKeys<M>, meta: RelationshipMeta<M>) => void,
1064 binding?: {}
1065 ): void;
1066 /**
1067 * Serializes the snapshot using the serializer for the model.
1068 */
1069 serialize<O extends object>(options: O): object;
1070 }
1071
1072 /**
1073 * The store contains all of the data for records loaded from the server.
1074 * It is also responsible for creating instances of `DS.Model` that wrap
1075 * the individual data for a record, so that they can be bound to in your
1076 * Handlebars templates.
1077 */
1078 class Store extends Ember.Service {
1079 /**
1080 * The default adapter to use to communicate to a backend server or
1081 * other persistence layer. This will be overridden by an application
1082 * adapter if present.
1083 */
1084 adapter: string;
1085 /**
1086 * Create a new record in the current store. The properties passed
1087 * to this method are set on the newly created record.
1088 */
1089 createRecord<K extends keyof ModelRegistry>(
1090 modelName: K,
1091 inputProperties?: {}
1092 ): ModelRegistry[K];
1093 /**
1094 * For symmetry, a record can be deleted via the store.
1095 */
1096 deleteRecord(record: Model): void;
1097 /**
1098 * For symmetry, a record can be unloaded via the store.
1099 * This will cause the record to be destroyed and freed up for garbage collection.
1100 */
1101 unloadRecord(record: Model): void;
1102 /**
1103 * This method returns a record for a given type and id combination.
1104 */
1105 findRecord<K extends keyof ModelRegistry>(
1106 modelName: K,
1107 id: string | number,
1108 options?: {}
1109 ): PromiseObject<ModelRegistry[K]>;
1110 /**
1111 * Get the reference for the specified record.
1112 */
1113 getReference<K extends keyof ModelRegistry>(
1114 modelName: K,
1115 id: string | number
1116 ): RecordReference<ModelRegistry[K]>;
1117 /**
1118 * Get a record by a given type and ID without triggering a fetch.
1119 */
1120 peekRecord<K extends keyof ModelRegistry>(
1121 modelName: K,
1122 id: string | number
1123 ): ModelRegistry[K] | null;
1124 /**
1125 * This method returns true if a record for a given modelName and id is already
1126 * loaded in the store. Use this function to know beforehand if a findRecord()
1127 * will result in a request or that it will be a cache hit.
1128 */
1129 hasRecordForId<K extends keyof ModelRegistry>(
1130 modelName: K,
1131 id: string | number
1132 ): boolean;
1133 /**
1134 * This method delegates a query to the adapter. This is the one place where
1135 * adapter-level semantics are exposed to the application.
1136 */
1137 query<K extends keyof ModelRegistry>(
1138 modelName: K,
1139 query: object,
1140 options?: { adapterOptions?: object | undefined }
1141 ): AdapterPopulatedRecordArray<ModelRegistry[K]> &
1142 PromiseArray<ModelRegistry[K], Ember.ArrayProxy<ModelRegistry[K]>>;
1143 /**
1144 * This method makes a request for one record, where the `id` is not known
1145 * beforehand (if the `id` is known, use [`findRecord`](#method_findRecord)
1146 * instead).
1147 */
1148 queryRecord<K extends keyof ModelRegistry>(
1149 modelName: K,
1150 query: object,
1151 options?: { adapterOptions?: object | undefined }
1152 ): RSVP.Promise<ModelRegistry[K]>;
1153 /**
1154 * `findAll` asks the adapter's `findAll` method to find the records for the
1155 * given type, and returns a promise which will resolve with all records of
1156 * this type present in the store, even if the adapter only returns a subset
1157 * of them.
1158 */
1159 findAll<K extends keyof ModelRegistry>(
1160 modelName: K,
1161 options?: {
1162 reload?: boolean | undefined;
1163 backgroundReload?: boolean | undefined;
1164 include?: string | undefined;
1165 adapterOptions?: any;
1166 }
1167 ): PromiseArray<ModelRegistry[K], Ember.ArrayProxy<ModelRegistry[K]>>;
1168 /**
1169 * This method returns a filtered array that contains all of the
1170 * known records for a given type in the store.
1171 */
1172 peekAll<K extends keyof ModelRegistry>(
1173 modelName: K
1174 ): RecordArray<ModelRegistry[K]>;
1175 /**
1176 * This method unloads all records in the store.
1177 * It schedules unloading to happen during the next run loop.
1178 */
1179 unloadAll<K extends keyof ModelRegistry>(modelName?: K): void;
1180 /**
1181 * DEPRECATED:
1182 * This method has been deprecated and is an alias for store.hasRecordForId, which should
1183 * be used instead.
1184 */
1185 recordIsLoaded<K extends keyof ModelRegistry>(
1186 modelName: K,
1187 id: string
1188 ): boolean;
1189 /**
1190 * Returns the model class for the particular `modelName`.
1191 */
1192 modelFor<K extends keyof ModelRegistry>(modelName: K): ModelRegistry[K];
1193 /**
1194 * Push some data for a given type into the store.
1195 */
1196 push(data: {}): Model | any[];
1197 /**
1198 * Push some raw data into the store.
1199 */
1200 pushPayload<K extends keyof ModelRegistry>(
1201 modelName: K,
1202 inputPayload: {}
1203 ): any;
1204 pushPayload(inputPayload: {}): any;
1205 /**
1206 * `normalize` converts a json payload into the normalized form that
1207 * [push](#method_push) expects.
1208 */
1209 normalize<K extends keyof ModelRegistry>(modelName: K, payload: {}): {};
1210 /**
1211 * Returns an instance of the adapter for a given type. For
1212 * example, `adapterFor('person')` will return an instance of
1213 * `App.PersonAdapter`.
1214 */
1215 adapterFor<K extends keyof AdapterRegistry>(
1216 modelName: K
1217 ): AdapterRegistry[K];
1218 /**
1219 * Returns an instance of the serializer for a given type. For
1220 * example, `serializerFor('person')` will return an instance of
1221 * `App.PersonSerializer`.
1222 */
1223 serializerFor<K extends keyof SerializerRegistry>(
1224 modelName: K
1225 ): SerializerRegistry[K];
1226 }
1227 /**
1228 * The `JSONAPIAdapter` is the default adapter used by Ember Data. It
1229 * is responsible for transforming the store's requests into HTTP
1230 * requests that follow the [JSON API](http://jsonapi.org/format/)
1231 * format.
1232 */
1233 class JSONAPIAdapter extends RESTAdapter {}
1234 /**
1235 * The REST adapter allows your store to communicate with an HTTP server by
1236 * transmitting JSON via XHR. Most Ember.js apps that consume a JSON API
1237 * should use the REST adapter.
1238 */
1239 class RESTAdapter extends Adapter implements BuildURLMixin {
1240 /**
1241 * Takes a URL, an HTTP method and a hash of data, and makes an HTTP request.
1242 */
1243 ajax(url: string, type: string, options?: object): RSVP.Promise<any>;
1244 /**
1245 * Generate ajax options
1246 */
1247 ajaxOptions(url: string, type: string, options?: object): object;
1248 /**
1249 * By default, the RESTAdapter will send the query params sorted alphabetically to the
1250 * server.
1251 */
1252 sortQueryParams(obj: {}): {};
1253 /**
1254 * Called by the store in order to fetch the JSON for a given
1255 * type and ID.
1256 */
1257 findRecord<K extends keyof ModelRegistry>(
1258 store: Store,
1259 type: ModelRegistry[K],
1260 id: string,
1261 snapshot: Snapshot<K>
1262 ): RSVP.Promise<any>;
1263 /**
1264 * Called by the store in order to fetch a JSON array for all
1265 * of the records for a given type.
1266 */
1267 findAll<K extends keyof ModelRegistry>(
1268 store: Store,
1269 type: ModelRegistry[K],
1270 sinceToken: string,
1271 snapshotRecordArray: SnapshotRecordArray<K>
1272 ): RSVP.Promise<any>;
1273 /**
1274 * Called by the store in order to fetch a JSON array for
1275 * the records that match a particular query.
1276 */
1277 query<K extends keyof ModelRegistry>(
1278 store: Store,
1279 type: ModelRegistry[K],
1280 query: {}
1281 ): RSVP.Promise<any>;
1282 /**
1283 * Called by the store in order to fetch a JSON object for
1284 * the record that matches a particular query.
1285 */
1286 queryRecord<K extends keyof ModelRegistry>(
1287 store: Store,
1288 type: ModelRegistry[K],
1289 query: {}
1290 ): RSVP.Promise<any>;
1291 /**
1292 * Called by the store in order to fetch several records together if `coalesceFindRequests` is true
1293 */
1294 findMany<K extends keyof ModelRegistry>(
1295 store: Store,
1296 type: ModelRegistry[K],
1297 ids: any[],
1298 snapshots: any[]
1299 ): RSVP.Promise<any>;
1300 /**
1301 * Called by the store in order to fetch a JSON array for
1302 * the unloaded records in a has-many relationship that were originally
1303 * specified as a URL (inside of `links`).
1304 */
1305 findHasMany<K extends keyof ModelRegistry>(
1306 store: Store,
1307 snapshot: Snapshot<K>,
1308 url: string,
1309 relationship: {}
1310 ): RSVP.Promise<any>;
1311 /**
1312 * Called by the store in order to fetch the JSON for the unloaded record in a
1313 * belongs-to relationship that was originally specified as a URL (inside of
1314 * `links`).
1315 */
1316 findBelongsTo<K extends keyof ModelRegistry>(
1317 store: Store,
1318 snapshot: Snapshot<K>,
1319 url: string
1320 ): RSVP.Promise<any>;
1321 /**
1322 * Called by the store when a newly created record is
1323 * saved via the `save` method on a model record instance.
1324 */
1325 createRecord<K extends keyof ModelRegistry>(
1326 store: Store,
1327 type: ModelRegistry[K],
1328 snapshot: Snapshot<K>
1329 ): RSVP.Promise<any>;
1330 /**
1331 * Called by the store when an existing record is saved
1332 * via the `save` method on a model record instance.
1333 */
1334 updateRecord<K extends keyof ModelRegistry>(
1335 store: Store,
1336 type: ModelRegistry[K],
1337 snapshot: Snapshot<K>
1338 ): RSVP.Promise<any>;
1339 /**
1340 * Called by the store when a record is deleted.
1341 */
1342 deleteRecord<K extends keyof ModelRegistry>(
1343 store: Store,
1344 type: ModelRegistry[K],
1345 snapshot: Snapshot<K>
1346 ): RSVP.Promise<any>;
1347 /**
1348 * Organize records into groups, each of which is to be passed to separate
1349 * calls to `findMany`.
1350 */
1351 groupRecordsForFindMany(store: Store, snapshots: any[]): any[];
1352 /**
1353 * Takes an ajax response, and returns the json payload or an error.
1354 */
1355 handleResponse(
1356 status: number,
1357 headers: {},
1358 payload: {},
1359 requestData: {}
1360 ): {};
1361 /**
1362 * Default `handleResponse` implementation uses this hook to decide if the
1363 * response is a success.
1364 */
1365 isSuccess(status: number, headers: {}, payload: {}): boolean;
1366 /**
1367 * Default `handleResponse` implementation uses this hook to decide if the
1368 * response is an invalid error.
1369 */
1370 isInvalid(status: number, headers: {}, payload: {}): boolean;
1371 /**
1372 * Get the data (body or query params) for a request.
1373 */
1374 dataForRequest(params: {}): {};
1375 /**
1376 * Get the HTTP method for a request.
1377 */
1378 methodForRequest(params: {}): string;
1379 /**
1380 * Get the URL for a request.
1381 */
1382 urlForRequest(params: {}): string;
1383 /**
1384 * Get the headers for a request.
1385 */
1386 headersForRequest(params: {}): {};
1387 /**
1388 * Builds a URL for a given type and optional ID.
1389 */
1390 buildURL<K extends keyof ModelRegistry>(
1391 modelName?: K,
1392 id?: string | any[] | {} | null,
1393 snapshot?: Snapshot<K> | any[] | null,
1394 requestType?: string,
1395 query?: {}
1396 ): string;
1397 /**
1398 * Used by `findAll` and `findRecord` to build the query's `data` hash supplied to the ajax method.
1399 */
1400 buildQuery<K extends keyof ModelRegistry>(
1401 snapshot: Snapshot<K>
1402 ): Record<string, unknown>;
1403 /**
1404 * Builds a URL for a `store.findRecord(type, id)` call.
1405 */
1406 urlForFindRecord<K extends keyof ModelRegistry>(
1407 id: string,
1408 modelName: K,
1409 snapshot: Snapshot<K>
1410 ): string;
1411 /**
1412 * Builds a URL for a `store.findAll(type)` call.
1413 */
1414 urlForFindAll<K extends keyof ModelRegistry>(
1415 modelName: K,
1416 snapshot: SnapshotRecordArray<K>
1417 ): string;
1418 /**
1419 * Builds a URL for a `store.query(type, query)` call.
1420 */
1421 urlForQuery<K extends keyof ModelRegistry>(
1422 query: {},
1423 modelName: K
1424 ): string;
1425 /**
1426 * Builds a URL for a `store.queryRecord(type, query)` call.
1427 */
1428 urlForQueryRecord<K extends keyof ModelRegistry>(
1429 query: {},
1430 modelName: K
1431 ): string;
1432 /**
1433 * Builds a URL for coalesceing multiple `store.findRecord(type, id)`
1434 * records into 1 request when the adapter's `coalesceFindRequests`
1435 * property is true.
1436 */
1437 urlForFindMany<K extends keyof ModelRegistry>(
1438 ids: any[],
1439 modelName: K,
1440 snapshots: any[]
1441 ): string;
1442 /**
1443 * Builds a URL for fetching a async hasMany relationship when a url
1444 * is not provided by the server.
1445 */
1446 urlForFindHasMany<K extends keyof ModelRegistry>(
1447 id: string,
1448 modelName: K,
1449 snapshot: Snapshot<K>
1450 ): string;
1451 /**
1452 * Builds a URL for fetching a async belongsTo relationship when a url
1453 * is not provided by the server.
1454 */
1455 urlForFindBelongsTo<K extends keyof ModelRegistry>(
1456 id: string,
1457 modelName: K,
1458 snapshot: Snapshot<K>
1459 ): string;
1460 /**
1461 * Builds a URL for a `record.save()` call when the record was created
1462 * locally using `store.createRecord()`.
1463 */
1464 urlForCreateRecord<K extends keyof ModelRegistry>(
1465 modelName: K,
1466 snapshot: Snapshot<K>
1467 ): string;
1468 /**
1469 * Builds a URL for a `record.save()` call when the record has been update locally.
1470 */
1471 urlForUpdateRecord<K extends keyof ModelRegistry>(
1472 id: string,
1473 modelName: K,
1474 snapshot: Snapshot<K>
1475 ): string;
1476 /**
1477 * Builds a URL for a `record.save()` call when the record has been deleted locally.
1478 */
1479 urlForDeleteRecord<K extends keyof ModelRegistry>(
1480 id: string,
1481 modelName: K,
1482 snapshot: Snapshot<K>
1483 ): string;
1484 /**
1485 * Determines the pathname for a given type.
1486 */
1487 pathForType<K extends keyof ModelRegistry>(modelName: K): string;
1488 }
1489
1490 // Instead of declaring `namespace`, `host`, and `headers` as a property we now declare it in an
1491 // interface. This works around the issue noted here with TypeScript 4:
1492 // https://github.com/microsoft/TypeScript/issues/40220
1493 interface RESTAdapter {
1494 /**
1495 * Endpoint paths can be prefixed with a `namespace` by setting the namespace
1496 * property on the adapter:
1497 */
1498 namespace: string;
1499 /**
1500 * An adapter can target other hosts by setting the `host` property.
1501 */
1502 host: string;
1503 /**
1504 * Some APIs require HTTP headers, e.g. to provide an API
1505 * key. Arbitrary headers can be set as key/value pairs on the
1506 * `RESTAdapter`'s `headers` object and Ember Data will send them
1507 * along with each ajax request. For dynamic headers see [headers
1508 * customization](/api/data/classes/DS.RESTAdapter.html#toc_headers-customization).
1509 */
1510 headers: {};
1511 }
1512 /**
1513 * ## Using Embedded Records
1514 */
1515 class EmbeddedRecordsMixin {
1516 /**
1517 * Normalize the record and recursively normalize/extract all the embedded records
1518 * while pushing them into the store as they are encountered
1519 */
1520 normalize(typeClass: Model, hash: {}, prop: string): {};
1521 /**
1522 * Serialize `belongsTo` relationship when it is configured as an embedded object.
1523 */
1524 serializeBelongsTo<K extends keyof ModelRegistry>(
1525 snapshot: Snapshot<K>,
1526 json: {},
1527 relationship: {}
1528 ): any;
1529 /**
1530 * Serializes `hasMany` relationships when it is configured as embedded objects.
1531 */
1532 serializeHasMany<K extends keyof ModelRegistry>(
1533 snapshot: Snapshot<K>,
1534 json: {},
1535 relationship: {}
1536 ): any;
1537 /**
1538 * When serializing an embedded record, modify the property (in the json payload)
1539 * that refers to the parent record (foreign key for relationship).
1540 */
1541 removeEmbeddedForeignKey<K extends keyof ModelRegistry>(
1542 snapshot: Snapshot<K>,
1543 embeddedSnapshot: Snapshot<K>,
1544 relationship: {},
1545 json: {}
1546 ): any;
1547 }
1548 /**
1549 * Ember Data 2.0 Serializer:
1550 */
1551 class JSONAPISerializer extends JSONSerializer {
1552 pushPayload(store: Store, payload: {}): any;
1553 /**
1554 * Dasherizes and singularizes the model name in the payload to match
1555 * the format Ember Data uses internally for the model name.
1556 */
1557 modelNameFromPayloadKey(key: string): string;
1558 /**
1559 * Converts the model name to a pluralized version of the model name.
1560 */
1561 payloadKeyFromModelName<K extends keyof ModelRegistry>(
1562 modelName: K
1563 ): string;
1564 /**
1565 * `keyForAttribute` can be used to define rules for how to convert an
1566 * attribute name in your model to a key in your JSON.
1567 * By default `JSONAPISerializer` follows the format used on the examples of
1568 * http://jsonapi.org/format and uses dashes as the word separator in the JSON
1569 * attribute keys.
1570 */
1571 keyForAttribute(key: string, method: string): string;
1572 /**
1573 * `keyForRelationship` can be used to define a custom key when
1574 * serializing and deserializing relationship properties.
1575 * By default `JSONAPISerializer` follows the format used on the examples of
1576 * http://jsonapi.org/format and uses dashes as word separators in
1577 * relationship properties.
1578 */
1579 keyForRelationship(
1580 key: string,
1581 typeClass: string,
1582 method: string
1583 ): string;
1584 /**
1585 * `modelNameFromPayloadType` can be used to change the mapping for a DS model
1586 * name, taken from the value in the payload.
1587 */
1588 modelNameFromPayloadType(payloadType: string): string;
1589 /**
1590 * `payloadTypeFromModelName` can be used to change the mapping for the type in
1591 * the payload, taken from the model name.
1592 */
1593 payloadTypeFromModelName<K extends keyof ModelRegistry>(
1594 modelName: K
1595 ): string;
1596 }
1597 /**
1598 * Ember Data 2.0 Serializer:
1599 */
1600 class JSONSerializer extends Serializer {
1601 /**
1602 * The `primaryKey` is used when serializing and deserializing
1603 * data. Ember Data always uses the `id` property to store the id of
1604 * the record. The external source may not always follow this
1605 * convention. In these cases it is useful to override the
1606 * `primaryKey` property to match the `primaryKey` of your external
1607 * store.
1608 */
1609 primaryKey: string;
1610 /**
1611 * The `attrs` object can be used to declare a simple mapping between
1612 * property names on `DS.Model` records and payload keys in the
1613 * serialized JSON object representing the record. An object with the
1614 * property `key` can also be used to designate the attribute's key on
1615 * the response payload.
1616 */
1617 attrs: {};
1618 /**
1619 * The `normalizeResponse` method is used to normalize a payload from the
1620 * server to a JSON-API Document.
1621 */
1622 normalizeResponse(
1623 store: Store,
1624 primaryModelClass: Model,
1625 payload: {},
1626 id: string | number,
1627 requestType: string
1628 ): {};
1629 normalizeFindRecordResponse(
1630 store: Store,
1631 primaryModelClass: Model,
1632 payload: {},
1633 id: string | number,
1634 requestType: string
1635 ): {};
1636 normalizeQueryRecordResponse(
1637 store: Store,
1638 primaryModelClass: Model,
1639 payload: {},
1640 id: string | number,
1641 requestType: string
1642 ): {};
1643 normalizeFindAllResponse(
1644 store: Store,
1645 primaryModelClass: Model,
1646 payload: {},
1647 id: string | number,
1648 requestType: string
1649 ): {};
1650 normalizeFindBelongsToResponse(
1651 store: Store,
1652 primaryModelClass: Model,
1653 payload: {},
1654 id: string | number,
1655 requestType: string
1656 ): {};
1657 normalizeFindHasManyResponse(
1658 store: Store,
1659 primaryModelClass: Model,
1660 payload: {},
1661 id: string | number,
1662 requestType: string
1663 ): {};
1664 normalizeFindManyResponse(
1665 store: Store,
1666 primaryModelClass: Model,
1667 payload: {},
1668 id: string | number,
1669 requestType: string
1670 ): {};
1671 normalizeQueryResponse(
1672 store: Store,
1673 primaryModelClass: Model,
1674 payload: {},
1675 id: string | number,
1676 requestType: string
1677 ): {};
1678 normalizeCreateRecordResponse(
1679 store: Store,
1680 primaryModelClass: Model,
1681 payload: {},
1682 id: string | number,
1683 requestType: string
1684 ): {};
1685 normalizeDeleteRecordResponse(
1686 store: Store,
1687 primaryModelClass: Model,
1688 payload: {},
1689 id: string | number,
1690 requestType: string
1691 ): {};
1692 normalizeUpdateRecordResponse(
1693 store: Store,
1694 primaryModelClass: Model,
1695 payload: {},
1696 id: string | number,
1697 requestType: string
1698 ): {};
1699 normalizeSaveResponse(
1700 store: Store,
1701 primaryModelClass: Model,
1702 payload: {},
1703 id: string | number,
1704 requestType: string
1705 ): {};
1706 normalizeSingleResponse(
1707 store: Store,
1708 primaryModelClass: Model,
1709 payload: {},
1710 id: string | number,
1711 requestType: string
1712 ): {};
1713 normalizeArrayResponse(
1714 store: Store,
1715 primaryModelClass: Model,
1716 payload: {},
1717 id: string | number,
1718 requestType: string
1719 ): {};
1720 /**
1721 * Normalizes a part of the JSON payload returned by
1722 * the server. You should override this method, munge the hash
1723 * and call super if you have generic normalization to do.
1724 */
1725 normalize(typeClass: Model, hash: {}): {};
1726 /**
1727 * Returns the resource's ID.
1728 */
1729 extractId(modelClass: {}, resourceHash: {}): string;
1730 /**
1731 * Returns the resource's attributes formatted as a JSON-API "attributes object".
1732 */
1733 extractAttributes(modelClass: {}, resourceHash: {}): {};
1734 /**
1735 * Returns a relationship formatted as a JSON-API "relationship object".
1736 */
1737 extractRelationship(
1738 relationshipModelName: {},
1739 relationshipHash: {}
1740 ): {};
1741 /**
1742 * Returns a polymorphic relationship formatted as a JSON-API "relationship object".
1743 */
1744 extractPolymorphicRelationship(
1745 relationshipModelName: {},
1746 relationshipHash: {},
1747 relationshipOptions: {}
1748 ): {};
1749 /**
1750 * Returns the resource's relationships formatted as a JSON-API "relationships object".
1751 */
1752 extractRelationships(modelClass: {}, resourceHash: {}): {};
1753 modelNameFromPayloadKey(key: string): string;
1754 /**
1755 * Check if the given hasMany relationship should be serialized
1756 */
1757 shouldSerializeHasMany<K extends keyof ModelRegistry>(
1758 snapshot: Snapshot<K>,
1759 key: string,
1760 relationshipType: string
1761 ): boolean;
1762 /**
1763 * Called when a record is saved in order to convert the
1764 * record into JSON.
1765 */
1766 serialize<K extends keyof ModelRegistry>(
1767 snapshot: Snapshot<K>,
1768 options: {}
1769 ): {};
1770 /**
1771 * You can use this method to customize how a serialized record is added to the complete
1772 * JSON hash to be sent to the server. By default the JSON Serializer does not namespace
1773 * the payload and just sends the raw serialized JSON object.
1774 * If your server expects namespaced keys, you should consider using the RESTSerializer.
1775 * Otherwise you can override this method to customize how the record is added to the hash.
1776 * The hash property should be modified by reference.
1777 */
1778 serializeIntoHash<K extends keyof ModelRegistry>(
1779 hash: {},
1780 typeClass: ModelRegistry[K],
1781 snapshot: Snapshot<K>,
1782 options?: {}
1783 ): any;
1784 /**
1785 * `serializeAttribute` can be used to customize how `DS.attr`
1786 * properties are serialized
1787 */
1788 serializeAttribute<K extends keyof ModelRegistry>(
1789 snapshot: Snapshot<K>,
1790 json: {},
1791 key: string,
1792 attribute: {}
1793 ): any;
1794 /**
1795 * `serializeBelongsTo` can be used to customize how `DS.belongsTo`
1796 * properties are serialized.
1797 */
1798 serializeBelongsTo<K extends keyof ModelRegistry>(
1799 snapshot: Snapshot<K>,
1800 json: {},
1801 relationship: {}
1802 ): any;
1803 /**
1804 * `serializeHasMany` can be used to customize how `DS.hasMany`
1805 * properties are serialized.
1806 */
1807 serializeHasMany<K extends keyof ModelRegistry>(
1808 snapshot: Snapshot<K>,
1809 json: {},
1810 relationship: {}
1811 ): any;
1812 /**
1813 * You can use this method to customize how polymorphic objects are
1814 * serialized. Objects are considered to be polymorphic if
1815 * `{ polymorphic: true }` is pass as the second argument to the
1816 * `DS.belongsTo` function.
1817 */
1818 serializePolymorphicType<K extends keyof ModelRegistry>(
1819 snapshot: Snapshot<K>,
1820 json: {},
1821 relationship: {}
1822 ): any;
1823 /**
1824 * `extractMeta` is used to deserialize any meta information in the
1825 * adapter payload. By default Ember Data expects meta information to
1826 * be located on the `meta` property of the payload object.
1827 */
1828 extractMeta(store: Store, modelClass: Model, payload: {}): any;
1829 /**
1830 * `extractErrors` is used to extract model errors when a call
1831 * to `DS.Model#save` fails with an `InvalidError`. By default
1832 * Ember Data expects error information to be located on the `errors`
1833 * property of the payload object.
1834 */
1835 extractErrors(
1836 store: Store,
1837 typeClass: Model,
1838 payload: {},
1839 id: string | number
1840 ): {};
1841 /**
1842 * `keyForAttribute` can be used to define rules for how to convert an
1843 * attribute name in your model to a key in your JSON.
1844 */
1845 keyForAttribute(key: string, method: string): string;
1846 /**
1847 * `keyForRelationship` can be used to define a custom key when
1848 * serializing and deserializing relationship properties. By default
1849 * `JSONSerializer` does not provide an implementation of this method.
1850 */
1851 keyForRelationship(
1852 key: string,
1853 typeClass: string,
1854 method: string
1855 ): string;
1856 /**
1857 * `keyForLink` can be used to define a custom key when deserializing link
1858 * properties.
1859 */
1860 keyForLink(key: string, kind: string): string;
1861 modelNameFromPayloadType(type: string): string;
1862 /**
1863 * serializeId can be used to customize how id is serialized
1864 * For example, your server may expect integer datatype of id
1865 */
1866 serializeId<K extends keyof ModelRegistry>(
1867 snapshot: Snapshot<K>,
1868 json: {},
1869 primaryKey: string
1870 ): any;
1871 }
1872 /**
1873 * Normally, applications will use the `RESTSerializer` by implementing
1874 * the `normalize` method.
1875 */
1876 class RESTSerializer extends JSONSerializer {
1877 /**
1878 * `keyForPolymorphicType` can be used to define a custom key when
1879 * serializing and deserializing a polymorphic type. By default, the
1880 * returned key is `${key}Type`.
1881 */
1882 keyForPolymorphicType(
1883 key: string,
1884 typeClass: string,
1885 method: string
1886 ): string;
1887 /**
1888 * Normalizes a part of the JSON payload returned by
1889 * the server. You should override this method, munge the hash
1890 * and call super if you have generic normalization to do.
1891 */
1892 normalize(modelClass: Model, resourceHash: {}, prop?: string): {};
1893 /**
1894 * This method allows you to push a payload containing top-level
1895 * collections of records organized per type.
1896 */
1897 pushPayload(store: Store, payload: {}): any;
1898 /**
1899 * This method is used to convert each JSON root key in the payload
1900 * into a modelName that it can use to look up the appropriate model for
1901 * that part of the payload.
1902 */
1903 modelNameFromPayloadKey(key: string): string;
1904 /**
1905 * Called when a record is saved in order to convert the
1906 * record into JSON.
1907 */
1908 serialize<K extends keyof ModelRegistry>(
1909 snapshot: Snapshot<K>,
1910 options: {}
1911 ): {};
1912 /**
1913 * You can use this method to customize the root keys serialized into the JSON.
1914 * The hash property should be modified by reference (possibly using something like _.extend)
1915 * By default the REST Serializer sends the modelName of a model, which is a camelized
1916 * version of the name.
1917 */
1918 serializeIntoHash<K extends keyof ModelRegistry>(
1919 hash: {},
1920 typeClass: Model,
1921 snapshot: Snapshot<K>,
1922 options?: {}
1923 ): any;
1924 /**
1925 * You can use `payloadKeyFromModelName` to override the root key for an outgoing
1926 * request. By default, the RESTSerializer returns a camelized version of the
1927 * model's name.
1928 */
1929 payloadKeyFromModelName<K extends keyof ModelRegistry>(
1930 modelName: K
1931 ): string;
1932 /**
1933 * You can use this method to customize how polymorphic objects are serialized.
1934 * By default the REST Serializer creates the key by appending `Type` to
1935 * the attribute and value from the model's camelcased model name.
1936 */
1937 serializePolymorphicType<K extends keyof ModelRegistry>(
1938 snapshot: Snapshot<K>,
1939 json: {},
1940 relationship: {}
1941 ): any;
1942 /**
1943 * You can use this method to customize how a polymorphic relationship should
1944 * be extracted.
1945 */
1946 extractPolymorphicRelationship(
1947 relationshipType: {},
1948 relationshipHash: {},
1949