Skip to content

Commit

Permalink
Restructure AjaxPostBodyParamsTransformPipe
Browse files Browse the repository at this point in the history
  • Loading branch information
casparneumann-cap committed Nov 3, 2023
1 parent 0c992fb commit ab7b5d4
Showing 1 changed file with 16 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,24 @@ import {
@Injectable()
export class AjaxPostBodyParamsTransformPipe implements PipeTransform {
async transform(value: AjaxPostBodyParams): Promise<unknown> {
if (value) {
let transformed: Exclude<AjaxPostBodyParams, undefined>;

if ('libraries' in value) {
transformed = plainToClass(LibrariesBodyParams, value);
} else if ('contentId' in value) {
transformed = plainToClass(ContentBodyParams, value);
} else if ('libraryParameters' in value) {
transformed = plainToClass(LibraryParametersBodyParams, value);
} else {
return undefined;
}

const validationResult = await validate(transformed);
if (validationResult.length > 0) {
const validationPipe = new ValidationPipe();
const exceptionFactory = validationPipe.createExceptionFactory();
throw exceptionFactory(validationResult);
}
if (value === undefined) {
return undefined;
}
if ('libraries' in value) {
value = plainToClass(LibrariesBodyParams, value);
} else if ('contentId' in value) {
value = plainToClass(ContentBodyParams, value);
} else if ('libraryParameters' in value) {
value = plainToClass(LibraryParametersBodyParams, value);
}

return transformed;
const validationResult = await validate(value);
if (validationResult.length > 0) {
const validationPipe = new ValidationPipe();
const exceptionFactory = validationPipe.createExceptionFactory();
throw exceptionFactory(validationResult);
}

return undefined;
return value;
}
}

0 comments on commit ab7b5d4

Please sign in to comment.