chore: migrate signup API to appDir (#19975)
* refactor calcomHandler * refactor selfHostedHandler * fix * migrate to appdir * fix type checks
This commit is contained in:
1 parent
109c303b60
commit
1277f5e9f7
4 files changed
+89
-97
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
import type { NextResponse } from "next/server";
|
||||
|
||||
import slugify from "@calcom/lib/slugify";
|
||||
import prisma from "@calcom/prisma";
|
||||
@@ -11,30 +11,28 @@ import notEmpty from "../notEmpty";
|
||||
const log = logger.getSubLogger({ prefix: ["server/username"] });
|
||||
const cachedData: Set<string> = new Set();
|
||||
|
||||
export type RequestWithUsernameStatus = NextApiRequest & {
|
||||
usernameStatus: {
|
||||
/**
|
||||
* ```text
|
||||
* 200: Username is available
|
||||
* 402: Pro username, must be purchased
|
||||
* 418: A user exists with that username
|
||||
* ```
|
||||
*/
|
||||
statusCode: 200 | 402 | 418;
|
||||
requestedUserName: string;
|
||||
json: {
|
||||
available: boolean;
|
||||
premium: boolean;
|
||||
message?: string;
|
||||
suggestion?: string;
|
||||
};
|
||||
type UsernameStatus = {
|
||||
/**
|
||||
* ```text
|
||||
* 200: Username is available
|
||||
* 402: Pro username, must be purchased
|
||||
* 418: A user exists with that username
|
||||
* ```
|
||||
*/
|
||||
statusCode: 200 | 402 | 418;
|
||||
requestedUserName: string;
|
||||
json: {
|
||||
available: boolean;
|
||||
premium: boolean;
|
||||
message?: string;
|
||||
suggestion?: string;
|
||||
};
|
||||
};
|
||||
|
||||
type CustomNextApiHandler<T = unknown> = (
|
||||
req: RequestWithUsernameStatus,
|
||||
res: NextApiResponse<T>
|
||||
) => void | Promise<void>;
|
||||
export type CustomNextApiHandler = (
|
||||
body: Record<string, string>,
|
||||
usernameStatus: UsernameStatus
|
||||
) => Promise<NextResponse<any>>;
|
||||
|
||||
export async function isBlacklisted(username: string) {
|
||||
// NodeJS forEach is very, very fast (these days) so even though we only have to construct the Set
|
||||
@@ -66,7 +64,7 @@ export const generateUsernameSuggestion = async (users: string[], username: stri
|
||||
const processResult = (
|
||||
result: "ok" | "username_exists" | "is_premium"
|
||||
): // explicitly assign return value to ensure statusCode is typehinted
|
||||
{ statusCode: RequestWithUsernameStatus["usernameStatus"]["statusCode"]; message: string } => {
|
||||
{ statusCode: UsernameStatus["statusCode"]; message: string } => {
|
||||
// using a switch statement instead of multiple ifs to make sure typescript knows
|
||||
// there is only limited options
|
||||
switch (result) {
|
||||
@@ -85,29 +83,27 @@ const processResult = (
|
||||
}
|
||||
};
|
||||
|
||||
const usernameHandler =
|
||||
(handler: CustomNextApiHandler) =>
|
||||
async (req: RequestWithUsernameStatus, res: NextApiResponse): Promise<void> => {
|
||||
const username = slugify(req.body.username);
|
||||
const check = await usernameCheckForSignup({ username, email: req.body.email });
|
||||
const usernameHandler = (handler: CustomNextApiHandler) => async (body: Record<string, string>) => {
|
||||
const username = slugify(body.username);
|
||||
const check = await usernameCheckForSignup({ username, email: body.email });
|
||||
|
||||
let result: Parameters<typeof processResult>[0] = "ok";
|
||||
if (check.premium) result = "is_premium";
|
||||
if (!check.available) result = "username_exists";
|
||||
let result: Parameters<typeof processResult>[0] = "ok";
|
||||
if (check.premium) result = "is_premium";
|
||||
if (!check.available) result = "username_exists";
|
||||
|
||||
const { statusCode, message } = processResult(result);
|
||||
req.usernameStatus = {
|
||||
statusCode,
|
||||
requestedUserName: username,
|
||||
json: {
|
||||
available: result !== "username_exists",
|
||||
premium: result === "is_premium",
|
||||
message,
|
||||
suggestion: check.suggestedUsername,
|
||||
},
|
||||
};
|
||||
return handler(req, res);
|
||||
const { statusCode, message } = processResult(result);
|
||||
const usernameStatus = {
|
||||
statusCode,
|
||||
requestedUserName: username,
|
||||
json: {
|
||||
available: result !== "username_exists",
|
||||
premium: result === "is_premium",
|
||||
message,
|
||||
suggestion: check.suggestedUsername,
|
||||
},
|
||||
};
|
||||
return handler(body, usernameStatus);
|
||||
};
|
||||
|
||||
const usernameCheck = async (usernameRaw: string, currentOrgDomain?: string | null) => {
|
||||
log.debug("usernameCheck", { usernameRaw, currentOrgDomain });
|
||||
|
||||
Reference in new issue
Block a user