51 lines
1.6 KiB
TypeScript
51 lines
1.6 KiB
TypeScript
import { BaseApi } from "./base-api.ts";
|
|
|
|
export type Token = {
|
|
id: number,
|
|
name: string,
|
|
scopes: string[],
|
|
sha1: string,
|
|
token_last_eight: string,
|
|
}
|
|
|
|
export class TokenApi extends BaseApi {
|
|
constructor(baseUrl: string, private tokenName: string) {
|
|
super(baseUrl);
|
|
}
|
|
|
|
async listTokens(username: string, secret: string): Promise<Token[] | undefined> {
|
|
return await this.request<Token[]>(`/api/v1/users/${username}/tokens`, {
|
|
method: "GET",
|
|
headers: {
|
|
"Authorization": `Basic ${btoa(`${username}:${secret}`)}`,
|
|
"Content-Type": "application/json",
|
|
},
|
|
});
|
|
}
|
|
|
|
async deleteToken(username: string, secret: string, tokenId: number): Promise<boolean> {
|
|
const result = await this.request(`/api/v1/users/${username}/tokens/${tokenId}`, {
|
|
method: "DELETE",
|
|
headers: {
|
|
"Authorization": `Basic ${btoa(`${username}:${secret}`)}`,
|
|
"Content-Type": "application/json",
|
|
},
|
|
});
|
|
|
|
return result !== undefined;
|
|
}
|
|
|
|
async createToken(username: string, secret: string): Promise<Token | undefined> {
|
|
return await this.request<Token>(`/api/v1/users/${username}/tokens`, {
|
|
method: "POST",
|
|
headers: {
|
|
"Authorization": `Basic ${btoa(`${username}:${secret}`)}`,
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify({
|
|
name: this.tokenName,
|
|
scopes: ["write:user", "read:user"],
|
|
}),
|
|
});
|
|
}
|
|
}
|