@shopgate/pwa-common 7.30.1-beta.2 → 7.30.1-beta.4
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.
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
export type RedirectHandler =
|
|
2
|
+
| string
|
|
3
|
+
| Promise<string>
|
|
4
|
+
| ((...args: any[]) => string | Promise<string>);
|
|
5
|
+
|
|
6
|
+
export type RedirectOptions = {
|
|
7
|
+
/**
|
|
8
|
+
* Whether to show a loading indicator while the redirect is being processed.
|
|
9
|
+
* @default true
|
|
10
|
+
*/
|
|
11
|
+
showLoading?: boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Whether to override an existing redirect.
|
|
14
|
+
* @default false
|
|
15
|
+
*/
|
|
16
|
+
override?: boolean;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export interface RedirectExtendedData {
|
|
20
|
+
/**
|
|
21
|
+
* The value passed as "from" to set()
|
|
22
|
+
*/
|
|
23
|
+
matcher?: string;
|
|
24
|
+
/**
|
|
25
|
+
* The value passed as "to" to set()
|
|
26
|
+
*/
|
|
27
|
+
handler: RedirectHandler;
|
|
28
|
+
/**
|
|
29
|
+
* Decoded params from the pathname - defined within the matcher
|
|
30
|
+
*/
|
|
31
|
+
pathParams?: Record<string, any> | null;
|
|
32
|
+
/**
|
|
33
|
+
* Decoded query params from the pathname
|
|
34
|
+
*/
|
|
35
|
+
queryParams: Record<string, any>;
|
|
36
|
+
/**
|
|
37
|
+
* Additional options for the redirect, passed as the third argument to set()
|
|
38
|
+
*/
|
|
39
|
+
options?: RedirectOptions | null;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export class Redirects {
|
|
43
|
+
/**
|
|
44
|
+
* Returns a specified element from the internal "redirects" Map object.
|
|
45
|
+
* @param pathname The pathname to lookup.
|
|
46
|
+
*/
|
|
47
|
+
get(pathname: string): RedirectHandler | null;
|
|
48
|
+
/**
|
|
49
|
+
* Returns the redirect for a passed pathname.
|
|
50
|
+
* @param {string} pathname The pathname to check.
|
|
51
|
+
*/
|
|
52
|
+
getRedirect(pathname: string): RedirectHandler | null;
|
|
53
|
+
/**
|
|
54
|
+
* Unlike "getRedirect" which only returns a matching handler for a passed pathname, this method
|
|
55
|
+
* returns an object that contains some extended data.
|
|
56
|
+
* @param pathname The pathname to check.
|
|
57
|
+
*/
|
|
58
|
+
getRedirectExtended(pathname: string): RedirectExtendedData | null;
|
|
59
|
+
/**
|
|
60
|
+
* Adds a redirect handler to the collection.
|
|
61
|
+
* @param from The link to redirect from. Route patterns are also supported.
|
|
62
|
+
* @param to redirect / handle to create a dynamic link
|
|
63
|
+
* @param force Whether or not to forcefully set the redirect.
|
|
64
|
+
*/
|
|
65
|
+
set(from?: string | null, to?: RedirectHandler | null, force?: boolean): void;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Adds a redirect handler to the collection.
|
|
69
|
+
* @param from The link to redirect from. Route patterns are also supported.
|
|
70
|
+
* @param to redirect / handle to create a dynamic link
|
|
71
|
+
* @param options Additional options for the redirect.
|
|
72
|
+
* @param force Whether or not to forcefully set the redirect.
|
|
73
|
+
*/
|
|
74
|
+
set(from?: string | null, to?: RedirectHandler | null, options?: RedirectOptions, force?: boolean): void;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Removes a specified element from the internal "redirects" Map object.
|
|
78
|
+
* @param pathname The pathname to remove.
|
|
79
|
+
*/
|
|
80
|
+
unset(pathname: string): void;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
declare const redirects: Redirects;
|
|
84
|
+
export default redirects;
|
package/collections/Redirects.js
CHANGED
|
@@ -10,6 +10,7 @@ let Redirects = /*#__PURE__*/function () {
|
|
|
10
10
|
*/
|
|
11
11
|
function Redirects() {
|
|
12
12
|
this.redirects = new Map();
|
|
13
|
+
this.redirectOptions = new Map();
|
|
13
14
|
this.matcher = pathMatch({
|
|
14
15
|
sensitive: false,
|
|
15
16
|
strict: false,
|
|
@@ -99,6 +100,11 @@ let Redirects = /*#__PURE__*/function () {
|
|
|
99
100
|
result.matcher = patternMatch;
|
|
100
101
|
result.pathParams = matcherResult;
|
|
101
102
|
}
|
|
103
|
+
const options = {
|
|
104
|
+
showLoading: true,
|
|
105
|
+
...this.redirectOptions.get(pathname)
|
|
106
|
+
};
|
|
107
|
+
result.options = options;
|
|
102
108
|
return result;
|
|
103
109
|
}
|
|
104
110
|
|
|
@@ -106,29 +112,31 @@ let Redirects = /*#__PURE__*/function () {
|
|
|
106
112
|
* Adds a redirect handler to the collection.
|
|
107
113
|
* @param {string} from The link to redirect from. Route patterns are also supported.
|
|
108
114
|
* @param {string|Function|Promise} to redirect / handle to create a dynamic link.
|
|
109
|
-
* @param {
|
|
115
|
+
* @param {Object} forceOrOptions Additional options for the redirect.
|
|
110
116
|
*/;
|
|
111
|
-
_proto.set = function set(from = null, to = null,
|
|
117
|
+
_proto.set = function set(from = null, to = null, forceOrOptions = false) {
|
|
112
118
|
if (!from || !to) {
|
|
113
119
|
return;
|
|
114
120
|
}
|
|
115
|
-
|
|
121
|
+
const options = typeof forceOrOptions === 'object' ? forceOrOptions : null;
|
|
122
|
+
const forceFlag = typeof forceOrOptions === 'boolean' ? forceOrOptions : forceOrOptions?.override;
|
|
123
|
+
if (!forceFlag && this.redirects.has(from)) {
|
|
116
124
|
return;
|
|
117
125
|
}
|
|
118
126
|
this.redirects.set(from, to);
|
|
127
|
+
if (options) {
|
|
128
|
+
this.redirectOptions.set(from, options);
|
|
129
|
+
}
|
|
119
130
|
}
|
|
120
131
|
|
|
121
|
-
/* eslint-disable extra-rules/potential-point-free */
|
|
122
|
-
|
|
123
132
|
/**
|
|
124
133
|
* Removes a specified element from the internal "redirects" Map object.
|
|
125
134
|
* @param {string} pathname The pathname to remove.
|
|
126
135
|
*/;
|
|
127
136
|
_proto.unset = function unset(pathname) {
|
|
128
137
|
this.redirects.delete(pathname);
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
/* eslint-enable extra-rules/potential-point-free */;
|
|
138
|
+
this.redirectOptions.delete(pathname);
|
|
139
|
+
};
|
|
132
140
|
return Redirects;
|
|
133
141
|
}();
|
|
134
142
|
export default new Redirects();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shopgate/pwa-common",
|
|
3
|
-
"version": "7.30.1-beta.
|
|
3
|
+
"version": "7.30.1-beta.4",
|
|
4
4
|
"description": "Common library for the Shopgate Connect PWA.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "Shopgate <support@shopgate.com>",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"dependencies": {
|
|
18
18
|
"@redux-devtools/extension": "^3.3.0",
|
|
19
19
|
"@sentry/browser": "6.0.1",
|
|
20
|
-
"@shopgate/pwa-benchmark": "7.30.1-beta.
|
|
20
|
+
"@shopgate/pwa-benchmark": "7.30.1-beta.4",
|
|
21
21
|
"@virtuous/conductor": "~2.5.0",
|
|
22
22
|
"@virtuous/react-conductor": "~2.5.0",
|
|
23
23
|
"@virtuous/redux-persister": "1.1.0-beta.7",
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
"swiper": "12.1.0"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
|
-
"@shopgate/pwa-core": "7.30.1-beta.
|
|
43
|
+
"@shopgate/pwa-core": "7.30.1-beta.4",
|
|
44
44
|
"@types/react-portal": "^3.0.9",
|
|
45
45
|
"lodash": "^4.17.23",
|
|
46
46
|
"prop-types": "~15.8.1",
|
package/subscriptions/router.js
CHANGED
|
@@ -172,7 +172,8 @@ export default function routerSubscriptions(subscribe) {
|
|
|
172
172
|
handler: redirect,
|
|
173
173
|
matcher,
|
|
174
174
|
pathParams,
|
|
175
|
-
queryParams
|
|
175
|
+
queryParams,
|
|
176
|
+
options
|
|
176
177
|
} = redirects.getRedirectExtended(location) || {};
|
|
177
178
|
/* eslint-enable prefer-const */
|
|
178
179
|
|
|
@@ -181,7 +182,9 @@ export default function routerSubscriptions(subscribe) {
|
|
|
181
182
|
const {
|
|
182
183
|
pathname
|
|
183
184
|
} = getCurrentRoute(state);
|
|
184
|
-
|
|
185
|
+
if (options.showLoading) {
|
|
186
|
+
LoadingProvider.setLoading(pathname);
|
|
187
|
+
}
|
|
185
188
|
const pattern = router.findPattern(location.split('?')[0]);
|
|
186
189
|
const {
|
|
187
190
|
transform
|
|
@@ -215,7 +218,9 @@ export default function routerSubscriptions(subscribe) {
|
|
|
215
218
|
redirect = null;
|
|
216
219
|
logger.error(e);
|
|
217
220
|
}
|
|
218
|
-
|
|
221
|
+
if (options.showLoading) {
|
|
222
|
+
LoadingProvider.unsetLoading(pathname);
|
|
223
|
+
}
|
|
219
224
|
if (!redirect) {
|
|
220
225
|
return;
|
|
221
226
|
}
|