Skip to content

Commit

Permalink
start projects api endpoint wip
Browse files Browse the repository at this point in the history
  • Loading branch information
EthanThatOneKid committed Jan 18, 2024
1 parent b9b5265 commit 97da2e2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 44 deletions.
2 changes: 1 addition & 1 deletion fresh.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
51 changes: 8 additions & 43 deletions plugins/deno_blocks_api/deno_blocks_api.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -35,46 +24,22 @@ export function denoBlocksAPIPlugin(
return new Response("Unauthorized", { status: 401 });
}

const accessTokenResult = await options.kv.get<string>([
...(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);
// },
// },
],
};
}

0 comments on commit 97da2e2

Please sign in to comment.