@rebuy/rebuy 1.6.0-alpha.2 → 2.0.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api.d.ts +1 -0
- package/dist/api.d.ts.map +1 -1
- package/dist/api.js +131 -0
- package/dist/api.js.map +7 -0
- package/dist/api.mjs +111 -0
- package/dist/api.mjs.map +7 -0
- package/dist/client.d.ts +1 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +145 -0
- package/dist/client.js.map +7 -0
- package/dist/client.mjs +115 -0
- package/dist/client.mjs.map +7 -0
- package/dist/cookie.js +139 -0
- package/dist/cookie.js.map +7 -0
- package/dist/cookie.mjs +119 -0
- package/dist/cookie.mjs.map +7 -0
- package/dist/geolocation.d.ts +2 -0
- package/dist/geolocation.d.ts.map +1 -1
- package/dist/geolocation.js +79 -0
- package/dist/geolocation.js.map +7 -0
- package/dist/geolocation.mjs +49 -0
- package/dist/geolocation.mjs.map +7 -0
- package/dist/identity.d.ts +4 -3
- package/dist/identity.d.ts.map +1 -1
- package/dist/identity.js +86 -0
- package/dist/identity.js.map +7 -0
- package/dist/identity.mjs +56 -0
- package/dist/identity.mjs.map +7 -0
- package/dist/index.js +8 -773
- package/dist/index.js.map +4 -4
- package/dist/index.mjs +7 -766
- package/dist/index.mjs.map +4 -4
- package/dist/session.js +72 -0
- package/dist/session.js.map +7 -0
- package/dist/session.mjs +42 -0
- package/dist/session.mjs.map +7 -0
- package/dist/utilities.js +321 -0
- package/dist/utilities.js.map +7 -0
- package/dist/utilities.mjs +301 -0
- package/dist/utilities.mjs.map +7 -0
- package/package.json +14 -1
package/dist/cookie.js
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var cookie_exports = {};
|
|
20
|
+
__export(cookie_exports, {
|
|
21
|
+
default: () => cookie_default,
|
|
22
|
+
destroy: () => destroy,
|
|
23
|
+
enabled: () => enabled,
|
|
24
|
+
find: () => find,
|
|
25
|
+
get: () => get,
|
|
26
|
+
getAll: () => getAll,
|
|
27
|
+
set: () => set
|
|
28
|
+
});
|
|
29
|
+
module.exports = __toCommonJS(cookie_exports);
|
|
30
|
+
var import_utilities = require("~/utilities");
|
|
31
|
+
function get(name) {
|
|
32
|
+
if (typeof document == "undefined" || !document.cookie) {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
const cookie = document.cookie.match("(^|;) ?" + decodeURIComponent(name) + "=([^;]*)(;|$)");
|
|
36
|
+
let value = null;
|
|
37
|
+
if (cookie != null) {
|
|
38
|
+
const data = decodeURIComponent(cookie[2]);
|
|
39
|
+
const decode = (0, import_utilities.isBase64Encoded)(data) ? true : false;
|
|
40
|
+
value = (0, import_utilities.stringToData)(data, decode);
|
|
41
|
+
}
|
|
42
|
+
return value;
|
|
43
|
+
}
|
|
44
|
+
function getAll() {
|
|
45
|
+
const cookies = {};
|
|
46
|
+
if (document && document.cookie && document.cookie !== "") {
|
|
47
|
+
const split = document.cookie.split(";");
|
|
48
|
+
for (let i = 0; i < split.length; i++) {
|
|
49
|
+
const pairs = split[i].split("=");
|
|
50
|
+
pairs[0] = pairs[0].replace(/^ /, "");
|
|
51
|
+
const key = decodeURIComponent(pairs[0]);
|
|
52
|
+
const value = decodeURIComponent(pairs[1]);
|
|
53
|
+
const decode = (0, import_utilities.isBase64Encoded)(value) ? true : false;
|
|
54
|
+
cookies[key] = (0, import_utilities.stringToData)(value, decode);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return cookies;
|
|
58
|
+
}
|
|
59
|
+
function set(name, value, config) {
|
|
60
|
+
if (typeof document == "undefined" || !document.cookie) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
const attributes = ["path", "domain", "maxAge", "expires", "secure", "sameSite"];
|
|
64
|
+
const convenienceTimes = ["seconds", "minutes", "hours", "days", "weeks", "months", "years"];
|
|
65
|
+
const cookieAttributes = {
|
|
66
|
+
path: "/"
|
|
67
|
+
};
|
|
68
|
+
if (typeof config != "undefined" && Number.isInteger(config)) {
|
|
69
|
+
cookieAttributes["max-age"] = config;
|
|
70
|
+
} else if (typeof config === "object" && config !== null) {
|
|
71
|
+
for (const key in config) {
|
|
72
|
+
if (attributes.includes(key)) {
|
|
73
|
+
if (key === "maxAge") {
|
|
74
|
+
cookieAttributes["max-age"] = config[key];
|
|
75
|
+
} else if (key === "sameSite") {
|
|
76
|
+
cookieAttributes["samesite"] = config[key];
|
|
77
|
+
} else if (key === "expires") {
|
|
78
|
+
cookieAttributes[key] = new Date(config[key]).toUTCString();
|
|
79
|
+
} else {
|
|
80
|
+
cookieAttributes[key] = config[key];
|
|
81
|
+
}
|
|
82
|
+
} else if (convenienceTimes.includes(key)) {
|
|
83
|
+
let duration = config[key];
|
|
84
|
+
if (key === "seconds") {
|
|
85
|
+
duration = duration * 1;
|
|
86
|
+
} else if (key === "minutes") {
|
|
87
|
+
duration = duration * 60;
|
|
88
|
+
} else if (key === "hours") {
|
|
89
|
+
duration = duration * 60 * 60;
|
|
90
|
+
} else if (key === "days") {
|
|
91
|
+
duration = duration * 60 * 60 * 24;
|
|
92
|
+
} else if (key === "weeks") {
|
|
93
|
+
duration = duration * 60 * 60 * 24 * 7;
|
|
94
|
+
} else if (key === "months") {
|
|
95
|
+
duration = duration * 60 * 60 * 24 * 30;
|
|
96
|
+
} else if (key === "years") {
|
|
97
|
+
duration = duration * 60 * 60 * 24 * 365;
|
|
98
|
+
}
|
|
99
|
+
cookieAttributes["max-age"] = duration;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
value = (0, import_utilities.dataToString)(value, config?.encode);
|
|
104
|
+
let cookie = encodeURIComponent(name) + "=" + encodeURIComponent(value);
|
|
105
|
+
for (const key in cookieAttributes) {
|
|
106
|
+
cookie += ";" + key + "=" + cookieAttributes[key];
|
|
107
|
+
}
|
|
108
|
+
document.cookie = cookie;
|
|
109
|
+
}
|
|
110
|
+
function find(name) {
|
|
111
|
+
const matches = [];
|
|
112
|
+
const cookies = getAll();
|
|
113
|
+
for (const key in cookies) {
|
|
114
|
+
if (key.includes(name)) {
|
|
115
|
+
matches.push({
|
|
116
|
+
name: key,
|
|
117
|
+
value: cookies[key]
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return matches;
|
|
122
|
+
}
|
|
123
|
+
function destroy(name) {
|
|
124
|
+
set(name, "", { encode: false, seconds: 0 });
|
|
125
|
+
}
|
|
126
|
+
function enabled() {
|
|
127
|
+
const test = {
|
|
128
|
+
key: "__cookie_test",
|
|
129
|
+
value: 1
|
|
130
|
+
};
|
|
131
|
+
set(test.key, test.value, { encode: false });
|
|
132
|
+
const enabled2 = get(test.key) === test.value ? true : false;
|
|
133
|
+
if (enabled2) {
|
|
134
|
+
destroy(test.key);
|
|
135
|
+
}
|
|
136
|
+
return enabled2;
|
|
137
|
+
}
|
|
138
|
+
var cookie_default = { destroy, enabled, find, get, getAll, set };
|
|
139
|
+
//# sourceMappingURL=cookie.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/cookie.ts"],
|
|
4
|
+
"sourcesContent": ["/* eslint-disable @typescript-eslint/no-explicit-any */\n\nimport { dataToString, isBase64Encoded, stringToData } from '~/utilities';\n\n/**\n * Get a cookie by name\n *\n * @param {string} name - The name of the cookie to get\n *\n * @returns {any} The value of the cookie or null if the cookie is not found\n */\nexport function get(name: string): any | null {\n if (typeof document == 'undefined' || !document.cookie) {\n return null;\n }\n\n // Get cookie\n const cookie = document.cookie.match('(^|;) ?' + decodeURIComponent(name) + '=([^;]*)(;|$)');\n\n let value: any = null;\n\n if (cookie != null) {\n // Get data\n const data = decodeURIComponent(cookie[2]);\n\n // Auto decode\n const decode = isBase64Encoded(data) ? true : false;\n\n // Convert to data object\n value = stringToData(data, decode);\n }\n\n return value;\n}\n\n/**\n * Get all cookies\n *\n * @returns {Record<string, any>} All cookies\n */\nexport function getAll(): Record<string, any> {\n const cookies: Record<string, any> = {};\n\n if (document && document.cookie && document.cookie !== '') {\n const split = document.cookie.split(';');\n\n for (let i = 0; i < split.length; i++) {\n const pairs = split[i].split('=');\n\n pairs[0] = pairs[0].replace(/^ /, '');\n\n const key = decodeURIComponent(pairs[0]);\n const value = decodeURIComponent(pairs[1]);\n\n // Auto decode\n const decode = isBase64Encoded(value) ? true : false;\n\n cookies[key] = stringToData(value, decode);\n }\n }\n\n return cookies;\n}\n\n/**\n * Set a cookie\n *\n * @param {string} name - The name of the cookie\n * @param {any} value - The value of the cookie\n * @param {any} config - The configuration of the cookie\n *\n * @returns {void}\n */\nexport function set(name: string, value: any, config: any): void {\n if (typeof document == 'undefined' || !document.cookie) {\n return;\n }\n\n const attributes = ['path', 'domain', 'maxAge', 'expires', 'secure', 'sameSite'];\n const convenienceTimes = ['seconds', 'minutes', 'hours', 'days', 'weeks', 'months', 'years'];\n\n const cookieAttributes: Record<string, any> = {\n path: '/',\n };\n\n if (typeof config != 'undefined' && Number.isInteger(config)) {\n cookieAttributes['max-age'] = config;\n } else if (typeof config === 'object' && config !== null) {\n for (const key in config) {\n if (attributes.includes(key)) {\n if (key === 'maxAge') {\n cookieAttributes['max-age'] = config[key];\n } else if (key === 'sameSite') {\n cookieAttributes['samesite'] = config[key];\n } else if (key === 'expires') {\n cookieAttributes[key] = new Date(config[key]).toUTCString();\n } else {\n cookieAttributes[key] = config[key];\n }\n } else if (convenienceTimes.includes(key)) {\n let duration = config[key];\n\n if (key === 'seconds') {\n duration = duration * 1;\n } else if (key === 'minutes') {\n duration = duration * 60;\n } else if (key === 'hours') {\n duration = duration * 60 * 60;\n } else if (key === 'days') {\n duration = duration * 60 * 60 * 24;\n } else if (key === 'weeks') {\n duration = duration * 60 * 60 * 24 * 7;\n } else if (key === 'months') {\n duration = duration * 60 * 60 * 24 * 30;\n } else if (key === 'years') {\n duration = duration * 60 * 60 * 24 * 365;\n }\n\n cookieAttributes['max-age'] = duration;\n }\n }\n }\n\n // Convert data to string\n value = dataToString(value, config?.encode);\n\n // Define cookie\n let cookie = encodeURIComponent(name) + '=' + encodeURIComponent(value);\n\n // Add optional cookie attributes\n for (const key in cookieAttributes) {\n cookie += ';' + key + '=' + cookieAttributes[key];\n }\n\n // Write cookie\n document.cookie = cookie;\n}\n\n/**\n * Find all cookies that match a name\n *\n * @param {string} name - The name of the cookie to find\n *\n * @returns {Array<{ name: string; value: any }>} All cookies that match the name\n */\nexport function find(name: string): { name: string; value: any }[] {\n const matches: { name: string; value: any }[] = [];\n const cookies = getAll();\n\n for (const key in cookies) {\n if (key.includes(name)) {\n matches.push({\n name: key,\n value: cookies[key],\n });\n }\n }\n\n return matches;\n}\n\n/**\n * Destroy a cookie\n *\n * @param {string} name - The name of the cookie to destroy\n */\nexport function destroy(name: string): void {\n set(name, '', { encode: false, seconds: 0 });\n}\n\n/**\n * Check if cookies are enabled\n *\n * @returns {boolean} True if cookies are enabled, false otherwise\n */\nexport function enabled(): boolean {\n const test = {\n key: '__cookie_test',\n value: 1,\n };\n\n set(test.key, test.value, { encode: false });\n\n const enabled = get(test.key) === test.value ? true : false;\n\n if (enabled) {\n destroy(test.key);\n }\n\n return enabled;\n}\n\n// eslint-disable-next-line import/no-default-export\nexport default { destroy, enabled, find, get, getAll, set };\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,uBAA4D;AASrD,SAAS,IAAI,MAA0B;AAC1C,MAAI,OAAO,YAAY,eAAe,CAAC,SAAS,QAAQ;AACpD,WAAO;AAAA,EACX;AAGA,QAAM,SAAS,SAAS,OAAO,MAAM,YAAY,mBAAmB,IAAI,IAAI,eAAe;AAE3F,MAAI,QAAa;AAEjB,MAAI,UAAU,MAAM;AAEhB,UAAM,OAAO,mBAAmB,OAAO,CAAC,CAAC;AAGzC,UAAM,aAAS,kCAAgB,IAAI,IAAI,OAAO;AAG9C,gBAAQ,+BAAa,MAAM,MAAM;AAAA,EACrC;AAEA,SAAO;AACX;AAOO,SAAS,SAA8B;AAC1C,QAAM,UAA+B,CAAC;AAEtC,MAAI,YAAY,SAAS,UAAU,SAAS,WAAW,IAAI;AACvD,UAAM,QAAQ,SAAS,OAAO,MAAM,GAAG;AAEvC,aAAS,IAAI,GAAG,IAAI,MAAM,QAAQ,KAAK;AACnC,YAAM,QAAQ,MAAM,CAAC,EAAE,MAAM,GAAG;AAEhC,YAAM,CAAC,IAAI,MAAM,CAAC,EAAE,QAAQ,MAAM,EAAE;AAEpC,YAAM,MAAM,mBAAmB,MAAM,CAAC,CAAC;AACvC,YAAM,QAAQ,mBAAmB,MAAM,CAAC,CAAC;AAGzC,YAAM,aAAS,kCAAgB,KAAK,IAAI,OAAO;AAE/C,cAAQ,GAAG,QAAI,+BAAa,OAAO,MAAM;AAAA,IAC7C;AAAA,EACJ;AAEA,SAAO;AACX;AAWO,SA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QAAQ,SAAS;AACxB,qBAAW,WAAW,KAAK,KAAK,KAAK;AAAA,QACzC;AAEA,yBAAiB,SAAS,IAAI;AAAA,MAClC;AAAA,IACJ;AAAA,EACJ;AAGA,cAAQ,+BAAa,OAAO,QAAQ,MAAM;AAG1C,MAAI,SAAS,mBAAmB,IAAI,IAAI,MAAM,mBAAmB,KAAK;AAGtE,aAAW,OAAO,kBAAkB;AAChC,cAAU,MAAM,MAAM,MAAM,iBAAiB,GAAG;AAAA,EACpD;AAGA,WAAS,SAAS;AACtB;AASO,SAAS,KAAK,MAA8C;AAC/D,QAAM,UAA0C,CAAC;AACjD,QAAM,UAAU,OAAO;AAEvB,aAAW,OAAO,SAAS;AACvB,QAAI,IAAI,SAAS,IAAI,GAAG;AACpB,cAAQ,KAAK;AAAA,QACT,MAAM;AAAA,QACN,OAAO,QAAQ,GAAG;AAAA,MACtB,CAAC;AAAA,IACL;AAAA,EACJ;AAEA,SAAO;AACX;AAOO,SAAS,QAAQ,MAAoB;AACxC,MAAI,MAAM,IAAI,EAAE,QAAQ,OAAO,SAAS,EAAE,CAAC;AAC/C;AAOO,SAAS,UAAmB;AAC/B,QAAM,OAAO;AAAA,IACT,KAAK;AAAA,IACL,OAAO;AAAA,EACX;AAEA,MAAI,KAAK,KAAK,KAAK,OAAO,EAAE,QAAQ,MAAM,CAAC;AAE3C,QAAMA,WAAU,IAAI,KAAK,GAAG,MAAM,KAAK,QAAQ,OAAO;AAEtD,MAAIA,UAAS;AACT,YAAQ,KAAK,GAAG;AAAA,EACpB;AAEA,SAAOA;AACX;AAGA,IAAO,iBAAQ,EAAE,SAAS,SAAS,MAAM,KAAK,QAAQ,IAAI;",
|
|
6
|
+
"names": ["enabled"]
|
|
7
|
+
}
|
package/dist/cookie.mjs
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { dataToString, isBase64Encoded, stringToData } from "~/utilities";
|
|
2
|
+
function get(name) {
|
|
3
|
+
if (typeof document == "undefined" || !document.cookie) {
|
|
4
|
+
return null;
|
|
5
|
+
}
|
|
6
|
+
const cookie = document.cookie.match("(^|;) ?" + decodeURIComponent(name) + "=([^;]*)(;|$)");
|
|
7
|
+
let value = null;
|
|
8
|
+
if (cookie != null) {
|
|
9
|
+
const data = decodeURIComponent(cookie[2]);
|
|
10
|
+
const decode = isBase64Encoded(data) ? true : false;
|
|
11
|
+
value = stringToData(data, decode);
|
|
12
|
+
}
|
|
13
|
+
return value;
|
|
14
|
+
}
|
|
15
|
+
function getAll() {
|
|
16
|
+
const cookies = {};
|
|
17
|
+
if (document && document.cookie && document.cookie !== "") {
|
|
18
|
+
const split = document.cookie.split(";");
|
|
19
|
+
for (let i = 0; i < split.length; i++) {
|
|
20
|
+
const pairs = split[i].split("=");
|
|
21
|
+
pairs[0] = pairs[0].replace(/^ /, "");
|
|
22
|
+
const key = decodeURIComponent(pairs[0]);
|
|
23
|
+
const value = decodeURIComponent(pairs[1]);
|
|
24
|
+
const decode = isBase64Encoded(value) ? true : false;
|
|
25
|
+
cookies[key] = stringToData(value, decode);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return cookies;
|
|
29
|
+
}
|
|
30
|
+
function set(name, value, config) {
|
|
31
|
+
if (typeof document == "undefined" || !document.cookie) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
const attributes = ["path", "domain", "maxAge", "expires", "secure", "sameSite"];
|
|
35
|
+
const convenienceTimes = ["seconds", "minutes", "hours", "days", "weeks", "months", "years"];
|
|
36
|
+
const cookieAttributes = {
|
|
37
|
+
path: "/"
|
|
38
|
+
};
|
|
39
|
+
if (typeof config != "undefined" && Number.isInteger(config)) {
|
|
40
|
+
cookieAttributes["max-age"] = config;
|
|
41
|
+
} else if (typeof config === "object" && config !== null) {
|
|
42
|
+
for (const key in config) {
|
|
43
|
+
if (attributes.includes(key)) {
|
|
44
|
+
if (key === "maxAge") {
|
|
45
|
+
cookieAttributes["max-age"] = config[key];
|
|
46
|
+
} else if (key === "sameSite") {
|
|
47
|
+
cookieAttributes["samesite"] = config[key];
|
|
48
|
+
} else if (key === "expires") {
|
|
49
|
+
cookieAttributes[key] = new Date(config[key]).toUTCString();
|
|
50
|
+
} else {
|
|
51
|
+
cookieAttributes[key] = config[key];
|
|
52
|
+
}
|
|
53
|
+
} else if (convenienceTimes.includes(key)) {
|
|
54
|
+
let duration = config[key];
|
|
55
|
+
if (key === "seconds") {
|
|
56
|
+
duration = duration * 1;
|
|
57
|
+
} else if (key === "minutes") {
|
|
58
|
+
duration = duration * 60;
|
|
59
|
+
} else if (key === "hours") {
|
|
60
|
+
duration = duration * 60 * 60;
|
|
61
|
+
} else if (key === "days") {
|
|
62
|
+
duration = duration * 60 * 60 * 24;
|
|
63
|
+
} else if (key === "weeks") {
|
|
64
|
+
duration = duration * 60 * 60 * 24 * 7;
|
|
65
|
+
} else if (key === "months") {
|
|
66
|
+
duration = duration * 60 * 60 * 24 * 30;
|
|
67
|
+
} else if (key === "years") {
|
|
68
|
+
duration = duration * 60 * 60 * 24 * 365;
|
|
69
|
+
}
|
|
70
|
+
cookieAttributes["max-age"] = duration;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
value = dataToString(value, config?.encode);
|
|
75
|
+
let cookie = encodeURIComponent(name) + "=" + encodeURIComponent(value);
|
|
76
|
+
for (const key in cookieAttributes) {
|
|
77
|
+
cookie += ";" + key + "=" + cookieAttributes[key];
|
|
78
|
+
}
|
|
79
|
+
document.cookie = cookie;
|
|
80
|
+
}
|
|
81
|
+
function find(name) {
|
|
82
|
+
const matches = [];
|
|
83
|
+
const cookies = getAll();
|
|
84
|
+
for (const key in cookies) {
|
|
85
|
+
if (key.includes(name)) {
|
|
86
|
+
matches.push({
|
|
87
|
+
name: key,
|
|
88
|
+
value: cookies[key]
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return matches;
|
|
93
|
+
}
|
|
94
|
+
function destroy(name) {
|
|
95
|
+
set(name, "", { encode: false, seconds: 0 });
|
|
96
|
+
}
|
|
97
|
+
function enabled() {
|
|
98
|
+
const test = {
|
|
99
|
+
key: "__cookie_test",
|
|
100
|
+
value: 1
|
|
101
|
+
};
|
|
102
|
+
set(test.key, test.value, { encode: false });
|
|
103
|
+
const enabled2 = get(test.key) === test.value ? true : false;
|
|
104
|
+
if (enabled2) {
|
|
105
|
+
destroy(test.key);
|
|
106
|
+
}
|
|
107
|
+
return enabled2;
|
|
108
|
+
}
|
|
109
|
+
var cookie_default = { destroy, enabled, find, get, getAll, set };
|
|
110
|
+
export {
|
|
111
|
+
cookie_default as default,
|
|
112
|
+
destroy,
|
|
113
|
+
enabled,
|
|
114
|
+
find,
|
|
115
|
+
get,
|
|
116
|
+
getAll,
|
|
117
|
+
set
|
|
118
|
+
};
|
|
119
|
+
//# sourceMappingURL=cookie.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/cookie.ts"],
|
|
4
|
+
"sourcesContent": ["/* eslint-disable @typescript-eslint/no-explicit-any */\n\nimport { dataToString, isBase64Encoded, stringToData } from '~/utilities';\n\n/**\n * Get a cookie by name\n *\n * @param {string} name - The name of the cookie to get\n *\n * @returns {any} The value of the cookie or null if the cookie is not found\n */\nexport function get(name: string): any | null {\n if (typeof document == 'undefined' || !document.cookie) {\n return null;\n }\n\n // Get cookie\n const cookie = document.cookie.match('(^|;) ?' + decodeURIComponent(name) + '=([^;]*)(;|$)');\n\n let value: any = null;\n\n if (cookie != null) {\n // Get data\n const data = decodeURIComponent(cookie[2]);\n\n // Auto decode\n const decode = isBase64Encoded(data) ? true : false;\n\n // Convert to data object\n value = stringToData(data, decode);\n }\n\n return value;\n}\n\n/**\n * Get all cookies\n *\n * @returns {Record<string, any>} All cookies\n */\nexport function getAll(): Record<string, any> {\n const cookies: Record<string, any> = {};\n\n if (document && document.cookie && document.cookie !== '') {\n const split = document.cookie.split(';');\n\n for (let i = 0; i < split.length; i++) {\n const pairs = split[i].split('=');\n\n pairs[0] = pairs[0].replace(/^ /, '');\n\n const key = decodeURIComponent(pairs[0]);\n const value = decodeURIComponent(pairs[1]);\n\n // Auto decode\n const decode = isBase64Encoded(value) ? true : false;\n\n cookies[key] = stringToData(value, decode);\n }\n }\n\n return cookies;\n}\n\n/**\n * Set a cookie\n *\n * @param {string} name - The name of the cookie\n * @param {any} value - The value of the cookie\n * @param {any} config - The configuration of the cookie\n *\n * @returns {void}\n */\nexport function set(name: string, value: any, config: any): void {\n if (typeof document == 'undefined' || !document.cookie) {\n return;\n }\n\n const attributes = ['path', 'domain', 'maxAge', 'expires', 'secure', 'sameSite'];\n const convenienceTimes = ['seconds', 'minutes', 'hours', 'days', 'weeks', 'months', 'years'];\n\n const cookieAttributes: Record<string, any> = {\n path: '/',\n };\n\n if (typeof config != 'undefined' && Number.isInteger(config)) {\n cookieAttributes['max-age'] = config;\n } else if (typeof config === 'object' && config !== null) {\n for (const key in config) {\n if (attributes.includes(key)) {\n if (key === 'maxAge') {\n cookieAttributes['max-age'] = config[key];\n } else if (key === 'sameSite') {\n cookieAttributes['samesite'] = config[key];\n } else if (key === 'expires') {\n cookieAttributes[key] = new Date(config[key]).toUTCString();\n } else {\n cookieAttributes[key] = config[key];\n }\n } else if (convenienceTimes.includes(key)) {\n let duration = config[key];\n\n if (key === 'seconds') {\n duration = duration * 1;\n } else if (key === 'minutes') {\n duration = duration * 60;\n } else if (key === 'hours') {\n duration = duration * 60 * 60;\n } else if (key === 'days') {\n duration = duration * 60 * 60 * 24;\n } else if (key === 'weeks') {\n duration = duration * 60 * 60 * 24 * 7;\n } else if (key === 'months') {\n duration = duration * 60 * 60 * 24 * 30;\n } else if (key === 'years') {\n duration = duration * 60 * 60 * 24 * 365;\n }\n\n cookieAttributes['max-age'] = duration;\n }\n }\n }\n\n // Convert data to string\n value = dataToString(value, config?.encode);\n\n // Define cookie\n let cookie = encodeURIComponent(name) + '=' + encodeURIComponent(value);\n\n // Add optional cookie attributes\n for (const key in cookieAttributes) {\n cookie += ';' + key + '=' + cookieAttributes[key];\n }\n\n // Write cookie\n document.cookie = cookie;\n}\n\n/**\n * Find all cookies that match a name\n *\n * @param {string} name - The name of the cookie to find\n *\n * @returns {Array<{ name: string; value: any }>} All cookies that match the name\n */\nexport function find(name: string): { name: string; value: any }[] {\n const matches: { name: string; value: any }[] = [];\n const cookies = getAll();\n\n for (const key in cookies) {\n if (key.includes(name)) {\n matches.push({\n name: key,\n value: cookies[key],\n });\n }\n }\n\n return matches;\n}\n\n/**\n * Destroy a cookie\n *\n * @param {string} name - The name of the cookie to destroy\n */\nexport function destroy(name: string): void {\n set(name, '', { encode: false, seconds: 0 });\n}\n\n/**\n * Check if cookies are enabled\n *\n * @returns {boolean} True if cookies are enabled, false otherwise\n */\nexport function enabled(): boolean {\n const test = {\n key: '__cookie_test',\n value: 1,\n };\n\n set(test.key, test.value, { encode: false });\n\n const enabled = get(test.key) === test.value ? true : false;\n\n if (enabled) {\n destroy(test.key);\n }\n\n return enabled;\n}\n\n// eslint-disable-next-line import/no-default-export\nexport default { destroy, enabled, find, get, getAll, set };\n"],
|
|
5
|
+
"mappings": "AAEA,SAAS,cAAc,iBAAiB,oBAAoB;AASrD,SAAS,IAAI,MAA0B;AAC1C,MAAI,OAAO,YAAY,eAAe,CAAC,SAAS,QAAQ;AACpD,WAAO;AAAA,EACX;AAGA,QAAM,SAAS,SAAS,OAAO,MAAM,YAAY,mBAAmB,IAAI,IAAI,eAAe;AAE3F,MAAI,QAAa;AAEjB,MAAI,UAAU,MAAM;AAEhB,UAAM,OAAO,mBAAmB,OAAO,CAAC,CAAC;AAGzC,UAAM,SAAS,gBAAgB,IAAI,IAAI,OAAO;AAG9C,YAAQ,aAAa,MAAM,MAAM;AAAA,EACrC;AAEA,SAAO;AACX;AAOO,SAAS,SAA8B;AAC1C,QAAM,UAA+B,CAAC;AAEtC,MAAI,YAAY,SAAS,UAAU,SAAS,WAAW,IAAI;AACv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K,OAAO,EAAE,QAAQ,MAAM,CAAC;AAE3C,QAAMA,WAAU,IAAI,KAAK,GAAG,MAAM,KAAK,QAAQ,OAAO;AAEtD,MAAIA,UAAS;AACT,YAAQ,KAAK,GAAG;AAAA,EACpB;AAEA,SAAOA;AACX;AAGA,IAAO,iBAAQ,EAAE,SAAS,SAAS,MAAM,KAAK,QAAQ,IAAI;",
|
|
6
|
+
"names": ["enabled"]
|
|
7
|
+
}
|
package/dist/geolocation.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"geolocation.d.ts","sourceRoot":"","sources":["../src/geolocation.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"geolocation.d.ts","sourceRoot":"","sources":["../src/geolocation.ts"],"names":[],"mappings":"AAcA,qBAAa,WAAW;IACpB,OAAO,CAAC,MAAM,CAAS;gBAEX,GAAG,EAAE,MAAM,GAAG,IAAI;IA0BxB,iBAAiB,IAAI,OAAO,CAAC,GAAG,CAAC;IAuBjC,WAAW,IAAI,OAAO,CAAC,GAAG,CAAC;CAOpC;AAGD,eAAe,WAAW,CAAC"}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var geolocation_exports = {};
|
|
30
|
+
__export(geolocation_exports, {
|
|
31
|
+
Geolocation: () => Geolocation,
|
|
32
|
+
default: () => geolocation_default
|
|
33
|
+
});
|
|
34
|
+
module.exports = __toCommonJS(geolocation_exports);
|
|
35
|
+
var import_api = __toESM(require("~/api"), 1);
|
|
36
|
+
var import_cookie = __toESM(require("~/cookie"), 1);
|
|
37
|
+
class Geolocation {
|
|
38
|
+
constructor(key) {
|
|
39
|
+
this.config = {
|
|
40
|
+
geolocation: null,
|
|
41
|
+
geolocationCookie: "_rebuyGeolocation",
|
|
42
|
+
geolocationDuration: {
|
|
43
|
+
minutes: 30
|
|
44
|
+
},
|
|
45
|
+
key: null
|
|
46
|
+
};
|
|
47
|
+
if (typeof document == "undefined" || !document.cookie) {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
if (typeof key == "string") {
|
|
51
|
+
this.config.key = key;
|
|
52
|
+
}
|
|
53
|
+
this.config.geolocation = import_cookie.default.get(this.config.geolocationCookie);
|
|
54
|
+
if (this.config.geolocation === null) {
|
|
55
|
+
this._fetchGeolocation();
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
async _fetchGeolocation() {
|
|
59
|
+
const api = new import_api.default({ key: this.config.key });
|
|
60
|
+
const response = await api.callGeo("GET", "/", null);
|
|
61
|
+
if (response.data) {
|
|
62
|
+
this.config.geolocation = response.data;
|
|
63
|
+
const cookieOptions = {
|
|
64
|
+
secure: true
|
|
65
|
+
};
|
|
66
|
+
Object.assign(cookieOptions, this.config.geolocationDuration);
|
|
67
|
+
import_cookie.default.set(this.config.geolocationCookie, this.config.geolocation, { ...cookieOptions, encode: false });
|
|
68
|
+
}
|
|
69
|
+
return this.config.geolocation;
|
|
70
|
+
}
|
|
71
|
+
async geolocation() {
|
|
72
|
+
if (this.config.geolocation == null) {
|
|
73
|
+
await this._fetchGeolocation();
|
|
74
|
+
}
|
|
75
|
+
return this.config.geolocation;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
var geolocation_default = Geolocation;
|
|
79
|
+
//# sourceMappingURL=geolocation.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/geolocation.ts"],
|
|
4
|
+
"sourcesContent": ["/* eslint-disable @typescript-eslint/no-explicit-any */\n\nimport Api from '~/api';\nimport Cookie from '~/cookie';\n\ntype Config = {\n geolocation: any;\n geolocationCookie: string;\n geolocationDuration: {\n minutes: number;\n };\n key: string | null;\n};\n\nexport class Geolocation {\n private config: Config;\n\n constructor(key: string | null) {\n this.config = {\n geolocation: null,\n geolocationCookie: '_rebuyGeolocation',\n geolocationDuration: {\n minutes: 30,\n },\n key: null,\n };\n\n if (typeof document == 'undefined' || !document.cookie) {\n return;\n }\n\n if (typeof key == 'string') {\n this.config.key = key;\n }\n\n this.config.geolocation = Cookie.get(this.config.geolocationCookie);\n\n // Create a new geolocation (if needed)\n if (this.config.geolocation === null) {\n this._fetchGeolocation();\n }\n }\n\n async _fetchGeolocation(): Promise<any> {\n const api = new Api({ key: this.config.key });\n const response = await api.callGeo('GET', '/', null);\n\n if (response.data) {\n // Update instance config with geolocation\n this.config.geolocation = response.data;\n\n // Write cookie with geolocation\n const cookieOptions: Record<string, any> = {\n secure: true,\n };\n\n // Merge cookie options with geolocation config\n Object.assign(cookieOptions, this.config.geolocationDuration);\n\n // Write cookie with geolocation\n Cookie.set(this.config.geolocationCookie, this.config.geolocation, { ...cookieOptions, encode: false });\n }\n\n return this.config.geolocation;\n }\n\n async geolocation(): Promise<any> {\n if (this.config.geolocation == null) {\n await this._fetchGeolocation();\n }\n\n return this.config.geolocation;\n }\n}\n\n// eslint-disable-next-line import/no-default-export\nexport default Geolocation;\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,iBAAgB;AAChB,oBAAmB;AAWZ,MAAM,YAAY;AAAA,EAGrB,YAAY,KAAoB;AAC5B,SAAK,SAAS;AAAA,MACV,aAAa;AAAA,MACb,mBAAmB;AAAA,MACnB,qBAAqB;AAAA,QACjB,SAAS;AAAA,MACb;AAAA,MACA,KAAK;AAAA,IACT;AAEA,QAAI,OAAO,YAAY,eAAe,CAAC,SAAS,QAAQ;AACpD;AAAA,IACJ;AAEA,QAAI,OAAO,OAAO,UAAU;AACxB,WAAK,OAAO,MAAM;AAAA,IACtB;AAEA,SAAK,OAAO,cAAc,cAAAA,QAAO,IAAI,KAAK,OAAO,iBAAiB;AAGlE,QAAI,KAAK,OAAO,gBAAgB,MAAM;AAClC,WAAK,kBAAkB;AAAA,IAC3B;AAAA,EACJ;AAAA,EAEA,MAAM,oBAAkC;AACpC,UAAM,MAAM,IAAI,WAAAC,QAAI,EAAE,KAAK,KAAK,OAAO,IAAI,CAAC;AAC5C,UAAM,WAAW,MAAM,IAAI,QAAQ,OAAO,KAAK,IAAI;AAEnD,QAAI,SAAS,MAAM;AAEf,WAAK,OAAO,cAAc,SAAS;AAGnC,YAAM,gBAAqC;AAAA,QACvC,QAAQ;AAAA,MACZ;AAGA,aAAO,OAAO,eAAe,KAAK,OAAO,mBAAmB;AAG5D,oBAAAD,QAAO,IAAI,KAAK,OAAO,mBAAmB,KAAK,OAAO,aAAa,EAAE,GAAG,eAAe,QAAQ,MAAM,CAAC;AAAA,IAC1G;AAEA,WAAO,KAAK,OAAO;AAAA,EACvB;AAAA,EAEA,MAAM,cAA4B;AAC9B,QAAI,KAAK,OAAO,eAAe,MAAM;AACjC,YAAM,KAAK,kBAAkB;AAAA,IACjC;AAEA,WAAO,KAAK,OAAO;AAAA,EACvB;AACJ;AAGA,IAAO,sBAAQ;",
|
|
6
|
+
"names": ["Cookie", "Api"]
|
|
7
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import Api from "~/api";
|
|
2
|
+
import Cookie from "~/cookie";
|
|
3
|
+
class Geolocation {
|
|
4
|
+
constructor(key) {
|
|
5
|
+
this.config = {
|
|
6
|
+
geolocation: null,
|
|
7
|
+
geolocationCookie: "_rebuyGeolocation",
|
|
8
|
+
geolocationDuration: {
|
|
9
|
+
minutes: 30
|
|
10
|
+
},
|
|
11
|
+
key: null
|
|
12
|
+
};
|
|
13
|
+
if (typeof document == "undefined" || !document.cookie) {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
if (typeof key == "string") {
|
|
17
|
+
this.config.key = key;
|
|
18
|
+
}
|
|
19
|
+
this.config.geolocation = Cookie.get(this.config.geolocationCookie);
|
|
20
|
+
if (this.config.geolocation === null) {
|
|
21
|
+
this._fetchGeolocation();
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
async _fetchGeolocation() {
|
|
25
|
+
const api = new Api({ key: this.config.key });
|
|
26
|
+
const response = await api.callGeo("GET", "/", null);
|
|
27
|
+
if (response.data) {
|
|
28
|
+
this.config.geolocation = response.data;
|
|
29
|
+
const cookieOptions = {
|
|
30
|
+
secure: true
|
|
31
|
+
};
|
|
32
|
+
Object.assign(cookieOptions, this.config.geolocationDuration);
|
|
33
|
+
Cookie.set(this.config.geolocationCookie, this.config.geolocation, { ...cookieOptions, encode: false });
|
|
34
|
+
}
|
|
35
|
+
return this.config.geolocation;
|
|
36
|
+
}
|
|
37
|
+
async geolocation() {
|
|
38
|
+
if (this.config.geolocation == null) {
|
|
39
|
+
await this._fetchGeolocation();
|
|
40
|
+
}
|
|
41
|
+
return this.config.geolocation;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
var geolocation_default = Geolocation;
|
|
45
|
+
export {
|
|
46
|
+
Geolocation,
|
|
47
|
+
geolocation_default as default
|
|
48
|
+
};
|
|
49
|
+
//# sourceMappingURL=geolocation.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/geolocation.ts"],
|
|
4
|
+
"sourcesContent": ["/* eslint-disable @typescript-eslint/no-explicit-any */\n\nimport Api from '~/api';\nimport Cookie from '~/cookie';\n\ntype Config = {\n geolocation: any;\n geolocationCookie: string;\n geolocationDuration: {\n minutes: number;\n };\n key: string | null;\n};\n\nexport class Geolocation {\n private config: Config;\n\n constructor(key: string | null) {\n this.config = {\n geolocation: null,\n geolocationCookie: '_rebuyGeolocation',\n geolocationDuration: {\n minutes: 30,\n },\n key: null,\n };\n\n if (typeof document == 'undefined' || !document.cookie) {\n return;\n }\n\n if (typeof key == 'string') {\n this.config.key = key;\n }\n\n this.config.geolocation = Cookie.get(this.config.geolocationCookie);\n\n // Create a new geolocation (if needed)\n if (this.config.geolocation === null) {\n this._fetchGeolocation();\n }\n }\n\n async _fetchGeolocation(): Promise<any> {\n const api = new Api({ key: this.config.key });\n const response = await api.callGeo('GET', '/', null);\n\n if (response.data) {\n // Update instance config with geolocation\n this.config.geolocation = response.data;\n\n // Write cookie with geolocation\n const cookieOptions: Record<string, any> = {\n secure: true,\n };\n\n // Merge cookie options with geolocation config\n Object.assign(cookieOptions, this.config.geolocationDuration);\n\n // Write cookie with geolocation\n Cookie.set(this.config.geolocationCookie, this.config.geolocation, { ...cookieOptions, encode: false });\n }\n\n return this.config.geolocation;\n }\n\n async geolocation(): Promise<any> {\n if (this.config.geolocation == null) {\n await this._fetchGeolocation();\n }\n\n return this.config.geolocation;\n }\n}\n\n// eslint-disable-next-line import/no-default-export\nexport default Geolocation;\n"],
|
|
5
|
+
"mappings": "AAEA,OAAO,SAAS;AAChB,OAAO,YAAY;AAWZ,MAAM,YAAY;AAAA,EAGrB,YAAY,KAAoB;AAC5B,SAAK,SAAS;AAAA,MACV,aAAa;AAAA,MACb,mBAAmB;AAAA,MACnB,qBAAqB;AAAA,QACjB,SAAS;AAAA,MACb;AAAA,MACA,KAAK;AAAA,IACT;AAEA,QAAI,OAAO,YAAY,eAAe,CAAC,SAAS,QAAQ;AACpD;AAAA,IACJ;AAEA,QAAI,OAAO,OAAO,UAAU;AACxB,WAAK,OAAO,MAAM;AAAA,IACtB;AAEA,SAAK,OAAO,cAAc,OAAO,IAAI,KAAK,OAAO,iBAAiB;AAGlE,QAAI,KAAK,OAAO,gBAAgB,MAAM;AAClC,WAAK,kBAAkB;AAAA,IAC3B;AAAA,EACJ;AAAA,EAEA,MAAM,oBAAkC;AACpC,UAAM,MAAM,IAAI,IAAI,EAAE,KAAK,KAAK,OAAO,IAAI,CAAC;AAC5C,UAAM,WAAW,MAAM,IAAI,QAAQ,OAAO,KAAK,IAAI;AAEnD,QAAI,SAAS,MAAM;AAEf,WAAK,OAAO,cAAc,SAAS;AAGnC,YAAM,gBAAqC;AAAA,QACvC,QAAQ;AAAA,MACZ;AAGA,aAAO,OAAO,eAAe,KAAK,OAAO,mBAAmB;AAG5D,aAAO,IAAI,KAAK,OAAO,mBAAmB,KAAK,OAAO,aAAa,EAAE,GAAG,eAAe,QAAQ,MAAM,CAAC;AAAA,IAC1G;AAEA,WAAO,KAAK,OAAO;AAAA,EACvB;AAAA,EAEA,MAAM,cAA4B;AAC9B,QAAI,KAAK,OAAO,eAAe,MAAM;AACjC,YAAM,KAAK,kBAAkB;AAAA,IACjC;AAEA,WAAO,KAAK,OAAO;AAAA,EACvB;AACJ;AAGA,IAAO,sBAAQ;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
package/dist/identity.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
export declare class Identity {
|
|
2
|
+
private config;
|
|
2
3
|
constructor(key: string | null);
|
|
3
4
|
visitorId(): string | null;
|
|
4
|
-
sessionId(): string;
|
|
5
|
-
sessionStart(): string;
|
|
6
|
-
sessionDuration(): string;
|
|
5
|
+
sessionId(): string | null;
|
|
6
|
+
sessionStart(): string | null;
|
|
7
|
+
sessionDuration(): string | null;
|
|
7
8
|
geolocation(): Promise<any>;
|
|
8
9
|
}
|
|
9
10
|
export default Identity;
|
package/dist/identity.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"identity.d.ts","sourceRoot":"","sources":["../src/identity.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"identity.d.ts","sourceRoot":"","sources":["../src/identity.ts"],"names":[],"mappings":"AAkBA,qBAAa,QAAQ;IACjB,OAAO,CAAC,MAAM,CAAS;gBAEX,GAAG,EAAE,MAAM,GAAG,IAAI;IA6C9B,SAAS,IAAI,MAAM,GAAG,IAAI;IAI1B,SAAS,IAAI,MAAM,GAAG,IAAI;IAI1B,YAAY,IAAI,MAAM,GAAG,IAAI;IAI7B,eAAe,IAAI,MAAM,GAAG,IAAI;IAI1B,WAAW,IAAI,OAAO,CAAC,GAAG,CAAC;CAGpC;AAGD,eAAe,QAAQ,CAAC"}
|
package/dist/identity.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var identity_exports = {};
|
|
30
|
+
__export(identity_exports, {
|
|
31
|
+
Identity: () => Identity,
|
|
32
|
+
default: () => identity_default
|
|
33
|
+
});
|
|
34
|
+
module.exports = __toCommonJS(identity_exports);
|
|
35
|
+
var import_cookie = __toESM(require("~/cookie"), 1);
|
|
36
|
+
var import_geolocation = __toESM(require("~/geolocation"), 1);
|
|
37
|
+
var import_session = __toESM(require("~/session"), 1);
|
|
38
|
+
var import_utilities = require("~/utilities");
|
|
39
|
+
class Identity {
|
|
40
|
+
constructor(key) {
|
|
41
|
+
this.config = {
|
|
42
|
+
geolocation: null,
|
|
43
|
+
key: null,
|
|
44
|
+
session: null,
|
|
45
|
+
visitorDuration: {
|
|
46
|
+
years: 1
|
|
47
|
+
},
|
|
48
|
+
visitorId: null,
|
|
49
|
+
visitorIdCookie: "_rebuyVisitorId"
|
|
50
|
+
};
|
|
51
|
+
if (typeof document == "undefined" || !document.cookie) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
if (typeof key == "string") {
|
|
55
|
+
this.config.key = key;
|
|
56
|
+
}
|
|
57
|
+
this.config.visitorId = import_cookie.default.get(this.config.visitorIdCookie);
|
|
58
|
+
if (this.config.visitorId === null) {
|
|
59
|
+
this.config.visitorId = (0, import_utilities.uuid)();
|
|
60
|
+
}
|
|
61
|
+
const cookieOptions = {
|
|
62
|
+
secure: true
|
|
63
|
+
};
|
|
64
|
+
Object.assign(cookieOptions, this.config.visitorDuration);
|
|
65
|
+
import_cookie.default.set(this.config.visitorIdCookie, this.config.visitorId, cookieOptions);
|
|
66
|
+
this.config.session = new import_session.default();
|
|
67
|
+
this.config.geolocation = new import_geolocation.default(this.config.key);
|
|
68
|
+
}
|
|
69
|
+
visitorId() {
|
|
70
|
+
return this.config.visitorId;
|
|
71
|
+
}
|
|
72
|
+
sessionId() {
|
|
73
|
+
return this.config.session ? this.config.session.sessionId() : null;
|
|
74
|
+
}
|
|
75
|
+
sessionStart() {
|
|
76
|
+
return this.config.session ? this.config.session.sessionStart() : null;
|
|
77
|
+
}
|
|
78
|
+
sessionDuration() {
|
|
79
|
+
return this.config.session ? this.config.session.sessionDuration() : null;
|
|
80
|
+
}
|
|
81
|
+
async geolocation() {
|
|
82
|
+
return this.config.geolocation ? await this.config.geolocation.geolocation() : null;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
var identity_default = Identity;
|
|
86
|
+
//# sourceMappingURL=identity.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/identity.ts"],
|
|
4
|
+
"sourcesContent": ["/* eslint-disable @typescript-eslint/no-explicit-any */\n\nimport Cookie from '~/cookie';\nimport Geolocation from '~/geolocation';\nimport Session from '~/session';\nimport { uuid } from '~/utilities';\n\ntype Config = {\n geolocation?: any;\n key: string | null;\n session: any;\n visitorDuration: {\n years: number;\n };\n visitorId: string | null;\n visitorIdCookie: string;\n};\n\nexport class Identity {\n private config: Config;\n\n constructor(key: string | null) {\n this.config = {\n geolocation: null,\n key: null,\n session: null,\n visitorDuration: {\n years: 1,\n },\n visitorId: null,\n visitorIdCookie: '_rebuyVisitorId',\n };\n\n if (typeof document == 'undefined' || !document.cookie) {\n return;\n }\n\n if (typeof key == 'string') {\n this.config.key = key;\n }\n\n this.config.visitorId = Cookie.get(this.config.visitorIdCookie);\n\n // Create a new identifier (if needed)\n if (this.config.visitorId === null) {\n this.config.visitorId = uuid();\n }\n\n // Write cookie with visitor ID\n const cookieOptions: Record<string, any> = {\n secure: true,\n };\n\n // Merge cookie options with visitor config\n Object.assign(cookieOptions, this.config.visitorDuration);\n\n // Write cookie with session ID\n Cookie.set(this.config.visitorIdCookie, this.config.visitorId, cookieOptions);\n\n // Create visitor session\n this.config.session = new Session();\n\n // Create visitor geolocation, passing the key\n this.config.geolocation = new Geolocation(this.config.key);\n }\n\n visitorId(): string | null {\n return this.config.visitorId;\n }\n\n sessionId(): string | null {\n return this.config.session ? this.config.session.sessionId() : null;\n }\n\n sessionStart(): string | null {\n return this.config.session ? this.config.session.sessionStart() : null;\n }\n\n sessionDuration(): string | null {\n return this.config.session ? this.config.session.sessionDuration() : null;\n }\n\n async geolocation(): Promise<any> {\n return this.config.geolocation ? await this.config.geolocation.geolocation() : null;\n }\n}\n\n// eslint-disable-next-line import/no-default-export\nexport default Identity;\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,oBAAmB;AACnB,yBAAwB;AACxB,qBAAoB;AACpB,uBAAqB;AAad,MAAM,SAAS;AAAA,E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+
"names": ["Cookie", "Session", "Geolocation"]
|
|
7
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import Cookie from "~/cookie";
|
|
2
|
+
import Geolocation from "~/geolocation";
|
|
3
|
+
import Session from "~/session";
|
|
4
|
+
import { uuid } from "~/utilities";
|
|
5
|
+
class Identity {
|
|
6
|
+
constructor(key) {
|
|
7
|
+
this.config = {
|
|
8
|
+
geolocation: null,
|
|
9
|
+
key: null,
|
|
10
|
+
session: null,
|
|
11
|
+
visitorDuration: {
|
|
12
|
+
years: 1
|
|
13
|
+
},
|
|
14
|
+
visitorId: null,
|
|
15
|
+
visitorIdCookie: "_rebuyVisitorId"
|
|
16
|
+
};
|
|
17
|
+
if (typeof document == "undefined" || !document.cookie) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
if (typeof key == "string") {
|
|
21
|
+
this.config.key = key;
|
|
22
|
+
}
|
|
23
|
+
this.config.visitorId = Cookie.get(this.config.visitorIdCookie);
|
|
24
|
+
if (this.config.visitorId === null) {
|
|
25
|
+
this.config.visitorId = uuid();
|
|
26
|
+
}
|
|
27
|
+
const cookieOptions = {
|
|
28
|
+
secure: true
|
|
29
|
+
};
|
|
30
|
+
Object.assign(cookieOptions, this.config.visitorDuration);
|
|
31
|
+
Cookie.set(this.config.visitorIdCookie, this.config.visitorId, cookieOptions);
|
|
32
|
+
this.config.session = new Session();
|
|
33
|
+
this.config.geolocation = new Geolocation(this.config.key);
|
|
34
|
+
}
|
|
35
|
+
visitorId() {
|
|
36
|
+
return this.config.visitorId;
|
|
37
|
+
}
|
|
38
|
+
sessionId() {
|
|
39
|
+
return this.config.session ? this.config.session.sessionId() : null;
|
|
40
|
+
}
|
|
41
|
+
sessionStart() {
|
|
42
|
+
return this.config.session ? this.config.session.sessionStart() : null;
|
|
43
|
+
}
|
|
44
|
+
sessionDuration() {
|
|
45
|
+
return this.config.session ? this.config.session.sessionDuration() : null;
|
|
46
|
+
}
|
|
47
|
+
async geolocation() {
|
|
48
|
+
return this.config.geolocation ? await this.config.geolocation.geolocation() : null;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
var identity_default = Identity;
|
|
52
|
+
export {
|
|
53
|
+
Identity,
|
|
54
|
+
identity_default as default
|
|
55
|
+
};
|
|
56
|
+
//# sourceMappingURL=identity.mjs.map
|