Skip to content

Commit

Permalink
Update connect middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelmund committed Feb 17, 2022
1 parent 3e157fd commit 36160db
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "svelte-kit-cookie-session",
"version": "2.1.0",
"version": "2.1.2",
"description": "⚒️ Encrypted 'stateless' cookie sessions for SvelteKit",
"repository": {
"type": "git",
Expand Down
22 changes: 13 additions & 9 deletions src/connect.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,52 @@
import initializeSession from "./core.js";
import type { SessionOptions, Session } from "./types";
import type { SessionOptions } from "./types";
import type { IncomingMessage, ServerResponse } from "http";

declare global {
namespace Express {
interface Request {
session: Session<SessionData>;
session: import("./types").Session<Session.SessionData>;
cookies: Record<string, string>;
}
}
namespace Polka {
interface Request {
session: Session<SessionData>;
session: import("./types").Session<Session.SessionData>;
}
}
}

declare namespace Session {
interface SessionData {}
}

/**
* This interface allows you to declare additional properties on your session object using [declaration merging](https://www.typescriptlang.org/docs/handbook/declaration-merging.html).
*
* @example
* declare module 'svelte-kit-cookie-session' {
* declare namespace Session {
* interface SessionData {
* views: number;
* }
* }
*
*/
interface SessionData {
[key: string]: any;
}

export function sessionMiddleware<
Req extends { headers: IncomingMessage["headers"] },
Res extends ServerResponse,
SessionType = Record<string, any>
>(options: SessionOptions): (req: Req, res: Res, next: () => void) => any {
return (req, res, next) => {
const session: any = initializeSession<SessionType>(
req.headers.cookie || '',
const { session, cookies } = initializeSession<SessionType>(
req.headers.cookie || "",
options
);

//@ts-ignore
req.session = session;
//@ts-ignore
req.cookies = cookies;

const setSessionHeaders = () => {
//@ts-ignore This can exist
Expand Down

0 comments on commit 36160db

Please sign in to comment.