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:
50
src/Former/FormerRestHeadSpecAPI.ts
Normal file
50
src/Former/FormerRestHeadSpecAPI.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import type { FormerAPICallType } from './Former.types';
|
||||
|
||||
function FormerRestHeadSpecAPI(options: {
|
||||
url: string;
|
||||
authToken: string;
|
||||
signal?: AbortSignal;
|
||||
fetchOptions?: Partial<RequestInit>;
|
||||
}): FormerAPICallType {
|
||||
return async (mode, request, value, key) => {
|
||||
const baseUrl = options.url ?? ''; // Remove trailing slashes
|
||||
let url = baseUrl;
|
||||
let fetchOptions: RequestInit = {
|
||||
cache: 'no-cache',
|
||||
signal: options.signal,
|
||||
...options.fetchOptions,
|
||||
method:
|
||||
mode === 'read'
|
||||
? 'GET'
|
||||
: request === 'delete'
|
||||
? 'DELETE'
|
||||
: request === 'update'
|
||||
? 'PUT'
|
||||
: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${options.authToken}`,
|
||||
...options.fetchOptions?.headers,
|
||||
},
|
||||
body: mode === 'mutate' && request !== 'delete' ? JSON.stringify(value) : undefined,
|
||||
};
|
||||
|
||||
if (request !== 'insert') {
|
||||
url = `${baseUrl}/${key}`;
|
||||
}
|
||||
|
||||
const response = await fetch(url, fetchOptions);
|
||||
if (!response.ok) {
|
||||
throw new Error(`API request failed with status ${response.status}`);
|
||||
}
|
||||
|
||||
if (mode === 'read') {
|
||||
const data = await response.json();
|
||||
return data as any;
|
||||
} else {
|
||||
return value as any;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export { FormerRestHeadSpecAPI };
|
||||
Reference in New Issue
Block a user