summary refs log tree commit diff
path: root/src/api/routes/unique-username/username-suggestions-unauthed.ts
blob: 2ce09285b9142de694e0a96eb3b0d7c2d0883b4d (plain) (blame)
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
import { route } from "@spacebar/api";
import { Config } from "@spacebar/util";
import { Request, Response, Router } from "express";
import { HTTPError } from "lambert-server";
const router = Router();

router.get(
	"/",
	route({
		description:
			"Returns a suggested unique username string based on the current user's username.",
		query: {
			global_name: {
				type: "string",
				required: false,
			},
		},
		responses: {
			400: { body: "APIErrorResponse" },
		},
	}),
	async (req: Request, res: Response) => {
		const globalName = req.query.globalName as string | undefined;
		const { uniqueUsernames } = Config.get().general;
		if (!uniqueUsernames) {
			throw new HTTPError(
				"Unique Usernames feature is not enabled on this instance.",
				400,
			);
		}

		// return a random suggestion
		if (!globalName) return res.json({ username: "" });
		// return a suggestion based on the globalName
		return res.json({ username: globalName });
	},
);

export default router;