Skip to content

Commit

Permalink
fix little mistake
Browse files Browse the repository at this point in the history
  • Loading branch information
cletqui committed Jul 11, 2024
1 parent 166e910 commit a381b9f
Show file tree
Hide file tree
Showing 10 changed files with 205 additions and 135 deletions.
17 changes: 0 additions & 17 deletions src/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Octokit } from "octokit";
import { RestEndpointMethodTypes } from "@octokit/plugin-rest-endpoint-methods";

declare global {
Expand All @@ -10,22 +9,6 @@ declare global {
}

declare module "hono" {
type Bindings = {
CLIENT_ID: string;
CLIENT_SECRET: string;
};

type Variables = {
max_id: { id: number; timestamp: number };
access_token?: string;
expires_in?: string;
refresh_token?: string;
state: string;
octokit: Octokit;
};

type Env = { Bindings: Bindings; Variables: Variables };

interface ContextRenderer {
(
content: string | Promise<string>,
Expand Down
39 changes: 30 additions & 9 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
import { Context, Hono, Env } from "hono";
import { Context, Hono } from "hono";
import { logger } from "hono/logger";
import { Octokit } from "octokit";

import { renderer } from "./utils/renderer";
import { Repository } from "./components/repository";
import { getRandomRepository, handleMaxId } from "./utils/octokit";
import { handleTokens } from "./utils/tokens";
import { getRandomRepository, handleMaxId } from "./utils/octokit";
import { Repository } from "./components/repository";

import api from "./routes/api";
import github from "./routes/github";
import template from "./routes/template";
import id from "./routes/id";

/* TYPES */
export type Bindings = {
CLIENT_ID: string;
CLIENT_SECRET: string;
};

export type Variables = {
max_id: { id: number; timestamp: number };
access_token?: string;
expires_in?: string;
refresh_token?: string;
state: string;
octokit: Octokit;
};

/* APP */
const app = new Hono<Env>();
const app = new Hono<{ Bindings: Bindings; Variables: Variables }>();

/* MIDDLEWARES */
app.use(logger());
Expand All @@ -27,11 +43,16 @@ app.route("/template", template);
app.route("/id", id);

/* ROOT */
app.get("/", async (c: Context<Env>): Promise<Response> => {
const { max_id, octokit } = c.var;
const repository = getRandomRepository(octokit, max_id.id);
return c.render(<Repository repository={repository} />, { repository });
});
app.get(
"/",
async (
c: Context<{ Bindings: Bindings; Variables: Variables }>
): Promise<Response> => {
const { max_id, octokit } = c.var;
const repository = getRandomRepository(octokit, max_id.id);
return c.render(<Repository repository={repository} />, { repository });
}
);

/* DEFAULT */
/* app.get(
Expand Down
28 changes: 16 additions & 12 deletions src/routes/api.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Context, Env } from "hono";
import { Context } from "hono";
import { poweredBy } from "hono/powered-by";
import { prettyJSON } from "hono/pretty-json";
import { cors } from "hono/cors";
import { createRoute, OpenAPIHono } from "@hono/zod-openapi";
import { swaggerUI } from "@hono/swagger-ui";

import { Bindings, Variables } from "..";
import {
ErrorSchema,
ParamsSchema,
Expand All @@ -16,7 +17,7 @@ import { handleTokens } from "../utils/tokens";
import { apiAuth, getRandomRepository, getRepository } from "../utils/octokit";

/* APP */
const app = new OpenAPIHono<Env>();
const app = new OpenAPIHono<{ Bindings: Bindings; Variables: Variables }>();

/* MIDDLEWARES */
app.use(poweredBy());
Expand Down Expand Up @@ -86,16 +87,19 @@ const route = createRoute({
}
); */

app.openapi(route, async (c: Context<Env>) => {
const { id } = c.req.param();
console.log("ID:", id);
const { max_id, octokit } = c.var;
try {
const repository = await getRandomRepository(octokit, max_id.id);
return c.json(repository, 200);
} catch (error: any) {
return c.json({ error: "Failed to fetch repository data" }, 500);
app.openapi(
route,
async (c: Context<{ Bindings: Bindings; Variables: Variables }>) => {
const { id } = c.req.param();
console.log("ID:", id);
const { max_id, octokit } = c.var;
try {
const repository = await getRandomRepository(octokit, max_id.id);
return c.json(repository, 200);
} catch (error: any) {
return c.json({ error: "Failed to fetch repository data" }, 500);
}
}
});
);

export default app;
89 changes: 52 additions & 37 deletions src/routes/github.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Context, Hono, Env } from "hono";
import { Context, Hono } from "hono";

import { Bindings, Variables } from "..";
import { generateState, handleState } from "../utils/state";
import { handleAccess, handleLogout } from "../utils/tokens";

/* APP */
const app = new Hono<Env>();
const app = new Hono<{ Bindings: Bindings; Variables: Variables }>();

/* MIDDLEWARES */
app.use("/login", generateState);
Expand All @@ -13,40 +14,54 @@ app.use("/access_token", handleAccess);
app.use("/logout", handleLogout);

/* ENDPOINTS */
app.get("/login", async (c: Context<Env>): Promise<Response> => {
const { CLIENT_ID } = c.env;
const { state } = c.var;
const { url } = c.req;
const redirect_url = new URL(url);
redirect_url.pathname = "/github/callback";
const searchParams = new URLSearchParams();
searchParams.append("client_id", CLIENT_ID);
searchParams.append("redirect_uri", redirect_url.toString());
searchParams.append("state", state);
return c.redirect(
`https://github.com/login/oauth/authorize?${searchParams.toString()}`,
302
);
});

app.get("/callback", async (c: Context<Env>) => {
const { refresh_token, access_token, expires_in } = c.var;
const searchParams = new URLSearchParams();
refresh_token && searchParams.append("refresh_token", refresh_token);
access_token && searchParams.append("access_token", access_token);
expires_in && searchParams.append("expires_in", expires_in);
searchParams.append("callback_url", "/");
return c.redirect(`/github/access_token?${searchParams.toString()}`, 302);
});

app.get("/access_token", async (c: Context<Env>) => {
const { callback_url } = c.req.query();
return c.redirect(callback_url || "/", 302);
});

app.get("/logout", async (c: Context<Env>) => {
const { callback_url } = c.req.query();
return c.redirect(callback_url || "/", 302);
});
app.get(
"/login",
async (
c: Context<{ Bindings: Bindings; Variables: Variables }>
): Promise<Response> => {
const { CLIENT_ID } = c.env;
const { state } = c.var;
const { url } = c.req;
const redirect_url = new URL(url);
redirect_url.pathname = "/github/callback";
const searchParams = new URLSearchParams();
searchParams.append("client_id", CLIENT_ID);
searchParams.append("redirect_uri", redirect_url.toString());
searchParams.append("state", state);
return c.redirect(
`https://github.com/login/oauth/authorize?${searchParams.toString()}`,
302
);
}
);

app.get(
"/callback",
async (c: Context<{ Bindings: Bindings; Variables: Variables }>) => {
const { refresh_token, access_token, expires_in } = c.var;
const searchParams = new URLSearchParams();
refresh_token && searchParams.append("refresh_token", refresh_token);
access_token && searchParams.append("access_token", access_token);
expires_in && searchParams.append("expires_in", expires_in);
searchParams.append("callback_url", "/");
return c.redirect(`/github/access_token?${searchParams.toString()}`, 302);
}
);

app.get(
"/access_token",
async (c: Context<{ Bindings: Bindings; Variables: Variables }>) => {
const { callback_url } = c.req.query();
return c.redirect(callback_url || "/", 302);
}
);

app.get(
"/logout",
async (c: Context<{ Bindings: Bindings; Variables: Variables }>) => {
const { callback_url } = c.req.query();
return c.redirect(callback_url || "/", 302);
}
);

export default app;
47 changes: 26 additions & 21 deletions src/routes/id.tsx
Original file line number Diff line number Diff line change
@@ -1,35 +1,40 @@
import { Context, Hono, Env } from "hono";
import { Context, Hono } from "hono";

import {} from "..";
import { Bindings, Variables } from "..";
import { handleTokens } from "../utils/tokens";
import { getMaxId, handleMaxId } from "../utils/octokit";
import { setCookie } from "hono/cookie";

/* APP */
const app = new Hono<Env>();
const app = new Hono<{ Bindings: Bindings; Variables: Variables }>();

/* MIDDLEWARES */
app.use(handleMaxId);
app.use(handleTokens);

/* ENDPOINTS */
app.get("/", async (c: Context<Env>): Promise<Response> => {
const {
max_id: { id, timestamp: old },
access_token,
octokit,
} = c.var;
const update = access_token ? await getMaxId(octokit, id) : id;
const timestamp = access_token ? new Date().getTime() : old;
setCookie(c, "max_id", `{ "id": ${update}, "timestamp": ${timestamp} }`, {
path: "/",
secure: true,
httpOnly: false, // true
maxAge: 31557600,
sameSite: "Strict",
prefix: "secure",
});
return c.json({ id: update, timestamp: timestamp });
});
app.get(
"/",
async (
c: Context<{ Bindings: Bindings; Variables: Variables }>
): Promise<Response> => {
const {
max_id: { id, timestamp: old },
access_token,
octokit,
} = c.var;
const update = access_token ? await getMaxId(octokit, id) : id;
const timestamp = access_token ? new Date().getTime() : old;
setCookie(c, "max_id", `{ "id": ${update}, "timestamp": ${timestamp} }`, {
path: "/",
secure: true,
httpOnly: false, // true
maxAge: 31557600,
sameSite: "Strict",
prefix: "secure",
});
return c.json({ id: update, timestamp: timestamp });
}
);

export default app;
22 changes: 14 additions & 8 deletions src/routes/template.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
import { Context, Hono, Env } from "hono";
import { Context, Hono } from "hono";

import { Bindings, Variables } from "..";
import { Repository } from "../components/repository";
import { handleTokens } from "../utils/tokens";
import { getRepository } from "../utils/octokit";

/* APP */
const app = new Hono<Env>();
const app = new Hono<{ Bindings: Bindings; Variables: Variables }>();

/* MIDDLEWARES */
app.use(handleTokens);

/* ENDPOINTS */
app.get("/", async (c: Context<Env>): Promise<Response> => {
const { octokit } = c.var;
const id = 811042081; // cletqui/petithub
const repository = getRepository(octokit, id);
return c.render(<Repository repository={repository} />, { repository });
});
app.get(
"/",
async (
c: Context<{ Bindings: Bindings; Variables: Variables }>
): Promise<Response> => {
const { octokit } = c.var;
const id = 811042081; // cletqui/petithub
const repository = getRepository(octokit, id);
return c.render(<Repository repository={repository} />, { repository });
}
);

export default app;
Loading

0 comments on commit a381b9f

Please sign in to comment.