fix: reduce websockets api communication data (#919)
This commit is contained in:
parent
4ff78452f9
commit
c4ae12d53f
8 changed files with 78 additions and 33 deletions
|
@ -143,7 +143,18 @@ export class HttpServiceProxy implements ServiceProxyInterface {
|
|||
return
|
||||
}
|
||||
|
||||
await this.callServer(this.webSocketServerUrl, request, response, endpointOrMethodIdentifier, payload)
|
||||
const isARequestComingFromApiGatewayAndShouldBeKeptInMinimalFormat = request.headers.connectionid !== undefined
|
||||
if (isARequestComingFromApiGatewayAndShouldBeKeptInMinimalFormat) {
|
||||
await this.callServerWithLegacyFormat(
|
||||
this.webSocketServerUrl,
|
||||
request,
|
||||
response,
|
||||
endpointOrMethodIdentifier,
|
||||
payload,
|
||||
)
|
||||
} else {
|
||||
await this.callServer(this.webSocketServerUrl, request, response, endpointOrMethodIdentifier, payload)
|
||||
}
|
||||
}
|
||||
|
||||
async callPaymentsServer(
|
||||
|
|
|
@ -16,11 +16,23 @@ describe('AddWebSocketsConnection', () => {
|
|||
|
||||
logger = {} as jest.Mocked<Logger>
|
||||
logger.debug = jest.fn()
|
||||
logger.error = jest.fn()
|
||||
})
|
||||
|
||||
it('should save a web sockets connection for a user for further communication', async () => {
|
||||
await createUseCase().execute({ userUuid: '1-2-3', connectionId: '2-3-4' })
|
||||
const result = await createUseCase().execute({ userUuid: '1-2-3', connectionId: '2-3-4' })
|
||||
|
||||
expect(webSocketsConnectionRepository.saveConnection).toHaveBeenCalledWith('1-2-3', '2-3-4')
|
||||
expect(result.isFailed()).toBe(false)
|
||||
})
|
||||
|
||||
it('should return a failure if the web sockets connection could not be saved', async () => {
|
||||
webSocketsConnectionRepository.saveConnection = jest
|
||||
.fn()
|
||||
.mockRejectedValueOnce(new Error('Could not save connection'))
|
||||
|
||||
const result = await createUseCase().execute({ userUuid: '1-2-3', connectionId: '2-3-4' })
|
||||
|
||||
expect(result.isFailed()).toBe(true)
|
||||
})
|
||||
})
|
||||
|
|
|
@ -1,26 +1,32 @@
|
|||
import { inject, injectable } from 'inversify'
|
||||
import { Logger } from 'winston'
|
||||
import { Result, UseCaseInterface } from '@standardnotes/domain-core'
|
||||
|
||||
import TYPES from '../../../Bootstrap/Types'
|
||||
import { WebSocketsConnectionRepositoryInterface } from '../../WebSockets/WebSocketsConnectionRepositoryInterface'
|
||||
import { UseCaseInterface } from '../UseCaseInterface'
|
||||
import { AddWebSocketsConnectionDTO } from './AddWebSocketsConnectionDTO'
|
||||
import { AddWebSocketsConnectionResponse } from './AddWebSocketsConnectionResponse'
|
||||
|
||||
@injectable()
|
||||
export class AddWebSocketsConnection implements UseCaseInterface {
|
||||
export class AddWebSocketsConnection implements UseCaseInterface<void> {
|
||||
constructor(
|
||||
@inject(TYPES.WebSocketsConnectionRepository)
|
||||
private webSocketsConnectionRepository: WebSocketsConnectionRepositoryInterface,
|
||||
@inject(TYPES.Logger) private logger: Logger,
|
||||
) {}
|
||||
|
||||
async execute(dto: AddWebSocketsConnectionDTO): Promise<AddWebSocketsConnectionResponse> {
|
||||
this.logger.debug(`Persisting connection ${dto.connectionId} for user ${dto.userUuid}`)
|
||||
async execute(dto: AddWebSocketsConnectionDTO): Promise<Result<void>> {
|
||||
try {
|
||||
this.logger.debug(`Persisting connection ${dto.connectionId} for user ${dto.userUuid}`)
|
||||
|
||||
await this.webSocketsConnectionRepository.saveConnection(dto.userUuid, dto.connectionId)
|
||||
await this.webSocketsConnectionRepository.saveConnection(dto.userUuid, dto.connectionId)
|
||||
|
||||
return {
|
||||
success: true,
|
||||
return Result.ok()
|
||||
} catch (error) {
|
||||
this.logger.error(
|
||||
`Error persisting connection ${dto.connectionId} for user ${dto.userUuid}: ${(error as Error).message}`,
|
||||
)
|
||||
|
||||
return Result.fail((error as Error).message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,3 +0,0 @@
|
|||
export type AddWebSocketsConnectionResponse = {
|
||||
success: boolean
|
||||
}
|
|
@ -16,11 +16,23 @@ describe('RemoveWebSocketsConnection', () => {
|
|||
|
||||
logger = {} as jest.Mocked<Logger>
|
||||
logger.debug = jest.fn()
|
||||
logger.error = jest.fn()
|
||||
})
|
||||
|
||||
it('should remove a web sockets connection', async () => {
|
||||
await createUseCase().execute({ connectionId: '2-3-4' })
|
||||
const result = await createUseCase().execute({ connectionId: '2-3-4' })
|
||||
|
||||
expect(webSocketsConnectionRepository.removeConnection).toHaveBeenCalledWith('2-3-4')
|
||||
expect(result.isFailed()).toBe(false)
|
||||
})
|
||||
|
||||
it('should return a failure if the web sockets connection could not be removed', async () => {
|
||||
webSocketsConnectionRepository.removeConnection = jest
|
||||
.fn()
|
||||
.mockRejectedValueOnce(new Error('Could not remove connection'))
|
||||
|
||||
const result = await createUseCase().execute({ connectionId: '2-3-4' })
|
||||
|
||||
expect(result.isFailed()).toBe(true)
|
||||
})
|
||||
})
|
||||
|
|
|
@ -1,26 +1,30 @@
|
|||
import { inject, injectable } from 'inversify'
|
||||
import { Logger } from 'winston'
|
||||
import { Result, UseCaseInterface } from '@standardnotes/domain-core'
|
||||
|
||||
import TYPES from '../../../Bootstrap/Types'
|
||||
import { WebSocketsConnectionRepositoryInterface } from '../../WebSockets/WebSocketsConnectionRepositoryInterface'
|
||||
import { UseCaseInterface } from '../UseCaseInterface'
|
||||
import { RemoveWebSocketsConnectionDTO } from './RemoveWebSocketsConnectionDTO'
|
||||
import { RemoveWebSocketsConnectionResponse } from './RemoveWebSocketsConnectionResponse'
|
||||
|
||||
@injectable()
|
||||
export class RemoveWebSocketsConnection implements UseCaseInterface {
|
||||
export class RemoveWebSocketsConnection implements UseCaseInterface<void> {
|
||||
constructor(
|
||||
@inject(TYPES.WebSocketsConnectionRepository)
|
||||
private webSocketsConnectionRepository: WebSocketsConnectionRepositoryInterface,
|
||||
@inject(TYPES.Logger) private logger: Logger,
|
||||
) {}
|
||||
|
||||
async execute(dto: RemoveWebSocketsConnectionDTO): Promise<RemoveWebSocketsConnectionResponse> {
|
||||
this.logger.debug(`Removing connection ${dto.connectionId}`)
|
||||
async execute(dto: RemoveWebSocketsConnectionDTO): Promise<Result<void>> {
|
||||
try {
|
||||
this.logger.debug(`Removing connection ${dto.connectionId}`)
|
||||
|
||||
await this.webSocketsConnectionRepository.removeConnection(dto.connectionId)
|
||||
await this.webSocketsConnectionRepository.removeConnection(dto.connectionId)
|
||||
|
||||
return {
|
||||
success: true,
|
||||
return Result.ok()
|
||||
} catch (error) {
|
||||
this.logger.error(`Error removing connection ${dto.connectionId}: ${(error as Error).message}`)
|
||||
|
||||
return Result.fail((error as Error).message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,3 +0,0 @@
|
|||
export type RemoveWebSocketsConnectionResponse = {
|
||||
success: boolean
|
||||
}
|
|
@ -36,21 +36,27 @@ export class AnnotatedWebSocketsController extends BaseHttpController {
|
|||
async storeWebSocketsConnection(
|
||||
request: Request,
|
||||
response: Response,
|
||||
): Promise<results.JsonResult | results.BadRequestErrorMessageResult> {
|
||||
await this.addWebSocketsConnection.execute({
|
||||
): Promise<results.OkResult | results.BadRequestResult> {
|
||||
const result = await this.addWebSocketsConnection.execute({
|
||||
userUuid: response.locals.user.uuid,
|
||||
connectionId: request.params.connectionId,
|
||||
})
|
||||
|
||||
return this.json({ success: true })
|
||||
if (result.isFailed()) {
|
||||
return this.badRequest()
|
||||
}
|
||||
|
||||
return this.ok()
|
||||
}
|
||||
|
||||
@httpDelete('/connections/:connectionId')
|
||||
async deleteWebSocketsConnection(
|
||||
request: Request,
|
||||
): Promise<results.JsonResult | results.BadRequestErrorMessageResult> {
|
||||
await this.removeWebSocketsConnection.execute({ connectionId: request.params.connectionId })
|
||||
async deleteWebSocketsConnection(request: Request): Promise<results.OkResult | results.BadRequestResult> {
|
||||
const result = await this.removeWebSocketsConnection.execute({ connectionId: request.params.connectionId })
|
||||
|
||||
return this.json({ success: true })
|
||||
if (result.isFailed()) {
|
||||
return this.badRequest()
|
||||
}
|
||||
|
||||
return this.ok()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue