Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 36 additions & 25 deletions src/api/endpoints/apiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any, any>(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,
};
}
};
Expand Down
12 changes: 1 addition & 11 deletions src/components/SingleOrganization/AddNewOntologyDialog.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
31 changes: 22 additions & 9 deletions vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -98,32 +97,46 @@ 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,
secure: false,
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
Expand Down