[phoenix] Improve type definitions (#33247)

* Update index.d.ts

* Update index.d.ts

* Update index.d.ts

* Update index.d.ts

* Update index.d.ts

* Update index.d.ts

* Update index.d.ts

* Update tslint.json

* Update tslint.json

* Update index.d.ts

* Update tslint.json

* Update tslint.json

* Update index.d.ts

* Update index.d.ts

* Update phoenix-tests.ts

* Update tslint.json
This commit is contained in:
Po Chen
2019-03-07 05:12:58 +11:00
committed by Nathan Shively-Sanders
parent 269c7718d5
commit b23cd56977
3 changed files with 202 additions and 149 deletions

View File

@@ -1,22 +1,26 @@
// Type definitions for phoenix
// Type definitions for phoenix 1.4
// Project: https://github.com/phoenixframework/phoenix
// Definitions by: Mirosław Ciastek <https://github.com/mciastek>, John Goff <https://github.com/John-Goff>
// Definitions by: Mirosław Ciastek <https://github.com/mciastek>, John Goff <https://github.com/John-Goff>, Po Chen <https://github.com/princemaple>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.4
declare module "phoenix" {
declare module 'phoenix' {
class Push {
constructor(channel: Channel, event: string, payload: any, timeout: number);
constructor(
channel: Channel,
event: string,
payload: object,
timeout: number,
);
resend(timeout: number): void;
send(): void;
resend(timeout: number): void;
receive(status: string, callback: (response?: any) => void): Push;
receive(status: string, callback: (response?: any) => any): this;
}
export class Channel {
constructor(topic: string, params?: Object, socket?: Socket);
rejoinUntilConnected(): void;
class Channel {
constructor(topic: string, params?: object | Function, socket?: Socket);
join(timeout?: number): Push;
leave(timeout?: number): Push;
@@ -28,50 +32,50 @@ declare module "phoenix" {
on(event: string, callback: (response?: any) => void): void;
off(event: string): void;
canPush(): boolean;
push(event: string, payload: Object, timeout?: number): Push;
push(event: string, payload: object, timeout?: number): Push;
}
export class Socket {
constructor(endPoint: string, opts?: Object);
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<SocketConnectOption>);
protocol(): string;
endPointURL(): string;
disconnect(callback?: Function, code?: string, reason?: any): void;
connect(params?: any): void;
disconnect(callback?: Function, code?: number, reason?: string): void;
connectionState(): ConnectionState;
isConnected(): boolean;
log(kind: string, msg: string, data: any): void;
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;
onConnOpen(): void;
onConnClose(event: any): void;
onConnError(error: any): void;
triggerChanError(): void;
connectionState(): string;
isConnected(): boolean;
remove(channel: Channel): void;
channel(topic: string, chanParams?: Object): Channel;
push(data: any): void;
makeRef(): string;
sendHeartbeat(): void;
flushSendBuffer(): void;
onConnMessage(rawMessage: any): void;
}
export class LongPoll {
class LongPoll {
constructor(endPoint: string);
normalizeEndpoint(endPoint: string): string;
@@ -86,8 +90,10 @@ declare module "phoenix" {
close(code?: any, reason?: any): void;
}
export class Ajax {
request(
class Ajax {
static states: {[state: string]: number};
static request(
method: string,
endPoint: string,
accept: string,
@@ -97,7 +103,7 @@ declare module "phoenix" {
callback?: (response?: any) => void
): void;
xdomainRequest(
static xdomainRequest(
req: any,
method: string,
endPoint: string,
@@ -107,7 +113,7 @@ declare module "phoenix" {
callback?: (response?: any) => void
): void;
xhrRequest(
static xhrRequest(
req: any,
method: string,
endPoint: string,
@@ -118,28 +124,37 @@ declare module "phoenix" {
callback?: (response?: any) => void
): void;
parseJSON(resp: string): JSON;
serialize(obj: any, parentKey: string): string;
appendParams(url: string, params: any): string;
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?: Object);
class Presence {
constructor(channel: Channel, opts?: object);
onJoin(callback: Function): void;
onLeave(callback: Function): void;
onSync(callback: Function): void;
list<T = any>(chooser?: (key: string, presence: any) => T): T[];
inPendingSyncState(): boolean;
static syncState(
currentState: any,
newState: any,
currentState: object,
newState: object,
onJoin?: (key?: string, currentPresence?: any, newPresence?: any) => void,
onLeave?: (key?: string, currentPresence?: any, newPresence?: any) => void
): any;
static syncDiff(
currentState: any,
newState: any,
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?: Function): any;
static list<T = any>(
presences: object,
chooser?: (key: string, presence: any) => T,
): T[];
}
}

View File

@@ -1,46 +1,46 @@
import { Socket, Channel, Presence } from 'phoenix';
function test_socket() {
const socket = new Socket("/ws", {params: {userToken: "123"}});
const socket = new Socket('/ws', {params: {userToken: '123'}});
socket.connect();
}
function test_channel() {
const socket = new Socket("/ws", {params: {userToken: "123"}});
const socket = new Socket('/ws', {params: {userToken: '123'}});
socket.connect();
const channel = socket.channel("room:123", {token: '123'});
const channel = socket.channel('room:123', {token: '123'});
channel.on("new_msg", msg => console.log("Got message", msg));
channel.on('new_msg', msg => console.log('Got message', msg));
channel.push("new_msg", {body: 'some value'}, 10000)
.receive("ok", (msg) => console.log("created message", msg))
.receive("error", (reasons) => console.log("create failed", reasons))
.receive("timeout", () => console.log("Networking issue..."));
channel.push('new_msg', {body: 'some value'}, 10000)
.receive('ok', (msg) => console.log('created message', msg))
.receive('error', (reasons) => console.log('create failed', reasons))
.receive('timeout', () => console.log('Networking issue...'));
channel.join()
.receive("ok", ({messages}) => console.log("catching up", messages))
.receive("error", ({reason}) => console.log("failed join", reason))
.receive("timeout", () => console.log("Networking issue. Still waiting..."));
.receive('ok', ({messages}) => console.log('catching up', messages))
.receive('error', ({reason}) => console.log('failed join', reason))
.receive('timeout', () => console.log('Networking issue. Still waiting...'));
}
function test_hooks() {
const socket = new Socket("/ws", {params: {userToken: "123"}});
const socket = new Socket('/ws', {params: {userToken: '123'}});
socket.connect();
socket.onError(() => console.log("there was an error with the connection!"));
socket.onClose(() => console.log("the connection dropped"));
socket.onError(() => console.log('there was an error with the connection!'));
socket.onClose(() => console.log('the connection dropped'));
const channel = socket.channel("room:123", {token: '123'});
const channel = socket.channel('room:123', {token: '123'});
channel.onError(() => console.log("there was an error!"));
channel.onClose(() => console.log("the channel has gone away gracefully"))
channel.onError(() => console.log('there was an error!'));
channel.onClose(() => console.log('the channel has gone away gracefully'));
}
function test_presence() {
const socket = new Socket("/ws", {params: {userToken: "123"}});
const socket = new Socket('/ws', {params: {userToken: '123'}});
const channel = socket.channel("room:123", {token: '123'});
const channel = socket.channel('room:123', {token: '123'});
const presence = new Presence(channel);
let presenceState = {};
@@ -49,12 +49,12 @@ function test_presence() {
Presence.list(state, (id: string) => id).forEach(console.log);
};
channel.on("presence_state", (state) => {
channel.on('presence_state', (state) => {
presenceState = Presence.syncState(presenceState, state);
logState(presenceState);
});
channel.on("presence_diff", (diff) => {
channel.on('presence_diff', (diff) => {
presenceState = Presence.syncState(presenceState, diff);
logState(presenceState);
});

View File

@@ -1,79 +1,117 @@
{
"extends": "dtslint/dt.json",
"rules": {
"adjacent-overload-signatures": false,
"array-type": false,
"arrow-return-shorthand": false,
"ban-types": false,
"callable-types": false,
"comment-format": false,
"dt-header": false,
"eofline": false,
"export-just-namespace": false,
"import-spacing": false,
"interface-name": false,
"interface-over-type-literal": false,
"jsdoc-format": false,
"max-line-length": false,
"member-access": false,
"new-parens": false,
"no-any-union": false,
"no-boolean-literal-compare": false,
"no-conditional-assignment": false,
"no-consecutive-blank-lines": false,
"no-construct": false,
"no-declare-current-package": false,
"no-duplicate-imports": false,
"no-duplicate-variable": false,
"no-empty-interface": false,
"no-for-in-array": false,
"no-inferrable-types": false,
"no-internal-module": false,
"no-irregular-whitespace": false,
"no-mergeable-namespace": false,
"no-misused-new": false,
"no-namespace": false,
"no-object-literal-type-assertion": false,
"no-padding": false,
"no-redundant-jsdoc": false,
"no-redundant-jsdoc-2": false,
"no-redundant-undefined": false,
"no-reference-import": false,
"no-relative-import-in-test": false,
"no-self-import": false,
"no-single-declare-module": false,
"no-string-throw": false,
"no-unnecessary-callback-wrapper": false,
"no-unnecessary-class": false,
"no-unnecessary-generics": false,
"no-unnecessary-qualifier": false,
"no-unnecessary-type-assertion": false,
"no-useless-files": false,
"no-var-keyword": false,
"no-var-requires": false,
"no-void-expression": false,
"no-trailing-whitespace": false,
"object-literal-key-quotes": false,
"object-literal-shorthand": false,
"one-line": false,
"one-variable-per-declaration": false,
"only-arrow-functions": false,
"prefer-conditional-expression": false,
"prefer-const": false,
"prefer-declare-function": false,
"prefer-for-of": false,
"prefer-method-signature": false,
"prefer-template": false,
"radix": false,
"semicolon": false,
"space-before-function-paren": false,
"space-within-parens": false,
"strict-export-declare-modifiers": false,
"trim-file": false,
"triple-equals": false,
"typedef-whitespace": false,
"unified-signatures": false,
"void-return": false,
"whitespace": false
}
"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
}
}