feat(api): add websocket api definitions

This commit is contained in:
Karol Sójko
2022-09-20 13:20:52 +02:00
parent dd5ca0c28c
commit 4a773fa537
18 changed files with 190 additions and 5 deletions

View File

@@ -0,0 +1,3 @@
export enum WebSocketApiOperations {
CreatingConnectionToken,
}

View File

@@ -0,0 +1,61 @@
import { WebSocketConnectionTokenResponse } from '../../Response'
import { WebSocketServerInterface } from '../../Server/WebSocket/WebSocketServerInterface'
import { WebSocketApiOperations } from './WebSocketApiOperations'
import { WebSocketApiService } from './WebSocketApiService'
describe('WebSocketApiService', () => {
let webSocketServer: WebSocketServerInterface
const createService = () => new WebSocketApiService(webSocketServer)
beforeEach(() => {
webSocketServer = {} as jest.Mocked<WebSocketServerInterface>
webSocketServer.createConnectionToken = jest.fn().mockReturnValue({
data: { token: 'foobar' },
} as jest.Mocked<WebSocketConnectionTokenResponse>)
})
it('should create a websocket connection token', async () => {
const response = await createService().createConnectionToken()
expect(response).toEqual({
data: {
token: 'foobar',
},
})
expect(webSocketServer.createConnectionToken).toHaveBeenCalledWith({})
})
it('should not create a token if it is already creating', async () => {
const service = createService()
Object.defineProperty(service, 'operationsInProgress', {
get: () => new Map([[WebSocketApiOperations.CreatingConnectionToken, true]]),
})
let error = null
try {
await service.createConnectionToken()
} catch (caughtError) {
error = caughtError
}
expect(error).not.toBeNull()
})
it('should not create a token if the server fails', async () => {
webSocketServer.createConnectionToken = jest.fn().mockImplementation(() => {
throw new Error('Oops')
})
let error = null
try {
await createService().createConnectionToken()
} catch (caughtError) {
error = caughtError
}
expect(error).not.toBeNull()
})
})

View File

@@ -0,0 +1,33 @@
import { ErrorMessage } from '../../Error/ErrorMessage'
import { ApiCallError } from '../../Error/ApiCallError'
import { WebSocketApiServiceInterface } from './WebSocketApiServiceInterface'
import { WebSocketApiOperations } from './WebSocketApiOperations'
import { WebSocketServerInterface } from '../../Server'
import { WebSocketConnectionTokenResponse } from '../../Response'
export class WebSocketApiService implements WebSocketApiServiceInterface {
private operationsInProgress: Map<WebSocketApiOperations, boolean>
constructor(private webSocketServer: WebSocketServerInterface) {
this.operationsInProgress = new Map()
}
async createConnectionToken(): Promise<WebSocketConnectionTokenResponse> {
if (this.operationsInProgress.get(WebSocketApiOperations.CreatingConnectionToken)) {
throw new ApiCallError(ErrorMessage.GenericInProgress)
}
this.operationsInProgress.set(WebSocketApiOperations.CreatingConnectionToken, true)
try {
const response = await this.webSocketServer.createConnectionToken({})
this.operationsInProgress.set(WebSocketApiOperations.CreatingConnectionToken, false)
return response
} catch (error) {
throw new ApiCallError(ErrorMessage.GenericFail)
}
}
}

View File

@@ -0,0 +1,5 @@
import { WebSocketConnectionTokenResponse } from '../../Response'
export interface WebSocketApiServiceInterface {
createConnectionToken(): Promise<WebSocketConnectionTokenResponse>
}

View File

@@ -3,3 +3,5 @@ export * from './Subscription/SubscriptionApiService'
export * from './Subscription/SubscriptionApiServiceInterface'
export * from './User/UserApiService'
export * from './User/UserApiServiceInterface'
export * from './WebSocket/WebSocketApiService'
export * from './WebSocket/WebSocketApiServiceInterface'