Comments (2)
create a simpler Param builder
export const getSchemaParam = (objParam: QueryParamConfig<any>): QueryParamConfig<any> => {
return {
encode(input: object) {
const objEncoded = Object.keys(input).reduce((acc, key) => {
const value = input[key];
const param = objParam[key];
// is it a QueryParam?
if (param && param.encode && param.decode) {
return {
...acc,
[key]: param.encode(value),
};
}
return {
...acc,
[key]: value,
};
}, {});
return encodeObject(objEncoded);
},
decode(input: string | string[] | undefined): object | undefined {
if (input == null || !input.length) {
return undefined;
}
const obj = decodeObject(input);
return Object.keys(obj).reduce((acc, key) => {
const value = obj[key];
const param = objParam[key];
// is it a QueryParam?
if (param && param.encode && param.decode) {
return {
...acc,
[key]: param.decode(value),
};
}
return {
...acc,
[key]: value,
};
}, {});
},
};
};
const filterDefinition = {
begin: MomentParam,
end: MomentParam,
};
const FilterParam = getSchemaParam(filterDefinition);
FilterParam.encode
FilterParam.decode
it also needs to be recursive
from serialize-query-params.
nesting solution
import { QueryParamConfig, encodeObject, decodeObject } from 'use-query-params';
const encodeObj = (objParam: QueryParamConfig<any>) => (input: object) => {
if (objParam && objParam.encode && objParam.decode) {
return objParam.encode(input);
}
const objEncoded = Object.keys(input).reduce((acc, key) => {
const value = input[key];
const param = objParam[key];
// is it a QueryParam?
if (param && param.encode && param.decode) {
return {
...acc,
[key]: param.encode(value),
};
}
// nested object param
if (typeof value === 'object') {
const encoded = encodeObj({
[key]: param,
})(value);
return {
...acc,
[key]: encoded,
};
}
return {
...acc,
[key]: value,
};
}, {});
return encodeObject(objEncoded);
};
const decodeObj = (objParam: QueryParamConfig<any>) => (input: string | string[] | undefined): object | undefined => {
if (input == null || !input.length) {
return undefined;
}
if (objParam && objParam.encode && objParam.decode) {
return objParam.decode(input);
}
const obj = decodeObject(input);
return Object.keys(obj).reduce((acc, key) => {
const value = obj[key];
const param = objParam[key];
// is it a QueryParam?
if (param && param.encode && param.decode) {
return {
...acc,
[key]: param.decode(value),
};
}
// nested object param
if (typeof param === 'object') {
const decoded = decodeObj(param)(value);
return {
...acc,
[key]: decoded,
};
}
return {
...acc,
[key]: value,
};
}, {});
};
// TODO - improve type
export const getSchemaParam = (objParam: QueryParamConfig<any>): QueryParamConfig<any> => {
const encode = encodeObj(objParam);
const decode = decodeObj(objParam);
return {
encode,
decode,
};
};
tests
import { StringParam, BooleanParam } from 'use-query-params';
import { getSchemaParam } from '../getSchemaParam';
const result = 'quadrant-atende_user-manager-managerId_oneOnOne-hasMadeOne-true';
// TODO - fix nesting
it.skip('should encode complex filter properly using schemaParam', () => {
const userFilterDefinition = {
quadrant: StringParam,
user: {
manager: StringParam,
oneOnOne: {
hasMadeOne: BooleanParam,
},
},
};
const UserFilterParam = getSchemaParam(userFilterDefinition);
const filters = {
quadrant: 'atende',
user: {
manager: 'managerId',
oneOnOne: {
hasMadeOne: true,
},
},
};
const encoded = UserFilterParam.encode(filters);
expect(encoded).toEqual(result);
});
// TODO - fix nesting
it.skip('should decode complex filter properly using schemaParam', () => {
const userFilterDefinition = {
quadrant: StringParam,
user: {
manager: StringParam,
oneOnOne: {
hasMadeOne: BooleanParam,
},
},
};
const UserFilterParam = getSchemaParam(userFilterDefinition);
const filters = {
quadrant: 'atende',
user: {
manager: 'managerId',
oneOnOne: {
hasMadeOne: true,
},
},
};
const encoded = UserFilterParam.decode(result);
expect(encoded).toEqual(filters);
});
tests are still failing
from serialize-query-params.
Related Issues (18)
- Add in date time param HOT 1
- updateLocation does not update href property HOT 3
- Updated locations have methods that don't work
- Allow null and empty string values
- UpdateInLocation does not remove Nully parameters HOT 1
- withDefault excludes equals property
- Example shows import with stringify yet no exported member exists
- withDefaults does not work if the property does not exist in the encodedQuery HOT 2
- query-string dependency should not be a peerDependency HOT 3
- ESM build breaks with snowpack since query-string version check uses require which is undefined
- React router uses stale query params due to `query` in location
- createEnumParam with withDefault gives null in types HOT 3
- Update query-string dependency to v 6.8.3 HOT 1
- Question: Why do empty array is returned empty string in encodeDelimitedArray? HOT 1
- Update query-string HOT 1
- Skipped query-string version in check function in "Development" mode HOT 1
- Delimited array params break when storing strings with '_' characters HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from serialize-query-params.