clone

push a directory of html from the terminal and get a four-character url back. a deno cli on cloudflare

feat/site-feedback
nzip / worker / src / api.ts
api.ts 3.8K
1import type { Env } from "./env.ts";2import { json } from "./env.ts";3import { ApiError, apiErrorResponse } from "./api/errors.ts";4import { handleBlobPut, handleCommit, handlePrepare } from "./api/push.ts";5import { handleAppInit } from "./api/apps.ts";6import { handleSite, handleSiteList } from "./api/sites.ts";7import { handleStatus } from "./api/status.ts";8import { handleDefaultVault, handleVault, handleVaults } from "./api/vaults.ts";9import { sendAlertTest } from "./security_alerts.ts";10import { handleNotifySend } from "./notify.ts";11import {12 approveEnrollment,13 listNotificationDevices,14 notifyErrorResponse,15 NotifyHttpError,16 openPairingWindow,17 previewApproval,18 revokeNotificationDevice,19} from "./notify_enrollment.ts";2021/** Authenticated management API router. Route handlers own validation and storage concerns. */22export async function api(23 request: Request,24 env: Env,25 url: URL,26 ctx: ExecutionContext,27): Promise<Response> {28 const parts = url.pathname.split("/").filter(Boolean);29 const method = request.method;3031 try {32 if (parts[1] === "notify" && parts.length === 2 && method === "POST") {33 return await handleNotifySend(request, env, ctx);34 }35 if (36 parts[1] === "notify" && parts[2] === "pairing" &&37 parts.length === 3 && method === "POST"38 ) {39 return await openPairingWindow(env);40 }41 if (42 parts[1] === "notify" && parts[2] === "devices" && parts.length === 3 &&43 method === "GET"44 ) {45 return await listNotificationDevices(env);46 }47 if (48 parts[1] === "notify" && parts[2] === "approvals" &&49 parts.length === 4 && method === "GET"50 ) {51 return await previewApproval(decodeURIComponent(parts[3]), env);52 }53 if (54 parts[1] === "notify" && parts[2] === "approvals" &&55 parts.length === 3 && method === "POST"56 ) {57 return await approveEnrollment(request, env);58 }59 if (60 parts[1] === "notify" && parts[2] === "devices" &&61 parts.length === 4 && method === "DELETE"62 ) {63 return await revokeNotificationDevice(decodeURIComponent(parts[3]), env);64 }65 if (parts[1] === "push" && parts[2] === "prepare" && method === "POST") {66 return await handlePrepare(request, env);67 }68 if (parts[1] === "apps" && parts.length === 2 && method === "POST") {69 return await handleAppInit(request, env);70 }71 if (parts[1] === "push" && parts[2] === "commit" && method === "POST") {72 return await handleCommit(request, env, ctx);73 }74 if (parts[1] === "blob" && parts.length === 3 && method === "PUT") {75 return await handleBlobPut(request, env, parts[2]);76 }77 if (parts[1] === "status" && parts.length === 2 && method === "GET") {78 return await handleStatus(env);79 }80 if (81 parts[1] === "security" && parts[2] === "test-alert" && method === "POST"82 ) {83 await sendAlertTest(env);84 return json({ ok: true });85 }86 if (87 parts[1] === "vaults" && parts.length === 2 &&88 (method === "GET" || method === "POST")89 ) {90 return await handleVaults(request, env);91 }92 if (parts[1] === "vaults" && parts.length === 3 && method === "PATCH") {93 return await handleVault(request, env, parts[2]);94 }95 if (96 parts[1] === "default-vaults" && parts.length === 3 && method === "PUT"97 ) {98 return await handleDefaultVault(request, env, parts[2]);99 }100 if (parts[1] === "sites" && parts.length === 2 && method === "GET") {101 return await handleSiteList(url, env);102 }103 if (parts[1] === "sites" && parts.length >= 3) {104 return await handleSite(request, env, parts, ctx);105 }106 throw new ApiError(404, "not found");107 } catch (error) {108 if (error instanceof NotifyHttpError) return notifyErrorResponse(error);109 return apiErrorResponse(error, request, url);110 }111}