@sesamy/sesamy-js 1.3.1 → 1.3.3
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/index.d.ts +108 -0
- package/dist/sesamy-js.cjs +6 -0
- package/dist/sesamy-js.iife.js +6 -0
- package/dist/sesamy-js.mjs +3082 -0
- package/package.json +4 -2
- package/.env +0 -2
- package/.env.production +0 -2
- package/.eslintignore +0 -4
- package/.eslintrc +0 -18
- package/.github/workflows/release.yml +0 -23
- package/.nvmrc +0 -1
- package/.prettierignore +0 -4
- package/.prettierrc +0 -5
- package/CHANGELOG.md +0 -132
- package/README.md +0 -69
- package/article.html +0 -65
- package/contracts.html +0 -23
- package/dts-bundle-generator.config.ts +0 -11
- package/entitlements.html +0 -23
- package/index.html +0 -24
- package/renovate.json +0 -4
- package/src/SesamyContracts.ts +0 -86
- package/src/SesamyEntitlements.ts +0 -85
- package/src/app.ts +0 -101
- package/src/constants.ts +0 -2
- package/src/controllers/index.ts +0 -1
- package/src/controllers/paywall.ts +0 -14
- package/src/entitlementsStyle.css +0 -147
- package/src/events/ready.ts +0 -12
- package/src/index.ts +0 -36
- package/src/javascript-api.ts +0 -84
- package/src/services/analytics/element-tracker.ts +0 -117
- package/src/services/analytics/index.ts +0 -234
- package/src/services/analytics/listeners/index.ts +0 -2
- package/src/services/analytics/listeners/route.ts +0 -9
- package/src/services/analytics/listeners/scroll.ts +0 -62
- package/src/services/analytics/session-id.ts +0 -11
- package/src/services/analytics/types/analytics-activity-utils.d.ts +0 -54
- package/src/services/analytics/types/analytics-router-utils.d.ts +0 -7
- package/src/services/analytics/types/analytics-scroll-utils.d.ts +0 -4
- package/src/services/analytics/types/track-event.ts +0 -70
- package/src/services/auth/index.ts +0 -74
- package/src/services/sesamy/index.ts +0 -160
- package/src/state.ts +0 -3
- package/src/style.css +0 -99
- package/src/types/Bills.ts +0 -12
- package/src/types/Config.ts +0 -11
- package/src/types/Contracts.ts +0 -12
- package/src/types/Entitlement.ts +0 -9
- package/src/types/Events.ts +0 -6
- package/src/types/Tag.ts +0 -16
- package/src/vite-env.d.ts +0 -1
- package/tsconfig.json +0 -23
- package/vite.config.ts +0 -43
- package/vite.dev.config.ts +0 -14
- /package/{public → dist}/sesamy.png +0 -0
|
@@ -1,234 +0,0 @@
|
|
|
1
|
-
import Analytics, { AnalyticsInstance } from 'analytics';
|
|
2
|
-
import saturated from 'saturated';
|
|
3
|
-
import { name, version } from '../../../package.json';
|
|
4
|
-
import { TrackEvent } from './types/track-event';
|
|
5
|
-
import { AnalyticsConfig } from '../../types/Config';
|
|
6
|
-
import { routeChangeListener } from './listeners';
|
|
7
|
-
import ElementTracker from './element-tracker';
|
|
8
|
-
import { ANALYTICS_BASE_URL } from '../../constants';
|
|
9
|
-
import { getSessionId } from './session-id';
|
|
10
|
-
import { Events } from '../../types/Events';
|
|
11
|
-
|
|
12
|
-
type Analytic = {
|
|
13
|
-
anonymousId: string;
|
|
14
|
-
userId?: string;
|
|
15
|
-
properties: { [key: string]: string | number };
|
|
16
|
-
event?: string;
|
|
17
|
-
type: string;
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
let flushing = false;
|
|
21
|
-
|
|
22
|
-
let _clientId: string;
|
|
23
|
-
let _enabled: boolean;
|
|
24
|
-
let _endpoint: string;
|
|
25
|
-
|
|
26
|
-
export interface AnalyticsEventWithType extends Analytic {
|
|
27
|
-
type: string;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export function initAnalytics({ clientId, enabled = true, endpoint = ANALYTICS_BASE_URL }: AnalyticsConfig) {
|
|
31
|
-
_clientId = clientId;
|
|
32
|
-
_enabled = enabled;
|
|
33
|
-
_endpoint = endpoint;
|
|
34
|
-
|
|
35
|
-
if (!_enabled) {
|
|
36
|
-
return;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
// Register route change listener
|
|
40
|
-
routeChangeListener();
|
|
41
|
-
|
|
42
|
-
// Register body events
|
|
43
|
-
const bodyTracker = new ElementTracker({
|
|
44
|
-
element: document.body,
|
|
45
|
-
viewCallback: () => {
|
|
46
|
-
analytics.page();
|
|
47
|
-
},
|
|
48
|
-
activeDurationCallback: (duration: number, flushing?: boolean) => {
|
|
49
|
-
analytics.track('activeDuration', {
|
|
50
|
-
duration,
|
|
51
|
-
flushing,
|
|
52
|
-
});
|
|
53
|
-
},
|
|
54
|
-
idleDurationCallback: (duration: number, flushing?: boolean) => {
|
|
55
|
-
analytics.track('idleDuration', {
|
|
56
|
-
duration,
|
|
57
|
-
flushing,
|
|
58
|
-
});
|
|
59
|
-
},
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
addUnloadPageListener(document.body, () => {
|
|
63
|
-
bodyTracker.flush();
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
window.addEventListener(Events.AUTHENTICATED, async (event: Event) => {
|
|
67
|
-
const customEvent = event as CustomEvent;
|
|
68
|
-
await analytics.identify(customEvent.detail.sub);
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
window.addEventListener(Events.LOGOUT, async () => {
|
|
72
|
-
await analytics.track('logout', {});
|
|
73
|
-
queue.flush();
|
|
74
|
-
await analytics.reset();
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
function createMessage(payload: AnalyticsEventWithType[]): string {
|
|
79
|
-
return JSON.stringify(
|
|
80
|
-
payload.map(item => ({
|
|
81
|
-
...item,
|
|
82
|
-
clientId: _clientId,
|
|
83
|
-
requestId: Math.random().toString(36).slice(2, 9),
|
|
84
|
-
sessionId: getSessionId(),
|
|
85
|
-
timestamp: new Date().toISOString(),
|
|
86
|
-
version,
|
|
87
|
-
event: item.event,
|
|
88
|
-
context: {
|
|
89
|
-
page: {
|
|
90
|
-
url: window.location.hostname,
|
|
91
|
-
path: window.location.pathname,
|
|
92
|
-
title: document.title,
|
|
93
|
-
search: window.location.search,
|
|
94
|
-
referrer: document.referrer,
|
|
95
|
-
},
|
|
96
|
-
locale: navigator.language,
|
|
97
|
-
library: name,
|
|
98
|
-
userAgent: navigator.userAgent,
|
|
99
|
-
clientId: _clientId,
|
|
100
|
-
},
|
|
101
|
-
})),
|
|
102
|
-
);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
const queue = saturated(
|
|
106
|
-
async arr => {
|
|
107
|
-
if (arr.length > 0) {
|
|
108
|
-
const payload = createMessage(arr);
|
|
109
|
-
|
|
110
|
-
if (flushing) {
|
|
111
|
-
navigator.sendBeacon(_endpoint, payload);
|
|
112
|
-
} else {
|
|
113
|
-
const response = await fetch(_endpoint, {
|
|
114
|
-
method: 'POST',
|
|
115
|
-
body: payload,
|
|
116
|
-
headers: {
|
|
117
|
-
'Content-Type': 'text/plain',
|
|
118
|
-
},
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
if (!response.ok) {
|
|
122
|
-
// TODO: Retry, maybe store data in local storage
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
},
|
|
127
|
-
{
|
|
128
|
-
max: 10, // limit
|
|
129
|
-
interval: 3000, // 3s
|
|
130
|
-
},
|
|
131
|
-
);
|
|
132
|
-
|
|
133
|
-
function send(payload: AnalyticsEventWithType) {
|
|
134
|
-
if (!payload.anonymousId) {
|
|
135
|
-
// When resetting the analytics, the anonymousId is set to null. Skip sending these events
|
|
136
|
-
} else if (payload.properties?.flushing) {
|
|
137
|
-
const payloadWithoutFlushing = { ...payload };
|
|
138
|
-
delete payloadWithoutFlushing.properties.flushing;
|
|
139
|
-
|
|
140
|
-
navigator.sendBeacon(_endpoint, createMessage([payloadWithoutFlushing]));
|
|
141
|
-
} else {
|
|
142
|
-
queue.push(payload);
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
export const analytics: AnalyticsInstance = Analytics({
|
|
147
|
-
app: name,
|
|
148
|
-
version,
|
|
149
|
-
plugins: [
|
|
150
|
-
{
|
|
151
|
-
name: 'custom-analytics-plugin',
|
|
152
|
-
page: ({ payload }: { payload: Analytic }) => {
|
|
153
|
-
const { properties, anonymousId, userId, event } = payload;
|
|
154
|
-
const analyticsEvent = {
|
|
155
|
-
anonymousId,
|
|
156
|
-
userId,
|
|
157
|
-
properties,
|
|
158
|
-
event,
|
|
159
|
-
type: 'page',
|
|
160
|
-
};
|
|
161
|
-
// Send data to custom collection endpoint
|
|
162
|
-
send(analyticsEvent);
|
|
163
|
-
},
|
|
164
|
-
track: ({ payload }: { payload: Analytic }) => {
|
|
165
|
-
const { properties, anonymousId, userId, event } = payload;
|
|
166
|
-
const analyticsEvent: AnalyticsEventWithType = {
|
|
167
|
-
anonymousId,
|
|
168
|
-
userId,
|
|
169
|
-
properties,
|
|
170
|
-
event,
|
|
171
|
-
type: 'track',
|
|
172
|
-
};
|
|
173
|
-
send(analyticsEvent);
|
|
174
|
-
},
|
|
175
|
-
identify: ({ payload }: { payload: Analytic }) => {
|
|
176
|
-
const { properties, anonymousId, userId } = payload;
|
|
177
|
-
const analyticsEvent: AnalyticsEventWithType = {
|
|
178
|
-
anonymousId,
|
|
179
|
-
userId,
|
|
180
|
-
properties,
|
|
181
|
-
type: 'identify',
|
|
182
|
-
};
|
|
183
|
-
send(analyticsEvent);
|
|
184
|
-
},
|
|
185
|
-
},
|
|
186
|
-
],
|
|
187
|
-
});
|
|
188
|
-
|
|
189
|
-
export function getAnonymousUserId() {
|
|
190
|
-
return analytics.user().anonymousId;
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
194
|
-
export function track(eventName: string, payload: any): void {
|
|
195
|
-
analytics.track(eventName, payload);
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
export function trackEvent(event: TrackEvent): void {
|
|
199
|
-
analytics.track(event.name, event);
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
export function flushQueue() {
|
|
203
|
-
flushing = true;
|
|
204
|
-
return queue.flush();
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
export function enableInteractions(callback?: () => void) {
|
|
208
|
-
analytics.plugins.enable('custom-analytics-plugin', callback);
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
export function disableInteractions(callback?: () => void) {
|
|
212
|
-
analytics.plugins.disable('custom-analytics-plugin', callback || (() => true));
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
export type Action = () => void;
|
|
216
|
-
const unloadPageListeners = new Map<Element, Action>();
|
|
217
|
-
|
|
218
|
-
export function addUnloadPageListener(element: Element, action: Action) {
|
|
219
|
-
unloadPageListeners.set(element, action);
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
export function removeUnloadPageListener(element: Element) {
|
|
223
|
-
unloadPageListeners.delete(element);
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
window.addEventListener('beforeunload', () => {
|
|
227
|
-
// Flush the queues for all the registered element trackers
|
|
228
|
-
unloadPageListeners.forEach((action, element) => {
|
|
229
|
-
action.bind(element)();
|
|
230
|
-
});
|
|
231
|
-
|
|
232
|
-
// Flush the outbound queue
|
|
233
|
-
flushQueue();
|
|
234
|
-
});
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
/// <reference path="../types/analytics-scroll-utils.d.ts" />
|
|
2
|
-
import { onScrollChange } from '@analytics/scroll-utils';
|
|
3
|
-
import { TrackEvents } from '../types/track-event';
|
|
4
|
-
import { trackEvent } from '..';
|
|
5
|
-
|
|
6
|
-
export type ScrollEvent = {
|
|
7
|
-
direction: string;
|
|
8
|
-
range: number[];
|
|
9
|
-
scrollMax: number;
|
|
10
|
-
scrollMin: number;
|
|
11
|
-
trigger: number;
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
export function addScrollListener(itemSrc: string, publisherContentId: string) {
|
|
15
|
-
onScrollChange({
|
|
16
|
-
25: (scrollData: ScrollEvent) => {
|
|
17
|
-
trackEvent({
|
|
18
|
-
name: TrackEvents.Scroll,
|
|
19
|
-
scrollData: scrollData.direction,
|
|
20
|
-
percentage: scrollData.trigger,
|
|
21
|
-
itemSrc,
|
|
22
|
-
publisherContentId,
|
|
23
|
-
});
|
|
24
|
-
},
|
|
25
|
-
50: (scrollData: ScrollEvent) => {
|
|
26
|
-
trackEvent({
|
|
27
|
-
name: TrackEvents.Scroll,
|
|
28
|
-
scrollData: scrollData.direction,
|
|
29
|
-
percentage: scrollData.trigger,
|
|
30
|
-
itemSrc,
|
|
31
|
-
publisherContentId,
|
|
32
|
-
});
|
|
33
|
-
},
|
|
34
|
-
75: (scrollData: ScrollEvent) => {
|
|
35
|
-
trackEvent({
|
|
36
|
-
name: TrackEvents.Scroll,
|
|
37
|
-
scrollData: scrollData.direction,
|
|
38
|
-
percentage: scrollData.trigger,
|
|
39
|
-
itemSrc,
|
|
40
|
-
publisherContentId,
|
|
41
|
-
});
|
|
42
|
-
},
|
|
43
|
-
90: (scrollData: ScrollEvent) => {
|
|
44
|
-
trackEvent({
|
|
45
|
-
name: TrackEvents.Scroll,
|
|
46
|
-
scrollData: scrollData.direction,
|
|
47
|
-
percentage: scrollData.trigger,
|
|
48
|
-
itemSrc,
|
|
49
|
-
publisherContentId,
|
|
50
|
-
});
|
|
51
|
-
},
|
|
52
|
-
100: (scrollData: ScrollEvent) => {
|
|
53
|
-
trackEvent({
|
|
54
|
-
name: TrackEvents.Scroll,
|
|
55
|
-
scrollData: scrollData.direction,
|
|
56
|
-
percentage: scrollData.trigger,
|
|
57
|
-
itemSrc,
|
|
58
|
-
publisherContentId,
|
|
59
|
-
});
|
|
60
|
-
},
|
|
61
|
-
});
|
|
62
|
-
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
const SESSION_ID_KEY = 'sesamy_session_id';
|
|
2
|
-
|
|
3
|
-
export function getSessionId() {
|
|
4
|
-
let sessionId = sessionStorage.getItem(SESSION_ID_KEY);
|
|
5
|
-
|
|
6
|
-
if (!sessionId) {
|
|
7
|
-
sessionId = Math.random().toString(36).slice(2, 9);
|
|
8
|
-
sessionStorage.setItem(SESSION_ID_KEY, sessionId);
|
|
9
|
-
}
|
|
10
|
-
return sessionId;
|
|
11
|
-
}
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
interface OnDomActivityOptions {
|
|
2
|
-
throttle?: number;
|
|
3
|
-
}
|
|
4
|
-
|
|
5
|
-
interface AnalyticsEvent {}
|
|
6
|
-
|
|
7
|
-
interface OnUserActivityOptions {
|
|
8
|
-
onIdle?: (elapsedTime: number, event?: AnalyticsEvent) => void;
|
|
9
|
-
onWakeUp?: (elapsedTime: number, event?: AnalyticsEvent) => void;
|
|
10
|
-
onHeartbeat?: (elapsedTime: number, event?: AnalyticsEvent) => void;
|
|
11
|
-
timeout?: number;
|
|
12
|
-
throttle?: number;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
interface UserActivityStatus {
|
|
16
|
-
disable: () => () => void;
|
|
17
|
-
getStatus: () => {
|
|
18
|
-
isIdle: boolean;
|
|
19
|
-
isDisabled: boolean;
|
|
20
|
-
active: number;
|
|
21
|
-
idle: number;
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
declare module '@analytics/activity-utils' {
|
|
26
|
-
/**
|
|
27
|
-
* Function to handle user inactivity.
|
|
28
|
-
* @param onIdle Function to be called when the user is idle.
|
|
29
|
-
* @param opts Optional configuration object.
|
|
30
|
-
* @returns An object with methods to control and get the status of the listener.
|
|
31
|
-
*/
|
|
32
|
-
function onIdle(
|
|
33
|
-
onIdle: (elapsedTime: number, event?: AnalyticsEvent) => void,
|
|
34
|
-
opts?: OnUserActivityOptions,
|
|
35
|
-
): UserActivityStatus;
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Function to handle user wake up from idle.
|
|
39
|
-
* @param onWakeUp Function to be called when the user wakes up from idle.
|
|
40
|
-
* @param opts Optional configuration object.
|
|
41
|
-
* @returns An object with methods to control and get the status of the listener.
|
|
42
|
-
*/
|
|
43
|
-
function onWakeUp(
|
|
44
|
-
onWakeUp: (elapsedTime: number, event?: AnalyticsEvent) => void,
|
|
45
|
-
opts?: OnUserActivityOptions,
|
|
46
|
-
): UserActivityStatus;
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Function to handle user activity.
|
|
50
|
-
* @param options Configuration object for user activity.
|
|
51
|
-
* @returns An object with methods to control and get the status of the listener.
|
|
52
|
-
*/
|
|
53
|
-
function onUserActivity(options: OnUserActivityOptions): UserActivityStatus;
|
|
54
|
-
}
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
export enum TrackEvents {
|
|
2
|
-
ViewArticle = 'viewArticle',
|
|
3
|
-
ActiveDuration = 'activeDuration',
|
|
4
|
-
IdleDuration = 'idleDuration',
|
|
5
|
-
Scroll = 'scroll',
|
|
6
|
-
ChangeView = 'changeView',
|
|
7
|
-
AddToCart = 'addToCart',
|
|
8
|
-
ViewArticleListing = 'viewArticleListing',
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
interface ViewArticleListingProps {
|
|
12
|
-
name: TrackEvents.ViewArticleListing;
|
|
13
|
-
publisherContentId?: string;
|
|
14
|
-
itemSrc: string;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
interface AddToCartEventProps {
|
|
18
|
-
name: TrackEvents.AddToCart;
|
|
19
|
-
publisherContentId?: string;
|
|
20
|
-
itemSrc: string;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
interface ChangeViewEventProps {
|
|
24
|
-
name: TrackEvents.ChangeView;
|
|
25
|
-
view: string;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
interface ScrollEventProps {
|
|
29
|
-
name: TrackEvents.Scroll;
|
|
30
|
-
publisherContentId?: string;
|
|
31
|
-
itemSrc: string;
|
|
32
|
-
scrollData: string;
|
|
33
|
-
percentage: number;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
interface IdleDurationEventProps {
|
|
37
|
-
name: TrackEvents.IdleDuration;
|
|
38
|
-
duration: number;
|
|
39
|
-
publisherContentId?: string;
|
|
40
|
-
itemSrc: string;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
interface ActiveDurationEventProps {
|
|
44
|
-
name: TrackEvents.ActiveDuration;
|
|
45
|
-
duration: number;
|
|
46
|
-
publisherContentId?: string;
|
|
47
|
-
itemSrc: string;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
interface ViewArticleEventProps {
|
|
51
|
-
name: TrackEvents.ViewArticle;
|
|
52
|
-
itemSrc: string;
|
|
53
|
-
publisherContentId?: string;
|
|
54
|
-
state: 'public' | 'locked' | 'unlocked' | 'logged-in';
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
interface Flushing {
|
|
58
|
-
flushing?: boolean;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export type TrackEvent = Flushing &
|
|
62
|
-
(
|
|
63
|
-
| ViewArticleEventProps
|
|
64
|
-
| ActiveDurationEventProps
|
|
65
|
-
| IdleDurationEventProps
|
|
66
|
-
| ScrollEventProps
|
|
67
|
-
| ChangeViewEventProps
|
|
68
|
-
| AddToCartEventProps
|
|
69
|
-
| ViewArticleListingProps
|
|
70
|
-
);
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import { Auth0Client, createAuth0Client } from '@auth0/auth0-spa-js';
|
|
2
|
-
import { BASE_URL_DOMAIN } from '../../constants';
|
|
3
|
-
import { triggerEvent } from '../../events/ready';
|
|
4
|
-
import { Events } from '../../types/Events';
|
|
5
|
-
|
|
6
|
-
let auth0Client: Auth0Client;
|
|
7
|
-
|
|
8
|
-
export async function init({ clientId }: { clientId: string }) {
|
|
9
|
-
auth0Client = await createAuth0Client({
|
|
10
|
-
domain: `token.${BASE_URL_DOMAIN}`,
|
|
11
|
-
clientId,
|
|
12
|
-
cacheLocation: 'localstorage',
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
if (window.location.search.includes('code=')) {
|
|
16
|
-
try {
|
|
17
|
-
await auth0Client.handleRedirectCallback();
|
|
18
|
-
const user = await auth0Client.getUser();
|
|
19
|
-
triggerEvent(Events.AUTHENTICATED, user);
|
|
20
|
-
window.history.replaceState({}, document.title, '/');
|
|
21
|
-
const accessToken = await auth0Client.getTokenSilently();
|
|
22
|
-
localStorage.setItem('accessToken', accessToken);
|
|
23
|
-
} catch (err) {
|
|
24
|
-
// Fail silently
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export async function isAuthenticated() {
|
|
30
|
-
if (!auth0Client) {
|
|
31
|
-
throw new Error('Auth0 client not initialized');
|
|
32
|
-
}
|
|
33
|
-
return auth0Client.isAuthenticated();
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export async function getUser() {
|
|
37
|
-
if (!auth0Client) {
|
|
38
|
-
throw new Error('Auth0 client not initialized');
|
|
39
|
-
}
|
|
40
|
-
return auth0Client.getUser();
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export function loginWithRedirect() {
|
|
44
|
-
if (!auth0Client) {
|
|
45
|
-
throw new Error('Auth0 client not initialized');
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
return auth0Client.loginWithRedirect({
|
|
49
|
-
authorizationParams: {
|
|
50
|
-
redirect_uri: window.location.href,
|
|
51
|
-
},
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export async function getTokenSilently() {
|
|
56
|
-
if (!auth0Client) {
|
|
57
|
-
throw new Error('Auth0 client not initialized');
|
|
58
|
-
}
|
|
59
|
-
return auth0Client.getTokenSilently();
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export async function logout() {
|
|
63
|
-
if (!auth0Client) {
|
|
64
|
-
throw new Error('Auth0 client not initialized');
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
triggerEvent(Events.LOGOUT, {});
|
|
68
|
-
|
|
69
|
-
return auth0Client.logout({
|
|
70
|
-
logoutParams: {
|
|
71
|
-
returnTo: window.location.href,
|
|
72
|
-
},
|
|
73
|
-
});
|
|
74
|
-
}
|
|
@@ -1,160 +0,0 @@
|
|
|
1
|
-
import { dedupe, retry } from "wretch/middlewares";
|
|
2
|
-
import wretch, {
|
|
3
|
-
ConfiguredMiddleware,
|
|
4
|
-
WretchOptions,
|
|
5
|
-
WretchResponse,
|
|
6
|
-
} from "wretch";
|
|
7
|
-
import { BASE_URL_DOMAIN } from "../../constants";
|
|
8
|
-
import { getTokenSilently } from "../auth";
|
|
9
|
-
import { Entitlement } from "../../types/Entitlement";
|
|
10
|
-
import { Contract } from "../../types/Contracts";
|
|
11
|
-
import { Bill } from "../../types/Bills";
|
|
12
|
-
import { Tag, TagValue } from "../../types/Tag";
|
|
13
|
-
|
|
14
|
-
const auth0Middleware: ConfiguredMiddleware =
|
|
15
|
-
next =>
|
|
16
|
-
(url: string, options: WretchOptions): Promise<WretchResponse> => {
|
|
17
|
-
return new Promise(async (resolve, reject) => {
|
|
18
|
-
try {
|
|
19
|
-
const accessToken = await getTokenSilently();
|
|
20
|
-
|
|
21
|
-
const modifiedOptions = {
|
|
22
|
-
...options,
|
|
23
|
-
headers: {
|
|
24
|
-
...options.headers,
|
|
25
|
-
Authorization: `Bearer ${accessToken}`,
|
|
26
|
-
},
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
resolve(next(url, modifiedOptions));
|
|
30
|
-
} catch (error) {
|
|
31
|
-
console.error('Error fetching access token:', error);
|
|
32
|
-
reject(error);
|
|
33
|
-
}
|
|
34
|
-
});
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
const basicAuthMiddleware: ConfiguredMiddleware =
|
|
38
|
-
next =>
|
|
39
|
-
(url: string, options: WretchOptions): Promise<WretchResponse> => {
|
|
40
|
-
const anonymousId = localStorage.getItem('__anon_id');
|
|
41
|
-
const accessToken = btoa(`${anonymousId}:`);
|
|
42
|
-
|
|
43
|
-
const modifiedOptions = {
|
|
44
|
-
...options,
|
|
45
|
-
headers: {
|
|
46
|
-
...options.headers,
|
|
47
|
-
Authorization: `Basic ${accessToken}`,
|
|
48
|
-
},
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
return next(url, modifiedOptions);
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
const api = wretch(`https://api2.${BASE_URL_DOMAIN}`)
|
|
55
|
-
.headers({ 'Content-Type': 'application/json' })
|
|
56
|
-
.middlewares([
|
|
57
|
-
auth0Middleware,
|
|
58
|
-
dedupe(),
|
|
59
|
-
retry({
|
|
60
|
-
delayTimer: 1000,
|
|
61
|
-
delayRamp: (delay, nbOfAttempts) => delay * nbOfAttempts,
|
|
62
|
-
maxAttempts: 3,
|
|
63
|
-
until: response => response?.ok || false,
|
|
64
|
-
retryOnNetworkError: false,
|
|
65
|
-
}),
|
|
66
|
-
]);
|
|
67
|
-
|
|
68
|
-
const apiWithBasicAuth = wretch(`https://api2.${BASE_URL_DOMAIN}`)
|
|
69
|
-
.headers({ 'Content-Type': 'application/json' })
|
|
70
|
-
.middlewares([
|
|
71
|
-
basicAuthMiddleware,
|
|
72
|
-
dedupe(),
|
|
73
|
-
retry({
|
|
74
|
-
delayTimer: 1000,
|
|
75
|
-
delayRamp: (delay, nbOfAttempts) => delay * nbOfAttempts,
|
|
76
|
-
maxAttempts: 3,
|
|
77
|
-
until: response => response?.ok || false,
|
|
78
|
-
retryOnNetworkError: false,
|
|
79
|
-
}),
|
|
80
|
-
]);
|
|
81
|
-
|
|
82
|
-
export async function getEntitlements(): Promise<Entitlement[]> {
|
|
83
|
-
return api.get('/entitlements').json();
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
export async function getEntitlement(entitlementId: string) {
|
|
87
|
-
const entitlements: Entitlement[] = await api.get('/entitlements').json();
|
|
88
|
-
|
|
89
|
-
return entitlements?.find(entitlement => entitlement.id === entitlementId);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
export async function getContracts(): Promise<Contract[]> {
|
|
93
|
-
return api.get('/contracts').json();
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
export async function getContract(contractId: string) {
|
|
97
|
-
const contracts: Contract[] = await api.get('/contracts').json();
|
|
98
|
-
|
|
99
|
-
return contracts?.find(contract => contract.id === contractId);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
export async function getBills(): Promise<Bill[]> {
|
|
103
|
-
return api.get('/bills').json();
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
export async function getBill(billId: string) {
|
|
107
|
-
const bills: Bill[] = await api.get('/bills').json();
|
|
108
|
-
|
|
109
|
-
return bills?.find(bill => bill.id === billId);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
export async function getTags() {
|
|
113
|
-
const tags: { [id: string]: TagValue }[] = await apiWithBasicAuth
|
|
114
|
-
.get("/tags")
|
|
115
|
-
.json();
|
|
116
|
-
|
|
117
|
-
return tags;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
export async function setTag(id: string, value: string | number | (string | number)[]) {
|
|
121
|
-
const tags: { [id: string]: Tag }[] = await apiWithBasicAuth
|
|
122
|
-
.url(`/tags/${id}`)
|
|
123
|
-
.put({
|
|
124
|
-
value,
|
|
125
|
-
})
|
|
126
|
-
.json();
|
|
127
|
-
|
|
128
|
-
return tags;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
export async function pushTag(id: string, value: string | number) {
|
|
132
|
-
const tags: { [id: string]: TagValue }[] = await apiWithBasicAuth
|
|
133
|
-
.url(`/tags/${id}/push`)
|
|
134
|
-
.put({
|
|
135
|
-
value,
|
|
136
|
-
})
|
|
137
|
-
.json();
|
|
138
|
-
|
|
139
|
-
return tags;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
// Deprecated. Use tags instead
|
|
143
|
-
export interface AccessArticleParams {
|
|
144
|
-
articleId: string;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
export async function accessArticle(params: AccessArticleParams): Promise<{ status: string }> {
|
|
148
|
-
const userId = localStorage.getItem('__anon_id');
|
|
149
|
-
if (!userId) {
|
|
150
|
-
throw new Error('User id not found');
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
return apiWithBasicAuth
|
|
154
|
-
.url('/articles/access')
|
|
155
|
-
.post({
|
|
156
|
-
articleId: params.articleId,
|
|
157
|
-
userId,
|
|
158
|
-
})
|
|
159
|
-
.json();
|
|
160
|
-
}
|
package/src/state.ts
DELETED