Skip to content

Commit

Permalink
Remove dev dependencies for typing (#1212)
Browse files Browse the repository at this point in the history
  • Loading branch information
pilcrowonpaper authored Oct 22, 2023
1 parent f4ce8ff commit 3e4ed1c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 26 deletions.
6 changes: 6 additions & 0 deletions .auri/$ymzad0k8.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
package: "lucia" # package name
type: "patch" # "major", "minor", "patch"
---

Remove dev dependencies for typing
3 changes: 0 additions & 3 deletions packages/lucia/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@
"author": "pilcrowonpaper",
"license": "MIT",
"devDependencies": {
"@sveltejs/kit": "1.10.0",
"@types/express": "^4.17.17",
"@types/node": "^18.6.2",
"fastify": "^4.21.0",
"prettier": "^2.3.0",
"vitest": "^0.33.0"
}
Expand Down
65 changes: 42 additions & 23 deletions packages/lucia/src/middleware/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@ import type {
RequestContext
} from "../auth/request.js";

import type {
IncomingMessage,
OutgoingMessage,
ServerResponse
} from "node:http";
import type {
Request as ExpressRequest,
Response as ExpressResponse
} from "express";
import type { FastifyReply, FastifyRequest } from "fastify";
type NodeIncomingMessage = {
method?: string;
headers: Record<string, string | string[] | undefined>;
};

export const node = (): Middleware<[IncomingMessage, OutgoingMessage]> => {
type NodeOutGoingMessage = {
getHeader: (name: string) => string | string[] | number | undefined;
setHeader: (name: string, value: string | number | readonly string[]) => void;
};

export const node = (): Middleware<
[NodeIncomingMessage, NodeOutGoingMessage]
> => {
return ({ args }) => {
const [incomingMessage, outgoingMessage] = args;
const requestContext = {
Expand All @@ -27,16 +28,16 @@ export const node = (): Middleware<[IncomingMessage, OutgoingMessage]> => {
headers: createHeadersFromObject(incomingMessage.headers)
},
setCookie: (cookie) => {
const setCookieHeaderValues =
outgoingMessage
.getHeader("Set-Cookie")
?.toString()
.split(",")
.filter((val) => val) ?? [];

let parsedSetCookieHeaderValues: string[] = [];
const setCookieHeaderValue = outgoingMessage.getHeader("Set-Cookie");
if (typeof setCookieHeaderValue === "string") {
parsedSetCookieHeaderValues = [setCookieHeaderValue];
} else if (Array.isArray(setCookieHeaderValue)) {
parsedSetCookieHeaderValues = setCookieHeaderValue;
}
outgoingMessage.setHeader("Set-Cookie", [
cookie.serialize(),
...setCookieHeaderValues
...parsedSetCookieHeaderValues
]);
}
} as const satisfies RequestContext;
Expand All @@ -45,6 +46,15 @@ export const node = (): Middleware<[IncomingMessage, OutgoingMessage]> => {
};
};

type ExpressRequest = {
method: string;
headers: Record<string, string | string[] | undefined>;
};

type ExpressResponse = {
cookie: (name: string, val: string, options?: CookieAttributes) => void;
};

export const express = (): Middleware<[ExpressRequest, ExpressResponse]> => {
return ({ args }) => {
const [req, res] = args;
Expand All @@ -61,6 +71,15 @@ export const express = (): Middleware<[ExpressRequest, ExpressResponse]> => {
};
};

type FastifyRequest = {
method: string;
headers: Record<string, string | string[] | undefined>;
};

type FastifyReply = {
header: (name: string, val: any) => void;
};

export const fastify = (): Middleware<[FastifyRequest, FastifyReply]> => {
return ({ args }) => {
const [req, res] = args;
Expand Down Expand Up @@ -197,8 +216,8 @@ export const web = (): Middleware<[Request]> => {
};

type NextJsPagesServerContext = {
req: IncomingMessage;
res?: OutgoingMessage;
req: NodeIncomingMessage;
res?: NodeOutGoingMessage;
};

type NextCookie =
Expand Down Expand Up @@ -362,8 +381,8 @@ export const nextjs_future = (): Middleware<

type H3Event = {
node: {
req: IncomingMessage;
res: ServerResponse;
req: NodeIncomingMessage;
res: NodeOutGoingMessage;
};
};

Expand Down

0 comments on commit 3e4ed1c

Please sign in to comment.