-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added types, services and repository api route
- Loading branch information
Showing
12 changed files
with
195 additions
and
137 deletions.
There are no files selected for viewing
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,11 @@ | ||
import { getApiKeyFromKey } from "@/lib/apikey" | ||
|
||
export async function isApiKeyValid(request: Request): Promise<boolean> { | ||
const apiKey = request.headers.get("x-api-key") | ||
if (!apiKey) { | ||
return false | ||
} | ||
|
||
const apiKeyData = await getApiKeyFromKey(apiKey) | ||
return Boolean(apiKeyData) | ||
} |
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,54 @@ | ||
// pages/api/repository.js | ||
import { db } from "@/lib/db" | ||
|
||
import { isApiKeyValid } from "../auth" | ||
|
||
export async function GET(request) { | ||
if (!isApiKeyValid(request)) return | ||
const { searchParams } = new URL(request.url) | ||
const repositoryId = searchParams.get("id") | ||
|
||
if (!repositoryId) { | ||
return new Response("Repository ID is required", { status: 400 }) | ||
} | ||
|
||
const repository = await db.repository.findUnique({ | ||
where: { id: repositoryId }, | ||
}) | ||
|
||
if (!repository) { | ||
return new Response("Repository not found", { status: 404 }) | ||
} | ||
|
||
return new Response(JSON.stringify(repository), { | ||
headers: { "Content-Type": "application/json" }, | ||
status: 200, | ||
}) | ||
} | ||
|
||
//this route is for teting purpose | ||
export async function POST(request) { | ||
const data = await request.json() | ||
|
||
try { | ||
const newRepository = await db.repository.create({ | ||
data: { | ||
githubId: data.githubId, | ||
name: data.name, | ||
description: data.description, | ||
homepage: data.homepage, | ||
topics: data.topics, | ||
default_branch: data.default_branch, | ||
installationId: data.installationId, | ||
levels: data.levels, | ||
}, | ||
}) | ||
|
||
return new Response(JSON.stringify(newRepository), { | ||
headers: { "Content-Type": "application/json" }, | ||
status: 201, | ||
}) | ||
} catch (error) { | ||
return new Response(error.message, { 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { z } from "zod" | ||
|
||
import { ZMembership } from "./membership" | ||
import { ZRepository } from "./repository" | ||
|
||
const InstallationTypeEnum = z.enum(["TYPE1", "TYPE2"]) | ||
|
||
export const ZInstallation = z.object({ | ||
id: z.string().cuid2(), | ||
githubId: z.number().int().nonnegative(), | ||
type: InstallationTypeEnum, | ||
memberships: z.array(ZMembership), | ||
repositories: z.array(ZRepository), | ||
}) | ||
|
||
export type TInstallation = z.infer<typeof ZInstallation> |
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,16 @@ | ||
import { z } from "zod" | ||
|
||
import { ZInstallation } from "./installation" | ||
import { ZUser } from "./user" | ||
|
||
const MembershipRoleEnum = z.enum(["owner", "member"]) | ||
|
||
export const ZMembership = z.object({ | ||
installationId: z.string().cuid2(), | ||
userId: z.string().cuid2(), | ||
role: MembershipRoleEnum, | ||
installation: ZInstallation, | ||
user: ZUser, | ||
}) | ||
|
||
export type TMembership = z.infer<typeof ZMembership> |
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,19 @@ | ||
import { z } from "zod" | ||
|
||
import { ZRepository } from "./repository" | ||
import { ZUser } from "./user" | ||
|
||
export const ZPointTransaction = z.object({ | ||
id: z.string().cuid2(), | ||
points: z.number().int(), | ||
description: z.string(), | ||
url: z.string().url().optional(), | ||
userId: z.string().cuid2(), | ||
repositoryId: z.string().cuid2(), | ||
createdAt: z.date(), | ||
updatedAt: z.date(), | ||
user: ZUser, | ||
repository: ZRepository, | ||
}) | ||
|
||
export type TPointTransaction = z.infer<typeof ZPointTransaction> |
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 |
---|---|---|
@@ -1,31 +1,34 @@ | ||
import { z } from "zod" | ||
|
||
import { ZApiKey } from "./apiKey" | ||
import { ZInstallation } from "./installation" | ||
import { ZLevel } from "./level" | ||
import { ZPointTransaction } from "./pointTransaction" | ||
|
||
export const ZRepository = z.object({ | ||
id: z.string().cuid2(), | ||
githubId: z.number().int().nonnegative(), | ||
name: z.string(), | ||
description: z.string().nullable(), | ||
isPrivate: z.boolean(), | ||
createdAt: z.date(), | ||
updatedAt: z.date(), | ||
ownerId: z.string().cuid2(), | ||
ownerType: z.enum(["ORGANIZATION", "USER"]), | ||
userId: z.string().cuid2(), | ||
apiKeys: z.array(ZApiKey), | ||
// Include other fields if needed | ||
description: z.string().optional(), | ||
homepage: z.string().url().optional(), | ||
topics: z.array(z.string()), | ||
default_branch: z.string(), | ||
installationId: z.string().cuid2(), | ||
levels: z.array(ZLevel), | ||
pointTransactions: z.array(ZPointTransaction), | ||
installation: ZInstallation, | ||
}) | ||
|
||
export type TRepository = z.infer<typeof ZRepository> | ||
|
||
export const ZRepositoryCreateInput = z.object({ | ||
githubId: z.number().int().nonnegative(), | ||
name: z.string(), | ||
description: z.string().optional(), | ||
isPrivate: z.boolean().optional(), | ||
ownerId: z.string().cuid2(), | ||
ownerType: z.enum(["ORGANIZATION", "USER"]), | ||
userId: z.string().cuid2(), | ||
// Include other fields required for creation | ||
homepage: z.string().url().optional(), | ||
topics: z.array(z.string()), | ||
default_branch: z.string(), | ||
installationId: z.string().cuid2(), | ||
levels: z.union([z.array(z.unknown()), z.record(z.unknown())]).optional(), | ||
}) | ||
|
||
export type TRepositoryCreateInput = z.infer<typeof ZRepositoryCreateInput> |
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,14 @@ | ||
import { z } from "zod" | ||
|
||
import { ZUser } from "./user" | ||
|
||
export const ZSession = z.object({ | ||
id: z.string().cuid2(), | ||
sessionToken: z.string(), | ||
userId: z.string().cuid2(), | ||
expires: z.date(), | ||
|
||
user: ZUser.optional(), | ||
}) | ||
|
||
export type TSession = z.infer<typeof ZSession> |
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,24 @@ | ||
import { z } from "zod" | ||
|
||
import { ZAccount } from "./account" | ||
import { ZMembership } from "./membership" | ||
import { ZPointTransaction } from "./pointTransaction" | ||
import { ZSession } from "./session" | ||
|
||
export const ZUser = z.object({ | ||
id: z.string().cuid2(), | ||
githubId: z.number().int().nonnegative().optional(), | ||
name: z.string().optional(), | ||
email: z.string().email().optional(), | ||
emailVerified: z.date().optional(), | ||
image: z.string().url().optional(), | ||
address: z.string().optional(), | ||
createdAt: z.date(), | ||
updatedAt: z.date(), | ||
accounts: z.array(ZAccount), | ||
sessions: z.array(ZSession), | ||
pointTransactions: z.array(ZPointTransaction), | ||
memberships: z.array(ZMembership), | ||
}) | ||
|
||
export type TUser = z.infer<typeof ZUser> |
80 changes: 0 additions & 80 deletions
80
prisma/migrations/20240119111335_add_api_key/migration.sql
This file was deleted.
Oops, something went wrong.