@shopgate/pwa-common 7.30.2-beta.1 → 7.30.2-beta.3

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 | null>
4
+ | ((...args: any[]) => string | Promise<string | null> | null);
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
+ /**
61
+ * Adds a redirect handler to the collection.
62
+ * @param from The link to redirect from. Route patterns are also supported.
63
+ * @param to redirect / handle to create a dynamic link
64
+ * @param options Additional options for the redirect.
65
+ */
66
+ set(from: string | null, to: RedirectHandler | null, options?: RedirectOptions): void;
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 force Whether or not to forcefully set the redirect.
72
+ * @deprecated Please refactor to use an options object as the third parameter.
73
+ */
74
+ set(from: string | null, to: RedirectHandler | null, 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;
@@ -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 {boolean} force Whether or not to forcefully set the redirect.
115
+ * @param {Object} forceOrOptions Additional options for the redirect.
110
116
  */;
111
- _proto.set = function set(from = null, to = null, force = false) {
117
+ _proto.set = function set(from = null, to = null, forceOrOptions = false) {
112
118
  if (!from || !to) {
113
119
  return;
114
120
  }
115
- if (!force && this.redirects.has(from)) {
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.2-beta.1",
3
+ "version": "7.30.2-beta.3",
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.2-beta.1",
20
+ "@shopgate/pwa-benchmark": "7.30.2-beta.3",
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.2-beta.1",
43
+ "@shopgate/pwa-core": "7.30.2-beta.3",
44
44
  "@types/react-portal": "^3.0.9",
45
45
  "lodash": "^4.17.23",
46
46
  "prop-types": "~15.8.1",
@@ -98,24 +98,62 @@ export const getDeviceModel = createSelector(getDeviceInformation, deviceInforma
98
98
  return model;
99
99
  });
100
100
 
101
+ /**
102
+ * Selector to check if the PWA is currently running inside the Android app.
103
+ * Unlike the `getIsAndroid` selector, this selector will return false if the PWA
104
+ * is running in a web browser on an Android device.
105
+ * @param {Object} state The application state.
106
+ * @return {boolean}
107
+ */
108
+ export const getIsAndroidApp = createSelector(getPlatform, platform => {
109
+ if (hasWebBridge()) {
110
+ return false;
111
+ }
112
+ return platform === OS_ANDROID;
113
+ });
114
+
101
115
  /**
102
116
  * Check if the platform is Android.
103
117
  * @param {Object} state The application state.
104
118
  * @return {boolean}
105
119
  */
106
- export const isAndroid = createSelector(getPlatform, platform => {
120
+ export const getIsAndroid = createSelector(getPlatform, platform => {
107
121
  if (hasWebBridge()) {
108
122
  return md.os() === 'AndroidOS';
109
123
  }
110
124
  return platform === OS_ANDROID;
111
125
  });
112
126
 
127
+ /**
128
+ * @deprecated Use `getIsAndroid` instead.
129
+ */
130
+ export const isAndroid = getIsAndroid;
131
+
132
+ /**
133
+ * Selector to check if the PWA is currently running inside the iOS app.
134
+ * Unlike the `getIsIos` selector, this selector will return false if the PWA
135
+ * is running in a web browser on an iOS device.
136
+ * @param {Object} state The application state.
137
+ * @return {boolean}
138
+ */
139
+ export const getIsIosApp = createSelector(getPlatform, platform => {
140
+ if (hasWebBridge()) {
141
+ return false;
142
+ }
143
+ return platform === OS_IOS;
144
+ });
145
+
113
146
  /**
114
147
  * Check if the platform is iOS.
115
148
  * @param {Object} state The application state.
116
149
  * @return {boolean}
117
150
  */
118
- export const isIos = createSelector(getPlatform, platform => platform === OS_IOS);
151
+ export const getIsIos = createSelector(getPlatform, platform => platform === OS_IOS);
152
+
153
+ /**
154
+ * @deprecated Use `getIsIos` instead.
155
+ */
156
+ export const isIos = getIsIos;
119
157
 
120
158
  /**
121
159
  * Checks if the currently stored lib version is one that supports the scanner.
@@ -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
- LoadingProvider.setLoading(pathname);
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
- LoadingProvider.unsetLoading(pathname);
221
+ if (options.showLoading) {
222
+ LoadingProvider.unsetLoading(pathname);
223
+ }
219
224
  if (!redirect) {
220
225
  return;
221
226
  }