Skip to content

Commit a29507f

Browse files
refactor: lint fix
1 parent 30ee34b commit a29507f

File tree

2 files changed

+128
-116
lines changed

2 files changed

+128
-116
lines changed

lib/utils/getCookieOptions.test.ts

Lines changed: 113 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,120 +1,123 @@
11
import { describe, it, expect, vi, afterEach } from "vitest";
22

33
import {
4-
getCookieOptions,
5-
removeTrailingSlash,
6-
TWENTY_NINE_DAYS,
7-
MAX_COOKIE_LENGTH,
4+
getCookieOptions,
5+
removeTrailingSlash,
6+
TWENTY_NINE_DAYS,
7+
MAX_COOKIE_LENGTH,
88
} from "./getCookieOptions";
99

1010
describe("getCookieOptions", () => {
11-
afterEach(() => {
12-
vi.restoreAllMocks();
13-
});
14-
15-
it("returns the default configuration when env is provided", () => {
16-
const result = getCookieOptions(undefined, {
17-
NODE_ENV: "production",
18-
KINDE_COOKIE_DOMAIN: "example.com/",
19-
});
20-
21-
expect(result).toMatchObject({
22-
maxAge: TWENTY_NINE_DAYS,
23-
domain: "example.com",
24-
maxCookieLength: MAX_COOKIE_LENGTH,
25-
sameSite: "lax",
26-
httpOnly: true,
27-
path: "/",
28-
secure: true,
29-
});
30-
});
31-
32-
it("allows consumers to override default options", () => {
33-
const result = getCookieOptions(
34-
{
35-
secure: false,
36-
sameSite: "none",
37-
path: "/custom",
38-
maxAge: 60,
39-
customOption: "value",
40-
},
41-
{
42-
NODE_ENV: "production",
43-
KINDE_COOKIE_DOMAIN: "example.com",
44-
}
45-
);
46-
47-
expect(result.secure).toBe(false);
48-
expect(result.sameSite).toBe("none");
49-
expect(result.path).toBe("/custom");
50-
expect(result.maxAge).toBe(60);
51-
expect(result.customOption).toBe("value");
52-
});
53-
54-
it("falls back to runtime environment variables when env param is omitted", () => {
55-
const previousNodeEnv = process.env.NODE_ENV;
56-
const previousCookieDomain = process.env.KINDE_COOKIE_DOMAIN;
57-
58-
process.env.NODE_ENV = "production";
59-
process.env.KINDE_COOKIE_DOMAIN = "runtime-domain.io/";
60-
61-
const result = getCookieOptions();
62-
63-
expect(result.domain).toBe("runtime-domain.io");
64-
expect(result.secure).toBe(true);
65-
66-
if (previousNodeEnv === undefined) {
67-
delete process.env.NODE_ENV;
68-
} else {
69-
process.env.NODE_ENV = previousNodeEnv;
70-
}
71-
72-
if (previousCookieDomain === undefined) {
73-
delete process.env.KINDE_COOKIE_DOMAIN;
74-
} else {
75-
process.env.KINDE_COOKIE_DOMAIN = previousCookieDomain;
76-
}
77-
});
78-
79-
it("warns when NODE_ENV is missing and secure option is not provided", () => {
80-
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
81-
82-
const result = getCookieOptions({}, {});
83-
84-
expect(result.secure).toBe(false);
85-
expect(warnSpy).toHaveBeenCalledWith(
86-
"getCookieOptions: NODE_ENV not set; defaulting secure cookie flag to false. Provide env or override secure to suppress this warning."
87-
);
88-
});
89-
90-
it("warns when KINDE_COOKIE_DOMAIN resolves to an empty string", () => {
91-
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
92-
93-
const result = getCookieOptions({}, { NODE_ENV: "development", KINDE_COOKIE_DOMAIN: " " });
94-
95-
expect(result.domain).toBeUndefined();
96-
expect(warnSpy).toHaveBeenCalledWith(
97-
"getCookieOptions: KINDE_COOKIE_DOMAIN is empty after trimming and will be ignored."
98-
);
99-
});
11+
afterEach(() => {
12+
vi.restoreAllMocks();
13+
});
14+
15+
it("returns the default configuration when env is provided", () => {
16+
const result = getCookieOptions(undefined, {
17+
NODE_ENV: "production",
18+
KINDE_COOKIE_DOMAIN: "example.com/",
19+
});
20+
21+
expect(result).toMatchObject({
22+
maxAge: TWENTY_NINE_DAYS,
23+
domain: "example.com",
24+
maxCookieLength: MAX_COOKIE_LENGTH,
25+
sameSite: "lax",
26+
httpOnly: true,
27+
path: "/",
28+
secure: true,
29+
});
30+
});
31+
32+
it("allows consumers to override default options", () => {
33+
const result = getCookieOptions(
34+
{
35+
secure: false,
36+
sameSite: "none",
37+
path: "/custom",
38+
maxAge: 60,
39+
customOption: "value",
40+
},
41+
{
42+
NODE_ENV: "production",
43+
KINDE_COOKIE_DOMAIN: "example.com",
44+
},
45+
);
46+
47+
expect(result.secure).toBe(false);
48+
expect(result.sameSite).toBe("none");
49+
expect(result.path).toBe("/custom");
50+
expect(result.maxAge).toBe(60);
51+
expect(result.customOption).toBe("value");
52+
});
53+
54+
it("falls back to runtime environment variables when env param is omitted", () => {
55+
const previousNodeEnv = process.env.NODE_ENV;
56+
const previousCookieDomain = process.env.KINDE_COOKIE_DOMAIN;
57+
58+
process.env.NODE_ENV = "production";
59+
process.env.KINDE_COOKIE_DOMAIN = "runtime-domain.io/";
60+
61+
const result = getCookieOptions();
62+
63+
expect(result.domain).toBe("runtime-domain.io");
64+
expect(result.secure).toBe(true);
65+
66+
if (previousNodeEnv === undefined) {
67+
delete process.env.NODE_ENV;
68+
} else {
69+
process.env.NODE_ENV = previousNodeEnv;
70+
}
71+
72+
if (previousCookieDomain === undefined) {
73+
delete process.env.KINDE_COOKIE_DOMAIN;
74+
} else {
75+
process.env.KINDE_COOKIE_DOMAIN = previousCookieDomain;
76+
}
77+
});
78+
79+
it("warns when NODE_ENV is missing and secure option is not provided", () => {
80+
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
81+
82+
const result = getCookieOptions({}, {});
83+
84+
expect(result.secure).toBe(false);
85+
expect(warnSpy).toHaveBeenCalledWith(
86+
"getCookieOptions: NODE_ENV not set; defaulting secure cookie flag to false. Provide env or override secure to suppress this warning.",
87+
);
88+
});
89+
90+
it("warns when KINDE_COOKIE_DOMAIN resolves to an empty string", () => {
91+
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
92+
93+
const result = getCookieOptions(
94+
{},
95+
{ NODE_ENV: "development", KINDE_COOKIE_DOMAIN: " " },
96+
);
97+
98+
expect(result.domain).toBeUndefined();
99+
expect(warnSpy).toHaveBeenCalledWith(
100+
"getCookieOptions: KINDE_COOKIE_DOMAIN is empty after trimming and will be ignored.",
101+
);
102+
});
100103
});
101104

