@ajaxjs/util 1.1.2 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/core/xhr.ts DELETED
@@ -1,296 +0,0 @@
1
- import { XhrConfig } from './xhr-config';
2
-
3
- /**
4
- * 默认的请求配置
5
- */
6
- const DEFAULT_XHR_CFG: XhrConfig = {
7
- timeout: 5000,
8
- withCredentials: false,
9
- parseContentType: 'json'
10
- };
11
-
12
- /**
13
- * 处理响应的回调函数
14
- */
15
- type XhrCallback = (json: {}, text: string) => void;
16
-
17
- /**
18
- * 全局请求的 head 参数
19
- */
20
- let BASE_HEAD_PARAMS: { [key: string]: any } | null = null;
21
-
22
- /**
23
- * 设置全局请求的 head 参数
24
- *
25
- * @param param
26
- */
27
- export function setBaseHeadParams(params: any): void {
28
- if (BASE_HEAD_PARAMS === null)
29
- BASE_HEAD_PARAMS = {};
30
-
31
- Object.assign(BASE_HEAD_PARAMS, params);
32
- }
33
-
34
- /**
35
- *
36
- * @param getOrDel
37
- * @param url
38
- * @param cb
39
- * @param params
40
- * @param cfg
41
- */
42
- function getOrDel(getOrDel: 'get' | 'delete', url: string, cb: XhrCallback, params?: {}, cfg: XhrConfig = DEFAULT_XHR_CFG): void {
43
- let xhr: XMLHttpRequest = initXhr(cfg);
44
-
45
- if (params != null) {
46
- if (url.indexOf('?') != -1)
47
- url += '&' + toParams(params);
48
- else
49
- url += '?' + toParams(params);
50
- }
51
-
52
- xhr.open(getOrDel.toUpperCase(), url, true);
53
- xhr.onreadystatechange = function () {
54
- responseHandle(this, cb, cfg);
55
- }
56
-
57
- if (BASE_HEAD_PARAMS)// 设置自定义请求头
58
- for (let key in BASE_HEAD_PARAMS)
59
- xhr.setRequestHeader(key, BASE_HEAD_PARAMS[key]);
60
-
61
- xhr.send();
62
- }
63
-
64
- /**
65
- *
66
- * @param method
67
- * @param url
68
- * @param cb
69
- * @param params
70
- * @param cfg
71
- */
72
- function postOrPut(method: 'post' | 'put', url: string, cb: XhrCallback, params: string | {}, cfg: XhrConfig = DEFAULT_XHR_CFG): void {
73
- let xhr: XMLHttpRequest = initXhr(cfg);
74
- xhr.open(method, url, true);
75
- xhr.onreadystatechange = function () {
76
- responseHandle(this, cb, cfg);
77
- }
78
-
79
- if (BASE_HEAD_PARAMS) // 设置自定义请求头
80
- for (let key in BASE_HEAD_PARAMS)
81
- xhr.setRequestHeader(key, BASE_HEAD_PARAMS[key]);
82
-
83
- // 此方法必须在 open() 方法和 send() 之间调用
84
-
85
- if (!cfg.contentType) // 如未设置,默认为表单请求
86
- xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
87
- else
88
- xhr.setRequestHeader("Content-Type", cfg.contentType);
89
-
90
-
91
- let _params: string = typeof params != 'string' ? toParams(params) : <string>params;
92
-
93
- if (_params)
94
- xhr.send(_params);
95
- else
96
- xhr.send();
97
- }
98
-
99
- /**
100
- *
101
- * @param url
102
- * @param cb
103
- * @param params
104
- * @param cfg
105
- */
106
- export function xhr_post_upload(url: string, cb: XhrCallback, params: Document | XMLHttpRequestBodyInit, cfg: XhrConfig = DEFAULT_XHR_CFG): void {
107
- let xhr: XMLHttpRequest = initXhr(cfg);
108
- xhr.open('post', url, true);
109
- xhr.onreadystatechange = function () {
110
- responseHandle(this, cb, cfg);
111
- }
112
-
113
- if (BASE_HEAD_PARAMS) // 设置自定义请求头
114
- for (let key in BASE_HEAD_PARAMS)
115
- xhr.setRequestHeader(key, BASE_HEAD_PARAMS[key]);
116
-
117
- // 什么 Content-Type 都不设置
118
-
119
- xhr.send(params);
120
- }
121
-
122
- /**
123
- * XHR GET 请求
124
- *
125
- * @param url 请求地址
126
- * @param cb 回调函数 @example (json: {}, text: string) => void;
127
- * @param params 参数,必填,如无填空字符串 "";参数类型是json;参数值会进行 URL 编码,最后附加到 QueryString 中
128
- * @param cfg 配置,可选的
129
- */
130
- export function xhr_get(url: string, cb: XhrCallback, params?: {}, cfg: XhrConfig = DEFAULT_XHR_CFG): void {
131
- getOrDel('get', url, cb, params, cfg);
132
- }
133
-
134
- /**
135
- * XHR DELETE 请求
136
- *
137
- * @param url 请求地址
138
- * @param cb 回调函数 @example (json: {}, text: string) => void;
139
- * @param params 参数,必填,如无填空字符串 "";参数类型是json;参数值会进行 URL 编码,最后附加到 QueryString 中
140
- * @param cfg 配置,可选的
141
- */
142
- export function xhr_del(url: string, cb: XhrCallback, params?: {}, cfg: XhrConfig = DEFAULT_XHR_CFG): void {
143
- getOrDel('delete', url, cb, params, cfg);
144
- }
145
-
146
- /**
147
- * XHR POST 请求
148
- *
149
- * @param url 请求地址
150
- * @param cb 回调函数 @example (json: {}, text: string) => void;
151
- * @param params 参数,必填,如无填空字符串 "";参数类型可以是字符串或 json;参数值会进行 URL 编码
152
- * @param cfg 配置,可选的
153
- */
154
- export function xhr_post(url: string, cb: XhrCallback, params: string | {}, cfg: XhrConfig = DEFAULT_XHR_CFG): void {
155
- postOrPut('post', url, cb, params, cfg);
156
- }
157
-
158
- /**
159
- * XHR PUT 请求
160
- *
161
- * @param url 请求地址
162
- * @param cb 回调函数 @example (json: {}, text: string) => void;
163
- * @param params 参数,必填,如无填空字符串 "";参数类型可以是字符串或 json;参数值会进行 URL 编码
164
- * @param cfg 配置,可选的
165
- */
166
- export function xhr_put(url: string, cb: XhrCallback, params: string | {}, cfg: XhrConfig = DEFAULT_XHR_CFG): void {
167
- postOrPut('put', url, cb, params, cfg);
168
- }
169
-
170
- /**
171
- * 初始化 XHR
172
- *
173
- * @param cfg
174
- * @returns
175
- */
176
- function initXhr(cfg: XhrConfig): XMLHttpRequest {
177
- let xhr: XMLHttpRequest = new XMLHttpRequest();
178
-
179
- if (cfg && cfg.timeout) {
180
- xhr.timeout = cfg.timeout;
181
- xhr.ontimeout = (e: ProgressEvent<EventTarget>) => console.error('系统异常,XHR 连接服务超时');
182
- }
183
-
184
- if (cfg && cfg.withCredentials)
185
- xhr.withCredentials = true;
186
-
187
- return xhr;
188
- }
189
-
190
- /**
191
- * 错误处理
192
- *
193
- * @param xhr
194
- */
195
- function errHandle(xhr: XMLHttpRequest): void {
196
- let msg: string;
197
-
198
- if (xhr.status <= 400)
199
- msg = '请求参数错误或者权限不足。';
200
- else if (xhr.status <= 500)
201
- msg = '服务端异常。';
202
- else
203
- msg = `未知异常,HTTP code:${xhr.status}。`;
204
-
205
- if (!xhr.responseText)
206
- msg += " 服务端返回空的字符串!";
207
-
208
- console.error(msg, xhr.responseText);
209
- }
210
-
211
- /**
212
- * 响应处理
213
- *
214
- * @param xhr
215
- * @param cb
216
- * @param cfg
217
- */
218
- function responseHandle(xhr: XMLHttpRequest, cb: XhrCallback, cfg: XhrConfig): void {
219
- if (xhr.readyState == 4) {
220
- if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) {
221
- let text: string = xhr.responseText;
222
- let json: any;
223
-
224
- if (!text)
225
- console.warn('服务端没有返回任何字符串');
226
-
227
- switch (cfg.parseContentType) {
228
- case 'text':
229
- break;
230
- case 'xml':
231
- json = xhr.responseXML;
232
- break;
233
- case 'json':
234
- default:
235
- try {
236
- json = JSON.parse(text);
237
- } catch (e) {
238
- console.error('解析 JSON 时候发生错误,非法 JSON');
239
- console.warn(e);
240
- }
241
- }
242
-
243
- cb && cb(json, text);
244
- } else errHandle(xhr);
245
- }
246
- }
247
-
248
- /**
249
- * 对象转换为 URL 参数列表,用 & 分隔
250
- *
251
- * @param {Object} param JSON 对象
252
- * @returns URL 参数列表
253
- */
254
- export function toParams(param: any): string {
255
- let result: string = "";
256
-
257
- for (let name in param) {
258
- if (typeof param[name] != "function")
259
- result += "&" + name + "=" + encodeURIComponent(param[name]);
260
- }
261
-
262
- return result.substring(1);
263
- }
264
-
265
- /**
266
- * 获取 QueryString 的某个参数
267
- *
268
- * @param val
269
- * @returns
270
- */
271
- export function getQuery(val: string): string {
272
- const w: number = location.hash.indexOf('?');
273
- const query: string = location.hash.substring(w + 1);
274
- let vars: string[] = query.split('&');
275
-
276
- for (let i = 0; i < vars.length; i++) {
277
- const pair = vars[i].split('=');
278
-
279
- if (pair[0] == val)
280
- return pair[1];
281
- }
282
-
283
- return '';
284
- }
285
-
286
- export function getPageList(self: any, listArray: any, callback?: Function): XhrCallback {
287
- return (j: any) => {
288
- if (j.status) {
289
- listArray.total = j.total;
290
- listArray.data = j.data;
291
-
292
- callback && callback();
293
- } else
294
- self.$Message.warning(j.message || '获取数据失败');
295
- }
296
- }
package/src/index.ts DELETED
@@ -1,6 +0,0 @@
1
- import * as Cookies from './core/cookies';
2
- import * as Dom from './core/dom';
3
- import * as Utils from './core/utils';
4
- import * as Xhr from './core/xhr';
5
-
6
- export { Cookies, Dom, Utils, Xhr };