-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d15164d
commit 3ef8e18
Showing
10 changed files
with
369 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { unfurl } from "unfurl.js"; | ||
import { NextRequest, NextResponse } from "next/server"; | ||
|
||
type ErrorResponse = { error: string }; | ||
type SuccessResponse = { | ||
title?: string | null; | ||
description?: string | null; | ||
favicon?: string | null; | ||
imageSrc?: string | null; | ||
}; | ||
|
||
const CACHE_RESULT_SECONDS = 60 * 60 * 24; // 1 day | ||
|
||
export async function GET( | ||
req: NextRequest, | ||
res: NextResponse<SuccessResponse | ErrorResponse> | ||
) { | ||
const searchParams = req.nextUrl.searchParams; | ||
const url = searchParams.get("url"); | ||
|
||
console.log('query url', url); | ||
|
||
if (!url || typeof url !== "string") { | ||
return NextResponse.json( | ||
{ | ||
error: "Please enter title", | ||
}, | ||
{ | ||
status: 400, | ||
} | ||
); | ||
} | ||
|
||
return unfurl(url) | ||
.then((unfurlResponse) => { | ||
|
||
console.log(unfurlResponse) | ||
|
||
const response = { | ||
title: unfurlResponse.title ?? null, | ||
description: unfurlResponse.description ?? null, | ||
favicon: unfurlResponse.favicon ?? null, | ||
imageSrc: unfurlResponse.open_graph?.images?.[0]?.url ?? null, | ||
}; | ||
|
||
const res = NextResponse.json(response); | ||
res.headers.set( | ||
"Cache-Control", | ||
`public, max-age=${CACHE_RESULT_SECONDS}` | ||
); | ||
return res; | ||
}) | ||
.catch((error) => { | ||
console.error(error); | ||
return NextResponse.json( | ||
{ | ||
error: "Internal server error", | ||
}, | ||
{ | ||
status: 500, | ||
} | ||
); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { useEffect, useState } from "react"; | ||
|
||
type RequestStatus = "iddle" | "loading" | "success" | "error"; | ||
|
||
export type UrlData = { | ||
title: string | null; | ||
description: string | null; | ||
favicon: string | null; | ||
imageSrc: string | null; | ||
}; | ||
|
||
export function useUnfurlUrl(url: string) { | ||
const [status, setStatus] = useState<RequestStatus>("iddle"); | ||
const [data, setData] = useState<null | UrlData>(null); | ||
useEffect(() => { | ||
if (url) { | ||
setStatus("loading"); | ||
const encoded = encodeURIComponent(url); | ||
fetch(`/api/unfurl?url=${encoded}`) | ||
.then(async (res) => { | ||
if (res.ok) { | ||
const data = await res.json(); | ||
setData(data); | ||
setStatus("success"); | ||
} else { | ||
setStatus("error"); | ||
} | ||
}) | ||
.catch((error) => { | ||
console.error(error); | ||
|
||
setStatus("error"); | ||
}); | ||
} else { | ||
setStatus("error"); | ||
} | ||
}, [url]); | ||
|
||
return { status, data }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.