Handle empty response when status 204
This commit is contained in:
4
apps/mina-sidor-fa/src/app/shared/models/export.model.ts
Normal file
4
apps/mina-sidor-fa/src/app/shared/models/export.model.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export interface Export {
|
||||
blob?: Blob;
|
||||
empty: boolean;
|
||||
}
|
||||
@@ -1,12 +1,13 @@
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { HttpClient, HttpResponse } from '@angular/common/http';
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Params } from '@angular/router';
|
||||
import { ErrorType } from '@msfa-enums/error-type.enum';
|
||||
import { environment } from '@msfa-environment';
|
||||
import { DeltagareExportRequest } from '@msfa-models/api/deltagare-export.request.model';
|
||||
import { CustomError } from '@msfa-models/error/custom-error';
|
||||
import { Export } from '@msfa-models/export.model';
|
||||
import { Observable } from 'rxjs';
|
||||
import { catchError } from 'rxjs/operators';
|
||||
import { catchError, map } from 'rxjs/operators';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
@@ -16,15 +17,22 @@ export class ExportApiService {
|
||||
|
||||
constructor(private httpClient: HttpClient) {}
|
||||
|
||||
public fetchDeltagareExportFile$(requestData: DeltagareExportRequest): Observable<Blob> {
|
||||
public fetchDeltagareExportFile$(requestData: DeltagareExportRequest): Observable<Export> {
|
||||
const params: Params = {
|
||||
...requestData,
|
||||
includeExportedDeltagare: requestData.includeExportedDeltagare.toString(),
|
||||
};
|
||||
|
||||
return this.httpClient
|
||||
.get<Blob>(`${this._apiBaseUrl}/deltagare`, { params, responseType: 'blob' as 'json' })
|
||||
.get(`${this._apiBaseUrl}/deltagare`, {
|
||||
params,
|
||||
observe: 'response',
|
||||
responseType: 'blob' as 'json',
|
||||
})
|
||||
.pipe(
|
||||
map((response: HttpResponse<Blob>) => {
|
||||
return response.status === 204 ? { empty: true } : { empty: false, blob: response.body };
|
||||
}),
|
||||
catchError((error: Error) => {
|
||||
throw new CustomError({
|
||||
error,
|
||||
|
||||
Reference in New Issue
Block a user