feat(form): ✨ enhance form functionality and API integration
* Refactor key handling to use uniqueKeyField * Add reset functionality to clear dirty state after save * Introduce new API call specifications for REST and resolve * Implement predefined wrappers for dialogs and popovers * Update todo list to reflect completed tasks
This commit is contained in:
72
src/Former/FormerResolveSpecAPI.ts
Normal file
72
src/Former/FormerResolveSpecAPI.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
import type { FormerAPICallType } from './Former.types';
|
||||
|
||||
interface ResolveSpecRequest {
|
||||
operation: 'read' | 'create' | 'update' | 'delete';
|
||||
data?: Record<string, any>;
|
||||
options?: {
|
||||
preload?: string[];
|
||||
columns?: string[];
|
||||
filters?: Array<{ column: string; operator: string; value: any }>;
|
||||
sort?: string[];
|
||||
limit?: number;
|
||||
offset?: number;
|
||||
customOperators?: any[];
|
||||
computedColumns?: any[];
|
||||
};
|
||||
}
|
||||
|
||||
function FormerResolveSpecAPI(options: {
|
||||
url: string;
|
||||
authToken: string;
|
||||
signal?: AbortSignal;
|
||||
fetchOptions?: Partial<RequestInit>;
|
||||
}): FormerAPICallType {
|
||||
return async (mode, request, value, key) => {
|
||||
const baseUrl = options.url.replace(/\/$/, '');
|
||||
|
||||
// Build URL: /[schema]/[table_or_entity]/[id]
|
||||
let url = `${baseUrl}`;
|
||||
if (request !== 'insert' && key) {
|
||||
url = `${url}/${key}`;
|
||||
}
|
||||
|
||||
// Build ResolveSpec request body
|
||||
const resolveSpecRequest: ResolveSpecRequest = {
|
||||
operation:
|
||||
mode === 'read'
|
||||
? 'read'
|
||||
: request === 'delete'
|
||||
? 'delete'
|
||||
: request === 'update'
|
||||
? 'update'
|
||||
: 'create',
|
||||
};
|
||||
|
||||
if (mode === 'mutate') {
|
||||
resolveSpecRequest.data = value;
|
||||
}
|
||||
|
||||
const fetchOptions: RequestInit = {
|
||||
cache: 'no-cache',
|
||||
signal: options.signal,
|
||||
...options.fetchOptions,
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${options.authToken}`,
|
||||
...options.fetchOptions?.headers,
|
||||
},
|
||||
body: JSON.stringify(resolveSpecRequest),
|
||||
};
|
||||
|
||||
const response = await fetch(url, fetchOptions);
|
||||
if (!response.ok) {
|
||||
throw new Error(`API request failed with status ${response.status}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
return data as any;
|
||||
};
|
||||
}
|
||||
|
||||
export { FormerResolveSpecAPI, type ResolveSpecRequest };
|
||||
Reference in New Issue
Block a user