aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/docs/pages/api/binaries/version.ts
diff options
context:
space:
mode:
author简律纯 <i@jyunko.cn>2023-11-03 21:13:33 +0800
committer简律纯 <i@jyunko.cn>2023-11-03 21:13:33 +0800
commit9f0d43fe099a95ab1516ae951dcb60a89e76a5a5 (patch)
tree51614fe47bff8bb11028a07d4a35c34c9ff6594a /docs/pages/api/binaries/version.ts
parent8f135707d069c900e055dae71e69909d6b9a41bb (diff)
downloadHydroRoll-9f0d43fe099a95ab1516ae951dcb60a89e76a5a5.tar.gz
HydroRoll-9f0d43fe099a95ab1516ae951dcb60a89e76a5a5.zip
chore: delete useless codes
Diffstat (limited to 'docs/pages/api/binaries/version.ts')
-rw-r--r--docs/pages/api/binaries/version.ts113
1 files changed, 0 insertions, 113 deletions
diff --git a/docs/pages/api/binaries/version.ts b/docs/pages/api/binaries/version.ts
deleted file mode 100644
index 339a34d..0000000
--- a/docs/pages/api/binaries/version.ts
+++ /dev/null
@@ -1,113 +0,0 @@
-import type { NextRequest } from "next/server";
-
-const REGISTRY = "https://registry.npmjs.org";
-const DEFAULT_TAG = "latest";
-const SUPPORTED_PACKAGES = ["turbo"];
-const SUPPORTED_METHODS = ["GET"];
-const [DEFAULT_NAME] = SUPPORTED_PACKAGES;
-
-async function fetchDistTags({ name }: { name: string }) {
- const result = await fetch(`${REGISTRY}/${name}`);
- const json = await result.json();
- return json["dist-tags"];
-}
-
-function errorResponse({
- status,
- message,
-}: {
- status: 400 | 404 | 500;
- message: string;
-}) {
- return new Response(
- JSON.stringify({
- error: message,
- }),
- {
- status,
- }
- );
-}
-
-/*
-This API is called via the turbo rust binary to check for version updates.
-
-Response Schema (200):
-{
- "type": "object",
- "properties": {
- "name": {
- "type": "string",
- },
- "version": {
- "type": "string",
- },
- "tag": {
- "type": "string",
- }
- }
-}
-
-Errors (400 | 404 | 500):
-{
- "type": "object",
- "properties": {
- "error": {
- "type": "string",
- }
- }
-}
-
-*/
-export default async function handler(req: NextRequest) {
- if (!SUPPORTED_METHODS.includes(req.method)) {
- return errorResponse({
- status: 404,
- message: `unsupported method - ${req.method}`,
- });
- }
-
- try {
- const { searchParams } = new URL(req.url);
- const name = searchParams.get("name") || DEFAULT_NAME;
- const tag = searchParams.get("tag") || DEFAULT_TAG;
-
- if (!SUPPORTED_PACKAGES.includes(name)) {
- return errorResponse({
- status: 400,
- message: `unsupported package - ${name}`,
- });
- }
-
- const versions = await fetchDistTags({ name });
- if (!versions || !versions[tag]) {
- return errorResponse({
- status: 404,
- message: `unsupported tag - ${tag}`,
- });
- }
-
- return new Response(
- JSON.stringify({
- name,
- version: versions[tag],
- tag,
- }),
- {
- status: 200,
- headers: {
- "content-type": "application/json",
- // cache for 15 minutes, and allow stale responses for 5 minutes
- "cache-control": "public, s-maxage=900, stale-while-revalidate=300",
- },
- }
- );
- } catch (e) {
- console.error(e);
- return errorResponse({ status: 500, message: e.message });
- }
-}
-
-export const config = {
- runtime: "experimental-edge",
-};