vap1 0.7.9 → 0.8.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/package.json CHANGED
@@ -1 +1 @@
1
- {"name":"vap1","version":"0.7.9","description":"vap1, Both support MicroService and SAP FrameWork, Support IE>9","main":"index.js","author":"Xiang da","license":"ISC"}
1
+ {"name":"vap1","version":"0.8.0","description":"vap1, Both support MicroService and SAP FrameWork, Support IE>9","main":"index.js","author":"Xiang da","license":"ISC"}
package/utils/Ajax.js CHANGED
@@ -115,9 +115,11 @@ var FETCH = function (url, options) { return __awaiter(void 0, void 0, void 0, f
115
115
  if (fetchOptions.body && lodash_1.default.isPlainObject(fetchOptions.body)) {
116
116
  fetchOptions.body = JSON.stringify(fetchOptions.body);
117
117
  }
118
+ fetchOptions.headers = (0, _AjaxUtil_1.appendMicorSessionHeaderForRequest)(url, fetchOptions.headers);
118
119
  return [4 /*yield*/, fetch(url, fetchOptions)];
119
120
  case 1:
120
121
  response = _a.sent();
122
+ (0, _AjaxUtil_1.receiveMicorSessionByFetch)(response);
121
123
  if (options.process !== false)
122
124
  done();
123
125
  if (!lodash_1.default.isFunction(Global_1.CONFIG.AJAX.beforeReceive)) return [3 /*break*/, 3];
package/utils/XHR.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { PlainObject } from '../basetype';
2
2
  /**
3
- * 不推荐使用同步方法,
3
+ * 不推荐使用同步方法
4
4
  */
5
5
  export declare const GETSYNC: (url: string) => PlainObject;
6
6
  /**
package/utils/XHR.js CHANGED
@@ -22,7 +22,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
22
22
  exports.POSTSYNC = exports.GETSYNC = void 0;
23
23
  var lodash_1 = __importDefault(require("lodash"));
24
24
  var _AjaxUtil_1 = require("./_AjaxUtil");
25
- var FETCH = function (method, url, param) {
25
+ var FETCH = function (method, url, param, headers) {
26
26
  var xhr = new XMLHttpRequest();
27
27
  var result = {};
28
28
  xhr.onreadystatechange = function () {
@@ -33,21 +33,22 @@ var FETCH = function (method, url, param) {
33
33
  }
34
34
  };
35
35
  xhr.open(method, url, false);
36
- xhr.setRequestHeader('Content-Type', 'application/json');
36
+ (0, _AjaxUtil_1.setXHRHeaders)(xhr, (0, _AjaxUtil_1.mergeHeaders)({ 'Content-Type': 'application/json' }, headers));
37
37
  if (method == 'GET') {
38
38
  xhr.send();
39
39
  }
40
40
  else {
41
41
  xhr.send(JSON.stringify(param));
42
42
  }
43
+ (0, _AjaxUtil_1.receiveMicorSessionByXHR)(xhr);
43
44
  return result;
44
45
  };
45
46
  var _FETCH = function (method, url, param) {
46
47
  var _a = __read((0, _AjaxUtil_1.AOP_BEFORE)(url, method, {}, param), 4), _url = _a[0], _method = _a[1], _headers = _a[2], _data = _a[3];
47
- return FETCH(_method == 'GET' ? 'GET' : 'POST', _url, _data);
48
+ return FETCH(_method == 'GET' ? 'GET' : 'POST', _url, _data, _headers);
48
49
  };
49
50
  /**
50
- * 不推荐使用同步方法,
51
+ * 不推荐使用同步方法
51
52
  */
52
53
  var GETSYNC = function (url) {
53
54
  if (_AjaxUtil_1.CACHE_URL.has(url) && sessionStorage.getItem(url)) {
@@ -18,6 +18,12 @@ export declare const WAIT: (ms: number) => Promise<unknown>;
18
18
  export declare const SESSION_TIME: (url: string, second: number) => void;
19
19
  export declare const SESSION: (...urls: string[]) => Set<string>[];
20
20
  export declare const CLEAR: (...urls: string[]) => void[];
21
+ type HeaderRecord = Record<string, string>;
22
+ export declare const mergeHeaders: (...headersList: HeadersInit[]) => HeaderRecord;
23
+ export declare const setXHRHeaders: (xhr: XMLHttpRequest, headers: HeadersInit) => void;
24
+ export declare const appendMicorSessionHeaderForRequest: (url: string, headers: HeadersInit) => HeaderRecord;
25
+ export declare const receiveMicorSessionByFetch: (response: Response) => void;
26
+ export declare const receiveMicorSessionByXHR: (xhr: XMLHttpRequest) => void;
21
27
  export interface Result {
22
28
  code: string;
23
29
  message: string;
@@ -27,3 +33,4 @@ export declare const filterNull: (param: any) => any;
27
33
  export declare const setFixedHeader: (header: Record<string, string>) => void;
28
34
  export declare const AOP_BEFORE: (url: string, method: "GET" | "POST" | "PUT" | "DELETE" | "PATCH", headers: HeadersInit, query: PlainObject) => [string, "GET" | "POST" | "PUT" | "DELETE" | "PATCH", HeadersInit, PlainObject];
29
35
  export declare const LOG_AJAX: (url: string, method: string, param?: any) => void;
36
+ export {};
@@ -1,15 +1,4 @@
1
1
  "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
2
  var __values = (this && this.__values) || function(o) {
14
3
  var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
15
4
  if (m) return m.call(o);
@@ -21,12 +10,28 @@ var __values = (this && this.__values) || function(o) {
21
10
  };
22
11
  throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
23
12
  };
13
+ var __read = (this && this.__read) || function (o, n) {
14
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
15
+ if (!m) return o;
16
+ var i = m.call(o), r, ar = [], e;
17
+ try {
18
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
19
+ }
20
+ catch (error) { e = { error: error }; }
21
+ finally {
22
+ try {
23
+ if (r && !r.done && (m = i["return"])) m.call(i);
24
+ }
25
+ finally { if (e) throw e.error; }
26
+ }
27
+ return ar;
28
+ };
24
29
  var __importDefault = (this && this.__importDefault) || function (mod) {
25
30
  return (mod && mod.__esModule) ? mod : { "default": mod };
26
31
  };
27
32
  var e_1, _a;
28
33
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.LOG_AJAX = exports.AOP_BEFORE = exports.setFixedHeader = exports.filterNull = exports.CLEAR = exports.SESSION = exports.SESSION_TIME = exports.WAIT = exports.CACHE_TIMER = exports.CACHE_URL = void 0;
34
+ exports.LOG_AJAX = exports.AOP_BEFORE = exports.setFixedHeader = exports.filterNull = exports.receiveMicorSessionByXHR = exports.receiveMicorSessionByFetch = exports.appendMicorSessionHeaderForRequest = exports.setXHRHeaders = exports.mergeHeaders = exports.CLEAR = exports.SESSION = exports.SESSION_TIME = exports.WAIT = exports.CACHE_TIMER = exports.CACHE_URL = void 0;
30
35
  var lodash_1 = __importDefault(require("lodash"));
31
36
  var _Support_1 = require("./_Support");
32
37
  var Global_1 = require("./Global");
@@ -69,6 +74,209 @@ var CLEAR = function () {
69
74
  };
70
75
  exports.CLEAR = CLEAR;
71
76
  var sessionStorage = window.sessionStorage;
77
+ var MICOR_SESSION_STORAGE = 'MICOR_SESSION';
78
+ var MICOR_SESSION_RESPONSE_HEADER = 'set-cems-cookies';
79
+ var MICOR_SESSION_REQUEST_HEADER = 'cems-cookies';
80
+ var hasMicorApp = function () {
81
+ return lodash_1.default.has(window, 'microApp');
82
+ };
83
+ var readMicorSessionStore = function () {
84
+ var value = sessionStorage.getItem(MICOR_SESSION_STORAGE);
85
+ if (!value)
86
+ return {};
87
+ try {
88
+ return JSON.parse(value);
89
+ }
90
+ catch (_a) {
91
+ return {};
92
+ }
93
+ };
94
+ var splitMicorSessionHeader = function (headerValue) {
95
+ return headerValue.split(/,(?=\s*[^;,=\s]+=)/g).map(function (value) { return lodash_1.default.trim(value); }).filter(Boolean);
96
+ };
97
+ var normalizePath = function (path) {
98
+ if (!path)
99
+ return '/';
100
+ return path.charAt(0) == '/' ? path : "/".concat(path);
101
+ };
102
+ var resolvePathname = function (url) {
103
+ var parser = document.createElement('a');
104
+ parser.href = url;
105
+ return normalizePath(parser.pathname || '/');
106
+ };
107
+ var isPathMatched = function (requestPath, cookiePath) {
108
+ var path = normalizePath(cookiePath);
109
+ if (path == '/')
110
+ return true;
111
+ if (requestPath == path)
112
+ return true;
113
+ if (lodash_1.default.endsWith(path, '/'))
114
+ return lodash_1.default.startsWith(requestPath, path);
115
+ return lodash_1.default.startsWith(requestPath, path) && requestPath.charAt(path.length) == '/';
116
+ };
117
+ var parseMicorSessionCookie = function (cookieValue) {
118
+ var e_2, _a;
119
+ var values = cookieValue.split(';').map(function (value) { return lodash_1.default.trim(value); }).filter(Boolean);
120
+ if (values.length == 0)
121
+ return null;
122
+ var cookiePair = values[0];
123
+ var idx = cookiePair.indexOf('=');
124
+ if (idx <= 0)
125
+ return null;
126
+ var path = '/';
127
+ try {
128
+ for (var values_1 = __values(values), values_1_1 = values_1.next(); !values_1_1.done; values_1_1 = values_1.next()) {
129
+ var value = values_1_1.value;
130
+ var attrIdx = value.indexOf('=');
131
+ var attrName = lodash_1.default.toLower(attrIdx >= 0 ? value.substring(0, attrIdx) : value);
132
+ if (attrName == 'path') {
133
+ path = attrIdx >= 0 ? lodash_1.default.trim(value.substring(attrIdx + 1)) : '/';
134
+ break;
135
+ }
136
+ }
137
+ }
138
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
139
+ finally {
140
+ try {
141
+ if (values_1_1 && !values_1_1.done && (_a = values_1.return)) _a.call(values_1);
142
+ }
143
+ finally { if (e_2) throw e_2.error; }
144
+ }
145
+ return {
146
+ name: cookiePair.substring(0, idx),
147
+ path: normalizePath(path),
148
+ value: cookiePair.substring(idx + 1),
149
+ source: cookieValue,
150
+ };
151
+ };
152
+ var receiveMicorSession = function (headerValue) {
153
+ if (!hasMicorApp())
154
+ return;
155
+ if (!headerValue)
156
+ return;
157
+ var store = readMicorSessionStore();
158
+ splitMicorSessionHeader(headerValue).forEach(function (value) {
159
+ var cookie = parseMicorSessionCookie(value);
160
+ if (cookie == null)
161
+ return;
162
+ if (!store[cookie.path])
163
+ store[cookie.path] = {};
164
+ store[cookie.path][cookie.name] = {
165
+ value: cookie.value,
166
+ source: cookie.source,
167
+ };
168
+ });
169
+ sessionStorage.setItem(MICOR_SESSION_STORAGE, JSON.stringify(store));
170
+ };
171
+ var getMatchedMicorSession = function (url) {
172
+ if (!hasMicorApp())
173
+ return '';
174
+ var store = readMicorSessionStore();
175
+ var requestPath = resolvePathname(url);
176
+ if (lodash_1.default.keys(store).length == 0)
177
+ return '';
178
+ var matchedPath = lodash_1.default.keys(store)
179
+ .filter(function (path) { return isPathMatched(requestPath, path); })
180
+ .sort(function (a, b) { return b.length - a.length; })[0];
181
+ if (!matchedPath)
182
+ return '';
183
+ return lodash_1.default.keys(store[matchedPath])
184
+ .map(function (name) { return "".concat(name, "=").concat(store[matchedPath][name].value); })
185
+ .join('; ');
186
+ };
187
+ var findHeaderKey = function (headers, headerName) {
188
+ var lowerName = lodash_1.default.toLower(headerName);
189
+ return lodash_1.default.find(lodash_1.default.keys(headers), function (key) { return lodash_1.default.toLower(key) == lowerName; });
190
+ };
191
+ var mergeHeaders = function () {
192
+ var headersList = [];
193
+ for (var _i = 0; _i < arguments.length; _i++) {
194
+ headersList[_i] = arguments[_i];
195
+ }
196
+ var result = {};
197
+ headersList.filter(Boolean).forEach(function (headers) {
198
+ if (typeof Headers != 'undefined' && headers instanceof Headers) {
199
+ headers.forEach(function (value, key) {
200
+ result[findHeaderKey(result, key) || key] = value;
201
+ });
202
+ return;
203
+ }
204
+ if (lodash_1.default.isArray(headers)) {
205
+ headers.forEach(function (header) {
206
+ var _a = __read(header, 2), key = _a[0], value = _a[1];
207
+ if (value != null)
208
+ result[findHeaderKey(result, key) || key] = value;
209
+ });
210
+ return;
211
+ }
212
+ lodash_1.default.keys(headers).forEach(function (key) {
213
+ var value = headers[key];
214
+ if (value != null)
215
+ result[findHeaderKey(result, key) || key] = value;
216
+ });
217
+ });
218
+ return result;
219
+ };
220
+ exports.mergeHeaders = mergeHeaders;
221
+ var appendCookieHeader = function (current, value) {
222
+ if (!current)
223
+ return value;
224
+ var exists = new Set();
225
+ current.split(';').map(function (item) { return lodash_1.default.trim(item); }).filter(Boolean).forEach(function (item) {
226
+ exists.add(lodash_1.default.toLower(item.split('=')[0]));
227
+ });
228
+ var additions = value.split(';').map(function (item) { return lodash_1.default.trim(item); }).filter(Boolean).filter(function (item) {
229
+ return !exists.has(lodash_1.default.toLower(item.split('=')[0]));
230
+ });
231
+ if (additions.length == 0)
232
+ return current;
233
+ return "".concat(current, "; ").concat(additions.join('; '));
234
+ };
235
+ var appendMicorSessionHeader = function (url, headers) {
236
+ var result = (0, exports.mergeHeaders)(headers);
237
+ var session = getMatchedMicorSession(url);
238
+ if (!session)
239
+ return result;
240
+ var headerKey = findHeaderKey(result, MICOR_SESSION_REQUEST_HEADER) || MICOR_SESSION_REQUEST_HEADER;
241
+ var oldHeaderValue = result[headerKey];
242
+ result[headerKey] = appendCookieHeader(oldHeaderValue, session);
243
+ return result;
244
+ };
245
+ var setXHRHeaders = function (xhr, headers) {
246
+ var requestHeaders = (0, exports.mergeHeaders)(headers);
247
+ lodash_1.default.keys(requestHeaders).forEach(function (key) { return xhr.setRequestHeader(key, requestHeaders[key]); });
248
+ };
249
+ exports.setXHRHeaders = setXHRHeaders;
250
+ var clearMicorSession = function (path) {
251
+ if (!lodash_1.default.isString(path)) {
252
+ sessionStorage.removeItem(MICOR_SESSION_STORAGE);
253
+ return;
254
+ }
255
+ var store = readMicorSessionStore();
256
+ delete store[normalizePath(path)];
257
+ sessionStorage.setItem(MICOR_SESSION_STORAGE, JSON.stringify(store));
258
+ };
259
+ var MICOR_SESSION = {
260
+ requestHeader: MICOR_SESSION_REQUEST_HEADER,
261
+ responseHeader: MICOR_SESSION_RESPONSE_HEADER,
262
+ receive: receiveMicorSession,
263
+ receiveByFetch: function (response) { return receiveMicorSession(response.headers.get(MICOR_SESSION_RESPONSE_HEADER)); },
264
+ receiveByXHR: function (xhr) { return receiveMicorSession(xhr.getResponseHeader(MICOR_SESSION_RESPONSE_HEADER)); },
265
+ appendHeader: appendMicorSessionHeader,
266
+ get: function (path) {
267
+ var store = readMicorSessionStore();
268
+ if (lodash_1.default.isString(path))
269
+ return store[normalizePath(path)] || {};
270
+ return store;
271
+ },
272
+ clear: clearMicorSession,
273
+ };
274
+ var appendMicorSessionHeaderForRequest = function (url, headers) { return MICOR_SESSION.appendHeader(url, headers); };
275
+ exports.appendMicorSessionHeaderForRequest = appendMicorSessionHeaderForRequest;
276
+ var receiveMicorSessionByFetch = function (response) { return MICOR_SESSION.receiveByFetch(response); };
277
+ exports.receiveMicorSessionByFetch = receiveMicorSessionByFetch;
278
+ var receiveMicorSessionByXHR = function (xhr) { return MICOR_SESSION.receiveByXHR(xhr); };
279
+ exports.receiveMicorSessionByXHR = receiveMicorSessionByXHR;
72
280
  //过滤空参数
73
281
  var filterNull = function (param) {
74
282
  if (!lodash_1.default.isObject(param))
@@ -118,7 +326,7 @@ if (FIXED_STORAGE) {
118
326
  }
119
327
  }
120
328
  var setFixedHeader = function (header) {
121
- var e_2, _a;
329
+ var e_3, _a;
122
330
  var keys = lodash_1.default.keys(header);
123
331
  try {
124
332
  for (var keys_2 = __values(keys), keys_2_1 = keys_2.next(); !keys_2_1.done; keys_2_1 = keys_2.next()) {
@@ -126,12 +334,12 @@ var setFixedHeader = function (header) {
126
334
  FIXED_HEADER[key] = header[key];
127
335
  }
128
336
  }
129
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
337
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
130
338
  finally {
131
339
  try {
132
340
  if (keys_2_1 && !keys_2_1.done && (_a = keys_2.return)) _a.call(keys_2);
133
341
  }
134
- finally { if (e_2) throw e_2.error; }
342
+ finally { if (e_3) throw e_3.error; }
135
343
  }
136
344
  sessionStorage.setItem(FIXED_HEADER_STROE, JSON.stringify(FIXED_HEADER));
137
345
  };
@@ -139,10 +347,11 @@ exports.setFixedHeader = setFixedHeader;
139
347
  exports.AOP_BEFORE = (0, _Support_1.globalDefault)('_VAP_BEFORE_SEND', function (url, method, headers, query) {
140
348
  var data = method == 'POST' ? (0, exports.filterNull)(query) : query;
141
349
  var resloveApiRoot = rewriteApiRoot(url);
350
+ var requestHeaders = appendMicorSessionHeader(resloveApiRoot, (0, exports.mergeHeaders)(FIXED_HEADER, headers));
142
351
  if (lodash_1.default.isFunction(Global_1.CONFIG.AJAX.beforeSend)) {
143
- return Global_1.CONFIG.AJAX.beforeSend(resloveApiRoot, method, __assign(__assign({}, FIXED_HEADER), headers), data);
352
+ return Global_1.CONFIG.AJAX.beforeSend(resloveApiRoot, method, requestHeaders, data);
144
353
  }
145
- return [resloveApiRoot, method, __assign(__assign({}, FIXED_HEADER), { headers: headers }), data];
354
+ return [resloveApiRoot, method, requestHeaders, data];
146
355
  });
147
356
  var CODES = { bold: [1, 22], green: [32, 39], blue: [34, 39], bgYellow: [43, 49], bgGray: [100, 49] };
148
357
  var SIGN = '\u001b[';