test-api-put.jsā¢5.52 kB
#!/usr/bin/env node
/**
* API PUT Test - Test actual composition creation
*/
import { readFileSync } from 'fs';
import { resolve, dirname } from 'path';
import { fileURLToPath } from 'url';
const currentDir = dirname(fileURLToPath(import.meta.url));
const tokenPath = resolve(currentDir, 'correct-jwt-new.txt');
const jwtToken = readFileSync(tokenPath, 'utf8').trim();
async function testPUTAPI() {
console.log('š Testing Composition Creation via PUT\n');
const baseURL = "https://api.digitalpages.com.br";
const projectUID = "36c92686-c494-ec11-a22a-dc984041c95d";
// Create a test composition
const compositionData = {
version: "1.1",
metadata: {
title: "Test Composition via API",
description: "Created directly via HTTP API without browser automation",
thumb: null,
tags: ["test", "api", "automated"]
},
interface: {
content_language: "pt_br",
index_option: "buttons",
font_family: "Lato",
show_summary: "disabled",
finish_btn: "disabled"
},
structure: [
{
id: generateUID(),
type: "head-1",
content: "<h1>API Test Composition</h1>",
background_color: "#FFFFFF",
padding_top: 20,
padding_bottom: 20
},
{
id: generateUID(),
type: "text-1",
content: "<p>This composition was created using direct HTTP API calls.</p>",
background_color: "#FFFFFF",
padding_top: 0,
padding_bottom: 0
},
{
id: generateUID(),
type: "text-1",
content: "<p>No browser automation was used - just pure API calls!</p>",
background_color: "#FFFFFF",
padding_top: 0,
padding_bottom: 0
}
]
};
const compositionJSON = JSON.stringify(compositionData, null, 2);
console.log('š Composition Data:');
console.log(` - Title: ${compositionData.metadata.title}`);
console.log(` - Widgets: ${compositionData.structure.length}`);
console.log(` - Size: ${compositionJSON.length} bytes`);
// Create FormData with the composition file
const formData = new FormData();
const blob = new Blob([compositionJSON], { type: 'application/json' });
formData.append('file', blob, 'composition.rdpcomposer');
// Generate a unique UID for this composition
const compositionUID = generateUID();
const url = `${baseURL}/storage/v1.0/content?uid=${compositionUID}&manual_project_uid=${projectUID}`;
console.log('\nš API Request:');
console.log(` - Method: PUT`);
console.log(` - URL: ${url}`);
console.log(` - Content-Type: multipart/form-data`);
console.log(` - File: composition.rdpcomposer`);
try {
console.log('\nš¤ Sending request...');
const response = await fetch(url, {
method: 'PUT',
headers: {
'Authorization': `Bearer ${jwtToken}`,
'User-Agent': 'EuConquisto-MCP-Client/1.0',
// Don't set Content-Type - let fetch set it with boundary for multipart
},
body: formData
});
console.log(`\nš„ Response received:`);
console.log(` - Status: ${response.status} ${response.statusText}`);
const responseText = await response.text();
let responseData;
try {
responseData = JSON.parse(responseText);
console.log(` - Response: ${JSON.stringify(responseData, null, 2)}`);
} catch {
console.log(` - Response: ${responseText}`);
}
if (response.ok) {
console.log('\nā
SUCCESS! Composition created via API');
console.log(` - Composition UID: ${compositionUID}`);
console.log(' - Browser automation successfully bypassed');
console.log(' - This proves the HTTP API approach works!');
// Try to construct the view URL
const viewURL = `https://composer.euconquisto.com/#/embed/auth-with-token/pt_br/view/${projectUID}/${compositionUID}/${jwtToken}`;
console.log(`\nš View URL (if pattern matches):`);
console.log(` ${viewURL}`);
} else {
console.log('\nā Request failed');
if (response.status === 400) {
console.log(' - Bad Request: Check the composition data format');
} else if (response.status === 401) {
console.log(' - Unauthorized: JWT token might be invalid');
} else if (response.status === 403) {
console.log(' - Forbidden: Insufficient permissions');
} else if (response.status === 500) {
console.log(' - Server Error: The API endpoint might expect different data');
}
}
} catch (error) {
console.error('\nā Request error:', error.message);
}
}
function generateUID() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
const r = Math.random() * 16 | 0;
const v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
testPUTAPI().catch(console.error);