Skip to main content

Minimales Beispiel

Folgend ein Action Script welches auf ein Button gelegt wurde um ein Report zu generieren. In diesem Script können reportName, reportRoute, reportData angepasst werden um mit dem nachfolgend aufgeführten NodeRed Flow zu interagieren.

const CONNECTOR_API_URL = fyzPlatform.env.get().config.connectorApiUrl;

// Set The NodeRed Endpoint you have chosen 
const reportRoute = 'downloadReport';

// Set the file name you want the user to have when downloading
const reportDate = "2025-09-01"
const reportName = `${reportDate}_report`;

// Set the date you want to use inside the endpoint
const reportData = {
    test: "1234",
    testArray: [
        { id: `1`, name: "content1" },
        { id: `2`, name: "content2" }
    ]
}

// execute the report
generateReport(reportRoute, reportName, reportData);


// helper function - call the node red endpoint
function generateReport(route, name, data) {
    fyzUtils.httpApi
        .executeRequestsAsync([{
            action: 'POST',
            url: CONNECTOR_API_URL + '/api/data/connect',
            body: {
                Configuration: {
                    Endpoint: 'FLYZE_NODE_RED',
                    Route: `${route}`,
                    Authentication: false
                },
                ApiConfig: data
            },
            responseType: "arraybuffer"
        }], (res) => {
            if (res && res[0]) {
                makeBrowserDownload(res[0], name);
            }
        })
}

// helper function - download the file to browser
function makeBrowserDownload(fileData, fileName) {
    // Create a blob from the response
    const blob = new Blob([fileData], { type: "application/pdf" });

    // Create a temporary download link
    const url = window.URL.createObjectURL(blob);
    const a = document.createElement("a");
    a.href = url;
    a.download = `${fileName}.pdf`; // filename
    document.body.appendChild(a);
    a.click();

    // Clean up
    setTimeout(() => {
        document.body.removeChild(a);
        window.URL.revokeObjectURL(url);
    }, 0);
}