执行过npm install命令的vue-element-admin源码
康凯
2022-05-20 aa4c235a8ca67ea8b731f90c951a465e92c0a865
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
"use strict";
 
exports.__esModule = true;
exports["default"] = void 0;
 
var _forEachArray = _interopRequireDefault(require("../collection/forEachArray"));
 
var _forEachOwnProperties = _interopRequireDefault(require("../collection/forEachOwnProperties"));
 
var _extend = _interopRequireDefault(require("../object/extend"));
 
var _isArray = _interopRequireDefault(require("../type/isArray"));
 
var _isEmpty = _interopRequireDefault(require("../type/isEmpty"));
 
var _isFunction = _interopRequireDefault(require("../type/isFunction"));
 
var _isNull = _interopRequireDefault(require("../type/isNull"));
 
var _isObject = _interopRequireDefault(require("../type/isObject"));
 
var _isUndefined = _interopRequireDefault(require("../type/isUndefined"));
 
function _interopRequireDefault(obj) {
  return obj && obj.__esModule ? obj : {
    "default": obj
  };
}
 
function encodePairs(key, value) {
  return encodeURIComponent(key) + "=" + encodeURIComponent((0, _isNull["default"])(value) || (0, _isUndefined["default"])(value) ? '' : value);
}
 
function serializeParams(key, value, serializedList) {
  if ((0, _isArray["default"])(value)) {
    (0, _forEachArray["default"])(value, function (arrVal, index) {
      serializeParams(key + "[" + ((0, _isObject["default"])(arrVal) ? index : '') + "]", arrVal, serializedList);
    });
  } else if ((0, _isObject["default"])(value)) {
    (0, _forEachOwnProperties["default"])(value, function (objValue, objKey) {
      serializeParams(key + "[" + objKey + "]", objValue, serializedList);
    });
  } else {
    serializedList.push(encodePairs(key, value));
  }
}
 
function serialize(params) {
  if (!params || (0, _isEmpty["default"])(params)) {
    return '';
  }
 
  var serializedList = [];
  (0, _forEachOwnProperties["default"])(params, function (value, key) {
    serializeParams(key, value, serializedList);
  });
  return serializedList.join('&');
}
 
var getDefaultOptions = function getDefaultOptions() {
  return {
    baseURL: '',
    headers: {
      common: {},
      get: {},
      post: {},
      put: {},
      "delete": {},
      patch: {},
      options: {},
      head: {}
    },
    serializer: serialize
  };
};
 
var HTTP_PROTOCOL_REGEXP = /^(http|https):\/\//i;
 
function combineURL(baseURL, url) {
  if (HTTP_PROTOCOL_REGEXP.test(url)) {
    return url;
  }
 
  if (baseURL.slice(-1) === '/' && url.slice(0, 1) === '/') {
    url = url.slice(1);
  }
 
  return baseURL + url;
}
 
function getComputedOptions(defaultOptions, customOptions) {
  var baseURL = defaultOptions.baseURL,
      defaultHeaders = defaultOptions.headers,
      defaultSerializer = defaultOptions.serializer,
      defaultBeforeRequest = defaultOptions.beforeRequest,
      defaultSuccess = defaultOptions.success,
      defaultError = defaultOptions.error,
      defaultComplete = defaultOptions.complete;
  var url = customOptions.url,
      contentType = customOptions.contentType,
      method = customOptions.method,
      params = customOptions.params,
      headers = customOptions.headers,
      serializer = customOptions.serializer,
      beforeRequest = customOptions.beforeRequest,
      success = customOptions.success,
      error = customOptions.error,
      complete = customOptions.complete,
      withCredentials = customOptions.withCredentials,
      mimeType = customOptions.mimeType;
  var options = {
    url: combineURL(baseURL, url),
    method: method,
    params: params,
    headers: (0, _extend["default"])(defaultHeaders.common, defaultHeaders[method.toLowerCase()], headers),
    serializer: serializer || defaultSerializer || serialize,
    beforeRequest: [defaultBeforeRequest, beforeRequest],
    success: [defaultSuccess, success],
    error: [defaultError, error],
    complete: [defaultComplete, complete],
    withCredentials: withCredentials,
    mimeType: mimeType
  };
  options.contentType = contentType || options.headers['Content-Type'];
  delete options.headers['Content-Type'];
  return options;
}
 
function validateStatus(status) {
  return status >= 200 && status < 300;
}
 
function hasRequestBody(method) {
  return /^(?:POST|PUT|PATCH)$/.test(method.toUpperCase());
}
 
function executeCallback(callback, param) {
  if ((0, _isArray["default"])(callback)) {
    (0, _forEachArray["default"])(callback, function (fn) {
      return executeCallback(fn, param);
    });
  } else if ((0, _isFunction["default"])(callback)) {
    callback(param);
  }
}
 
