99 lines
2.8 KiB
TypeScript
99 lines
2.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 { 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',
|
|
}));
|
|
|
|
console.log(1234, mockProducts)
|
|
|
|
// 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,
|
|
});
|
|
}),
|
|
];
|