From b0bbddcf9f94a1ecf87f79646d9326872dcb5525 Mon Sep 17 00:00:00 2001 From: kpawelczak <42094017+kpawelczak@users.noreply.github.com> Date: Thu, 9 Nov 2023 16:57:49 +0100 Subject: [PATCH] fix Blob is not defined In SSR mode (#18033) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: github-actions[bot] Co-authored-by: Paweł Fraś --- .../blob-error.interceptor.spec.ts | 46 +++++++++++++++---- .../blob-error.interceptor.ts | 7 ++- .../root/http-interceptors/index.ts | 7 +++ .../account-summary/root/public_api.ts | 1 + 4 files changed, 50 insertions(+), 11 deletions(-) create mode 100644 feature-libs/organization/account-summary/root/http-interceptors/index.ts diff --git a/feature-libs/organization/account-summary/root/http-interceptors/blob-error.interceptor.spec.ts b/feature-libs/organization/account-summary/root/http-interceptors/blob-error.interceptor.spec.ts index 9ad2928f1aa..0b4ceef5957 100644 --- a/feature-libs/organization/account-summary/root/http-interceptors/blob-error.interceptor.spec.ts +++ b/feature-libs/organization/account-summary/root/http-interceptors/blob-error.interceptor.spec.ts @@ -5,8 +5,8 @@ */ import { - HttpClient, HTTP_INTERCEPTORS, + HttpClient, HttpErrorResponse, } from '@angular/common/http'; import { @@ -18,10 +18,20 @@ import { TestBed } from '@angular/core/testing'; import { FileReaderService } from '@spartacus/storefront'; import { take } from 'rxjs/operators'; import { BlobErrorInterceptor } from './blob-error.interceptor'; +import { WindowRef } from '@spartacus/core'; + +const errors = JSON.stringify({ + errors: [{ type: 'InvalidTokenError' }], +}); + +const error = new Blob([errors], { + type: 'application/json', +}); describe('BlobErrorInterceptor', () => { let httpMock: HttpTestingController; let http: HttpClient; + let windowRef: WindowRef; beforeEach(() => { TestBed.configureTestingModule({ @@ -38,9 +48,12 @@ describe('BlobErrorInterceptor', () => { httpMock = TestBed.inject(HttpTestingController); http = TestBed.inject(HttpClient); + windowRef = TestBed.inject(WindowRef); }); - it(`Should extract JSON from errors wrapped in blob`, (done: DoneFn) => { + it(`Should extract json from errors wrapped in blob`, (done) => { + spyOn(windowRef, 'isBrowser').and.returnValue(true); + http .get('/occ', { responseType: 'blob' as 'json' }) .pipe(take(1)) @@ -48,7 +61,6 @@ describe('BlobErrorInterceptor', () => { error: (err: HttpErrorResponse) => { expect(err.status).toEqual(401); expect(err.error.errors[0].type).toEqual('InvalidTokenError'); - done(); }, }); @@ -56,14 +68,30 @@ describe('BlobErrorInterceptor', () => { return req.method === 'GET' && req.url === '/occ'; }); - const errors = { errors: [{ type: 'InvalidTokenError' }] }; - const errorBlob = new Blob([JSON.stringify(errors)], { - type: 'application/json', - }); - - mockReq.flush(errorBlob, { + mockReq.flush(error, { status: 401, statusText: 'Unauthorized', }); + + expect(windowRef.isBrowser).toHaveBeenCalled(); + done(); + }); + + it(`Should extract json from errors wrapped in blob`, (done) => { + spyOn(windowRef, 'isBrowser').and.returnValue(false); + + http + .get('/occ', { responseType: 'blob' as 'json' }) + .pipe(take(1)) + .subscribe(); + + const mockReq: TestRequest = httpMock.expectOne((req) => { + return req.method === 'GET' && req.url === '/occ'; + }); + + mockReq.flush(error); + + expect(windowRef.isBrowser).not.toHaveBeenCalled(); + done(); }); }); diff --git a/feature-libs/organization/account-summary/root/http-interceptors/blob-error.interceptor.ts b/feature-libs/organization/account-summary/root/http-interceptors/blob-error.interceptor.ts index 1c41703b52e..1de0d88ab43 100644 --- a/feature-libs/organization/account-summary/root/http-interceptors/blob-error.interceptor.ts +++ b/feature-libs/organization/account-summary/root/http-interceptors/blob-error.interceptor.ts @@ -11,8 +11,9 @@ import { HttpInterceptor, HttpRequest, } from '@angular/common/http'; -import { Injectable } from '@angular/core'; +import { inject, Injectable } from '@angular/core'; import { FileReaderService } from '@spartacus/storefront'; +import { WindowRef } from '@spartacus/core'; import { Observable, throwError } from 'rxjs'; import { catchError, switchMap } from 'rxjs/operators'; @@ -20,7 +21,8 @@ import { catchError, switchMap } from 'rxjs/operators'; providedIn: 'root', }) export class BlobErrorInterceptor implements HttpInterceptor { - constructor(private fileReaderService: FileReaderService) {} + protected readonly fileReaderService = inject(FileReaderService); + protected readonly windowRef = inject(WindowRef); intercept( request: HttpRequest, @@ -29,6 +31,7 @@ export class BlobErrorInterceptor implements HttpInterceptor { return next.handle(request).pipe( catchError((errResponse: any) => { if ( + this.windowRef.isBrowser() && errResponse instanceof HttpErrorResponse && errResponse.error instanceof Blob && errResponse.error.type === 'application/json' diff --git a/feature-libs/organization/account-summary/root/http-interceptors/index.ts b/feature-libs/organization/account-summary/root/http-interceptors/index.ts new file mode 100644 index 00000000000..d134b9581ca --- /dev/null +++ b/feature-libs/organization/account-summary/root/http-interceptors/index.ts @@ -0,0 +1,7 @@ +/* + * SPDX-FileCopyrightText: 2023 SAP Spartacus team + * + * SPDX-License-Identifier: Apache-2.0 + */ + +export * from './blob-error.interceptor'; diff --git a/feature-libs/organization/account-summary/root/public_api.ts b/feature-libs/organization/account-summary/root/public_api.ts index e7fd5624b9f..067b1e552b1 100644 --- a/feature-libs/organization/account-summary/root/public_api.ts +++ b/feature-libs/organization/account-summary/root/public_api.ts @@ -9,3 +9,4 @@ export * from './config/index'; export * from './facade/index'; export * from './feature-name'; export * from './model/index'; +export * from './http-interceptors';