@dbs-portal/core-api
Version:
HTTP client and API utilities for DBS Portal
165 lines • 4.79 kB
JavaScript
/**
* Default MSW handlers for common API patterns
*/
import { createGetHandler, createPostHandler } from './factory';
/**
* Health check handler
*/
const healthHandler = createGetHandler('/api/health', async () => ({
success: true,
data: {
status: 'healthy',
timestamp: new Date().toISOString(),
version: '1.0.0',
environment: 'mock',
},
message: 'Service is healthy',
}));
/**
* Version info handler
*/
const versionHandler = createGetHandler('/api/version', async () => ({
success: true,
data: {
version: '1.0.0',
buildDate: new Date().toISOString(),
commit: 'mock-commit',
environment: 'mock',
},
message: 'Version information',
}));
/**
* Configuration handler
*/
const configHandler = createGetHandler('/api/configuration', async () => ({
success: true,
data: {
features: {
authentication: true,
fileUpload: true,
notifications: true,
realtime: false,
},
limits: {
maxFileSize: 10485760, // 10MB
maxRequestSize: 1048576, // 1MB
rateLimit: 1000,
},
endpoints: {
auth: '/api/auth',
users: '/api/users',
files: '/api/files',
},
},
message: 'Application configuration',
}));
/**
* Echo handler for testing
*/
const echoHandler = createPostHandler('/api/echo', async (request) => ({
success: true,
data: {
method: request.method,
url: request.url.toString(),
headers: Object.fromEntries(request.headers.entries()),
body: request.body,
params: request.params,
query: request.query,
timestamp: new Date().toISOString(),
},
message: 'Echo response',
}));
/**
* Time handler
*/
const timeHandler = createGetHandler('/api/time', async () => ({
success: true,
data: {
timestamp: new Date().toISOString(),
unix: Math.floor(Date.now() / 1000),
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
},
message: 'Current time',
}));
/**
* Default handlers array
*/
export const defaultHandlers = [
healthHandler,
versionHandler,
configHandler,
echoHandler,
timeHandler,
];
/**
* Create default handlers with custom base path
*/
export function createDefaultHandlers(basePath = '/api') {
return [
createGetHandler(`${basePath}/health`, async () => ({
success: true,
data: {
status: 'healthy',
timestamp: new Date().toISOString(),
version: '1.0.0',
environment: 'mock',
},
message: 'Service is healthy',
})),
createGetHandler(`${basePath}/version`, async () => ({
success: true,
data: {
version: '1.0.0',
buildDate: new Date().toISOString(),
commit: 'mock-commit',
environment: 'mock',
},
message: 'Version information',
})),
createGetHandler(`${basePath}/configuration`, async () => ({
success: true,
data: {
features: {
authentication: true,
fileUpload: true,
notifications: true,
realtime: false,
},
limits: {
maxFileSize: 10485760, // 10MB
maxRequestSize: 1048576, // 1MB
rateLimit: 1000,
},
endpoints: {
auth: `${basePath}/auth`,
users: `${basePath}/users`,
files: `${basePath}/files`,
},
},
message: 'Application configuration',
})),
createPostHandler(`${basePath}/echo`, async (request) => ({
success: true,
data: {
method: request.method,
url: request.url.toString(),
headers: Object.fromEntries(request.headers.entries()),
body: request.body,
params: request.params,
query: request.query,
timestamp: new Date().toISOString(),
},
message: 'Echo response',
})),
createGetHandler(`${basePath}/time`, async () => ({
success: true,
data: {
timestamp: new Date().toISOString(),
unix: Math.floor(Date.now() / 1000),
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
},
message: 'Current time',
})),
];
}
//# sourceMappingURL=default-handlers.js.map