Skip to content

Commit

Permalink
Merge pull request #10 from dbpkgs/feat/add-parameters-validation
Browse files Browse the repository at this point in the history
feat: Add actual validation of parameters
  • Loading branch information
sheldon-welinga authored Mar 25, 2023
2 parents a893d60 + 8de2094 commit da2496b
Show file tree
Hide file tree
Showing 4 changed files with 235 additions and 100 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# CHANGELOG

## 1.2.0 (May 10, 2022)
## 1.2.2 (Mar 26, 2023)

- Tighten validation of parameters by adding actual field validation rather than depending on typescript types

## 1.2.1 (May 10, 2022)

- Add support for applications with both server and client rendering like nextjs and gatsby
- Bug fix for multiple cookie values
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dbpkgs/cookie",
"version": "1.2.1",
"version": "1.2.2",
"private": false,
"description": "A lightweight browser cookie for frontend applications",
"main": "lib/index.js",
Expand Down
32 changes: 28 additions & 4 deletions src/Cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ interface Options {
export const encode = global?.window?.encodeURIComponent;
export const decode = global?.window?.decodeURIComponent;

export const errorsSchema = {
key: '"key" must be a string',
options: '"options" value must be an object',
options_domain: '"options.domain" must be a string',
options_expires: '"options.expires" must be a valid date',
options_path: '"options.path" must be a string',
options_secure: '"options.secure" must be a boolean',
value: '"value" must be a string',
};

export default class Cookie {
private doc: Partial<Document> | undefined | string;
constructor(domDocument?: Partial<Document & { cookie: string }> | string) {
Expand All @@ -22,6 +32,16 @@ export default class Cookie {
if (typeof this.doc.cookie !== 'string') this.doc.cookie = '';
}

private validateOptions = (options?: Options): void => {
if (options && typeof options !== 'object') throw new Error(errorsSchema.options);
if (options && options.domain && typeof options.domain !== 'string') throw new Error(errorsSchema.options_domain);
if (options && options.expires && !(options.expires instanceof Date)) throw new Error(errorsSchema.options_expires);
if (options && options.path && typeof options.path !== 'string') throw new Error(errorsSchema.options_path);
if (options && typeof options.secure !== 'undefined' && typeof options.secure !== 'boolean') {
throw new Error(errorsSchema.options_secure);
}
};

/**
* This method will get your cookies from the browser with the specified key
*
Expand All @@ -34,6 +54,8 @@ export default class Cookie {
*
*/
get = (key: string): string | null => {
if (typeof key !== 'string') throw new Error(errorsSchema.key);

if (typeof this.doc === 'object' && this.doc.cookie) {
const splittedCookie = this.doc.cookie.split(/;\cookieString*/);
for (let cookieIndex = 0; cookieIndex < splittedCookie.length; cookieIndex++) {
Expand Down Expand Up @@ -75,6 +97,10 @@ export default class Cookie {
*
*/
set = (key: string, value: string, options?: Options): string | undefined => {
if (typeof key !== 'string') throw new Error(errorsSchema.key);
if (typeof value !== 'string') throw new Error(errorsSchema.value);
this.validateOptions(options);

let opts: Options | undefined = options;

if (!opts) opts = {};
Expand All @@ -100,14 +126,12 @@ export default class Cookie {
* cookie.remove("session_value")
*/
remove = (key: string): void => {
if (typeof key !== 'string') throw new Error(errorsSchema.key);

let cookieString = encode(key) + '=';
cookieString += '; expires=' + new Date(0);
if (typeof this.doc === 'object') {
this.doc.cookie = cookieString;
}
};
}

// const cookie = new Cookie(global?.window?.document);
// export const Cookies = Cookie;
// export default cookie;
Loading

0 comments on commit da2496b

Please sign in to comment.