@lightsoft/js-sdk 1.0.0 → 1.0.2
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/dist/index.d.ts +262 -2
- package/dist/index.js +2728 -223
- package/dist/index.umd.js +925 -231
- package/dist/rebuild.d.ts +4 -0
- package/dist/rebuild.js +236 -0
- package/dist/rebuild.umd.js +245 -0
- package/dist/types/enums/api.d.ts +61 -0
- package/dist/types/enums/index.d.ts +23 -0
- package/dist/types/index.d.ts +4 -3
- package/dist/types/reg-exp/index.d.ts +12 -0
- package/dist/types/types/api.d.ts +16 -0
- package/dist/types/types/index.d.ts +32 -0
- package/dist/types/utils/common.d.ts +102 -0
- package/dist/types/utils/index.d.ts +6 -0
- package/dist/types/utils/mask.d.ts +11 -0
- package/dist/types/utils/rebuild.d.ts +3 -0
- package/dist/types/utils/request.d.ts +36 -0
- package/dist/types/utils/tour.d.ts +22 -0
- package/dist/types/utils/vue.d.ts +15 -0
- package/dist/types/utils/ws.d.ts +13 -0
- package/dist/ws-0exRUlFu.js +144 -0
- package/dist/ws-DIZYbi31.js +144 -0
- package/dist/ws.d.ts +14 -0
- package/dist/ws.js +1 -0
- package/dist/ws.umd.js +118 -0
- package/package.json +8 -4
- package/dist/types/enums/index.enum.d.ts +0 -5
- package/dist/types/types/index.interface.d.ts +0 -11
- package/dist/types/uilts/index.d.ts +0 -1
- package/dist/types/uilts/rebuild.d.ts +0 -1
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { HAS_FEILD } from '../enums';
|
|
2
|
+
import { typeIsNull, ISectoralTable } from '../types';
|
|
3
|
+
/**
|
|
4
|
+
* 对象数组转为字符串
|
|
5
|
+
* @param {object} obj 目标对象
|
|
6
|
+
* @param {string | string[]} keys 键名
|
|
7
|
+
* @param {string} splitter 分隔符
|
|
8
|
+
*/
|
|
9
|
+
export declare function objectArrayToString(obj: {
|
|
10
|
+
[key: string]: any;
|
|
11
|
+
}, keys: string | string[], splitter?: string): {
|
|
12
|
+
[key: string]: any;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* 防抖
|
|
16
|
+
* @param {Function} fn 回调函数
|
|
17
|
+
* @param {number} delay 延迟时间
|
|
18
|
+
*/
|
|
19
|
+
export declare function debounce(fn: <T>(v: T) => void, delay: number): (value: string) => void;
|
|
20
|
+
/**
|
|
21
|
+
*
|
|
22
|
+
* 判断是否移动端
|
|
23
|
+
* @return {boolean}
|
|
24
|
+
*/
|
|
25
|
+
export declare function isMobileDevice(): boolean;
|
|
26
|
+
/**
|
|
27
|
+
* 节流
|
|
28
|
+
* @param {Function} fn 回调函数
|
|
29
|
+
* @param {number} delay 延迟时间
|
|
30
|
+
*/
|
|
31
|
+
export declare function throttle(fn: <T>(v: T) => void, delay: number): (value: string) => void;
|
|
32
|
+
/**
|
|
33
|
+
* 深度合并对象
|
|
34
|
+
* @param target 目标对象
|
|
35
|
+
* @param source 源对象
|
|
36
|
+
*/
|
|
37
|
+
export declare function deepMerge<T>(target: T, source: T): T;
|
|
38
|
+
/**
|
|
39
|
+
* input类型验证
|
|
40
|
+
*/
|
|
41
|
+
export declare class InputLegalityValidate {
|
|
42
|
+
private _phoneReg;
|
|
43
|
+
private _mailReg;
|
|
44
|
+
private _idCardReg;
|
|
45
|
+
/**
|
|
46
|
+
* 验证手机号
|
|
47
|
+
* @param phone 手机号
|
|
48
|
+
*/
|
|
49
|
+
phone(phone: string): boolean;
|
|
50
|
+
/**
|
|
51
|
+
* 验证邮箱
|
|
52
|
+
* @param email 手机号
|
|
53
|
+
*/
|
|
54
|
+
email(email: string): boolean;
|
|
55
|
+
/**
|
|
56
|
+
* 验证身份证
|
|
57
|
+
* @param idCard 身份证号
|
|
58
|
+
*/
|
|
59
|
+
idCard(idCard: string): boolean;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* input长度验证
|
|
63
|
+
*/
|
|
64
|
+
export declare class InputLengthValidate {
|
|
65
|
+
private validators;
|
|
66
|
+
private checkLength;
|
|
67
|
+
private maxLength;
|
|
68
|
+
constructor();
|
|
69
|
+
limitLength(value: string, limit: number): string;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* 检查值是否为空
|
|
73
|
+
* @param value 要检查的值
|
|
74
|
+
* @return {boolean} 如果值为空返回 true,否则返回 false
|
|
75
|
+
*/
|
|
76
|
+
export declare function isNull(value: typeIsNull): boolean;
|
|
77
|
+
/**
|
|
78
|
+
* 对数型结构进行数据处理和映射关系
|
|
79
|
+
*/
|
|
80
|
+
type Mapping = Record<string, string>;
|
|
81
|
+
export declare class dataHandler {
|
|
82
|
+
private fields;
|
|
83
|
+
private node;
|
|
84
|
+
private mapping;
|
|
85
|
+
constructor(fields: typeof HAS_FEILD, node: any, mapping: Mapping);
|
|
86
|
+
private processNode;
|
|
87
|
+
private processAllNodes;
|
|
88
|
+
getProcessedData(): any;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* @param data 树形数据
|
|
92
|
+
* @param targetDeptCode 子节点DeptCode
|
|
93
|
+
* @returns 查找叶子节点并返回路径
|
|
94
|
+
*/
|
|
95
|
+
export declare function findTreeDataNode(data: ISectoralTable[], targetDeptCode: string): string | null;
|
|
96
|
+
/**
|
|
97
|
+
* @param dept 树形数据
|
|
98
|
+
* @param queryString 子节点DeptCode
|
|
99
|
+
* @param results 查找结果集
|
|
100
|
+
*/
|
|
101
|
+
export declare function findTreeDataNodeName(dept: ISectoralTable, queryString: string, results: ISectoralTable[]): void;
|
|
102
|
+
export {};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare class Mask {
|
|
2
|
+
private task;
|
|
3
|
+
private selectors;
|
|
4
|
+
private opacity;
|
|
5
|
+
private elementIndex;
|
|
6
|
+
constructor(selectors: string[], opacity?: number);
|
|
7
|
+
start(): void;
|
|
8
|
+
process(handle: (resolve: any, index: number, element: Element) => void): this;
|
|
9
|
+
open(element?: Element): void;
|
|
10
|
+
close(): void;
|
|
11
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { RequestOptions, RequestType } from '../types/api';
|
|
2
|
+
export declare class Request {
|
|
3
|
+
private readonly xhr;
|
|
4
|
+
private beforeRequestOptions;
|
|
5
|
+
private beforeResponseCallback;
|
|
6
|
+
constructor();
|
|
7
|
+
create(): XMLHttpRequest;
|
|
8
|
+
useState(state: 'SUCCESS' | 'FAIL' | 'PENDING'): boolean;
|
|
9
|
+
send(type: RequestType, url: string, options?: RequestOptions): Promise<unknown>;
|
|
10
|
+
transformResponse(response: XMLHttpRequest): {
|
|
11
|
+
data: any;
|
|
12
|
+
code: number;
|
|
13
|
+
message: string;
|
|
14
|
+
};
|
|
15
|
+
transformQuery(url: string, query: any): string;
|
|
16
|
+
beforeSend(callback: () => RequestOptions): void;
|
|
17
|
+
beforeResponse(callback: (...args: any) => void): void;
|
|
18
|
+
abort(): void;
|
|
19
|
+
get(url: string, query?: any, headers?: RequestOptions['headers']): Promise<unknown>;
|
|
20
|
+
post(url: string, data?: any, headers?: RequestOptions['headers']): Promise<unknown>;
|
|
21
|
+
put(url: string, data?: any, headers?: RequestOptions['headers']): Promise<unknown>;
|
|
22
|
+
delete(url: string, data?: any, headers?: RequestOptions['headers']): Promise<unknown>;
|
|
23
|
+
patch(url: string, data?: any, headers?: RequestOptions['headers']): Promise<unknown>;
|
|
24
|
+
options(url: string, data?: any, headers?: RequestOptions['headers']): Promise<unknown>;
|
|
25
|
+
head(url: string, data?: any, headers?: RequestOptions['headers']): Promise<unknown>;
|
|
26
|
+
trace(url: string, data?: any, headers?: RequestOptions['headers']): Promise<unknown>;
|
|
27
|
+
connect(url: string, data?: any, headers?: RequestOptions['headers']): Promise<unknown>;
|
|
28
|
+
}
|
|
29
|
+
export declare class RequestChain {
|
|
30
|
+
private fieldList;
|
|
31
|
+
private responses;
|
|
32
|
+
constructor(...args: Record<string, any>[]);
|
|
33
|
+
getParams(type: 'query' | 'body', data: any, fields: any): string | Record<string, any>;
|
|
34
|
+
setParams(...args: Record<string, any>[]): void;
|
|
35
|
+
chain(list: any[]): any;
|
|
36
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
type TourPlaceholder = {
|
|
2
|
+
title: string;
|
|
3
|
+
text: string;
|
|
4
|
+
next: string;
|
|
5
|
+
};
|
|
6
|
+
export declare class Tour {
|
|
7
|
+
private selectors;
|
|
8
|
+
private elementIndex;
|
|
9
|
+
private tourTargetElement;
|
|
10
|
+
private tourElement;
|
|
11
|
+
private tourElementContentTemplate;
|
|
12
|
+
private placeholder;
|
|
13
|
+
private config;
|
|
14
|
+
constructor(selectors: string[]);
|
|
15
|
+
setPlaceholder(placeholder: TourPlaceholder): void;
|
|
16
|
+
setContentTemplate(template: string): void;
|
|
17
|
+
setConfig(config: Partial<typeof this.config>): void;
|
|
18
|
+
start(): void;
|
|
19
|
+
layout(element: HTMLElement): void;
|
|
20
|
+
close(): void;
|
|
21
|
+
}
|
|
22
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Ref } from 'vue';
|
|
2
|
+
interface CountDown {
|
|
3
|
+
countdownText: Ref<string>;
|
|
4
|
+
disabled: Ref<boolean>;
|
|
5
|
+
send: () => void;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
*
|
|
9
|
+
* @param {number} num 倒计时时间
|
|
10
|
+
* @param {string} text 按钮文本
|
|
11
|
+
* @returns { object } countdownText 倒计时文本 isDisabled 是否禁用 send 发送验证码方法
|
|
12
|
+
* @description 发送验证码倒计时
|
|
13
|
+
*/
|
|
14
|
+
export declare function useCountDown(num: number, text?: string): CountDown;
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
type WebSocketState = 'CONNECTING' | 'OPEN' | 'CLOSING' | 'CLOSED';
|
|
2
|
+
interface WebSocketConfig {
|
|
3
|
+
timeout?: number;
|
|
4
|
+
reconnect: boolean;
|
|
5
|
+
reconnectTime?: number;
|
|
6
|
+
maxReconnectNumber?: number;
|
|
7
|
+
}
|
|
8
|
+
export declare function createWebSocket(url: string, config: WebSocketConfig | null, onOpen?: Function | null): WebSocket;
|
|
9
|
+
export declare function useSendMessage(ws: WebSocket, data: any): void;
|
|
10
|
+
export declare function useWebSocketMessage(ws: WebSocket, callback: Function): void;
|
|
11
|
+
export declare function useWebSocketClose(ws: WebSocket, code?: number, message?: string): void;
|
|
12
|
+
export declare function getState(ws: WebSocket, type: WebSocketState): boolean;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/******************************************************************************
|
|
2
|
+
Copyright (c) Microsoft Corporation.
|
|
3
|
+
|
|
4
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
5
|
+
purpose with or without fee is hereby granted.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
8
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
9
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
10
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
11
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
12
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
13
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
14
|
+
***************************************************************************** */
|
|
15
|
+
/* global Reflect, Promise, SuppressedError, Symbol, Iterator */
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
var __assign = function() {
|
|
19
|
+
__assign = Object.assign || function __assign(t) {
|
|
20
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
21
|
+
s = arguments[i];
|
|
22
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
|
|
23
|
+
}
|
|
24
|
+
return t;
|
|
25
|
+
};
|
|
26
|
+
return __assign.apply(this, arguments);
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
30
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
31
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
32
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
33
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
34
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
35
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function __generator(thisArg, body) {
|
|
40
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
41
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
42
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
43
|
+
function step(op) {
|
|
44
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
45
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
46
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
47
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
48
|
+
switch (op[0]) {
|
|
49
|
+
case 0: case 1: t = op; break;
|
|
50
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
51
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
52
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
53
|
+
default:
|
|
54
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
55
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
56
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
57
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
58
|
+
if (t[2]) _.ops.pop();
|
|
59
|
+
_.trys.pop(); continue;
|
|
60
|
+
}
|
|
61
|
+
op = body.call(thisArg, _);
|
|
62
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
63
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
68
|
+
var e = new Error(message);
|
|
69
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
var defaultWebSocketConfig = {
|
|
73
|
+
timeout: 6000,
|
|
74
|
+
reconnect: false,
|
|
75
|
+
reconnectTime: 1000,
|
|
76
|
+
maxReconnectNumber: 3,
|
|
77
|
+
};
|
|
78
|
+
function createWebSocket(url, config, onOpen) {
|
|
79
|
+
config = __assign(__assign({}, defaultWebSocketConfig), config);
|
|
80
|
+
var open = reconnectWebSocket();
|
|
81
|
+
var init = function () {
|
|
82
|
+
var ws = new WebSocket(url);
|
|
83
|
+
ws.onerror = function () {
|
|
84
|
+
if ((config === null || config === void 0 ? void 0 : config.reconnect) && open) {
|
|
85
|
+
open(ws, config, function () {
|
|
86
|
+
onOpen === null || onOpen === void 0 ? void 0 : onOpen();
|
|
87
|
+
init();
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
open = null;
|
|
92
|
+
config = null;
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
return ws;
|
|
96
|
+
};
|
|
97
|
+
return init();
|
|
98
|
+
}
|
|
99
|
+
function reconnectWebSocket() {
|
|
100
|
+
var reconnectTimeout;
|
|
101
|
+
var reconnectNumber = 0;
|
|
102
|
+
var open = function (ws, config, callback) {
|
|
103
|
+
var reconnectTime = config.reconnectTime, _a = config.maxReconnectNumber, maxReconnectNumber = _a === void 0 ? 1 : _a;
|
|
104
|
+
var isOpen = getState(ws, 'OPEN');
|
|
105
|
+
if (isOpen || (reconnectTimeout && reconnectNumber >= maxReconnectNumber)) {
|
|
106
|
+
clearTimeout(reconnectTimeout);
|
|
107
|
+
reconnectTimeout = null;
|
|
108
|
+
reconnectNumber = null;
|
|
109
|
+
config.reconnect = false;
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
reconnectTimeout = setTimeout(function () {
|
|
113
|
+
reconnectNumber += 1;
|
|
114
|
+
callback();
|
|
115
|
+
}, reconnectTime);
|
|
116
|
+
};
|
|
117
|
+
return open;
|
|
118
|
+
}
|
|
119
|
+
function useSendMessage(ws, data) {
|
|
120
|
+
var isOpen = getState(ws, 'OPEN');
|
|
121
|
+
if (isOpen)
|
|
122
|
+
ws.send(JSON.stringify(data));
|
|
123
|
+
}
|
|
124
|
+
function useWebSocketMessage(ws, callback) {
|
|
125
|
+
try {
|
|
126
|
+
ws.onmessage = function (e) {
|
|
127
|
+
var data = JSON.parse(e.data);
|
|
128
|
+
callback(data, e);
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
catch (error) {
|
|
132
|
+
console.error(error);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
function useWebSocketClose(ws, code, message) {
|
|
136
|
+
code = code || 1000;
|
|
137
|
+
message = message || 'connection closed by client';
|
|
138
|
+
ws.close(code, message);
|
|
139
|
+
}
|
|
140
|
+
function getState(ws, type) {
|
|
141
|
+
return ws.readyState === WebSocket[type];
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export { __assign as _, __awaiter as a, __generator as b, createWebSocket as c, useWebSocketMessage as d, useWebSocketClose as e, getState as g, useSendMessage as u };
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/******************************************************************************
|
|
2
|
+
Copyright (c) Microsoft Corporation.
|
|
3
|
+
|
|
4
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
5
|
+
purpose with or without fee is hereby granted.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
8
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
9
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
10
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
11
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
12
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
13
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
14
|
+
***************************************************************************** */
|
|
15
|
+
/* global Reflect, Promise, SuppressedError, Symbol, Iterator */
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
var __assign = function() {
|
|
19
|
+
__assign = Object.assign || function __assign(t) {
|
|
20
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
21
|
+
s = arguments[i];
|
|
22
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
|
|
23
|
+
}
|
|
24
|
+
return t;
|
|
25
|
+
};
|
|
26
|
+
return __assign.apply(this, arguments);
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
30
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
31
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
32
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
33
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
34
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
35
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function __generator(thisArg, body) {
|
|
40
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
41
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
42
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
43
|
+
function step(op) {
|
|
44
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
45
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
46
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
47
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
48
|
+
switch (op[0]) {
|
|
49
|
+
case 0: case 1: t = op; break;
|
|
50
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
51
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
52
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
53
|
+
default:
|
|
54
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
55
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
56
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
57
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
58
|
+
if (t[2]) _.ops.pop();
|
|
59
|
+
_.trys.pop(); continue;
|
|
60
|
+
}
|
|
61
|
+
op = body.call(thisArg, _);
|
|
62
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
63
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
68
|
+
var e = new Error(message);
|
|
69
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
var defaultWebSocketConfig = {
|
|
73
|
+
timeout: 6000,
|
|
74
|
+
reconnect: false,
|
|
75
|
+
reconnectTime: 1000,
|
|
76
|
+
maxReconnectNumber: 3,
|
|
77
|
+
};
|
|
78
|
+
function createWebSocket(url, config, onOpen) {
|
|
79
|
+
config = __assign(__assign({}, defaultWebSocketConfig), config);
|
|
80
|
+
var open = reconnectWebSocket();
|
|
81
|
+
var init = function () {
|
|
82
|
+
var ws = new WebSocket(url);
|
|
83
|
+
ws.onopen = function () {
|
|
84
|
+
onOpen === null || onOpen === void 0 ? void 0 : onOpen();
|
|
85
|
+
};
|
|
86
|
+
ws.onerror = function () {
|
|
87
|
+
if ((config === null || config === void 0 ? void 0 : config.reconnect) && open) {
|
|
88
|
+
open(ws, config, init);
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
open = null;
|
|
92
|
+
config = null;
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
return ws;
|
|
96
|
+
};
|
|
97
|
+
return init();
|
|
98
|
+
}
|
|
99
|
+
function reconnectWebSocket() {
|
|
100
|
+
var reconnectTimeout;
|
|
101
|
+
var reconnectNumber = 0;
|
|
102
|
+
var open = function (ws, config, callback) {
|
|
103
|
+
var reconnectTime = config.reconnectTime, _a = config.maxReconnectNumber, maxReconnectNumber = _a === void 0 ? 1 : _a;
|
|
104
|
+
var isOpen = getState(ws, 'OPEN');
|
|
105
|
+
if (isOpen || (reconnectTimeout && reconnectNumber >= maxReconnectNumber)) {
|
|
106
|
+
clearTimeout(reconnectTimeout);
|
|
107
|
+
reconnectTimeout = null;
|
|
108
|
+
reconnectNumber = null;
|
|
109
|
+
config.reconnect = false;
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
reconnectTimeout = setTimeout(function () {
|
|
113
|
+
reconnectNumber += 1;
|
|
114
|
+
callback();
|
|
115
|
+
}, reconnectTime);
|
|
116
|
+
};
|
|
117
|
+
return open;
|
|
118
|
+
}
|
|
119
|
+
function useSendMessage(ws, data) {
|
|
120
|
+
var isOpen = getState(ws, 'OPEN');
|
|
121
|
+
if (isOpen)
|
|
122
|
+
ws.send(JSON.stringify(data));
|
|
123
|
+
}
|
|
124
|
+
function useWebSocketMessage(ws, callback) {
|
|
125
|
+
try {
|
|
126
|
+
ws.onmessage = function (e) {
|
|
127
|
+
var data = JSON.parse(e.data);
|
|
128
|
+
callback(data, e);
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
catch (error) {
|
|
132
|
+
console.error(error);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
function useWebSocketClose(ws, code, message) {
|
|
136
|
+
code = code || 1000;
|
|
137
|
+
message = message || 'connection closed by client';
|
|
138
|
+
ws.close(code, message);
|
|
139
|
+
}
|
|
140
|
+
function getState(ws, type) {
|
|
141
|
+
return ws.readyState === WebSocket[type];
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export { __assign as _, __awaiter as a, __generator as b, createWebSocket as c, useWebSocketMessage as d, useWebSocketClose as e, getState as g, useSendMessage as u };
|
package/dist/ws.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
type WebSocketState = 'CONNECTING' | 'OPEN' | 'CLOSING' | 'CLOSED';
|
|
2
|
+
interface WebSocketConfig {
|
|
3
|
+
timeout?: number;
|
|
4
|
+
reconnect: boolean;
|
|
5
|
+
reconnectTime?: number;
|
|
6
|
+
maxReconnectNumber?: number;
|
|
7
|
+
}
|
|
8
|
+
declare function createWebSocket(url: string, config: WebSocketConfig | null, onOpen?: Function | null): WebSocket;
|
|
9
|
+
declare function useSendMessage(ws: WebSocket, data: any): void;
|
|
10
|
+
declare function useWebSocketMessage(ws: WebSocket, callback: Function): void;
|
|
11
|
+
declare function useWebSocketClose(ws: WebSocket, code?: number, message?: string): void;
|
|
12
|
+
declare function getState(ws: WebSocket, type: WebSocketState): boolean;
|
|
13
|
+
|
|
14
|
+
export { createWebSocket, getState, useSendMessage, useWebSocketClose, useWebSocketMessage };
|
package/dist/ws.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { c as createWebSocket, g as getState, u as useSendMessage, e as useWebSocketClose, d as useWebSocketMessage } from './ws-DIZYbi31.js';
|
package/dist/ws.umd.js
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
(function (global, factory) {
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.w = {}));
|
|
5
|
+
})(this, (function (exports) { 'use strict';
|
|
6
|
+
|
|
7
|
+
/******************************************************************************
|
|
8
|
+
Copyright (c) Microsoft Corporation.
|
|
9
|
+
|
|
10
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
11
|
+
purpose with or without fee is hereby granted.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
14
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
15
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
16
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
17
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
18
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
19
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
20
|
+
***************************************************************************** */
|
|
21
|
+
/* global Reflect, Promise, SuppressedError, Symbol, Iterator */
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
var __assign = function() {
|
|
25
|
+
__assign = Object.assign || function __assign(t) {
|
|
26
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
27
|
+
s = arguments[i];
|
|
28
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
|
|
29
|
+
}
|
|
30
|
+
return t;
|
|
31
|
+
};
|
|
32
|
+
return __assign.apply(this, arguments);
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
36
|
+
var e = new Error(message);
|
|
37
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
var defaultWebSocketConfig = {
|
|
41
|
+
timeout: 6000,
|
|
42
|
+
reconnect: false,
|
|
43
|
+
reconnectTime: 1000,
|
|
44
|
+
maxReconnectNumber: 3,
|
|
45
|
+
};
|
|
46
|
+
function createWebSocket(url, config, onOpen) {
|
|
47
|
+
config = __assign(__assign({}, defaultWebSocketConfig), config);
|
|
48
|
+
var open = reconnectWebSocket();
|
|
49
|
+
var init = function () {
|
|
50
|
+
var ws = new WebSocket(url);
|
|
51
|
+
ws.onopen = function () {
|
|
52
|
+
onOpen === null || onOpen === void 0 ? void 0 : onOpen();
|
|
53
|
+
};
|
|
54
|
+
ws.onerror = function () {
|
|
55
|
+
if ((config === null || config === void 0 ? void 0 : config.reconnect) && open) {
|
|
56
|
+
open(ws, config, init);
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
open = null;
|
|
60
|
+
config = null;
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
return ws;
|
|
64
|
+
};
|
|
65
|
+
return init();
|
|
66
|
+
}
|
|
67
|
+
function reconnectWebSocket() {
|
|
68
|
+
var reconnectTimeout;
|
|
69
|
+
var reconnectNumber = 0;
|
|
70
|
+
var open = function (ws, config, callback) {
|
|
71
|
+
var reconnectTime = config.reconnectTime, _a = config.maxReconnectNumber, maxReconnectNumber = _a === void 0 ? 1 : _a;
|
|
72
|
+
var isOpen = getState(ws, 'OPEN');
|
|
73
|
+
if (isOpen || (reconnectTimeout && reconnectNumber >= maxReconnectNumber)) {
|
|
74
|
+
clearTimeout(reconnectTimeout);
|
|
75
|
+
reconnectTimeout = null;
|
|
76
|
+
reconnectNumber = null;
|
|
77
|
+
config.reconnect = false;
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
reconnectTimeout = setTimeout(function () {
|
|
81
|
+
reconnectNumber += 1;
|
|
82
|
+
callback();
|
|
83
|
+
}, reconnectTime);
|
|
84
|
+
};
|
|
85
|
+
return open;
|
|
86
|
+
}
|
|
87
|
+
function useSendMessage(ws, data) {
|
|
88
|
+
var isOpen = getState(ws, 'OPEN');
|
|
89
|
+
if (isOpen)
|
|
90
|
+
ws.send(JSON.stringify(data));
|
|
91
|
+
}
|
|
92
|
+
function useWebSocketMessage(ws, callback) {
|
|
93
|
+
try {
|
|
94
|
+
ws.onmessage = function (e) {
|
|
95
|
+
var data = JSON.parse(e.data);
|
|
96
|
+
callback(data, e);
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
catch (error) {
|
|
100
|
+
console.error(error);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
function useWebSocketClose(ws, code, message) {
|
|
104
|
+
code = code || 1000;
|
|
105
|
+
message = message || 'connection closed by client';
|
|
106
|
+
ws.close(code, message);
|
|
107
|
+
}
|
|
108
|
+
function getState(ws, type) {
|
|
109
|
+
return ws.readyState === WebSocket[type];
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
exports.createWebSocket = createWebSocket;
|
|
113
|
+
exports.getState = getState;
|
|
114
|
+
exports.useSendMessage = useSendMessage;
|
|
115
|
+
exports.useWebSocketClose = useWebSocketClose;
|
|
116
|
+
exports.useWebSocketMessage = useWebSocketMessage;
|
|
117
|
+
|
|
118
|
+
}));
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lightsoft/js-sdk",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.umd.js",
|
|
6
6
|
"module": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
8
8
|
"type": "module",
|
|
9
|
-
|
|
9
|
+
"files": [
|
|
10
10
|
"dist"
|
|
11
11
|
],
|
|
12
12
|
"scripts": {
|
|
@@ -17,11 +17,15 @@
|
|
|
17
17
|
"author": "",
|
|
18
18
|
"license": "ISC",
|
|
19
19
|
"devDependencies": {
|
|
20
|
+
"@rollup/plugin-commonjs": "^28.0.6",
|
|
21
|
+
"@rollup/plugin-node-resolve": "^16.0.1",
|
|
20
22
|
"@rollup/plugin-typescript": "^12.1.4",
|
|
21
|
-
"rollup": "^4.
|
|
23
|
+
"rollup": "^4.46.2",
|
|
22
24
|
"rollup-plugin-dts": "^6.2.1",
|
|
25
|
+
"rollup-plugin-vue": "^6.0.0",
|
|
23
26
|
"ts-node": "^10.9.2",
|
|
24
27
|
"tslib": "^2.8.1",
|
|
25
|
-
"typescript": "^5.8.3"
|
|
28
|
+
"typescript": "^5.8.3",
|
|
29
|
+
"vue": "^3.5.18"
|
|
26
30
|
}
|
|
27
31
|
}
|