diff --git a/types/twilio-common/index.d.ts b/types/twilio-common/index.d.ts new file mode 100644 index 0000000000..2ad8b73ea4 --- /dev/null +++ b/types/twilio-common/index.d.ts @@ -0,0 +1,24 @@ +// Type definitions for twilio-common 0.1 +// Project: https://github.com/twilio/twilio-common.js +// Definitions by: William Lohan +// 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; + + on(type: 'error', listener: (error: Error) => void): this; + + on(type: 'tokenExpired' | 'tokenWillExpire' | 'tokenUpdated', listener: (manager: AccessManager) => void): this; +} diff --git a/types/twilio-common/tsconfig.json b/types/twilio-common/tsconfig.json new file mode 100644 index 0000000000..3653f40f71 --- /dev/null +++ b/types/twilio-common/tsconfig.json @@ -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" + ] +} diff --git a/types/twilio-common/tslint.json b/types/twilio-common/tslint.json new file mode 100644 index 0000000000..c2e987daf6 --- /dev/null +++ b/types/twilio-common/tslint.json @@ -0,0 +1,6 @@ +{ + "extends": "dtslint/dt.json", + "rules": { + "only-arrow-functions": false + } +} diff --git a/types/twilio-common/twilio-common-tests.ts b/types/twilio-common/twilio-common-tests.ts new file mode 100644 index 0000000000..688be12c9a --- /dev/null +++ b/types/twilio-common/twilio-common-tests.ts @@ -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); +});