Skip to content

Commit 64b3a5f

Browse files
committed
feat: api keys
1 parent 5c7d005 commit 64b3a5f

File tree

6 files changed

+149
-1
lines changed

6 files changed

+149
-1
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- CreateTable
2+
CREATE TABLE "ApiKeys" (
3+
"id" TEXT NOT NULL PRIMARY KEY,
4+
"key" TEXT NOT NULL,
5+
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
6+
);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
Warnings:
3+
4+
- The primary key for the `ApiKeys` table will be changed. If it partially fails, the table could be left without primary key constraint.
5+
- You are about to drop the column `id` on the `ApiKeys` table. All the data in the column will be lost.
6+
- Added the required column `name` to the `ApiKeys` table without a default value. This is not possible if the table is not empty.
7+
8+
*/
9+
-- RedefineTables
10+
PRAGMA defer_foreign_keys=ON;
11+
PRAGMA foreign_keys=OFF;
12+
CREATE TABLE "new_ApiKeys" (
13+
"key" TEXT NOT NULL PRIMARY KEY,
14+
"name" TEXT NOT NULL,
15+
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
16+
);
17+
INSERT INTO "new_ApiKeys" ("createdAt", "key") SELECT "createdAt", "key" FROM "ApiKeys";
18+
DROP TABLE "ApiKeys";
19+
ALTER TABLE "new_ApiKeys" RENAME TO "ApiKeys";
20+
PRAGMA foreign_keys=ON;
21+
PRAGMA defer_foreign_keys=OFF;

prisma/schema.prisma

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,9 @@ model User {
3131
forcePasswordChange Boolean @default(false)
3232
createdAt DateTime @default(now())
3333
}
34+
35+
model ApiKeys {
36+
key String @id @default(cuid())
37+
name String
38+
createdAt DateTime @default(now())
39+
}

src/app/api/domain/registered/route.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ export async function GET() {
2626
total: domainsWithCheckResults.length,
2727
});
2828
} catch (err) {
29-
console.log("Error", err);
3029
return NextResponse.json(
3130
{ error: "Failed to retrieve registered domains" },
3231
{ status: 500 }

src/app/api/keys/keys-schema.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { z } from "zod";
2+
3+
export const addKeySchema = z.object({
4+
name: z
5+
.string()
6+
.min(1, "Incoming address is required")
7+
});
8+
export type AddKeyValues = z.infer<typeof addKeySchema>
9+
10+
export const deleteKeySchema = z.object({
11+
key: z
12+
.string()
13+
.min(1, "Incoming address is required")
14+
});
15+
export type DeleteKeyValues = z.infer<typeof deleteKeySchema>

src/app/api/keys/route.ts

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import { NextRequest, NextResponse } from "next/server";
2+
import { addKeySchema, deleteKeySchema } from "./keys-schema";
3+
import prisma from "@/lib/prisma";
4+
import { z } from "zod";
5+
6+
export async function POST(request: NextRequest) {
7+
try {
8+
const reqBody = await request.json();
9+
const reqPayload = addKeySchema.parse(reqBody);
10+
11+
await prisma.apiKeys.create({
12+
data: {
13+
name: reqPayload.name,
14+
},
15+
});
16+
17+
return NextResponse.json(
18+
{
19+
message: "Key created successfully!",
20+
},
21+
{ status: 201 }
22+
);
23+
} catch (err) {
24+
if (err instanceof z.ZodError) {
25+
return NextResponse.json(
26+
{
27+
error: "Validation Failed",
28+
details: err.errors,
29+
},
30+
{ status: 400 }
31+
);
32+
}
33+
return NextResponse.json(
34+
{ error: "Failed to create key!" },
35+
{ status: 500 }
36+
);
37+
}
38+
}
39+
40+
export async function GET() {
41+
try {
42+
const keys = await prisma.apiKeys.findMany({
43+
orderBy: {
44+
createdAt: "desc",
45+
},
46+
});
47+
48+
return NextResponse.json({
49+
data: keys,
50+
total: keys.length,
51+
});
52+
} catch (err) {
53+
return NextResponse.json(
54+
{ error: "Failed to fetch keys!" },
55+
{ status: 500 }
56+
);
57+
}
58+
}
59+
60+
export async function DELETE(request: NextRequest) {
61+
try {
62+
const reqBody = await request.json();
63+
const reqPayload = deleteKeySchema.parse(reqBody);
64+
65+
const keyDetails = await prisma.apiKeys.findUnique({
66+
where: {
67+
key: reqPayload.key,
68+
},
69+
});
70+
71+
if (!keyDetails) {
72+
return NextResponse.json({ error: "Key not found!" }, { status: 404 });
73+
}
74+
await prisma.apiKeys.deleteMany({
75+
where: {
76+
key: reqPayload.key,
77+
},
78+
});
79+
return NextResponse.json(
80+
{
81+
message: "Key deleted successfully!",
82+
},
83+
{ status: 200 }
84+
);
85+
} catch (err) {
86+
if (err instanceof z.ZodError) {
87+
return NextResponse.json(
88+
{
89+
error: "Validation Failed",
90+
details: err.errors,
91+
},
92+
{ status: 400 }
93+
);
94+
}
95+
96+
return NextResponse.json(
97+
{ error: "Failed to delete domain" },
98+
{ status: 500 }
99+
);
100+
}
101+
}

0 commit comments

Comments
 (0)