mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-10-07 00:07:07 +00:00
add twilio-common
This commit is contained in:
Vendored
+24
@@ -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;
|
||||
}
|
||||
@@ -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"
|
||||
]
|
||||
}
|
||||
@@ -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);
|
||||
});
|
||||
Reference in New Issue
Block a user