diff --git a/types/strophejs-plugin-roster/index.d.ts b/types/strophejs-plugin-roster/index.d.ts new file mode 100644 index 0000000000..d6188a5fc2 --- /dev/null +++ b/types/strophejs-plugin-roster/index.d.ts @@ -0,0 +1,58 @@ +// Type definitions for strophejs-plugin-roster 1.1 +// Project: https://github.com/strophe/strophejs-plugin-roster#readme +// Definitions by: LeartS +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.1 + +import { Strophe } from 'strophe.js'; + +export {}; + +export type IqCallback = (stanza: Element) => any; +export type IqID = string; +export type RosterSubscriptionState = 'none' | 'to' | 'from' | 'both' | 'remove'; +export type PresenceSubscriptionType = 'subscribe' | 'unsubscribe' | 'subscribed' | 'unsubscribed'; + +export interface RosterResource { + priority: string; + show: string; + status: string; +} + +export interface RosterItem { + name: string; + jid: string; + subscription: RosterSubscriptionState; + ask: string; + groups: string[]; + resources: {[resourceId: string]: RosterResource}; +} + +export type RosterUpdateCallback = +(items: RosterItem[], item: RosterItem, previousItem: RosterItem) => any; +export type PresenceRequestCallback = (from: string) => any; + +interface StropheRosterPlugin { + supportVersioning(): boolean; + get(userCallback: IqCallback, ver?: string, items?: string[]): IqID; + registerCallback(callback: RosterUpdateCallback): void; + registerRequestCallback(callback: PresenceRequestCallback): void; + findItem(jid: string): RosterItem | false; + removeItem(jid: string): boolean; + subscribe(jid: string, message?: string, nick?: string): void; + unsubscribe(jid: string, message?: string): void; + authorize(jid: string, message?: string): void; + unauthorize(jid: string, message?: string): void; + add(jid: string, name: string, groups: string[], call_back?: IqCallback): IqID; + update(jid: string, name?: string, groups?: string[], call_back?: IqCallback): IqID; + remove(jid: string, call_back?: IqCallback): void; +} + +/*~ Here, declare the same module as the one you imported above */ +declare module 'strophe.js' { + namespace Strophe { + interface Connection { + roster: StropheRosterPlugin; + } + } +} diff --git a/types/strophejs-plugin-roster/strophejs-plugin-roster-tests.ts b/types/strophejs-plugin-roster/strophejs-plugin-roster-tests.ts new file mode 100644 index 0000000000..10f005826b --- /dev/null +++ b/types/strophejs-plugin-roster/strophejs-plugin-roster-tests.ts @@ -0,0 +1,43 @@ +import { RosterItem } from 'strophejs-plugin-roster'; + +// Test that RosterItem interface is correctly exported +// Pretty common for users to extend it in their own custom types +interface AugmentedRosterItem extends RosterItem { + customField: string; +} + +function onSubscribeRequest(from: string) { + // Automatically accept and also send "inverse" subscribe request so that + // both users will be in each other rosters with subscription="both" + connection.roster.authorize(from); + connection.roster.subscribe(from); + connection.roster.unsubscribe(from); + connection.roster.unauthorize(from); + return true; +} + +function onRosterUpdate(items: RosterItem[], updatedItem?: RosterItem): void { + if (updatedItem && updatedItem.subscription === 'remove') { + // This is the case where we (or another of our resources) + // deleted a roster item using a roster set + // and this is the server's resulting roster push. + console.log(`Successfully removed ${updatedItem.jid} from roster`); + } +} + +const connection = new Strophe.Connection('someservice'); + +function onRosterAdd(stanza: Element) { + console.log(stanza.tagName); +} + +// Initial roster ask in order to become "interested resource" +connection.roster.get(() => console.log('Sent initial roster request')); +connection.roster.registerRequestCallback(onSubscribeRequest); +connection.roster.registerCallback(onRosterUpdate); + +connection.roster.add('node@domain/resource', 'Contact One', ['group1'], onRosterAdd); +connection.roster.update('node@domain/resource', undefined, ['group1', 'group2']); +const contactOne: RosterItem = (connection.roster.findItem('node@domain/resource') || undefined)!; +contactOne.groups.length === 2; +connection.roster.remove(contactOne.jid); diff --git a/types/strophejs-plugin-roster/tsconfig.json b/types/strophejs-plugin-roster/tsconfig.json new file mode 100644 index 0000000000..07a6d097a2 --- /dev/null +++ b/types/strophejs-plugin-roster/tsconfig.json @@ -0,0 +1,24 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6", + "dom" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "strophejs-plugin-roster-tests.ts" + ] +} diff --git a/types/strophejs-plugin-roster/tslint.json b/types/strophejs-plugin-roster/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/strophejs-plugin-roster/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }