import EmberObject from '@ember/object'; import MirageModelRegistry from 'ember-cli-mirage/registries/model'; import MirageSchemaRegistry from 'ember-cli-mirage/registries/schema'; // eslint-disable-next-line ember/use-ember-data-rfc-395-imports import DS from 'ember-data'; // eslint-disable-next-line ember/use-ember-data-rfc-395-imports import EmberDataModelRegistry from 'ember-data/types/registries/model'; import { BelongsTo } from 'miragejs/-types'; declare global { // eslint-disable-next-line no-redeclare const server: Server; // TODO: only in tests? } export type ID = number | string; interface AnyAttrs { [key: string]: any; } type DatabaseRecord = T & { id: ID }; export interface DatabaseCollection { insert(data: S): S extends T ? DatabaseRecord : Array>; find(ids: S): S extends ID ? DatabaseRecord : Array>; findBy(query: T): DatabaseRecord; where(query: T | ((r: DatabaseRecord) => boolean)): Array>; update(attrs: T): Array>; update(target: ID | T, attrs: T): Array>; remove(target?: ID | T): void; firstOrCreate(query: T, attributesForCreate?: T): DatabaseRecord; } export interface Database { [collectionName: string]: DatabaseCollection; } export type ModelInstanceAttrs = { [P in keyof T]: T[P] extends DS.Model & DS.PromiseObject ? ModelInstance : T[P] extends DS.Model ? ModelInstance : T[P] extends DS.PromiseManyArray ? Collection : T[P] extends DS.Model[] & DS.PromiseManyArray ? Collection : T[P] extends DS.Model[] ? Collection : T[P] extends Date ? Date | string : T[P]; }; interface ModelInstanceShared { id: ID; attrs: T; _schema: Schema; save(): void; update(key: K, val: T[K]): void; update(attrs: { [k in keyof T]: T[K] }): void; destroy(): void; isNew(): boolean; isSaved(): boolean; reload(): void; toString(): string; } export function hasMany(model: string): void; export type ModelInstance = ModelInstanceShared & ModelInstanceAttrs; export class ModelClass { extend(attrs: unknown): ModelClass; } export const Model: ModelClass; export interface Collection { models: Array>; length: number; modelName: string; update(key: K, val: T[K]): void; update(attrs: { [k in keyof T]: T[K] }): void; save(): void; reload(): void; destroy(): void; sort(sortFn: (a: ModelInstance, b: ModelInstance) => number): Collection; filter(filterFn: (model: ModelInstance) => boolean): Collection; } interface SchemaModelCollection { new (attrs: Partial>): ModelInstance; create(attrs: Partial>): ModelInstance; update(attrs: Partial>): ModelInstance; all(): Collection; find(ids: S): S extends ID ? ModelInstance : Collection; findBy(query: Partial>): ModelInstance; first(): ModelInstance; where(query: Partial> | ((r: ModelInstance) => boolean)): Collection; } export type Schema = { [modelName in keyof MirageSchemaRegistry]: SchemaModelCollection; } & { [modelName: string]: SchemaModelCollection; } & { db: Database; }; export declare class Response { constructor(code: number, headers?: Record, body?: Record); } export interface Request { requestBody: any; url: string; params: { [key: string]: string | number; }; queryParams: { [key: string]: string; }; method: string; } export type NormalizedRequestAttrs = { [P in keyof T]: T[P] extends DS.Model & DS.PromiseObject ? never : T[P] extends DS.Model ? never : T[P] extends DS.PromiseManyArray ? never : T[P] extends DS.Model[] & DS.PromiseManyArray ? never : T[P] extends DS.Model[] ? never : T[P]; }; export interface HandlerContext { request: Request; serialize(modelOrCollection: ModelInstance | ModelInstance[] | SchemaModelCollection, serializerName?: string): any; normalizedRequestAttrs(model: M): NormalizedRequestAttrs; } interface HandlerObject { [k: string]: any; } interface HandlerOptions { timing?: number; coalesce?: boolean; } export type HandlerFunction = (this: HandlerContext, schema: Schema, request: Request) => any; /* tslint:disable unified-signatures */ export function handlerDefinition(path: string, options?: HandlerOptions): void; export function handlerDefinition(path: string, shorthand: string | string[], options?: HandlerOptions): void; export function handlerDefinition( path: string, shorthand: string | string[], responseCode: number, options?: HandlerOptions ): void; export function handlerDefinition(path: string, responseCode?: number, options?: HandlerOptions): void; export function handlerDefinition( path: string, handler: HandlerFunction | HandlerObject, options?: HandlerOptions ): void; export function handlerDefinition( path: string, handler: HandlerFunction | HandlerObject, responseCode: number, options?: HandlerOptions ): void; /* tslint:enable unified-signatures */ export type resourceAction = 'index' | 'show' | 'create' | 'update' | 'delete'; export type ModelAttrs = { [P in keyof T]: P extends 'id' ? string | number : T[P] extends DS.Model & DS.PromiseObject ? ModelInstance : T[P] extends DS.Model ? ModelInstance : T[P] extends DS.PromiseManyArray ? Array> : T[P] extends DS.Model[] & DS.PromiseManyArray ? Array> : T[P] extends DS.Model[] ? Array> : T[P] extends Date ? Date | string : T[P]; }; export type ModelRegistry = EmberDataModelRegistry & MirageModelRegistry; export interface Server { schema: Schema; db: Database; namespace: string; timing: number; logging: boolean; pretender: any; urlPrefix: string; get: typeof handlerDefinition; post: typeof handlerDefinition; put: typeof handlerDefinition; patch: typeof handlerDefinition; del: typeof handlerDefinition; resource( resourceName: string, options?: { only?: resourceAction[]; except?: resourceAction[]; path?: string } ): void; loadFixtures(...fixtures: string[]): void; // TODO when https://github.com/Microsoft/TypeScript/issues/1360 // passthrough(...paths: string[], verbs?: Verb[]): void; passthrough(...args: any[]): void; create(modelName: T, ...traits: string[]): ModelInstance; create( modelName: T, attrs?: Partial>, ...traits: string[] ): ModelInstance; createList( modelName: T, amount: number, ...traits: string[] ): Array>; createList( modelName: T, amount: number, attrs?: Partial>, ...traits: string[] ): Array>; shutdown(): void; } export type TraitOptions = AnyAttrs & { afterCreate?: (obj: ModelInstance, svr: Server) => void; }; export interface Trait = Record> { extension: O; __isTrait__: true; } export function trait = TraitOptions>( options: O ): Trait; // TODO when https://github.com/Microsoft/TypeScript/issues/1360 // function association(...traits: string[], overrides?: { [key: string]: any }): any; export function association(...args: any[]): any; export { belongsTo } from 'miragejs'; export type FactoryAttrs = { [P in keyof T]?: T[P] | BelongsTo | ((index: number) => T[P]); } & { afterCreate?(newObj: ModelInstance, server: Server): void; }; export class FactoryClass { extend(attrs: FactoryAttrs): FactoryClass; } export const Factory: FactoryClass; export class JSONAPISerializer extends EmberObject { declare request: Request; keyForAttribute(attr: string): string; keyForCollection(modelName: string): string; keyForModel(modelName: string): string; keyForRelationship(relationship: string): string; typeKeyForModel(model: ModelInstance): string; serialize(object: ModelInstance, request: Request): any; normalize(json: any): any; }