Skip to content

Instantly share code, notes, and snippets.

@tw3
Created November 15, 2019 19:02
Show Gist options
  • Save tw3/714dfd6a58a69f89aeb1727e79093ddb to your computer and use it in GitHub Desktop.
Save tw3/714dfd6a58a69f89aeb1727e79093ddb to your computer and use it in GitHub Desktop.
Example usage of InterceptorRequestConfigSerializer to ignore certain response status codes for a request
import { HttpRequest } from '@angular/common/http';
import { isNullOrUndefined } from '../../util/misc-util';
import { isPositiveInteger } from '../../util/num-util';
import { InterceptorRequestConfigSerializer, PullInterceptorConfigResult } from '../entities/interceptor-request-config-serializer';
import { HttpClientOptions } from '../entities/http-client-options';
export interface ErrorNotificationInterceptorConfig {
ignoredResStatusCodes: number[];
}
export class ErrorNotificationInterceptorConfigSerializer {
private static readonly INTERCEPTOR_CONFIG_NAME = 'ErrorNotificationInterceptorConfig';
static pullConfigFromRequest(req: HttpRequest<any>): PullInterceptorConfigResult<ErrorNotificationInterceptorConfig> { // NOSONAR
return InterceptorRequestConfigSerializer.pullConfig<ErrorNotificationInterceptorConfig>(req, this.INTERCEPTOR_CONFIG_NAME);
}
static setConfigInHttpOptions(config: ErrorNotificationInterceptorConfig, options: HttpClientOptions = {}): object | undefined {
config.ignoredResStatusCodes = this.getSanitizedCodes(config.ignoredResStatusCodes);
return InterceptorRequestConfigSerializer.setConfig(options, this.INTERCEPTOR_CONFIG_NAME, config);
}
private static getSanitizedCodes(codes: number[]): number[] | undefined {
if (isNullOrUndefined(codes)) {
return codes;
}
// Convert to any array if necessary
if (!Array.isArray(codes)) {
codes = [codes];
}
// Filter out invalid codes
codes = codes.filter(code => isPositiveInteger(code));
return codes;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment