MCPcopy Create free account
hub / github.com/wendux/fly / request

Method request

_.js:80–306  ·  view source on GitHub ↗
(url, data, options)

Source from the content-addressed store, hash-verified

78 }
79
80 request(url, data, options) {
81 var engine = new this.engine;
82 var contentType = "Content-Type";
83 var interceptors = this.interceptors;
84 var requestInterceptor = interceptors.request;
85 var responseInterceptor = interceptors.response;
86 var requestInterceptorHandler = requestInterceptor.handler;
87 var promise = new Promise((resolve, reject) => {
88 if (utils.isObject(url)) {
89 options = url;
90 url = options.url;
91 }
92 options = options || {};
93 options.headers = options.headers || {};
94
95 function isPromise(p) {
96 // some polyfill implementation of Promise may be not standard,
97 // so, we test by duck-typing
98 return p && p.then && p.catch
99 }
100
101 /**
102 * If the request/response interceptor has been locked,
103 * the new request/response will enter a queue. otherwise, it will be performed directly.
104 * @param [promise] if the promise exist, means the interceptor is locked.
105 * @param [callback]
106 */
107 function enqueueIfLocked(promise, callback) {
108 if (promise) {
109 promise.then(() => {
110 callback()
111 })
112 } else {
113 callback()
114 }
115 }
116
117 // make the http request
118 function makeRequest(options) {
119 data = options.body;
120 // Normalize the request url
121 url = utils.trim(options.url);
122 var baseUrl = utils.trim(options.baseURL || "");
123 if (!url && isBrowser && !baseUrl) url = location.href;
124 if (url.indexOf("http") !== 0) {
125 var isAbsolute = url[0] === "/";
126 if (!baseUrl && isBrowser) {
127 var arr = location.pathname.split("/");
128 arr.pop();
129 baseUrl = location.protocol + "//" + location.host + (isAbsolute ? "" : arr.join("/"))
130 }
131 if (baseUrl[baseUrl.length - 1] !== "/") {
132 baseUrl += "/"
133 }
134 url = baseUrl + (isAbsolute ? url.substr(1) : url)
135 if (isBrowser) {
136
137 // Normalize the url which contains the ".." or ".", such as

Callers

nothing calls this directly

Calls 3

enqueueIfLockedFunction · 0.85
isPromiseFunction · 0.85
makeRequestFunction · 0.85

Tested by

no test coverage detected