11import type { RequestError } from '@octokit/request-error' ;
22import type { CancellationToken } from 'vscode' ;
33import { version as codeVersion , env , Uri , window } from 'vscode' ;
4- import type { HeadersInit , RequestInfo , RequestInit , Response } from '@env/fetch' ;
4+ import type { RequestInfo , RequestInit , Response } from '@env/fetch' ;
55import { fetch as _fetch , getProxyAgent } from '@env/fetch' ;
66import { getPlatform } from '@env/platform' ;
77import type { Disposable } from '../../api/gitlens' ;
@@ -27,45 +27,27 @@ import { memoize } from '../../system/decorators/-webview/memoize';
2727import { Logger } from '../../system/logger' ;
2828import type { LogScope } from '../../system/logger.scope' ;
2929import { getLogScope } from '../../system/logger.scope' ;
30+ import type { UrlsProvider } from './urlsProvider' ;
3031
3132interface FetchOptions {
3233 cancellation ?: CancellationToken ;
3334 timeout ?: number ;
3435}
3536
3637interface GKFetchOptions extends FetchOptions {
37- token ?: string ;
38- unAuthenticated ?: boolean ;
39- query ?: string ;
38+ token ?: string | false ;
4039 organizationId ?: string | false ;
40+ query ?: string ;
4141}
4242
4343export class ServerConnection implements Disposable {
44- constructor ( private readonly container : Container ) { }
44+ constructor (
45+ private readonly container : Container ,
46+ public readonly urls : UrlsProvider ,
47+ ) { }
4548
4649 dispose ( ) : void { }
4750
48- @memoize ( )
49- private get baseGkApiUri ( ) : Uri {
50- if ( this . container . env === 'staging' ) {
51- return Uri . parse ( 'https://staging-api.gitkraken.dev' ) ;
52- }
53-
54- if ( this . container . env === 'dev' ) {
55- return Uri . parse ( 'https://dev-api.gitkraken.dev' ) ;
56- }
57-
58- return Uri . parse ( 'https://api.gitkraken.dev' ) ;
59- }
60-
61- getGkApiUrl ( ...pathSegments : string [ ] ) : string {
62- return Uri . joinPath ( this . baseGkApiUri , ...pathSegments ) . toString ( ) ;
63- }
64-
65- getGkConfigUrl ( ...pathSegments : string [ ] ) : string {
66- return Uri . joinPath ( Uri . parse ( 'https://configs.gitkraken.dev' ) , 'gitlens' , ...pathSegments ) . toString ( ) ;
67- }
68-
6951 @memoize ( )
7052 get userAgent ( ) : string {
7153 // TODO@eamodio figure out standardized format/structure for our user agents
@@ -121,11 +103,11 @@ export class ServerConnection implements Disposable {
121103 }
122104
123105 async fetchGkApi ( path : string , init ?: RequestInit , options ?: GKFetchOptions ) : Promise < Response > {
124- return this . gkFetch ( this . getGkApiUrl ( path ) , init , options ) ;
106+ return this . gkFetch ( this . urls . getGkApiUrl ( path ) , init , options ) ;
125107 }
126108
127109 async fetchGkConfig ( path : string , init ?: RequestInit , options ?: FetchOptions ) : Promise < Response > {
128- return this . fetch ( this . getGkConfigUrl ( path ) , init , options ) ;
110+ return this . fetch ( this . urls . getGkConfigUrl ( path ) , init , options ) ;
129111 }
130112
131113 async fetchGkApiGraphQL (
@@ -134,15 +116,34 @@ export class ServerConnection implements Disposable {
134116 init ?: RequestInit ,
135117 options ?: GKFetchOptions ,
136118 ) : Promise < Response > {
137- return this . fetchGkApi (
138- path ,
139- {
140- method : 'POST' ,
141- ...init ,
142- body : JSON . stringify ( request ) ,
143- } ,
144- options ,
145- ) ;
119+ return this . fetchGkApi ( path , { method : 'POST' , ...init , body : JSON . stringify ( request ) } , options ) ;
120+ }
121+
122+ async getGkHeaders (
123+ token ?: string | false ,
124+ organizationId ?: string | false ,
125+ init ?: Record < string , string > ,
126+ ) : Promise < Record < string , string > > {
127+ const headers : Record < string , string > = {
128+ 'Content-Type' : 'application/json' ,
129+ 'Client-Name' : this . clientName ,
130+ 'Client-Version' : this . container . version ,
131+ 'User-Agent' : this . userAgent ,
132+ ...init ,
133+ } ;
134+
135+ token ??= await this . getAccessToken ( ) ;
136+ if ( token ) {
137+ headers . Authorization = `Bearer ${ token } ` ;
138+ }
139+
140+ // only check for cached subscription or we'll get into an infinite loop
141+ organizationId ??= ( await this . container . subscription . getSubscription ( true ) ) . activeOrganization ?. id ;
142+ if ( organizationId ) {
143+ headers [ 'gk-org-id' ] = organizationId ;
144+ }
145+
146+ return headers ;
146147 }
147148
148149 private async gkFetch ( url : RequestInfo , init ?: RequestInit , options ?: GKFetchOptions ) : Promise < Response > {
@@ -152,29 +153,11 @@ export class ServerConnection implements Disposable {
152153 const scope = getLogScope ( ) ;
153154
154155 try {
155- let token ;
156- ( { token, ...options } = options ?? { } ) ;
157- if ( ! options ?. unAuthenticated ) {
158- token ??= await this . getAccessToken ( ) ;
159- }
160-
161- const headers : Record < string , unknown > = {
162- Authorization : `Bearer ${ token } ` ,
163- 'Content-Type' : 'application/json' ,
164- 'Client-Name' : this . clientName ,
165- 'Client-Version' : this . container . version ,
166- ...init ?. headers ,
167- } ;
168-
169- // only check for cached subscription or we'll get into an infinite loop
170- let organizationId = options ?. organizationId ;
171- if ( organizationId === undefined ) {
172- organizationId = ( await this . container . subscription . getSubscription ( true ) ) . activeOrganization ?. id ;
173- }
174-
175- if ( organizationId ) {
176- headers [ 'gk-org-id' ] = organizationId ;
177- }
156+ const headers = await this . getGkHeaders (
157+ options ?. token ,
158+ options ?. organizationId ,
159+ init ?. headers ? { ...( init ?. headers as Record < string , string > ) } : undefined ,
160+ ) ;
178161
179162 if ( options ?. query != null ) {
180163 if ( url instanceof URL ) {
@@ -184,14 +167,7 @@ export class ServerConnection implements Disposable {
184167 }
185168 }
186169
187- const rsp = await this . fetch (
188- url ,
189- {
190- ...init ,
191- headers : headers as HeadersInit ,
192- } ,
193- options ,
194- ) ;
170+ const rsp = await this . fetch ( url , { ...init , headers : headers } , options ) ;
195171 if ( ! rsp . ok ) {
196172 await this . handleGkUnsuccessfulResponse ( rsp , scope ) ;
197173 } else {
0 commit comments