remove deprecated code
This commit is contained in:
parent
df0caef0eb
commit
e6d963fc5a
@ -1,106 +0,0 @@
|
|||||||
import { apiRequestHandler } from "./apiRequestHandler";
|
|
||||||
import { AuthenticationSuccessResponse } from "./types";
|
|
||||||
import "isomorphic-fetch";
|
|
||||||
|
|
||||||
|
|
||||||
describe("authentication", () => {
|
|
||||||
const authSuccessResultMatcher: AuthenticationSuccessResponse = {
|
|
||||||
id: expect.any(String),
|
|
||||||
email: expect.any(String),
|
|
||||||
login: expect.any(String),
|
|
||||||
phoneNumber: expect.any(String),
|
|
||||||
avatar: expect.any(String),
|
|
||||||
role: expect.any(String),
|
|
||||||
accessToken: expect.any(String),
|
|
||||||
};
|
|
||||||
|
|
||||||
describe("register route", () => {
|
|
||||||
it("should return an error if any field is empty", async () => {
|
|
||||||
const randomNumber = Math.floor(Math.random() * 1e6);
|
|
||||||
await expect(apiRequestHandler.register({
|
|
||||||
email: `test${randomNumber}@test.com`,
|
|
||||||
login: `test${randomNumber}`,
|
|
||||||
password: "password",
|
|
||||||
phoneNumber: ""
|
|
||||||
})).resolves.toBeInstanceOf(Error);
|
|
||||||
});
|
|
||||||
it("should return an error if user already exists", async () => {
|
|
||||||
const randomNumber = Math.floor(Math.random() * 1e6);
|
|
||||||
await apiRequestHandler.register({
|
|
||||||
email: `test${randomNumber}@test.com`,
|
|
||||||
login: `test${randomNumber}`,
|
|
||||||
password: "password",
|
|
||||||
phoneNumber: "1234"
|
|
||||||
});
|
|
||||||
await expect(apiRequestHandler.register({
|
|
||||||
email: `test${randomNumber}@test.com`,
|
|
||||||
login: `test${randomNumber}`,
|
|
||||||
password: "password",
|
|
||||||
phoneNumber: "12345"
|
|
||||||
})).resolves.toBeInstanceOf(Error);
|
|
||||||
});
|
|
||||||
it("should return user object on register success", async () => {
|
|
||||||
const randomNumber = Math.floor(Math.random() * 1e6);
|
|
||||||
const result = await apiRequestHandler.register({
|
|
||||||
email: `test${randomNumber}@test.com`,
|
|
||||||
login: `test${randomNumber}`,
|
|
||||||
password: "password",
|
|
||||||
phoneNumber: "1234567890"
|
|
||||||
});
|
|
||||||
expect(result).toStrictEqual(authSuccessResultMatcher);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
describe("login route", () => {
|
|
||||||
const randomNumber = Math.floor(Math.random() * 1e6);
|
|
||||||
beforeAll(async () => {
|
|
||||||
await apiRequestHandler.register({
|
|
||||||
email: `test${randomNumber}@test.com`,
|
|
||||||
login: `test${randomNumber}`,
|
|
||||||
password: "password",
|
|
||||||
phoneNumber: "123456"
|
|
||||||
});
|
|
||||||
});
|
|
||||||
it("should return an error if any field is empty", async () => {
|
|
||||||
await expect(apiRequestHandler.login({
|
|
||||||
email: `test${randomNumber}@test.com`,
|
|
||||||
password: ""
|
|
||||||
})).resolves.toBeInstanceOf(Error);
|
|
||||||
});
|
|
||||||
it("should return an error if password is wrong", async () => {
|
|
||||||
await expect(apiRequestHandler.login({
|
|
||||||
email: `test${randomNumber}@test.com`,
|
|
||||||
password: "wrong_password"
|
|
||||||
})).resolves.toBeInstanceOf(Error);
|
|
||||||
});
|
|
||||||
it("should return user object on login success", async () => {
|
|
||||||
const result = await apiRequestHandler.login({
|
|
||||||
email: `test${randomNumber}@test.com`,
|
|
||||||
password: "password"
|
|
||||||
});
|
|
||||||
expect(result).toStrictEqual(authSuccessResultMatcher);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
/**
|
|
||||||
* TODO
|
|
||||||
* 1. jest не отсылает куки с запросом -> возвращается пустой объект без refreshToken
|
|
||||||
* https://github.com/facebook/jest/issues/3547
|
|
||||||
* 2. Сервер возвращает https-only куки, но сервер использует http.
|
|
||||||
*/
|
|
||||||
describe("logout route", () => {
|
|
||||||
const randomNumber = Math.floor(Math.random() * 1e6);
|
|
||||||
it("should return refresh token", async () => {
|
|
||||||
await apiRequestHandler.register({
|
|
||||||
email: `test${randomNumber}@test.com`,
|
|
||||||
login: `test${randomNumber}`,
|
|
||||||
password: "password",
|
|
||||||
phoneNumber: "123456"
|
|
||||||
});
|
|
||||||
await apiRequestHandler.login({
|
|
||||||
email: `test${randomNumber}@test.com`,
|
|
||||||
password: "password",
|
|
||||||
});
|
|
||||||
const result = await apiRequestHandler.logout();
|
|
||||||
expect(result).toStrictEqual({ refreshToken: expect.any(String) });
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
@ -1,236 +0,0 @@
|
|||||||
import {
|
|
||||||
RegistrationRequest,
|
|
||||||
AuthenticationSuccessResponse,
|
|
||||||
LoginRequest,
|
|
||||||
RefreshRequest,
|
|
||||||
CreateTicketRequest,
|
|
||||||
CreateTicketResponse,
|
|
||||||
SendTicketMessageRequest,
|
|
||||||
GetTicketsRequest,
|
|
||||||
GetTicketsResponse,
|
|
||||||
ApiError,
|
|
||||||
GetMessagesRequest,
|
|
||||||
GetMessagesResponse
|
|
||||||
} from "./types";
|
|
||||||
import ReconnectingEventSource from "reconnecting-eventsource";
|
|
||||||
|
|
||||||
|
|
||||||
const TEST_ACCESS_TOKEN = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJJRCI6ImMxcDM0YjhqZHBmOG04Zm43cnIwIiwiU2Vzc2lvbiI6ImMxcDM0YjhqZHBmOG04Zm43cnJnIiwiVXNlciI6IiIsIlRhcmlmZiI6MCwiQ3JlYXRlZCI6MTYxODA5NjY4NTc4MCwiTGFzdFNlZW4iOjE2MTgwOTY2ODU3ODF9.ciJoJiOxzIPv0LY4h3rG8Tf3AsSBXXLcYEpyN9mIki0";
|
|
||||||
|
|
||||||
/** @deprecated */
|
|
||||||
class ApiRequestHandler {
|
|
||||||
private authApiUrl: string;
|
|
||||||
private supportApiUrl: string;
|
|
||||||
private accessToken?: string;
|
|
||||||
|
|
||||||
constructor({ authApiUrl, supportApiUrl }: {
|
|
||||||
authApiUrl: string;
|
|
||||||
supportApiUrl: string;
|
|
||||||
}) {
|
|
||||||
this.authApiUrl = authApiUrl;
|
|
||||||
this.supportApiUrl = supportApiUrl;
|
|
||||||
this.accessToken = TEST_ACCESS_TOKEN;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async register(request: RegistrationRequest): Promise<AuthenticationSuccessResponse | ApiError | Error> {
|
|
||||||
try {
|
|
||||||
const response = await fetch(this.authApiUrl + "auth/register", {
|
|
||||||
method: "POST",
|
|
||||||
body: JSON.stringify(request),
|
|
||||||
headers: {
|
|
||||||
"Accept": "application/json, text/plain, */*",
|
|
||||||
"Content-Type": "application/json"
|
|
||||||
},
|
|
||||||
});
|
|
||||||
const result = await response.json();
|
|
||||||
if (result.error) return new ApiError(result.message);
|
|
||||||
|
|
||||||
this.accessToken = (result as AuthenticationSuccessResponse).accessToken;
|
|
||||||
|
|
||||||
return result as AuthenticationSuccessResponse;
|
|
||||||
} catch (error) {
|
|
||||||
return error as Error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public async login(request: LoginRequest): Promise<AuthenticationSuccessResponse | ApiError | Error> {
|
|
||||||
try {
|
|
||||||
const response = await fetch(this.authApiUrl + "auth/login", {
|
|
||||||
method: "POST",
|
|
||||||
body: JSON.stringify(request),
|
|
||||||
headers: {
|
|
||||||
"Accept": "application/json, text/plain, */*",
|
|
||||||
"Content-Type": "application/json"
|
|
||||||
},
|
|
||||||
});
|
|
||||||
const result = await response.json();
|
|
||||||
if (result.error) return new ApiError(result.message);
|
|
||||||
|
|
||||||
this.accessToken = (result as AuthenticationSuccessResponse).accessToken;
|
|
||||||
|
|
||||||
return result as AuthenticationSuccessResponse;
|
|
||||||
} catch (error) {
|
|
||||||
return error as Error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public async refresh(request: RefreshRequest) {
|
|
||||||
try {
|
|
||||||
const response = await fetch(this.authApiUrl + "auth/refresh", {
|
|
||||||
method: "POST",
|
|
||||||
credentials: "include",
|
|
||||||
body: JSON.stringify(request),
|
|
||||||
headers: {
|
|
||||||
"Accept": "application/json, text/plain, */*",
|
|
||||||
"Content-Type": "application/json"
|
|
||||||
},
|
|
||||||
});
|
|
||||||
const result = await response.json();
|
|
||||||
return result;
|
|
||||||
} catch (error) {
|
|
||||||
return error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public async logout(): Promise<null | ApiError | Error> {
|
|
||||||
try {
|
|
||||||
const response = await fetch(this.authApiUrl + "auth/logout", {
|
|
||||||
method: "POST",
|
|
||||||
credentials: "include"
|
|
||||||
});
|
|
||||||
if (response.status !== 200) return new ApiError(`Unexpected status code. Expected: 200, received: ${response.status}`); // TODO correct success status code
|
|
||||||
|
|
||||||
return null;
|
|
||||||
} catch (error) {
|
|
||||||
return error as Error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public async createTicket(request: CreateTicketRequest): Promise<CreateTicketResponse | ApiError | Error> {
|
|
||||||
try {
|
|
||||||
const response = await fetch(this.supportApiUrl + "support/create", {
|
|
||||||
method: "POST",
|
|
||||||
credentials: "include",
|
|
||||||
body: JSON.stringify(request),
|
|
||||||
headers: {
|
|
||||||
"Accept": "application/json, text/plain, */*",
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
"Authorization": `Bearer ${this.accessToken}`,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
const result = await response.json();
|
|
||||||
if (result.error) return new ApiError(result.message);
|
|
||||||
|
|
||||||
return result as CreateTicketResponse;
|
|
||||||
} catch (error) {
|
|
||||||
return error as Error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public subscribeToAllTickets({ onMessage, onError }: {
|
|
||||||
onMessage: (e: MessageEvent) => void;
|
|
||||||
onError: (e: Event) => void;
|
|
||||||
}) {
|
|
||||||
if (!this.accessToken) throw new Error("Trying to subscribe to SSE without access token");
|
|
||||||
|
|
||||||
const url = `${this.supportApiUrl}/support/subscribe?Authorization=${this.accessToken}`;
|
|
||||||
const eventSource = new ReconnectingEventSource(url);
|
|
||||||
|
|
||||||
eventSource.addEventListener("open", () => console.log(`EventSource connected with ${url}`));
|
|
||||||
eventSource.addEventListener("close", () => console.log(`EventSource closed with ${url}`));
|
|
||||||
eventSource.addEventListener("message", onMessage);
|
|
||||||
eventSource.addEventListener("error", onError);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* TODO
|
|
||||||
* Похоже, что сервер закрывает соединение после каждого сообщения и EventSource выдает ошибку, после чего переподключается и так каждые 3 секунды
|
|
||||||
* https://stackoverflow.com/questions/66047672/server-sent-events-error-despite-http-200
|
|
||||||
* https://stackoverflow.com/questions/53888030/why-are-server-sent-events-fired-every-3-seconds
|
|
||||||
*/
|
|
||||||
|
|
||||||
return () => {
|
|
||||||
eventSource.close();
|
|
||||||
};
|
|
||||||
}
|
|
||||||
public subscribeToTicket({ ticketId, onMessage, onError }: {
|
|
||||||
ticketId: string;
|
|
||||||
onMessage: (e: MessageEvent) => void;
|
|
||||||
onError: (e: Event) => void;
|
|
||||||
}) {
|
|
||||||
if (!this.accessToken) throw new Error("Trying to subscribe to SSE without access token");
|
|
||||||
|
|
||||||
const url = `${this.supportApiUrl}/support/ticket?ticket=${ticketId}&Authorization=${this.accessToken}`;
|
|
||||||
const eventSource = new ReconnectingEventSource(url);
|
|
||||||
|
|
||||||
eventSource.addEventListener("open", () => console.log(`EventSource connected with ${url}`));
|
|
||||||
eventSource.addEventListener("close", () => console.log(`EventSource closed with ${url}`));
|
|
||||||
eventSource.addEventListener("message", onMessage);
|
|
||||||
eventSource.addEventListener("error", onError);
|
|
||||||
|
|
||||||
return () => {
|
|
||||||
eventSource.close();
|
|
||||||
};
|
|
||||||
}
|
|
||||||
public async sendTicketMessage(request: SendTicketMessageRequest): Promise<null | ApiError | Error> {
|
|
||||||
try {
|
|
||||||
const response = await fetch(this.supportApiUrl + "support/send", {
|
|
||||||
method: "POST",
|
|
||||||
credentials: "include",
|
|
||||||
body: JSON.stringify(request),
|
|
||||||
headers: {
|
|
||||||
"Accept": "application/json, text/plain, */*",
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
"Authorization": `Bearer ${this.accessToken}`,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
if (response.status !== 200) return new ApiError(`Unexpected status code. Expected: 200, received: ${response.status}`); // TODO correct success status code
|
|
||||||
|
|
||||||
return null;
|
|
||||||
} catch (error) {
|
|
||||||
return error as Error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public async getTickets(request: GetTicketsRequest, signal: AbortSignal): Promise<GetTicketsResponse | ApiError | Error> {
|
|
||||||
try {
|
|
||||||
const response = await fetch(this.supportApiUrl + "support/getTickets", {
|
|
||||||
method: "POST",
|
|
||||||
credentials: "include",
|
|
||||||
body: JSON.stringify(request),
|
|
||||||
headers: {
|
|
||||||
"Accept": "application/json, text/plain, */*",
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
"Authorization": `Bearer ${this.accessToken}`,
|
|
||||||
},
|
|
||||||
signal,
|
|
||||||
});
|
|
||||||
const result = await response.json();
|
|
||||||
if (result.error) return new ApiError(result.message);
|
|
||||||
|
|
||||||
return result as GetTicketsResponse;
|
|
||||||
} catch (error) {
|
|
||||||
return error as Error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public async getMessages(request: GetMessagesRequest, signal: AbortSignal): Promise<GetMessagesResponse | ApiError | Error> {
|
|
||||||
try {
|
|
||||||
const response = await fetch(this.supportApiUrl + "support/getMessages", {
|
|
||||||
method: "POST",
|
|
||||||
credentials: "include",
|
|
||||||
body: JSON.stringify(request),
|
|
||||||
headers: {
|
|
||||||
"Accept": "application/json, text/plain, */*",
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
"Authorization": `Bearer ${this.accessToken}`,
|
|
||||||
},
|
|
||||||
signal,
|
|
||||||
});
|
|
||||||
const result = await response.json();
|
|
||||||
if (result.error) return new ApiError(result.message);
|
|
||||||
|
|
||||||
return result as GetMessagesResponse;
|
|
||||||
} catch (error) {
|
|
||||||
return error as Error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @deprecated */
|
|
||||||
export const apiRequestHandler = new ApiRequestHandler({
|
|
||||||
authApiUrl: "http://localhost:8080/",
|
|
||||||
supportApiUrl: "http://localhost:1488/",
|
|
||||||
});
|
|
@ -1,106 +0,0 @@
|
|||||||
|
|
||||||
/** @deprecated */
|
|
||||||
export type LoginRequest = {
|
|
||||||
email?: string;
|
|
||||||
password?: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
/** @deprecated */
|
|
||||||
export type RegistrationRequest = {
|
|
||||||
login?: string;
|
|
||||||
email?: string;
|
|
||||||
phoneNumber?: string;
|
|
||||||
password?: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
/** @deprecated */
|
|
||||||
export type AuthenticationSuccessResponse = {
|
|
||||||
id: string;
|
|
||||||
email: string;
|
|
||||||
login: string;
|
|
||||||
phoneNumber: string;
|
|
||||||
avatar: string;
|
|
||||||
role: string;
|
|
||||||
accessToken: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
/** @deprecated */
|
|
||||||
export type RefreshRequest = {
|
|
||||||
userId: string;
|
|
||||||
refreshToken?: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
/** @deprecated */
|
|
||||||
export type CreateTicketRequest = {
|
|
||||||
Title: string;
|
|
||||||
Message: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
/** @deprecated */
|
|
||||||
export type CreateTicketResponse = {
|
|
||||||
Ticket: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
/** @deprecated */
|
|
||||||
export type SendTicketMessageRequest = {
|
|
||||||
message: string;
|
|
||||||
ticket: string;
|
|
||||||
lang: string;
|
|
||||||
files: string[];
|
|
||||||
};
|
|
||||||
|
|
||||||
/** @deprecated */
|
|
||||||
export type GetTicketsRequest = {
|
|
||||||
amt: number;
|
|
||||||
page: number;
|
|
||||||
srch: string;
|
|
||||||
status: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
/** @deprecated */
|
|
||||||
export type GetTicketsResponse = {
|
|
||||||
count: number;
|
|
||||||
data: Ticket[];
|
|
||||||
};
|
|
||||||
|
|
||||||
/** @deprecated */
|
|
||||||
export type Ticket = {
|
|
||||||
id: string;
|
|
||||||
user: string;
|
|
||||||
sess: string;
|
|
||||||
ans: string;
|
|
||||||
state: string;
|
|
||||||
top_message: TicketMessage;
|
|
||||||
title: string;
|
|
||||||
created_at: string;
|
|
||||||
updated_at: string;
|
|
||||||
rate: number;
|
|
||||||
};
|
|
||||||
|
|
||||||
/** @deprecated */
|
|
||||||
export type TicketMessage = {
|
|
||||||
id: string;
|
|
||||||
ticket_id: string;
|
|
||||||
user_id: string,
|
|
||||||
session_id: string;
|
|
||||||
message: string;
|
|
||||||
files: string[],
|
|
||||||
shown: { [key: string]: number; },
|
|
||||||
request_screenshot: string,
|
|
||||||
created_at: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
/** @deprecated */
|
|
||||||
export type GetMessagesRequest = {
|
|
||||||
amt: number;
|
|
||||||
page: number;
|
|
||||||
srch: string;
|
|
||||||
ticket: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
/** @deprecated */
|
|
||||||
export type GetMessagesResponse = TicketMessage[];
|
|
||||||
|
|
||||||
|
|
||||||
/** @deprecated */
|
|
||||||
export class ApiError extends Error { }
|
|
Loading…
Reference in New Issue
Block a user