Skip to content

Commit

Permalink
feat: separate module concern between client and server
Browse files Browse the repository at this point in the history
  • Loading branch information
ccrsxx committed Nov 8, 2023
1 parent f47d6b4 commit 9414fdb
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/app/(public)/s/[slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
7 changes: 2 additions & 5 deletions src/app/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
22 changes: 22 additions & 0 deletions src/lib/helper-server.ts
Original file line number Diff line number Diff line change
@@ -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<boolean> {
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');
}
21 changes: 1 addition & 20 deletions src/lib/helper.ts
Original file line number Diff line number Diff line change
@@ -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<boolean> {
const link = await prisma.link.findUnique({
where: {
slug
}
});

return !!link;
}

export function checkIfUrlIsValid(url: string): boolean {
return !url.includes(URL_WITHOUT_PROTOCOL);
}

0 comments on commit 9414fdb

Please sign in to comment.