improve pubnub typings (#23085)

This commit is contained in:
Vito
2018-01-25 09:13:21 -08:00
committed by Andy
parent 21cd9a3ab3
commit 4ed51b52ab
2 changed files with 205 additions and 16 deletions
+166 -14
View File
@@ -1,12 +1,22 @@
// Type definitions for pubnub 4.0
// Project: https://github.com/pubnub/javascript
// Definitions by: bitbankinc <https://github.com/bitbankinc>, rollymaduk <https://github.com/rollymaduk>
// Definitions by: bitbankinc <https://github.com/bitbankinc>, rollymaduk <https://github.com/rollymaduk>, vitosamson <https://github.com/vitosamson>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// @see https://www.pubnub.com/docs/web-javascript/api-reference-configuration
declare class Pubnub {
constructor(config: Pubnub.PubnubConfig);
static CATEGORIES: Pubnub.Categories;
static OPERATIONS: Pubnub.Operations;
static generateUUID(): string;
setUUID(uuid: string): void;
getUUID(): string;
setAuthKey(authKey: string): void;
setFilterExpression(filterExpression: string): void;
@@ -15,7 +25,7 @@ declare class Pubnub {
publish(
params: Pubnub.PublishParameters,
callback: (status: any, response: Pubnub.PublishResponse) => void
callback: (status: Pubnub.PublishStatus, response: Pubnub.PublishResponse) => void
): void;
publish(
@@ -24,7 +34,7 @@ declare class Pubnub {
fire(
params: Pubnub.FireParameters,
callback: (status: any, response: Pubnub.PublishResponse) => void
callback: (status: Pubnub.PublishStatus, response: Pubnub.PublishResponse) => void
): void;
fire(
@@ -45,7 +55,7 @@ declare class Pubnub {
hereNow(
params: Pubnub.HereNowParameters,
callback: (status: any, response: Pubnub.HereNowResponse) => void
callback: (status: Pubnub.HereNowStatus, response: Pubnub.HereNowResponse) => void
): void;
hereNow(
@@ -53,9 +63,31 @@ declare class Pubnub {
): Promise<Pubnub.HereNowResponse>;
whereNow(
params: {uuid: string},
params: Pubnub.WhereNowParameters,
callback: (status: Pubnub.WhereNowStatus, response: Pubnub.WhereNowResponse) => void
): void;
whereNow(
params: Pubnub.WhereNowParameters
): Promise<Pubnub.WhereNowResponse>;
getState(
params: Pubnub.GetStateParameters,
callback: (status: Pubnub.GetStateStatus, state: Pubnub.GetStateResponse) => void
): void;
getState(
params: Pubnub.GetStateParameters
): Promise<Pubnub.GetStateResponse>;
setState(
params: Pubnub.SetStateParameters,
callback: (status: Pubnub.SetStateStatus, state: Pubnub.SetStateResponse) => void
): void;
setState(
params: Pubnub.SetStateParameters
): Promise<Pubnub.SetStateResponse>;
}
declare namespace Pubnub {
@@ -83,15 +115,30 @@ declare namespace Pubnub {
secretKey?: string;
}
interface PubnubData {
actualChannel: string;
interface MessageEvent {
channel: string;
subscription: string;
timetoken: string;
message: any;
publisher: string;
/**
* @deprecated
*/
actualChannel: string;
/**
* @deprecated
*/
subscribedChannel: string;
}
// PubnubData was renamed to MessageEvent, keep old name for backwards compatibility
type PubnubData = MessageEvent;
interface StatusEvent {
category: string;
operation: string;
category: string; // see Pubnub.Categories
operation: string; // see Pubnub.Operations
affectedChannels: string[];
subscribedChannels: string[];
affectedChannelGroups: string[];
@@ -99,6 +146,26 @@ declare namespace Pubnub {
currentTimetoken: number | string;
}
interface PresenceEvent {
action: 'join' | 'leave' | 'state-change' | 'timeout';
channel: string;
occupancy: number;
state?: any;
subscription: string;
timestamp: number;
timetoken: string;
uuid: string;
/**
* @deprecated
*/
actualChannel: string;
/**
* @deprecated
*/
subscribedChannel: string;
}
// publish
interface PublishParameters {
message: any;
@@ -113,6 +180,13 @@ declare namespace Pubnub {
timetoken: number;
}
interface PublishStatus {
operation: string; // see Pubnub.Operations
category: string; // see Pubnub.Categories;
error: boolean;
errorData: Error;
}
// fire
interface FireParameters {
message: any;
@@ -137,9 +211,9 @@ declare namespace Pubnub {
// addListener
interface ListenerParameters {
status?: (statusEvent: StatusEvent) => void;
message?: (data: PubnubData) => void;
presence?: (presenceEvent: any) => void;
status?(statusEvent: StatusEvent): void;
message?(messageEvent: MessageEvent): void;
presence?(presenceEvent: PresenceEvent): void;
}
// hereNow
@@ -153,10 +227,29 @@ declare namespace Pubnub {
interface HereNowResponse {
totalChannels: number;
totalOccupancy: number;
channels: {[channel: string]: any};
channels: {
[channel: string]: {
name: string;
occupancy: number;
occupants: Array<{
uuid: string;
state?: any;
}>;
};
};
}
interface HereNowStatus {
error: boolean;
operation: string; // see Pubnub.Operations;
statusCode: number;
}
// whereNow
interface WhereNowParameters {
uuid?: string;
}
interface WhereNowStatus {
error: boolean;
operation: string;
@@ -174,12 +267,71 @@ declare namespace Pubnub {
state?: any;
}
interface SetStateStatus {
error: boolean;
operation: string;
statusCode: number;
}
interface SetStateResponse {
state: any;
}
// getState
interface GetStateParameters {
uuid?: string;
channels?: string[];
channelGroups?: string[];
}
interface GetStateStatus {
error: boolean;
operation: string;
statusCode: number;
}
interface GetStateResponse {
channels: {
[channel: string]: any;
};
}
interface Categories {
PNNetworkUpCategory: string;
PNNetworkDownCategory: string;
PNNetworkIssuesCategory: string;
PNTimeoutCategory: string;
PNBadRequestCategory: string;
PNAccessDeniedCategory: string;
PNUnknownCategory: string;
PNReconnectedCategory: string;
PNConnectedCategory: string;
PNRequestMessageCountExceededCategory: string;
}
interface Operations {
PNTimeOperation: string;
PNHistoryOperation: string;
PNDeleteMessagesOperation: string;
PNFetchMessagesOperation: string;
PNSubscribeOperation: string;
PNUnsubscribeOperation: string;
PNPublishOperation: string;
PNPushNotificationEnabledChannelsOperation: string;
PNRemoveAllPushNotificationsOperation: string;
PNWhereNowOperation: string;
PNSetStateOperation: string;
PNHereNowOperation: string;
PNGetStateOperation: string;
PNHeartbeatOperation: string;
PNChannelGroupsOperation: string;
PNRemoveGroupOperation: string;
PNChannelsForGroupOperation: string;
PNAddChannelsToGroupOperation: string;
PNRemoveChannelsFromGroupOperation: string;
PNAccessManagerGrant: string;
PNAccessManagerAudit: string;
}
}
export default Pubnub;
export = Pubnub;
+39 -2
View File
@@ -1,4 +1,4 @@
import Pubnub from 'pubnub';
import * as Pubnub from 'pubnub';
const console = {
log: (params: any) => {}
@@ -36,8 +36,10 @@ pubnub.subscribe({channels: ['channel-1']});
pubnub.addListener({
status: statusEvent => {
if (statusEvent.category === "PNConnectedCategory") {
if (statusEvent.category === Pubnub.CATEGORIES.PNConnectedCategory) {
console.log(statusEvent.category);
} else if (statusEvent.operation === Pubnub.OPERATIONS.PNAccessManagerAudit) {
console.log(statusEvent.operation);
}
},
message: message => {
@@ -49,3 +51,38 @@ pubnub.addListener({
});
pubnub.unsubscribe({channels: ['channel-1']});
pubnub.unsubscribeAll();
pubnub.setUUID(Pubnub.generateUUID());
const uuid = pubnub.getUUID();
pubnub.whereNow({ uuid: '' }, (status, res) => {
if (!status.error) {
console.log(res.channels[0]);
}
});
pubnub.whereNow({ uuid: '' }).then(res => {
console.log(res.channels[1]);
});
pubnub.getState({ uuid: '' }, (status, res) => {
if (!status.error) {
console.log(res.channels[0]);
}
});
pubnub.getState({ uuid: '' }).then(res => {
console.log(res.channels[1]);
});
pubnub.setState({ channels: [] }, (status, res) => {
if (!status.error) {
console.log(res.state);
}
});
pubnub.setState({ channels: [] }).then(res => {
console.log(res.state);
});