Created
December 18, 2018 08:46
-
-
Save dhilgarth/783ed994be6e3af0c236fc36f327c9cd to your computer and use it in GitHub Desktop.
Passing meta data for interceptors to Angular HttpClient
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class LoadingInterceptorConfig { | |
constructor(public disableLoadingIndicator: boolean = false) { | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { HttpParams } from '@angular/common/http'; | |
import { Type } from '@angular/core'; | |
export interface IWithMetaData { | |
meta: IMetaData; | |
} | |
export interface IMetaData { | |
add<T>(data: T): void, | |
get<T>(type: Type<T>): T | |
} | |
interface IMetaDataInternal { | |
metaData: { [key: string]: any } | |
} | |
function add<T>(this: IMetaDataInternal, data: T): void { | |
this.metaData[data.constructor.name] = data; | |
} | |
function get<T>(this: IMetaDataInternal, type: Type<T>): T | undefined { | |
return this.metaData[type.name]; | |
} | |
function hasMetaData(obj: any): obj is IWithMetaData { | |
return obj.meta && obj.meta.get && obj.meta.add; | |
} | |
export function getMetaData<T>(httpParams: HttpParams, type: Type<T>): T | undefined { | |
if (hasMetaData(httpParams)) { | |
return httpParams.meta.get(type); | |
} | |
return undefined; | |
} | |
export function withMetaData(httpParams: HttpParams): HttpParams & IWithMetaData { | |
const meta: IMetaData & IMetaDataInternal = { | |
add, | |
get, | |
metaData: {} | |
}; | |
return Object.assign(httpParams, {meta}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { getMetaData } from '../some/path/meta-data' | |
import { LoadingInterceptorConfig } from '../some/path/LoadingInterceptorConfig' | |
// ... | |
private overlayDisabled(req: HttpRequest<any>): boolean { | |
const config = getMetaData(req.params, LoadingInterceptorConfig); | |
return !!config && config.disableLoadingIndicator; | |
} | |
// ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { withMetaData } from '../some/path/meta-data' | |
import { LoadingInterceptorConfig } from '../some/path/LoadingInterceptorConfig' | |
// if you don't want to pass any other parameters: | |
const params = withMetaData(new HttpParams()); | |
// if you have other parameters to pass: | |
const params = withMetaData(queryParameters); | |
params.meta.add(new LoadingInterceptorConfig(this.disableLoadingIndicator)); | |
// Important: Don't perform any other operations on `params` after you added the metadata. | |
// As HttpParams is immutable, this would mean that our meta data would be lost. | |
this.httpClient.get<...>(`${this.basePath}/api/x/y`, {params}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great, happy to hear it was helpful