11import { Injectable , Autowired } from '@ali/common-di' ;
2- import { localize , IReporterService , formatLocalize } from '@ali/ide-core-common' ;
2+ import { localize , IReporterService } from '@ali/ide-core-common' ;
33import { REPORT_NAME } from '@alipay/alex-core' ;
4- import { CodeModelService , ICodeAPIService , EntryParam } from '@alipay/alex-code-service' ;
4+ import { CodeModelService , ICodeAPIService } from '@alipay/alex-code-service' ;
5+ import type { EntryParam , RefsParam } from '@alipay/alex-code-service' ;
56import { IMessageService } from '@ali/ide-overlay' ;
6- import { request , RequestOptions } from '@alipay/alex-shared' ;
7- import { createUrl } from '../common/utils' ;
7+ import { request , RequestOptions , isResponseError } from '@alipay/alex-shared' ;
88import { API } from './types' ;
99
1010@Injectable ( )
@@ -28,37 +28,39 @@ export class AntCodeService implements ICodeAPIService {
2828 }
2929
3030 private async request < T > ( path : string , options ?: RequestOptions ) : Promise < T > {
31- const url = createUrl ( this . codeModel . endpoint , path ) ;
32-
3331 try {
34- const data = await request ( url , {
32+ const data = await request ( path , {
33+ baseURL : this . codeModel . endpoint ,
3534 credentials : 'include' ,
3635 responseType : 'json' ,
3736 ...options ,
3837 } ) ;
3938 return data ;
40- } catch ( err : any ) {
41- const status = err . response ?. status ;
42- this . reporter . point ( REPORT_NAME . CODE_SERVICE_REQUEST_ERROR , err . message , {
43- url,
44- status,
45- platform : this . codeModel . platform ,
46- } ) ;
47- if ( status === 401 ) {
48- const goto = localize ( 'api.login.goto' ) ;
49- this . messageService
50- . error ( localize ( 'api.response.no-login-antcode' ) , [ goto ] )
51- . then ( ( value ) => {
52- if ( value === goto ) {
53- window . open ( this . codeModel . origin ) ;
54- }
55- } ) ;
56- } else if ( status === 403 ) {
57- this . messageService . error ( localize ( 'api.response.project-no-access' ) ) ;
58- } else if ( status === 404 ) {
59- this . messageService . error (
60- formatLocalize ( 'api.response.project-not-found' , this . codeModel . project )
61- ) ;
39+ } catch ( err : unknown ) {
40+ if ( isResponseError ( err ) ) {
41+ const { status } = err . response ;
42+ this . reporter . point ( REPORT_NAME . CODE_SERVICE_REQUEST_ERROR , err . message , {
43+ path,
44+ status,
45+ platform : this . codeModel . platform ,
46+ } ) ;
47+ if ( status === 401 ) {
48+ const goto = localize ( 'api.login.goto' ) ;
49+ this . messageService
50+ . error ( localize ( 'api.response.no-login-antcode' ) , [ goto ] )
51+ . then ( ( value ) => {
52+ if ( value === goto ) {
53+ window . open ( this . codeModel . origin ) ;
54+ }
55+ } ) ;
56+ } else if ( status === 403 ) {
57+ this . messageService . error ( localize ( 'api.response.project-no-access' ) ) ;
58+ } else if ( status === 404 ) {
59+ // TODO 更精细化的错误提示
60+ this . messageService . error ( localize ( 'error.resource-not-found' ) ) ;
61+ } else {
62+ this . messageService . error ( `${ status } - ${ localize ( 'error.request' ) } ` ) ;
63+ }
6264 } else {
6365 this . messageService . error ( localize ( 'api.response.unknown-error' ) ) ;
6466 }
@@ -75,7 +77,7 @@ export class AntCodeService implements ICodeAPIService {
7577 }
7678
7779 async getTree ( path : string ) {
78- await this . codeModel . isInitialized ;
80+ await this . codeModel . headInitialized ;
7981 return this . request < API . ResponseGetTree > (
8082 `/api/v3/projects/${ this . codeModel . projectId } /repository/tree` ,
8183 {
@@ -88,7 +90,7 @@ export class AntCodeService implements ICodeAPIService {
8890 }
8991
9092 async getBlob ( entry : EntryParam ) {
91- await this . codeModel . isInitialized ;
93+ await this . codeModel . headInitialized ;
9294 const buf = await this . request < ArrayBuffer > (
9395 `/api/v3/projects/${ this . codeModel . projectId } /repository/blobs/${ this . codeModel . HEAD } ` ,
9496 {
@@ -102,7 +104,7 @@ export class AntCodeService implements ICodeAPIService {
102104 }
103105
104106 async getEntryInfo ( entry : EntryParam ) {
105- await this . codeModel . isInitialized ;
107+ await this . codeModel . headInitialized ;
106108 const data = await this . request < API . ResponseGetEntry > (
107109 `/api/v3/projects/${ this . codeModel . projectId } /repository/tree_entry` ,
108110 {
@@ -117,4 +119,19 @@ export class AntCodeService implements ICodeAPIService {
117119 fileType : data . render === 'download' ? 'binary' : data . render ,
118120 } as const ;
119121 }
122+
123+ async getRefs ( ) : Promise < RefsParam > {
124+ const [ branches , tags ] = await Promise . all ( [
125+ this . request < API . ResponseGetRefs > (
126+ `/api/v3/projects/${ this . codeModel . projectId } /repository/branches`
127+ ) ,
128+ this . request < API . ResponseGetRefs > (
129+ `/api/v3/projects/${ this . codeModel . projectId } /repository/tags`
130+ ) ,
131+ ] ) ;
132+ return {
133+ branches,
134+ tags,
135+ } ;
136+ }
120137}
0 commit comments