@@ -44,12 +44,20 @@ export interface AuthorizationCodeLoginInput {
4444 readonly code : string ;
4545 readonly redirectUri : string ;
4646 readonly codeVerifier : string ;
47+ readonly signal ?: AbortSignal ;
4748}
4849
4950export interface RefreshTokenInput {
5051 readonly profile : string ;
5152 readonly clientId : string ;
5253 readonly tokenUrl : string ;
54+ readonly signal ?: AbortSignal ;
55+ }
56+
57+ export interface OpenSessionOptions {
58+ readonly profile ?: string ;
59+ readonly timeoutMs ?: number ;
60+ readonly signal ?: AbortSignal ;
5361}
5462
5563export interface ActiveSession {
@@ -59,6 +67,26 @@ export interface ActiveSession {
5967 readonly credentials : StoredCredentials ;
6068}
6169
70+ function resolveRequestSignal ( options ?: OpenSessionOptions ) : AbortSignal | undefined {
71+ if ( ! options ) {
72+ return undefined ;
73+ }
74+ const signals : AbortSignal [ ] = [ ] ;
75+ if ( options . signal ) {
76+ signals . push ( options . signal ) ;
77+ }
78+ if ( typeof options . timeoutMs === "number" && options . timeoutMs > 0 ) {
79+ signals . push ( AbortSignal . timeout ( options . timeoutMs ) ) ;
80+ }
81+ if ( signals . length === 0 ) {
82+ return undefined ;
83+ }
84+ if ( signals . length === 1 ) {
85+ return signals [ 0 ] ;
86+ }
87+ return AbortSignal . any ( signals ) ;
88+ }
89+
6290function toStoredCredentials ( token : OAuthToken ) : StoredCredentials {
6391 return {
6492 accessToken : token . accessToken ,
@@ -138,6 +166,7 @@ export class AuthManager {
138166 code : input . code ,
139167 redirectUri : input . redirectUri ,
140168 codeVerifier : input . codeVerifier ,
169+ signal : input . signal ,
141170 } ) ;
142171
143172 await this . loginWithToken ( {
@@ -163,6 +192,7 @@ export class AuthManager {
163192 clientId : input . clientId ,
164193 tokenUrl : input . tokenUrl ,
165194 refreshToken : existing . refreshToken ,
195+ signal : input . signal ,
166196 } ) ;
167197
168198 await store . set ( input . profile , {
@@ -210,13 +240,14 @@ export class AuthManager {
210240 } ;
211241 }
212242
213- public async openSession ( options ?: { readonly profile ?: string } ) : Promise < ActiveSession > {
243+ public async openSession ( options ?: OpenSessionOptions ) : Promise < ActiveSession > {
214244 const config = await this . configStore . load ( ) ;
215245 const selectedProfile = options ?. profile ?? config . defaultProfile ;
216246 const selected = config . profiles [ selectedProfile ] ;
217247 const store = await this . credentialsStore ( ) ;
218248 const credentials = await store . get ( selectedProfile ) ;
219249 const oauthConfig = selected ?. oauth ;
250+ const requestSignal = resolveRequestSignal ( options ) ;
220251
221252 if ( credentials ?. accessToken ) {
222253 let token : OAuthToken = {
@@ -230,6 +261,7 @@ export class AuthManager {
230261 clientId : oauthConfig . clientId ,
231262 tokenUrl : oauthConfig . tokenUrl ,
232263 refreshToken : token . refreshToken ,
264+ signal : requestSignal ,
233265 } ) ;
234266 token = refreshed ;
235267 await store . set ( selectedProfile , {
@@ -240,6 +272,7 @@ export class AuthManager {
240272
241273 const client = new LinearClient ( {
242274 accessToken : token . accessToken ,
275+ ...( requestSignal ? { signal : requestSignal } : { } ) ,
243276 } ) ;
244277
245278 return {
@@ -255,7 +288,10 @@ export class AuthManager {
255288 }
256289
257290 if ( credentials ?. apiKey ) {
258- const client = new LinearClient ( { apiKey : credentials . apiKey } ) ;
291+ const client = new LinearClient ( {
292+ apiKey : credentials . apiKey ,
293+ ...( requestSignal ? { signal : requestSignal } : { } ) ,
294+ } ) ;
259295 return {
260296 profile : selectedProfile ,
261297 client,
@@ -268,7 +304,10 @@ export class AuthManager {
268304
269305 const envApiKey = process . env . LINEAR_API_KEY ;
270306 if ( envApiKey ) {
271- const client = new LinearClient ( { apiKey : envApiKey } ) ;
307+ const client = new LinearClient ( {
308+ apiKey : envApiKey ,
309+ ...( requestSignal ? { signal : requestSignal } : { } ) ,
310+ } ) ;
272311 return {
273312 profile : selectedProfile ,
274313 client,
0 commit comments