forgejo-oauth-app/api/oauth2-api.ts

78 lines
2.3 KiB
TypeScript

import { BaseApi } from "./base-api.ts";
export type Oauth2ApplicationCreateParams = {
name: string,
redirect_uris: string[],
confidential_client?: boolean,
}
export type Oauth2Application = {
id: number,
name: string,
redirect_uris: string[],
client_id: string,
confidential_client: boolean,
/**
* @format date-time
*/
created: string,
}
export type Oauth2ApplicationResponse = {
client_secret: string,
} & Oauth2Application
export class OAuth2Api extends BaseApi {
private token: string;
constructor(baseUrl: string, token: string) {
super(baseUrl);
this.token = token;
}
async createOauth2Application(params: Oauth2ApplicationCreateParams): Promise<Oauth2ApplicationResponse> {
return await this.request<Oauth2ApplicationResponse>(`/api/v1/user/applications/oauth2`, {
method: "POST",
headers: {
"Authorization": `Bearer ${this.token}`,
"Content-Type": "application/json",
},
body: JSON.stringify(params),
});
}
async getOauth2Applications(): Promise<Oauth2Application[]> {
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: Oauth2ApplicationCreateParams): Promise<Oauth2ApplicationResponse> {
return await this.request<Oauth2ApplicationResponse>(`/api/v1/user/applications/oauth2/${id}`, {
method: "PATCH",
headers: {
"Authorization": `Bearer ${this.token}`,
"Content-Type": "application/json",
},
body: JSON.stringify(params),
});
}
}