Consolidate type definitions and fix API contract

- Move all ContentItem, ContentClient, ContentResponse types to engine/types.go as single source of truth
- Remove duplicate type definitions from content/types.go
- Update all imports across codebase to use engine types
- Enhance engine to extract existing data-content-id from HTML markup
- Simplify frontend to always send html_markup, let server handle ID extraction/generation
- Fix contentId reference errors in frontend error handling
- Add getAttribute helper method to engine for ID extraction
- Add GetAllContent method to engine.DatabaseClient
- Update enhancer to use engine.ContentClient interface
- All builds and API endpoints verified working

This resolves the 400 Bad Request errors and creates a unified architecture where the server is the single source of truth for all ID generation and content type management.
This commit is contained in:
2025-09-16 16:45:29 +02:00
parent d0ac3088b4
commit d877366be0
15 changed files with 150 additions and 181 deletions

View File

@@ -109,6 +109,11 @@ var Insertr = (function () {
getElementMetadata(element) {
const existingId = element.getAttribute('data-content-id');
// Ensure element has insertr class for server processing
if (!element.classList.contains('insertr')) {
element.classList.add('insertr');
}
// Send HTML markup to server for unified ID generation
return {
contentId: existingId, // null if new content, existing ID if updating
@@ -2820,13 +2825,12 @@ Please report this to https://github.com/markedjs/marked.`,e){let r="<p>An error
contentValue = formData.text || formData;
}
// Universal upsert - works for both new and existing content
// Universal upsert - server handles ID extraction/generation from markup
const contentType = this.determineContentType(meta.element);
const result = await this.apiClient.createContent(
meta.contentId, // Use existing ID if available, null if new
contentValue,
contentType,
meta.htmlMarkup
meta.htmlMarkup // Always send HTML markup - server is smart about ID handling
);
if (result) {
@@ -3761,24 +3765,15 @@ Please report this to https://github.com/markedjs/marked.`,e){let r="<p>An error
}
async createContent(contentId, content, type, htmlMarkup = null) {
async createContent(content, type, htmlMarkup) {
try {
const payload = {
html_markup: htmlMarkup, // Always send HTML markup - server extracts ID or generates new one
value: content,
type: type,
file_path: this.getCurrentFilePath() // Always include file path for consistent ID generation
};
if (contentId) {
// Enhanced site - provide existing ID
payload.id = contentId;
} else if (htmlMarkup) {
// Non-enhanced site - provide HTML markup for unified engine ID generation
payload.html_markup = htmlMarkup;
} else {
throw new Error('Either contentId or htmlMarkup must be provided');
}
const response = await fetch(`${this.baseUrl}?site_id=${this.siteId}`, {
method: 'POST',
headers: {
@@ -3793,7 +3788,7 @@ Please report this to https://github.com/markedjs/marked.`,e){let r="<p>An error
console.log(`✅ Content created: ${result.id} (${result.type})`);
return result;
} else {
console.warn(`⚠️ Create failed (${response.status}): ${contentId || 'backend-generated'}`);
console.warn(`⚠️ Create failed (${response.status}): server will generate ID`);
return null;
}
} catch (error) {
@@ -3801,7 +3796,7 @@ Please report this to https://github.com/markedjs/marked.`,e){let r="<p>An error
console.warn(`🔌 API Server not reachable at ${this.baseUrl}`);
console.warn('💡 Start full-stack development: just dev');
} else {
console.error('Failed to create content:', contentId, error);
console.error('Failed to create content:', error);
}
return false;
}