From 97da2e23e42ba046e7d9fb4266a6525d7538633c Mon Sep 17 00:00:00 2001 From: EthanThatOneKid <31261035+EthanThatOneKid@users.noreply.github.com> Date: Wed, 17 Jan 2024 19:29:36 -0800 Subject: [PATCH] start projects api endpoint wip --- fresh.config.ts | 2 +- plugins/deno_blocks_api/deno_blocks_api.ts | 51 ++++------------------ 2 files changed, 9 insertions(+), 44 deletions(-) diff --git a/fresh.config.ts b/fresh.config.ts index 1abe9d8..eca0285 100644 --- a/fresh.config.ts +++ b/fresh.config.ts @@ -7,7 +7,7 @@ import { kv } from "#/lib/resources/kv.ts"; const plugins = [ kvOAuthPlugin(), - denoBlocksAPIPlugin({ kv }), + denoBlocksAPIPlugin(), ]; const ENABLE_KV_INSIGHTS_KEY = "ENABLE_KV_INSIGHTS"; diff --git a/plugins/deno_blocks_api/deno_blocks_api.ts b/plugins/deno_blocks_api/deno_blocks_api.ts index 47395ff..ed1d90c 100644 --- a/plugins/deno_blocks_api/deno_blocks_api.ts +++ b/plugins/deno_blocks_api/deno_blocks_api.ts @@ -1,26 +1,15 @@ import type { Plugin } from "$fresh/server.ts"; import { getSessionId } from "deno_kv_oauth/mod.ts"; -import { getGitHubAPIUserByAccessToken } from "#/lib/github_api/mod.ts"; export interface DenoBlocksAPIPluginOptions { - kv: Deno.Kv; - kvKeyNamespace?: Deno.KvKey; basePath?: string; } -export enum DenoBlocksAPIPluginKVKey { - ACCESS_TOKEN_BY_SESSION_ID = "access_token_by_session_id", -} - -export const DENO_BLOCKS_API_PLUGIN_KV_KEY_NAMESPACE: Deno.KvKey = [ - "deno_blocks_api", -]; - /** * Requires kv-oauth plugin to be loaded before this plugin. */ export function denoBlocksAPIPlugin( - options: DenoBlocksAPIPluginOptions, + options: DenoBlocksAPIPluginOptions = {}, ): Plugin { const { basePath = "/api" } = options; return { @@ -35,46 +24,22 @@ export function denoBlocksAPIPlugin( return new Response("Unauthorized", { status: 401 }); } - const accessTokenResult = await options.kv.get([ - ...(options.kvKeyNamespace ?? - DENO_BLOCKS_API_PLUGIN_KV_KEY_NAMESPACE), - DenoBlocksAPIPluginKVKey.ACCESS_TOKEN_BY_SESSION_ID, - sessionID, - ]); - if (!accessTokenResult.value) { + // TODO: Create a new project for the user. + } + + if (request.method === "GET") { + const sessionID = await getSessionId(request); + if (!sessionID) { return new Response("Unauthorized", { status: 401 }); } - const githubUser = await getGitHubAPIUserByAccessToken( - accessTokenResult.value, - ); - - return new Response(JSON.stringify(githubUser)); - - // Create a project with subhosting API. - // https://github.com/denoland/subhosting_ide_starter/blob/main/subhosting.ts - - // const projectID = crypto.randomUUID(); + // List project from Kv that are owned by the user. } // return await signIn(req); return new Response("Not implemented", { status: 501 }); }, }, - // { - // path: "/callback", - // async handler(req) { - // // Return object also includes `accessToken` and `sessionId` properties. - // const { response } = await handleCallback(req); - // return response; - // }, - // }, - // { - // path: "/signout", - // async handler(req) { - // return await signOut(req); - // }, - // }, ], }; }