-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
98 lines (78 loc) · 1.92 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import request from './src/request';
import cache from './src/cache';
const requests = {};
// DEPRECATED: use the secure session ID, via getSessionId
const getCookie = () => {
return (/FTSession=([^;]+)/.exec(document.cookie) || [null, ''])[1];
};
const getSessionId = () => {
const [, sessionId] = /FTSession_s=([^;]+)/.exec(document.cookie) || [];
return sessionId;
};
const getUuid = () => {
const cachedUUID = cache('uuid');
if (cachedUUID) {
return Promise.resolve({ uuid: cachedUUID });
}
const sessionId = getSessionId();
if (!sessionId) {
return Promise.resolve({ uuid: undefined });
}
if (!requests.uuid) {
requests.uuid = request(`/sessions/s/${sessionId}`)
.then(({ uuid } = {}) => {
delete requests.uuid;
if (uuid) {
cache('uuid', uuid);
}
return { uuid };
});
}
return requests.uuid;
};
const getProducts = () => {
const cachedProducts = cache('products');
const cachedUUID = cache('uuid');
if (cachedProducts && cachedUUID) {
return Promise.resolve({ products: cachedProducts, uuid: cachedUUID });
}
// if we don't have a session token cookie, don't bother...
if (!getSessionId()) {
return Promise.resolve({});
}
if (!requests.products) {
requests.products = request('/products', { credentials: 'include' })
.then(({ products, uuid } = {}) => {
requests.products = undefined;
if (products) {
cache('products', products);
}
if (uuid) {
cache('uuid', uuid);
}
return { products, uuid };
});
}
return requests.products;
};
// DEPRECATED: use getUuid, will only return a uuid if session is valid
const validate = () => {
return getUuid()
.then(({ uuid }) => uuid ? true : false);
};
export {
getUuid as uuid,
getProducts as products,
validate,
cache,
getCookie as cookie,
getSessionId as sessionId
}
export default {
uuid: getUuid,
products: getProducts,
validate,
cache,
cookie: getCookie,
sessionId: getSessionId
}