UNPKG

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