feat: api token page in the settings

This commit is contained in:
Catalin Pit
2023-11-24 13:59:33 +02:00
parent 2ccede72ea
commit 80fe7ccdf5
6 changed files with 131 additions and 4 deletions

View File

@ -0,0 +1,65 @@
import { TRPCError } from '@trpc/server';
import { createApiToken } from '@documenso/lib/server-only/public-api/create-api-token';
import { deleteApiTokenById } from '@documenso/lib/server-only/public-api/delete-api-token-by-id';
import { getApiTokenById } from '@documenso/lib/server-only/public-api/get-api-token-by-id';
import { authenticatedProcedure, router } from '../trpc';
import {
ZCreateTokenMutationSchema,
ZDeleteTokenByIdMutationSchema,
ZGetApiTokenByIdQuerySchema,
} from './schema';
export const apiTokenRouter = router({
getTokenById: authenticatedProcedure
.input(ZGetApiTokenByIdQuerySchema)
.query(async ({ input, ctx }) => {
try {
const { id } = input;
return await getApiTokenById({
id,
userId: ctx.user.id,
});
} catch (e) {
throw new TRPCError({
code: 'BAD_REQUEST',
message: 'We were unable to find this API token. Please try again.',
});
}
}),
createToken: authenticatedProcedure
.input(ZCreateTokenMutationSchema)
.mutation(async ({ input, ctx }) => {
try {
const { tokenName } = input;
return await createApiToken({
userId: ctx.user.id,
tokenName,
});
} catch (e) {
throw new TRPCError({
code: 'BAD_REQUEST',
message: 'We were unable to create an API token. Please try again.',
});
}
}),
deleteTokenById: authenticatedProcedure
.input(ZDeleteTokenByIdMutationSchema)
.mutation(async ({ input, ctx }) => {
try {
const { id } = input;
return await deleteApiTokenById({
id,
userId: ctx.user.id,
});
} catch (e) {
throw new TRPCError({
code: 'BAD_REQUEST',
message: 'We were unable to delete this API Token. Please try again.',
});
}
}),
});

View File

@ -0,0 +1,13 @@
import { z } from 'zod';
export const ZGetApiTokenByIdQuerySchema = z.object({
id: z.number().min(1),
});
export const ZCreateTokenMutationSchema = z.object({
tokenName: z.string().min(3, { message: 'The token name should be 3 characters or longer' }),
});
export const ZDeleteTokenByIdMutationSchema = z.object({
id: z.number().min(1),
});