109 lines
3.1 KiB
TypeScript
109 lines
3.1 KiB
TypeScript
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,
|
|
});
|
|
}),
|
|
];
|