-
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
Showing
15 changed files
with
91 additions
and
65 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
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
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 |
---|---|---|
@@ -1,15 +1,17 @@ | ||
import type { WebInfoData } from '../types' | ||
|
||
export type OGInfoFun = () => Promise< WebInfoData> | ||
export type OGInfoFun = (url: string) => Promise< WebInfoData> | ||
|
||
export class OGInfo { | ||
constructor(fun: OGInfoFun) { | ||
this.fun = fun | ||
constructor(fields: { fun: OGInfoFun; url: string }) { | ||
this.fun = fields.fun | ||
this.url = fields.url | ||
} | ||
|
||
private fun | ||
private url | ||
|
||
async call() { | ||
return await this.fun() | ||
return await this.fun(this.url) | ||
} | ||
} |
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
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 |
---|---|---|
@@ -1,50 +1,20 @@ | ||
import type { WebInfoData } from '@starnexus/core' | ||
import { OGInfo, WebInfo, getDomain } from '@starnexus/core' | ||
import { OGInfo, WebInfo, errorMessage } from '@starnexus/core' | ||
import { routes } from '@starnexus/web-hub' | ||
import { unfurl } from 'unfurl.js' | ||
import { ogInfoFun } from '../utils' | ||
|
||
export default eventHandler(async (event) => { | ||
const req = await readBody<{ webUrl: string }>(event) | ||
const webUrl = req.webUrl | ||
const ogInfoFun = async () => { | ||
const res = await unfurl(webUrl) | ||
let content = res.description || '' | ||
let title = res.title || '' | ||
let url = res.canonical_url || webUrl | ||
let website = '' | ||
const domain = getDomain(url) | ||
if (res.open_graph) { | ||
const og = res.open_graph | ||
title = og.title | ||
content = og.description || content | ||
url = og.url || url | ||
website = og.site_name || domain.split('.')[0].toUpperCase() | ||
} | ||
|
||
if (!title || !content) | ||
throw new Error('Not Supported Website. No OG info') | ||
|
||
return { | ||
title, | ||
content, | ||
url, | ||
meta: { | ||
domain, | ||
website, | ||
}, | ||
} as WebInfoData | ||
} | ||
const ogInfo = new OGInfo(ogInfoFun) | ||
const ogInfo = new OGInfo({ fun: ogInfoFun, url: webUrl }) | ||
try { | ||
const webInfo = new WebInfo({ urls: { webUrl }, routes, ogInfo }) | ||
const webData = await webInfo.call() | ||
return webData | ||
} | ||
catch (error: any) { | ||
setResponseStatus(event, 400) | ||
let message = error.message || '' | ||
if (error.data) | ||
message = JSON.stringify(error.data) | ||
const message = errorMessage(error) | ||
return { error: message } | ||
} | ||
}) |
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,33 @@ | ||
import { unfurl } from 'unfurl.js' | ||
import type { WebInfoData } from '@starnexus/core' | ||
import { getDomain } from '@starnexus/core' | ||
|
||
export async function ogInfoFun(webUrl: string) { | ||
const res = await unfurl(webUrl) | ||
let content = res.description || '' | ||
let title = res.title || '' | ||
let url = res.canonical_url || webUrl | ||
const domain = getDomain(url) | ||
const word = domain.split('.')[0] | ||
let siteName = word.charAt(0).toUpperCase() + word.slice(1) | ||
if (res.open_graph) { | ||
const og = res.open_graph | ||
title = og.title | ||
content = og.description || content | ||
url = og.url || url | ||
siteName = og.site_name || siteName | ||
} | ||
|
||
if (!title || !content) | ||
throw new Error('Not Supported Website. No OG info') | ||
|
||
return { | ||
title, | ||
content, | ||
url, | ||
meta: { | ||
domain, | ||
siteName, | ||
}, | ||
} as WebInfoData | ||
} |
a74a0e9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
star-nexus – ./
star-nexus.vercel.app
star-nexus-larchliu.vercel.app
star-nexus-git-main-larchliu.vercel.app