-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathgenerate-disposable-token.ts
48 lines (41 loc) · 1.58 KB
/
generate-disposable-token.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import {BaseResponseSuccess, BaseResponseError} from './response-base';
import {GenerateDisposableTokenResponse} from './enums';
import {SdkError} from '../../errors';
import {encodeToBase64} from '../../internal/utils';
import {ExpiresAt} from '../../utils';
interface IResponse {
readonly type: GenerateDisposableTokenResponse;
}
export class Success extends BaseResponseSuccess implements IResponse {
readonly type: GenerateDisposableTokenResponse.Success =
GenerateDisposableTokenResponse.Success;
readonly authToken: string;
readonly endpoint: string;
readonly expiresAt: ExpiresAt;
constructor(apiKey: string, endpoint: string, expiresAt: ExpiresAt) {
super();
this.authToken = encodeToBase64(
JSON.stringify({endpoint: endpoint, api_key: apiKey})
);
this.endpoint = endpoint;
this.expiresAt = expiresAt;
}
}
/**
* Indicates that an error occurred during the generate disposable token request.
*
* This response object includes the following fields that you can use to determine
* how you would like to handle the error:
*
* - `errorCode()` - a unique Momento error code indicating the type of error that occurred.
* - `message()` - a human-readable description of the error
* - `innerException()` - the original error that caused the failure; can be re-thrown.
*/
export class Error extends BaseResponseError implements IResponse {
constructor(innerException: SdkError) {
super(innerException);
}
readonly type: GenerateDisposableTokenResponse.Error =
GenerateDisposableTokenResponse.Error;
}
export type Response = Success | Error;