All files / routes/pro/onboarding slug.routes.ts

100% Statements 26/26
100% Branches 12/12
100% Functions 2/2
100% Lines 25/25

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141                                              1x                                                                               2x 2x       2x 6x 6x     2x     1x     1x       1x         8x 8x 8x 8x     8x 1x               7x 1x             6x 2x               4x 3x 1x             2x         1x            
// Onboarding Slug Validation Routes
import { zValidator } from "@hono/zod-validator";
import { Hono } from "hono";
import { z } from "zod";
import type { Dal } from "../../../dal";
import { handleError, success } from "../../../lib/response";
import { generateSlug, randomBase36Suffix } from "../../../lib/utils";
import { requireProAccess } from "../../../middleware";
import type { Services } from "../../../services";
 
type Env = {
	Bindings: CloudflareBindings;
	Variables: {
		user: { id: string; name: string; email: string } | null;
		session: unknown;
		dal: Dal;
		services: Services;
		proId: string;
		proRole: string;
	};
};
 
// Reserved slugs that cannot be used
const RESERVED_SLUGS = new Set([
	"admin",
	"api",
	"app",
	"www",
	"mail",
	"blog",
	"help",
	"support",
	"docs",
	"status",
	"marketplace",
	"portal",
	"vendor",
	"vendors",
	"pro",
	"pros",
	"home",
	"about",
	"contact",
	"login",
	"register",
	"signup",
	"signin",
	"settings",
	"profile",
	"dashboard",
	"new",
	"create",
	"edit",
	"delete",
	"search",
	"explore",
	"preview",
	"staging",
	"test",
]);
 
// Generate slug suggestions from a base slug
function generateSlugSuggestions(baseSlug: string): string[] {
	const clean = generateSlug(baseSlug);
	const suggestions: string[] = [];
 
	// Add crypto-random base36 suffixes (widened from 3 to 6 chars for
	// collision resistance; non-cryptographic Math.random() was unsafe at scale).
	for (let i = 0; i < 3; i++) {
		const suffix = randomBase36Suffix(6);
		suggestions.push(`${clean}-${suffix}`);
	}
 
	return suggestions;
}
 
const slugRouter = new Hono<Env>();
 
// Check slug availability
const checkSlugSchema = z.object({
	slug: z.string().min(1),
});
 
slugRouter.get(
	"/slug/check",
	requireProAccess,
	zValidator("query", checkSlugSchema),
	async (c) => {
		try {
			const dal = c.get("dal");
			const proId = c.get("proId");
			const { slug } = c.req.valid("query");
 
			// Check reserved slugs
			if (RESERVED_SLUGS.has(slug.toLowerCase())) {
				return success(c, {
					available: false,
					reason: "This slug is reserved",
					suggestions: generateSlugSuggestions(slug),
				});
			}
 
			// Validate slug format
			if (!/^[a-z0-9-]+$/.test(slug)) {
				return success(c, {
					available: false,
					reason: "Slug must be lowercase alphanumeric with hyphens only",
					suggestions: [generateSlug(slug)],
				});
			}
 
			if (slug.startsWith("-") || slug.endsWith("-")) {
				return success(c, {
					available: false,
					reason: "Slug cannot start or end with a hyphen",
					suggestions: [slug.replace(/^-+|-+$/g, "")],
				});
			}
 
			// Check if slug is taken
			const existingPro = await dal.pros.findBySlug(slug);
			if (existingPro && existingPro.id !== proId) {
				return success(c, {
					available: false,
					reason: "This slug is already taken",
					suggestions: generateSlugSuggestions(slug),
				});
			}
 
			return success(c, {
				available: true,
				slug,
			});
		} catch (err) {
			return handleError(c, err);
		}
	},
);
 
export default slugRouter;