/
compatibility.ts
145 lines (133 loc) · 3.61 KB
/
compatibility.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { AbortSignalLike } from "@azure/abort-controller";
import {
HttpHeaders,
HttpMethods,
PipelineRequest,
PipelineResponse,
ProxySettings,
} from "@azure/core-rest-pipeline";
import { HttpHeadersLike, HttpHeaders as HttpHeadersV1 } from "./httpHeaders";
/**
* Fired in response to upload or download progress.
*/
export type TransferProgressEvent = {
/**
* The number of bytes loaded so far.
*/
loadedBytes: number;
};
/**
* A description of a HTTP request to be made to a remote server.
*/
export interface WebResourceLike {
/**
* The URL being accessed by the request.
*/
url: string;
/**
* The HTTP method to use when making the request.
*/
method: HttpMethods;
/**
* The HTTP body contents of the request.
*/
body?: any;
/**
* The HTTP headers to use when making the request.
*/
headers: HttpHeadersLike;
/**
* Whether or not the body of the HttpOperationResponse should be treated as a stream.
* @deprecated Use streamResponseStatusCodes property instead.
*/
streamResponseBody?: boolean;
/**
* A list of response status codes whose corresponding HttpOperationResponse body should be treated as a stream.
*/
streamResponseStatusCodes?: Set<number>;
/**
* Form data, used to build the request body.
*/
formData?: any;
/**
* A query string represented as an object.
*/
query?: { [key: string]: any };
/**
* If credentials (cookies) should be sent along during an XHR.
*/
withCredentials: boolean;
/**
* The number of milliseconds a request can take before automatically being terminated.
* If the request is terminated, an `AbortError` is thrown.
*/
timeout: number;
/**
* Proxy configuration.
*/
proxySettings?: ProxySettings;
/**
* If the connection should be reused.
*/
keepAlive?: boolean;
/**
* Whether or not to decompress response according to Accept-Encoding header (node-fetch only)
*/
decompressResponse?: boolean;
/**
* A unique identifier for the request. Used for logging and tracing.
*/
requestId: string;
/**
* Signal of an abort controller. Can be used to abort both sending a network request and waiting for a response.
*/
abortSignal?: AbortSignalLike;
/**
* Callback which fires upon upload progress.
*/
onUploadProgress?: (progress: TransferProgressEvent) => void;
/** Callback which fires upon download progress. */
onDownloadProgress?: (progress: TransferProgressEvent) => void;
}
/**
* The properties on an HTTP response which will always be present.
*/
export interface HttpResponse {
/**
* The raw request
*/
request: WebResourceLike;
/**
* The HTTP response status (e.g. 200)
*/
status: number;
/**
* The HTTP response headers.
*/
headers: HttpHeadersLike;
}
function toHttpHeaderLike(headers: HttpHeaders): HttpHeadersLike {
return new HttpHeadersV1(headers.toJSON({ preserveCase: true }));
}
function toWebResourceLike(request: PipelineRequest): WebResourceLike {
return {
url: request.url,
method: request.method,
headers: toHttpHeaderLike(request.headers),
withCredentials: request.withCredentials,
timeout: request.timeout,
requestId: request.headers.get("x-ms-client-request-id") || "",
};
}
/**
* Helper to transform PipelineResponse to slimmed-down HttpResponse used in Service Bus.
*/
export function toHttpResponse(response: PipelineResponse): HttpResponse {
return {
request: toWebResourceLike(response.request),
status: response.status,
headers: toHttpHeaderLike(response.headers),
};
}