diff --git a/types/phoenix/index.d.ts b/types/phoenix/index.d.ts index daff513651..22a4d25f40 100644 --- a/types/phoenix/index.d.ts +++ b/types/phoenix/index.d.ts @@ -1,160 +1,177 @@ // Type definitions for phoenix 1.4 // Project: https://github.com/phoenixframework/phoenix -// Definitions by: Mirosław Ciastek , John Goff , Po Chen +// Definitions by: Mirosław Ciastek +// John Goff +// Po Chen // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.4 -declare module 'phoenix' { - class Push { - constructor( - channel: Channel, - event: string, - payload: object, - timeout: number, - ); +export class Push { + constructor( + channel: Channel, + event: string, + payload: object, + timeout: number, + ); - send(): void; - resend(timeout: number): void; + send(): void; + resend(timeout: number): void; - receive(status: string, callback: (response?: any) => any): this; - } - - class Channel { - constructor(topic: string, params?: object | Function, socket?: Socket); - - join(timeout?: number): Push; - leave(timeout?: number): Push; - - onClose(callback: Function): void; - onError(callback: (reason?: any) => void): void; - onMessage(event: string, payload: any, ref: any): any; - - on(event: string, callback: (response?: any) => void): void; - off(event: string): void; - - push(event: string, payload: object, timeout?: number): Push; - } - - type ConnectionState = 'connecting' | 'open' | 'closing' | 'closed'; - - interface SocketConnectOption { - params: object | Function; - transport: any; - timeout: number; - heartbeatIntervalMs: number; - reconnectAfterMs: number; - longpollernumber: number; - encode: (payload: object, callback: Function) => any; - decode: (payload: string, callback: Function) => any; - logger: (kind: string, message: string, data: any) => void; - } - - class Socket { - constructor(endPoint: string, opts?: Partial); - - protocol(): string; - endPointURL(): string; - - connect(params?: any): void; - disconnect(callback?: Function, code?: number, reason?: string): void; - connectionState(): ConnectionState; - isConnected(): boolean; - - remove(channel: Channel): void; - channel(topic: string, chanParams?: object): Channel; - push(data: object): void; - - log(kind: string, message: string, data: object): void; - hasLogger(): boolean; - - onOpen(callback: Function): void; - onClose(callback: Function): void; - onError(callback: Function): void; - onMessage(callback: Function): void; - - makeRef(): string; - } - - class LongPoll { - constructor(endPoint: string); - - normalizeEndpoint(endPoint: string): string; - endpointURL(): string; - - closeAndRetry(): void; - ontimeout(): void; - - poll(): void; - - send(body: any): void; - close(code?: any, reason?: any): void; - } - - class Ajax { - static states: {[state: string]: number}; - - static request( - method: string, - endPoint: string, - accept: string, - body: any, - timeout?: number, - ontimeout?: any, - callback?: (response?: any) => void - ): void; - - static xdomainRequest( - req: any, - method: string, - endPoint: string, - body: any, - timeout?: number, - ontimeout?: any, - callback?: (response?: any) => void - ): void; - - static xhrRequest( - req: any, - method: string, - endPoint: string, - accept: string, - body: any, - timeout?: number, - ontimeout?: any, - callback?: (response?: any) => void - ): void; - - static parseJSON(resp: string): JSON; - static serialize(obj: any, parentKey: string): string; - static appendParams(url: string, params: any): string; - } - - class Presence { - constructor(channel: Channel, opts?: object); - - onJoin(callback: Function): void; - onLeave(callback: Function): void; - onSync(callback: Function): void; - list(chooser?: (key: string, presence: any) => T): T[]; - inPendingSyncState(): boolean; - - static syncState( - currentState: object, - newState: object, - onJoin?: (key?: string, currentPresence?: any, newPresence?: any) => void, - onLeave?: (key?: string, currentPresence?: any, newPresence?: any) => void - ): any; - - static syncDiff( - currentState: object, - diff: {joins: object; leaves: object}, - onJoin?: (key?: string, currentPresence?: any, newPresence?: any) => void, - onLeave?: (key?: string, currentPresence?: any, newPresence?: any) => void - ): any; - - static list( - presences: object, - chooser?: (key: string, presence: any) => T, - ): T[]; - } + receive(status: string, callback: (response?: any) => any): this; +} + +export class Channel { + constructor(topic: string, params?: object | (() => object), socket?: Socket); + + join(timeout?: number): Push; + leave(timeout?: number): Push; + + onClose(callback: (payload: any, ref: any, joinRef: any) => void): void; + onError(callback: (reason?: any) => void): void; + onMessage(event: string, payload: any, ref: any): any; + + on(event: string, callback: (response?: any) => void): number; + off(event: string): void; + + push(event: string, payload: object, timeout?: number): Push; +} + +export type ConnectionState = 'connecting' | 'open' | 'closing' | 'closed'; + +export interface SocketConnectOption { + params: object | (() => object); + transport: string; + timeout: number; + heartbeatIntervalMs: number; + reconnectAfterMs: number; + longpollerTimeout: number; + encode: (payload: object, callback: (encoded: any) => void) => void; + decode: (payload: string, callback: (decoded: any) => void) => void; + logger: (kind: string, message: string, data: any) => void; +} + +export class Socket { + constructor(endPoint: string, opts?: Partial); + + protocol(): string; + endPointURL(): string; + + connect(params?: any): void; + disconnect(callback?: () => void, code?: number, reason?: string): void; + connectionState(): ConnectionState; + isConnected(): boolean; + + remove(channel: Channel): void; + channel(topic: string, chanParams?: object): Channel; + push(data: object): void; + + log(kind: string, message: string, data: any): void; + hasLogger(): boolean; + + onOpen(callback: () => void): void; + onClose(callback: () => void): void; + onError(callback: () => void): void; + onMessage(callback: () => void): void; + + makeRef(): string; +} + +export class LongPoll { + constructor(endPoint: string); + + normalizeEndpoint(endPoint: string): string; + endpointURL(): string; + + closeAndRetry(): void; + ontimeout(): void; + + poll(): void; + + send(body: any): void; + close(code?: any, reason?: any): void; +} + +// tslint:disable:no-unnecessary-class +export class Ajax { + static states: {[state: string]: number}; + + static request( + method: string, + endPoint: string, + accept: string, + body: any, + timeout?: number, + ontimeout?: any, + callback?: (response?: any) => void + ): void; + + static xdomainRequest( + req: any, + method: string, + endPoint: string, + body: any, + timeout?: number, + ontimeout?: any, + callback?: (response?: any) => void + ): void; + + static xhrRequest( + req: any, + method: string, + endPoint: string, + accept: string, + body: any, + timeout?: number, + ontimeout?: any, + callback?: (response?: any) => void + ): void; + + static parseJSON(resp: string): JSON; + static serialize(obj: any, parentKey: string): string; + static appendParams(url: string, params: any): string; +} + +export class Presence { + constructor(channel: Channel, opts?: PresenceOpts); + + onJoin(callback: PresenceOnJoinCallback): void; + onLeave(callback: PresenceOnLeaveCallback): void; + onSync(callback: () => void): void; + list(chooser?: (key: string, presence: any) => T): T[]; + inPendingSyncState(): boolean; + + static syncState( + currentState: object, + newState: object, + onJoin?: PresenceOnJoinCallback, + onLeave?: PresenceOnLeaveCallback + ): any; + + static syncDiff( + currentState: object, + diff: {joins: object; leaves: object}, + onJoin?: PresenceOnJoinCallback, + onLeave?: PresenceOnLeaveCallback + ): any; + + static list( + presences: object, + chooser?: (key: string, presence: any) => T, + ): T[]; +} + +export type PresenceOnJoinCallback = ( + key?: string, + currentPresence?: any, + newPresence?: any +) => void; + +export type PresenceOnLeaveCallback = ( + key?: string, + currentPresence?: any, + newPresence?: any +) => void; + +export interface PresenceOpts { + events?: { state: string; diff: string }; } diff --git a/types/phoenix/phoenix-tests.ts b/types/phoenix/phoenix-tests.ts index 6e34b5c50c..f0b0937851 100644 --- a/types/phoenix/phoenix-tests.ts +++ b/types/phoenix/phoenix-tests.ts @@ -45,7 +45,7 @@ function test_presence() { let presenceState = {}; - const logState = (state: Object) => { + const logState = (state: object) => { Presence.list(state, (id: string) => id).forEach(console.log); }; diff --git a/types/phoenix/tslint.json b/types/phoenix/tslint.json index 6fca40ff8d..03f37e72ca 100644 --- a/types/phoenix/tslint.json +++ b/types/phoenix/tslint.json @@ -1,117 +1,2 @@ -{ - "extends": "dtslint/dt.json", - "rules": { - "arrow-return-shorthand": true, - "ban-types": false, - "callable-types": true, - "class-name": true, - "comment-format": [ - true, - "check-space" - ], - "curly": true, - "deprecation": { - "severity": "warn" - }, - "eofline": true, - "forin": true, - "import-spacing": false, - "indent": [ - true, - "spaces" - ], - "interface-over-type-literal": true, - "label-position": true, - "max-line-length": false, - "member-access": false, - "member-ordering": false, - "no-arg": true, - "no-bitwise": true, - "no-console": [ - true, - "debug", - "info", - "time", - "timeEnd", - "trace" - ], - "no-construct": true, - "no-debugger": true, - "no-declare-current-package": false, - "no-duplicate-super": true, - "no-empty": false, - "no-empty-interface": true, - "no-eval": true, - "no-inferrable-types": [ - true, - "ignore-params" - ], - "no-misused-new": true, - "no-non-null-assertion": true, - "no-redundant-jsdoc": true, - "no-single-declare-module": false, - "no-shadowed-variable": true, - "no-string-literal": false, - "no-string-throw": true, - "no-switch-case-fall-through": true, - "no-trailing-whitespace": true, - "no-unnecessary-class": false, - "no-unnecessary-initializer": true, - "no-unused-expression": true, - "no-use-before-declare": true, - "no-var-keyword": true, - "object-literal-sort-keys": false, - "one-line": [ - true, - "check-open-brace", - "check-catch", - "check-else", - "check-whitespace" - ], - "prefer-const": true, - "quotemark": [ - true, - "single" - ], - "radix": true, - "semicolon": [ - true, - "always" - ], - "triple-equals": [ - true, - "allow-null-check" - ], - "typedef-whitespace": [ - true, - { - "call-signature": "nospace", - "index-signature": "nospace", - "parameter": "nospace", - "property-declaration": "nospace", - "variable-declaration": "nospace" - } - ], - "unified-signatures": true, - "variable-name": false, - "whitespace": [ - true, - "check-branch", - "check-decl", - "check-operator", - "check-separator", - "check-type" - ], - "no-output-on-prefix": true, - "use-input-property-decorator": true, - "use-output-property-decorator": true, - "use-host-property-decorator": true, - "no-input-rename": true, - "no-output-rename": true, - "use-life-cycle-interface": true, - "use-pipe-transform-interface": true, - "component-class-suffix": true, - "directive-class-suffix": true - } -} +{ "extends": "dtslint/dt.json" }