Migrate to vercel-deno@3.0.0

This commit is contained in:
Sam Poder 2023-02-25 09:36:38 +08:00
parent 2cc43383d1
commit 509a0a007a
6 changed files with 60 additions and 75 deletions

View file

@ -1,4 +1,3 @@
import { ServerRequest } from "https://deno.land/std@0.75.0/http/server.ts";
import { urlParse } from "https://deno.land/x/url_parse/mod.ts";
const endpoint = (path: string) => {
@ -39,42 +38,42 @@ const deploy = async (
return { status: req.status, fileURLs };
};
export default async (req: ServerRequest) => {
export default async (req: Request) => {
if (req.method == "OPTIONS") {
return req.respond(
return new Response(
JSON.stringify(
{ status: "YIPPE YAY. YOU HAVE CLEARANCE TO PROCEED." },
),
{
status: 204,
body: JSON.stringify(
{ status: "YIPPE YAY. YOU HAVE CLEARANCE TO PROCEED." },
),
status: 204
},
);
}
if (req.method == "GET") {
return req.respond(
return new Response(
JSON.stringify(
{ error: "*GET outta here!* (Method not allowed, use POST)" },
),
{
status: 405,
body: JSON.stringify(
{ error: "*GET outta here!* (Method not allowed, use POST)" },
),
status: 405
},
);
}
if (req.method == "PUT") {
return req.respond(
return new Response(
JSON.stringify(
{ error: "*PUT that request away!* (Method not allowed, use POST)" },
),
{
status: 405,
body: JSON.stringify(
{ error: "*PUT that request away!* (Method not allowed, use POST)" },
),
},
);
}
if (req.method != "POST") {
return req.respond(
return new Response(
JSON.stringify({ error: "Method not allowed, use POST" }),
{
status: 405,
body: JSON.stringify({ error: "Method not allowed, use POST" }),
},
);
}
@ -83,8 +82,9 @@ export default async (req: ServerRequest) => {
const buf = await Deno.readAll(req.body);
const fileURLs = JSON.parse(decoder.decode(buf));
if (!Array.isArray(fileURLs) || fileURLs.length < 1) {
return req.respond(
{ status: 422, body: JSON.stringify({ error: "Empty file array" }) },
return return new Response(
JSON.stringify({ error: "Empty file array" }),
{ status: 422 }
);
}
@ -114,9 +114,9 @@ export default async (req: ServerRequest) => {
}));
const result = await deploy(uploadedURLs);
req.respond({
status: result.status,
body: JSON.stringify(result.fileURLs),
});
return return new Response(
JSON.stringify(result.fileURLs),
{ status: result.status }
);
};

View file

@ -1,4 +1,3 @@
import { ServerRequest } from "https://deno.land/std@0.75.0/http/server.ts";
import { Hash } from "https://deno.land/x/checksum@1.4.0/mod.ts";
const endpoint = (path: string) => {
@ -38,12 +37,12 @@ const uploadFile = async (url: string, authorization: string|null) => {
};
};
export default async (req: ServerRequest) => {
export default async (req: Request) => {
if (req.method != "POST") {
return req.respond(
return new Response(
JSON.stringify({ error: "Method not allowed, use POST" }),
{
status: 405,
body: JSON.stringify({ error: "Method not allowed, use POST" }),
},
);
}
@ -52,16 +51,14 @@ export default async (req: ServerRequest) => {
const buf = await Deno.readAll(req.body);
const singleFileURL = decoder.decode(buf);
if (typeof singleFileURL != "string") {
return req.respond(
return new Response(
JSON.stringify({ error: "newSingle only accepts a single URL" }),
{
status: 422,
body: JSON.stringify({ error: "newSingle only accepts a single URL" }),
status: 422
},
);
}
const uploadedFileURL = await uploadFile(singleFileURL, req.headers.get("Authorization"));
req.respond({
body: JSON.stringify(uploadedFileURL),
});
return new Response(JSON.stringify(uploadedFileURL))
};

View file

@ -1,21 +1,18 @@
import { ServerRequest } from "https://deno.land/std@0.75.0/http/server.ts";
import { urlParse } from "https://deno.land/x/url_parse/mod.ts";
import { uploadEndpoint } from "./upload.ts";
import { deployEndpoint } from "./deploy.ts";
import { ensurePost, parseBody } from "./utils.ts";
export default async (req: ServerRequest) => {
export default async (req: Request) => {
if (!ensurePost(req)) return null;
const body = await parseBody(req.body);
const fileURLs = JSON.parse(body);
if (!Array.isArray(fileURLs) || fileURLs.length < 1) {
return req.respond(
{
status: 422,
body: JSON.stringify({ error: "Empty/invalid file array" }),
},
return new Response(
JSON.stringify({ error: "Empty/invalid file array" }),
{ status: 422 }
);
}
@ -33,10 +30,8 @@ export default async (req: ServerRequest) => {
const deploymentData = await deployEndpoint(deploymentFiles);
req.respond(
{
body: JSON.stringify(deploymentData.files),
status: deploymentData.status,
},
return new Response(
JSON.stringify(deploymentData.files),
{ status: deploymentData.status }
);
};

View file

@ -1,4 +1,3 @@
import { ServerRequest } from "https://deno.land/std@0.75.0/http/server.ts";
import { Hash } from "https://deno.land/x/checksum@1.4.0/hash.ts";
import { endpoint, ensurePost, parseBody } from "./utils.ts";
@ -44,11 +43,11 @@ const upload = async (url: string, authorization: string | null) => {
};
};
export default async (req: ServerRequest) => {
export default async (req: Request) => {
if (!ensurePost(req)) return null;
const body = await parseBody(req.body);
const uploadedFileUrl = await upload(body, req.headers.get("Authorization"));
req.respond({ body: JSON.stringify(uploadedFileUrl) });
return new Response(JSON.stringify(uploadedFileUrl));
};

View file

@ -1,5 +1,3 @@
import { ServerRequest } from "https://deno.land/std@0.75.0/http/server.ts";
export const endpoint = (path: string) => {
// https://vercel.com/docs/api#api-basics/authentication/accessing-resources-owned-by-a-team
let url = "https://api.vercel.com/" + path;
@ -9,55 +7,51 @@ export const endpoint = (path: string) => {
return url;
};
export const parseBody = async (body: ServerRequest["body"]) => {
export const parseBody = async (body: Request["body"]) => {
const decoder = new TextDecoder();
const buf = await Deno.readAll(body);
const result = decoder.decode(buf);
return result;
};
export const ensurePost = (req: ServerRequest) => {
export const ensurePost = (req: Request) => {
if (req.method == "OPTIONS") {
req.respond(
return new Response(
JSON.stringify(
{ status: "YIPPE YAY. YOU HAVE CLEARANCE TO PROCEED." },
),
{
status: 204,
body: JSON.stringify(
{ status: "YIPPE YAY. YOU HAVE CLEARANCE TO PROCEED." },
),
status: 204
},
);
return false;
}
if (req.method == "GET") {
req.respond(
return new Response(
JSON.stringify(
{ error: "*GET outta here!* (Method not allowed, use POST)" },
),
{
status: 405,
body: JSON.stringify(
{ error: "*GET outta here!* (Method not allowed, use POST)" },
),
status: 405
},
);
return false;
}
if (req.method == "PUT") {
req.respond(
return new Response(
JSON.stringify(
{ error: "*PUT that request away!* (Method not allowed, use POST)" },
),
{
status: 405,
body: JSON.stringify(
{ error: "*PUT that request away!* (Method not allowed, use POST)" },
),
},
);
return false;
}
if (req.method != "POST") {
req.respond(
return new Response(
JSON.stringify({ error: "Method not allowed, use POST" }),
{
status: 405,
body: JSON.stringify({ error: "Method not allowed, use POST" }),
},
);
return false;
}
return true;
};

View file

@ -1,7 +1,7 @@
{
"version": 2,
"functions": {
"api/**/*.{j,t}s": { "runtime": "vercel-deno@0.4.4" }
"api/**/*.[jt]s": { "runtime": "vercel-deno@3.0.0" }
},
"redirects": [
{ "source": "/", "destination": "https://github.com/hackclub/cdn" },