import {
  CallHandler,
  ExecutionContext,
  Injectable,
  NestInterceptor,
} from "@nestjs/common";
import { map, Observable } from "rxjs";
import * as _ from "lodash";
const pjson = require('../../package.json');

export interface Response<T> {
  data: T;
}

@Injectable()
export class TransformationInterceptor<T>
  implements NestInterceptor<T, Response<T>>
{
  intercept(
    context: ExecutionContext,
    next: CallHandler
  ): Observable<Response<T>> {
    const methodName = _.lowerCase(context.getHandler().name);
    const className = _.lowerCase(
      context.getClass().name.replace("Controller", "")
    );
    return next.handle().pipe(
      map((data) => {
        return {
          data,
          meta: {
            version: pjson['version']
          }
        };
      })
    );
  }
}
