1.0.0 • Published 12 months ago

@louisgustaf/jasperconnect v1.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
12 months ago

JasperConnect

JasperConnect is a library that simplifies the integration between NodeJS (using Express and TypeScript) applications and JasperServer. With it, you can connect to JasperServer, request reports, and obtain the generated content in a straightforward and efficient manner.

Features

  • Simplified Integration: Easily connect your NodeJS applications with JasperServer.
  • Authentication and Parameters: Simple configuration of credentials and query parameters to customize your report request.
  • Reverse Proxy for CORS: Acts as a reverse proxy, avoiding CORS issues when sending the report to the front-end.
  • NodeJS Compatibility: Leverages native NodeJS features, such as the Buffer object, eliminating the need for extra dependencies.

Installation

Install the library via npm:

npm install @louisgustaf/jasperconnect

Usage

Import and Instantiation To use the library, import the JasperIntegration class, instantiate it, and call the execute() method by providing the following parameters:

JasperServer URL: The address where JasperServer is hosted. Report Path: The path to the report on JasperServer. File Format: The file extension that will be returned (e.g., pdf). Username and Password: Credentials for accessing JasperServer. Query Parameters (Optional): An object containing any additional parameters to be sent with the request.

import JasperIntegration from '@louisgustaf/jasperconnect';

const jasperServer = new JasperIntegration(
    'http://localhost:8080',       // JasperServer URL
    'Reports/Relatorio_01',        // Report path
    'pdf',                         // File format
    'jasperadmin',                 // Username
    'jasperadmin',                 // Password
    {
      // Optional parameters, e.g.:
      // 'param1': 'value1',
      // 'param2': 123
    }
);

Executing and Returning the Report

After instantiating the class, call the execute() method to obtain the report. The method returns a Buffer containing the report data, which you can send to the front-end using Express. Below is an example:

app.get('/report', async (req, res) => {
    try {
        const jasperServer = new JasperIntegration(
            'http://localhost:8080',
            'Reports/Relatorio_01',
            'pdf',
            'jasperadmin',
            'jasperadmin'
        );

        const data = await jasperServer.execute();

        // Set the headers according to the file format and desired disposition (inline for viewing or attachment for download)
        res.setHeader("Content-Type", "application/pdf");
        res.setHeader("Content-Disposition", "inline; filename=report.pdf");
        res.send(data);
    } catch (error) {
        res.status(500).send("Error obtaining the report");
    }
});

Considerations

Authentication and Security: Use environment variables or other secure methods to store your JasperServer credentials. Reverse Proxy and CORS: Since the library functions as a reverse proxy, it helps avoid CORS issues on the front-end. Build and Compilation: Although your source file is index.ts, it is recommended to compile it to JavaScript (generating an index.js) and update the main field in your package.json accordingly.

Contribution

Contributions are always welcome! If you have suggestions or encounter issues, feel free to open issues or submit pull requests.