102105
describe("removeTrailingSlash", () => {
103-
it("removes trailing slashes and trims whitespace", () => {
104-
expect(removeTrailingSlash("example.com/")).toBe("example.com");
105-
expect(removeTrailingSlash(" example.com/ ")).toBe("example.com");
106-
});
107-
108-
it("returns the original string when there is no trailing slash", () => {
109-
expect(removeTrailingSlash("example.com")).toBe("example.com");
110-
});
111-
112-
it("returns undefined for nullish values", () => {
113-
expect(removeTrailingSlash(undefined)).toBeUndefined();
114-
expect(removeTrailingSlash(null)).toBeUndefined();
115-
});
116-
117-
it("returns undefined for whitespace-only strings", () => {
118-
expect(removeTrailingSlash(" ")).toBeUndefined();
119-
});
106+
it("removes trailing slashes and trims whitespace", () => {
107+
expect(removeTrailingSlash("example.com/")).toBe("example.com");
108+
expect(removeTrailingSlash(" example.com/ ")).toBe("example.com");
109+
});
110+
111+
it("returns the original string when there is no trailing slash", () => {
112+
expect(removeTrailingSlash("example.com")).toBe("example.com");
113+
});
114+
115+
it("returns undefined for nullish values", () => {
116+
expect(removeTrailingSlash(undefined)).toBeUndefined();
117+
expect(removeTrailingSlash(null)).toBeUndefined();
118+
});
119+
120+
it("returns undefined for whitespace-only strings", () => {
121+
expect(removeTrailingSlash(" ")).toBeUndefined();
122+
});
120123
});

lib/utils/getCookieOptions.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ const getRuntimeEnv = (): CookieEnv => {
3737
return maybeProcess?.env ?? {};
3838
};
3939

40-
export function removeTrailingSlash(url: string | undefined | null): string | undefined {
40+
export function removeTrailingSlash(
41+
url: string | undefined | null,
42+
): string | undefined {
4143
if (url === undefined || url === null) return undefined;
4244

4345
url = url.trim();
@@ -52,15 +54,22 @@ export function removeTrailingSlash(url: string | undefined | null): string | un
5254
return url;
5355
}
5456

55-
export const getCookieOptions = (options: CookieOptions = {}, env?: CookieEnv): CookieOptions => {
57+
export const getCookieOptions = (
58+
options: CookieOptions = {},
59+
env?: CookieEnv,
60+
): CookieOptions => {
5661
const resolvedEnv = env ?? getRuntimeEnv();
5762
const rawDomain = resolvedEnv.KINDE_COOKIE_DOMAIN;
5863
const domainFromEnv = removeTrailingSlash(rawDomain);
5964
const secureDefault = resolvedEnv.NODE_ENV === "production";
6065

61-
if (rawDomain && domainFromEnv === undefined && options.domain === undefined) {
66+
if (
67+
rawDomain &&
68+
domainFromEnv === undefined &&
69+
options.domain === undefined
70+
) {
6271
console.warn(
63-
"getCookieOptions: KINDE_COOKIE_DOMAIN is empty after trimming and will be ignored."
72+
"getCookieOptions: KINDE_COOKIE_DOMAIN is empty after trimming and will be ignored.",
6473
);
6574
}
6675

@@ -76,10 +85,10 @@ export const getCookieOptions = (options: CookieOptions = {}, env?: CookieEnv):
7685
merged.secure = secureDefault;
7786
if (resolvedEnv.NODE_ENV === undefined) {
7887
console.warn(
79-
"getCookieOptions: NODE_ENV not set; defaulting secure cookie flag to false. Provide env or override secure to suppress this warning."
88+
"getCookieOptions: NODE_ENV not set; defaulting secure cookie flag to false. Provide env or override secure to suppress this warning.",
8089
);
8190
}
8291
}
8392

8493
return merged;
85-
};
94+
};

0 commit comments

Comments
 (0)