1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
import { generateApiKey } from "@/server/auth";
import { authedProcedure, router } from "../trpc";
import { prisma } from "@hoarder/db";
import { z } from "zod";
export const apiKeysAppRouter = router({
create: authedProcedure
.input(
z.object({
name: z.string(),
}),
)
.output(
z.object({
id: z.string(),
name: z.string(),
key: z.string(),
createdAt: z.date(),
}),
)
.mutation(async ({ input, ctx }) => {
return await generateApiKey(input.name, ctx.user.id);
}),
revoke: authedProcedure
.input(
z.object({
id: z.string(),
}),
)
.output(z.object({}))
.mutation(async ({ input, ctx }) => {
const resp = await prisma.apiKey.delete({
where: {
id: input.id,
userId: ctx.user.id,
},
});
return resp;
}),
list: authedProcedure
.output(
z.object({
keys: z.array(
z.object({
id: z.string(),
name: z.string(),
createdAt: z.date(),
keyId: z.string(),
}),
),
}),
)
.query(async ({ ctx }) => {
const resp = await prisma.apiKey.findMany({
where: {
userId: ctx.user.id,
},
select: {
id: true,
name: true,
createdAt: true,
keyId: true,
},
});
return { keys: resp };
}),
});
|