add twilio-common

This commit is contained in:
William Lohan
2018-11-06 16:34:11 -08:00
parent a4b76fc66f
commit 9214851647
4 changed files with 75 additions and 0 deletions
+24
View File
@@ -0,0 +1,24 @@
// Type definitions for twilio-common 0.1
// Project: https://github.com/twilio/twilio-common.js
// Definitions by: William Lohan <https://github.com/gatimus>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
import { EventEmitter } from 'events';
export class AccessManager extends EventEmitter {
readonly expires?: Date;
readonly identity?: string;
readonly isExpired: boolean;
readonly token: string;
constructor(initialToken: string);
updateToken(newToken: string): Promise<AccessManager>;
on(type: 'error', listener: (error: Error) => void): this;
on(type: 'tokenExpired' | 'tokenWillExpire' | 'tokenUpdated', listener: (manager: AccessManager) => void): this;
}
+23
View File
@@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"twilio-common-tests.ts"
]
}
+6
View File
@@ -0,0 +1,6 @@
{
"extends": "dtslint/dt.json",
"rules": {
"only-arrow-functions": false
}
}
@@ -0,0 +1,22 @@
// https://www.twilio.com/docs/chat/access-token-lifecycle#javascript
import { AccessManager } from 'twilio-common';
function generateToken(): string {
return 'SOME_TOKEN';
}
const token = generateToken();
const accessManager = new AccessManager(token);
const chatClient = { updateToken: (token: string) => {} }; // mock for test
accessManager.on('tokenUpdated', function(am) {
// get new token from AccessManager and pass it to the library instance
chatClient.updateToken(am.token);
});
accessManager.on('tokenExpired', function() {
// generate new token here and set it to the accessManager
const updatedToken = generateToken();
accessManager.updateToken(updatedToken);
});