src/lib/src/view-type-strategy.ts
Helper interface with subset of HttpResponse fields, for better abstraction.
Properties |
body |
body:
|
Type : any
|
Defined in src/lib/src/view-type-strategy.ts:20
|
The response body, or |
headers |
headers:
|
Type : ReadOnlyHeaders
|
Defined in src/lib/src/view-type-strategy.ts:12
|
All response headers. |
status |
status:
|
Type : number
|
Defined in src/lib/src/view-type-strategy.ts:16
|
Response status code. |
import { Injectable } from '@angular/core';
import { normalizeMediaType } from './utils/normalize-media-type';
import { ReadOnlyHeaders } from './read-only-headers';
/**
* Helper interface with subset of HttpResponse fields, for better abstraction.
*/
export interface ViewTypeResponse {
/**
* All response headers.
*/
readonly headers: ReadOnlyHeaders;
/**
* Response status code.
*/
readonly status: number;
/**
* The response body, or `null` if one was not returned.
*/
readonly body: any;
}
export abstract class ViewTypeStrategy {
/**
* Extracts type from the server response, understandable by application.
* Default implementation uses `Content-Type` header.
*
* @param response Actual response.
* @returns Found response type. Null if not found.
*/
abstract extractType(response: ViewTypeResponse): string | null;
}
/**
* Extracts type from the HTTP header. By default its `Content-Type`.
*/
@Injectable()
export class HeaderViewTypeStrategy implements ViewTypeStrategy {
protected headerName = 'content-type';
extractType(response: ViewTypeResponse): string | null {
const contentType = response.headers ? response.headers.get(this.headerName) : null;
return contentType ? this.normalizeMediaType(contentType) : null;
}
//noinspection JSMethodCanBeStatic
normalizeMediaType(contentType: string): string {
// This is overridable
return normalizeMediaType(contentType);
}
}