Add strophejs-plugin-roster type declarations (#34229)

* Add strophejs-plugin-roster type declarations

* Linting fixes

* Add typescript version, same as strophe.js
This commit is contained in:
Leonardo Donelli 2019-03-30 04:43:39 +01:00 committed by Ron Buckton
parent 61862404d4
commit 2d7eecfb00
4 changed files with 126 additions and 0 deletions

View File

@ -0,0 +1,58 @@
// Type definitions for strophejs-plugin-roster 1.1
// Project: https://github.com/strophe/strophejs-plugin-roster#readme
// Definitions by: LeartS <https://github.com/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;
}
}
}

View File

@ -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);

View File

@ -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"
]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }