πŸš€ Extras

Here are some additional notes and tips for working with apiRequest effectively.


πŸ” Accessing Session Data

You can retrieve session information directly using:

const session = await apiRequest({ name: "session" });

πŸ”Ή You do not need to create a session.ts API route for this.


✍️ Updating Session Data

You can update the session inside any API route using saveSession:

user.groupId = 12;
await saveSession(user.token, user);
  • user.token is always included in the user object that is paased in every API call.

  • Updated session data is saved to Redis immediately.


❌ You don’t need to call:

  • getSession(token) – Not necessary. The user’s session is already available via the user parameter in every API.

  • deleteSession(token) – Handled automatically when the user logs out.

πŸ”Ή If you know what you are doing you could use these functions but for most people they are not necessary.


πŸšͺ Logging Out

To log a user out:

await apiRequest({ name: "logout" });

This will:

  • Automatically call deleteSession server-side

  • Remove the session from Redis

  • Redirect the user to the login page

➑️ If the user logs in with a different account, this also happens behind the scenes.


πŸ›‘οΈ Preventing Duplicate API Requests

To avoid race conditions or server overload, apiRequest uses an AbortController when the API name starts with certain keywords.

If a new request is made with the same name before the previous one finishes, the earlier one is aborted automatically.

⚠️ Auto-abort applies to names starting with:

  • get – e.g. getUser

  • fetch – e.g. fetchProfile

  • load – e.g. loadTasks

  • is – e.g. isAdmin

  • has – e.g. hasPermission

  • list – e.g. listGroups

  • all – e.g. allUsers

  • search – e.g. searchStudents

  • view – e.g. viewResults

  • retrieve – e.g. retrieveData

πŸ” This helps reduce lag and duplicated requests when users trigger the same action quickly (like clicking a button multiple times).


Last updated