From 9414fdbc6980727e0e8f561aa3eec5ca20528972 Mon Sep 17 00:00:00 2001 From: ccrsxx Date: Wed, 8 Nov 2023 23:00:15 +0700 Subject: [PATCH] feat: separate module concern between client and server --- src/app/(public)/s/[slug]/page.tsx | 2 +- src/app/actions.ts | 7 ++----- src/lib/helper-server.ts | 22 ++++++++++++++++++++++ src/lib/helper.ts | 21 +-------------------- 4 files changed, 26 insertions(+), 26 deletions(-) create mode 100644 src/lib/helper-server.ts diff --git a/src/app/(public)/s/[slug]/page.tsx b/src/app/(public)/s/[slug]/page.tsx index 5a5470d..ef2b410 100644 --- a/src/app/(public)/s/[slug]/page.tsx +++ b/src/app/(public)/s/[slug]/page.tsx @@ -1,6 +1,6 @@ import Link from 'next/link'; import { redirect } from 'next/navigation'; -import { checkSlugExists } from '@/lib/helper'; +import { checkSlugExists } from '@/lib/helper-server'; import { NEXT_PUBLIC_URL } from '@/lib/env'; import { CopyButton } from '@/components/ui/copy-button'; import { Button } from '@/components/ui/button'; diff --git a/src/app/actions.ts b/src/app/actions.ts index b615c2f..c18d914 100644 --- a/src/app/actions.ts +++ b/src/app/actions.ts @@ -3,11 +3,8 @@ import { redirect } from 'next/navigation'; import { ZodError } from 'zod'; import { prisma } from '@/lib/db'; -import { - checkIfUrlIsValid, - checkSlugExists, - generateRandomSlug -} from '@/lib/helper'; +import { checkIfUrlIsValid } from '@/lib/helper'; +import { checkSlugExists, generateRandomSlug } from '@/lib/helper-server'; import { linkSchema } from './schema'; export async function createLink( diff --git a/src/lib/helper-server.ts b/src/lib/helper-server.ts new file mode 100644 index 0000000..2b90101 --- /dev/null +++ b/src/lib/helper-server.ts @@ -0,0 +1,22 @@ +import { randomBytes } from 'crypto'; +import { prisma } from './db'; + +/** + * Returns true if slug exists in database. + */ +export async function checkSlugExists(slug: string): Promise { + const link = await prisma.link.findUnique({ + where: { + slug + } + }); + + return !!link; +} + +/** + * Returns a random string for slug. + */ +export function generateRandomSlug(): string { + return randomBytes(3).toString('hex'); +} diff --git a/src/lib/helper.ts b/src/lib/helper.ts index 157f929..c6838c6 100644 --- a/src/lib/helper.ts +++ b/src/lib/helper.ts @@ -1,27 +1,8 @@ -import { randomBytes } from 'crypto'; -import { prisma } from './db'; import { URL_WITHOUT_PROTOCOL } from './env'; /** - * Returns a random string for slug. + * Returns true if the url is not from the same domain. */ -export function generateRandomSlug(): string { - return randomBytes(3).toString('hex'); -} - -/** - * Returns true if slug exists in database. - */ -export async function checkSlugExists(slug: string): Promise { - const link = await prisma.link.findUnique({ - where: { - slug - } - }); - - return !!link; -} - export function checkIfUrlIsValid(url: string): boolean { return !url.includes(URL_WITHOUT_PROTOCOL); }