forgejo-oauth-app/api/oauth2-api.ts
2025-04-10 21:36:48 +09:00

82 lines
2.4 KiB
TypeScript

import { BaseApi } from "./base-api.ts";
export type Oauth2Application = {
id: number,
name: string,
redirect_uris: string[],
client_id: string,
client_secret: string,
confidential_client: boolean,
/**
* @format date-time
*/
created: string,
}
export class OAuth2Api extends BaseApi {
private token: string;
constructor(baseUrl: string, token: string) {
super(baseUrl);
this.token = token;
}
// Method to update token if needed later
updateToken(token: string): void {
this.token = token;
}
async createOauth2Application(params: {
name: string,
redirect_uris: string[],
confidential_client?: boolean,
}): Promise<Oauth2Application | undefined> {
return await this.request<Oauth2Application>(`/api/v1/user/applications/oauth2`, {
method: "POST",
headers: {
"Authorization": `Bearer ${this.token}`,
"Content-Type": "application/json",
},
body: JSON.stringify(params),
});
}
async getOauth2Applications(): Promise<Oauth2Application[] | undefined> {
return await this.request<Oauth2Application[]>(`/api/v1/user/applications/oauth2`, {
method: "GET",
headers: {
"Authorization": `Bearer ${this.token}`,
"Content-Type": "application/json",
},
});
}
async deleteOauth2Application(id: number): Promise<void> {
if (!this.token) {
throw new Error("Authentication required. Please login first.");
}
await this.request<void>(`/api/v1/user/applications/oauth2/${id}`, {
method: "DELETE",
headers: {
"Authorization": `Bearer ${this.token}`,
"Content-Type": "application/json",
},
});
}
async updateOauth2Application(id: number, params: {
name: string,
redirect_uris: string[],
confidential_client?: boolean,
}): Promise<Oauth2Application | undefined> {
return await this.request<Oauth2Application>(`/api/v1/user/applications/oauth2/${id}`, {
method: "PATCH",
headers: {
"Authorization": `Bearer ${this.token}`,
"Content-Type": "application/json",
},
body: JSON.stringify(params),
});
}
}