3.0 KiB
Base API Utilities
plugs/api/base.ts- Core reusable API functions.
Overview
The base module provides generic CRUD operations that other API modules build upon. All functions return Promise and handle errors gracefully by resolving with falsy values instead of rejecting.
Functions
queryPage
Paginated query with automatic empty result handling.
queryPage(url: string, example?: object, noMsg?: boolean, noLoading?: boolean): Promise<PageResult>
Behavior:
- Resolves with
PageResultinstance when response is falsy - Resolves with
responsedirectly when truthy
Example from plugs/api/dictionary.ts:
export const list = (example) => {
return queryPage(root, example);
};
queryList
List query returning array.
queryList(url: string, example?: object, noMsg?: boolean, noLoading?: boolean): Promise<any[]>
Behavior:
- Resolves with
[]when response is falsy - Resolves with
responsedirectly when truthy
Example from plugs/api/role.ts:
export const list = (example) => {
return queryList(root, example);
};
getById
Fetch single resource by ID.
getById(url: string, id: string | number): Promise<object>
Behavior:
- Resolves with
{}when response is falsy - Resolves with
responsedirectly when truthy
Example:
// URL pattern: resource/id
getById('user', 123) // GET user/123
save
Create new resource (POST).
save(url: string, data: object): Promise<any>
Behavior:
- Resolves with
responsewhen truthy - Resolves with
falseon error
update
Update existing resource (PUT).
update(url: string, data: object): Promise<any>
Behavior:
- Resolves with
responsewhen truthy - Resolves with
falseon error
deleteById
Delete resource by ID.
deleteById(url: string, id: string | number): Promise<any>
Behavior:
- Uses
delate(HTTP DELETE) - Resolves with
responsewhen truthy - Resolves with
falseon error
exportCsv
Recursive CSV export with pagination.
exportCsv(url: string, head: string[], formatter: (row: any) => string, param?: object, content?: string[]): Promise<string>
Behavior:
- Fetches pages until no more data
- Default page size:
10000 - Concatenates rows with
\r\n
Download Utilities
downloadText(name: string, text: string): void
downloadBlob(name: string, blob: Blob): void
downloadFile(name: string, href: string): void
Anti-Patterns
-
Never rejects - All functions resolve, never reject on error. This makes debugging difficult.
-
Uses
any- No typed responses. Consider adding TypeScript generics. -
debuggerstatement - Line 114 ofbase.tscontainsdebugger;which will pause execution in browser devtools. -
Swallows errors silently - Error cases resolve with
false,[], or{}making it impossible to distinguish between "not found" and "network error".