From 654ac18444c836552a8a00dd7c7d785d9261fe08 Mon Sep 17 00:00:00 2001 From: bougwal Date: Sat, 1 Apr 2023 15:50:13 +0200 Subject: [PATCH] fix(@nguniversal/common): update TransferHttpResponse requiring body and headers As per the discussion and the nice point made by @alan-agius4 regarding the TransferHttpResponse shape under https://github.com/angular/angular/pull/49509. I suggest to align the status of optional and required keys of TransferHttpResponse interface of universal's interceptor to meet Angular's packages/common/http/src/transfer_cache.ts counterpart. --- .../transfer-http-cache.interceptor.ts | 18 ++++++++--------- modules/common/src/transfer_http.ts | 20 +++++++++---------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/modules/common/clover/src/transfer-http-cache/transfer-http-cache.interceptor.ts b/modules/common/clover/src/transfer-http-cache/transfer-http-cache.interceptor.ts index 51008acd1..8c9916503 100644 --- a/modules/common/clover/src/transfer-http-cache/transfer-http-cache.interceptor.ts +++ b/modules/common/clover/src/transfer-http-cache/transfer-http-cache.interceptor.ts @@ -22,8 +22,8 @@ import { filter, take, tap } from 'rxjs/operators'; type ResponseType = HttpRequest['responseType']; interface TransferHttpResponse { - body?: any | null; - headers?: Record; + body: any; + headers: Record; status?: number; statusText?: string; url?: string; @@ -74,10 +74,10 @@ export class TransferHttpCacheInterceptor implements HttpInterceptor { if (this.transferState.hasKey(storeKey)) { // Request found in cache. Respond using it. - const response = this.transferState.get(storeKey, {}); - let body: ArrayBuffer | Blob | string | undefined = response.body; + const response = this.transferState.get(storeKey, null); + let body: ArrayBuffer | Blob | string | undefined = response?.body; - switch (response.responseType) { + switch (response?.responseType) { case 'arraybuffer': { // If we're in Node... @@ -102,10 +102,10 @@ export class TransferHttpCacheInterceptor implements HttpInterceptor { return of( new HttpResponse({ body, - headers: new HttpHeaders(response.headers), - status: response.status, - statusText: response.statusText, - url: response.url, + headers: new HttpHeaders(response?.headers), + status: response?.status, + statusText: response?.statusText, + url: response?.url, }), ); } diff --git a/modules/common/src/transfer_http.ts b/modules/common/src/transfer_http.ts index 9cc346daa..18e3d96f9 100644 --- a/modules/common/src/transfer_http.ts +++ b/modules/common/src/transfer_http.ts @@ -29,9 +29,9 @@ import { defaultIfEmpty, first, tap } from 'rxjs/operators'; type ResponseType = HttpRequest['responseType']; -export interface TransferHttpResponse { - body?: any | null; - headers?: Record; +interface TransferHttpResponse { + body: any; + headers: Record; status?: number; statusText?: string; url?: string; @@ -96,10 +96,10 @@ export class TransferHttpCacheInterceptor implements HttpInterceptor { if (this.transferState.hasKey(storeKey)) { // Request found in cache. Respond using it. - const response = this.transferState.get(storeKey, {}); - let body: ArrayBuffer | Blob | string | undefined = response.body; + const response = this.transferState.get(storeKey, null); + let body: ArrayBuffer | Blob | string | undefined = response?.body; - switch (response.responseType) { + switch (response?.responseType) { case 'arraybuffer': body = new TextEncoder().encode(response.body).buffer; break; @@ -111,10 +111,10 @@ export class TransferHttpCacheInterceptor implements HttpInterceptor { return observableOf( new HttpResponse({ body, - headers: new HttpHeaders(response.headers), - status: response.status, - statusText: response.statusText, - url: response.url, + headers: new HttpHeaders(response?.headers), + status: response?.status, + statusText: response?.statusText, + url: response?.url, }), ); } else {