-
-
Notifications
You must be signed in to change notification settings - Fork 730
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Serialization for Date
is not working when using filter
option.
#467
Comments
Yes, that's true - using the One possibility would be to pass a third argument to |
Somewhat related to this. Is there a way I can pass a custom prefix? For example I have an object: And want to serialize it so that it turns into: The current filter only lets me return the value but not a prefix. Am i missing something here? |
@dbnar2 I think there's few options that you can choose. The point is that you should translate the array into an object, and you can get the querystring with key inside of brakets qs.stringify({ foo: [1, 2] }, { encodeValuesOnly: true });
// foo[0]=1&foo[1]=2
qs.stringify({ foo: { bar: 1, baz: 2 } }, { encodeValuesOnly: true });
// foo[bar]=1&foo[baz]=2
const data = {
myRange: [10, 20],
}
qs.stringify(data, {
encodeValuesOnly: true,
filter(key, value) {
if (key === 'myRange') {
const [gte, lte] = value
return { gte, lte }
}
// You have to serialize `Date` by yourself
if (value instanceof Date) {
return value.toISOString()
}
return value;
}
});
// myRange[gte]=10&myRange[lte]=20
/**
* returns true only if value is an array with 2 number elements.
*/
const isRange = (value: any): value is [number, number] => {
if (!Array.isArray(value)) return false;
if (value.length !== 2) return false;
if (value.some(el => typeof el !== 'number')) return false;
return true;
}
const data = {
myRange: [10, 20],
}
qs.stringify(data, {
encodeValuesOnly: true,
filter(key, value) {
if (isRange(value)) {
const [gte, lte] = value
return { gte, lte }
}
// You have to serialize `Date` by yourself
if (value instanceof Date) {
return value.toISOString()
}
return value;
}
});
// myRange[gte]=10&myRange[lte]=20
const toNumberRange = ([gte, lte]: [number, number]) => {
return { gte, lte }
}
const data = {
myRange: toNumberRange([10, 20]),
}
qs.stringify(data, {
encodeValuesOnly: true,
});
// myRange[gte]=10&myRange[lte]=20 |
Are there any plans for this to be fixed soon? I am willing to contribute. |
@carpics if there's a way to provide this capability without a breaking change, i'd love to hear about it. |
serialization for
Date
type values are skipped when I'm usinngfilter
option for custom serialization.code to reproduce
what I expected
what actually worked
so I have to call
qs.stringify
like this:The text was updated successfully, but these errors were encountered: