44// See LICENSE or go to <https://www.apache.org/licenses/LICENSE-2.0> for full license details.
55
66import * as vscode from "vscode" ;
7- import axios , { AxiosResponse , AxiosError } from "axios" ;
87
98let config = vscode . workspace . getConfiguration ( "api" ) ;
109
@@ -40,6 +39,10 @@ export interface StreamingMessage {
4039 done : boolean ;
4140}
4241
42+ interface QuackToken {
43+ access_token : string ;
44+ }
45+
4346export async function verifyQuackEndpoint (
4447 endpointURL : string ,
4548) : Promise < boolean > {
@@ -48,14 +51,15 @@ export async function verifyQuackEndpoint(
4851 endpointURL ,
4952 ) . toString ( ) ;
5053 try {
51- await axios . get ( routeURL ) ;
52- return true ;
53- } catch ( error ) {
54- if ( error instanceof AxiosError ) {
55- if ( error . response && error . response . status === 401 ) {
56- return true ;
57- }
54+ const response = await fetch ( routeURL ) ;
55+ if ( response . ok ) {
56+ return true ;
57+ } else if ( response . status === 401 ) {
58+ return true ;
59+ } else {
60+ return false ;
5861 }
62+ } catch ( error ) {
5963 return false ;
6064 }
6165}
@@ -69,19 +73,21 @@ export async function getAPIAccessStatus(
6973 endpointURL ,
7074 ) . toString ( ) ;
7175 try {
72- await axios . get ( routeURL , {
76+ const response = await fetch ( routeURL , {
7377 headers : { Authorization : `Bearer ${ quackToken } ` } ,
7478 } ) ;
75- return "ok" ; // Token & endpoint good
79+ if ( response . ok ) {
80+ return "ok" ; // Token & endpoint good
81+ } else if ( response . status === 404 ) {
82+ return "unknown-route" ; // Unknown route
83+ } else if ( response . status === 401 ) {
84+ return "expired-token" ; // Expired token
85+ } else {
86+ return "other" ; // Other HTTP status codes
87+ }
7688 } catch ( error ) {
77- if ( error instanceof AxiosError ) {
78- if ( error . code === "ECONNREFUSED" ) {
79- return "unreachable-endpoint" ; // Wrong endpoint
80- } else if ( error . response && error . response . status === 404 ) {
81- return "unknown-route" ; // Unknown route
82- } else if ( error . response && error . response . status === 401 ) {
83- return "expired-token" ; // expired
84- }
89+ if ( error instanceof Error && error . name === "TypeError" ) {
90+ return "unreachable-endpoint" ; // Wrong endpoint
8591 }
8692 return "other" ; // Token or server issues
8793 }
@@ -115,13 +121,20 @@ export async function getToken(
115121 const quackURL = new URL ( "/api/v1/login/token" , endpointURL ) . toString ( ) ;
116122 try {
117123 // Retrieve the guidelines
118- const response : AxiosResponse < any > = await axios . post ( quackURL , {
119- github_token : githubToken ,
124+ const response = await fetch ( quackURL , {
125+ method : "POST" ,
126+ headers : {
127+ "Content-Type" : "application/json" ,
128+ } ,
129+ body : JSON . stringify ( {
130+ github_token : githubToken ,
131+ } ) ,
120132 } ) ;
121133
122134 // Handle the response
123- if ( response . status === 200 ) {
124- return response . data . access_token ;
135+ if ( response . ok ) {
136+ const data = ( await response . json ( ) ) as QuackToken ;
137+ return data . access_token ;
125138 } else {
126139 // The request returned a non-200 status code (e.g., 404)
127140 // Show an error message or handle the error accordingly
@@ -149,13 +162,13 @@ export async function fetchGuidelines(
149162 const quackURL = new URL ( `/api/v1/guidelines` , endpointURL ) . toString ( ) ;
150163 try {
151164 // Retrieve the guidelines
152- const response : AxiosResponse < any > = await axios . get ( quackURL , {
165+ const response = await fetch ( quackURL , {
153166 headers : { Authorization : `Bearer ${ token } ` } ,
154167 } ) ;
155168
156169 // Handle the response
157- if ( response . status === 200 ) {
158- return response . data ;
170+ if ( response . ok ) {
171+ return ( await response . json ( ) ) as QuackGuideline [ ] ;
159172 } else {
160173 // The request returned a non-200 status code (e.g., 404)
161174 // Show an error message or handle the error accordingly
@@ -185,24 +198,36 @@ export async function postChatMessage(
185198) : Promise < void > {
186199 const quackURL = new URL ( "/api/v1/code/chat" , endpointURL ) . toString ( ) ;
187200 try {
188- const response : AxiosResponse < any > = await axios . post (
189- quackURL ,
190- { messages : messages } ,
191- { headers : { Authorization : `Bearer ${ token } ` } , responseType : "stream" } ,
192- ) ;
193- response . data . on ( "data" , ( chunk : any ) => {
194- // Handle the chunk of data
195- onChunkReceived ( JSON . parse ( chunk ) . message . content ) ;
196- } ) ;
197-
198- response . data . on ( "end" , ( ) => {
199- // console.log("Stream ended");
200- onEnd ( ) ;
201+ const response = await fetch ( quackURL , {
202+ method : "POST" ,
203+ headers : {
204+ "Content-Type" : "application/json" ,
205+ Authorization : `Bearer ${ token } ` ,
206+ } ,
207+ body : JSON . stringify ( { messages : messages } ) ,
201208 } ) ;
202209
203- response . data . on ( "error" , ( error : Error ) => {
204- console . error ( error ) ;
205- } ) ;
210+ if ( response . body ) {
211+ const reader = response . body . getReader ( ) ;
212+ try {
213+ while ( true ) {
214+ const { done, value } = await reader . read ( ) ;
215+ if ( done ) {
216+ break ;
217+ }
218+ // Assume each chunk is a Uint8Array, convert to a string or otherwise process
219+ const chunk = new TextDecoder ( ) . decode ( value ) ;
220+ // Process the chunk, e.g., assuming JSON content
221+ onChunkReceived ( JSON . parse ( chunk ) . message . content ) ;
222+ }
223+ // Stream ended
224+ onEnd ( ) ;
225+ } catch ( error ) {
226+ console . error ( error ) ;
227+ } finally {
228+ reader . releaseLock ( ) ;
229+ }
230+ }
206231 } catch ( error ) {
207232 // Handle other errors that may occur during the request
208233 console . error ( "Error sending Quack API request:" , error ) ;
@@ -219,17 +244,18 @@ export async function postGuideline(
219244 const quackURL = new URL ( `/api/v1/guidelines` , endpointURL ) . toString ( ) ;
220245 try {
221246 // Retrieve the guidelines
222- const response : AxiosResponse < any > = await axios . post (
223- quackURL ,
224- { content : content } ,
225- {
226- headers : { Authorization : `Bearer ${ token } ` } ,
247+ const response = await fetch ( quackURL , {
248+ method : "POST" ,
249+ headers : {
250+ "Content-Type" : "application/json" ,
251+ Authorization : `Bearer ${ token } ` ,
227252 } ,
228- ) ;
253+ body : JSON . stringify ( { content : content } ) ,
254+ } ) ;
229255
230256 // Handle the response
231- if ( response . status === 201 ) {
232- return response . data ;
257+ if ( response . ok ) {
258+ return ( await response . json ( ) ) as QuackGuideline ;
233259 } else {
234260 vscode . window . showErrorMessage (
235261 `Quack API returned status code ${ response . status } ` ,
@@ -254,18 +280,19 @@ export async function patchGuideline(
254280) : Promise < QuackGuideline > {
255281 const quackURL = new URL ( `/api/v1/guidelines/${ id } ` , endpointURL ) . toString ( ) ;
256282 try {
257- // Retrieve the guidelines
258- const response : AxiosResponse < any > = await axios . patch (
259- quackURL ,
260- { content : content } ,
261- {
262- headers : { Authorization : `Bearer ${ token } ` } ,
283+ // Update the guideline
284+ const response = await fetch ( quackURL , {
285+ method : "PATCH" ,
286+ headers : {
287+ "Content-Type" : "application/json" ,
288+ Authorization : `Bearer ${ token } ` ,
263289 } ,
264- ) ;
290+ body : JSON . stringify ( { content : content } ) ,
291+ } ) ;
265292
266293 // Handle the response
267- if ( response . status === 200 ) {
268- return response . data ;
294+ if ( response . ok ) {
295+ return ( await response . json ( ) ) as QuackGuideline ;
269296 } else {
270297 vscode . window . showErrorMessage (
271298 `Quack API returned status code ${ response . status } ` ,
@@ -289,14 +316,18 @@ export async function deleteGuideline(
289316) : Promise < QuackGuideline > {
290317 const quackURL = new URL ( `/api/v1/guidelines/${ id } ` , endpointURL ) . toString ( ) ;
291318 try {
292- // Retrieve the guidelines
293- const response : AxiosResponse < any > = await axios . delete ( quackURL , {
294- headers : { Authorization : `Bearer ${ token } ` } ,
319+ // Delete the guideline
320+ const response = await fetch ( quackURL , {
321+ method : "DELETE" ,
322+ headers : {
323+ "Content-Type" : "application/json" ,
324+ Authorization : `Bearer ${ token } ` ,
325+ } ,
295326 } ) ;
296327
297328 // Handle the response
298- if ( response . status === 200 ) {
299- return response . data ;
329+ if ( response . ok ) {
330+ return ( await response . json ( ) ) as QuackGuideline ;
300331 } else {
301332 vscode . window . showErrorMessage (
302333 `Quack API returned status code ${ response . status } ` ,
0 commit comments