Skip to content

Commit

Permalink
Merge pull request #11 from PoCInnovation/feat/api-development
Browse files Browse the repository at this point in the history
Single Repos Page + New Api Routes
  • Loading branch information
quentinbol committed Aug 20, 2024
2 parents 6fd336e + a4e7c9c commit ff42089
Show file tree
Hide file tree
Showing 19 changed files with 1,192 additions and 151 deletions.
31 changes: 0 additions & 31 deletions src/app/api/auth/[...nextauth]/route.js

This file was deleted.

37 changes: 37 additions & 0 deletions src/app/api/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import NextAuth, { NextAuthOptions } from "next-auth"
import { JWT } from "next-auth/jwt"
import GitHubProvider from "next-auth/providers/github"
import { Account, Profile, Session, User } from "next-auth"

export const authOptions: NextAuthOptions = {
providers: [
GitHubProvider({
clientId: process.env.GITHUB_ID ?? "",
clientSecret: process.env.GITHUB_SECRET ?? "",
}),
],
callbacks: {
async jwt({ token, account, profile }: { token: JWT, account: Account | null, profile?: Profile})
{
if (account && account.access_token) {
token.accessToken = account.access_token;
}

if (profile && (profile as any).id) {
token.id = (profile as any).id as string;
}

return token;
},
async session({ session, token }: { session: Session, token: JWT }) {
session.accessToken = token.accessToken;
session.user.id = token.id as string;

return session;
}
}
}

export const handler = NextAuth(authOptions)

export { handler as GET, handler as POST }
57 changes: 47 additions & 10 deletions src/app/api/repos/[owner]/[repo]/commits/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import fetchURL from "../../../../utils/utils";
const querySchema = z.object({
owner: z.string(),
repo: z.string(),
per_page: z.string().optional(),
page: z.string().optional(),
});

