π 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 theuser
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-sideRemove 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