refactor: use planetscale instead of custom api

This commit is contained in:
iCrawl
2023-11-07 15:08:03 +01:00
parent 344a3f9344
commit 009c0a3bae
10 changed files with 131 additions and 69 deletions

View File

@@ -1,16 +1,27 @@
import { connect } from '@planetscale/database';
import { get } from '@vercel/edge-config';
import { NextResponse, type NextRequest } from 'next/server';
import { PACKAGES } from './util/constants';
const sql = connect({
url: process.env.DATABASE_URL!,
async fetch(url, init) {
delete init?.cache;
return fetch(url, { ...init, next: { revalidate: 3_600 } });
},
});
async function fetchLatestVersion(packageName: string) {
if (process.env.NEXT_PUBLIC_LOCAL_DEV || process.env.NEXT_PUBLIC_VERCEL_ENV === 'preview') {
return 'main';
}
const res = await fetch(`https://docs.discordjs.dev/api/info?package=${packageName}`, { cache: 'no-store' });
const data: string[] = await res.json();
const { rows } = await sql.execute('select version from documentation where name = ? order by version desc', [
packageName,
]);
return data.at(-2);
// @ts-expect-error: https://github.com/planetscale/database-js/issues/71
return rows[0].data.at(1);
}
export default async function middleware(request: NextRequest) {