@@ -8,6 +8,16 @@ interface Options {
88export const encode = global ?. window ?. encodeURIComponent ;
99export 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+
1121export 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 ( / ; \co o k i e S t r i n g * / ) ;
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