All files / src/decorators user-transform.decorator.ts

100% Statements 11/11
100% Branches 1/1
100% Functions 4/4
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 241x 1x 1x             1x 47x   47x           47x 47x 47x      
import {property} from '@loopback/repository';
import {Type, Expose, Transform} from 'class-transformer';
import _ from 'lodash';
 
/**
 * Custom decorator to handle User transformation
 * @param exposeName - The name to expose in the response
 * @param groups - Optional class-transformer groups
 */
export function UserTransform(exposeName: string, groups: string[] = []) {
  return function (target: any, key: string) {
    // Apply LoopBack's @property decorator
    property({
      type: 'object',
      jsonSchema: {nullable: true},
    })(target, key);
 
    // Apply class-transformer's Type, Transform, and Expose decorators
    Type(() => Object)(target, key);
    Transform(({value}) => _.pick(value, ['name', 'id']))(target, key);
    Expose({name: exposeName, groups})(target, key);
  };
}