@syfei49/mini-dynamic-renderer 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/src/handler.js CHANGED
@@ -1,49 +1,43 @@
1
- /**
2
- * 从响应数据中解析渲染配置(预留,供后续版本使用)
3
- * @param {*} data - 接口返回的 data 字段
4
- * @returns {Object|null}
5
- */
6
- function parseRenderConfig(data) {
7
- if (!data || typeof data !== 'object') {
8
- return null;
9
- }
10
-
11
- if (data.renderConfig && typeof data.renderConfig === 'object') {
12
- return data.renderConfig;
13
- }
14
-
15
- if (data.type) {
16
- return data;
17
- }
18
-
19
- return null;
20
- }
21
-
22
- /**
23
- * v1.0.0: 拦截成功后弹出 hello world 测试弹窗
24
- * @param {Object} res - wx.request success 回调的响应对象
25
- * @param {Object} requestOptions - 原始 wx.request 入参
26
- * @param {Object} config - SDK 配置
27
- */
28
- function handleResponse(res, requestOptions, config) {
29
- var renderConfig = parseRenderConfig(res.data);
30
-
31
- if (config.debug) {
32
- console.log('[mini-dynamic-renderer] intercepted:', requestOptions.url, res.data);
33
- }
34
-
35
- wx.showModal({
36
- title: 'mini-dynamic-renderer',
37
- content: 'hello world',
38
- showCancel: false
39
- });
40
-
41
- if (renderConfig && config.debug) {
42
- console.log('[mini-dynamic-renderer] renderConfig:', renderConfig);
43
- }
44
- }
45
-
46
- module.exports = {
47
- parseRenderConfig: parseRenderConfig,
48
- handleResponse: handleResponse
49
- };
1
+ /**
2
+ * 从响应数据中解析渲染配置(预留,供后续版本使用)
3
+ * @param {*} data - 接口返回的 data 字段
4
+ * @returns {Object|null}
5
+ */
6
+ function parseRenderConfig(data) {
7
+ if (!data || typeof data !== 'object') {
8
+ return null;
9
+ }
10
+
11
+ if (data.renderConfig && typeof data.renderConfig === 'object') {
12
+ return data.renderConfig;
13
+ }
14
+
15
+ if (data.type) {
16
+ return data;
17
+ }
18
+
19
+ return null;
20
+ }
21
+
22
+ /**
23
+ * 拦截响应处理(预留,默认不再弹窗)
24
+ * @param {Object} res - wx.request success 回调的响应对象
25
+ * @param {Object} requestOptions - 原始 wx.request 入参
26
+ * @param {Object} config - SDK 配置
27
+ */
28
+ function handleResponse(res, requestOptions, config) {
29
+ var renderConfig = parseRenderConfig(res.data);
30
+
31
+ if (config.debug) {
32
+ console.log('[mini-dynamic-renderer] intercepted:', requestOptions.url, res.data);
33
+ }
34
+
35
+ if (renderConfig && config.debug) {
36
+ console.log('[mini-dynamic-renderer] renderConfig:', renderConfig);
37
+ }
38
+ }
39
+
40
+ module.exports = {
41
+ parseRenderConfig: parseRenderConfig,
42
+ handleResponse: handleResponse
43
+ };
package/src/init.js CHANGED
@@ -1,57 +1,38 @@
1
- var interceptor = require('./interceptor');
2
-
3
- var currentConfig = null;
4
- var initialized = false;
5
-
6
- /**
7
- * 合并用户配置与默认配置
8
- * @param {Object} userConfig
9
- * @returns {Object}
10
- */
11
- function mergeConfig(userConfig) {
12
- var base = interceptor.DEFAULT_CONFIG;
13
- var config = userConfig || {};
14
-
15
- return {
16
- urlPatterns: config.urlPatterns || base.urlPatterns,
17
- debug: typeof config.debug === 'boolean' ? config.debug : base.debug
18
- };
19
- }
20
-
21
- /**
22
- * 初始化 SDK,注册 wx.request 拦截器
23
- * @param {Object} [userConfig]
24
- * @param {Array<string|RegExp>} [userConfig.urlPatterns] - URL 匹配规则,默认 ['*'] 匹配全部
25
- * @param {boolean} [userConfig.debug] - 是否输出调试日志
26
- * @returns {Object} 当前配置
27
- */
28
- function init(userConfig) {
29
- if (initialized && !userConfig) {
30
- return currentConfig;
31
- }
32
-
33
- if (initialized && userConfig) {
34
- interceptor.uninstallInterceptor();
35
- initialized = false;
36
- }
37
-
38
- currentConfig = mergeConfig(userConfig);
39
- interceptor.installInterceptor(currentConfig);
40
- initialized = true;
41
-
42
- if (currentConfig.debug) {
43
- console.log('[mini-dynamic-renderer] initialized', currentConfig);
44
- }
45
-
46
- return currentConfig;
47
- }
48
-
49
- module.exports = {
50
- init: init,
51
- getConfig: function () {
52
- return currentConfig;
53
- },
54
- isInitialized: function () {
55
- return initialized;
56
- }
57
- };
1
+ var interceptor = require('./interceptor');
2
+ var configModule = require('./config');
3
+ var chatService = require('./chat/service');
4
+
5
+ var initialized = false;
6
+
7
+ /**
8
+ * 初始化 SDK
9
+ * @param {Object} [userConfig]
10
+ * @returns {Object} 当前配置
11
+ */
12
+ function init(userConfig) {
13
+ if (initialized) {
14
+ return configModule.getConfig();
15
+ }
16
+
17
+ var config = configModule.setConfig(userConfig);
18
+ initialized = true;
19
+
20
+ if (config.enableInterceptor) {
21
+ interceptor.installInterceptor(config);
22
+ }
23
+
24
+ if (config.debug) {
25
+ console.log('[mini-dynamic-renderer] initialized', config);
26
+ }
27
+
28
+ return config;
29
+ }
30
+
31
+ function isInitialized() {
32
+ return initialized;
33
+ }
34
+
35
+ module.exports = {
36
+ init: init,
37
+ isInitialized: isInitialized
38
+ };
@@ -1,104 +1,105 @@
1
- var handler = require('./handler');
2
-
3
- var originalRequest = null;
4
- var installed = false;
5
-
6
- var DEFAULT_CONFIG = {
7
- urlPatterns: ['*'],
8
- debug: false
9
- };
10
-
11
- /**
12
- * 判断 URL 是否匹配配置的模式
13
- * @param {string} url
14
- * @param {Array<string|RegExp>} patterns
15
- * @returns {boolean}
16
- */
17
- function matchUrl(url, patterns) {
18
- if (!patterns || patterns.length === 0) {
19
- return true;
20
- }
21
-
22
- for (var i = 0; i < patterns.length; i++) {
23
- var pattern = patterns[i];
24
-
25
- if (pattern === '*') {
26
- return true;
27
- }
28
-
29
- if (pattern instanceof RegExp) {
30
- if (pattern.test(url)) {
31
- return true;
32
- }
33
- continue;
34
- }
35
-
36
- if (typeof pattern === 'string' && url.indexOf(pattern) !== -1) {
37
- return true;
38
- }
39
- }
40
-
41
- return false;
42
- }
43
-
44
- /**
45
- * 安装 wx.request 拦截器
46
- * @param {Object} config
47
- */
48
- function installInterceptor(config) {
49
- if (installed) {
50
- return;
51
- }
52
-
53
- if (typeof wx === 'undefined' || typeof wx.request !== 'function') {
54
- console.warn('[mini-dynamic-renderer] wx.request is not available, skip interceptor install');
55
- return;
56
- }
57
-
58
- originalRequest = wx.request;
59
- installed = true;
60
-
61
- wx.request = function (options) {
62
- var userSuccess = options.success;
63
- var mergedConfig = config || DEFAULT_CONFIG;
64
-
65
- options.success = function (res) {
66
- if (res && res.statusCode === 200 && matchUrl(options.url || '', mergedConfig.urlPatterns)) {
67
- try {
68
- handler.handleResponse(res, options, mergedConfig);
69
- } catch (err) {
70
- console.error('[mini-dynamic-renderer] handleResponse error:', err);
71
- }
72
- }
73
-
74
- if (typeof userSuccess === 'function') {
75
- userSuccess(res);
76
- }
77
- };
78
-
79
- return originalRequest.call(wx, options);
80
- };
81
- }
82
-
83
- /**
84
- * 卸载拦截器,恢复原始 wx.request
85
- */
86
- function uninstallInterceptor() {
87
- if (!installed || !originalRequest) {
88
- return;
89
- }
90
-
91
- wx.request = originalRequest;
92
- originalRequest = null;
93
- installed = false;
94
- }
95
-
96
- module.exports = {
97
- DEFAULT_CONFIG: DEFAULT_CONFIG,
98
- matchUrl: matchUrl,
99
- installInterceptor: installInterceptor,
100
- uninstallInterceptor: uninstallInterceptor,
101
- isInstalled: function () {
102
- return installed;
103
- }
104
- };
1
+ var handler = require('./handler');
2
+
3
+ var originalRequest = null;
4
+ var installed = false;
5
+
6
+ var DEFAULT_CONFIG = {
7
+ urlPatterns: ['*'],
8
+ debug: false,
9
+ enableInterceptor: false
10
+ };
11
+
12
+ /**
13
+ * 判断 URL 是否匹配配置的模式
14
+ * @param {string} url
15
+ * @param {Array<string|RegExp>} patterns
16
+ * @returns {boolean}
17
+ */
18
+ function matchUrl(url, patterns) {
19
+ if (!patterns || patterns.length === 0) {
20
+ return true;
21
+ }
22
+
23
+ for (var i = 0; i < patterns.length; i++) {
24
+ var pattern = patterns[i];
25
+
26
+ if (pattern === '*') {
27
+ return true;
28
+ }
29
+
30
+ if (pattern instanceof RegExp) {
31
+ if (pattern.test(url)) {
32
+ return true;
33
+ }
34
+ continue;
35
+ }
36
+
37
+ if (typeof pattern === 'string' && url.indexOf(pattern) !== -1) {
38
+ return true;
39
+ }
40
+ }
41
+
42
+ return false;
43
+ }
44
+
45
+ /**
46
+ * 安装 wx.request 拦截器
47
+ * @param {Object} config
48
+ */
49
+ function installInterceptor(config) {
50
+ if (installed) {
51
+ return;
52
+ }
53
+
54
+ if (typeof wx === 'undefined' || typeof wx.request !== 'function') {
55
+ console.warn('[mini-dynamic-renderer] wx.request is not available, skip interceptor install');
56
+ return;
57
+ }
58
+
59
+ originalRequest = wx.request;
60
+ installed = true;
61
+
62
+ wx.request = function (options) {
63
+ var userSuccess = options.success;
64
+ var mergedConfig = config || DEFAULT_CONFIG;
65
+
66
+ options.success = function (res) {
67
+ if (res && res.statusCode === 200 && matchUrl(options.url || '', mergedConfig.urlPatterns)) {
68
+ try {
69
+ handler.handleResponse(res, options, mergedConfig);
70
+ } catch (err) {
71
+ console.error('[mini-dynamic-renderer] handleResponse error:', err);
72
+ }
73
+ }
74
+
75
+ if (typeof userSuccess === 'function') {
76
+ userSuccess(res);
77
+ }
78
+ };
79
+
80
+ return originalRequest.call(wx, options);
81
+ };
82
+ }
83
+
84
+ /**
85
+ * 卸载拦截器,恢复原始 wx.request
86
+ */
87
+ function uninstallInterceptor() {
88
+ if (!installed || !originalRequest) {
89
+ return;
90
+ }
91
+
92
+ wx.request = originalRequest;
93
+ originalRequest = null;
94
+ installed = false;
95
+ }
96
+
97
+ module.exports = {
98
+ DEFAULT_CONFIG: DEFAULT_CONFIG,
99
+ matchUrl: matchUrl,
100
+ installInterceptor: installInterceptor,
101
+ uninstallInterceptor: uninstallInterceptor,
102
+ isInstalled: function () {
103
+ return installed;
104
+ }
105
+ };