Skip to content

@tasco-fe/utils ​

Helper TypeScript thuần, không phụ thuộc Nuxt hay Vue: lỗi chuẩn hoá AppError, kiểu phân trang, thiết lập bảng. Cách dùng: AppError & phân loại, Lỗi 422 và form.

Hàm ​

clearTableSettings() ​

ts
function clearTableSettings(settingsKey, options?): void;

Xoá thiết lập đã lưu của một bảng (người dùng đặt lại về cấu hình gốc).

Tham số ​

Tham sốKiểu
settingsKeystring
optionsTableSettingsStorageOptions

Trả về ​

void


errorSeverity() ​

ts
function errorSeverity(input): ErrorSeverity;

Mức độ nghiêm trọng để chọn kiểu hiển thị: 5xx và mất mạng → error; 403, 422 → warning; còn lại → error.

Tham số ​

Tham sốKiểu
inputunknown

Trả về ​

ErrorSeverity


getFieldErrors() ​

ts
function getFieldErrors(input): Record<string, string[]>;

Trích field errors từ AppError.data (dạng { errors|fields: { field: string|string[] } }) để gán vào CForm. Trả object rỗng nếu không có.

Tham số ​

Tham sốKiểu
inputunknown

Trả về ​

Record<string, string[]>


getTableSettings() ​

ts
function getTableSettings(settingsKey, options?): TableSettings | null;

Thiết lập đã lưu của một bảng; chưa lưu, dữ liệu hỏng hoặc không truy cập được storage → null.

Tham số ​

Tham sốKiểu
settingsKeystring
optionsTableSettingsStorageOptions

Trả về ​

TableSettings | null


isAppError() ​

ts
function isAppError(input): input is AppError;

Type guard kiểm tra input đã là AppError. Không chuẩn hoá — lỗi bất kỳ thì gọi toAppError.

Tham số ​

Tham sốKiểu
inputunknown

Trả về ​

input is AppError


isAuthError() ​

ts
function isAuthError(input): boolean;

401 — chưa/đã hết đăng nhập.

Tham số ​

Tham sốKiểu
inputunknown

Trả về ​

boolean


isForbidden() ​

ts
function isForbidden(input): boolean;

403 — không đủ quyền.

Tham số ​

Tham sốKiểu
inputunknown

Trả về ​

boolean


isNetworkError() ​

ts
function isNetworkError(input): boolean;

Không có mã HTTP (mất mạng, timeout, lỗi JS chưa bắt…).

Tham số ​

Tham sốKiểu
inputunknown

Trả về ​

boolean


isServerError() ​

ts
function isServerError(input): boolean;

5xx — lỗi phía server.

Tham số ​

Tham sốKiểu
inputunknown

Trả về ​

boolean


isTableSortOrder() ​

ts
function isTableSortOrder(value): value is TableSortOrder;

Type guard kiểm tra giá trị là chiều sắp xếp hợp lệ ('ascend' | 'descend').

Tham số ​

Tham sốKiểu
valueunknown

Trả về ​

value is TableSortOrder


isValidation() ​

ts
function isValidation(input): boolean;

422 — lỗi validate dữ liệu (thường kèm field errors).

Tham số ​

Tham sốKiểu
inputunknown

Trả về ​

boolean


parseTableSettings() ​

ts
function parseTableSettings(raw): TableSettings | null;

Đọc thiết lập từ dữ liệu không tin cậy (JSON trong storage, response server). Sai cấu trúc hoặc khác version → null để bảng dùng cấu hình gốc. Kết quả là object mới, chỉ gồm các field đã biết.

Tham số ​

Tham sốKiểu
rawunknown

Trả về ​

TableSettings | null


setTableSettings() ​

ts
function setTableSettings(
   settingsKey, 
   settings, 
   options?
): void;

Lưu thiết lập của một bảng (kèm version). Storage đầy hoặc bị chặn (vd Safari private) → bỏ qua: thiết lập vẫn áp dụng trong phiên, chỉ không nhớ được cho lần mở sau.

Tham số ​

Tham sốKiểu
settingsKeystring
settingsTableSettings
optionsTableSettingsStorageOptions

Trả về ​

void


tableSettingsKeyOf() ​

ts
function tableSettingsKeyOf(settingsKey): string;

Key trong storage của một bảng: tasco:table:<settingsKey>.

