@@ -60,6 +60,10 @@ type ResponseHandlerExtendsType<T> = Partial<Record<'success' | StatusCodesSucce
6060type VoidToUndefinedReturn < T extends ( ...args : any ) => any > =
6161 ReturnType < T > extends void ? ( ...args : Parameters < T > ) => undefined : T ;
6262
63+ export interface Abortable {
64+ abort ( ) : void ;
65+ }
66+
6367/**
6468 * The response handler is a class that allows you to handle the response of a request to the API.
6569 * It allows you to define what to do when the request is successful, when it returns an error (an API error), when it fails (a request error), or when it returns a specific status code or specific failure.
@@ -84,11 +88,16 @@ type VoidToUndefinedReturn<T extends (...args: any) => any> =
8488 * .toPromise();
8589 * }
8690 */
87- export class ResponseHandler < T , R extends ResponseHandlerExtendsType < T > = { fallback : HandlerNoParam < undefined > } > {
91+ export class ResponseHandler < T , R extends ResponseHandlerExtendsType < T > = { fallback : HandlerNoParam < undefined > } >
92+ implements Abortable
93+ {
8894 private readonly handlers = { fallback : ( ) => undefined } as R ;
8995 private readonly promise : Promise < ReturnType < R [ keyof R ] extends ( ...args : any ) => any ? R [ keyof R ] : never > > ;
9096
91- constructor ( rawResponse : Promise < APIResponse < T > > ) {
97+ constructor (
98+ rawResponse : Promise < APIResponse < T > > ,
99+ private readonly abortController : AbortController ,
100+ ) {
92101 this . promise = rawResponse . then ( ( response ) => {
93102 if ( 'failureReason' in response ) {
94103 if ( this . handlers [ response . failureReason ] ) {
@@ -152,6 +161,14 @@ export class ResponseHandler<T, R extends ResponseHandlerExtendsType<T> = { fall
152161 > ;
153162 }
154163
164+ /**
165+ * Aborts the request that produces this reponse.
166+ * Fires ResponseError.timeout handler or falls back to failure handler.
167+ */
168+ abort ( ) {
169+ this . abortController . abort ( ) ;
170+ }
171+
155172 async toPromise ( ) : Promise < Awaited < ReturnType < R [ keyof R ] extends ( ...args : any ) => any ? R [ keyof R ] : never > > > {
156173 return await this . promise ;
157174 }
@@ -193,6 +210,7 @@ async function internalRequestAPI<RequestType>(
193210 version : string ,
194211 isFile : true ,
195212 applicationId : string ,
213+ abortController : AbortController ,
196214) : Promise < APIResponse < Blob > > ;
197215async function internalRequestAPI < RequestType , ResponseType > (
198216 method : string ,
@@ -202,6 +220,7 @@ async function internalRequestAPI<RequestType, ResponseType>(
202220 version : string ,
203221 isFile : boolean ,
204222 applicationId : string ,
223+ abortController : AbortController ,
205224) : Promise < APIResponse < ResponseType > > ;
206225async function internalRequestAPI < RequestType , ResponseType > (
207226 method : string ,
@@ -211,6 +230,7 @@ async function internalRequestAPI<RequestType, ResponseType>(
211230 version : string ,
212231 isFile : boolean ,
213232 applicationId : string ,
233+ abortController : AbortController ,
214234) : Promise < APIResponse < ResponseType | Blob > > {
215235 // Generate headers
216236 const headers = new Headers ( ) ;
@@ -219,7 +239,6 @@ async function internalRequestAPI<RequestType, ResponseType>(
219239 if ( ! isFile ) headers . append ( 'Content-Type' , 'application/json' ) ;
220240
221241 // Add timeout to the request
222- const abortController = new AbortController ( ) ;
223242 const timeout = setTimeout ( ( ) => {
224243 abortController . abort ( ) ;
225244 } , timeoutMillis ) ;
@@ -309,7 +328,11 @@ function requestAPI<RequestType, ResponseType>(
309328 applicationId = etuuttWebApplicationId ,
310329 } : { timeoutMillis ?: number ; version ?: string ; isFile ?: boolean ; applicationId ?: string } = { } ,
311330) : ResponseHandler < ResponseType > {
312- return new ResponseHandler ( internalRequestAPI ( method , route , body , timeoutMillis , version , isFile , applicationId ) ) ;
331+ const abortController = new AbortController ( ) ;
332+ return new ResponseHandler (
333+ internalRequestAPI ( method , route , body , timeoutMillis , version , isFile , applicationId , abortController ) ,
334+ abortController ,
335+ ) ;
313336}
314337
315338// Set the authorization header with the given token for next requests
0 commit comments