@rebuy/rebuy 1.5.1 → 1.6.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/client.js DELETED
@@ -1,148 +0,0 @@
1
- import Api from './api.js';
2
- import Identity from './identity.js';
3
- import { convertProductToStorefrontFormat } from './utilities.js';
4
-
5
- const config = {
6
- key: null,
7
- defaultParameters: null,
8
- contextParameters: null,
9
- api: null,
10
- identity: null,
11
- shop: null,
12
- };
13
-
14
- const trackEvent = async (eventData) => {
15
- if (config.identity && config.identity.visitorId()) {
16
- eventData.uuid = config.identity.visitorId();
17
- }
18
-
19
- return await config.api.callEvent('POST', '/api/v1/analytics/event', eventData);
20
- };
21
-
22
- const makeShieldCall = async (endpoint, params, format, options = {}) => {
23
- return await makeCall(endpoint, params, format, { ...options, shield: true });
24
- };
25
-
26
- const makeStaticCall = async (endpoint, params, format, options = {}) => {
27
- return await makeCall(endpoint, params, format, { ...options, static: true });
28
- };
29
-
30
- /**
31
- * @deprecated
32
- */
33
- const makeCDNCall = async (endpoint, params, format, options = {}) => {
34
- return await makeCall(endpoint, params, format, { ...options, cdn: true });
35
- };
36
-
37
- const makeCall = async (endpoint, params, format, options = {}) => {
38
- const query = {};
39
-
40
- if (config.defaultParameters != null) {
41
- Object.assign(query, config.defaultParameters);
42
- }
43
-
44
- if (config.contextParameters != null) {
45
- Object.assign(query, config.contextParameters);
46
- }
47
-
48
- if (typeof params == 'object' && params != null) {
49
- Object.assign(query, params);
50
- }
51
-
52
- if (typeof options != 'object' || options == null) {
53
- console.warn('Unsupported fetch options provided.', options);
54
- options = {};
55
- }
56
-
57
- if (config.identity && config.identity.visitorId()) {
58
- query.uuid = config.identity.visitorId();
59
- }
60
-
61
- // Origin or dedicated edge?
62
- const source = options.cdn ? 'callCdn' : options.shield ? 'callShield' : options.static ? 'callStatic' : 'callApi';
63
- const response = await config.api[source]('GET', endpoint, query, options);
64
-
65
- if (response.data && format == 'storefront') {
66
- for (let i = 0; i < response.data.length; i++) {
67
- response.data[i] = convertProductToStorefrontFormat(response.data[i]);
68
- }
69
- }
70
-
71
- return response;
72
- };
73
-
74
- export class RebuyClient {
75
- constructor(key, defaultParameters, shop) {
76
- if (typeof key == 'string') {
77
- config.key = key;
78
- }
79
-
80
- if (typeof defaultParameters == 'object' && defaultParameters != null) {
81
- config.defaultParameters = defaultParameters;
82
- }
83
-
84
- if (typeof shop == 'string' && shop.endsWith('.myshopify.com')) {
85
- config.shop = shop;
86
- }
87
-
88
- config.api = new Api({ key: config.key, shop: config.shop });
89
- config.identity = new Identity();
90
- }
91
-
92
- setDefaultParameters(defaultParameters) {
93
- if (typeof defaultParameters == 'object' && defaultParameters != null) {
94
- config.defaultParameters = defaultParameters;
95
- }
96
- }
97
-
98
- setContextParameters(contextParameters) {
99
- if (typeof contextParameters == 'object' && contextParameters != null) {
100
- config.contextParameters = contextParameters;
101
- }
102
- }
103
-
104
- async getData(endpoint, params, options = {}) {
105
- return await makeCall(endpoint, params, null, options);
106
- }
107
-
108
- /**
109
- * @deprecated
110
- */
111
- async getDataFromCDN(endpoint, params, options = {}) {
112
- return await makeCDNCall(endpoint, params, null, options);
113
- }
114
-
115
- async getShieldedAsset(endpoint, params, options = {}) {
116
- return await makeShieldCall(endpoint, params, null, options);
117
- }
118
-
119
- async getStaticAsset(endpoint, params, options = {}) {
120
- return await makeStaticCall(endpoint, params, null, options);
121
- }
122
-
123
- async getStorefrontData(endpoint, params, options = {}) {
124
- return await makeCall(endpoint, params, 'storefront', options);
125
- }
126
-
127
- async trackProductViewed(data) {
128
- const requiredKeys = ['shopify_product_id', 'shopify_product_handle'];
129
-
130
- const defaultData = {
131
- subject: 'user',
132
- verb: 'viewed',
133
- noun: 'product',
134
- };
135
-
136
- if (typeof data != 'undefined' && data != null) {
137
- const dataKeys = Object.keys(data);
138
- if (dataKeys.some((key) => requiredKeys.includes(key))) {
139
- const payload = Object.assign(data, defaultData);
140
- return await trackEvent(payload);
141
- }
142
- }
143
-
144
- return null;
145
- }
146
- }
147
-
148
- export default RebuyClient;
package/cookie.js DELETED
@@ -1,153 +0,0 @@
1
- import { dataToString, stringToData, isBase64Encoded } from './utilities.js';
2
-
3
- export function get(name) {
4
- if (typeof document == 'undefined' || !document.cookie) {
5
- return null;
6
- }
7
-
8
- // Get cookie
9
- const cookie = document.cookie.match('(^|;) ?' + decodeURIComponent(name) + '=([^;]*)(;|$)');
10
-
11
- let value = null;
12
-
13
- if (cookie != null) {
14
- // Get data
15
- const data = decodeURIComponent(cookie[2]);
16
-
17
- // Auto decode
18
- const decode = isBase64Encoded(data) ? true : false;
19
-
20
- // Convert to data object
21
- value = stringToData(data, decode);
22
- }
23
-
24
- return value;
25
- }
26
-
27
- export function getAll() {
28
- const cookies = {};
29
-
30
- if (document && document.cookie && document.cookie != '') {
31
- const split = document.cookie.split(';');
32
-
33
- for (let i = 0; i < split.length; i++) {
34
- const pairs = split[i].split('=');
35
-
36
- pairs[0] = pairs[0].replace(/^ /, '');
37
-
38
- const key = decodeURIComponent(pairs[0]);
39
- const value = decodeURIComponent(pairs[1]);
40
-
41
- // Auto decode
42
- const decode = isBase64Encoded(value) ? true : false;
43
-
44
- cookies[key] = stringToData(value, decode);
45
- }
46
- }
47
-
48
- return cookies;
49
- }
50
-
51
- export function set(name, value, config) {
52
- if (typeof document == 'undefined' || !document.cookie) {
53
- return null;
54
- }
55
-
56
- const attributes = ['path', 'domain', 'maxAge', 'expires', 'secure', 'sameSite'];
57
- const convenienceTimes = ['seconds', 'minutes', 'hours', 'days', 'weeks', 'months', 'years'];
58
-
59
- let cookieAttributes = {
60
- path: '/',
61
- };
62
-
63
- if (typeof config != 'undefined' && Number.isInteger(config)) {
64
- cookieAttributes['max-age'] = config;
65
- } else if (typeof config === 'object' && config !== null) {
66
- for (let key in config) {
67
- if (attributes.includes(key)) {
68
- if (key == 'maxAge') {
69
- cookieAttributes['max-age'] = config[key];
70
- } else if (key == 'sameSite') {
71
- cookieAttributes['samesite'] = config[key];
72
- } else if (key == 'expires') {
73
- cookieAttributes[key] = new Date(config[key]).toGMTString();
74
- } else {
75
- cookieAttributes[key] = config[key];
76
- }
77
- } else if (convenienceTimes.includes(key)) {
78
- let duration = config[key];
79
-
80
- if (key == 'seconds') {
81
- duration = duration * 1;
82
- } else if (key == 'minutes') {
83
- duration = duration * 60;
84
- } else if (key == 'hours') {
85
- duration = duration * 60 * 60;
86
- } else if (key == 'days') {
87
- duration = duration * 60 * 60 * 24;
88
- } else if (key == 'weeks') {
89
- duration = duration * 60 * 60 * 24 * 7;
90
- } else if (key == 'months') {
91
- duration = duration * 60 * 60 * 24 * 30;
92
- } else if (key == 'years') {
93
- duration = duration * 60 * 60 * 24 * 365;
94
- }
95
-
96
- cookieAttributes['max-age'] = duration;
97
- }
98
- }
99
- }
100
-
101
- // Convert data to string
102
- value = dataToString(value, config.encode);
103
-
104
- // Define cookie
105
- let cookie = encodeURIComponent(name) + '=' + encodeURIComponent(value);
106
-
107
- // Add optional cookie attributes
108
- for (let key in cookieAttributes) {
109
- cookie += ';' + key + '=' + cookieAttributes[key];
110
- }
111
-
112
- // Write cookie
113
- document.cookie = cookie;
114
- }
115
-
116
- export function find(name) {
117
- const matches = [];
118
- const cookies = getAll();
119
-
120
- for (let key in cookies) {
121
- if (key.includes(name)) {
122
- matches.push({
123
- name: key,
124
- value: cookies[key],
125
- });
126
- }
127
- }
128
-
129
- return matches;
130
- }
131
-
132
- export function destroy(name) {
133
- set(name, '', { seconds: 0 });
134
- }
135
-
136
- export function enabled() {
137
- const test = {
138
- key: '__cookie_test',
139
- value: 1,
140
- };
141
-
142
- set(test.key, test.value);
143
-
144
- const enabled = get(test.key) == test.value ? true : false;
145
-
146
- if (enabled) {
147
- destroy(test.key);
148
- }
149
-
150
- return enabled;
151
- }
152
-
153
- export default { get, set, getAll, find, destroy, enabled };
package/geolocation.js DELETED
@@ -1,63 +0,0 @@
1
- import Api from './api.js';
2
- import Cookie from './cookie.js';
3
-
4
- const config = {
5
- key: null,
6
- geolocation: null,
7
- geolocationCookie: '_rebuyGeolocation',
8
- geolocationDuration: {
9
- minutes: 30,
10
- },
11
- };
12
-
13
- const getGeolocation = async () => {
14
- const api = new Api(config.key);
15
- const response = await api.callGeo('GET', '/');
16
-
17
- if (response.data) {
18
- // Update config with geolocation
19
- config.geolocation = response.data;
20
-
21
- // Write cookie with geolocation
22
- const cookieOptions = {
23
- secure: true,
24
- };
25
-
26
- // Merge cookie options with geolocation config
27
- Object.assign(cookieOptions, config.geolocationDuration);
28
-
29
- // Write cookie with geolocation
30
- Cookie.set(config.geolocationCookie, config.geolocation, cookieOptions);
31
- }
32
-
33
- return config.geolocation;
34
- };
35
-
36
- export class Geolocation {
37
- constructor(key) {
38
- if (typeof document == 'undefined' || !document.cookie) {
39
- return;
40
- }
41
-
42
- if (typeof key == 'string') {
43
- config.key = key;
44
- }
45
-
46
- config.geolocation = Cookie.get(config.geolocationCookie);
47
-
48
- // Create a new geolocation (if needed)
49
- if (config.geolocation === null) {
50
- getGeolocation();
51
- }
52
- }
53
-
54
- async geolocation() {
55
- if (config.geolocation == null) {
56
- await getGeolocation();
57
- }
58
-
59
- return config.geolocation;
60
- }
61
- }
62
-
63
- export default Geolocation;
package/identity.js DELETED
@@ -1,72 +0,0 @@
1
- import { uuid } from './utilities.js';
2
- import Session from './session.js';
3
- import Cookie from './cookie.js';
4
- import Geolocation from './geolocation.js';
5
-
6
- const config = {
7
- key: null,
8
- visitorId: null,
9
- visitorIdCookie: '_rebuyVisitorId',
10
- visitorDuration: {
11
- years: 1,
12
- },
13
- session: null,
14
- };
15
-
16
- export class Identity {
17
- constructor(key) {
18
- if (typeof document == 'undefined' || !document.cookie) {
19
- return;
20
- }
21
-
22
- if (typeof key == 'string') {
23
- config.key = key;
24
- }
25
-
26
- config.visitorId = Cookie.get(config.visitorIdCookie);
27
-
28
- // Create a new identifier (if needed)
29
- if (config.visitorId === null) {
30
- config.visitorId = uuid();
31
- }
32
-
33
- // Write cookie with visitor ID
34
- const cookieOptions = {
35
- secure: true,
36
- };
37
-
38
- // Merge cookie options with visitor config
39
- Object.assign(cookieOptions, config.visitorDuration);
40
-
41
- // Write cookie with session ID
42
- Cookie.set(config.visitorIdCookie, config.visitorId, cookieOptions);
43
-
44
- // Create visitor session
45
- config.session = new Session();
46
-
47
- // Create visitor geolocation
48
- config.geolocation = new Geolocation(config.key);
49
- }
50
-
51
- visitorId() {
52
- return config.visitorId;
53
- }
54
-
55
- sessionId() {
56
- return config.session.sessionId();
57
- }
58
-
59
- sessionStart() {
60
- return config.session.sessionStart();
61
- }
62
-
63
- sessionDuration() {
64
- return config.session.sessionDuration();
65
- }
66
-
67
- async geolocation() {
68
- return await config.geolocation.geolocation();
69
- }
70
- }
71
-
72
- export default Identity;
package/session.js DELETED
@@ -1,52 +0,0 @@
1
- import { sessionId } from './utilities.js';
2
- import Cookie from './cookie.js';
3
-
4
- const config = {
5
- now: null,
6
- sessionId: null,
7
- sessionIdCookie: '_rebuySessionId',
8
- sessionDuration: {
9
- minutes: 30,
10
- },
11
- };
12
-
13
- export class Session {
14
- constructor() {
15
- if (typeof document == 'undefined' || !document.cookie) {
16
- return;
17
- }
18
-
19
- config.now = new Date().getTime();
20
- config.sessionId = Cookie.get(config.sessionIdCookie);
21
-
22
- // Create a new session (if needed)
23
- if (config.sessionId === null) {
24
- config.sessionId = sessionId();
25
- }
26
-
27
- // Write cookie with session ID
28
- const cookieOptions = {
29
- secure: true,
30
- };
31
-
32
- // Merge cookie options with session config
33
- Object.assign(cookieOptions, config.sessionDuration);
34
-
35
- // Write cookie with session ID
36
- Cookie.set(config.sessionIdCookie, config.sessionId, cookieOptions);
37
- }
38
-
39
- sessionId() {
40
- return config.sessionId;
41
- }
42
-
43
- sessionStart() {
44
- return Number(config.sessionId.split('.')[1]);
45
- }
46
-
47
- sessionDuration() {
48
- return parseInt((config.now - this.sessionStart()) / 1000 / 60);
49
- }
50
- }
51
-
52
- export default Session;