Tham số ​

Tham sốKiểu
settingsKeystring

Trả về ​

string


toAppError() ​

ts
function toAppError(input): AppError;

Chuẩn hoá bất kỳ giá trị lỗi nào (ofetch response error, Error, string...) thành AppError.

Tham số ​

Tham sốKiểu
inputunknown

Trả về ​

AppError

Interface ​

AppErrorOptions ​

Tuỳ chọn khi tạo AppError.

Thuộc tính ​

Thuộc tínhKiểuMô tả
cause?unknownLỗi gốc, giữ lại để debug (chuẩn Error.cause).
code?stringMã lỗi nghiệp vụ do backend trả về, nếu có.
data?unknownBody response gốc, vd { message, errors } của lỗi 422.
status?numberMã HTTP khi lỗi đến từ response.

Paginated ​

Một trang dữ liệu danh sách — hình dạng chuẩn API danh sách trả về và fetcher của useTable phải trả.

Tham số kiểu ​

Tham số kiểu
T

Thuộc tính ​

Thuộc tínhKiểuMô tả
itemsT[]Bản ghi của trang đang xem.
totalnumberTổng số bản ghi khớp truy vấn trên mọi trang — dùng để tính số trang.

TableSettings ​

Thiết lập bảng của người dùng. Key cột = key → dataIndex của cột.

Thuộc tính ​

Thuộc tínhKiểuMô tả
columnOrderstring[]Key các cột cấu hình được theo thứ tự hiển thị; rỗng = đúng thứ tự khai báo trong columns.
defaultSortTableSort | nullSắp xếp áp dụng khi mở bảng; null = tắt (dữ liệu theo thứ tự mặc định của backend).
hiddenColumnsstring[]Key các cột đang ẩn.

TableSettingsStorageOptions ​

Tuỳ chọn chung của các hàm đọc/ghi thiết lập bảng.

Thuộc tính ​

Thuộc tínhKiểuMô tả
storage?TableSettingsStorageNơi đọc/ghi; mặc định localStorage của trình duyệt.

TableSort ​

Sắp xếp theo một cột.

Thuộc tính ​

Thuộc tínhKiểuMô tả
fieldstringdataIndex của cột — giá trị a-table phát ở sorter.field, useTable gửi lên qua sortField.
orderTableSortOrderChiều sắp xếp.

Kiểu ​

ErrorSeverity ​

ts
type ErrorSeverity = "info" | "warning" | "error";

Mức độ nghiêm trọng của lỗi, dùng để chọn cách hiển thị.


Maybe ​

ts
type Maybe<T> = T | null | undefined;

Giá trị có thể null hoặc undefined, thường là input chưa chuẩn hoá.

Tham số kiểu ​

Tham số kiểu
T

Nullable ​

ts
type Nullable<T> = T | null;

Giá trị có thể vắng, biểu diễn bằng null — quy ước DTO: "không có" là T | null.

Tham số kiểu ​

Tham số kiểu
T

TableSettingsStorage ​

ts
type TableSettingsStorage = Pick<Storage, "getItem" | "setItem" | "removeItem">;

Phần Web Storage cần dùng — localStorage, sessionStorage hoặc bản giả khi test.


TableSortOrder ​

ts
type TableSortOrder = "ascend" | "descend";

Chiều sắp xếp — cùng giá trị với TableQuery.sortOrder và sorter của a-table.

Lớp ​

AppError ​

Lỗi chuẩn hoá của framework. Ở client, mọi lỗi quy về lớp này qua toAppError; phân loại bằng isAuthError, isForbidden, isValidation, isServerError, isNetworkError.

Kế thừa ​

  • Error

Hàm khởi tạo ​

Hàm khởi tạo ​
ts
new AppError(message, options?): AppError;
Tham số ​
Tham sốKiểu
messagestring
optionsAppErrorOptions
Trả về ​

AppError

Ghi đè ​
ts
Error.constructor

Thuộc tính ​

Thuộc tínhBổ ngữKiểuMô tả
code?readonlystringMã lỗi nghiệp vụ do backend trả về, nếu có.
data?readonlyunknownBody response gốc — lỗi 422 đọc field errors từ đây bằng getFieldErrors.
status?readonlynumberMã HTTP; undefined nghĩa là không có response (mất mạng, timeout, lỗi JS).