/
requestPolicy.ts
98 lines (86 loc) · 3.46 KB
/
requestPolicy.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { HttpOperationResponse } from "../httpOperationResponse";
import { HttpPipelineLogger } from "../httpPipelineLogger";
import { HttpPipelineLogLevel } from "../httpPipelineLogLevel";
import { WebResourceLike } from "../webResource";
/**
* Creates a new RequestPolicy per-request that uses the provided nextPolicy.
*/
export type RequestPolicyFactory = {
create(nextPolicy: RequestPolicy, options: RequestPolicyOptionsLike): RequestPolicy;
};
export interface RequestPolicy {
sendRequest(httpRequest: WebResourceLike): Promise<HttpOperationResponse>;
}
export abstract class BaseRequestPolicy implements RequestPolicy {
protected constructor(
readonly _nextPolicy: RequestPolicy,
readonly _options: RequestPolicyOptionsLike
) {}
public abstract sendRequest(webResource: WebResourceLike): Promise<HttpOperationResponse>;
/**
* Get whether or not a log with the provided log level should be logged.
* @param logLevel - The log level of the log that will be logged.
* @returns Whether or not a log with the provided log level should be logged.
*/
public shouldLog(logLevel: HttpPipelineLogLevel): boolean {
return this._options.shouldLog(logLevel);
}
/**
* Attempt to log the provided message to the provided logger. If no logger was provided or if
* the log level does not meat the logger's threshold, then nothing will be logged.
* @param logLevel - The log level of this log.
* @param message - The message of this log.
*/
public log(logLevel: HttpPipelineLogLevel, message: string): void {
this._options.log(logLevel, message);
}
}
/**
* Optional properties that can be used when creating a RequestPolicy.
*/
export interface RequestPolicyOptionsLike {
/**
* Get whether or not a log with the provided log level should be logged.
* @param logLevel - The log level of the log that will be logged.
* @returns Whether or not a log with the provided log level should be logged.
*/
shouldLog(logLevel: HttpPipelineLogLevel): boolean;
/**
* Attempt to log the provided message to the provided logger. If no logger was provided or if
* the log level does not meet the logger's threshold, then nothing will be logged.
* @param logLevel - The log level of this log.
* @param message - The message of this log.
*/
log(logLevel: HttpPipelineLogLevel, message: string): void;
}
/**
* Optional properties that can be used when creating a RequestPolicy.
*/
export class RequestPolicyOptions {
constructor(private _logger?: HttpPipelineLogger) {}
/**
* Get whether or not a log with the provided log level should be logged.
* @param logLevel - The log level of the log that will be logged.
* @returns Whether or not a log with the provided log level should be logged.
*/
public shouldLog(logLevel: HttpPipelineLogLevel): boolean {
return (
!!this._logger &&
logLevel !== HttpPipelineLogLevel.OFF &&
logLevel <= this._logger.minimumLogLevel
);
}
/**
* Attempt to log the provided message to the provided logger. If no logger was provided or if
* the log level does not meet the logger's threshold, then nothing will be logged.
* @param logLevel - The log level of this log.
* @param message - The message of this log.
*/
public log(logLevel: HttpPipelineLogLevel, message: string): void {
if (this._logger && this.shouldLog(logLevel)) {
this._logger.log(logLevel, message);
}
}
}