class-transformer#instanceToPlain TypeScript Examples
The following examples show how to use
class-transformer#instanceToPlain.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: user.controller.ts From nestjs-api-example with MIT License | 9 votes |
@Get(':id')
@ApiOperation({ summary: '유저 정보 조회 API' })
@ApiOkResponse({
description: 'Id가 일치하는 유저 정보를 조회한다.',
type: UserResponseDto,
})
async findOne(
@Param('id', new ParseIntPipe()) id: number,
@Res() res: Response,
) {
const responseDto = await this.userService.findById(id);
return res.status(HttpStatus.OK).json(instanceToPlain(responseDto));
}
Example #2
Source File: EntityDataService.ts From viewer-components-react with MIT License | 7 votes |
// Convenience method for retrieving selected observations queries (metric, unit, params) for a given sensor
private getObservationQueriesForSensor$(id: string, returnFirstQueryOnly = false): Observable<ObservationQuery[]> {
return IModelSettingsService.iModelSettings$()
.pipe(
map((iModelSettings: IModelSettings) => {
return iModelSettings.getAssociation(id)?.getObservationQueries() || [];
}),
distinctUntilChanged((p: ObservationQuery[], c: ObservationQuery[]) => {
return _isEqual(
instanceToPlain(returnFirstQueryOnly ? p[0] : p),
instanceToPlain(returnFirstQueryOnly ? c[0] : c)
);
})
);
}
Example #3
Source File: IModelSettingsService.ts From viewer-components-react with MIT License | 6 votes |
private saveSettings(): void {
// Get current auth state because we need the project id
AuthService.authState$()
.pipe(
filter((authState: AuthState | null) => !!authState),
first()
)
.subscribe((authState: AuthState | null) => {
if (authState) {
const newSettings = instanceToPlain(this.iModelSettings.getValue());
ConfigSettingsService.saveConfigSettings$(authState.getProjectId(), newSettings)
.subscribe({
next: (() => {
LoggerService.log("iModel settings saved:", this.iModelSettings.getValue());
}),
});
} else {
LoggerService.warn("Error saving iModel settings: Auth State not available");
}
});
}
Example #4
Source File: pipe-transtorm.ts From malagu with MIT License | 5 votes |
public async transform(value: any, metadata: ArgumentMetadata): Promise<any> {
const opts = this.options || {};
const { argType } = metadata;
if (!argType || !this.toValidate(metadata)) {
return value;
}
const originalValue = value;
value = this.toEmptyIfNil(value);
const isNil = value !== originalValue;
const isPrimitive = this.isPrimitive(value);
this.stripProtoKeys(value);
let entity = plainToInstance(
argType,
value,
opts.transformOptions,
);
const originalEntity = entity;
const isCtorNotEqual = entity.constructor !== argType;
if (isCtorNotEqual && !isPrimitive) {
entity.constructor = argType;
} else if (isCtorNotEqual) {
// when "entity" is a primitive value, we have to temporarily
// replace the entity to perform the validation against the original
// metatype defined inside the handler
entity = { constructor: argType } as any;
}
const errors = await validate(entity, opts.validatorOptions);
if (errors.length > 0) {
throw new ValidationErrors(opts.detailedOutputDisabled ? undefined : errors);
}
if (isPrimitive) {
// if the value is a primitive value and the validation process has been successfully completed
// we have to revert the original value passed through the pipe
entity = originalEntity;
}
if (opts.transformEnabled) {
return entity;
}
if (isNil) {
// if the value was originally undefined or null, revert it back
return originalValue;
}
return Object.keys(opts.validatorOptions).length > 0
? instanceToPlain(entity, opts.transformOptions)
: value;
}
Example #5
Source File: setup.ts From epicgames-freegames-node with MIT License | 5 votes |
L.debug({ config: instanceToPlain(config) });