hydrogen-sanity
Version:
Sanity.io toolkit for Hydrogen
47 lines (44 loc) • 1.15 kB
JavaScript
import { perspectiveCookieName } from '@sanity/preview-url-secret/constants';
import { createCookieSessionStorage } from 'react-router';
class PreviewSession {
#sessionStorage;
#session;
constructor(sessionStorage, session) {
this.#sessionStorage = sessionStorage;
this.#session = session;
}
static async init(request, secrets) {
const storage = createCookieSessionStorage({
cookie: {
name: perspectiveCookieName,
httpOnly: true,
path: "/",
sameSite: "none",
secure: true,
secrets
}
});
const session = await storage.getSession(request.headers.get("Cookie")).catch(() => storage.getSession());
return new this(storage, session);
}
get has() {
return this.#session.has;
}
get get() {
return this.#session.get;
}
get unset() {
return this.#session.unset;
}
get set() {
return this.#session.set;
}
destroy() {
return this.#sessionStorage.destroySession(this.#session);
}
commit() {
return this.#sessionStorage.commitSession(this.#session);
}
}
export { PreviewSession };
//# sourceMappingURL=session.js.map