-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7bdb6ec
commit 616f236
Showing
4 changed files
with
61 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,16 @@ | ||
import { Transform, TransformationType } from 'class-transformer'; | ||
import { merge } from 'lodash'; | ||
|
||
interface ExposeObjectIdOptions { | ||
toClass?: boolean; | ||
toPlain?: boolean; | ||
} | ||
export const ExposeObjectId = () => (target: any, propertyKey: string) => { | ||
Transform(({ type, obj }) => { | ||
switch (type) { | ||
case TransformationType.PLAIN_TO_CLASS: | ||
return obj[propertyKey]; | ||
|
||
export const ExposeObjectId = | ||
(options?: ExposeObjectIdOptions) => (target: any, propertyKey: string) => { | ||
const mergedOptions = merge({ toClass: true, toPlain: true }, options); | ||
Transform(({ type, obj }) => { | ||
switch (type) { | ||
case TransformationType.PLAIN_TO_CLASS: | ||
if (mergedOptions.toClass) { | ||
return obj[propertyKey]; | ||
} else { | ||
return undefined; | ||
} | ||
case TransformationType.CLASS_TO_PLAIN: | ||
return obj[propertyKey].toString(); | ||
|
||
case TransformationType.CLASS_TO_PLAIN: | ||
if (mergedOptions.toPlain) { | ||
return obj[propertyKey].toString(); | ||
} else { | ||
return undefined; | ||
} | ||
|
||
case TransformationType.CLASS_TO_CLASS: | ||
return obj[propertyKey]; | ||
} | ||
})(target, propertyKey); | ||
}; | ||
case TransformationType.CLASS_TO_CLASS: | ||
return obj[propertyKey]; | ||
} | ||
})(target, propertyKey); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters