Skip to content

Commit

Permalink
fix types
Browse files Browse the repository at this point in the history
  • Loading branch information
Bluzzi committed Dec 25, 2024
1 parent 76e5a86 commit 59ff3fd
Show file tree
Hide file tree
Showing 28 changed files with 68 additions and 68 deletions.
20 changes: 10 additions & 10 deletions src/dom/dom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ describe("domCookie functions", () => {

expect(getAllCookies()).toEqual([
{ name: "foo", value: "bar" },
{ name: "bar", value: "foo" }
{ name: "bar", value: "foo" },
]);
});

it("must throw an error when setting a cookie of more than 4096 bytes", () => {
// 4096 bytes: large=a*4082; Path=/
expect(() => setCookie({ name: "large", value: "a".repeat(4082) })).not.toThrowError(
"The size of this cookie is greater than 4096 bytes, most browsers limit the number of cookies to this size"
expect(() => { setCookie({ name: "large", value: "a".repeat(4082) }); }).not.toThrowError(

Check failure on line 46 in src/dom/dom.test.ts

View workflow job for this annotation

GitHub Actions / ESLint

This line has 2 statements. Maximum allowed is 1
"The size of this cookie is greater than 4096 bytes, most browsers limit the number of cookies to this size",
);

// 4097 bytes: large=a*4083; Path=/
expect(() => setCookie({ name: "large", value: "a".repeat(4083) })).toThrowError(
"The size of this cookie is greater than 4096 bytes, most browsers limit the number of cookies to this size"
expect(() => { setCookie({ name: "large", value: "a".repeat(4083) }); }).toThrowError(

Check failure on line 51 in src/dom/dom.test.ts

View workflow job for this annotation

GitHub Actions / ESLint

This line has 2 statements. Maximum allowed is 1
"The size of this cookie is greater than 4096 bytes, most browsers limit the number of cookies to this size",
);
});

Expand All @@ -58,13 +58,13 @@ describe("domCookie functions", () => {
for (let i = 0; i < 49; i++) setCookie({ name: `cookie${i}`, value: `${i}` });

Check failure on line 58 in src/dom/dom.test.ts

View workflow job for this annotation

GitHub Actions / ESLint

Unary operator '++' used

Check failure on line 58 in src/dom/dom.test.ts

View workflow job for this annotation

GitHub Actions / ESLint

Invalid type "number" of template literal expression

Check failure on line 58 in src/dom/dom.test.ts

View workflow job for this annotation

GitHub Actions / ESLint

Invalid type "number" of template literal expression

// 50th cookie:
expect(() => setCookie({ name: "bar", value: "foo" })).not.toThrowError(
"You have more than 50 cookies, most browsers limit the number of cookies to 50"
expect(() => { setCookie({ name: "bar", value: "foo" }); }).not.toThrowError(

Check failure on line 61 in src/dom/dom.test.ts

View workflow job for this annotation

GitHub Actions / ESLint

This line has 2 statements. Maximum allowed is 1
"You have more than 50 cookies, most browsers limit the number of cookies to 50",
);

// 51th cookie:
expect(() => setCookie({ name: "foo", value: "bar" })).toThrowError(
"You have more than 50 cookies, most browsers limit the number of cookies to 50"
expect(() => { setCookie({ name: "foo", value: "bar" }); }).toThrowError(

Check failure on line 66 in src/dom/dom.test.ts

View workflow job for this annotation

GitHub Actions / ESLint

This line has 2 statements. Maximum allowed is 1
"You have more than 50 cookies, most browsers limit the number of cookies to 50",
);
});
});
});
2 changes: 1 addition & 1 deletion src/dom/get-all/get-all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ export function getAllCookies(): Cookie[] {
isCookieEnabled();

return parseCookies(document.cookie);
}
}
2 changes: 1 addition & 1 deletion src/dom/get-all/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { getAllCookies } from "./get-all";
export { getAllCookies } from "./get-all";
2 changes: 1 addition & 1 deletion src/dom/get/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ export function getCookie(name: string): Cookie | null {
isCookieEnabled();

return parseCookies(document.cookie).find(cookie => cookie.name === name) || null;

Check failure on line 8 in src/dom/get/get.ts

View workflow job for this annotation

GitHub Actions / ESLint

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator
}
}
2 changes: 1 addition & 1 deletion src/dom/get/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { getCookie } from "./get";
export { getCookie } from "./get";
4 changes: 2 additions & 2 deletions src/dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ export const domCookie = {
getAll: getAllCookies,
get: getCookie,
set: setCookie,
remove: removeCookie
};
remove: removeCookie,
};
2 changes: 1 addition & 1 deletion src/dom/remove/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { removeCookie } from "./remove";
export { removeCookie } from "./remove";
6 changes: 3 additions & 3 deletions src/dom/remove/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function removeCookie(name: string, options?: RemoveDomCookieOptions): vo
{ name, value: "" },
{
...options,
maxAge: CookieMaxAge.Now
}
maxAge: CookieMaxAge.Now,
},
);
}
}
2 changes: 1 addition & 1 deletion src/dom/set/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { setCookie } from "./set";
export { setCookie } from "./set";
2 changes: 1 addition & 1 deletion src/dom/set/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ export function setCookie(cookie: Cookie, options: DomCookieOptions = {}): void
}

