Initial structure
This commit is contained in:
parent
ecb354cf6e
commit
874bd18f11
14 changed files with 157 additions and 1 deletions
5
src/api/config.json
Normal file
5
src/api/config.json
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"schema": "http",
|
||||
"url": "localhost",
|
||||
"port": 3012
|
||||
}
|
||||
12
src/api/game/game.ts
Normal file
12
src/api/game/game.ts
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
import { callApi, HTTP } from "../tools";
|
||||
import { IGameTimeResponse } from "./types";
|
||||
|
||||
async function fetchTimeApi(): Promise<IGameTimeResponse> {
|
||||
const data = await callApi(HTTP.GET, "game/time");
|
||||
|
||||
return {
|
||||
time: data,
|
||||
};
|
||||
}
|
||||
|
||||
export { fetchTimeApi };
|
||||
5
src/api/game/types.ts
Normal file
5
src/api/game/types.ts
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
interface IGameTimeResponse {
|
||||
time: number;
|
||||
}
|
||||
|
||||
export { type IGameTimeResponse };
|
||||
41
src/api/tools.ts
Normal file
41
src/api/tools.ts
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
import config from "./config.json";
|
||||
|
||||
enum HTTP {
|
||||
GET,
|
||||
POST,
|
||||
}
|
||||
|
||||
async function callApi(type: HTTP, path: string, body?: any) {
|
||||
let response: Response;
|
||||
switch (type) {
|
||||
case HTTP.GET:
|
||||
response = await fetch(
|
||||
`${config.schema}://${config.url}:${config.port}/${path}`,
|
||||
{
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
},
|
||||
);
|
||||
break;
|
||||
case HTTP.POST:
|
||||
response = await fetch(
|
||||
`${config.schema}://${config.url}:${config.port}/${path}`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(body ?? {}),
|
||||
},
|
||||
);
|
||||
break;
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
export { callApi, HTTP };
|
||||
Loading…
Add table
Add a link
Reference in a new issue