-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.demo.ts
More file actions
35 lines (30 loc) · 1.44 KB
/
vite.config.demo.ts
File metadata and controls
35 lines (30 loc) · 1.44 KB
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
// vite.config.demo.ts
import { defineConfig } from 'vite';
import books from './demo/books.json' with { type: 'json' };
export default defineConfig({
plugins: [
{
name: 'mock-books-api-with-pagination',
configureServer(server) {
server.middlewares.use('/api/books', (req, res) => {
// Extrai os parâmetros de paginação da URL do pedido
const url = new URL(req.url!, `http://${req.headers.host}`);
const page = parseInt(url.searchParams.get('page') || '1', 10);
const perPage = parseInt(url.searchParams.get('perPage') || '10', 10);
// Calcula o índice de início e fim para "fatiar" o array
const start = (page - 1) * perPage;
const end = start + perPage;
// "Fatia" os dados para devolver apenas a página pedida
const paginatedData = books.data.slice(start, end);
// A resposta da API
const responseData = {
data: paginatedData, // Envia apenas os dados da página atual
totalRecords: books.data.length // Envia o total REAL de registos
};
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(responseData));
});
}
}
]
});