@dbs-portal/tool-mock
Version:
API mocking toolkit using MSW for DBS Portal development workflows
253 lines • 6.78 kB
TypeScript
/**
* Handler management types
*/
import type { RequestHandler } from 'msw';
import type { MockRequest, MockResponseFactory } from '../core/types';
import type { MockDataFactory } from '../factories';
/**
* CRUD handler options
*/
export interface CrudHandlerOptions<T = any> {
/** Base URL path */
basePath: string;
/** Data factory for creating mock items */
dataFactory: MockDataFactory<T>;
/** Initial data set */
initialData?: T[];
/** Pagination configuration */
pagination?: PaginationConfig;
/** Validation function for create/update operations */
validate?: (data: Partial<T>) => string[] | null;
/** Custom ID field name (default: 'id') */
idField?: keyof T;
/** Custom response delay */
delay?: number | [number, number];
/** Custom headers */
headers?: Record<string, string>;
/** Enable soft delete (sets deleted flag instead of removing) */
softDelete?: boolean;
/** Deleted field name for soft delete (default: 'deleted') */
deletedField?: keyof T;
}
/**
* Pagination configuration
*/
export interface PaginationConfig {
/** Default page size */
defaultPageSize: number;
/** Maximum page size */
maxPageSize: number;
/** Total items (for static data) */
totalItems?: number;
}
/**
* Auth handler options
*/
export interface AuthHandlerOptions {
/** Base path for auth endpoints */
basePath: string;
/** Mock users for authentication */
users?: MockUser[];
/** JWT token expiry time */
tokenExpiry?: string;
/** Refresh token expiry time */
refreshTokenExpiry?: string;
/** Enable user registration */
enableRegistration?: boolean;
/** Enable password reset */
enablePasswordReset?: boolean;
/** Custom token generation */
tokenGenerator?: (user: MockUser) => string;
/** Custom user validation */
userValidator?: (credentials: LoginCredentials) => MockUser | null;
}
/**
* Mock user for authentication
*/
export interface MockUser {
id: string;
email: string;
password: string;
firstName?: string;
lastName?: string;
roles?: string[];
permissions?: string[];
isActive?: boolean;
[key: string]: any;
}
/**
* Login credentials
*/
export interface LoginCredentials {
email: string;
password: string;
}
/**
* Auth response data
*/
export interface AuthResponse {
user: Omit<MockUser, 'password'>;
token: string;
refreshToken?: string;
expiresIn?: number;
}
/**
* Handler factory function type
*/
export type HandlerFactory<TOptions = any> = (options: TOptions) => RequestHandler[];
/**
* Custom handler options
*/
export interface CustomHandlerOptions<T = any> {
/** HTTP method */
method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
/** URL path or pattern */
path: string;
/** Response factory */
response: MockResponseFactory<T>;
/** Response delay */
delay?: number | [number, number];
/** Success status code */
status?: number;
/** Custom headers */
headers?: Record<string, string>;
/** Authentication requirements */
auth?: AuthRequirements;
}
/**
* Authentication requirements
*/
export interface AuthRequirements {
/** Require authentication */
requireAuth?: boolean;
/** Required roles */
requiredRoles?: string[];
/** Required permissions */
requiredPermissions?: string[];
/** Custom auth validator */
validator?: (request: MockRequest) => boolean;
}
/**
* Handler registry interface
*/
export interface HandlerRegistry {
/** Register handlers */
register(...handlers: RequestHandler[]): void;
/** Unregister handlers */
unregister(...handlers: RequestHandler[]): void;
/** Get all registered handlers */
getHandlers(): RequestHandler[];
/** Clear all handlers */
clear(): void;
/** Get handlers by tag */
getByTag(tag: string): RequestHandler[];
/** Tag handlers */
tag(tag: string, ...handlers: RequestHandler[]): void;
}
/**
* Handler manager interface
*/
export interface HandlerManager {
/** Add handlers */
add(...handlers: RequestHandler[]): void;
/** Remove handlers */
remove(...handlers: RequestHandler[]): void;
/** Replace all handlers */
replace(handlers: RequestHandler[]): void;
/** Get current handlers */
getHandlers(): RequestHandler[];
/** Enable/disable handlers */
setEnabled(enabled: boolean): void;
/** Check if enabled */
isEnabled(): boolean;
}
/**
* File upload handler options
*/
export interface FileUploadHandlerOptions {
/** Base path for file endpoints */
basePath: string;
/** Allowed file types */
allowedTypes?: string[];
/** Maximum file size (bytes) */
maxSize?: number;
/** Upload progress simulation */
simulateProgress?: boolean;
/** Upload delay */
delay?: number | [number, number];
/** Storage simulation */
storage?: 'memory' | 'mock-s3' | 'mock-local';
}
/**
* Bulk operation handler options
*/
export interface BulkHandlerOptions<T = any> {
/** Base path */
basePath: string;
/** Data factory */
dataFactory: MockDataFactory<T>;
/** Supported operations */
operations?: ('create' | 'update' | 'delete')[];
/** Batch size limit */
batchSizeLimit?: number;
/** Validation function */
validate?: (items: Partial<T>[]) => string[] | null;
}
/**
* Search handler options
*/
export interface SearchHandlerOptions<T = any> {
/** Base path */
basePath: string;
/** Data source */
dataSource: T[] | (() => T[]);
/** Searchable fields */
searchFields: (keyof T)[];
/** Filterable fields */
filterFields?: (keyof T)[];
/** Sortable fields */
sortFields?: (keyof T)[];
/** Default sort */
defaultSort?: {
field: keyof T;
order: 'asc' | 'desc';
};
/** Pagination */
pagination?: PaginationConfig;
}
/**
* WebSocket handler options
*/
export interface WebSocketHandlerOptions {
/** WebSocket URL pattern */
url: string | RegExp;
/** Message handlers */
messageHandlers?: Record<string, (data: any) => any>;
/** Connection simulation */
connectionDelay?: number;
/** Disconnect simulation */
disconnectRate?: number;
}
/**
* Handler metadata
*/
export interface HandlerMetadata {
/** Handler name */
name?: string;
/** Handler description */
description?: string;
/** Handler tags */
tags?: string[];
/** Handler version */
version?: string;
/** Handler author */
author?: string;
}
/**
* Tagged handler
*/
export interface TaggedHandler {
handler: RequestHandler;
metadata: HandlerMetadata;
}
//# sourceMappingURL=types.d.ts.map