blob: c7f6e5984e4fa41059eefe7a2a6aae7e28bc21bb (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
import {SocketController} from "./controllers/connection/socket";
export class ServerConnection {
private static _socketControllerInstance: SocketController;
public static connect(onConnect: () => any): void {
this._socketControllerInstance = new SocketController(onConnect);
}
public static send(request: IRequest): Promise<any> {
return new Promise((resolve, reject) => {
let checkUnimplemented = ServerConnection.interceptUnimplementedEndpoint(request);
if (checkUnimplemented) {
resolve(checkUnimplemented.content);
return;
}
this._socketControllerInstance.sendRequest(request, (response: IResponse) => {
if (response.status.code === 200) {
ServerConnection.convertFlatToNestedPositionData(response.content, resolve);
} else {
reject(response.status);
}
});
})
}
public static convertFlatToNestedPositionData(responseContent, resolve): void {
let nestPositionCoords = (content: any) => {
if (content["positionX"] !== undefined) {
content["position"] = {
x: content["positionX"],
y: content["positionY"]
};
}
};
if (responseContent instanceof Array) {
responseContent.forEach(nestPositionCoords);
} else {
nestPositionCoords(responseContent);
}
resolve(responseContent);
}
/**
* Intercepts endpoints that are still unimplemented and responds with mock data.
*
* @param request The request
* @returns {any} A response, or null if the endpoint is not on the list of unimplemented ones.
*/
public static interceptUnimplementedEndpoint(request: IRequest): IResponse {
// Endpoints that are unimplemented can be intercepted here
return null;
}
}
|