zustand-querystring 0.0.7 → 0.0.9
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/lib/middleware.d.ts +0 -1
- package/lib/middleware.js +23 -43
- package/package.json +1 -1
package/lib/middleware.d.ts
CHANGED
|
@@ -5,7 +5,6 @@ type DeepSelect<T> = T extends object ? {
|
|
|
5
5
|
export interface QueryStringOptions<T> {
|
|
6
6
|
url?: string;
|
|
7
7
|
select?: (pathname: string) => DeepSelect<T>;
|
|
8
|
-
key?: string;
|
|
9
8
|
}
|
|
10
9
|
type QueryString = <T, Mps extends [StoreMutatorIdentifier, unknown][] = [], Mcs extends [StoreMutatorIdentifier, unknown][] = []>(initializer: StateCreator<T, Mps, Mcs>, options?: QueryStringOptions<T>) => StateCreator<T, Mps, Mcs>;
|
|
11
10
|
export declare const querystring: QueryString;
|
package/lib/middleware.js
CHANGED
|
@@ -18,9 +18,6 @@ const compact = (newState, initialState) => {
|
|
|
18
18
|
}
|
|
19
19
|
}
|
|
20
20
|
});
|
|
21
|
-
if (Object.keys(output).length === 0) {
|
|
22
|
-
return null;
|
|
23
|
-
}
|
|
24
21
|
return output;
|
|
25
22
|
};
|
|
26
23
|
const translateSelectionToState = (selection, state) => Object.keys(selection).reduce((acc, key) => {
|
|
@@ -41,8 +38,6 @@ const translateSelectionToState = (selection, state) => Object.keys(selection).r
|
|
|
41
38
|
}, {});
|
|
42
39
|
const queryStringImpl = (fn, options) => (set, get, api) => {
|
|
43
40
|
const defaultedOptions = {
|
|
44
|
-
partialize: state => state,
|
|
45
|
-
key: '$',
|
|
46
41
|
...options,
|
|
47
42
|
};
|
|
48
43
|
const url = defaultedOptions.url;
|
|
@@ -54,27 +49,24 @@ const queryStringImpl = (fn, options) => (set, get, api) => {
|
|
|
54
49
|
const selectedState = translateSelectionToState(selection, state);
|
|
55
50
|
return selectedState;
|
|
56
51
|
}
|
|
57
|
-
return state;
|
|
52
|
+
return state ?? {};
|
|
58
53
|
};
|
|
59
54
|
const initialize = (url, _set = set) => {
|
|
60
55
|
const fallback = () => fn(_set, get, api);
|
|
61
56
|
try {
|
|
62
57
|
const splitUrl = url.split('?');
|
|
63
|
-
|
|
58
|
+
let queryString = splitUrl[1];
|
|
64
59
|
const pathname = splitUrl[0];
|
|
65
60
|
if (!queryString) {
|
|
66
61
|
return fallback();
|
|
67
62
|
}
|
|
68
|
-
|
|
69
|
-
if (idx === -1) {
|
|
63
|
+
if (!queryString) {
|
|
70
64
|
return fallback();
|
|
71
65
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
return fallback();
|
|
66
|
+
if (!queryString.startsWith('$')) {
|
|
67
|
+
queryString = '$' + queryString;
|
|
75
68
|
}
|
|
76
|
-
|
|
77
|
-
const parsed = parse(toParse);
|
|
69
|
+
const parsed = parse(queryString);
|
|
78
70
|
const currentValue = get() ?? fn(_set, get, api);
|
|
79
71
|
const merged = mergeWith(currentValue, getSelectedState(parsed, pathname));
|
|
80
72
|
set(merged, true);
|
|
@@ -82,44 +74,32 @@ const queryStringImpl = (fn, options) => (set, get, api) => {
|
|
|
82
74
|
}
|
|
83
75
|
catch (error) {
|
|
84
76
|
console.error(error);
|
|
85
|
-
if (typeof window !== 'undefined') {
|
|
86
|
-
window.history.replaceState(null, '', window.location.pathname);
|
|
87
|
-
}
|
|
88
77
|
return fn(_set, get, api);
|
|
89
78
|
}
|
|
90
79
|
};
|
|
91
80
|
if (typeof window !== 'undefined') {
|
|
92
81
|
const setQuery = () => {
|
|
93
82
|
const selectedState = getSelectedState(get(), window.location.pathname);
|
|
94
|
-
if (!selectedState) {
|
|
83
|
+
if (!selectedState || Object.keys(selectedState).length === 0) {
|
|
95
84
|
return;
|
|
96
85
|
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
const variables = {};
|
|
106
|
-
const currentQuery = currentQueryString.split('&');
|
|
107
|
-
for (const variable of currentQuery) {
|
|
108
|
-
if (stringified?.includes(variable)) {
|
|
109
|
-
continue;
|
|
110
|
-
}
|
|
111
|
-
const [key, value] = variable.split('=');
|
|
112
|
-
variables[key] = value;
|
|
86
|
+
let currentQueryString = window.location.search.slice(1);
|
|
87
|
+
if (!currentQueryString.startsWith('$')) {
|
|
88
|
+
currentQueryString = '$' + currentQueryString;
|
|
89
|
+
}
|
|
90
|
+
const currentParsed = parse(currentQueryString);
|
|
91
|
+
const newMerged = mergeWith(currentParsed, selectedState, (objValue, srcValue) => {
|
|
92
|
+
if (Array.isArray(objValue)) {
|
|
93
|
+
return srcValue;
|
|
113
94
|
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
window.history.replaceState(null, '', newQueryString ? `?${newQueryString}` : window.location.pathname);
|
|
95
|
+
});
|
|
96
|
+
const newCompacted = compact(newMerged, initialState);
|
|
97
|
+
if (Object.keys(newCompacted).length) {
|
|
98
|
+
const stringified = stringify(newCompacted).substring(1);
|
|
99
|
+
window.history.replaceState(null, '', `?${stringified}`);
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
window.history.replaceState(null, '', window.location.pathname);
|
|
123
103
|
}
|
|
124
104
|
};
|
|
125
105
|
//TODO: find a better way to do this
|