diff --git a/types/pubnub/index.d.ts b/types/pubnub/index.d.ts index 1e078de28c..355e700b7b 100644 --- a/types/pubnub/index.d.ts +++ b/types/pubnub/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for pubnub 4.26 +// Type definitions for pubnub 4.27 // Project: https://github.com/pubnub/javascript // Definitions by: bitbankinc , // rollymaduk , @@ -22,6 +22,8 @@ declare class Pubnub { channelGroups: Pubnub.ChannelGroups; + push: Pubnub.Push; + setUUID(uuid: string): void; getUUID(): string; @@ -58,6 +60,26 @@ declare class Pubnub { callback: (status: Pubnub.PubnubStatus, response: Pubnub.HistoryResponse) => void, ): void; + history(params: Pubnub.HistoryParameters): Promise; + + fetchMessages( + params: Pubnub.FetchMessagesParameters, + callback: (status: Pubnub.PubnubStatus, response: Pubnub.FetchMessagesResponse) => void, + ): void; + + fetchMessages(params: Pubnub.FetchMessagesParameters): Promise; + + deleteMessages(params: Pubnub.DeleteMessagesParameters, callback: (status: Pubnub.PubnubStatus) => void): void; + + deleteMessages(params: Pubnub.DeleteMessagesParameters): Promise; + + messageCounts( + params: Pubnub.MessageCountsParameters, + callback: (status: Pubnub.PubnubStatus, response: Pubnub.MessageCountsResponse) => void, + ): void; + + messageCounts(params: Pubnub.MessageCountsParameters): Promise; + subscribe(params: Pubnub.SubscribeParameters): void; unsubscribe(params: Pubnub.UnsubscribeParameters): void; @@ -228,6 +250,27 @@ declare class Pubnub { removeMembers(params: Pubnub.RemoveMembersParameters): Promise; + addMessageAction( + params: Pubnub.AddMessageActionParameters, + callback: (status: Pubnub.PubnubStatus, response: { data: Pubnub.MessageAction }) => void, + ): void; + + addMessageAction(params: Pubnub.AddMessageActionParameters): Promise<{ data: Pubnub.MessageAction }>; + + removeMessageAction( + params: Pubnub.RemoveMessageActionParameters, + callback: (status: Pubnub.PubnubStatus, response: { data: {} }) => void, + ): void; + + removeMessageAction(params: Pubnub.RemoveMessageActionParameters): Promise<{ data: {} }>; + + getMessageActions( + params: Pubnub.GetMessageActionsParameters, + callback: (status: Pubnub.PubnubStatus, response: Pubnub.GetMessageActionsResponse) => void, + ): void; + + getMessageActions(params: Pubnub.GetMessageActionsParameters): Promise; + encrypt(data: string, customCipherKey?: string, options?: Pubnub.CryptoParameters): any; decrypt(data: string | object, customCipherKey?: string, options?: Pubnub.CryptoParameters): any; @@ -395,21 +438,105 @@ declare namespace Pubnub { stringifiedTimeToken?: boolean; includeTimetoken?: boolean; reverse?: boolean; - start?: number; // timetoken - end?: number; // timetoken + start?: string | number; // timetoken + end?: string | number; // timetoken + includeMeta?: boolean; } interface HistoryMessage { entry: any; timetoken?: string | number; + meta?: object; } interface HistoryResponse { - endTimeToken?: number; - startTimeToken?: number; + endTimeToken?: string | number; + startTimeToken?: string | number; messages: HistoryMessage[]; } + interface FetchMessagesParameters { + channels: string[]; + count?: number; + stringifiedTimeToken?: boolean; + start?: string | number; // timetoken + end?: string | number; // timetoken + withMessageActions?: boolean; + includeMeta?: boolean; + includeMessageActions?: boolean; + } + + interface FetchMessagesResponse { + channels: { + [channel: string]: Array<{ + message: any; + timetoken: string | number; + meta?: object; + actions: { + [type: string]: { + [value: string]: Array<{ + uuid: string; + actionTimetoken: string | number; // timetoken + }>; + }; + }; + }>; + }; + } + + interface DeleteMessagesParameters { + channel: string; + start?: string | number; // timetoken + end?: string | number; // timetoken + } + + interface MessageCountsParameters { + channels: string[]; + channelTimetokens: string[] | number[]; + } + + interface MessageCountsResponse { + channels: { + [channel: string]: number; + }; + } + + interface Push { + addChannels(params: PushChannelParameters, callback: (status: PubnubStatus) => void): void; + + addChannels(params: PushChannelParameters): Promise<{}>; + + listChannels( + params: PushDeviceParameters, + callback: (status: PubnubStatus, response: PushListChannelsResponse) => void, + ): void; + + listChannels(params: PushDeviceParameters): Promise; + + removeChannels(params: PushChannelParameters, callback: (status: PubnubStatus) => void): void; + + removeChannels(params: PushChannelParameters): Promise<{}>; + + deleteDevice(params: PushDeviceParameters, callback: (status: PubnubStatus) => void): void; + + deleteDevice(params: PushDeviceParameters): Promise<{}>; + } + + interface PushChannelParameters { + channels: string[]; + device: string; + pushGateway: string; + } + + interface PushDeviceParameters { + device: string; + pushGateway: string; + } + + interface PushListChannelsResponse { + channels: string[]; + } + interface PubnubStatus { error: boolean; category?: string; // see Pubnub.Categories @@ -734,6 +861,42 @@ declare namespace Pubnub { users: string[]; } + interface AddMessageActionParameters { + channel: string; + messageTimetoken: string; + action: { + type: string; + value: string; + }; + } + + interface MessageAction { + type: string; + value: string; + uuid: string; + actionTimetoken: string; + messageTimetoken: string; + } + + interface RemoveMessageActionParameters { + channel: string; + messageTimetoken: string; + actionTimetoken: string; + } + + interface GetMessageActionsParameters { + channel: string; + start?: string; + end?: string; + limit?: number; + } + + interface GetMessageActionsResponse { + data: MessageAction[]; + start?: string; + end?: string; + } + // encrypt & decrypt interface CryptoParameters { encryptKey?: boolean; @@ -765,6 +928,7 @@ declare namespace Pubnub { PNHistoryOperation: string; PNDeleteMessagesOperation: string; PNFetchMessagesOperation: string; + PNMessageCountsOperation: string; PNSubscribeOperation: string; PNUnsubscribeOperation: string; PNPublishOperation: string; @@ -793,6 +957,9 @@ declare namespace Pubnub { PNGetMembershipsOperation: string; PNGetMembersOperation: string; PNUpdateMembershipsOperation: string; + PNAddMessageActionOperation: string; + PNRemoveMessageActionOperation: string; + PNGetMessageActionsOperation: string; } } diff --git a/types/pubnub/pubnub-tests.ts b/types/pubnub/pubnub-tests.ts index 41fe694673..25886fb720 100644 --- a/types/pubnub/pubnub-tests.ts +++ b/types/pubnub/pubnub-tests.ts @@ -125,9 +125,141 @@ pubnub.grant(grantOptions).then(status => { console.log(status); }); -pubnub.history({ channel: 'channel-1', count: 2 }, (status, res) => { - console.log(status, res); -}); +pubnub.history({ channel: 'channel-1', count: 2 }, (status, res) => console.log(status, res)); + +pubnub.history({ channel: 'channel-1', count: 2 }).then(res => console.log(res)); + +pubnub.fetchMessages( + { + channels: ['my_channel'], + stringifiedTimeToken: true, + start: '15343325214676133', + end: '15343325004275466', + includeMeta: true, + includeMessageActions: true, + }, + (status, { channels }) => + Object.keys(channels).forEach(channel => + channels[channel].forEach(({ message, timetoken, meta, actions = {} }) => { + console.log({ message, timetoken, meta }); + Object.keys(actions).forEach(type => + Object.keys(actions[type]).forEach(value => + actions[type][value].forEach(({ uuid, actionTimetoken }) => + console.log({ uuid, actionTimetoken }), + ), + ), + ); + }), + ), +); + +pubnub + .fetchMessages({ + channels: ['my_channel'], + stringifiedTimeToken: true, + start: '15343325214676133', + end: '15343325004275466', + }) + .then(res => console.log(res)); + +pubnub.deleteMessages( + { + channel: 'ch1', + start: '15088506076921021', + end: '15088532035597390', + }, + status => console.log(status), +); + +pubnub + .deleteMessages({ + channel: 'ch1', + start: '15088506076921021', + end: '15088532035597390', + }) + .then(); + +pubnub.messageCounts( + { + channels: ['ch1'], + channelTimetokens: ['15518041524300251'], + }, + (status, { channels }) => { + Object.keys(channels).forEach(channel => { + console.log(channels[channel]); + }); + }, +); + +pubnub + .messageCounts({ + channels: ['ch1'], + channelTimetokens: ['15518041524300251'], + }) + .then(res => console.log(res)); + +pubnub.push.addChannels( + { + channels: ['a', 'b'], + device: 'niceDevice', + pushGateway: 'apns', + }, + status => console.log(status), +); + +pubnub.push + .addChannels({ + channels: ['a', 'b'], + device: 'niceDevice', + pushGateway: 'apns', + }) + .then(); + +pubnub.push.listChannels( + { + device: 'niceDevice', + pushGateway: 'apns', + }, + (status, { channels = [] }) => channels.forEach(channel => console.log(channel)), +); + +pubnub.push + .listChannels({ + device: 'niceDevice', + pushGateway: 'apns', + }) + .then(res => console.log(res)); + +pubnub.push.removeChannels( + { + channels: ['a', 'b'], + device: 'niceDevice', + pushGateway: 'apns', // apns, gcm, mpns + }, + status => console.log(status), +); +pubnub.push + .removeChannels({ + channels: ['a', 'b'], + device: 'niceDevice', + pushGateway: 'apns', // apns, gcm, mpns + }) + .then(); + +pubnub.push.deleteDevice( + { + device: 'niceDevice', + pushGateway: 'apns', // apns, gcm, mpns + }, + status => console.log(status), +); + +pubnub.push + .deleteDevice({ + device: 'niceDevice', + pushGateway: 'apns', // apns, gcm, mpns + }) + .then(); const cryptoOptions = { encryptKey: true, @@ -501,3 +633,67 @@ pubnub users: ['user-1', 'user-2'], }) .then(res => console.log(res)); + +pubnub.addMessageAction( + { + channel: 'channel1', + messageTimetoken: '15610547826970040', + action: { + type: 'reaction', + value: 'smiley_face', + }, + }, + (status, { data: { type, value, uuid, actionTimetoken, messageTimetoken } }) => + console.log({ type, value, uuid, actionTimetoken, messageTimetoken }), +); + +pubnub + .addMessageAction({ + channel: 'channel1', + messageTimetoken: '15610547826970040', + action: { + type: 'reaction', + value: 'smiley_face', + }, + }) + .then(res => console.log(res)); + +pubnub.removeMessageAction( + { + channel: 'channel1', + messageTimetoken: '15610547826970040', + actionTimetoken: '15610547826970040', + }, + (status, data) => console.log(status, data), +); + +pubnub + .removeMessageAction({ + channel: 'channel1', + messageTimetoken: '15610547826970040', + actionTimetoken: '15610547826970040', + }) + .then(res => console.log(res)); + +pubnub.getMessageActions( + { + channel: 'channel1', + start: '15610547826970040', + end: '15610547826970040', + limit: 100, + }, + (status, { data }) => { + data.forEach(({ type, value, uuid, actionTimetoken, messageTimetoken }, index) => { + console.log(index, { type, value, uuid, actionTimetoken, messageTimetoken }); + }); + }, +); + +pubnub + .getMessageActions({ + channel: 'channel1', + start: '15610547826970040', + end: '15610547826970040', + limit: 100, + }) + .then(res => console.log(res));