mirror of
https://github.com/documenso/documenso.git
synced 2025-11-12 15:53:02 +10:00
fix: tidy code and update endpoints
This commit is contained in:
@ -3,7 +3,7 @@ import { authRouter } from './auth-router/router';
|
||||
import { documentRouter } from './document-router/router';
|
||||
import { fieldRouter } from './field-router/router';
|
||||
import { profileRouter } from './profile-router/router';
|
||||
import { shareRouter } from './share-router/router';
|
||||
import { shareLinkRouter } from './share-link-router/router';
|
||||
import { procedure, router } from './trpc';
|
||||
|
||||
export const appRouter = router({
|
||||
@ -12,7 +12,7 @@ export const appRouter = router({
|
||||
profile: profileRouter,
|
||||
document: documentRouter,
|
||||
field: fieldRouter,
|
||||
share: shareRouter,
|
||||
shareLink: shareLinkRouter,
|
||||
});
|
||||
|
||||
export type AppRouter = typeof appRouter;
|
||||
|
||||
35
packages/trpc/server/share-link-router/router.ts
Normal file
35
packages/trpc/server/share-link-router/router.ts
Normal file
@ -0,0 +1,35 @@
|
||||
import { TRPCError } from '@trpc/server';
|
||||
|
||||
import { createOrGetShareLink } from '@documenso/lib/server-only/share/create-or-get-share-link';
|
||||
|
||||
import { procedure, router } from '../trpc';
|
||||
import { ZCreateOrGetShareLinkMutationSchema } from './schema';
|
||||
|
||||
export const shareLinkRouter = router({
|
||||
createOrGetShareLink: procedure
|
||||
.input(ZCreateOrGetShareLinkMutationSchema)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
try {
|
||||
const { documentId, token } = input;
|
||||
|
||||
if (token) {
|
||||
return await createOrGetShareLink({ documentId, token });
|
||||
}
|
||||
|
||||
if (!ctx.user?.id) {
|
||||
throw new Error(
|
||||
'You must either provide a token or be logged in to create a sharing link.',
|
||||
);
|
||||
}
|
||||
|
||||
return await createOrGetShareLink({ documentId, userId: ctx.user.id });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
|
||||
throw new TRPCError({
|
||||
code: 'BAD_REQUEST',
|
||||
message: 'We were unable to create a sharing link.',
|
||||
});
|
||||
}
|
||||
}),
|
||||
});
|
||||
10
packages/trpc/server/share-link-router/schema.ts
Normal file
10
packages/trpc/server/share-link-router/schema.ts
Normal file
@ -0,0 +1,10 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
export const ZCreateOrGetShareLinkMutationSchema = z.object({
|
||||
documentId: z.number(),
|
||||
token: z.string().optional(),
|
||||
});
|
||||
|
||||
export type TCreateOrGetShareLinkMutationSchema = z.infer<
|
||||
typeof ZCreateOrGetShareLinkMutationSchema
|
||||
>;
|
||||
@ -1,38 +0,0 @@
|
||||
import { TRPCError } from '@trpc/server';
|
||||
|
||||
import { createSharingId } from '@documenso/lib/server-only/share/create-share-id';
|
||||
import { getSharingId } from '@documenso/lib/server-only/share/get-share-id';
|
||||
|
||||
import { procedure, router } from '../trpc';
|
||||
import { ZShareLinkCreateSchema, ZShareLinkGetSchema } from './schema';
|
||||
|
||||
export const shareRouter = router({
|
||||
create: procedure.input(ZShareLinkCreateSchema).mutation(async ({ input }) => {
|
||||
try {
|
||||
const { documentId, recipientId } = input;
|
||||
|
||||
return await createSharingId({ documentId, recipientId });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
|
||||
throw new TRPCError({
|
||||
code: 'BAD_REQUEST',
|
||||
message: 'We were unable to create a sharing link.',
|
||||
});
|
||||
}
|
||||
}),
|
||||
get: procedure.input(ZShareLinkGetSchema).query(async ({ input }) => {
|
||||
try {
|
||||
const { shareId } = input;
|
||||
|
||||
return await getSharingId({ shareId });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
|
||||
throw new TRPCError({
|
||||
code: 'BAD_REQUEST',
|
||||
message: 'We were unable to create a sharing link.',
|
||||
});
|
||||
}
|
||||
}),
|
||||
});
|
||||
@ -1,13 +0,0 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
export const ZShareLinkCreateSchema = z.object({
|
||||
documentId: z.number(),
|
||||
recipientId: z.number(),
|
||||
});
|
||||
|
||||
export const ZShareLinkGetSchema = z.object({
|
||||
shareId: z.string(),
|
||||
});
|
||||
|
||||
export type ZShareLinkCreateSchema = z.infer<typeof ZShareLinkCreateSchema>;
|
||||
export type ZShareLinkGetSchema = z.infer<typeof ZShareLinkGetSchema>;
|
||||
Reference in New Issue
Block a user