-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from rudderlabs/feature/store_cookie
adding cookie support
- Loading branch information
Showing
8 changed files
with
2,398 additions
and
569 deletions.
There are no files selected for viewing
2,588 changes: 2,068 additions & 520 deletions
2,588
rudder-client-javascript/analytics/dist/browser.js
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
rudder-client-javascript/analytics/utils/storage/cookie.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import clone from "@ndhoule/clone"; | ||
import cookie from "component-cookie"; | ||
import defaults from "@ndhoule/defaults"; | ||
import json from "json3"; | ||
import topDomain from "@segment/top-domain"; | ||
|
||
/** | ||
* An object utility to persist values in cookies | ||
*/ | ||
class CookieLocal { | ||
constructor(options) { | ||
this._options = {}; | ||
this.options(options); | ||
} | ||
|
||
/** | ||
* | ||
* @param {*} options | ||
*/ | ||
options(options = {}) { | ||
if (arguments.length === 0) return this._options; | ||
|
||
let domain = "." + topDomain(window.location.href); | ||
if (domain === ".") domain = null; | ||
|
||
// the default maxage and path | ||
this._options = defaults(options, { | ||
maxage: 31536000000, | ||
path: "/", | ||
domain: domain | ||
}); | ||
|
||
//try setting a cookie first | ||
this.set("test_rudder", true); | ||
if (!this.get("test_rudder")) { | ||
this._options.domain = null; | ||
} | ||
this.remove("test_rudder"); | ||
} | ||
|
||
/** | ||
* | ||
* @param {*} key | ||
* @param {*} value | ||
*/ | ||
set(key, value) { | ||
try { | ||
value = json.stringify(value); | ||
cookie(key, value, clone(this._options)); | ||
return true; | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* @param {*} key | ||
*/ | ||
get(key) { | ||
try { | ||
let value = cookie(key); | ||
value = value ? json.parse(value) : null; | ||
return value; | ||
} catch (e) { | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* @param {*} key | ||
*/ | ||
remove(key) { | ||
try { | ||
cookie(key, null, clone(this._options)); | ||
return true; | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
// Exporting only the instance | ||
let Cookie = new CookieLocal({}); | ||
|
||
export { Cookie }; |
Oops, something went wrong.