-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkeys.ts
153 lines (147 loc) · 4.53 KB
/
keys.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { CreateKeyOptions, KeyResponse, Key, KeyResponseObj } from "./deps.ts";
export class Keys {
constructor(private _credentials: string, private _apiUrl: string) {}
private apiPath = "/v1/projects";
/**
* Retrieves all keys associated with the provided projectId
* @param projectId Unique identifier of the project containing API keys
*/
async list(projectId: string): Promise<KeyResponse> {
let response = null;
try {
const firstResponse = await fetch(
`https://${this._apiUrl}${this.apiPath}/${projectId}/keys`,
{
method: "GET",
headers: {
Authorization: `token ${this._credentials}`,
"Content-Type": "application/json",
"X-DG-Agent": window.dgAgent,
},
}
);
if (firstResponse.ok) {
response = await firstResponse.json();
const output = response.api_keys.map((apiKey: KeyResponseObj) => {
return {
...apiKey,
...apiKey.api_key,
};
});
return { api_keys: output };
} else {
throw `${firstResponse.status} ${firstResponse.statusText}`;
}
} catch (error) {
throw "DG: Cannot List Keys. " + error;
}
}
/**
* Retrieves a specific key associated with the provided projectId
* @param projectId Unique identifier of the project containing API keys
* @param keyId Unique identifier for the key to retrieve
*/
async get(projectId: string, keyId: string): Promise<Key> {
try {
const response = await fetch(
`https://${this._apiUrl}${this.apiPath}/${projectId}/keys/${keyId}`,
{
method: "GET",
headers: {
Authorization: `token ${this._credentials}`,
"Content-Type": "application/json",
"X-DG-Agent": window.dgAgent,
},
}
);
if (response.ok) {
return response.json();
} else {
throw `${response.status} ${response.statusText}`;
}
} catch (error) {
throw "DG: Cannot Get Key. " + error;
}
}
/**
* Creates an API key with the provided scopes
* @param projectId Unique identifier of the project to create an API key under
* @param comment Comment to describe the key
* @param scopes Permission scopes associated with the API key
* @param options Optional options used when creating API keys
*/
async create(
projectId: string,
comment: string,
scopes: Array<string>,
options?: CreateKeyOptions
): Promise<Key> {
/** Throw an error if the user provided both expirationDate and timeToLive */
if (
options &&
options.expirationDate !== undefined &&
options.timeToLive !== undefined
) {
throw new Error(
"Please provide expirationDate or timeToLive or neither. Providing both is not allowed."
);
}
try {
const response = await fetch(
`https://${this._apiUrl}${this.apiPath}/${projectId}/keys`,
{
method: "POST",
headers: {
Authorization: `token ${this._credentials}`,
"Content-Type": "application/json",
"X-DG-Agent": window.dgAgent,
},
body: JSON.stringify({
comment,
scopes,
expiration_date:
options && options.expirationDate
? options.expirationDate
: undefined,
time_to_live_in_seconds:
options && options.timeToLive ? options.timeToLive : undefined,
}),
}
);
if (response.ok) {
return response.json();
} else {
throw `${response.status} ${response.statusText}`;
}
} catch (error) {
throw "DG: Cannot Create Key. " + error;
}
}
/**
* Deletes an API key
* @param projectId Unique identifier of the project to create an API key under
* @param keyId Unique identifier for the key to delete
*/
async delete(projectId: string, keyId: string): Promise<void> {
try {
const response = await fetch(
`https://${this._apiUrl}${this.apiPath}/${projectId}/keys/${keyId}`,
{
method: "DELETE",
headers: {
Authorization: `token ${this._credentials}`,
"Content-Type": "application/json",
"X-DG-Agent": window.dgAgent,
},
}
);
if (response.ok) {
return response.json();
} else {
throw `${response.status} ${response.statusText}`;
}
} catch (error) {
throw "DG: Cannot Delete Key. " + error;
}
}
}