document.cookie = serializeCookie(cookie, options);
}
}
4 changes: 2 additions & 2 deletions src/http/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import { serializeCookie } from "./serializer";

export const httpCookie = {
parse: parseCookies,
serialize: serializeCookie
};
serialize: serializeCookie,
};
16 changes: 8 additions & 8 deletions src/http/parser/_test/parser.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,28 @@ import type { CookieMock } from "./parser.type";
export const singleCookie: CookieMock = {
string: "sessionID=1234",
object: [
{ name: "sessionID", value: "1234" }
]
{ name: "sessionID", value: "1234" },
],
};

export const multipleCookies: CookieMock = {
string: "sessionID=1234; username=john_doe; auth_token=abc123",
object: [
{ name: "sessionID", value: "1234" },
{ name: "username", value: "john_doe" },
{ name: "auth_token", value: "abc123" }
]
{ name: "auth_token", value: "abc123" },
],
};

export const whitespaceCookies: CookieMock = {
string: " sessionID = 1234 ; username = john_doe ; auth_token=abc123 ",
object: multipleCookies.object
object: multipleCookies.object,
};

export const encodedCookies: CookieMock = {
string: "name=John%20Doe; age=25",
object: [
{ name: "name", value: "John Doe" },
{ name: "age", value: "25" }
]
};
{ name: "age", value: "25" },
],
};
2 changes: 1 addition & 1 deletion src/http/parser/_test/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ describe("parseCookies", () => {
it("decodes encoded cookie values", () => {
expect(parseCookies(encodedCookies.string)).toEqual(encodedCookies.object);
});
});
});
2 changes: 1 addition & 1 deletion src/http/parser/_test/parser.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ import type { Cookie } from "#/typing/cookie";
export type CookieMock = {
string: string;
object: Cookie[];
}
};
2 changes: 1 addition & 1 deletion src/http/parser/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { parseCookies } from "./parser";
export { parseCookies } from "./parser";
6 changes: 3 additions & 3 deletions src/http/parser/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ export function parseCookies(cookies: string): Cookie[] {

return cookies
.split(";")
.filter(str => (/^\s*([\w!#$%&'*+\-.^_`|~]+)\s*=\s*([^;]*)\s*$/).test(str) === true)
.map(cookie => {
.filter(str => (/^\s*([\w!#$%&'*+\-.^_`|~]+)\s*=\s*([^;]*)\s*$/).test(str))
.map((cookie) => {
const [rawName, rawValue] = cookie.split("=");

const name = rawName.trim();

Check failure on line 15 in src/http/parser/parser.ts

View workflow job for this annotation

GitHub Actions / TSup build

'rawName' is possibly 'undefined'.

Check failure on line 15 in src/http/parser/parser.ts

View workflow job for this annotation

GitHub Actions / TS Typecheck

'rawName' is possibly 'undefined'.
const value = decodeURIComponent(rawValue.trim());

Check failure on line 16 in src/http/parser/parser.ts

View workflow job for this annotation

GitHub Actions / TSup build

'rawValue' is possibly 'undefined'.

Check failure on line 16 in src/http/parser/parser.ts

View workflow job for this annotation

GitHub Actions / TS Typecheck

'rawValue' is possibly 'undefined'.

return { name, value };
});
}
}
4 changes: 2 additions & 2 deletions src/http/serializer/_test/serializer.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Cookie } from "#/typing/cookie";

export const cookie: Cookie = {
name: "sessionID",
value: "1234"
value: "1234",
};

export const cookieString = "sessionID=1234";
export const cookieString = "sessionID=1234";
20 changes: 10 additions & 10 deletions src/http/serializer/_test/serializer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,55 +9,55 @@ describe("serializeCookie", () => {

it("should include 'Max-Age' when provided in the options", () => {
const result = serializeCookie(cookie, {
maxAge: 3600
maxAge: 3600,
});

expect(result).toBe(`${cookieString}; Max-Age=3600; Path=/`);
});

it("should include 'Expires' when provided in the options", () => {
const result = serializeCookie(cookie, {
expires: new Date("2023-04-23T23:59:59Z")
expires: new Date("2023-04-23T23:59:59Z"),
});

expect(result).toBe(`${cookieString}; Expires=Sun, 23 Apr 2023 23:59:59 GMT; Path=/`);
});

it("should include 'Domain' when provided in the options", () => {
const result = serializeCookie(cookie, {
domain: "example.com"
domain: "example.com",
});

expect(result).toBe(`${cookieString}; Domain=example.com; Path=/`);
});

it("should include 'Path' when provided in the options", () => {
const result = serializeCookie(cookie, {
path: "/path"
path: "/path",
});

expect(result).toBe(`${cookieString}; Path=/path`);
});

it("should include 'Secure' when provided in the options", () => {
const result = serializeCookie(cookie, {
secure: true
secure: true,
});

expect(result).toBe(`${cookieString}; Path=/; Secure`);
});

it("should include 'HttpOnly' when provided in the options", () => {
const result = serializeCookie(cookie, {
httpOnly: true
httpOnly: true,
});

expect(result).toBe(`${cookieString}; Path=/; HttpOnly`);
});

it("should include 'SameSite' when provided in the options", () => {
const result = serializeCookie(cookie, {
sameSite: "lax"
sameSite: "lax",
});

expect(result).toBe(`${cookieString}; Path=/; SameSite=Lax`);
Expand All @@ -71,14 +71,14 @@ describe("serializeCookie", () => {
path: "/path",
secure: true,
httpOnly: true,
sameSite: "lax"
sameSite: "lax",
});

const expected = [
`${cookieString}; Max-Age=3600; Expires=Sun, 23 Apr 2023 23:59:59 GMT;`,
"Domain=example.com; Path=/path; Secure; HttpOnly; SameSite=Lax"
"Domain=example.com; Path=/path; Secure; HttpOnly; SameSite=Lax",
].join(" ");

expect(result).toBe(expected);
});
});
});
2 changes: 1 addition & 1 deletion src/http/serializer/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { serializeCookie } from "./serializer";
export { serializeCookie } from "./serializer";
2 changes: 1 addition & 1 deletion src/http/serializer/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ export function serializeCookie(cookie: Cookie, options: HttpCookieOptions = {})
if (options.sameSite) parts.push(`SameSite=${capitalizeFirstLetter(options.sameSite)}`);

return parts.join("; ");
}
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ export * from "#/dom";
export * from "#/utils/duration";

// Types:
export * from "#/typing/cookie";
export type * from "#/typing/cookie";
6 changes: 3 additions & 3 deletions src/typing/cookie.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export type Cookie = {
name: string;
value: string;
}
};

/**
* @property {number} maxAge - The maximum age of the cookie in seconds
Expand All @@ -20,9 +20,9 @@ type CookieOptions = {
secure?: boolean;
httpOnly?: boolean;
sameSite?: "strict" | "lax" | "none";
}
};

export type DomCookieOptions = Omit<CookieOptions, "httpOnly">;
export type HttpCookieOptions = CookieOptions;

export type RemoveDomCookieOptions = Pick<CookieOptions, "domain" | "path" | "sameSite">;
export type RemoveDomCookieOptions = Pick<CookieOptions, "domain" | "path" | "sameSite">;
2 changes: 1 addition & 1 deletion src/utils/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ export const cookieMaxCount = 50;

export function isCookieEnabled(): void {
if (!navigator.cookieEnabled) throw Error("Cookies are disabled by the browser");
}
}
2 changes: 1 addition & 1 deletion src/utils/duration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ export enum CookieMaxAge {
ThreeMonths = 90 * 24 * 60 * 60, // 3 months
SixMonths = 180 * 24 * 60 * 60, // 6 months
OneYear = 365 * 24 * 60 * 60, // 1 year
}
}
2 changes: 1 addition & 1 deletion src/utils/string/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from "./string.util";
export * from "./string.util";
2 changes: 1 addition & 1 deletion src/utils/string/string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ describe("getByteSize", () => {
it("should return 0 for empty string", () => {
expect(getByteSize("")).toBe(0);
});
});
});
2 changes: 1 addition & 1 deletion src/utils/string/string.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ export function getByteSize(content: string): number {
const encoder = new TextEncoder();

return encoder.encode(content).byteLength;
}
}
14 changes: 7 additions & 7 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ export default defineConfig({
environment: "jsdom",
environmentOptions: {
jsdom: {
url: "https://example.com"
}
}
url: "https://example.com",
},
},
},
resolve: {
alias: [
{ find: "#", replacement: resolve(__dirname, "src") }
]
}
});
{ find: "#", replacement: resolve(__dirname, "src") },
],
},
});

0 comments on commit 59ff3fd

Please sign in to comment.