converter-z
v0.2.2
Published
Convert data values by data type.
Downloads
3
Maintainers
Readme
converter
Javascript conmunicates to java with js-to-java need special schemas, we will preprocess data (if you need a number but get a string parameter) then we can use js-to-java
to wrap js object to java object.
Install
$ npm install converter-z
Usage
Convert primitives data type, like number, string, boolean, and more.
@converter()
method(
@convert({ type: 'number' }) id: number, // recommend
@convert({ id: { type: 'number' } }) id: number, // deprecated
) {
...
}
Convert primtives data type in array
@converter()
method(
@convert({ type: 'number' }) ids: number[], // recommend
@convert({ ids: { type: 'number' } }) ids: number[], // deprecated
) {
...
}
Convert shallow object
@converter()
method(
@convert({ id: { type: 'number' } }) param: { id: number },
) {
...
}
Convert object in array
@converter()
method(
@convert({ id: { type: 'number' } }) param: Array<{ id: number }>,
) {
...
}
Add a new convert type or rewrite convert type
converter.config({
type: 'increase',
adaptor: (value, required?: boolean, message?:string) => {
let nextValue = value
if (value === '' || value === null || value === undefined) {
if (required) throw new Error(message)
nextValue = null
} else {
nextValue = +value + 1
}
return nextValue
},
})
Convert deep object
@converter()
method(
@convert({
ids: {
id: { type: 'number' },
},
}) param: { ids: { id: number } },
) {
...
}
Project progress
- [ ] Converter develop
- [x] Convert primitives data type, like number, string, boolean, and more
- [x] Convert primtives data type in array
- [x] Convert shallow object
- [x] Convert object in array
- [x] Add a new convert type or rewrite convert type
- [ ] Convert deep object
- [ ] More
License
MIT