import * as msw from 'msw'; /** * Helper khớp với envelope protocol của @aseansc-admin/sea-http. * * sea-http gửi mọi request dạng POST với cùng một URL endpoint, phân biệt thao tác * bằng `body.authenType` (API thường) hoặc `body.command` (login). Vì vậy KHÔNG * match được bằng URL path — phải đọc body rồi route theo operation key. * * Xem README section "Testing with MSW" để biết ví dụ đầy đủ. */ /** * Bọc data vào response envelope mà sea-http mong đợi (body.status === 'OK'). * sea-http tự unwrap và trả về `body.data` cho caller. * * Dùng trực tiếp khi cần tuỳ chỉnh envelope ngoài phạm vi của ascOperation(): * @example * return HttpResponse.json(ascOk({ meetings: [] })); */ declare function ascOk(data: T): { header: { resType: string; status: string; }; body: { status: string; data: T; }; }; /** * Bọc data vào error envelope — sea-http sẽ ném `AscApiError` khi status !== 'OK'. * * @example * return HttpResponse.json(ascFail('UNAUTHORIZED')); * return HttpResponse.json(ascFail('VALIDATION_ERROR', { field: 'email', msg: 'invalid' })); */ declare function ascFail(status: string, data?: unknown): { header: { resType: string; status: string; }; body: { status: string; data: unknown; }; }; type AscMockResolver = (input: { /** `body.body.data` từ request — payload thực tế consumer gửi vào */ data: unknown; /** Toàn bộ request body (envelope đầy đủ) */ body: unknown; /** Request gốc của MSW */ request: Request; }) => unknown | Promise; interface AscMockOperationOptions { /** * Lỗi nghiệp vụ trong envelope: sea-http ném `AscApiError` khi `body.status !== 'OK'`. * Resolver VẪN được gọi — giá trị trả về sẽ vào `body.data` của envelope lỗi. * * @example * ascOperation('getUser', () => ({ reason: 'Not found' }), { errorStatus: 'USER_NOT_FOUND' }) * // → AscApiError { status: 'USER_NOT_FOUND', data: { reason: 'Not found' } } */ errorStatus?: string; /** * HTTP-level error status code (401 | 403 | 404 | 500 ...). * Resolver KHÔNG được gọi. Phản hồi bypass envelope hoàn toàn. * * Hành vi của `ascErrorInterceptor` khi nhận HTTP error: * - **401** → xoá session, redirect về `/login` * - **403** → gọi `ASC_HTTP_ERROR_HANDLER` * - **500** → retry tối đa 3 lần (delay 1s / 2s / 3s) rồi gọi `ASC_HTTP_ERROR_HANDLER` * - **0** → dùng `networkError: true` thay vì httpStatus * * @example * ascOperation('getUser', () => null, { httpStatus: 401 }) * ascOperation('getReport', () => null, { httpStatus: 500, delayMs: 200 }) */ httpStatus?: number; /** * Giả lập mất kết nối mạng — Angular nhận `HttpErrorResponse` với `status === 0`. * `ascErrorInterceptor` gọi `ASC_HTTP_ERROR_HANDLER` với thông báo 'Network Error'. * Resolver KHÔNG được gọi. * * @example * ascOperation('getData', () => null, { networkError: true }) */ networkError?: boolean; /** * Giả lập độ trễ mạng (ms) trước khi trả response. * Hữu ích để kiểm tra loading indicator và skeleton UI. * * @example * ascOperation('heavyReport', () => rows, { delayMs: 2000 }) */ delayMs?: number; } interface AscMock { operationKey: string; resolver: AscMockResolver; options: AscMockOperationOptions; } /** * Khai báo mock cho một operation (authenType hoặc command). * Gom nhiều operations lại trong `createAscHandler()`. * * @param operationKey `body.authenType` (API thường) hoặc `body.command` (login) * @param resolver Hàm trả về data — bọc tự động trong OK envelope * (không gọi khi httpStatus hoặc networkError được đặt) * @param options errorStatus | httpStatus | networkError | delayMs */ declare function ascOperation(operationKey: string, resolver: AscMockResolver, options?: AscMockOperationOptions): AscMock; /** * Tạo một MSW POST handler cho endpoint của sea-http. * Handler tự route theo `authenType`/`command` trong body đến đúng mock. * * @param endpoint URL pattern khớp với baseUrl trong provideAscHttp. * Dùng wildcard để khớp mọi origin: `'*\/api'` * Hoặc dùng URL chính xác: `'https://api.example.com/api'` * @param mocks Danh sách operations tạo bằng ascOperation() * * Thứ tự ưu tiên khi xử lý một mock: * 1. `networkError: true` → trả lỗi mạng ngay (resolver không gọi) * 2. `httpStatus` → trả HTTP error response (resolver không gọi) * 3. `errorStatus` → gọi resolver, bọc kết quả trong error envelope * 4. (mặc định) → gọi resolver, bọc kết quả trong OK envelope */ declare function createAscHandler(endpoint: string, mocks: AscMock[]): msw.HttpHandler; export { ascFail, ascOk, ascOperation, createAscHandler }; export type { AscMock, AscMockOperationOptions, AscMockResolver };