mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 08:13:56 +10:00
feat: get documents api route with pagination
This commit is contained in:
28
apps/web/src/pages/api/v1/[...ts-rest].tsx
Normal file
28
apps/web/src/pages/api/v1/[...ts-rest].tsx
Normal file
@ -0,0 +1,28 @@
|
||||
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||
|
||||
import { getDocuments } from '@documenso/lib/server-only/public-api/get-documents';
|
||||
import { contract } from '@documenso/trpc/api-contract/contract';
|
||||
import { createNextRoute, createNextRouter } from '@documenso/trpc/server/public-api/ts-rest';
|
||||
|
||||
const router = createNextRoute(contract, {
|
||||
getDocuments: async (args) => {
|
||||
const page = Number(args.query.page) || 1;
|
||||
const perPage = Number(args.query.perPage) || 10;
|
||||
|
||||
const { documents, totalPages } = await getDocuments({ page, perPage });
|
||||
|
||||
return {
|
||||
status: 200,
|
||||
body: {
|
||||
documents,
|
||||
totalPages,
|
||||
},
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
const nextRouter = createNextRouter(contract, router);
|
||||
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
await nextRouter(req, res);
|
||||
}
|
||||
21
packages/lib/server-only/public-api/get-documents.ts
Normal file
21
packages/lib/server-only/public-api/get-documents.ts
Normal file
@ -0,0 +1,21 @@
|
||||
import { prisma } from '@documenso/prisma';
|
||||
|
||||
type GetDocumentsProps = {
|
||||
page: number;
|
||||
perPage: number;
|
||||
};
|
||||
|
||||
export const getDocuments = async ({ page = 1, perPage = 10 }: GetDocumentsProps) => {
|
||||
const [documents, count] = await Promise.all([
|
||||
await prisma.document.findMany({
|
||||
take: perPage,
|
||||
skip: Math.max(page - 1, 0) * perPage,
|
||||
}),
|
||||
await prisma.document.count(),
|
||||
]);
|
||||
|
||||
return {
|
||||
documents,
|
||||
totalPages: Math.ceil(count / perPage),
|
||||
};
|
||||
};
|
||||
@ -4,19 +4,19 @@ import { z } from 'zod';
|
||||
const c = initContract();
|
||||
|
||||
const GetDocumentsQuery = z.object({
|
||||
take: z.string().default('10'),
|
||||
skip: z.string().default('0'),
|
||||
page: z.string().optional(),
|
||||
perPage: z.string().optional(),
|
||||
});
|
||||
|
||||
const DocumentSchema = z.object({
|
||||
id: z.string(),
|
||||
id: z.number(),
|
||||
userId: z.number(),
|
||||
title: z.string(),
|
||||
status: z.string(),
|
||||
documentDataId: z.string(),
|
||||
createdAt: z.string(),
|
||||
updatedAt: z.string(),
|
||||
completedAt: z.string(),
|
||||
createdAt: z.date(),
|
||||
updatedAt: z.date(),
|
||||
completedAt: z.date().nullable(),
|
||||
});
|
||||
|
||||
export const contract = c.router({
|
||||
|
||||
3
packages/trpc/server/public-api/ts-rest.ts
Normal file
3
packages/trpc/server/public-api/ts-rest.ts
Normal file
@ -0,0 +1,3 @@
|
||||
import { createNextRoute, createNextRouter } from '@ts-rest/next';
|
||||
|
||||
export { createNextRoute, createNextRouter };
|
||||
Reference in New Issue
Block a user