import { VssueAPI } from 'vssue' ; import axios , { AxiosInstance , AxiosRequestConfig } from 'axios' ; import { buildURL , concatURL , getCleanURL , parseQuery } from '@vssue/utils' ; import { normalizeUser , normalizeIssue , normalizeComment } from './utils' ; import { ResponseAccessToken , ResponseUser , ResponseIssue , ResponseComment , } from './types' ; see https see https export default class GiteeV5 implements VssueAPI . Instance { baseURL : string ; owner : string ; repo : string ; labels : Array < string > ; clientId : string ; clientSecret : string ; state : string ; proxy : string | ( ( url : string ) => string ) ; $http : AxiosInstance ; constructor ( { baseURL = 'https://gitee.com' , owner , repo , labels , clientId , clientSecret , state , proxy , } ) { if ( typeof clientSecret === 'undefined' || typeof proxy === 'undefined' ) { throw new Error ( 'clientSecret and proxy is required for Gitee V5' ) ; } this . baseURL = baseURL ; this . owner = owner ; this . repo = repo ; this . labels = labels ; this . clientId = clientId ; this . clientSecret = clientSecret ; this . state = state ; this . proxy = proxy ; this . $http = axios . create ( { baseURL : concatURL ( baseURL , 'api/v5' ) , } ) ; this . $http . interceptors . response . use ( response => response , error => { if ( error . response . data && error . response . data . message ) { error . message = error . response . data . message ; } return Promise . reject ( error ) ; } ) ; } /**
     * The platform api info
     */ get platform ( ) : VssueAPI . Platform { return { name : 'Gitee' , link : this . baseURL , version : 'v5' , meta : { reactable : false , sortable : false , } , } ; } see https redirectAuth ( ) { window . location . href = buildURL ( concatURL ( this . baseURL , 'oauth/authorize' ) , { client_id : this . clientId , redirect_uri : window . location . href , scope : 'user_info issues notes' , response_type : 'code' , state : this . state , } ) ; } see https remarks async handleAuth ( ) { const query = parseQuery ( window . location . search ) ; if ( query . code ) { if ( query . state !== this . state ) { return null ; } const code = query . code ; delete query . code ; delete query . state ; const replaceURL = buildURL ( getCleanURL ( window . location . href ) , query ) + window . location . hash ; window . history . replaceState ( null , '' , replaceURL ) ; const accessToken = await this . getAccessToken ( { code } ) ; return accessToken ; } return null ; } see https async getAccessToken ( { code } ) { const originalURL = concatURL ( this . baseURL , 'oauth/token' ) ; const proxyURL = typeof this . proxy === 'function' ? this . proxy ( originalURL ) : this . proxy ; const { data } = await this . $http . post < ResponseAccessToken > ( proxyURL , { client_id : this . clientId , client_secret : this . clientSecret , code , grant_type : 'authorization_code' , redirect_uri : window . location . href , } ) ; return data . access_token ; } see https async getUser ( { accessToken , } ) { const { data } = await this . $http . get < ResponseUser > ( 'user' , { params : { access_token : accessToken } , } ) ; return normalizeUser ( data ) ; } see https see https see https async getIssue ( { accessToken , issueId , issueTitle , } ) { const options = { params : { timestamp : Date . now ( ) , } , } ; if ( accessToken ) { options . params . access_token = accessToken ; } if ( issueId ) { try { const { data } = await this . $http . get < ResponseIssue > ( `repos/${ this . owner }/${ this . repo }/issues/${ issueId }` , options ) ; return normalizeIssue ( data ) ; } catch ( e ) { if ( e . response && e . response . status === 404 ) { return null ; } else { throw e ; } } } else { Object . assign ( options . params , { q : issueTitle , repo : `${ this . owner }/${ this . repo }` , per_page : 1 , } ) ; const { data } = await this . $http . get < ResponseIssue [ ] > ( `search/issues` , options ) ; const issue = data . map ( normalizeIssue ) . find ( item => item . title === issueTitle ) ; return issue || null ; } } see https async postIssue ( { accessToken , title , content , } ) { const { data } = await this . $http . post < ResponseIssue > ( `repos/${ this . owner }/issues` , { access_token : accessToken , repo : this . repo , title , body : content , labels : this . labels . join ( ',' ) , } ) ; return normalizeIssue ( data ) ; } see https remarks async getComments ( { accessToken , issueId , query : { page = 1 , perPage = 10 } = { } , } ) { const options = { params : { page : page , per_page : perPage , timestamp : Date . now ( ) , } , headers : { Accept : [ 'application/vnd.gitee.html+json' ] , } , } ; if ( accessToken ) { options . params . access_token = accessToken ; } const response = await this . $http . get < ResponseComment [ ] > ( `repos/${ this . owner }/${ this . repo }/issues/${ issueId }/comments` , options ) ; const count = Number ( response . headers . total_count ) ; return { count : count , page : page , perPage : perPage , data : response . data . map ( normalizeComment ) , } ; } see https async postComment ( { accessToken , issueId , content , } ) { const { data } = await this . $http . post < ResponseComment > ( `repos/${ this . owner }/${ this . repo }/issues/${ issueId }/comments` , { body : content , access_token : accessToken , } , { headers : { Accept : [ 'application/vnd.gitee.html+json' ] , } , } ) ; return normalizeComment ( data ) ; } see https async putComment ( { accessToken , commentId , content , } ) { const { data } = await this . $http . patch < ResponseComment > ( `repos/${ this . owner }/${ this . repo }/issues/comments/${ commentId }` , { body : content , access_token : accessToken , } , { headers : { Accept : [ 'application/vnd.gitee.html+json' ] , } , } ) ; return normalizeComment ( data ) ; } see https async deleteComment ( { accessToken , commentId , } ) { const { status } = await this . $http . delete ( `repos/${ this . owner }/${ this . repo }/issues/comments/${ commentId }` , { params : { access_token : accessToken } , } ) ; return status === 204 ; } /**
     * Gitee does not support reactions now
     */ async getCommentReactions ( options ) { throw new Error ( '501 Not Implemented' ) ; } /**
     * Gitee does not support reactions now
     */ async postCommentReaction ( options ) { throw new Error ( '501 Not Implemented' ) ; } }