mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-12-04 20:21:46 +00:00
Adding a new type definition for nes (#10129)
* adding nes type definitions * Update nes.d.ts * Payload is also optional when making request * adding client interfaces types to Client from 'nes' to 'nes/client' * until TypeScript 2.0 all types are nullable ...
This commit is contained in:
committed by
Mohamed Hegazy
parent
1a1c465892
commit
1df8038e7e
@@ -0,0 +1,65 @@
|
||||
/// <reference path="nes.d.ts" />
|
||||
|
||||
import Hapi = require('hapi');
|
||||
import Nes = require('nes');
|
||||
|
||||
let server: Hapi.Server = new Hapi.Server();
|
||||
server.connection({port: 8080});
|
||||
|
||||
server.register(Nes, (regErr: any) => {
|
||||
if(regErr) {
|
||||
console.log('register err');
|
||||
console.log(regErr);
|
||||
} else {
|
||||
let wsServer: Nes.Server = server as Nes.Server;
|
||||
wsServer.subscription('/item/{id}');
|
||||
wsServer.route( {
|
||||
method: 'GET',
|
||||
path: '/test',
|
||||
config: {
|
||||
handler: (request: Hapi.Request, reply: Hapi.IReply) => {
|
||||
reply({test: 'passes'});
|
||||
}
|
||||
}
|
||||
});
|
||||
wsServer.start((err: any) => {
|
||||
if(err) {
|
||||
console.log('start err');
|
||||
console.log(err);
|
||||
} else {
|
||||
setTimeout(() => {
|
||||
wsServer.publish('/item/5', { id: 5, status: 'complete' });
|
||||
wsServer.publish('/item/6', { id: 6, status: 'initial' });
|
||||
}, 100);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
let wsClient: Nes.Client = new Nes.Client('ws://localhost:8080');
|
||||
wsClient.connect((err: any) => {
|
||||
if(err) {
|
||||
console.log('start err');
|
||||
console.log(err);
|
||||
} else {
|
||||
wsClient.subscribe('/item/5', (update) => {
|
||||
wsClient.request('/test', (reqErr: any, payload: any, statusCode: number) => {
|
||||
if(reqErr) {
|
||||
console.log('request err');
|
||||
console.log(reqErr);
|
||||
} else {
|
||||
console.log(update);
|
||||
console.log(payload);
|
||||
if(payload.test === 'passes') {
|
||||
process.exit(0);
|
||||
}
|
||||
}
|
||||
})
|
||||
}, (subErr) => {
|
||||
if(subErr) {
|
||||
console.log('subscribe err');
|
||||
console.log(subErr);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
Vendored
+130
@@ -0,0 +1,130 @@
|
||||
// Type definitions for nes 6.2.1
|
||||
// Project: https://github.com/hapijs/nes
|
||||
// Definitions by: Ivo Stratev <https://github.com/NoHomey>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
|
||||
/// <reference path="../hapi/hapi.d.ts" />
|
||||
|
||||
declare module 'nes' {
|
||||
import Hapi = require('hapi');
|
||||
|
||||
export interface SocketAuthObject {
|
||||
isAuthenticated: boolean;
|
||||
credentials: any;
|
||||
artifacts: any;
|
||||
}
|
||||
|
||||
export class Socket {
|
||||
id: any;
|
||||
app: Object;
|
||||
auth: SocketAuthObject;
|
||||
disconect(callback?: () => void): void;
|
||||
send(message: any, callback?: (err?: any) => void): void;
|
||||
publish(path: string, message: any, callback?: (err?: any) => void): void;
|
||||
revoke(path: string, message: any, callback?: (err?: any) => void): void;
|
||||
}
|
||||
|
||||
export interface ServerBroadcastOptions {
|
||||
user: any
|
||||
}
|
||||
|
||||
export interface ServerSubscriptionOptionsFilterOptions {
|
||||
socket: Socket;
|
||||
credentials?: any;
|
||||
params?: any;
|
||||
}
|
||||
|
||||
export interface ServerSubscriptionOptionsAuthOptions {
|
||||
mode?: 'required' | 'optional';
|
||||
scope?: string | string[];
|
||||
entity?: 'user' | 'app' | 'any';
|
||||
index?: boolean;
|
||||
}
|
||||
|
||||
export type ServerOnSubscribeWithParams = (socket: Socket, path: string, params: any, next: (err?: any) => void) => void;
|
||||
export type ServerOnSubscribeWithoutParams = (socket: Socket, path: string, next: (err?: any) => void) => void;
|
||||
export type ServerOnSubscribe = ServerOnSubscribeWithParams | ServerOnSubscribeWithoutParams;
|
||||
|
||||
export type ServerOnUnSubscribeWithParams = (socket: Socket, path: string, params: any, next: () => void) => void;
|
||||
export type ServerOnUnSubscribeWithoutParams = (socket: Socket, path: string, next: () => void) => void;
|
||||
export type ServerOnUnSubscribe = ServerOnUnSubscribeWithParams | ServerOnUnSubscribeWithoutParams;
|
||||
|
||||
export interface ServerSubscriptionOptions {
|
||||
filter?: (path: string, message: any, options: ServerSubscriptionOptionsFilterOptions, next: (isMatch: boolean, override: any) => void) => void;
|
||||
auth?: boolean | ServerSubscriptionOptionsAuthOptions;
|
||||
onSubscribe?: ServerOnSubscribe;
|
||||
onUnsubscribe?: ServerOnUnSubscribe;
|
||||
}
|
||||
|
||||
export interface ServerPublishOptions {
|
||||
internal?: any;
|
||||
user?: any;
|
||||
}
|
||||
|
||||
export interface ServerEachSokcetOptions {
|
||||
subscription?: string;
|
||||
user?: any;
|
||||
}
|
||||
|
||||
export class Server extends Hapi.Server {
|
||||
broadcast(message: any, options?: ServerBroadcastOptions): void;
|
||||
subscription(path: string, options?: ServerSubscriptionOptions): void;
|
||||
publish(path: string, message: any, options?: ServerPublishOptions): void;
|
||||
eachSocket(each: (socket: Socket) => void, options?: ServerEachSokcetOptions): void;
|
||||
}
|
||||
|
||||
export class Request extends Hapi.Request {
|
||||
socket: Socket;
|
||||
}
|
||||
|
||||
export interface ClientOptions {
|
||||
ws?: any;
|
||||
timeout?: number | boolean;
|
||||
}
|
||||
|
||||
export interface ClientConnnectOptions {
|
||||
auth?: any;
|
||||
delay?: number;
|
||||
maxDelay?: number;
|
||||
retries?: number;
|
||||
timeout?: number;
|
||||
}
|
||||
|
||||
export interface ClientRequestOptions {
|
||||
path: string;
|
||||
method?: string;
|
||||
headers?: Object;
|
||||
payload?: any;
|
||||
}
|
||||
|
||||
export interface ClientSubscribeFlags {
|
||||
revoked?: boolean;
|
||||
}
|
||||
|
||||
export class Client {
|
||||
constructor(url: string, options?: ClientOptions);
|
||||
onError: (err: any) => void;
|
||||
onConnect: () => void;
|
||||
onDisconnect: () => void;
|
||||
onUpdate: (message: any) => void;
|
||||
connect(options: ClientConnnectOptions, callback: (err?: any) => void): void;
|
||||
connect(callback: (err?: any) => void): void;
|
||||
disconnect(): void;
|
||||
id: any;
|
||||
request(options: string | ClientRequestOptions, callback: (err: any, payload: any, statusCode?: number, headers?: Object) => void): void;
|
||||
message(message: any, callback: (err: any, message: any) => void): void;
|
||||
subscribe(path: string, handler: (message: any, flags: ClientSubscribeFlags) => void, callback: (err?: any) => void): void;
|
||||
unsubscribe(path: string, handler: (message: any, flags: ClientSubscribeFlags) => void, callback: (err?: any) => void): void;
|
||||
subscriptions(): string[];
|
||||
overrideReconnectionAuth(auth: any): void;
|
||||
}
|
||||
}
|
||||
|
||||
declare module 'nes/client' {
|
||||
export {
|
||||
Client,
|
||||
ClientConnnectOptions,
|
||||
ClientRequestOptions,
|
||||
ClientSubscribeFlags
|
||||
} from 'nes';
|
||||
}
|
||||
Reference in New Issue
Block a user