mirror of https://github.com/ghostfolio/ghostfolio
5 changed files with 138 additions and 49 deletions
@ -0,0 +1,37 @@ |
|||
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) { |
|||
// Decode data source
|
|||
if (request.params.dataSource) { |
|||
request.params.dataSource = Buffer.from( |
|||
request.params.dataSource, |
|||
'hex' |
|||
).toString(); |
|||
} |
|||
} |
|||
|
|||
return next.handle(); |
|||
} |
|||
} |
@ -0,0 +1,52 @@ |
|||
import { |
|||
CallHandler, |
|||
ExecutionContext, |
|||
Injectable, |
|||
NestInterceptor |
|||
} from '@nestjs/common'; |
|||
import { DataSource } from '@prisma/client'; |
|||
import { Observable } from 'rxjs'; |
|||
import { map } from 'rxjs/operators'; |
|||
import { ConfigurationService } from '../services/configuration.service'; |
|||
|
|||
@Injectable() |
|||
export class TransformDataSourceInResponseInterceptor<T> |
|||
implements NestInterceptor<T, any> |
|||
{ |
|||
public constructor( |
|||
private readonly configurationService: ConfigurationService |
|||
) {} |
|||
|
|||
public intercept( |
|||
context: ExecutionContext, |
|||
next: CallHandler<T> |
|||
): Observable<any> { |
|||
return next.handle().pipe( |
|||
map((data: any) => { |
|||
if ( |
|||
this.configurationService.get('ENABLE_FEATURE_SUBSCRIPTION') === true |
|||
) { |
|||
if (data.activities) { |
|||
data.activities.map((activity) => { |
|||
activity.dataSource = this.encodeDataSource(activity.dataSource); |
|||
return activity; |
|||
}); |
|||
} |
|||
|
|||
if (data.positions) { |
|||
data.positions.map((position) => { |
|||
position.dataSource = this.encodeDataSource(position.dataSource); |
|||
return position; |
|||
}); |
|||
} |
|||
} |
|||
|
|||
return data; |
|||
}) |
|||
); |
|||
} |
|||
|
|||
private encodeDataSource(aDataSource: DataSource) { |
|||
return Buffer.from(aDataSource, 'utf-8').toString('hex'); |
|||
} |
|||
} |
Loading…
Reference in new issue