Skip to content

Commit

Permalink
fix(@nguniversal/common): update TransferHttpResponse requiring body …
Browse files Browse the repository at this point in the history
…and headers

As per the discussion and the nice point made by @alan-agius4 regarding the TransferHttpResponse shape under angular/angular#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.

(cherry picked from commit 654ac18)
  • Loading branch information
bougwal authored and alan-agius4 committed Apr 12, 2023
1 parent b4dcb00 commit 71c2cef
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import { filter, take, tap } from 'rxjs/operators';
type ResponseType = HttpRequest<unknown>['responseType'];

interface TransferHttpResponse {
body?: any | null;
headers?: Record<string, string[]>;
body: any;
headers: Record<string, string[]>;
status?: number;
statusText?: string;
url?: string;
Expand Down Expand Up @@ -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...
Expand All @@ -102,10 +102,10 @@ export class TransferHttpCacheInterceptor implements HttpInterceptor {
return of(
new HttpResponse<any>({
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,
}),
);
}
Expand Down
20 changes: 10 additions & 10 deletions modules/common/src/transfer_http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ import { defaultIfEmpty, first, tap } from 'rxjs/operators';

type ResponseType = HttpRequest<unknown>['responseType'];

export interface TransferHttpResponse {
body?: any | null;
headers?: Record<string, string[]>;
interface TransferHttpResponse {
body: any;
headers: Record<string, string[]>;
status?: number;
statusText?: string;
url?: string;
Expand Down Expand Up @@ -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;
Expand All @@ -111,10 +111,10 @@ export class TransferHttpCacheInterceptor implements HttpInterceptor {
return observableOf(
new HttpResponse<any>({
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 {
Expand Down

0 comments on commit 71c2cef

Please sign in to comment.