Finished implementing CallService
This commit is contained in:
17
src/domain/callService.schema.ts
Normal file
17
src/domain/callService.schema.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
export interface InviteToCallReq {
|
||||
userid: string
|
||||
chatid: string
|
||||
targetId: string
|
||||
}
|
||||
|
||||
export interface GetRTCAccessReq {
|
||||
roomId: string
|
||||
roomType: string
|
||||
userid: string
|
||||
networkId: string
|
||||
}
|
||||
|
||||
export interface GetRTCAccessResp {
|
||||
rtcConfig: RTCConfiguration
|
||||
liveKitToken: string
|
||||
}
|
||||
10
src/mocks/handlers/call.http.ts
Normal file
10
src/mocks/handlers/call.http.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import {http, HttpResponse} from "msw";
|
||||
import {GetRTCAccessResp} from "../../domain/callService.schema";
|
||||
|
||||
export const callHandlers = [
|
||||
http.post('*/v2/chat/getRTCAccess', () => {
|
||||
return HttpResponse.json(<GetRTCAccessResp>{
|
||||
liveKitToken: "liveKitToken",
|
||||
})
|
||||
}),
|
||||
]
|
||||
@@ -1,9 +1,11 @@
|
||||
import {networkHandlers} from "./handlers/auth.http";
|
||||
import {authHandlers} from "./handlers/network.http";
|
||||
import {pictureHandlers} from "./handlers/picture.http";
|
||||
import {callHandlers} from "./handlers/call.http";
|
||||
|
||||
export const allHandlers = [
|
||||
...authHandlers,
|
||||
...networkHandlers,
|
||||
...pictureHandlers
|
||||
...pictureHandlers,
|
||||
...callHandlers
|
||||
]
|
||||
11
src/services/callService.test.ts
Normal file
11
src/services/callService.test.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import {describe, expect, it} from "vitest";
|
||||
import {CallService} from "./callService";
|
||||
|
||||
describe("CallService", () => {
|
||||
const handler = new CallService("", "")
|
||||
|
||||
it('should get RTC access', async () => {
|
||||
const access = await handler.getRTCAccess("", "", "", null)
|
||||
expect(access.liveKitToken).toBe("liveKitToken");
|
||||
});
|
||||
})
|
||||
60
src/services/callService.ts
Normal file
60
src/services/callService.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import {getClient} from "../core/http";
|
||||
import {OtpPleCodeSendTestingResp, OtpSendCodeReq} from "../domain/authService.schema";
|
||||
import {isAxiosError} from "axios";
|
||||
import {GenericErrorBody} from "../domain/http.schema";
|
||||
import {GetRTCAccessReq, GetRTCAccessResp, InviteToCallReq} from "../domain/callService.schema";
|
||||
|
||||
export class CallService {
|
||||
userid: string;
|
||||
token: string;
|
||||
|
||||
constructor(token: string, userid: string) {
|
||||
this.token = token;
|
||||
this.userid = userid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls your chat partner (WebSocket + Apple VoIP PUSH)
|
||||
* @param targetId
|
||||
* @param chatid
|
||||
*/
|
||||
async inviteToCall(targetId: string, chatid: string): Promise<void> {
|
||||
try {
|
||||
await getClient(false).post("v2/chat/rtcInvite", <InviteToCallReq>{
|
||||
userid: this.userid,
|
||||
chatid: chatid,
|
||||
targetId: targetId
|
||||
});
|
||||
return
|
||||
} catch (e) {
|
||||
if (isAxiosError<GenericErrorBody>(e)) {
|
||||
throw e;
|
||||
}
|
||||
throw new Error("Unexpected error")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides access to liveKit and a WebRTC configuration
|
||||
* @param targetId
|
||||
* @param roomId
|
||||
* @param roomType
|
||||
* @param networkId
|
||||
*/
|
||||
async getRTCAccess(targetId: string, roomId: string, roomType: string, networkId: string|null): Promise<GetRTCAccessResp> {
|
||||
try {
|
||||
const resp = await getClient(false).post("v2/chat/getRTCAccess", <GetRTCAccessReq>{
|
||||
userid: targetId,
|
||||
networkId: networkId,
|
||||
roomId: roomId,
|
||||
roomType: roomType,
|
||||
});
|
||||
return resp.data
|
||||
} catch (e) {
|
||||
if (isAxiosError<GenericErrorBody>(e)) {
|
||||
throw e;
|
||||
}
|
||||
throw new Error("Unexpected error")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user