forked from pnp/cli-microsoft365
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.ts
More file actions
199 lines (182 loc) · 6.76 KB
/
request.ts
File metadata and controls
199 lines (182 loc) · 6.76 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import Axios, { AxiosError, AxiosInstance, AxiosPromise, AxiosRequestConfig, AxiosResponse } from 'axios';
import { Stream } from 'stream';
import auth, { Auth } from './Auth';
import { Logger } from './cli';
import Utils from './Utils';
const packageJSON = require('../package.json');
class Request {
private req: AxiosInstance;
private _logger?: Logger;
private _debug: boolean = false;
public set debug(debug: boolean) {
// if the value to set is the same as current value return early to avoid
// instantiating interceptors multiple times. This can happen when calling
// one command from another
if (this._debug === debug) {
return;
}
this._debug = debug;
if (this._debug) {
this.req.interceptors.request.use((config: AxiosRequestConfig): AxiosRequestConfig => {
if (this._logger) {
this._logger.logToStderr('Request:');
const properties: string[] = ['url', 'method', 'headers', 'responseType', 'decompress'];
if (config.responseType !== 'stream') {
properties.push('data');
}
this._logger.logToStderr(JSON.stringify(Utils.filterObject(config, properties), null, 2));
}
return config;
});
// since we're stubbing requests, response interceptor is never called in
// tests, so let's exclude it from coverage
/* c8 ignore next 22 */
this.req.interceptors.response.use((response: AxiosResponse): AxiosResponse => {
if (this._logger) {
this._logger.logToStderr('Response:');
const properties: string[] = ['status', 'statusText', 'headers'];
if (response.headers['content-type'] &&
response.headers['content-type'].indexOf('json') > -1) {
properties.push('data');
}
this._logger.logToStderr(JSON.stringify(Utils.filterObject(response, properties), null, 2));
}
return response;
}, (error: AxiosError): void => {
if (this._logger) {
const properties: string[] = ['status', 'statusText', 'headers'];
this._logger.logToStderr('Request error:');
this._logger.logToStderr(JSON.stringify({
...Utils.filterObject(error.response, properties),
error: (error as any).error
}, null, 2));
}
throw error;
});
}
}
public set logger(logger: Logger) {
this._logger = logger;
}
constructor() {
this.req = Axios.create({
headers: {
'user-agent': `NONISV|SharePointPnP|CLIMicrosoft365/${packageJSON.version}`,
'accept-encoding': 'gzip, deflate'
},
decompress: true,
responseType: 'text',
/* c8 ignore next */
transformResponse: [data => data]
});
// since we're stubbing requests, request interceptor is never called in
// tests, so let's exclude it from coverage
/* c8 ignore next 7 */
this.req.interceptors.request.use((config: AxiosRequestConfig): AxiosRequestConfig => {
if (config.responseType === 'json') {
config.transformResponse = Axios.defaults.transformResponse;
}
return config;
});
// since we're stubbing requests, response interceptor is never called in
// tests, so let's exclude it from coverage
/* c8 ignore next 15 */
this.req.interceptors.response.use(
(response: AxiosResponse) => response,
(error: AxiosError): void => {
if (error &&
error.response &&
error.response.data &&
!(error.response.data instanceof Stream)) {
// move error details from response.data to error property to make
// it compatible with our code
(error as any).error = JSON.parse(JSON.stringify(error.response.data));
}
throw error;
}
);
}
public post<TResponse>(options: AxiosRequestConfig): Promise<TResponse> {
options.method = 'POST';
return this.execute(options);
}
public get<TResponse>(options: AxiosRequestConfig): Promise<TResponse> {
options.method = 'GET';
return this.execute(options);
}
public patch<TResponse>(options: AxiosRequestConfig): Promise<TResponse> {
options.method = 'PATCH';
return this.execute(options);
}
public put<TResponse>(options: AxiosRequestConfig): Promise<TResponse> {
options.method = 'PUT';
return this.execute(options);
}
public delete<TResponse>(options: AxiosRequestConfig): Promise<TResponse> {
options.method = 'DELETE';
return this.execute(options);
}
public head<TResponse>(options: AxiosRequestConfig): Promise<TResponse> {
options.method = 'HEAD';
return this.execute(options);
}
private execute<TResponse>(options: AxiosRequestConfig, resolve?: (res: TResponse) => void, reject?: (error: any) => void): Promise<TResponse> {
if (!this._logger) {
return Promise.reject('Logger not set on the request object');
}
return new Promise<TResponse>((_resolve: (res: TResponse) => void, _reject: (error: any) => void): void => {
((): Promise<string> => {
if (options.headers && options.headers['x-anonymous']) {
return Promise.resolve('');
}
else {
const resource: string = Auth.getResourceFromUrl(options.url as string);
return auth.ensureAccessToken(resource, this._logger as Logger, this._debug);
}
})()
.then((accessToken: string): AxiosPromise<TResponse> => {
if (options.headers) {
if (options.headers['x-anonymous']) {
delete options.headers['x-anonymous'];
}
else {
options.headers.authorization = `Bearer ${accessToken}`;
}
}
return this.req(options);
})
.then((res: any): void => {
if (resolve) {
resolve(options.responseType === 'stream' ? res : res.data);
}
else {
_resolve(options.responseType === 'stream' ? res : res.data);
}
}, (error: AxiosError): void => {
if (error && error.response &&
(error.response.status === 429 ||
error.response.status === 503)) {
let retryAfter: number = parseInt(error.response.headers['retry-after'] || '10');
if (isNaN(retryAfter)) {
retryAfter = 10;
}
if (this._debug) {
(this._logger as Logger).log(`Request throttled. Waiting ${retryAfter}sec before retrying...`);
}
setTimeout(() => {
this.execute(options, resolve || _resolve, reject || _reject);
}, retryAfter * 1000);
}
else {
if (reject) {
reject(error);
}
else {
_reject(error);
}
}
});
});
}
}
export default new Request();