import axios, { AxiosInstance } from "axios"; import { GithubAuthorizeState, GithubUserAuthState } from "@plane/etl/github"; // plane web types import { TGithubWorkspaceConnection, TGithubWorkspaceUserConnection } from "@/plane-web/types/integrations"; export class GithubAuthService { protected baseURL: string; private axiosInstance: AxiosInstance; constructor(baseURL: string) { this.baseURL = baseURL; this.axiosInstance = axios.create({ baseURL, withCredentials: true }); } /** * @description fetch organization * @param { string } workspaceId * @returns { Promise } */ fetchOrganizationConnection = async (workspaceId: string): Promise => await this.axiosInstance .get(`/api/github/auth/organization-status/${workspaceId}`) .then((res) => res.data) .catch((error) => { throw error?.response?.data; }); /** * @description connect organization * @param { GithubAuthorizeState } payload * @returns { Promise } */ connectOrganization = async (payload: GithubAuthorizeState): Promise => await this.axiosInstance .post(`/api/github/auth/url`, payload) .then((res) => res.data) .catch((error) => { throw error?.response?.data; }); /** * @description disconnect organization * @param { string } workspaceId * @param { string } organizationId * @returns { Promise } */ disconnectOrganization = async (workspaceId: string, organizationId: string, userId: string): Promise => await this.axiosInstance .post(`/api/github/auth/organization-disconnect/${workspaceId}/${organizationId}/${userId}`) .then((res) => res.data) .catch((error) => { throw error?.response?.data; }); /** * @description fetch user * @param { string } workspaceId * @param { string } userId * @returns { Promise } */ fetchUserConnection = async ( workspaceId: string, userId: string ): Promise => await this.axiosInstance .get(`/api/github/auth/user-status/${workspaceId}/${userId}`) .then((res) => res.data) .catch((error) => { throw error?.response?.data; }); /** * @description connect user * @param { GithubUserAuthState } payload * @returns { Promise } */ connectUser = async (payload: GithubUserAuthState): Promise => await this.axiosInstance .post(`/api/github/auth/user/url`, payload) .then((res) => res.data) .catch((error) => { throw error?.response?.data; }); /** * @description disconnect user * @param { string } workspaceId * @param { string } userId * @returns { Promise } */ disconnectUser = async (workspaceId: string, userId: string): Promise => await this.axiosInstance .post(`/api/github/auth/user-disconnect/${workspaceId}/${userId}`) .then((res) => res.data) .catch((error) => { throw error?.response?.data; }); }