rakkasjs
rakkasjs copied to clipboard
Clarification needed on hattip-entry file
I am trying to add cookie / session support to my rakkasjs project.
The docs give some instructions on how this might be achieved, but I am still unsure if I am doing it right.
I think it would be helpful to clarify the following points in the documentation:
- are the listed "available" hattip modules already loaded by rakkasjs, or is this merely an information on what we might want to add to a custom configuration?
- the page mentions a default implementation, but now where to find and how to build upon it.
- Will a custom configuration (if present) replace the default or extend it? If replacing: should we manually extend the defaults to ensure that we're not losing functionality?
I am new to both rakkasjs and hattip and find this part very confusing.
Hi,
Thank you for your interest!
See the session example in the repo, it should give you the direction in which to start.
The entry-hattip
file is for customizing the server. If you don't provide one, nothing is customized and Rakkas acts as if you provided the following:
import { createRequestHandler } from "rakkasjs";
export default createRequestHandler();
The docs show all the options you can pass to createRequestHandler
, all which are optional. For session support, you'd add cookie and session middlewares using the middleware.beforePages
option:
import { createRequestHandler } from "rakkasjs";
import { cookie } from "@hattip/cookie";
import { session, SimpleCookieStore } from "@hattip/session";
// Declare session data type
declare module "@hattip/session" {
interface SessionData {
foo: string;
bar: number;
}
}
export default createRequestHandler({
middleware: {
beforePages: [
cookie(),
session({
store: new SimpleCookieStore(),
defaultSessionData: { foo: "xyz", bar: 42 },
cookieOptions: {
httpOnly: true,
secure: import.meta.env.PROD,
path: "/",
maxAge: 60 * 60 * 1000, // 1 week
},
}),
],
},
});
I know how frustrating it can be when the documentation isn't great, feel free to ping me on Rakkas Discord channel or my Twitter DMs for quick questions on getting started.
Hello @cyco130, thanks so much for taking the time to reply and to reply so extensively. I have replaced my implementation with an adjusted version of your example, and the application is now working 😊
Reopened because I think this part from your answer might help others, too, if it was taken into the documentation:
The entry-hattip file is for customizing the server. If you don't provide one, nothing is customized and Rakkas acts as if you provided the following:
import { createRequestHandler } from "rakkasjs"; export default createRequestHandler();
Yes, let's leave it open. It'll serve as a reminder for me to update the docs :)