generated from angeloanan/personal-next-template
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement TrustPositif Checking & Refactor Index (#29)
* Implement TrustPositif checking * Moved websiteSections to own file in _data, move TrustPositif fetching to getStaticProps * Split TP check request to chunks of 100 websites at a time * Add TP indicator to registered but inaccessible websites * Added extra explanation for TrustPositive status * fix: await promise * fix: grammar error * feat: implement basic trycatching Co-authored-by: Christopher Angelo <angelo@angeloanan.xyz>
- Loading branch information
1 parent
cbec296
commit c10393f
Showing
8 changed files
with
181 additions
and
85 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { devStarterPack, idnStarterPack, linuxStarterPack, websiteListUSA } from './websites' | ||
|
||
export const websiteSections = [ | ||
{ | ||
title: 'Indonesia Starterpack', | ||
description: <p>Situs atau service ini terpopuler dengan orang Indonesia</p>, | ||
sites: idnStarterPack | ||
}, | ||
{ | ||
title: 'Essential Developer Toolkit', | ||
description: <p>Service yang ✨ anak bangsa ✨ mungkin akan pakai saat membuat app</p>, | ||
sites: devStarterPack | ||
}, | ||
{ | ||
title: 'Top websites USA', | ||
description: ( | ||
<> | ||
<p> | ||
2022 Top websites in the USA. Sumber data website diambil dari{' '} | ||
<a | ||
href='https://www.semrush.com/blog/most-visited-websites/' | ||
className='text-blue-700 underline' | ||
target='_blank' | ||
rel='noreferrer' | ||
> | ||
semrush.com | ||
</a> | ||
</p> | ||
<p> | ||
<em>Catatan: Ada beberapa website ditiadakan karena tidak berhubungan</em> | ||
</p> | ||
</> | ||
), | ||
sites: websiteListUSA | ||
}, | ||
{ | ||
title: 'Linux Starterpack', | ||
description: <p>Service yang sering dipakai Linux user 🐧</p>, | ||
sites: linuxStarterPack | ||
} | ||
] |
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,56 @@ | ||
import https from 'https' | ||
|
||
import { devStarterPack, idnStarterPack, linuxStarterPack, websiteListUSA } from '../_data/websites' | ||
|
||
const allWebsitesCombined = [ | ||
devStarterPack, | ||
idnStarterPack, | ||
linuxStarterPack, | ||
websiteListUSA | ||
].flat() | ||
|
||
const httpsAgent = new https.Agent({ | ||
rejectUnauthorized: false | ||
}) | ||
|
||
export const fetchTrustPositif = async () => { | ||
try { | ||
const requestChunks = Array.from( | ||
{ length: Math.ceil(allWebsitesCombined.length / 100) }, | ||
(_, i) => allWebsitesCombined.slice(i * 100, i * 100 + 100) | ||
) | ||
|
||
const requests: Promise<Record<string, boolean>>[] = [] | ||
|
||
requestChunks.forEach((chunk) => { | ||
requests.push( | ||
fetch('https://trustpositif.kominfo.go.id/Rest_server/getrecordsname_home', { | ||
method: 'POST', | ||
header: {}, | ||
body: new URLSearchParams({ | ||
name: chunk | ||
.map((item) => item.website.replace(/(https?:\/\/)/, '').replace(/www\./, '')) | ||
.join('\n') | ||
}), | ||
agent: httpsAgent | ||
} as any) | ||
.then((res) => res.json()) | ||
.then((res) => { | ||
let trustPositifStatus: Record<string, boolean> = {} | ||
res.values.forEach((item: any) => { | ||
trustPositifStatus[item.Domain] = item.Status === 'Ada' | ||
}) | ||
return trustPositifStatus | ||
}) | ||
) | ||
}) | ||
|
||
const trustPositifData = await Promise.all(requests) | ||
|
||
return Object.assign({}, ...trustPositifData) | ||
} catch (e) { | ||
console.error(e) | ||
|
||
return {} | ||
} | ||
} |
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,13 @@ | ||
import { NextApiHandler } from 'next' | ||
|
||
import { fetchTrustPositif } from '../../functions/fetchTrustPositif' | ||
|
||
const handler: NextApiHandler = async (req, res) => { | ||
const trustPositifData = await fetchTrustPositif() | ||
|
||
return res | ||
.setHeader('Cache-Control', 'public, s-maxage=60, stale-while-revalidate=3600') | ||
.json(trustPositifData) | ||
} | ||
|
||
export default handler |
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