-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from PoCInnovation/feat/api-development
Single Repos Page + New Api Routes
- Loading branch information
Showing
19 changed files
with
1,192 additions
and
151 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
); | ||
} | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.