mirror of
https://github.com/hubHarmony/servii-frontend.git
synced 2024-11-17 21:40:30 +00:00
[+] ServiiApi class for easier API usage.
[+] TypeScript support [-] Shouldn't pass the tests
This commit is contained in:
parent
9accc4f279
commit
f270e3220e
@ -6,11 +6,15 @@ module.exports = {
|
||||
'plugin:react/recommended',
|
||||
'plugin:react/jsx-runtime',
|
||||
'plugin:react-hooks/recommended',
|
||||
"plugin:@typescript-eslint/recommended",
|
||||
],
|
||||
ignorePatterns: ['dist', '.eslintrc.cjs'],
|
||||
parserOptions: { ecmaVersion: 'latest', sourceType: 'module' },
|
||||
settings: { react: { version: '18.2' } },
|
||||
plugins: ['react-refresh'],
|
||||
plugins: [
|
||||
'react-refresh',
|
||||
'@typescript-eslint',
|
||||
],
|
||||
rules: {
|
||||
'react/jsx-no-target-blank': 'off',
|
||||
'react-refresh/only-export-components': [
|
||||
|
983
package-lock.json
generated
983
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -10,17 +10,22 @@
|
||||
"preview": "vite preview"
|
||||
},
|
||||
"dependencies": {
|
||||
"@types/jest": "^29.5.12",
|
||||
"@types/node": "^20.14.9",
|
||||
"firebase": "^10.12.2",
|
||||
"react": "^18.3.1",
|
||||
"react-dom": "^18.3.1",
|
||||
"react-router-dom": "^6.24.0",
|
||||
"react-toastify": "^10.0.5",
|
||||
"sass": "^1.77.6",
|
||||
"toast": "^0.5.4"
|
||||
"toast": "^0.5.4",
|
||||
"typescript": "^5.5.3"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/react": "^18.3.3",
|
||||
"@types/react-dom": "^18.3.0",
|
||||
"@typescript-eslint/eslint-plugin": "^7.15.0",
|
||||
"@typescript-eslint/parser": "^7.15.0",
|
||||
"@vitejs/plugin-react": "^4.3.1",
|
||||
"eslint": "^8.57.0",
|
||||
"eslint-plugin-react": "^7.34.2",
|
||||
|
@ -1,7 +1,7 @@
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import styles from './CreateServer.module.scss';
|
||||
import { getUserSubdomain } from "../../service/firebase";
|
||||
import { saveSubdomain } from "../../service/serverService";
|
||||
import { saveSubdomain } from "../../service/api.tsx";
|
||||
|
||||
const CreateServer = ({ user, onCreateServer }) => {
|
||||
const [subdomain, setSubdomain] = useState(null);
|
||||
|
@ -4,7 +4,9 @@ import Navbar from '../../components/navbar/Navbar';
|
||||
import styles from './DashboardPage.module.scss';
|
||||
import NoServer from '../NoServer/NoServer';
|
||||
import { getUserSubdomain } from "../../service/firebase";
|
||||
import { fetchServers, createServer, runServer, stopServer, deleteServer } from '../../service/serverService';
|
||||
import '../../service/api.tsx';
|
||||
import serviiApi from "../../service/api.js";
|
||||
|
||||
|
||||
const DashboardPage = ({ user }) => {
|
||||
const [servers, setServers] = useState([]);
|
||||
@ -12,7 +14,8 @@ const DashboardPage = ({ user }) => {
|
||||
|
||||
const loadServers = async () => {
|
||||
try {
|
||||
const data = await fetchServers();
|
||||
const ApiResponse = await serviiApi.fetchServers();
|
||||
const data = ApiResponse.message
|
||||
setServers(data);
|
||||
const userSubdomain = await getUserSubdomain(user.uid);
|
||||
setSubdomain(userSubdomain);
|
||||
|
128
src/service/api.tsx
Normal file
128
src/service/api.tsx
Normal file
@ -0,0 +1,128 @@
|
||||
// src/services/serverService.ts
|
||||
import {getAuth} from 'firebase/auth';
|
||||
|
||||
const apiUrl: string = 'http://176.165.62.226:3000';
|
||||
|
||||
interface ApiResponse {
|
||||
return_code: number;
|
||||
message: string;
|
||||
}
|
||||
|
||||
//fetchServers, accountCreate, accountDelete,
|
||||
interface BaseRequest {
|
||||
token: string;
|
||||
}
|
||||
//setSubdomain
|
||||
interface SubdomainRequest {
|
||||
token: string;
|
||||
subdomain: string;
|
||||
}
|
||||
//serverDelete, serverRun, serverStop
|
||||
interface ServerRequest {
|
||||
token: string;
|
||||
name: string;
|
||||
}
|
||||
//serverCreate
|
||||
interface ServerCreationRequest {
|
||||
token: string;
|
||||
name: string;
|
||||
version: string;
|
||||
framework: string;
|
||||
}
|
||||
//updateProperty
|
||||
interface UpdatePropertyRequest {
|
||||
token: string;
|
||||
name: string;
|
||||
prop: string;
|
||||
value: string;
|
||||
}
|
||||
//command
|
||||
interface CommandRequest {
|
||||
token: string;
|
||||
command: string;
|
||||
name: string;
|
||||
}
|
||||
|
||||
enum serviiRequest {
|
||||
setSubdomain = 'setSubdomain',
|
||||
fetchServers = 'fetchServers',
|
||||
accountCreate = 'accountCreate',
|
||||
serverCreate = 'serverCreate',
|
||||
serverDelete = 'serverDelete',
|
||||
accountDelete = 'accountDelete',
|
||||
serverRun = 'serverRun',
|
||||
serverStop = 'serverStop',
|
||||
updateProperty = 'updateProperty',
|
||||
command = 'command',
|
||||
}
|
||||
|
||||
class serviiApi {
|
||||
constructor() {}
|
||||
|
||||
private static async call<T extends BaseRequest>(endpoint: serviiRequest, body: T):Promise<ApiResponse>{
|
||||
const response = await fetch(`${apiUrl}/${endpoint}`, {
|
||||
credentials: "omit",
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
const status = response.status
|
||||
const json = await response.json();
|
||||
|
||||
if ('message' in json) {
|
||||
return {return_code: status, message: json.message};
|
||||
}
|
||||
return {return_code: status, message: "Couldn't find an available API, we're sorry for the inconvenience."};
|
||||
}
|
||||
|
||||
private static token(): string {
|
||||
const currentUser = getAuth().currentUser;
|
||||
if (!currentUser) {throw new Error('No user is currently logged in.');}
|
||||
return currentUser.uid;
|
||||
}
|
||||
|
||||
public static async setSubdomain(subdomain: string): Promise<ApiResponse> {
|
||||
const payload : SubdomainRequest = {token: this.token(), subdomain: subdomain}
|
||||
return this.call(serviiRequest.setSubdomain, payload);
|
||||
}
|
||||
public static async fetchServers(): Promise<ApiResponse> {
|
||||
const payload : BaseRequest = {token: this.token()}
|
||||
return this.call(serviiRequest.fetchServers, payload);
|
||||
}
|
||||
public static async accountCreate(): Promise<ApiResponse> {
|
||||
const payload : BaseRequest = {token: this.token()}
|
||||
return this.call(serviiRequest.accountCreate, payload);
|
||||
}
|
||||
public static async serverCreate(name: string, version: string, framework: string): Promise<ApiResponse> {
|
||||
const payload : ServerCreationRequest = {token: this.token(), name: name, version: version, framework: framework};
|
||||
return this.call(serviiRequest.setSubdomain, payload);
|
||||
}
|
||||
public static async serverDelete(name: string): Promise<ApiResponse> {
|
||||
const payload : ServerRequest = {token: this.token(), name: name}
|
||||
return this.call(serviiRequest.fetchServers, payload);
|
||||
}
|
||||
public static async accountDelete(): Promise<ApiResponse> {
|
||||
const payload : BaseRequest = {token: this.token()}
|
||||
return this.call(serviiRequest.accountCreate, payload);
|
||||
}
|
||||
public static async serverRun(name: string): Promise<ApiResponse> {
|
||||
const payload : ServerRequest = {token: this.token(), name: name}
|
||||
return this.call(serviiRequest.accountCreate, payload);
|
||||
}
|
||||
public static async serverStop(name: string): Promise<ApiResponse> {
|
||||
const payload : ServerRequest = {token: this.token(), name: name}
|
||||
return this.call(serviiRequest.setSubdomain, payload);
|
||||
}
|
||||
public static async updateProperty(name: string, prop: string, value: string): Promise<ApiResponse> {
|
||||
const payload : UpdatePropertyRequest = {token: this.token(), name: name, prop: prop, value: value}
|
||||
return this.call(serviiRequest.fetchServers, payload);
|
||||
}
|
||||
public static async command(command: string, name: string): Promise<ApiResponse> {
|
||||
const payload : CommandRequest = {token: this.token(), command: command, name: name};
|
||||
return this.call(serviiRequest.accountCreate, payload);
|
||||
}
|
||||
}
|
||||
|
||||
export default serviiApi;
|
@ -1,146 +0,0 @@
|
||||
// src/services/serverService.js
|
||||
import { getAuth } from 'firebase/auth';
|
||||
|
||||
const apiUrl = 'http://176.165.62.226:3000';
|
||||
|
||||
const fetchServers = async () => {
|
||||
const auth = getAuth();
|
||||
const currentUser = auth.currentUser;
|
||||
if (!currentUser) {
|
||||
throw new Error('No current user found');
|
||||
}
|
||||
const userId = currentUser.uid;
|
||||
|
||||
const response = await fetch(`${apiUrl}/FetchServers`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${userId}`,
|
||||
},
|
||||
body: JSON.stringify({ token: userId }),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text();
|
||||
throw new Error(`Network response was not ok: ${errorText}`);
|
||||
}
|
||||
|
||||
return await response.json();
|
||||
};
|
||||
|
||||
const createServer = async (serverName) => {
|
||||
const auth = getAuth();
|
||||
const currentUser = auth.currentUser;
|
||||
if (!currentUser) {
|
||||
throw new Error('No current user found');
|
||||
}
|
||||
const userId = currentUser.uid;
|
||||
|
||||
const response = await fetch(`${apiUrl}/CreateServer`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${userId}`,
|
||||
},
|
||||
body: JSON.stringify({ name: serverName, token: userId }),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text();
|
||||
throw new Error(`Failed to create server: ${errorText}`);
|
||||
}
|
||||
};
|
||||
|
||||
const runServer = async (serverName) => {
|
||||
const auth = getAuth();
|
||||
const currentUser = auth.currentUser;
|
||||
if (!currentUser) {
|
||||
throw new Error('No current user found');
|
||||
}
|
||||
const userId = currentUser.uid;
|
||||
|
||||
const response = await fetch(`${apiUrl}/ServerRun`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${userId}`,
|
||||
},
|
||||
body: JSON.stringify({ name: serverName, token: userId }),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text();
|
||||
throw new Error(`Failed to start server: ${errorText}`);
|
||||
}
|
||||
};
|
||||
|
||||
const stopServer = async (serverName) => {
|
||||
const auth = getAuth();
|
||||
const currentUser = auth.currentUser;
|
||||
if (!currentUser) {
|
||||
throw new Error('No current user found');
|
||||
}
|
||||
const userId = currentUser.uid;
|
||||
|
||||
const response = await fetch(`${apiUrl}/ServerStop`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${userId}`,
|
||||
},
|
||||
body: JSON.stringify({ name: serverName, token: userId }),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text();
|
||||
throw new Error(`Failed to stop server: ${errorText}`);
|
||||
}
|
||||
};
|
||||
|
||||
const deleteServer = async (serverName) => {
|
||||
const auth = getAuth();
|
||||
const currentUser = auth.currentUser;
|
||||
if (!currentUser) {
|
||||
throw new Error('No current user found');
|
||||
}
|
||||
const userId = currentUser.uid;
|
||||
|
||||
const response = await fetch(`${apiUrl}/ServerDelete`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${userId}`,
|
||||
},
|
||||
body: JSON.stringify({ name: serverName, token: userId }),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text();
|
||||
throw new Error(`Failed to delete server: ${errorText}`);
|
||||
}
|
||||
};
|
||||
|
||||
const saveSubdomain = async (subdomain) => {
|
||||
const auth = getAuth();
|
||||
const currentUser = auth.currentUser;
|
||||
if (!currentUser) {
|
||||
throw new Error('No current user found');
|
||||
}
|
||||
const userId = currentUser.uid;
|
||||
|
||||
const response = await fetch(`${apiUrl}/SetSubdomain`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${userId}`,
|
||||
},
|
||||
body: JSON.stringify({ subdomain, token: userId }),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text();
|
||||
throw new Error(`Failed to save subdomain: ${errorText}`);
|
||||
}
|
||||
};
|
||||
|
||||
export { fetchServers, createServer, runServer, stopServer, deleteServer, saveSubdomain };
|
Loading…
Reference in New Issue
Block a user