Skip to content

Commit

Permalink
working on API
Browse files Browse the repository at this point in the history
  • Loading branch information
cletqui committed Jul 11, 2024
1 parent a381b9f commit 37e8b95
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 25 deletions.
9 changes: 8 additions & 1 deletion src/components/login.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Suspense } from "hono/jsx";
import { useRequestContext } from "hono/jsx-renderer";
import { Octokit } from "octokit";
import { RestEndpointMethodTypes } from "@octokit/plugin-rest-endpoint-methods";

import { getAuthenticatedUser } from "../utils/octokit";

Expand All @@ -13,7 +14,13 @@ const LoginButton = () => {
);
};

const User = async ({ user }: { user: Promise<UserResponse> }) => {
const User = async ({
user,
}: {
user: Promise<
RestEndpointMethodTypes["users"]["getAuthenticated"]["response"]
>;
}) => {
try {
const c = useRequestContext();
const { path } = c.req;
Expand Down
7 changes: 5 additions & 2 deletions src/components/repository.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { JSX } from "hono/jsx/jsx-runtime";
import { Suspense } from "hono/jsx";
import { RestEndpointMethodTypes } from "@octokit/plugin-rest-endpoint-methods";

import { timeAgo, dateOptions } from "../utils/time";
import { constructUrl } from "../utils/url";

export const Repository = async ({
repository,
}: {
repository: Promise<RepositoryResponse["data"]>;
repository: Promise<
RestEndpointMethodTypes["repos"]["get"]["response"]["data"]
>;
}) => {
return (
<Suspense fallback={<div>{"prout"}</div>}>
Expand All @@ -19,7 +22,7 @@ export const Repository = async ({
const Container = ({
repository,
}: {
repository: RepositoryResponse["data"];
repository: RestEndpointMethodTypes["repos"]["get"]["response"]["data"];
}): JSX.Element => {
const {
id,
Expand Down
8 changes: 0 additions & 8 deletions src/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
import { RestEndpointMethodTypes } from "@octokit/plugin-rest-endpoint-methods";

declare global {
type RepositoriesResponse =
RestEndpointMethodTypes["repos"]["listPublic"]["response"];
type RepositoryResponse = RestEndpointMethodTypes["repos"]["get"]["response"];
type UserResponse =
RestEndpointMethodTypes["users"]["getAuthenticated"]["response"];
}

declare module "hono" {
interface ContextRenderer {
(
Expand Down
6 changes: 3 additions & 3 deletions src/routes/api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ app.use(handleTokens);
app.use(apiAuth);

/* SWAGGER */
app.doc("/swagger/json", swaggerDoc);
app.get("/swagger", swaggerUI({ url: `/api/swagger/json`, version: "3.1" }));
app.doc("/swagger.json", swaggerDoc);
app.get("/swagger", swaggerUI({ url: `/api/swagger.json`, version: "3.1" }));

/* ROUTES */
const route = createRoute({
method: "get",
path: "/{id}",
path: "/{id}?",
request: {
params: ParamsSchema,
},
Expand Down
23 changes: 13 additions & 10 deletions src/utils/octokit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Context, Next } from "hono";
import { getCookie } from "hono/cookie";
import { createMiddleware } from "hono/factory";
import { Octokit } from "octokit";
import { RestEndpointMethodTypes } from "@octokit/plugin-rest-endpoint-methods";

import { version } from "../../package.json";
import { Bindings, Variables } from "..";
Expand Down Expand Up @@ -93,12 +94,12 @@ export const getOctokitInstance = (
* @async @function getRepositories
* @param {Octokit} octokit - The Octokit instance for GitHub API.
* @param {number} since - The ID to start fetching repositories from.
* @returns {Promise<RepositoriesResponse>} A promise that resolves to the response containing an array of repositories.
* @returns {Promise<RestEndpointMethodTypes["repos"]["listPublic"]["response"]>} A promise that resolves to the response containing an array of repositories.
*/
const getRepositories = async (
octokit: Octokit,
since: number
): Promise<RepositoriesResponse> => {
): Promise<RestEndpointMethodTypes["repos"]["listPublic"]["response"]> => {
try {
return octokit.rest.repos.listPublic({ since }); // octokit.request("GET /repositories", { since });
} catch (error: any) {
Expand All @@ -112,13 +113,13 @@ const getRepositories = async (
* @param {Octokit} octokit - The Octokit instance for GitHub API.
* @param {string} owner - The owner of the repository.
* @param {string} repo - The name of the repository.
* @returns {Promise<>} A promise that resolves to the response containing the repository information.
* @returns {Promise<RestEndpointMethodTypes["repos"]["get"]["response"]>} A promise that resolves to the response containing the repository information.
*/
export const getRepos = async (
octokit: Octokit,
owner: string,
repo: string
): Promise<RepositoryResponse> => {
): Promise<RestEndpointMethodTypes["repos"]["get"]["response"]> => {
try {
return octokit.rest.repos.get({ owner, repo }); // octokit.request("GET /repos/{owner}/{repo}", { owner, repo });
} catch (error: any) {
Expand All @@ -131,12 +132,12 @@ export const getRepos = async (
* @async @function getRepository
* @param {Octokit} octokit - The Octokit instance for GitHub API.
* @param {number} id - The ID of the repository to retrieve.
* @returns {Promise<RepositoryResponse["data"]>} A promise that resolves to the requested repository.
* @returns {Promise<RestEndpointMethodTypes["repos"]["get"]["response"]["data"]>} A promise that resolves to the requested repository.
*/
export const getRepository = async (
octokit: Octokit,
id: number
): Promise<RepositoryResponse["data"]> => {
): Promise<RestEndpointMethodTypes["repos"]["get"]["response"]["data"]> => {
try {
const { data, status, url } = await getRepositories(
octokit,
Expand Down Expand Up @@ -167,12 +168,12 @@ export const getRepository = async (
* @async @function getRandomRepository
* @param {Octokit} octokit - The Octokit instance for GitHub API.
* @param {number} maxId - The maximum ID to consider for repository selection.
* @returns {Promise<RepositoryResponse["data"]>} A promise that resolves to the selected repository.
* @returns {Promise<RestEndpointMethodTypes["repos"]["get"]["response"]["data"]>} A promise that resolves to the selected repository.
*/
export const getRandomRepository = async (
octokit: Octokit,
maxId: number
): Promise<RepositoryResponse["data"]> => {
): Promise<RestEndpointMethodTypes["repos"]["get"]["response"]["data"]> => {
try {
const maxIterations = 10; // max iterations
for (let loop = 0; loop < maxIterations; loop++) {
Expand Down Expand Up @@ -225,11 +226,13 @@ export const fetchRepositoryData = async <T, K extends keyof T>(
* Asynchronously retrieves the authenticated user's information using the provided Octokit instance.
* @async @function getAuthenticatedUser
* @param {Octokit} octokit - The Octokit instance for GitHub API.
* @returns {Promise<UserResponse>} A Promise that resolves to the user's information response.
* @returns {Promise<RestEndpointMethodTypes["users"]["getAuthenticated"]["response"]>} A Promise that resolves to the user's information response.
*/
export const getAuthenticatedUser = async (
octokit: Octokit
): Promise<UserResponse> => {
): Promise<
RestEndpointMethodTypes["users"]["getAuthenticated"]["response"]
> => {
try {
return octokit.rest.users.getAuthenticated(); // octokit.request("GET /user");
} catch (error: any) {
Expand Down
5 changes: 4 additions & 1 deletion src/utils/renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { jsxRenderer } from "hono/jsx-renderer";
import { PropsWithChildren, Suspense } from "hono/jsx";
import { JSX } from "hono/jsx/jsx-runtime";
import { useRequestContext } from "hono/jsx-renderer";
import { RestEndpointMethodTypes } from "@octokit/plugin-rest-endpoint-methods";

import { fetchRepositoryData } from "./octokit";
import { Loader } from "../components/loader";
Expand All @@ -10,7 +11,9 @@ import { Login } from "../components/login";
const Head = ({
repository,
}: {
repository: Promise<RepositoryResponse["data"]>;
repository: Promise<
RestEndpointMethodTypes["repos"]["get"]["response"]["data"]
>;
}) => {
const full_name = fetchRepositoryData(repository, "full_name");
return (
Expand Down

0 comments on commit 37e8b95

Please sign in to comment.