-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
debug: why it does not run on server, why GET and not POST
- Loading branch information
Showing
4 changed files
with
73 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
"use server"; | ||
|
||
/** | ||
* @typedef {Object } RecordEventApiResponse | ||
* @property {string} message - The message returned from the API. | ||
*/ | ||
|
||
/** | ||
* Records an event to the API. | ||
* @param {string} endpoint - The endpoint to record the event to. | ||
* @returns {Promise<void>} A promise that resolves when the event is successfully recorded. | ||
*/ | ||
export const recordEvent = async (endpoint) => { | ||
// "use server"; | ||
try { | ||
const rootUrl = process.env.NEXT_PUBLIC_ROOT_API_URL; | ||
const url = `${rootUrl}/${endpoint}`; | ||
|
||
const response = await fetch(url, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
}); | ||
|
||
console.log("response: ", response); | ||
|
||
if (!response.ok) { | ||
throw new Error("Failed to record event"); | ||
} | ||
|
||
const data = await response.json(); | ||
if (!data || !data.message) { | ||
throw new Error("Invalid response data"); | ||
} | ||
return; | ||
} catch (error) { | ||
console.log(error); | ||
throw new Error(`Error recording event: ${error.message}`); | ||
} | ||
}; |