diff --git a/realm/index.d.ts b/realm/index.d.ts index 034d9e390f..01975fa15a 100644 --- a/realm/index.d.ts +++ b/realm/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for realm-js 0.14 +// Type definitions for realm-js 1.0 // Project: https://github.com/realm/realm-js // Definitions by: Akim // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped @@ -6,13 +6,13 @@ declare namespace Realm { /** * PropertyType - * @see { @link https://realm.io/docs/react-native/latest/api/Realm.html#~PropertyType } + * @see { @link https://realm.io/docs/javascript/latest/api/Realm.html#~PropertyType } */ export type PropertyType = string | 'bool' | 'int' | 'float' | 'double' | 'string' | 'data' | 'date' | 'list'; /** * ObjectSchemaProperty - * @see { @link https://realm.io/docs/react-native/latest/api/Realm.html#~ObjectSchemaProperty } + * @see { @link https://realm.io/docs/javascriptlatest/api/Realm.html#~ObjectSchemaProperty } */ export interface ObjectSchemaProperty { type: PropertyType; @@ -29,7 +29,7 @@ declare namespace Realm { /** * ObjectSchema - * @see { @link https://realm.io/docs/react-native/latest/api/Realm.html#~ObjectSchema } + * @see { @link https://realm.io/docs/javascript/latest/api/Realm.html#~ObjectSchema } */ export interface ObjectSchema { name: string; @@ -39,7 +39,7 @@ declare namespace Realm { /** * ObjectClass - * @see { @link https://realm.io/docs/react-native/latest/api/Realm.html#~ObjectClass } + * @see { @link https://realm.io/docs/javascript/latest/api/Realm.html#~ObjectClass } */ export interface ObjectClass { schema: ObjectSchema; @@ -47,15 +47,20 @@ declare namespace Realm { /** * ObjectType - * @see { @link https://realm.io/docs/react-native/latest/api/Realm.html#~ObjectType } + * @see { @link https://realm.io/docs/javascript/latest/api/Realm.html#~ObjectType } */ export interface ObjectType { type: ObjectClass; } + interface SyncConfiguration { + user: User; + url: string; + } + /** * realm configuration - * @see { @link https://realm.io/docs/react-native/latest/api/Realm.html#~Configuration } + * @see { @link https://realm.io/docs/javascript/latest/api/Realm.html#~Configuration } */ export interface Configuration { encryptionKey?: any; @@ -64,6 +69,7 @@ declare namespace Realm { readOnly?: boolean; schema?: ObjectClass[] | ObjectSchema[]; schemaVersion?: number; + sync?: SyncConfiguration; } // object props type @@ -73,13 +79,13 @@ declare namespace Realm { /** * SortDescriptor - * @see { @link https://realm.io/docs/react-native/latest/api/Realm.Collection.html#~SortDescriptor } + * @see { @link https://realm.io/docs/javascript/latest/api/Realm.Collection.html#~SortDescriptor } */ export type SortDescriptor = string | [string, boolean] | any[]; /** * Iterator - * @see { @link https://realm.io/docs/react-native/latest/api/Realm.Collection.html#~Iterator } + * @see { @link https://realm.io/docs/javascript/latest/api/Realm.Collection.html#~Iterator } */ export interface IteratorResult { done: boolean; @@ -87,12 +93,13 @@ declare namespace Realm { } export interface Iterator { - next(value?: any): IteratorResult; + next(done: boolean, value?: any): IteratorResult; + [Symbol.iterator](): any; } /** * Collection - * @see { @link https://realm.io/docs/react-native/latest/api/Realm.Collection.html } + * @see { @link https://realm.io/docs/javascript/latest/api/Realm.Collection.html } */ export interface Collection { readonly length: number; @@ -117,7 +124,7 @@ declare namespace Realm { sorted(descriptor: string | SortDescriptor, reverse?: boolean): Results; /** - * @returns Iterator + * @returns Iterator */ [Symbol.iterator](): Iterator; @@ -150,70 +157,87 @@ declare namespace Realm { /** * @param {number} start? * @param {number} end? - * @returns T[] | Object[] + * @returns T */ slice(start?: number, end?: number): T[]; /** * @param {(object:any,index?:any,collection?:any)=>void} callback * @param {any} thisArg? - * @returns Object|void + * @returns T */ find(callback: (object: any, index?: any, collection?: any) => void, thisArg?: any): T | null | undefined; /** - * @param {(object:any,index?:any,collection?:any)=>void} callback + * @param {(object:any,index?:number,collection?:any)=>void} callback * @param {any} thisArg? * @returns number */ findIndex(callback: (object: any, index?: number, collection?: any) => void, thisArg?: any): number; /** - * @param {(object:T|any,index?:number,collection?:any)=>void} callback + * @param {(object:T,index?:number,collection?:any)=>void} callback * @param {any} thisArg? * @returns void */ forEach(callback: (object: T, index?: number, collection?: any) => void, thisArg?: any): void; /** - * @param {(object:T|any,index?:number,collection?:any)=>void} callback + * @param {(object:T,index?:number,collection?:any)=>void} callback * @param {any} thisArg? * @returns boolean */ every(callback: (object: T, index?: number, collection?: any) => void, thisArg?: any): boolean; /** - * @param {(object:any,index?:number,collection?:any)=>void} callback + * @param {(object:T,index?:number,collection?:any)=>void} callback * @param {any} thisArg? * @returns boolean */ some(callback: (object: T, index?: number, collection?: any) => void, thisArg?: any): boolean; /** - * @param {(object:any,index?:number,collection?:any)=>void} callback + * @param {(object:T,index?:number,collection?:any)=>void} callback * @param {any} thisArg? * @returns any */ map(callback: (object: T, index?: number, collection?: any) => void, thisArg?: any): any[]; /** - * @param {(previousValue:T|any,object?:any,index?:number,collection?:any)=>void} callback + * @param {(previousValue:T,object?:T,index?:number,collection?:any)=>void} callback * @param {any} initialValue? * @returns any */ reduce(callback: (previousValue: T, object?: T, index?: number, collection?: any) => void, initialValue?: any): any; /** - * @param {(previousValue:any,object?:any,index?:any,collection?:any)=>void} callback + * @param {(previousValue:T,object?:T,index?:any,collection?:any)=>void} callback * @param {any} initialValue? * @returns any */ reduceRight(callback: (previousValue: T, object?: T, index?: any, collection?: any) => void, initialValue?: any): any; + + /** + * @param {(collection:any,changes:any)=>void} callback + * @returns void + */ + addListener(callback: (collection: any, changes: any) => void): void; + + /** + * @returns void + */ + removeAllListeners(): void; + + /** + * @param {()=>void} callback + * @returns void + */ + removeListener(callback: () => void): void; } /** * Object - * @see { @link https://realm.io/docs/react-native/latest/api/Realm.Object.html } + * @see { @link https://realm.io/docs/javascript/latest/api/Realm.Object.html } */ export interface Object { /** @@ -224,22 +248,22 @@ declare namespace Realm { /** * List - * @see { @link https://realm.io/docs/react-native/latest/api/Realm.List.html } + * @see { @link https://realm.io/docs/javascript/latest/api/Realm.List.html } */ export interface List extends Collection { /** - * @returns Object|void + * @returns T */ pop(): T | null | undefined; /** - * @param {any} object + * @param {T} object * @returns number */ push(object: T): number; /** - * @returns Object|void + * @returns T */ shift(): T | null | undefined; @@ -247,12 +271,12 @@ declare namespace Realm { * @param {number} index * @param {number} count? * @param {any} object? - * @returns Object + * @returns T */ splice(index: number, count?: number, object?: any): T[]; /** - * @param {any} object + * @param {T} object * @returns number */ unshift(object: T): number; @@ -260,9 +284,81 @@ declare namespace Realm { /** * Results - * @see { @link https://realm.io/docs/react-native/latest/api/Realm.Results.html } + * @see { @link https://realm.io/docs/javascriptlatest/api/Realm.Results.html } */ export type Results = Collection; + + /** + * User + * @see { @link https://realm.io/docs/javascript/latest/api/Realm.Sync.User.html } + */ + export interface User { + all: any; + current: User; + readonly identity: string; + readonly isAdmin: boolean; + readonly server: string; + readonly token: string; + adminUser(adminToken: string): User; + login(server: string, username: string, password: string, callback: (error: any, user: any) => void): void; + loginWithProvider(server: string, provider: string, providerToken: string, callback: (error: any, user: any) => void): void; + register(server: string, username: string, password: string, callback: (error: any, user: any) => void): void; + registerWithProvider(server: string, provider: string, providerToken: string, callback: (error: any, user: any) => void): void; + logout(): void; + openManagementRealm(): Realm; + } + + /** + * Session + * @see { @link https://realm.io/docs/javascript/latest/api/Realm.Sync.Session.html } + */ + export interface Session { + readonly config: any; + readonly state: string; + readonly url: string; + readonly user: User; + } + + /** + * AuthError + * @see { @link https://realm.io/docs/javascript/latest/api/Realm.Sync.AuthError.html } + */ + export interface AuthError { + readonly code: number; + readonly type: string; + } + + /** + * ChangeEvent + * @see { @link https://realm.io/docs/javascript/latest/api/Realm.Sync.ChangeEvent.html } + */ + export interface ChangeEvent { + readonly changes: any; + readonly oldRealm: Realm; + readonly path: string; + readonly realm: Realm; + } + + /** + * LogLevel + * @see { @link https://realm.io/docs/javascript/latest/api/Realm.Sync.html#~LogLevel } + */ + type LogLevelType = string | 'error' | 'info' | 'defug'; + + /** + * Sync + * @see { @link https://realm.io/docs/javascript/latest/api/Realm.Sync.html } + */ + interface Sync { + User: User; + Session: Session; + AuthError: AuthError; + ChangeEvent: ChangeEvent; + addListener(serverURL: string, adminUser: User, regex: string, name: string, changeCallback: () => void): void; + removeAllListeners(name?: string[]): void; + removeListener(regex: string, name: string, changeCallback: () => void): void; + setLogLevel(logLevel: LogLevelType): void; + } } declare class Realm { @@ -274,12 +370,18 @@ declare class Realm { readonly schema: Realm.ObjectSchema[]; + readonly schemaVersion: number; + + static Sync: Realm.Sync; + + syncSession: Realm.Session; + /** - * @param {string} path? + * @param {string} path * @param {any} encryptionKey? * @returns number */ - schemaVersion(path?: string, encryptionKey?: any): number; + static schemaVersion(path: string, encryptionKey?: any): number; /** * @param {Realm.Configuration} config? @@ -293,14 +395,14 @@ declare class Realm { /** * @param {string|Realm.ObjectType} type - * @param {Realm.ObjectPropsType} properties + * @param {T&Realm.ObjectPropsType} properties * @param {boolean} update? - * @returns Realm.Object|T|any + * @returns T */ create(type: string | Realm.ObjectType, properties: T & Realm.ObjectPropsType, update?: boolean): T; /** - * @param {Realm.Object|Realm.Object[]|Realm.List|Realm.Results|any} object + * @param {Realm.Object|Realm.Object[]|Realm.List|Realm.Results|any} object * @returns void */ delete(object: Realm.Object | Realm.Object[] | Realm.List | Realm.Results | any): void; @@ -313,13 +415,13 @@ declare class Realm { /** * @param {string|Realm.ObjectType} type * @param {number|string} key - * @returns Realm.Object|void + * @returns T */ objectForPrimaryKey(type: string | Realm.ObjectType, key: number | string): T | void; /** * @param {string|Realm.ObjectType} type - * @returns Realm.Results + * @returns Realm */ objects(type: string | Realm.ObjectType): Realm.ObjectType & Realm.Results; diff --git a/realm/realm-tests.ts b/realm/realm-tests.ts index e99247919b..2daf41ef74 100644 --- a/realm/realm-tests.ts +++ b/realm/realm-tests.ts @@ -15,7 +15,7 @@ const personSchema = { const key = new Int8Array(64); // constructor test -let realm = new Realm({ +const realm = new Realm({ schema: [personSchema], encryptionKey: key }); @@ -59,3 +59,41 @@ realm.removeAllListeners(); allPerson.find((person: any) => { return person.name === 'Jack'; }); + +const currentVersion = Realm.schemaVersion(Realm.defaultPath); + +// username/password authentication +Realm.Sync.User.register('http://localhost:9080', 'username@example.com', 'p@s$w0rd', (error, user) => { /* ... */ }); + +Realm.Sync.User.login('http://localhost.com:9080', 'username@example.com', 'p@s$w0rd', (error, user) => { + + const todoSchema = { + name: 'Todo', + primaryKey: 'id', + properties: { + id: 'int', + task: 'string', + } + }; + + // sync test + const realm = new Realm({ + schema: [todoSchema], + sync: { user, url: 'realm://localhost:9080/~/todos' } + }); + + // session test + const session = realm.syncSession; + const sessionUrl = session.config.url; +}); + +// facebook authentication +const fbAccessToken = 'acc3ssT0ken...'; +Realm.Sync.User.registerWithProvider('http://localhost:9080', 'facebook', fbAccessToken, (error, user) => { /* ... */ }); + +// user test +const user = Realm.Sync.User.current; +const users = Realm.Sync.User.all; + +// access control test +const managementRealm = user.openManagementRealm();