1- import { HttpClient , HttpContext , HttpHeaders , HttpParams } from '@angular/common/http' ;
1+ import { HttpClient , HttpContext , HttpHeaders , HttpParams , HttpResponse } from '@angular/common/http' ;
22import { inject , Injectable } from '@angular/core' ;
33import { defer , finalize , Observable } from 'rxjs' ;
44import { ApiLoadingService } from './api-loading.service' ;
@@ -99,6 +99,30 @@ export class ApiService {
9999 delete < T > ( endpoint : string , options ?: ApiRequestOptions ) : Observable < T > {
100100 return this . request < T > ( 'DELETE' , endpoint , null , options ) ;
101101 }
102+
103+ /**
104+ * Downloads a binary file by performing a `GET` request to the specified API endpoint
105+ * with optional request options.
106+ *
107+ * @param endpoint API endpoint (e.g. `'/resource/{id}'`).
108+ * @param options Additional request options (query params, headers, etc.).
109+ * @returns An `Observable` of the downloaded file as a `Blob`.
110+ */
111+ download ( endpoint : string , options ?: ApiRequestOptions ) : Observable < Blob > {
112+ return this . request < Blob > ( 'GET' , endpoint , null , options , 'blob' ) ;
113+ }
114+
115+ /**
116+ * Downloads a binary file and returns the complete HTTP response, including
117+ * response metadata such as headers and the filename from the `Content-Disposition` header.
118+ *
119+ * @param endpoint API endpoint (e.g. `'/resource/{id}'`).
120+ * @param options Additional request options (query params, headers, etc.).
121+ * @returns An `Observable` of the complete HTTP response containing the downloaded file as a `Blob`.
122+ */
123+ downloadResponse ( endpoint : string , options ?: ApiRequestOptions ) : Observable < HttpResponse < Blob > > {
124+ return this . request < HttpResponse < Blob > > ( 'GET' , endpoint , null , options , 'blob' , true ) ;
125+ }
102126
103127 /**
104128 * Convenience method for paginated `GET` requests.
@@ -138,13 +162,16 @@ export class ApiService {
138162 * @param endpoint API endpoint (e.g. `'/resource'`).
139163 * @param body Request body (for POST, PUT, PATCH).
140164 * @param options Additional request options (query params, headers, flags for interceptors, etc.).
141- * @returns An `Observable` of the response body typed as `T`.
165+ * @param observeResponse Whether to return the complete HTTP response, including headers and other metadata.
166+ * @returns An `Observable` of the response typed as `T`.
142167 */
143168 private request < T > (
144169 method : string ,
145170 endpoint : string ,
146171 body : unknown ,
147172 options ?: ApiRequestOptions ,
173+ responseType : 'json' | 'blob' = 'json' ,
174+ observeResponse = false ,
148175 ) : Observable < T > {
149176 const showLoader = options ?. showLoader ?? this . defaultShowLoader ;
150177
@@ -157,12 +184,16 @@ export class ApiService {
157184 this . loadingService . start ( ) ;
158185 }
159186
160- return this . http . request < T > ( method , url , {
187+ const requestOptions = {
161188 body,
162189 params : this . buildParams ( options , versioning ) ,
163190 headers : this . buildHeaders ( options , versioning ) ,
164191 context,
165- } ) ;
192+ responseType,
193+ ...( observeResponse ? { observe : 'response' as const } : { } ) ,
194+ } ;
195+
196+ return this . http . request ( method , url , requestOptions ) as Observable < T > ;
166197 } ) . pipe (
167198 finalize ( ( ) => {
168199 if ( showLoader ) {
0 commit comments