Skip to content

Instantly share code, notes, and snippets.

View AlexDaSoul's full-sized avatar

Alexander Dukhnovskiy AlexDaSoul

View GitHub Profile
@AlexDaSoul
AlexDaSoul / .ts
Created January 19, 2020 15:16
nga-24
@Controller()
export class ChatController {
constructor(private readonly chatService: ChatService) {
}
@UseGuards(JwtGuard)
@GrpcMethod('ChatService', 'GetChat')
@UseFilters(RpcExceptionFilter.for('ChatService::getChat'))
public getChat(data: Identity<chatApiTypes.chat.Stub>): Observable<chatApi.chat.ChatList> {
@AlexDaSoul
AlexDaSoul / .ts
Created January 19, 2020 15:15
nga-23
@Injectable()
export class ChatService {
constructor(
private readonly messageDataFinder: MessageDataFinder,
private readonly chatEventService: ChatEventService,
) {
}
public getChatStream(): Observable<api.chat.ChatList> {
@AlexDaSoul
AlexDaSoul / .ts
Created January 19, 2020 15:14
nga-23
@Injectable()
export class MessageService implements OnModuleInit {
@Client(grpcUser) private readonly grpcUserClient: ClientGrpc;
private grpcUserService: userApi.user.UserService;
constructor(
private readonly messageDataProducer: MessageDataProducer,
private readonly messageDataUpdater: MessageDataUpdater,
private readonly messageDataRemover: MessageDataRemover,
@AlexDaSoul
AlexDaSoul / .ts
Last active January 19, 2020 15:39
nga-22
@Injectable()
export class ChatEventService {
private readonly updates$ = new Subject<api.chat.Message[]>();
public emit(message: api.chat.Message): void {
this.updates$.next([message]);
}
public broadcast(): Observable<api.chat.Message[]> {
return this.updates$.asObservable();
@AlexDaSoul
AlexDaSoul / .ts
Created January 19, 2020 15:13
nga-21
@GrpcMethod('AuthService', 'Auth')
@UseFilters(RpcExceptionFilter.for('AuthController::auth'))
public auth(data: AuthReqDTO): Observable<authApi.auth.AuthRes> {
return this.grpcUserService.verifyUser(data, meta).pipe(
map(user => this.jwtService.addToken(user)),
map(token => ({ token })),
);
}
@AlexDaSoul
AlexDaSoul / .ts
Created January 19, 2020 15:11
nga-20
@GrpcMethod('AuthService', 'GetCertStream')
@UseFilters(RpcExceptionFilter.for('AuthController::getCertStream'))
public getCertStream(data: Identity<authTypes.auth.Stub>): Observable<authApi.auth.GetCertStreamRes> {
return this.certSubscribeService.getCert()
.pipe(map(key => ({ key })));
}
@AlexDaSoul
AlexDaSoul / .ts
Last active January 19, 2020 15:39
nga-19
const TOKEN_HEADER_NAME = 'authorization';
const DECODING_OPTIONS = {
algorithms: ['RS256'],
};
export class JwtGuard implements CanActivate {
canActivate(context: ExecutionContext): boolean {
const meta = context.getArgByIndex(1);
const token = meta.get(TOKEN_HEADER_NAME)[0];
@AlexDaSoul
AlexDaSoul / .ts
Last active January 19, 2020 15:39
nga-18
@Catch(...EXCEPTION_LIST)
export class RpcExceptionFilter extends BaseRpcExceptionFilter {
private readonly exceptionHandlerFactory: IExceptionHandlerFactory;
public static for(label: string): RpcExceptionFilter {
return new RpcExceptionFilter(label);
}
protected constructor(protected readonly label: string) {
super();
@AlexDaSoul
AlexDaSoul / .ts
Created January 19, 2020 15:06
nga-17
@GrpcMethod('UserService', 'VerifyUser')
@UseFilters(RpcExceptionFilter.for('UserController::verifyUser'))
public verifyUser(data: VerifyUserReqDTO, meta: Metadata): Observable<userTypes.user.User> {
/* for example metadata */
this.logger.info(meta);
/* for example metadata */
return this.userService.verifyUser(data);
}
@AlexDaSoul
AlexDaSoul / .ts
Created January 19, 2020 15:05
nga-16
@GrpcMethod('AuthService', 'Auth')
@UseFilters(RpcExceptionFilter.for('AuthController::auth'))
public auth(data: AuthReqDTO): Observable<authApi.auth.AuthRes> {
/* for example metadata */
const meta = new Metadata();
meta.add('X-Grpc-From', 'auth');
/* for example metadata */
return this.grpcUserService.verifyUser(data, meta).pipe(
map(user => this.jwtService.addToken(user)),