@faststore/sdk
Advanced tools
Comparing version 3.0.68 to 3.0.84
import { SDKError } from '../utils/error'; | ||
import { isSearchSort, setFacet } from './facets'; | ||
import { initialize } from './useSearchState'; | ||
function getPassThroughSearchParams(params, denyList) { | ||
const passthroughParams = new URLSearchParams(); | ||
const denySet = new Set(denyList); | ||
const entriesArray = Array.from(params.entries()); | ||
for (const [key, value] of entriesArray) { | ||
if (!denySet.has(key)) { | ||
passthroughParams.append(key, value); | ||
} | ||
} | ||
return passthroughParams; | ||
} | ||
export const parse = ({ pathname, searchParams }) => { | ||
@@ -24,4 +35,11 @@ const state = initialize({ | ||
} | ||
state.passThrough = getPassThroughSearchParams(searchParams, [ | ||
'q', | ||
'sort', | ||
'page', | ||
'facets', | ||
...facets, | ||
]); | ||
return state; | ||
}; | ||
//# sourceMappingURL=serializer.js.map |
import type { State } from '../types'; | ||
export declare const initialize: ({ sort, selectedFacets, term, base, page, }?: Partial<State> | undefined) => { | ||
export declare const initialize: ({ sort, selectedFacets, term, base, page, passThrough }?: Partial<State> | undefined) => { | ||
sort: import("../types").SearchSort; | ||
@@ -8,2 +8,3 @@ selectedFacets: import("../types").Facet[]; | ||
page: number; | ||
passThrough: URLSearchParams; | ||
}; | ||
@@ -17,2 +18,3 @@ export declare const useSearchState: (initialState: Partial<State>, onChange: (url: URL) => void) => { | ||
page: number; | ||
passThrough: URLSearchParams; | ||
}; | ||
@@ -19,0 +21,0 @@ setState: (newState: State) => false | void; |
import { useMemo } from 'react'; | ||
import format from '../utils/format'; | ||
export const initialize = ({ sort = 'score_desc', selectedFacets = [], term = null, base = '/', page = 0, } = {}) => ({ | ||
export const initialize = ({ sort = 'score_desc', selectedFacets = [], term = null, base = '/', page = 0, passThrough = new URLSearchParams() } = {}) => ({ | ||
sort, | ||
@@ -9,2 +9,3 @@ selectedFacets, | ||
page, | ||
passThrough, | ||
}); | ||
@@ -11,0 +12,0 @@ const equals = (s1, s2) => format(s1).href === format(s2).href; |
@@ -23,2 +23,6 @@ export type SearchSort = 'price_desc' | 'price_asc' | 'orders_desc' | 'name_desc' | 'name_asc' | 'release_desc' | 'discount_desc' | 'score_desc'; | ||
page: number; | ||
/** | ||
* @description params from other sources to preserve when building URLs | ||
*/ | ||
passThrough: URLSearchParams; | ||
} |
const format = (params) => { | ||
const url = new URL(params.base, 'http://localhost'); | ||
const { page, selectedFacets, sort, term } = params; | ||
const { page, selectedFacets, sort, term, passThrough } = params; | ||
if (term !== null) { | ||
@@ -17,2 +17,7 @@ url.searchParams.set('q', term); | ||
url.searchParams.set('page', page.toString()); | ||
if (passThrough) { | ||
for (const [key, value] of passThrough.entries()) { | ||
url.searchParams.append(key, value); | ||
} | ||
} | ||
return url; | ||
@@ -19,0 +24,0 @@ }; |
{ | ||
"name": "@faststore/sdk", | ||
"version": "3.0.68", | ||
"version": "3.0.84", | ||
"description": "Hooks for creating your next component library", | ||
@@ -55,3 +55,3 @@ "license": "MIT", | ||
}, | ||
"gitHead": "d12b653cdc379d4449666b2a405922c1b27c372d" | ||
"gitHead": "15b588a359a81a9b6f5d83d1738a57e12bf764f2" | ||
} |
@@ -6,2 +6,20 @@ import { SDKError } from '../utils/error' | ||
function getPassThroughSearchParams( | ||
params: URLSearchParams, | ||
denyList: string[] | ||
) { | ||
const passthroughParams = new URLSearchParams() | ||
const denySet = new Set(denyList) | ||
const entriesArray = Array.from(params.entries()) | ||
for (const [key, value] of entriesArray) { | ||
if (!denySet.has(key)) { | ||
passthroughParams.append(key, value) | ||
} | ||
} | ||
return passthroughParams | ||
} | ||
export const parse = ({ pathname, searchParams }: URL): State => { | ||
@@ -32,3 +50,11 @@ const state = initialize({ | ||
state.passThrough = getPassThroughSearchParams(searchParams, [ | ||
'q', | ||
'sort', | ||
'page', | ||
'facets', | ||
...facets, | ||
]) | ||
return state | ||
} |
@@ -12,2 +12,3 @@ import { useMemo } from 'react' | ||
page = 0, | ||
passThrough = new URLSearchParams() | ||
}: Partial<State> | undefined = {}) => ({ | ||
@@ -19,2 +20,3 @@ sort, | ||
page, | ||
passThrough, | ||
}) | ||
@@ -21,0 +23,0 @@ |
@@ -33,2 +33,6 @@ export type SearchSort = | ||
page: number | ||
/** | ||
* @description params from other sources to preserve when building URLs | ||
*/ | ||
passThrough: URLSearchParams | ||
} |
@@ -5,3 +5,3 @@ import type { State } from '../types' | ||
const url = new URL(params.base, 'http://localhost') | ||
const { page, selectedFacets, sort, term } = params | ||
const { page, selectedFacets, sort, term, passThrough } = params | ||
@@ -26,2 +26,8 @@ if (term !== null) { | ||
if (passThrough) { | ||
for (const [key, value] of passThrough.entries()) { | ||
url.searchParams.append(key, value) | ||
} | ||
} | ||
return url | ||
@@ -28,0 +34,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
118618
2887