@ray-js/wechat-router 0.0.1-beta-1

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/index..js ADDED
@@ -0,0 +1,213 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ /******************************************************************************
6
+ Copyright (c) Microsoft Corporation.
7
+
8
+ Permission to use, copy, modify, and/or distribute this software for any
9
+ purpose with or without fee is hereby granted.
10
+
11
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17
+ PERFORMANCE OF THIS SOFTWARE.
18
+ ***************************************************************************** */
19
+
20
+ function __awaiter(thisArg, _arguments, P, generator) {
21
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
22
+ return new (P || (P = Promise))(function (resolve, reject) {
23
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
24
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
25
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
26
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
27
+ });
28
+ }
29
+
30
+ const routeConfig = {}; // TODO 无页面时跳到一个404页面
31
+
32
+ let url404 = '';
33
+ /**
34
+ * 注册路由
35
+ * @param spaceName
36
+ * @param data
37
+ */
38
+
39
+ const register = (spaceName, data) => {
40
+ if (!routeConfig[spaceName]) {
41
+ initSpace(spaceName);
42
+ }
43
+
44
+ Object.assign(routeConfig[spaceName].routes, data);
45
+ };
46
+ /**
47
+ * 获取路由地址
48
+ * @param spaceName
49
+ * @param name
50
+ * @param params
51
+ * @returns
52
+ */
53
+
54
+
55
+ const getRouteUrl = (spaceName, name, params) => {
56
+ const space = routeConfig[spaceName];
57
+
58
+ if (space) {
59
+ const config = space.routes[name];
60
+
61
+ if (config) {
62
+ const {
63
+ url,
64
+ params: basicParams
65
+ } = config;
66
+ const data = Object.assign(Object.assign({}, basicParams), params);
67
+ const queryData = Object.keys(data).reduce((res, key) => {
68
+ const value = data[key];
69
+
70
+ if (typeof value !== 'undefined') {
71
+ res.push(`${key}=${encodeURIComponent(value)}`);
72
+ }
73
+
74
+ return res;
75
+ }, []);
76
+
77
+ if (queryData.length) {
78
+ return `${url}?${queryData.join('&')}`;
79
+ }
80
+
81
+ return url;
82
+ }
83
+ }
84
+
85
+ return url404;
86
+ };
87
+ /**
88
+ * 跳页
89
+ * @param spaceName
90
+ * @param name
91
+ * @param params
92
+ */
93
+
94
+
95
+ const push = (spaceName, name, params, options) => __awaiter(void 0, void 0, void 0, function* () {
96
+ return new Promise((resovle, reject) => {
97
+ if (wx && wx.navigateTo) {
98
+ wx.navigateTo({
99
+ url: getRouteUrl(spaceName, name, params),
100
+ events: options === null || options === void 0 ? void 0 : options.events,
101
+ fail: reject,
102
+ success: resovle
103
+ });
104
+ } // TODO 其他平台
105
+
106
+
107
+ reject('only support wechat');
108
+ });
109
+ });
110
+ /**
111
+ * 跳页,并关掉当前页
112
+ * @param spaceName
113
+ * @param name
114
+ * @param params
115
+ */
116
+
117
+
118
+ const replace = (spaceName, name, params) => __awaiter(void 0, void 0, void 0, function* () {
119
+ return new Promise((resovle, reject) => {
120
+ if (wx && wx.redirectTo) {
121
+ wx.redirectTo({
122
+ url: getRouteUrl(spaceName, name, params),
123
+ fail: reject,
124
+ success: resovle
125
+ });
126
+ } // TODO 其他平台
127
+
128
+
129
+ reject('only support wechat');
130
+ });
131
+ });
132
+ /**
133
+ * 后退
134
+ * @param delta
135
+ */
136
+
137
+
138
+ const back = (delta = 1) => __awaiter(void 0, void 0, void 0, function* () {
139
+ return new Promise((resovle, reject) => {
140
+ if (wx && wx.navigateBack) {
141
+ wx.navigateBack({
142
+ delta,
143
+ fail: reject,
144
+ success: resovle
145
+ });
146
+ } // TODO 其他平台
147
+
148
+
149
+ reject('only support wechat');
150
+ });
151
+ });
152
+ /**
153
+ * 跳页,并把其他所有页面关闭
154
+ * @param name
155
+ * @param params
156
+ */
157
+
158
+
159
+ const reLaunch = (spaceName, name, params) => __awaiter(void 0, void 0, void 0, function* () {
160
+ return new Promise((resovle, reject) => {
161
+ if (wx && wx.reLaunch) {
162
+ wx.reLaunch({
163
+ url: getRouteUrl(spaceName, name, params),
164
+ fail: reject,
165
+ success: resovle
166
+ });
167
+ } // TODO 其他平台
168
+
169
+
170
+ reject('only support wechat');
171
+ });
172
+ });
173
+ /**
174
+ * 返回路由地址
175
+ * @param spaceName
176
+ * @param name
177
+ * @param params
178
+ * @returns
179
+ */
180
+
181
+
182
+ const getPath = (spaceName, name, params) => {
183
+ return getRouteUrl(spaceName, name, params);
184
+ };
185
+ /**
186
+ * 指定使用的空间名
187
+ * @param spaceName
188
+ */
189
+
190
+
191
+ const initSpace = (spaceName, options) => {
192
+ var _a;
193
+
194
+ routeConfig[spaceName] = {
195
+ name: spaceName,
196
+ routes: {}
197
+ };
198
+ url404 = (_a = options === null || options === void 0 ? void 0 : options.url404) !== null && _a !== void 0 ? _a : '';
199
+ return {
200
+ register: routes => register(spaceName, routes),
201
+ push: (name, params, options) => push(spaceName, name, params, options),
202
+ reLaunch: (name, params) => reLaunch(spaceName, name, params),
203
+ replace: (name, params) => replace(spaceName, name, params),
204
+ back,
205
+ getPath: (name, params) => getPath(spaceName, name, params)
206
+ };
207
+ };
208
+ var index = {
209
+ initSpace
210
+ };
211
+
212
+ exports["default"] = index;
213
+ exports.initSpace = initSpace;
package/index.esm.js ADDED
@@ -0,0 +1,208 @@
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
+
16
+ function __awaiter(thisArg, _arguments, P, generator) {
17
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
18
+ return new (P || (P = Promise))(function (resolve, reject) {
19
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
20
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
21
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
22
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
23
+ });
24
+ }
25
+
26
+ const routeConfig = {}; // TODO 无页面时跳到一个404页面
27
+
28
+ let url404 = '';
29
+ /**
30
+ * 注册路由
31
+ * @param spaceName
32
+ * @param data
33
+ */
34
+
35
+ const register = (spaceName, data) => {
36
+ if (!routeConfig[spaceName]) {
37
+ initSpace(spaceName);
38
+ }
39
+
40
+ Object.assign(routeConfig[spaceName].routes, data);
41
+ };
42
+ /**
43
+ * 获取路由地址
44
+ * @param spaceName
45
+ * @param name
46
+ * @param params
47
+ * @returns
48
+ */
49
+
50
+
51
+ const getRouteUrl = (spaceName, name, params) => {
52
+ const space = routeConfig[spaceName];
53
+
54
+ if (space) {
55
+ const config = space.routes[name];
56
+
57
+ if (config) {
58
+ const {
59
+ url,
60
+ params: basicParams
61
+ } = config;
62
+ const data = Object.assign(Object.assign({}, basicParams), params);
63
+ const queryData = Object.keys(data).reduce((res, key) => {
64
+ const value = data[key];
65
+
66
+ if (typeof value !== 'undefined') {
67
+ res.push(`${key}=${encodeURIComponent(value)}`);
68
+ }
69
+
70
+ return res;
71
+ }, []);
72
+
73
+ if (queryData.length) {
74
+ return `${url}?${queryData.join('&')}`;
75
+ }
76
+
77
+ return url;
78
+ }
79
+ }
80
+
81
+ return url404;
82
+ };
83
+ /**
84
+ * 跳页
85
+ * @param spaceName
86
+ * @param name
87
+ * @param params
88
+ */
89
+
90
+
91
+ const push = (spaceName, name, params, options) => __awaiter(void 0, void 0, void 0, function* () {
92
+ return new Promise((resovle, reject) => {
93
+ if (wx && wx.navigateTo) {
94
+ wx.navigateTo({
95
+ url: getRouteUrl(spaceName, name, params),
96
+ events: options === null || options === void 0 ? void 0 : options.events,
97
+ fail: reject,
98
+ success: resovle
99
+ });
100
+ } // TODO 其他平台
101
+
102
+
103
+ reject('only support wechat');
104
+ });
105
+ });
106
+ /**
107
+ * 跳页,并关掉当前页
108
+ * @param spaceName
109
+ * @param name
110
+ * @param params
111
+ */
112
+
113
+
114
+ const replace = (spaceName, name, params) => __awaiter(void 0, void 0, void 0, function* () {
115
+ return new Promise((resovle, reject) => {
116
+ if (wx && wx.redirectTo) {
117
+ wx.redirectTo({
118
+ url: getRouteUrl(spaceName, name, params),
119
+ fail: reject,
120
+ success: resovle
121
+ });
122
+ } // TODO 其他平台
123
+
124
+
125
+ reject('only support wechat');
126
+ });
127
+ });
128
+ /**
129
+ * 后退
130
+ * @param delta
131
+ */
132
+
133
+
134
+ const back = (delta = 1) => __awaiter(void 0, void 0, void 0, function* () {
135
+ return new Promise((resovle, reject) => {
136
+ if (wx && wx.navigateBack) {
137
+ wx.navigateBack({
138
+ delta,
139
+ fail: reject,
140
+ success: resovle
141
+ });
142
+ } // TODO 其他平台
143
+
144
+
145
+ reject('only support wechat');
146
+ });
147
+ });
148
+ /**
149
+ * 跳页,并把其他所有页面关闭
150
+ * @param name
151
+ * @param params
152
+ */
153
+
154
+
155
+ const reLaunch = (spaceName, name, params) => __awaiter(void 0, void 0, void 0, function* () {
156
+ return new Promise((resovle, reject) => {
157
+ if (wx && wx.reLaunch) {
158
+ wx.reLaunch({
159
+ url: getRouteUrl(spaceName, name, params),
160
+ fail: reject,
161
+ success: resovle
162
+ });
163
+ } // TODO 其他平台
164
+
165
+
166
+ reject('only support wechat');
167
+ });
168
+ });
169
+ /**
170
+ * 返回路由地址
171
+ * @param spaceName
172
+ * @param name
173
+ * @param params
174
+ * @returns
175
+ */
176
+
177
+
178
+ const getPath = (spaceName, name, params) => {
179
+ return getRouteUrl(spaceName, name, params);
180
+ };
181
+ /**
182
+ * 指定使用的空间名
183
+ * @param spaceName
184
+ */
185
+
186
+
187
+ const initSpace = (spaceName, options) => {
188
+ var _a;
189
+
190
+ routeConfig[spaceName] = {
191
+ name: spaceName,
192
+ routes: {}
193
+ };
194
+ url404 = (_a = options === null || options === void 0 ? void 0 : options.url404) !== null && _a !== void 0 ? _a : '';
195
+ return {
196
+ register: routes => register(spaceName, routes),
197
+ push: (name, params, options) => push(spaceName, name, params, options),
198
+ reLaunch: (name, params) => reLaunch(spaceName, name, params),
199
+ replace: (name, params) => replace(spaceName, name, params),
200
+ back,
201
+ getPath: (name, params) => getPath(spaceName, name, params)
202
+ };
203
+ };
204
+ var index = {
205
+ initSpace
206
+ };
207
+
208
+ export { index as default, initSpace };
package/package.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "@ray-js/wechat-router",
3
+ "version": "0.0.1-beta-1",
4
+ "publishConfig": {
5
+ "access": "public",
6
+ "registry": "https://registry.npmjs.org"
7
+ },
8
+ "module": "./index.esm.js",
9
+ "main": "./index.js",
10
+ "type": "module",
11
+ "types": "./src/index.d.ts",
12
+ "exports": {
13
+ ".": {
14
+ "types": "./src/index.d.ts",
15
+ "import": "./index.esm.js",
16
+ "require": "./index.js"
17
+ }
18
+ }
19
+ }
package/src/index.d.ts ADDED
@@ -0,0 +1,45 @@
1
+ interface Params {
2
+ [key: string]: string | number | boolean;
3
+ }
4
+ interface Route {
5
+ name: string;
6
+ url: string;
7
+ params?: Params;
8
+ }
9
+ interface RouteOption {
10
+ events: {
11
+ [eventType: string]: (...args: any[]) => void;
12
+ };
13
+ }
14
+ declare let url404: string;
15
+ /**
16
+ * 指定使用的空间名
17
+ * @param spaceName
18
+ */
19
+ export declare const initSpace: (spaceName: string, options?: {
20
+ url404: string;
21
+ }) => {
22
+ register: (routes: {
23
+ [key: string]: Route;
24
+ }) => void;
25
+ push: (name: string, params?: Params, options?: RouteOption) => Promise<unknown>;
26
+ reLaunch: (name: string, params?: Params) => Promise<unknown>;
27
+ replace: (name: string, params?: Params) => Promise<unknown>;
28
+ back: (delta?: number) => Promise<unknown>;
29
+ getPath: (name: string, params?: Params) => string;
30
+ };
31
+ declare const _default: {
32
+ initSpace: (spaceName: string, options?: {
33
+ url404: string;
34
+ } | undefined) => {
35
+ register: (routes: {
36
+ [key: string]: Route;
37
+ }) => void;
38
+ push: (name: string, params?: Params | undefined, options?: RouteOption | undefined) => Promise<unknown>;
39
+ reLaunch: (name: string, params?: Params | undefined) => Promise<unknown>;
40
+ replace: (name: string, params?: Params | undefined) => Promise<unknown>;
41
+ back: (delta?: number) => Promise<unknown>;
42
+ getPath: (name: string, params?: Params | undefined) => string;
43
+ };
44
+ };
45
+ export default _default;