Skip to content

Commit da2496b

Browse files
Merge pull request #10 from dbpkgs/feat/add-parameters-validation
feat: Add actual validation of parameters
2 parents a893d60 + 8de2094 commit da2496b

4 files changed

Lines changed: 235 additions & 100 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# CHANGELOG
22

3-
## 1.2.0 (May 10, 2022)
3+
## 1.2.2 (Mar 26, 2023)
4+
5+
- Tighten validation of parameters by adding actual field validation rather than depending on typescript types
6+
7+
## 1.2.1 (May 10, 2022)
48

59
- Add support for applications with both server and client rendering like nextjs and gatsby
610
- Bug fix for multiple cookie values

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@dbpkgs/cookie",
3-
"version": "1.2.1",
3+
"version": "1.2.2",
44
"private": false,
55
"description": "A lightweight browser cookie for frontend applications",
66
"main": "lib/index.js",

src/Cookie.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ interface Options {
88
export const encode = global?.window?.encodeURIComponent;
99
export const decode = global?.window?.decodeURIComponent;
1010

11+
export const errorsSchema = {
12+
key: '"key" must be a string',
13+
options: '"options" value must be an object',
14+
options_domain: '"options.domain" must be a string',
15+
options_expires: '"options.expires" must be a valid date',
16+
options_path: '"options.path" must be a string',
17+
options_secure: '"options.secure" must be a boolean',
18+
value: '"value" must be a string',
19+
};
20+
1121
export default class Cookie {
1222
private doc: Partial<Document> | undefined | string;
1323
constructor(domDocument?: Partial<Document & { cookie: string }> | string) {
@@ -22,6 +32,16 @@ export default class Cookie {
2232
if (typeof this.doc.cookie !== 'string') this.doc.cookie = '';
2333
}
2434

35+
private validateOptions = (options?: Options): void => {
36+
if (options && typeof options !== 'object') throw new Error(errorsSchema.options);
37+
if (options && options.domain && typeof options.domain !== 'string') throw new Error(errorsSchema.options_domain);
38+
if (options && options.expires && !(options.expires instanceof Date)) throw new Error(errorsSchema.options_expires);
39+
if (options && options.path && typeof options.path !== 'string') throw new Error(errorsSchema.options_path);
40+
if (options && typeof options.secure !== 'undefined' && typeof options.secure !== 'boolean') {
41+
throw new Error(errorsSchema.options_secure);
42+
}
43+
};
44+
2545
/**
2646
* This method will get your cookies from the browser with the specified key
2747
*
@@ -34,6 +54,8 @@ export default class Cookie {
3454
*
3555
*/
3656
get = (key: string): string | null => {
57+
if (typeof key !== 'string') throw new Error(errorsSchema.key);
58+
3759
if (typeof this.doc === 'object' && this.doc.cookie) {
3860
const splittedCookie = this.doc.cookie.split(/;\cookieString*/);
3961
for (let cookieIndex = 0; cookieIndex < splittedCookie.length; cookieIndex++) {
@@ -75,6 +97,10 @@ export default class Cookie {
7597
*
7698
*/
7799
set = (key: string, value: string, options?: Options): string | undefined => {
100+
if (typeof key !== 'string') throw new Error(errorsSchema.key);
101+
if (typeof value !== 'string') throw new Error(errorsSchema.value);
102+
this.validateOptions(options);
103+
78104
let opts: Options | undefined = options;
79105

80106
if (!opts) opts = {};
@@ -100,14 +126,12 @@ export default class Cookie {
100126
* cookie.remove("session_value")
101127
*/
102128
remove = (key: string): void => {
129+
if (typeof key !== 'string') throw new Error(errorsSchema.key);
130+
103131
let cookieString = encode(key) + '=';
104132
cookieString += '; expires=' + new Date(0);
105133
if (typeof this.doc === 'object') {
106134
this.doc.cookie = cookieString;
107135
}
108136
};
109137
}
110-
111-
// const cookie = new Cookie(global?.window?.document);
112-
// export const Cookies = Cookie;
113-
// export default cookie;

0 commit comments

Comments
 (0)