/**
Expand All @@ -30,6 +32,18 @@ const querySchema = z.object({
* description: The name of the repository
* schema:
* type: string
* - name: per_page
* in: query
* required: false
* description: The number of commits per page (max 100)
* schema:
* type: integer
* - name: page
* in: query
* required: false
* description: The page number of the results to fetch
* schema:
* type: integer
* produces:
* - application/json
* responses:
Expand Down Expand Up @@ -75,7 +89,7 @@ const querySchema = z.object({
* description: Failed to fetch commits
*/
export async function GET(
req: NextRequest, {params}: any
req: NextRequest, { params }: any
) {
try {
const session = await getServerSession({ req, ...authOptions });
Expand All @@ -90,23 +104,46 @@ export async function GET(

if (!result.success) {
return NextResponse.json(
{ message: "Invalid query parameters" },
{ status: 400 }
)
{ message: "Invalid query parameters" },
{ status: 400 }
);
}

const { owner, repo } = result.data;
const per_page: string = result.data.per_page || "100";
let page: string = result.data.page || "1";

const encodedOwner: string = encodeURIComponent(owner);
const encodedRepo: string = encodeURIComponent(repo);

let allCommits: Array<Object> = [];
let hasMorePages: boolean = true;

const encodedOwner : string = encodeURIComponent(owner);
const encodedRepo : string = encodeURIComponent(repo);
while (hasMorePages) {
const apiUrl: string = `https://api.github.com/repos/${encodedOwner}/${encodedRepo}/commits?per_page=${per_page}&page=${page}`;

const response = await fetchURL(req, apiUrl, "GET");

const apiUrl : string = `https://api.github.com/repos/${encodedOwner}/${encodedRepo}/commits`;
if (!response.ok) {
return NextResponse.json(
{ message: `Failed to fetch commits: ${response.statusText}` },
{ status: response.status }
);
}

const response = await fetchURL(req, apiUrl, "GET");
const commits: Array<Object> = await response.json();

allCommits = allCommits.concat(commits);

if (commits.length < parseInt(per_page)) {
hasMorePages = false;
} else {
page = (parseInt(page) + 1).toString();
}
}

const commits : Array<Object> = await response.json();
return NextResponse.json(
commits[0],
allCommits,
{ status: 200 }
);
} catch (error) {
Expand Down
51 changes: 51 additions & 0 deletions src/app/api/repos/[owner]/[repo]/pulls/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { getServerSession } from 'next-auth';
import { z } from "zod";
import fetchURL from "../../../../utils/utils";
import { authOptions } from "../../../../../api/auth/[...nextauth]/route";
import { NextRequest, NextResponse } from "next/server";

const querySchema = z.object({
owner: z.string(),
repo: z.string(),
});

export async function GET(
req: NextRequest, { params }: any
) {
try {
const session = await getServerSession({ req, ...authOptions });
if (!session) {
console.error('No valid access token found in session');
return NextResponse.json({ message: 'Unauthorized' }, { status: 401 });
}

const result = querySchema.safeParse(params);

if (!result.success) {
return NextResponse.json(
{ message: "Invalid query parameters" },
{ status: 400 }
);
}

const { owner, repo } = result.data

const encodedOwner : string = encodeURIComponent(owner);
const encodedRepo : string = encodeURIComponent(repo);

const apiUrl : string = `https://api.github.com/repos/${encodedOwner}/${encodedRepo}/pulls`;

const response = await fetchURL(req, apiUrl, "GET");

const pulls : object = await response.json();
return NextResponse.json(
pulls,
{ status: 200 }
);
} catch (error) {
return NextResponse.json(
{ message: 'Failed to fetch pulls' },
{ status: 500 }
);
}
};
55 changes: 55 additions & 0 deletions src/app/api/repos/[owner]/[repo]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { NextRequest, NextResponse } from 'next/server';
import { getServerSession } from 'next-auth';
import { authOptions } from '../../../../api/auth/[...nextauth]/route';
import fetchURL from '../../../utils/utils';
import { z } from 'zod';

const querySchema = z.object({
owner: z.string(),
repo: z.string(),
});


export async function GET(
req: NextRequest, {params}: any
) {
try {
const session = await getServerSession({ req, ...authOptions });
if (!session) {
return NextResponse.json(
{ message: 'Unauthorized' },
{ status: 401 }
);
}

const result = querySchema.safeParse(params);

if (!result.success) {
return NextResponse.json(
{ message: "Invalid query parameters" },
{ status: 400 }
)
}

const { owner, repo } = result.data;

const encodedOwner: string = encodeURIComponent(owner);
const encodedRepo: string = encodeURIComponent(repo);

const apiUrl: string = `https://api.github.com/repos/${encodedOwner}/${encodedRepo}`;

const response = await fetchURL(req, apiUrl, "GET");

const repository: object = await response.json();
return NextResponse.json(
repository,
{ status: 200 }
);
} catch (error) {
return NextResponse.json(
{ message: 'Failed to fetch repository' },
{ status: 500 }
);
}
};

2 changes: 1 addition & 1 deletion src/app/api/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const fetchURL = async (req: NextRequest, url: string, method: string) => {
method: method,
headers: {
Authorization: `Bearer ${accessToken}`,
'Content-Type': 'application/json',
'Content-Type': 'application/vnd.github+json',
},
});

Expand Down
50 changes: 49 additions & 1 deletion src/app/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,27 @@
--ring: 222.2 84% 4.9%;

--radius: 0.5rem;

::-webkit-scrollbar {
width: 10px;
}

::-webkit-scrollbar-track {
background: #f1f1f1;
}

::-webkit-scrollbar-thumb {
background: #c5c5c58f;
border-radius: 5px;
}

::-webkit-scrollbar-thumb:hover {
background: #555;
}
}

.dark {
--background: 222.2 100% 0%;
--background: #001229;
--foreground: 210 40% 98%;

--card: 222.2 84% 4.9%;
Expand All @@ -63,6 +80,23 @@
--border: 217.2 32.6% 17.5%;
--input: 217.2 32.6% 17.5%;
--ring: 212.7 26.8% 83.9%;

::-webkit-scrollbar {
width: 10px;
}

::-webkit-scrollbar-track {
background: #000000;
}

::-webkit-scrollbar-thumb {
background: #ffffff;
border-radius: 5px;
}

::-webkit-scrollbar-thumb:hover {
background: #555;
}
}
}

Expand All @@ -73,4 +107,18 @@
body {
@apply bg-background text-foreground;
}
}

@layer components {
.max-h-transition {
transition: max-height 1s ease-out;
}
}

.truncate-text {
display: block;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
max-width: 200px;
}
Loading

0 comments on commit ff42089

Please sign in to comment.