Refactor event bus and queue system; detect cycles in DI realization and make
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
54
src/auth/serial/AuthenticationEventSerializer.ts
Normal file
54
src/auth/serial/AuthenticationEventSerializer.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import {BaseSerializer, ObjectSerializer, SerialPayload} from '../../support/bus'
|
||||
import {AuthenticationEvent} from '../event/AuthenticationEvent'
|
||||
import {ErrorWithContext, JSONState} from '../../util'
|
||||
import {Authenticatable} from '../types'
|
||||
import {StaticInstantiable} from '../../di'
|
||||
import {SecurityContext} from '../context/SecurityContext'
|
||||
import {UserAuthenticatedEvent} from '../event/UserAuthenticatedEvent'
|
||||
import {UserAuthenticationResumedEvent} from '../event/UserAuthenticationResumedEvent'
|
||||
import {UserFlushedEvent} from '../event/UserFlushedEvent'
|
||||
|
||||
export interface AuthenticationEventSerialPayload extends JSONState {
|
||||
user: SerialPayload<Authenticatable, JSONState>
|
||||
eventName: string
|
||||
}
|
||||
|
||||
@ObjectSerializer()
|
||||
export class AuthenticationEventSerializer extends BaseSerializer<AuthenticationEvent, AuthenticationEventSerialPayload> {
|
||||
protected async decodeSerial(serial: AuthenticationEventSerialPayload): Promise<AuthenticationEvent> {
|
||||
const user = await this.getSerialization().decode(serial.user)
|
||||
const context = await this.getRequest().make(SecurityContext)
|
||||
|
||||
const EventClass = this.getEventClass(serial.eventName)
|
||||
return new EventClass(user, context)
|
||||
}
|
||||
|
||||
protected async encodeActual(actual: AuthenticationEvent): Promise<AuthenticationEventSerialPayload> {
|
||||
return {
|
||||
eventName: actual.eventName,
|
||||
user: await this.getSerialization().encode(actual.user),
|
||||
}
|
||||
}
|
||||
|
||||
protected getName(): string {
|
||||
return '@extollo/lib:AuthenticationEventSerializer'
|
||||
}
|
||||
|
||||
matchActual(some: AuthenticationEvent): boolean {
|
||||
return some instanceof AuthenticationEvent
|
||||
}
|
||||
|
||||
protected getEventClass(name: string): StaticInstantiable<AuthenticationEvent> {
|
||||
if ( name === '@extollo/lib:UserAuthenticatedEvent' ) {
|
||||
return UserAuthenticatedEvent
|
||||
} else if ( name === '@extollo/lib:UserAuthenticationResumedEvent' ) {
|
||||
return UserAuthenticationResumedEvent
|
||||
} else if ( name === '@extollo/lib:UserFlushedEvent' ) {
|
||||
return UserFlushedEvent
|
||||
}
|
||||
|
||||
throw new ErrorWithContext('Unable to map event name to AuthenticationEvent implementation', {
|
||||
eventName: name,
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user