diff --git a/src/api/endpoints/apiService.ts b/src/api/endpoints/apiService.ts index 3e2f0387..9770d797 100644 --- a/src/api/endpoints/apiService.ts +++ b/src/api/endpoints/apiService.ts @@ -141,49 +141,60 @@ export const createNewOntology = async ({ const endpoint = `/${groupname}/ontologies/uris/${ontologyName}/spec`; const data = { - title : title, - subjects : subjects, + title: title, + subjects: subjects, }; const headers = { 'Content-Type': 'application/json', - 'Authorization': `Bearer ${token}` + 'Authorization': `Bearer ${token}`, }; try { - const postResponse = await createPostRequest(endpoint, headers)(data); - - // If the POST creates a new location, try fetching it (simulate follow-up GETs from the test) - if (postResponse?.location) { - const getResponse = await fetch(postResponse.location, { - headers: { - Accept: 'application/json', - }, - }); - - // Optionally fetch HTML if needed (like the .html equivalent in the Python test) - const htmlResponse = await fetch(endpoint, { - headers: { - Accept: 'text/html', - }, - }); + const postResponse = await fetch(endpoint, { + method: 'POST', + headers, + credentials: "include", + body: JSON.stringify(data), + redirect: 'manual', + }); + // Check for custom redirect header (all lowercase in fetch) + const redirectLocation = postResponse.headers.get('x-redirect-location'); + + if (redirectLocation) { + const olympianRedirectLocation = redirectLocation.replace('http://uri.interlex.org','').replace(/\.html$/, '.jsonld'); + + const getResponse = await fetch(olympianRedirectLocation, { headers: { Authorization: `Bearer ${token}` } }); + const jsonResponse = await getResponse.json(); + + const newOntologyID = jsonResponse?.["@graph"]?.find((object) => object["@type"] === "owl:Ontology")?.["@id"] || null; + return { created: true, - data: postResponse, - jsonResponse: await getResponse.json(), - htmlAvailable: htmlResponse.ok, + location: olympianRedirectLocation, + newOntologyID: newOntologyID }; } + // Try to parse the response as JSON (if present) + let jsonResponse: any = null; + try { + jsonResponse = await postResponse.json(); + } catch (e) { + // No JSON body, ignore + } + return { - created: true, - data: postResponse, + created: postResponse.ok, + location: endpoint, + jsonResponse, }; } catch (error: any) { + let errMsg = error?.message ?? String(error); return { created: false, - error: error?.response?.data || error.message, + error: errMsg, }; } }; diff --git a/src/components/SingleOrganization/AddNewOntologyDialog.jsx b/src/components/SingleOrganization/AddNewOntologyDialog.jsx index dbc76619..ba1963c0 100644 --- a/src/components/SingleOrganization/AddNewOntologyDialog.jsx +++ b/src/components/SingleOrganization/AddNewOntologyDialog.jsx @@ -73,17 +73,7 @@ const AddNewOntologyDialog = ({ open, handleClose }) => { let ontologyResponseMessage = "Ontology created successfully!" - if (result.created) { - console.log('Ontology details:', result.data); - - if (result.jsonResponse) { - console.log('Retrieved JSON:', result.jsonResponse); - } - - if (result.htmlAvailable !== undefined) { - console.log('HTML version available:', result.htmlAvailable); - } - } else { + if (!result.created) { ontologyResponseMessage = "Failed to create ontology" console.error('❌ Failed to create ontology:', result.error); } diff --git a/vite.config.js b/vite.config.js index c2ef94f0..41829e13 100644 --- a/vite.config.js +++ b/vite.config.js @@ -67,7 +67,6 @@ export default defineConfig({ if (proxyRes.statusCode === 303 && location) { // Prevent browser from seeing the actual Location delete proxyRes.headers['location']; - // Inject the location into a custom header we can use in Axios res.setHeader('X-Redirect-Location', location); } @@ -98,6 +97,21 @@ export default defineConfig({ }); }, }, + '^/[^/]+/ontologies/uris/.*\\.(html|jsonld)$': { + target: 'https://uri.olympiangods.org', + changeOrigin: true, + secure: false, + rewrite: path => path, // Keep the full path + configure: (proxy) => { + proxy.on('proxyRes', (proxyRes, req, res) => { + const origin = req.headers.origin; + if (origin) { + res.setHeader('Access-Control-Allow-Origin', origin); + } + res.setHeader('Access-Control-Allow-Credentials', 'true'); + }); + }, + }, '^/[^/]+/ontologies/uris/.*/spec': { target: 'https://uri.olympiangods.org', changeOrigin: true, @@ -105,25 +119,24 @@ export default defineConfig({ rewrite: path => path, // Keep full path configure: (proxy) => { proxy.on('proxyReq', (proxyReq, req) => { - console.log('Proxying ontology spec request:', req.method, req.url); - console.log('Headers:', proxyReq.getHeaders()); if (req.headers.authorization) { proxyReq.setHeader('Authorization', req.headers.authorization); } }); proxy.on('proxyRes', (proxyRes, req, res) => { - console.log('Received response', res); - console.log('Received Response from the Target:', proxyRes.statusCode, req.url); const location = proxyRes.headers['location']; - console.log('Received location', location); if (proxyRes.statusCode === 303 && location) { - // Prevent browser from seeing the actual Location delete proxyRes.headers['location']; - - // Inject the location into a custom header we can use in Axios + res.statusCode = 200; // Prevent browser redirect res.setHeader('X-Redirect-Location', location); + res.setHeader('Access-Control-Allow-Origin', req.headers.origin || '*'); + res.setHeader('Access-Control-Allow-Credentials', 'true'); + res.setHeader('Access-Control-Expose-Headers', 'X-Redirect-Location'); + // Send a JSON body (for fetch, etc.) + res.end(JSON.stringify({ location })); + return; } // Required for credentialed CORS