Skip to content
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

feat(common): allow to serialize plain object #10087

Merged
merged 1 commit into from Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 15 additions & 6 deletions packages/common/serializer/class-serializer.interceptor.ts
@@ -1,3 +1,4 @@
import { ClassSerializerContextOptions } from './class-serializer.interfaces';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { Inject, Injectable, Optional } from '../decorators/core';
Expand Down Expand Up @@ -63,7 +64,7 @@ export class ClassSerializerInterceptor implements NestInterceptor {
*/
serialize(
response: PlainLiteralObject | Array<PlainLiteralObject>,
options: ClassTransformOptions,
options: ClassSerializerContextOptions,
): PlainLiteralObject | Array<PlainLiteralObject> {
if (!isObject(response) || response instanceof StreamableFile) {
return response;
Expand All @@ -76,16 +77,24 @@ export class ClassSerializerInterceptor implements NestInterceptor {

transformToPlain(
plainOrClass: any,
options: ClassTransformOptions,
options: ClassSerializerContextOptions,
): PlainLiteralObject {
return plainOrClass
? classTransformer.classToPlain(plainOrClass, options)
: plainOrClass;
if (!plainOrClass) {
return plainOrClass;
}
if (!options.type) {
return classTransformer.classToPlain(plainOrClass, options);
}
if (plainOrClass instanceof options.type) {
return classTransformer.classToPlain(plainOrClass, options);
}
const instance = classTransformer.plainToClass(options.type, plainOrClass);
return classTransformer.classToPlain(instance, options);
}

protected getContextOptions(
context: ExecutionContext,
): ClassTransformOptions | undefined {
): ClassSerializerContextOptions | undefined {
return this.reflector.getAllAndOverride(CLASS_SERIALIZER_OPTIONS, [
context.getHandler(),
context.getClass(),
Expand Down
6 changes: 6 additions & 0 deletions packages/common/serializer/class-serializer.interfaces.ts
@@ -0,0 +1,6 @@
import { ClassTransformOptions } from '../interfaces/external/class-transform-options.interface';
import { Type } from '../interfaces';

export interface ClassSerializerContextOptions extends ClassTransformOptions {
type?: Type<any>;
}
@@ -1,6 +1,6 @@
import { SetMetadata } from '../../decorators';
import { ClassTransformOptions } from '../../interfaces/external/class-transform-options.interface';
import { ClassSerializerContextOptions } from '../class-serializer.interfaces';
import { CLASS_SERIALIZER_OPTIONS } from '../class-serializer.constants';

export const SerializeOptions = (options: ClassTransformOptions) =>
export const SerializeOptions = (options: ClassSerializerContextOptions) =>
SetMetadata(CLASS_SERIALIZER_OPTIONS, options);
1 change: 1 addition & 0 deletions packages/common/serializer/index.ts
@@ -1,2 +1,3 @@
export * from './class-serializer.interceptor';
export * from './decorators';
export * from './class-serializer.interfaces';