VonaJS 提供了序列化能力,可以对 API 的响应数据进行转换,比如:排除密码字段,对 Email 和 Mobile 进行脱敏处理,等等
前文介绍了序列化的一般用法。这里再介绍一组工具函数。通过工具函数可以更加便利的使用序列化能力
工具清单
工具: @Serializer工具: v说明@Serializer.transformv.serializerTransform使用Serializer Transform,参见: 序列化@Serializer.excludev.serializerExclude排除字段@Serializer.replacev.serializerReplace对字段值进行脱敏处理@Serializer.getterv.serializerGetter采用getter机制生成新的字段值@Serializer.customv.serializerCustom使用自定义函数对字段值进行处理- 既然`@Serializer`工具非常简洁,直观,为什么还要提供`v`工具?
- 1. `v`工具可以实现通过 App Config 修改配置
- 2. `v`工具和`@Serializer`工具底层逻辑是一致的
复制代码 @Serializer.transform/v.serializerTransform
比如,将EntityStudent中的name字段值转化为大写
我们直接使用序列化中创建的 Serializer Transform upper
1. @Serializer.transform
- class EntityStudent {
- + @Serializer.transform('demo-student:upper')
- @Api.field(v.title($locale('Name')))
- name: string;
- }
复制代码 2. v.serializerTransform
- class EntityStudent {
- + @Api.field(v.serializerTransform('demo-student:upper'), v.title($locale('Name')))
- name: string;
- }
复制代码 3. App Config
可以在 App Config 中修改配置
src/backend/config/config/config.ts
- // onions
- config.onions = {
- entity: {
- 'demo-student:student': {
- fields: {
- name: {
- serializerTransforms: {
- 'demo-student:upper': {},
- },
- },
- },
- },
- },
- };
复制代码- import { $makeSchema, v } from 'vona-module-a-openapi';
- // onions
- config.onions = {
- entity: {
- 'demo-student:student': {
- fields: {
- name: $makeSchema(v.serializerTransform('demo-student:upper'), z.string()),
- },
- },
- },
- };
复制代码 @Serializer.exclude/v.serializerExclude
比如,排除EntityStudent中的name字段
1. @Serializer.exclude
- class EntityStudent {
- + @Serializer.exclude()
- @Api.field(v.title($locale('Name')))
- name: string;
- }
复制代码 2. v.serializerExclude
- class EntityStudent {
- + @Api.field(v.serializerExclude(), v.title($locale('Name')))
- name: string;
- }
复制代码 3. App Config
可以在 App Config 中修改配置
src/backend/config/config/config.ts
- // onions
- config.onions = {
- entity: {
- 'demo-student:student': {
- fields: {
- name: { exclude: false },
- },
- },
- },
- };
复制代码- import { $makeSchema, v } from 'vona-module-a-openapi';
- // onions
- config.onions = {
- entity: {
- 'demo-student:student': {
- fields: {
- name: $makeSchema(v.serializerExclude(), z.string()),
- },
- },
- },
- };
复制代码 @Serializer.replace/v.serializerReplace
比如,将EntityStudent中的name字段值进行脱敏处理
比如,name 原始值为tom,脱敏之后为t***m
1. @Serializer.replace
- class EntityStudent {
- + @Serializer.replace({ patternFrom: /(\w)(\w+)(\w)/, patternTo: '$1***$3' })
- @Api.field(v.title($locale('Name')))
- name: string;
- }
复制代码 2. v.serializerReplace
- class EntityStudent {
- @Api.field(
- + v.serializerReplace({ patternFrom: /(\w)(\w+)(\w)/, patternTo: '$1***$3' }),
- v.title($locale('Name')),
- )
- name: string;
- }
复制代码 3. App Config
可以在 App Config 中修改配置
src/backend/config/config/config.ts
- // onions
- config.onions = {
- entity: {
- 'demo-student:student': {
- fields: {
- name: {
- serializerTransforms: {
- 'a-serialization:replace': {
- patternFrom: /(\w)(\w+)(\w)/,
- patternTo: '$1***$3',
- },
- },
- },
- },
- },
- },
- };
复制代码
- a-serialization:replace: a-serialization模块提供的 Serializer Transform
- import { $makeSchema, v } from 'vona-module-a-openapi';
- // onions
- config.onions = {
- entity: {
- 'demo-student:student': {
- fields: {
- name: $makeSchema(
- v.serializerReplace({ patternFrom: /(\w)(\w+)(\w)/, patternTo: '$1***$3' }),
- z.string(),
- ),
- },
- },
- },
- };
复制代码 @Serializer.getter/v.serializerGetter
比如,EntityStudent中的fullName字段由firstName和lastName字段组合而成
1. getter
- class EntityStudent {
- @Api.field()
- firstName: string;
- @Api.field()
- lastName: string;
- @Api.field()
- + get fullName(): string | undefined {
- + return `${this.firstName} ${this.lastName}`;
- + }
- }
复制代码 2. @Serializer.getter
- class EntityStudent {
- + @Serializer.getter((data: EntityStudent) => {
- + return `${data.firstName} ${data.lastName}`;
- + })
- @Api.field()
- fullName: string;
- }
复制代码 3. v.serializerGetter
- class EntityStudent {
- + @Api.field(v.serializerGetter((data: EntityStudent) => {
- + return `${data.firstName} ${data.lastName}`;
- + }))
- fullName: string;
- }
复制代码 4. App Config
可以在 App Config 中修改配置
src/backend/config/config/config.ts
- // onions
- config.onions = {
- entity: {
- 'demo-student:student': {
- fields: {
- fullName: {
- serializerTransforms: {
- 'a-serialization:getter': {
- getter: (data: EntityStudent) => {
- return `${data.firstName} ${data.lastName}`;
- },
- },
- },
- },
- },
- },
- },
- };
复制代码
- a-serialization:getter: a-serialization模块提供的 Serializer Transform
- import { $makeSchema, v } from 'vona-module-a-openapi';
- // onions
- config.onions = {
- entity: {
- 'demo-student:student': {
- fields: {
- fullName: $makeSchema(
- v.serializerGetter((data: EntityStudent) => {
- return `${data.firstName} ${data.lastName}`;
- }),
- z.string(),
- ),
- },
- },
- },
- };
复制代码 @Serializer.custom/v.serializerCustom
比如,将EntityStudent中的name字段值转换为大写
1. @Serializer.custom
- class EntityStudent {
- + @Serializer.custom((value: string) => {
- + return value.toUpperCase();
- + })
- @Api.field(v.title($locale('Name')))
- name: string;
- }
复制代码 2. v.serializerCustom
- class EntityStudent {
- @Api.field(
- + v.serializerCustom((value: string) => {
- + return value.toUpperCase();
- + }),
- v.title($locale('Name')),
- )
- name: string;
- }
复制代码 3. App Config
可以在 App Config 中修改配置
src/backend/config/config/config.ts
- // onions
- config.onions = {
- entity: {
- 'demo-student:student': {
- fields: {
- name: {
- serializerTransforms: {
- 'a-serialization:custom': {
- custom: (value: string) => {
- return value.toUpperCase();
- },
- },
- },
- },
- },
- },
- },
- };
复制代码
- a-serialization:custom: a-serialization模块提供的 Serializer Transform
- import { $makeSchema, v } from 'vona-module-a-openapi';
- // onions
- config.onions = {
- entity: {
- 'demo-student:student': {
- fields: {
- name: $makeSchema(
- v.serializerCustom((value: string) => {
- return value.toUpperCase();
- }),
- z.string(),
- ),
- },
- },
- },
- };
复制代码 资源
- Github:https://github.com/vonajs/vona
- 文档:https://vona.js.org
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |