@@ -7,7 +7,7 @@ import * as PlatformError from "effect/PlatformError";
77import * as Redacted from "effect/Redacted" ;
88import * as Result from "effect/Result" ;
99import * as Schema from "effect/Schema" ;
10- import { HttpClient , HttpClientRequest } from "effect/http" ;
10+ import { FetchHttpClient , HttpClient , HttpClientRequest } from "effect/http" ;
1111import { PositiveInt , TrimmedNonEmptyString , type ChangeRequest } from "@t3tools/contracts" ;
1212
1313import { collectUint8StreamText } from "../stream/collectUint8StreamText.ts" ;
@@ -258,70 +258,86 @@ export const make = Effect.gen(function* () {
258258 detail,
259259 ...( cause === undefined ? { } : { cause } ) ,
260260 } ) ;
261- const { token } = yield * credentials
262- . get ( host )
263- . pipe (
264- Effect . mapError ( ( cause ) =>
265- failed (
266- cause . _tag === "GitCafeCliMissingError"
267- ? "CLI_UNAVAILABLE"
268- : cause . _tag === "GitCafeNotSignedInError"
269- ? "AUTHENTICATION_REQUIRED"
270- : "COMMAND_FAILED" ,
271- null ,
272- cause . message ,
273- cause ,
261+ const credential = ( ) =>
262+ credentials
263+ . get ( host )
264+ . pipe (
265+ Effect . mapError ( ( cause ) =>
266+ failed (
267+ cause . _tag === "GitCafeCliMissingError"
268+ ? "CLI_UNAVAILABLE"
269+ : cause . _tag === "GitCafeNotSignedInError"
270+ ? "AUTHENTICATION_REQUIRED"
271+ : "COMMAND_FAILED" ,
272+ null ,
273+ cause . message ,
274+ cause ,
275+ ) ,
274276 ) ,
275- ) ,
277+ ) ;
278+ const send = ( token : Redacted . Redacted < string > ) => {
279+ const base = HttpClientRequest . make (
280+ ( input . method ?? "GET" ) as HttpClientRequest . HttpClientRequest [ "method" ] ,
281+ ) ( `https://${ host } /api${ input . endpoint } ` ) . pipe (
282+ HttpClientRequest . bearerToken ( Redacted . value ( token ) ) ,
283+ HttpClientRequest . setHeaders ( {
284+ accept : "application/json" ,
285+ "user-agent" : "t3code" ,
286+ ...input . headers ,
287+ } ) ,
276288 ) ;
277- const base = HttpClientRequest . make (
278- ( input . method ?? "GET" ) as HttpClientRequest . HttpClientRequest [ "method" ] ,
279- ) ( `https://${ host } /api${ input . endpoint } ` ) . pipe (
280- HttpClientRequest . bearerToken ( Redacted . value ( token ) ) ,
281- HttpClientRequest . setHeaders ( {
282- accept : "application/json" ,
283- "user-agent" : "t3code" ,
284- ...input . headers ,
285- } ) ,
286- ) ;
287- const request =
288- input . body === undefined ? base : HttpClientRequest . bodyJsonUnsafe ( base , input . body ) ;
289- const { status, collected } = yield * Effect . gen ( function * ( ) {
290- const response = yield * httpClient
291- . execute ( request )
292- // The client's own span would record the query string, which can carry branch names.
293- . pipe ( Effect . provideService ( HttpClient . TracerDisabledWhen , ( ) => true ) ) ;
294- const collected = yield * collectUint8StreamText ( {
295- stream : response . stream ,
296- maxBytes : input . maxOutputBytes ?? DEFAULT_MAX_RESPONSE_BYTES ,
289+ return input . body === undefined ? base : HttpClientRequest . bodyJsonUnsafe ( base , input . body ) ;
290+ } ;
291+ const exchange = ( request : HttpClientRequest . HttpClientRequest ) =>
292+ Effect . gen ( function * ( ) {
293+ const response = yield * httpClient . execute ( request ) . pipe (
294+ // The client's own span would record the query string, which can carry branch names.
295+ Effect . provideService ( HttpClient . TracerDisabledWhen , ( ) => true ) ,
296+ // A redirect is never followed with the token attached; GitCafe's API answers in place.
297+ Effect . provideService ( FetchHttpClient . RequestInit , { redirect : "manual" } ) ,
298+ ) ;
299+ const collected = yield * collectUint8StreamText ( {
300+ stream : response . stream ,
301+ maxBytes : input . maxOutputBytes ?? DEFAULT_MAX_RESPONSE_BYTES ,
302+ } ) . pipe (
303+ // 204 and many refusals carry no body at all, which is an empty answer.
304+ Effect . catchIf (
305+ ( cause ) => cause . reason . _tag === "EmptyBodyError" ,
306+ ( ) => Effect . succeed ( { text : "" , truncated : false , invalidUtf8 : false } ) ,
307+ ) ,
308+ ) ;
309+ return { status : response . status , collected } ;
297310 } ) . pipe (
298- // 204 and many refusals carry no body at all, which is an empty answer.
299- Effect . catchIf (
300- ( cause ) => cause . reason . _tag === "EmptyBodyError" ,
301- ( ) => Effect . succeed ( { text : "" , truncated : false , invalidUtf8 : false } ) ,
311+ Effect . timeout ( API_TIMEOUT ) ,
312+ Effect . mapError ( ( cause ) =>
313+ cause . _tag === "TimeoutError"
314+ ? failed (
315+ "TIMEOUT" ,
316+ null ,
317+ `GitCafe did not answer within ${ Duration . toSeconds ( API_TIMEOUT ) } seconds. Check the connection and try refreshing.` ,
318+ cause ,
319+ )
320+ : failed ( "COMMAND_FAILED" , null , "Could not reach GitCafe." , cause ) ,
302321 ) ,
303322 ) ;
304- return { status : response . status , collected } ;
305- } ) . pipe (
306- Effect . timeout ( API_TIMEOUT ) ,
307- Effect . mapError ( ( cause ) =>
308- cause . _tag === "TimeoutError"
309- ? failed (
310- "TIMEOUT" ,
311- null ,
312- `GitCafe did not answer within ${ Duration . toSeconds ( API_TIMEOUT ) } seconds. Check the connection and try refreshing.` ,
313- cause ,
314- )
315- : failed ( "COMMAND_FAILED" , null , "Could not reach GitCafe." , cause ) ,
316- ) ,
317- ) ;
323+ const first = yield * credential ( ) ;
324+ let answer = yield * exchange ( send ( first . token ) ) ;
325+ // The source may hold a newer token than the one that was refused. A `cafe` login that was
326+ // just refreshed answers again with the new one, once; an environment token cannot change.
327+ if ( answer . status === 401 ) {
328+ yield * credentials . invalidate ( host ) ;
329+ if ( first . source === "cafe" ) {
330+ const fresh = yield * credential ( ) ;
331+ if ( Redacted . value ( fresh . token ) !== Redacted . value ( first . token ) )
332+ answer = yield * exchange ( send ( fresh . token ) ) ;
333+ }
334+ }
335+ const { status, collected } = answer ;
318336 if ( status >= 200 && status < 300 ) {
319337 if ( collected . truncated )
320338 return yield * failed ( "RESPONSE_TOO_LARGE" , status , "GitCafe's response was too large." ) ;
321339 return collected . text ;
322340 }
323- // The source may hold a newer token than the one that was refused.
324- if ( status === 401 ) yield * credentials . invalidate ( host ) ;
325341 const problem = Option . getOrUndefined ( decodeProblem ( collected . text ) ) ;
326342 return yield * failed (
327343 problemCode ( problem ?. type , status ) ,
0 commit comments