I’m trying to create my first app where I’m following several different examples including the ones from Atlassian.
I’m able to deploy the app but it keep showing the loading animation, when looking at the development tools on the browser I get the error:
Refused to connect to ‘https://jira/rest/api/3/search’ because it violates the following Content Security Policy directive: “connect-src ‘self’ https://api.atlassian.com/metal/ingest”.
I looked online and this is due to CSP restrictions that doesn’t allow me to do API calls from the frontend. But because the Atlassian example only shows how to do it from the frontend it’s impossible for me to continue.
I looked on examples on how to do the API calls from the backend but I can’t get it to work and I’m not sure if it’s because I’m not connecting something right, or what else it could be. I believe it could be related to a bunch of 409 errors I’m getting.
Here’s my code if it helps:
backend:
import Resolver from ‘@forge/resolver’;
import api, { route } from ‘@forge/api’;const resolver = new Resolver();
resolver.define(‘getNumber’, async ({ context }) => {
const projectKey = context.projectKey;// Fetch issues from the project, including subtasks and epics
const response = await api.asUser().requestJira(route/rest/api/3/search?jql=project=${projectKey}&maxResults=0
);
const data = await response.json();return data.total; // Return total number of issues in current project
});export const handler = resolver.getDefinitions();
Frontend:
import React, { useEffect, useState } from ‘react’;
import { Text } from ‘@forge/react’;
import { invoke } from ‘@forge/bridge’;const IssueCounter = () => {
const [numero, setNumero] = useState(null); // State to hold the number of issues
const [error, setError] = useState(null); // State to hold errorsuseEffect(() => {
const loadIssues = async () => {
try {
const numero = await invoke(‘getNumber’);
setNumero(numero); // Update state with the number of issues
} catch (error) {
console.error(‘Error fetching issues:’, error);
setError(‘There was an issue fetching the data. Please try again.’);
}
};loadIssues(); // Call the function inside useEffect to load issues on component mount
}, ); // Empty dependency array ensures it runs once on mount
return (
<>
{error ? (
{error}
) : (
Number of issues: {numero !== null ? numero : ‘Loading…’}
)}
</>
);
};export default IssueCounter;
The help from this community would be appreciated as I wasn’t able to find any other thread about how to use the API between backend and frontend.