Skip to content

Commit

Permalink
feat: add increment link count
Browse files Browse the repository at this point in the history
  • Loading branch information
theodorusclarence committed Jan 18, 2022
1 parent db09505 commit 46ba0c7
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 7 deletions.
39 changes: 35 additions & 4 deletions src/lib/notion.ts
Expand Up @@ -8,6 +8,13 @@ const NOTION_INTEGRATION_SECRET =

const notion = new Client({ auth: NOTION_INTEGRATION_SECRET });

export type Url = {
pageId: string;
slug: string;
link?: string;
count: number;
};

/**
* Get long URL by slug
*/
Expand All @@ -25,11 +32,35 @@ export const getUrlBySlug = async (slug: string) => {
});

const results = response.results[0] as unknown as LinkResult;
const url = {
slug: results?.properties.slug.title[0].plain_text,
link: results?.properties.link.rich_text[0].plain_text,
count: Number(results?.properties.count.rich_text[0].plain_text),

const url: Url = {
pageId: results?.id,
slug: results?.properties.slug.title[0]?.plain_text,
link: results?.properties.link.rich_text[0]?.plain_text,
count: Number(results?.properties.count.rich_text[0]?.plain_text ?? 0),
};

return url;
};

/**
* Increment count column by 1
*/
export const incrementLinkCount = async (url: Url) => {
if (!NOTION_LINK_DATABASE_ID) {
throw new Error('NEXT_PUBLIC_NOTION_LINK_DATABASE_ID env is not defined');
}

if (!url.pageId) {
throw new Error('URL data is not found');
}

await notion.pages.update({
page_id: url.pageId,
properties: {
count: {
rich_text: [{ text: { content: String(url.count + 1) } }],
},
},
});
};
5 changes: 3 additions & 2 deletions src/pages/_middleware.ts
@@ -1,18 +1,19 @@
import { NextRequest, NextResponse } from 'next/server';

import { getUrlBySlug } from '@/lib/notion';
import { getUrlBySlug, incrementLinkCount } from '@/lib/notion';

export default async function middleware(req: NextRequest) {
const path = req.nextUrl.pathname.split('/')[1];
const whitelist = ['favicons', 'fonts', 'images', 'svg', ''];

const whitelist = ['favicons', 'fonts', 'images', 'svg', '', 'testing'];
if (whitelist.includes(path)) {
return;
}

const url = await getUrlBySlug(path);

if (url.link) {
await incrementLinkCount(url);
return NextResponse.redirect(url.link);
}
}
34 changes: 34 additions & 0 deletions src/pages/testing.tsx
@@ -0,0 +1,34 @@
import { InferGetStaticPropsType } from 'next';
import * as React from 'react';

import { getUrlBySlug, incrementLinkCount } from '@/lib/notion';

import Layout from '@/components/layout/Layout';
import Seo from '@/components/Seo';

export default function TestingPage({
link,
}: InferGetStaticPropsType<typeof getStaticProps>) {
return (
<Layout>
<Seo templateTitle='Testing' />

<main>
<section className=''>
<div className='layout py-20 min-h-screen'>
<pre>{JSON.stringify(link, null, 2)}</pre>
</div>
</section>
</main>
</Layout>
);
}

export const getStaticProps = async () => {
const link = await getUrlBySlug('clarence');
await incrementLinkCount(link);

return {
props: { link },
};
};
2 changes: 1 addition & 1 deletion src/types/notion.ts
Expand Up @@ -18,7 +18,7 @@ interface TitleColumn {
interface TextColumn {
id: string;
type: string;
rich_text: [RichText];
rich_text: [RichText | undefined];
}

interface RichText {
Expand Down

0 comments on commit 46ba0c7

Please sign in to comment.