-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
69 lines (60 loc) · 1.95 KB
/
index.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import * as rm from "typed-rest-client/RestClient";
import {sign} from "jsonwebtoken";
import {IRequestOptions} from "typed-rest-client/Interfaces";
/**
* An invalid API key was passed into {@link Session}.
*/
export class InvalidApiKeyError extends Error {}
/**
* A caller attempted to call {@link Session#generateSignature|generateSignature} when the
* {@link Session} doesn't have a JWT key set.
*/
export class NoJwtKeyError extends Error {}
/**
* A session that can be used to interact with the Hyper Solutions API services.
*/
export class Session {
/**
* The API key.
*/
public readonly apiKey: string;
/**
* The optional JWT key.
*/
public readonly jwtKey?: string;
readonly client: rm.RestClient;
/**
* Creates a new session.
* @param apiKey Your Hyper Solutions API key
* @param jwtKey Your JWT key. This is only required if you wish to utilize request signing to prevent replay attacks.
* @param requestOptions Request options for the internal HTTP client
*/
public constructor(apiKey: string, jwtKey?: string, requestOptions?: IRequestOptions) {
if (apiKey.length == 0) {
throw new InvalidApiKeyError();
}
this.apiKey = apiKey;
this.jwtKey = jwtKey;
this.client = new rm.RestClient("Hyper Solutions TypeScript SDK", undefined, undefined, requestOptions);
}
/**
* Generates the value used for the `X-Signature` API request header.
*
* The signature is automatically added if `jwtKey` is set.
*/
public generateSignature(): string {
if (this.jwtKey == undefined || this.jwtKey.length == 0) {
throw new NoJwtKeyError();
}
return sign(
{
"key": this.apiKey,
"exp": Math.floor(Date.now() / 1000) + 60 // 60 seconds
},
this.jwtKey,
{
algorithm: "HS256"
}
);
}
}