2025-07-14 19:25:10 +02:00

132 lines
3.8 KiB
TypeScript

// import { http, HttpResponse } from "msw";
//
// export const handlers = [
// http.get(
// "https://test-bo.paymentiq.io/paymentiq/backoffice/api/v2/metrics/txsummary",
// (req, _res, _ctx) => {
// const merchantId = req.url.searchParams.get("merchantId");
// const fromDate = req.url.searchParams.get("fromDate");
// const toDate = req.url.searchParams.get("toDate");
//
// console.log(merchantId, fromDate, toDate);
//
// return HttpResponse.json({
// result: {
// txCount: { total: 0, successful: 0 },
// amount: { value: "0", currency: "EUR" },
// },
// });
// }
// ),
// ];
import { transactionDummyData } from '@/app/features/Pages/transactions/mockData';
import { http, HttpResponse } from 'msw';
export const handlers = [
// Simple GET endpoint
http.get('https://api.example.com/user', () => {
return HttpResponse.json({
id: 'usr_123',
name: 'John Doe',
email: 'john@example.com'
});
}),
// POST endpoint with request validation
http.post('https://api.example.com/login', async ({ request }) => {
const { username, password } = await request.json() as { username: string; password: string };
if (username === 'admin' && password === 'password123') {
return HttpResponse.json({
token: 'mock-jwt-token',
user: { id: 'usr_123', name: 'Admin User' }
});
}
return HttpResponse.json(
{ error: 'Invalid credentials' },
{ status: 401 }
);
}),
// Example with query parameters
http.get('https://api.example.com/products', ({ request }) => {
// Parse the URL to access query parameters
const url = new URL(request.url);
// Get query parameters
const category = url.searchParams.get('category');
const sort = url.searchParams.get('sort') || 'price';
const page = url.searchParams.get('page') || '1';
const limit = url.searchParams.get('limit') || '10';
// Validate parameters
if (limit && parseInt(limit) > 100) {
return HttpResponse.json(
{ error: 'Limit cannot exceed 100' },
{ status: 400 }
);
}
// Generate mock response based on parameters
const mockProducts = Array.from({ length: parseInt(limit) }, (_, i) => ({
id: i + 1,
name: `Product ${i + 1}${category ? ` in ${category}` : ''}`,
price: Math.floor(Math.random() * 100),
category: category || 'general',
}));
// Sort products if sort parameter provided
if (sort === 'price') {
mockProducts.sort((a, b) => a.price - b.price);
} else if (sort === 'name') {
mockProducts.sort((a, b) => a.name.localeCompare(b.name));
}
return HttpResponse.json({
products: mockProducts,
page: parseInt(page),
totalPages: 5,
itemsPerPage: parseInt(limit),
sortBy: sort,
});
}),
http.get('https://api.example.com/transactions', ({ request }) => {
const url = new URL(request.url);
// Get query parameters
const userId = url.searchParams.get('userId');
const state = url.searchParams.get('state');
const statusCode = url.searchParams.get('statusCode');
// Filter transactions based on query parameters
let filteredTransactions = [...transactionDummyData];
if (userId) {
filteredTransactions = filteredTransactions.filter(
tx => tx.user.toString() === userId
);
}
if (state) {
filteredTransactions = filteredTransactions.filter(
tx => tx.state.toLowerCase() === state.toLowerCase()
);
}
if (statusCode) {
filteredTransactions = filteredTransactions.filter(
tx => tx.pspStatusCode.toString() === statusCode
);
}
return HttpResponse.json({
transactions: filteredTransactions,
count: filteredTransactions.length
});
}),
];