forked from perry-mitchell/webdav-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.js
82 lines (75 loc) · 2.9 KB
/
request.js
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
const axios = require("axios");
const { merge } = require("./merge.js");
const { getPatcher } = require("./patcher.js");
const SEP_PATH_POSIX = "__PATH_SEPARATOR_POSIX__";
const SEP_PATH_WINDOWS = "__PATH_SEPARATOR_WINDOWS__";
/**
* Encode a path for use with WebDAV servers
* @param {String} path The path to encode
* @returns {String} The encoded path (separators protected)
*/
function encodePath(path) {
const replaced = path.replace(/\//g, SEP_PATH_POSIX).replace(/\\\\/g, SEP_PATH_WINDOWS);
const formatted = encodeURIComponent(replaced);
return formatted
.split(SEP_PATH_WINDOWS)
.join("\\\\")
.split(SEP_PATH_POSIX)
.join("/");
}
/**
* @typedef {Object} UserOptions
* @property {Object=} httpAgent - HTTP agent instance
* @property {Object=} httpsAgent - HTTPS agent instance
* @property {Object=} headers - Set additional request headers
* @property {Boolean=} withCredentials - Set whether or not credentials should
* be included with the request. Defaults to value used by axios.
*/
/**
* Process request options before being passed to Axios
* @param {RequestOptions} requestOptions The request options object
* @param {UserOptions} methodOptions Provided options (external)
*/
function prepareRequestOptions(requestOptions, methodOptions) {
if (methodOptions.httpAgent) {
requestOptions.httpAgent = methodOptions.httpAgent;
}
if (methodOptions.httpsAgent) {
requestOptions.httpsAgent = methodOptions.httpsAgent;
}
if (methodOptions.headers && typeof methodOptions.headers === "object") {
requestOptions.headers = merge(requestOptions.headers || {}, methodOptions.headers);
}
if (typeof methodOptions.withCredentials === "boolean") {
requestOptions.withCredentials = methodOptions.withCredentials;
}
if (methodOptions.maxContentLength) {
requestOptions.maxContentLength = methodOptions.maxContentLength;
}
}
/**
* @typedef {Object} RequestOptions
* @property {String} url - The URL to request
* @property {String} method - The method to use (eg. "POST")
* @property {Object=} headers - Headers to set on the request
* @property {Object=} httpAgent - A HTTP agent instance
* @property {Object=} httpsAgent - A HTTPS agent interface
* @property {Object|String|*=} body - Body data for the request
*/
/**
* Make a request
* This method can be patched by patching or plugging-in to the "request"
* item using {@link https://github.com/perry-mitchell/hot-patcher HotPatcher}.
* It uses {@link https://github.com/axios/axios Axios} by default.
* @param {RequestOptions} requestOptions Options for the request
* @returns {Promise.<Object>} A promise that resolves with a response object
*/
function request(requestOptions) {
return getPatcher().patchInline("request", options => axios(options), requestOptions);
}
module.exports = {
axios,
encodePath,
prepareRequestOptions,
request
};