Skip to content

Commit 357d638

Browse files
committed
fix: πŸ› support non primitive types
βœ… Closes: #15
1 parent 27e854d commit 357d638

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

β€Žprojects/ngneat/bind-query-params/src/lib/QueryParamDef.ts

+18
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,24 @@ export class QueryParamDef<QueryParams = any> {
2828
return this.config.serializer;
2929
}
3030

31+
serialize(controlValue: any): string | null {
32+
if (this.serializer) {
33+
return this.serializer(controlValue);
34+
}
35+
36+
if (controlValue === null || controlValue === undefined) {
37+
return null;
38+
}
39+
40+
let serializedValue = controlValue?.toString();
41+
42+
if (controlValue.toString() === '[object Object]') {
43+
serializedValue = JSON.stringify(controlValue);
44+
}
45+
46+
return serializedValue;
47+
}
48+
3149
parse(queryParamValue: string) {
3250
if (this.parser) {
3351
return this.parser(queryParamValue);

β€Žprojects/ngneat/bind-query-params/src/lib/utils.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,10 @@ export function get(obj: Record<string, any>, path: string): any {
2929
export function resolveParams(params: ResolveParamsOption | ResolveParamsOption[]) {
3030
const toArray = coerceArray(params);
3131

32-
const result: Record<string, string> = {};
32+
const result: Record<string, string | null> = {};
3333

34-
toArray.forEach(({ def: { queryKey, serializer }, value }) => {
35-
const serializedValue = serializer?.(value) || value?.toString();
36-
result[queryKey] = serializedValue || null;
34+
toArray.forEach(({ def, value }) => {
35+
result[def.queryKey] = def.serialize(value);
3736
});
3837

3938
return result;

0 commit comments

Comments
Β (0)