11import * as vscode from "vscode"
22import { getExtensionVersion } from "../extension"
33import { AUTH_PROVIDER_ID } from "./auth"
4- import type { App , Deployment , ListResponse , Team } from "./types"
4+ import type { App , Deployment , ListResponse , Team , UserInfo } from "./types"
55
66export interface UploadInfo {
77 url : string
88 fields : Record < string , string >
99}
1010
11+ export const BASE_URL = "https://api.fastapicloud.com/api/v1"
12+ export const DASHBOARD_URL = "https://dashboard.fastapicloud.com"
13+
1114function getUserAgentHeaders ( ) : Record < string , string > {
12- // User-Agent is a forbidden header in browsers and causes fetch to fail
1315 if ( vscode . env . uiKind === vscode . UIKind . Web ) return { }
1416 return { "User-Agent" : `fastapi-vscode/${ getExtensionVersion ( ) } ` }
1517}
1618
1719export class ApiService {
18- public static readonly BASE_URL = "https://api.fastapicloud.com/api/v1"
19- public static readonly DASHBOARD_URL = "https://dashboard.fastapicloud.com"
20+ static async fetchUserInfo ( token : string ) : Promise < UserInfo | null > {
21+ try {
22+ const response = await fetch ( `${ BASE_URL } /users/me` , {
23+ headers : {
24+ Authorization : `Bearer ${ token } ` ,
25+ "Content-Type" : "application/json" ,
26+ } ,
27+ } )
28+ if ( ! response . ok ) return null
29+ return ( await response . json ( ) ) as UserInfo
30+ } catch {
31+ return null
32+ }
33+ }
2034
2135 static getDashboardUrl ( teamSlug : string , appSlug : string ) : string {
22- return `${ ApiService . DASHBOARD_URL } /${ teamSlug } /apps/${ appSlug } /general`
36+ return `${ DASHBOARD_URL } /${ teamSlug } /apps/${ appSlug } /general`
2337 }
2438
2539 private async request < T > (
@@ -36,7 +50,7 @@ export class ApiService {
3650 }
3751 const token = session . accessToken
3852
39- const response = await fetch ( `${ ApiService . BASE_URL } ${ endpoint } ` , {
53+ const response = await fetch ( `${ BASE_URL } ${ endpoint } ` , {
4054 ...options ,
4155 headers : {
4256 Authorization : `Bearer ${ token } ` ,
@@ -117,17 +131,14 @@ export class ApiService {
117131 expires_in ?: number
118132 interval ?: number
119133 } > {
120- const response = await fetch (
121- `${ ApiService . BASE_URL } /login/device/authorization` ,
122- {
123- method : "POST" ,
124- headers : {
125- "Content-Type" : "application/x-www-form-urlencoded" ,
126- ...getUserAgentHeaders ( ) ,
127- } ,
128- body : new URLSearchParams ( { client_id : clientId } ) . toString ( ) ,
134+ const response = await fetch ( `${ BASE_URL } /login/device/authorization` , {
135+ method : "POST" ,
136+ headers : {
137+ "Content-Type" : "application/x-www-form-urlencoded" ,
138+ ...getUserAgentHeaders ( ) ,
129139 } ,
130- )
140+ body : new URLSearchParams ( { client_id : clientId } ) . toString ( ) ,
141+ } )
131142
132143 if ( ! response . ok ) {
133144 throw new Error (
@@ -168,22 +179,19 @@ export class ApiService {
168179 throw new Error ( "Sign-in cancelled" )
169180 }
170181
171- const response = await fetch (
172- `${ ApiService . BASE_URL } /login/device/token` ,
173- {
174- method : "POST" ,
175- headers : {
176- "Content-Type" : "application/x-www-form-urlencoded" ,
177- ...getUserAgentHeaders ( ) ,
178- } ,
179- body : new URLSearchParams ( {
180- client_id : clientId ,
181- device_code : deviceCode ,
182- grant_type : "urn:ietf:params:oauth:grant-type:device_code" ,
183- } ) . toString ( ) ,
184- signal,
182+ const response = await fetch ( `${ BASE_URL } /login/device/token` , {
183+ method : "POST" ,
184+ headers : {
185+ "Content-Type" : "application/x-www-form-urlencoded" ,
186+ ...getUserAgentHeaders ( ) ,
185187 } ,
186- )
188+ body : new URLSearchParams ( {
189+ client_id : clientId ,
190+ device_code : deviceCode ,
191+ grant_type : "urn:ietf:params:oauth:grant-type:device_code" ,
192+ } ) . toString ( ) ,
193+ signal,
194+ } )
187195
188196 const data = ( await response . json ( ) ) as {
189197 access_token ?: string
0 commit comments