v0.1 alpha

This commit is contained in:
2025-03-05 21:51:05 +01:00
parent f315c97048
commit 553db4c8a6
7 changed files with 1345 additions and 0 deletions
+1109
View File
File diff suppressed because it is too large Load Diff
+29
View File
@@ -0,0 +1,29 @@
{
"name": "auraclient-api",
"version": "1.0.0",
"description": "The api for AuraClient which is a Minecraft mod Client",
"main": "dist/index.js",
"scripts": {
"start": "tsc && node dist/index.js",
"test": "tsc && node test.mjs"
},
"repository": {
"type": "git",
"url": "git+https://github.com/kleo-dev/auraclient-api.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/kleo-dev/auraclient-api/issues"
},
"homepage": "https://github.com/kleo-dev/auraclient-api#readme",
"dependencies": {
"@types/express": "^5.0.0",
"@types/mongodb": "^4.0.6",
"@types/node": "^22.13.5",
"dotenv": "^16.4.7",
"express": "^4.21.2",
"mongodb": "^6.13.1"
}
}
+10
View File
@@ -0,0 +1,10 @@
import { MongoClient } from "mongodb";
import { Cloak, Player, mongoUrl } from "./utils";
export const validCloaks: Cloak[] = ["glitch"];
const mongoClient = new MongoClient(mongoUrl);
const db = mongoClient.db("saturnclient");
export const players = db.collection<Player>("players");
+165
View File
@@ -0,0 +1,165 @@
import express from "express";
import { players, validCloaks } from "./database";
import { Cloak } from "./utils";
async function getPlayerData(token: string) {
try {
const response = await fetch(
`https://api.minecraftservices.com/minecraft/profile`,
{
headers: {
Authorization: "Bearer " + token,
},
}
);
if (!response.ok) {
throw new Error("Invalid session");
}
const { id, name } = await response.json();
return { uuid: id, name };
} catch (error) {
throw new Error("Failed to validate session");
}
}
const port = 3000;
const app = express();
app.use(express.json());
app.get("/", (_, res) => {
res.send({
version: "0.0.1-alpha.0",
});
});
app.get("/player/:uuid", async (req, res) => {
const player = await players.findOne({
uuid: req.params.uuid.replaceAll("-", ""),
});
if (!player) {
res.send({
saturn: false,
});
return;
}
const response = await fetch(
`https://api.minecraftservices.com/minecraft/profile/lookup/${player.uuid}`
);
const { name } = await response.json();
res.send({
saturn: true,
cloak: player.cloak,
name,
});
});
app.get("/auth", async (req, res) => {
const { token } = req.query;
try {
const { uuid, name } = await getPlayerData(token as string);
const player = await players.findOne({ uuid });
if (!player) {
await players.insertOne({
uuid,
cloak: "",
cloaks: [],
});
res.send({
success: true,
uuid,
cloak: "",
cloaks: [],
});
return;
}
res.send({
success: true,
uuid,
name,
cloak: player.cloak,
cloaks: player.cloaks,
});
} catch (error) {
res.status(500).send({
success: false,
error,
});
}
});
app.post("/cloak/:cloak", async (req, res) => {
const { cloak } = req.params;
const { token } = req.query;
const { uuid } = await getPlayerData(token as string);
const player = await players.findOne({ uuid });
if (!player) {
res.status(400).send({
success: false,
error: "Player not found",
});
}
if (!validCloaks.includes(cloak as Cloak)) {
res.status(400).send({
success: false,
error: "Invalid cloak",
});
return;
}
if (!player.cloaks.includes(cloak as Cloak)) {
res.status(400).send({
success: false,
error: "You don't have this cloak",
});
return;
}
await players.updateOne({ uuid }, { $set: { cloak: cloak as Cloak } });
console.log("Cloak set to " + cloak + " for " + uuid);
res.send({ success: true });
});
app.post("/cloak/", async (req, res) => {
const { token } = req.query;
const { uuid } = await getPlayerData(token as string);
const player = await players.findOne({ uuid });
if (!player) {
res.status(400).send({
success: false,
error: "Player not found",
});
}
await players.updateOne({ uuid }, { $set: { cloak: "" } });
console.log("Cloak set to none for " + uuid);
res.send({ success: true });
});
app.listen(3000, () => {
console.log(`listening as http://localhost:${port}/`);
});
+16
View File
@@ -0,0 +1,16 @@
import { config } from "dotenv";
config();
function notFound(): string {
throw "'MONGO_URL' not found in .env";
}
export const mongoUrl = process.env.MONGO_URL || notFound();
export type Cloak = "glitch" | "forrest" | "";
export interface Player {
uuid: string;
cloak: Cloak;
cloaks: Cloak[];
}
+3
View File
@@ -0,0 +1,3 @@
await fetch("http://localhost:3000/player/d362a042-28bd-49e1-a807-ae74dbe8aba9", {
method: "GET",
});
+13
View File
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"module": "NodeNext",
"esModuleInterop": true,
"noImplicitAny": true,
"removeComments": true,
"preserveConstEnums": true,
"sourceMap": true,
"outDir": "dist/"
},
"include": ["src/**/*", "test.ts"],
"exclude": ["**/*.spec.ts"]
}