mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 00:03:33 +10:00
fix: refactor token router (#1981)
This commit is contained in:
27
packages/trpc/server/api-token-router/create-api-token.ts
Normal file
27
packages/trpc/server/api-token-router/create-api-token.ts
Normal file
@ -0,0 +1,27 @@
|
||||
import { createApiToken } from '@documenso/lib/server-only/public-api/create-api-token';
|
||||
|
||||
import { authenticatedProcedure } from '../trpc';
|
||||
import {
|
||||
ZCreateApiTokenRequestSchema,
|
||||
ZCreateApiTokenResponseSchema,
|
||||
} from './create-api-token.types';
|
||||
|
||||
export const createApiTokenRoute = authenticatedProcedure
|
||||
.input(ZCreateApiTokenRequestSchema)
|
||||
.output(ZCreateApiTokenResponseSchema)
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
const { tokenName, teamId, expirationDate } = input;
|
||||
|
||||
ctx.logger.info({
|
||||
input: {
|
||||
teamId,
|
||||
},
|
||||
});
|
||||
|
||||
return await createApiToken({
|
||||
userId: ctx.user.id,
|
||||
teamId,
|
||||
tokenName,
|
||||
expiresIn: expirationDate,
|
||||
});
|
||||
});
|
||||
@ -0,0 +1,12 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
export const ZCreateApiTokenRequestSchema = z.object({
|
||||
teamId: z.number(),
|
||||
tokenName: z.string().min(3, { message: 'The token name should be 3 characters or longer' }),
|
||||
expirationDate: z.string().nullable(),
|
||||
});
|
||||
|
||||
export const ZCreateApiTokenResponseSchema = z.object({
|
||||
id: z.number(),
|
||||
token: z.string(),
|
||||
});
|
||||
27
packages/trpc/server/api-token-router/delete-api-token.ts
Normal file
27
packages/trpc/server/api-token-router/delete-api-token.ts
Normal file
@ -0,0 +1,27 @@
|
||||
import { deleteTokenById } from '@documenso/lib/server-only/public-api/delete-api-token-by-id';
|
||||
|
||||
import { authenticatedProcedure } from '../trpc';
|
||||
import {
|
||||
ZDeleteApiTokenRequestSchema,
|
||||
ZDeleteApiTokenResponseSchema,
|
||||
} from './delete-api-token.types';
|
||||
|
||||
export const deleteApiTokenRoute = authenticatedProcedure
|
||||
.input(ZDeleteApiTokenRequestSchema)
|
||||
.output(ZDeleteApiTokenResponseSchema)
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
const { id, teamId } = input;
|
||||
|
||||
ctx.logger.info({
|
||||
input: {
|
||||
id,
|
||||
teamId,
|
||||
},
|
||||
});
|
||||
|
||||
await deleteTokenById({
|
||||
id,
|
||||
teamId,
|
||||
userId: ctx.user.id,
|
||||
});
|
||||
});
|
||||
@ -0,0 +1,8 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
export const ZDeleteApiTokenRequestSchema = z.object({
|
||||
id: z.number().min(1),
|
||||
teamId: z.number(),
|
||||
});
|
||||
|
||||
export const ZDeleteApiTokenResponseSchema = z.void();
|
||||
19
packages/trpc/server/api-token-router/get-api-tokens.ts
Normal file
19
packages/trpc/server/api-token-router/get-api-tokens.ts
Normal file
@ -0,0 +1,19 @@
|
||||
import { getApiTokens } from '@documenso/lib/server-only/public-api/get-api-tokens';
|
||||
|
||||
import { authenticatedProcedure } from '../trpc';
|
||||
import { ZGetApiTokensRequestSchema, ZGetApiTokensResponseSchema } from './get-api-tokens.types';
|
||||
|
||||
export const getApiTokensRoute = authenticatedProcedure
|
||||
.input(ZGetApiTokensRequestSchema)
|
||||
.output(ZGetApiTokensResponseSchema)
|
||||
.query(async ({ ctx }) => {
|
||||
const { teamId } = ctx;
|
||||
|
||||
ctx.logger.info({
|
||||
input: {
|
||||
teamId,
|
||||
},
|
||||
});
|
||||
|
||||
return await getApiTokens({ userId: ctx.user.id, teamId });
|
||||
});
|
||||
@ -0,0 +1,16 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
import ApiTokenSchema from '@documenso/prisma/generated/zod/modelSchema/ApiTokenSchema';
|
||||
|
||||
export const ZGetApiTokensRequestSchema = z.void();
|
||||
|
||||
export const ZGetApiTokensResponseSchema = z.array(
|
||||
ApiTokenSchema.pick({
|
||||
id: true,
|
||||
name: true,
|
||||
createdAt: true,
|
||||
expires: true,
|
||||
}),
|
||||
);
|
||||
|
||||
export type TGetApiTokensResponse = z.infer<typeof ZGetApiTokensResponseSchema>;
|
||||
@ -1,50 +1,10 @@
|
||||
import { createApiToken } from '@documenso/lib/server-only/public-api/create-api-token';
|
||||
import { deleteTokenById } from '@documenso/lib/server-only/public-api/delete-api-token-by-id';
|
||||
import { getApiTokens } from '@documenso/lib/server-only/public-api/get-api-tokens';
|
||||
|
||||
import { authenticatedProcedure, router } from '../trpc';
|
||||
import { ZCreateTokenMutationSchema, ZDeleteTokenByIdMutationSchema } from './schema';
|
||||
import { router } from '../trpc';
|
||||
import { createApiTokenRoute } from './create-api-token';
|
||||
import { deleteApiTokenRoute } from './delete-api-token';
|
||||
import { getApiTokensRoute } from './get-api-tokens';
|
||||
|
||||
export const apiTokenRouter = router({
|
||||
getTokens: authenticatedProcedure.query(async ({ ctx }) => {
|
||||
return await getApiTokens({ userId: ctx.user.id, teamId: ctx.teamId });
|
||||
}),
|
||||
|
||||
createToken: authenticatedProcedure
|
||||
.input(ZCreateTokenMutationSchema)
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
const { tokenName, teamId, expirationDate } = input;
|
||||
|
||||
ctx.logger.info({
|
||||
input: {
|
||||
teamId,
|
||||
},
|
||||
});
|
||||
|
||||
return await createApiToken({
|
||||
userId: ctx.user.id,
|
||||
teamId,
|
||||
tokenName,
|
||||
expiresIn: expirationDate,
|
||||
});
|
||||
}),
|
||||
|
||||
deleteTokenById: authenticatedProcedure
|
||||
.input(ZDeleteTokenByIdMutationSchema)
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
const { id, teamId } = input;
|
||||
|
||||
ctx.logger.info({
|
||||
input: {
|
||||
id,
|
||||
teamId,
|
||||
},
|
||||
});
|
||||
|
||||
return await deleteTokenById({
|
||||
id,
|
||||
teamId,
|
||||
userId: ctx.user.id,
|
||||
});
|
||||
}),
|
||||
create: createApiTokenRoute,
|
||||
getMany: getApiTokensRoute,
|
||||
delete: deleteApiTokenRoute,
|
||||
});
|
||||
|
||||
@ -1,16 +0,0 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
export const ZCreateTokenMutationSchema = z.object({
|
||||
teamId: z.number(),
|
||||
tokenName: z.string().min(3, { message: 'The token name should be 3 characters or longer' }),
|
||||
expirationDate: z.string().nullable(),
|
||||
});
|
||||
|
||||
export type TCreateTokenMutationSchema = z.infer<typeof ZCreateTokenMutationSchema>;
|
||||
|
||||
export const ZDeleteTokenByIdMutationSchema = z.object({
|
||||
id: z.number().min(1),
|
||||
teamId: z.number(),
|
||||
});
|
||||
|
||||
export type TDeleteTokenByIdMutationSchema = z.infer<typeof ZDeleteTokenByIdMutationSchema>;
|
||||
Reference in New Issue
Block a user