function parseHeaders(text) {
  var headers = {};
  (0, _forEachArray["default"])(text.split('\r\n'), function (header) {
    var _header$split = header.split(': '),
        key = _header$split[0],
        value = _header$split[1];
 
    if (key !== '' && !(0, _isUndefined["default"])(value)) {
      headers[key] = value;
    }
  });
  return headers;
}
 
function parseJSONData(data) {
  var result = '';
 
  try {
    result = JSON.parse(data);
  } catch (_) {
    result = data;
  }
 
  return result;
}
 
var REQUEST_DONE = 4;
 
function handleReadyStateChange(xhr, options) {
  var readyState = xhr.readyState;
 
  if (readyState != REQUEST_DONE) {
    return;
  }
 
  var status = xhr.status,
      statusText = xhr.statusText,
      responseText = xhr.responseText;
  var success = options.success,
      resolve = options.resolve,
      error = options.error,
      reject = options.reject,
      complete = options.complete;
 
  if (validateStatus(status)) {
    var contentType = xhr.getResponseHeader('Content-Type');
    var data = responseText;
 
    if (contentType && contentType.indexOf('application/json') > -1) {
      data = parseJSONData(data);
    }
 
    executeCallback([success, resolve], {
      status: status,
      statusText: statusText,
      data: data,
      headers: parseHeaders(xhr.getAllResponseHeaders())
    });
  } else {
    executeCallback([error, reject], {
      status: status,
      statusText: statusText
    });
  }
 
  executeCallback(complete, {
    status: status,
    statusText: statusText
  });
}
 
var QS_DELIM_REGEXP = /\?/;
 
function open(xhr, options) {
  var url = options.url,
      method = options.method,
      serializer = options.serializer,
      params = options.params;
  var requestUrl = url;
 
  if (!hasRequestBody(method) && params) {
    var qs = (QS_DELIM_REGEXP.test(url) ? '&' : '?') + serializer(params);
    requestUrl = "" + url + qs;
  }
 
  xhr.open(method, requestUrl);
}
 
function applyConfig(xhr, options) {
  var method = options.method,
      contentType = options.contentType,
      mimeType = options.mimeType,
      headers = options.headers,
      _options$withCredenti = options.withCredentials,
      withCredentials = _options$withCredenti === void 0 ? false : _options$withCredenti;
 
  if (withCredentials) {
    xhr.withCredentials = withCredentials;
  }
 
  if (mimeType) {
    xhr.overrideMimeType(mimeType);
  }
 
  (0, _forEachOwnProperties["default"])(headers, function (value, header) {
    if (!(0, _isObject["default"])(value)) {
      xhr.setRequestHeader(header, value);
    }
  });
 
  if (hasRequestBody(method)) {
    xhr.setRequestHeader('Content-Type', contentType + "; charset=UTF-8");
  }
 
  xhr.setRequestHeader('x-requested-with', 'XMLHttpRequest');
}
 
var ENCODED_SPACE_REGEXP = /%20/g;
 
function send(xhr, options) {
  var method = options.method,
      serializer = options.serializer,
      beforeRequest = options.beforeRequest,
      _options$params = options.params,
      params = _options$params === void 0 ? {} : _options$params,
      _options$contentType = options.contentType,
      contentType = _options$contentType === void 0 ? 'application/x-www-form-urlencoded' : _options$contentType;
  var body = null;
 
  if (hasRequestBody(method)) {
    body = contentType.indexOf('application/x-www-form-urlencoded') > -1 ? serializer(params).replace(ENCODED_SPACE_REGEXP, '+') : JSON.stringify(params);
  }
 
  xhr.onreadystatechange = function () {
    return handleReadyStateChange(xhr, options);
  };
 
  executeCallback(beforeRequest, xhr);
  xhr.send(body);
}
 
function ajax(options) {
  var xhr = new XMLHttpRequest();
 
  var request = function request(opts) {
    return (0, _forEachArray["default"])([open, applyConfig, send], function (fn) {
      return fn(xhr, opts);
    });
  };
 
  options = getComputedOptions(ajax.defaults, options);
 
  if (typeof Promise !== 'undefined') {
    return new Promise(function (resolve, reject) {
      request((0, _extend["default"])(options, {
        resolve: resolve,
        reject: reject
      }));
    });
  }
 
  request(options);
  return null;
}
 
ajax.defaults = getDefaultOptions();
 
ajax._reset = function () {
  ajax.defaults = getDefaultOptions();
};
 
ajax._request = function (url, method, options) {
  if (options === void 0) {
    options = {};
  }
 
  return ajax((0, _extend["default"])(options, {
    url: url,
    method: method
  }));
};
 
(0, _forEachArray["default"])(['get', 'post', 'put', 'delete', 'patch', 'options', 'head'], function (type) {
  ajax[type] = function (url, options) {
    return ajax._request(url, type.toUpperCase(), options);
  };
});
var _default = ajax;
exports["default"] = _default;
module.exports = exports["default"];