Skip to content

Commit

Permalink
Merge branch 'd2api-updates'
Browse files Browse the repository at this point in the history
  • Loading branch information
olavpo committed Nov 25, 2024
2 parents 4d0dd13 + bd1ed8b commit 32d802c
Showing 1 changed file with 40 additions and 9 deletions.
49 changes: 40 additions & 9 deletions src/js/d2api.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
const dhisDevConfig = DHIS_CONFIG;
const isDev = "baseUrl" in dhisDevConfig;
const baseUrl = isDev ? dhisDevConfig.baseUrl : "../../..";

// Helper function to set headers for development mode
const getHeaders = () => {
let headers = new Headers();
Expand Down Expand Up @@ -35,7 +31,17 @@ const validateUID = (endpoint) => {
};


//GET from API async
// Helper function to handle API errors and throw detailed error messages
const handleApiError = async (response) => {
let errorMessage = "Network response was not ok";
let errorDetail = await response.json(); // Capture the error response body text

errorMessage = errorDetail.message || errorMessage;

throw new Error(`${response.statusText} - ${errorMessage}`);
};

// GET from API async
export const d2Get = async (endpoint) => {
try {
endpoint = formatEndpoint(endpoint);
Expand All @@ -45,7 +51,7 @@ export const d2Get = async (endpoint) => {
headers: headers
});
if (!response.ok) {
throw new Error("Network response was not ok " + response.statusText);
await handleApiError(response); // Handle the error response
}
let data = await response.json();
return data;
Expand All @@ -56,7 +62,7 @@ export const d2Get = async (endpoint) => {
}
};

//POST to API async
// POST to API async
export const d2PostJson = async (endpoint, body) => {
try {
endpoint = formatEndpoint(endpoint);
Expand All @@ -68,7 +74,7 @@ export const d2PostJson = async (endpoint, body) => {
body: JSON.stringify(body)
});
if (!response.ok) {
throw new Error("Network response was not ok " + response.statusText);
await handleApiError(response); // Handle the error response
}
let data = await response.json();
return data;
Expand Down Expand Up @@ -96,7 +102,7 @@ export const d2PutJson = async (endpoint, body) => {
body: JSON.stringify(body)
});
if (!response.ok) {
throw new Error("Network response was not ok " + response.statusText);
await handleApiError(response); // Handle the error response
}
let data = await response.json();
return data;
Expand All @@ -106,3 +112,28 @@ export const d2PutJson = async (endpoint, body) => {
throw error;
}
};

// DELETE from API async
export const d2Delete = async (endpoint) => {
try {
endpoint = formatEndpoint(endpoint);

if (!validateUID(endpoint)) {
console.warn("Warning: The endpoint does not end with a valid 11-character UID");
}

let headers = getHeaders();
let response = await fetch(baseUrl + endpoint, {
method: "DELETE",
headers: headers
});
if (!response.ok) {
await handleApiError(response); // Handle the error response
}
return { status: "success" };
} catch (error) {
console.log("ERROR in DELETE:");
console.log(error);
throw error;
}
};

0 comments on commit 32d802c

Please sign in to comment.