Updated typings

Removed I from interfaces.
Renamed interfaces to be more clear.
Added TODO: Create typings on protodef to define here.
Added supportedVersions static strings.
Rearranged order of exports and properties.
Changed unused returns of functions from any to void (More strict).
Put entire typings inside module declaration.

Now works in strict mode.
Suggestion: Add typings to protodef so createSerializer and createDeserializer have valid types.
This commit is contained in:
Shayne Hartford 2018-10-03 07:37:19 -04:00 committed by GitHub
parent eac4f3d8ac
commit 566b766f84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

190
src/index.d.ts vendored
View File

@ -1,113 +1,114 @@
import { EventEmitter } from 'events';
import { Socket } from 'net' import { Socket } from 'net'
import * as Stream from 'stream' import * as Stream from 'stream'
import EventEmitter = NodeJS.EventEmitter
declare enum EnumStates { declare module 'minecraft-protocol' {
export class Client extends EventEmitter {
constructor(isServer: boolean, version: string, customPackets?: any)
isServer: boolean
latency: number
profile: any
session: any
socket: Socket
state: States
username: string
uuid: string
end(reason: string): void
registerChannel(name: string, typeDefinition: any, custom?: boolean): void
unregisterChannel(name: string): void
write(name: string, params: any): void
writeChannel(channel: any, params: any): void
on(event: 'packet', handler: (data: any, packetMeta: PacketMeta) => void): this
on(event: 'raw', handler: (data: any, packetMeta: PacketMeta) => void): this
on(event: 'session', handler: (session: any) => void): this
on(event: 'state', handler: (newState: States, oldState: States) => void): this
}
export interface ClientOptions {
accessToken?: string
checkTimeoutInterval?: number
clientToken?: string
customPackets?: any
hideErrors?: boolean
host?: string
keepAlive?: boolean
password?: string
port?: number
username: string
version?: string
}
export class Server extends EventEmitter {
constructor(version: string, customPackets?: any)
clients: ClientsMap
favicon: string
maxPlayers: number
motd: string
onlineModeExceptions: object
playerCount: number
on(event: 'connection', handler: (client: Client) => void): this
on(event: 'login', handler: (client: Client) => void): this
}
export interface ServerOptions {
'online-mode'?: boolean
checkTimeoutInterval?: number
customPackets?: any
hideErrors?: boolean
host?: string
keepAlive?: boolean
kickTimeout?: number
maxPlayers?: number
motd?: string
port?: number
stream?: Stream
version?: string
beforePing?: (response: any, client: Client, callback?: (result: any) => any) => any
connect?: (client: Client) => void
errorHandler?: (client: Client, error: Error) => void
}
export interface SerializerOptions {
customPackets: any
isServer?: boolean
state?: States
version: string
}
enum States {
HANDSHAKING = 'handshaking', HANDSHAKING = 'handshaking',
STATUS = 'status',
LOGIN = 'login', LOGIN = 'login',
PLAY = 'play', PLAY = 'play',
STATUS = 'status',
} }
export interface IPacketMeta { export interface PacketMeta {
name: string name: string
state: EnumStates state: States
} }
export declare class Client extends EventEmitter { interface ClientsMap {
constructor(isServer: boolean, version: string, customPackets?: any)
write(name: string, params: any)
end(reason: string)
state: EnumStates
isServer: boolean
socket: Socket
uuid: string
username: string
session: any
profile: any
latency: number
on(event: 'packet', handler: (data: any, packetMeta: IPacketMeta) => any)
on(event: 'raw', handler: (data: any, packetMeta: IPacketMeta) => any)
on(event: 'state', handler: (newState: EnumStates, oldState: EnumStates) => any)
on(event: 'session', handler: (session: any) => any)
writeChannel(channel: any, params: any)
registerChannel(name: string, typeDefinition: any, custom?: boolean)
unregisterChannel(name: string)
}
interface IClientsMap {
[key: string]: Client [key: string]: Client
} }
export declare class Server extends EventEmitter { export interface PingOptions {
constructor(version: string, customPackets?: any)
onlineModeExceptions: object
clients: IClientsMap
playerCount: number
maxPlayers: number
motd: string
favicon: string
on(event: 'connection', handler: (client: Client) => any)
on(event: 'login', handler: (client: Client) => any)
}
export interface ICreateServerOptions {
host?: string host?: string
port?: number
kickTimeout?: number
checkTimeoutInterval?: number
'online-mode'?: boolean
motd?: string
maxPlayers?: number
keepAlive?: boolean
version?: string
customPackets?: any
stream?: Stream
beforePing?: (response: any, client: Client, callback?: (result: any) => any) => any
errorHandler?: (client: Client, error: Error) => any
connect?: (client: Client) => any
hideErrors?: boolean
}
export interface ICreateClientOptions {
username: string
port?: number
password?: string
host?: string
clientToken?: string
accessToken?: string
keepAlive?: boolean
checkTimeoutInterval?: number
version?: string
customPackets?: any
hideErrors?: boolean
}
export interface ICreateSerializerOptions {
state?: EnumStates
isServer?: boolean
version: string
customPackets: any
}
export interface IPingOptions {
host?: string
port?: number
version?: string
majorVersion?: string majorVersion?: string
port?: number
protocolVersion?: string protocolVersion?: string
version?: string
} }
export interface IPingOldResult { export interface OldPingResult {
maxPlayers: number,
motd: string
playerCount: number
prefix: string prefix: string
protocol: string protocol: string
version: string version: string
motd: string
playerCount: number
maxPlayers: number,
} }
export interface IPingNewResult { export interface NewPingResult {
description: string description: string
players: { players: {
max: number max: number
@ -125,14 +126,15 @@ export interface IPingNewResult {
latency: number latency: number
} }
export declare function createServer(options: ICreateServerOptions): Server export const state: States
export declare function createClient(options: ICreateClientOptions): Client export const supportedVersions: ['1.7', '1.8', '1.9', '1.10', '1.11.2', '1.12.2', '1.13.1']
export const state: EnumStates export function createServer(options: ServerOptions): Server
export function createClient(options: ClientOptions): Client
export declare function createSerializer({state, isServer, version, customPackets}: ICreateSerializerOptions) // TODO: Create typings on protodef to define here
export declare function createDeserializer({state, isServer, version, customPackets}: ICreateSerializerOptions) export function createSerializer({ state, isServer, version, customPackets }: SerializerOptions): any
export function createDeserializer({ state, isServer, version, customPackets }: SerializerOptions): any
export declare function ping(options: IPingOptions, callback: (err: Error, result: IPingOldResult | IPingNewResult) => any); export function ping(options: PingOptions, callback: (error: Error, result: OldPingResult | NewPingResult) => void): void
}
export const supportedVersions: string[]