ghostfolio/apps/api/src/interceptors/transform-data-source-in-request.interceptor.ts
Thomas Kaul 155c08d665
Transform data source (#658)
* Transform data source

* Update changelog
2022-02-01 20:35:25 +01:00

46 lines
1.1 KiB
TypeScript

import {
CallHandler,
ExecutionContext,
Injectable,
NestInterceptor
} from '@nestjs/common';
import { Observable } from 'rxjs';
import { ConfigurationService } from '../services/configuration.service';
@Injectable()
export class TransformDataSourceInRequestInterceptor<T>
implements NestInterceptor<T, any>
{
public constructor(
private readonly configurationService: ConfigurationService
) {}
public intercept(
context: ExecutionContext,
next: CallHandler<T>
): Observable<any> {
const http = context.switchToHttp();
const request = http.getRequest();
if (this.configurationService.get('ENABLE_FEATURE_SUBSCRIPTION') === true) {
if (request.body.dataSource) {
request.body.dataSource = this.decodeDataSource(
request.body.dataSource
);
}
if (request.params.dataSource) {
request.params.dataSource = this.decodeDataSource(
request.params.dataSource
);
}
}
return next.handle();
}
private decodeDataSource(encodeDataSource: string) {
return Buffer.from(encodeDataSource, 'hex').toString();
}
}