feat: autoset username

This commit is contained in:
Aravinth Manivannan 2023-09-19 03:36:40 +05:30
parent 7fa40b5872
commit 674815ca70
Signed by: realaravinth
GPG key ID: F8F50389936984FF
2 changed files with 7 additions and 11 deletions

View file

@ -9,14 +9,10 @@ const forgeoUrl = authtoken["forgejo_url"];
const token = authtoken["sha1"];
const username = authtoken["login"];
const repo = authtoken["repo"];
const api = new Forgejo(forgeoUrl, "owner_user");
api.setTokenAuth(token);
const api = new Forgejo(forgeoUrl);
test("use authentication without setting it ", () => {
const api = new Forgejo(forgeoUrl, "owner_user");
expect(() => api.getTokenAuth()).toThrow();
api.setTokenAuth(token);
test("use authentication without setting it ", async () => {
await api.setTokenAuth(token);
expect(api.getTokenAuth().getToken()).toBe(token);
let headers = api.getTokenAuthHeader();
expect(headers.Authorization).toBe(`token ${token}`);

View file

@ -17,19 +17,19 @@ class Forgejo {
* Represents a Forgejo instance.
* @constructor
* @param {string} url - The URL of the Forgejo instance
* @param {string} username - username of the logged in user
*/
constructor(url: string, username: string) {
constructor(url: string) {
this.url = new URL(url);
this.username = username;
}
/**
* Authenticate with token
* @param {Auth} token - access token
*/
setTokenAuth(token: string) {
async setTokenAuth(token: string) {
this.token = new Auth(token);
let res = await this.getUser();
this.username = res.username;
}
/**