@amedia/user 0.1.1 → 0.2.0
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/actions/authentication.d.ts +4 -0
- package/dist/actions/authorization.d.ts +3 -0
- package/dist/cache/engines/global.d.ts +6 -0
- package/dist/cache/engines/indexeddb.d.ts +6 -0
- package/dist/cache/index.d.ts +9 -0
- package/dist/cache/types.d.ts +10 -0
- package/dist/eventHandlers/activation.d.ts +13 -0
- package/dist/eventHandlers/authentication.d.ts +15 -0
- package/dist/eventHandlers/userAttributes.d.ts +12 -0
- package/dist/index.d.ts +21 -206
- package/dist/index.js +1 -497
- package/dist/lib/authentication.d.ts +20 -0
- package/dist/lib/authorization.d.ts +15 -0
- package/dist/lib/cache/db.d.ts +6 -0
- package/dist/lib/cache/global.d.ts +9 -0
- package/dist/lib/cache/types.d.ts +5 -0
- package/dist/lib/dataOrganizer.d.ts +2 -0
- package/dist/lib/eventHandler.d.ts +3 -0
- package/dist/lib/queue.d.ts +5 -0
- package/dist/lib/types.d.ts +7 -0
- package/dist/lib/userAttributes.d.ts +12 -0
- package/dist/services/cache.d.ts +9 -0
- package/dist/services/jupiter.d.ts +29 -0
- package/dist/services/mercury.d.ts +15 -0
- package/dist/services/pluto.d.ts +8 -0
- package/dist/types.d.ts +12 -0
- package/dist/utils/customEventWrapper.d.ts +3 -0
- package/dist/utils/queue.d.ts +5 -0
- package/package.json +8 -5
- package/src/actions/authentication.ts +37 -0
- package/src/actions/authorization.ts +51 -0
- package/src/{lib/cache → cache/engines}/global.ts +5 -3
- package/src/{lib/cache/db.ts → cache/engines/indexeddb.ts} +5 -5
- package/src/cache/index.spec.ts +82 -0
- package/src/{services/cache.ts → cache/index.ts} +25 -13
- package/src/{lib/cache → cache}/types.ts +7 -1
- package/src/eventHandlers/activation.ts +29 -0
- package/src/eventHandlers/authentication.ts +29 -0
- package/src/{lib → eventHandlers}/userAttributes.ts +11 -12
- package/src/index.spec.ts +49 -30
- package/src/index.ts +14 -5
- package/src/lib/dataOrganizer.ts +33 -14
- package/src/services/pluto.spec.ts +3 -2
- package/src/services/pluto.ts +14 -10
- package/src/types.ts +13 -0
- package/src/utils/customEventWrapper.ts +42 -0
- package/src/utils/log.ts +7 -0
- package/tsconfig.json +2 -4
- package/webpack.config.js +25 -0
- package/src/lib/authentication.ts +0 -94
- package/src/lib/authorization.ts +0 -86
- package/src/lib/eventHandler.ts +0 -16
- package/src/lib/types.ts +0 -7
- /package/src/{lib → utils}/queue.ts +0 -0
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { AccessFeatures, CheckAccessForPaper } from '../services/jupiter';
|
|
2
|
+
export declare const checkForAccessToPublication: (publicationDomain: string, accessFeatures: AccessFeatures) => Promise<CheckAccessForPaper | null>;
|
|
3
|
+
export declare const activate: () => Promise<unknown>;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { CacheEntry, Engine } from '../types';
|
|
2
|
+
export declare const get: (key: string) => Promise<CacheEntry>;
|
|
3
|
+
export declare const set: (key: string, data: unknown, expires: number) => Promise<void>;
|
|
4
|
+
export declare const remove: (key: string) => Promise<void>;
|
|
5
|
+
declare const _default: Engine;
|
|
6
|
+
export default _default;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { CacheEntry, Engine } from '../types';
|
|
2
|
+
export declare const get: (key: string) => Promise<CacheEntry>;
|
|
3
|
+
export declare const set: (key: string, data: unknown, expires: number) => Promise<void>;
|
|
4
|
+
export declare const remove: (key: string) => Promise<void>;
|
|
5
|
+
declare const _default: Engine;
|
|
6
|
+
export default _default;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Record, Static } from 'runtypes';
|
|
2
|
+
import { Engine } from './types';
|
|
3
|
+
export declare const setCacheEngine: (engine: Engine) => void;
|
|
4
|
+
declare const _default: {
|
|
5
|
+
get: <T extends Record<any, any>>(key: string, validator: T) => Promise<void | Static<T>>;
|
|
6
|
+
set: (key: string, value: unknown, expires: number) => Promise<void>;
|
|
7
|
+
remove: (key: string) => Promise<void>;
|
|
8
|
+
};
|
|
9
|
+
export default _default;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare type CacheEntry = {
|
|
2
|
+
key: string;
|
|
3
|
+
data: unknown;
|
|
4
|
+
expires: number;
|
|
5
|
+
};
|
|
6
|
+
export declare type Engine = {
|
|
7
|
+
get: (key: string) => Promise<CacheEntry | null>;
|
|
8
|
+
set: (key: string, data: unknown, expires: number) => Promise<void>;
|
|
9
|
+
remove: (key: string) => Promise<void>;
|
|
10
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export declare enum ActivationEvents {
|
|
2
|
+
REQUEST_ACTIVATION = "amedia-user.activation.request-activation",
|
|
3
|
+
ON_ACTIVATION_COMPLETE = "amedia-user.activation.on-activation-complete"
|
|
4
|
+
}
|
|
5
|
+
declare global {
|
|
6
|
+
interface WindowEventMap {
|
|
7
|
+
[ActivationEvents.REQUEST_ACTIVATION]: CustomEvent;
|
|
8
|
+
[ActivationEvents.ON_ACTIVATION_COMPLETE]: CustomEvent<{
|
|
9
|
+
status: 'success' | 'error';
|
|
10
|
+
}>;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
export declare const listenForActivationRequests: () => void;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export declare enum AuthenticationEvents {
|
|
2
|
+
REQUEST_LOGIN = "amedia-user.authentication.request-login",
|
|
3
|
+
REQUEST_LOGOUT = "amedia-user.authentication.request-logout"
|
|
4
|
+
}
|
|
5
|
+
declare global {
|
|
6
|
+
interface WindowEventMap {
|
|
7
|
+
[AuthenticationEvents.REQUEST_LOGIN]: CustomEvent<{
|
|
8
|
+
requestedUrl?: string;
|
|
9
|
+
}>;
|
|
10
|
+
[AuthenticationEvents.REQUEST_LOGOUT]: CustomEvent<{
|
|
11
|
+
requestedUrl?: string;
|
|
12
|
+
}>;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
export declare const listenForAuthenticationRequests: () => void;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { UserAttributes } from '../types';
|
|
2
|
+
export declare enum AmediaUserEvents {
|
|
3
|
+
REQUEST_USER_ATTRIBUTES = "amedia-user.request-user-attributes",
|
|
4
|
+
ON_USER_ATTRIBUTES_UPDATED = "amedia-user.on-user-attributes-updated"
|
|
5
|
+
}
|
|
6
|
+
declare global {
|
|
7
|
+
interface WindowEventMap {
|
|
8
|
+
[AmediaUserEvents.REQUEST_USER_ATTRIBUTES]: CustomEvent<Array<keyof UserAttributes>>;
|
|
9
|
+
[AmediaUserEvents.ON_USER_ATTRIBUTES_UPDATED]: CustomEvent<UserAttributes>;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export declare const listenForUserAttributeRequests: () => void;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,206 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
export const setCacheEngine: (engine: Engine) => void;
|
|
23
|
-
const _default_1: {
|
|
24
|
-
get: <T extends Record<any, any>>(key: string, validator: T) => Promise<void | Static<T>>;
|
|
25
|
-
set: (key: string, value: unknown, expires: number) => Promise<void>;
|
|
26
|
-
remove: (key: string) => Promise<void>;
|
|
27
|
-
};
|
|
28
|
-
export default _default_1;
|
|
29
|
-
}
|
|
30
|
-
declare module "services/pluto" {
|
|
31
|
-
import { Optional, Record, Static, String } from 'runtypes';
|
|
32
|
-
export const SessionSchema: Record<{
|
|
33
|
-
created_at: String;
|
|
34
|
-
updated_at: String;
|
|
35
|
-
user_uuid: String;
|
|
36
|
-
user_name: String;
|
|
37
|
-
tracking_key: String;
|
|
38
|
-
user_agent: Optional<String>;
|
|
39
|
-
user_key: Optional<String>;
|
|
40
|
-
}, false>;
|
|
41
|
-
export type Session = Static<typeof SessionSchema>;
|
|
42
|
-
export const getCurrentSession: () => Promise<Session>;
|
|
43
|
-
export const removeSession: (domain: string) => Promise<void>;
|
|
44
|
-
}
|
|
45
|
-
declare module "lib/eventHandler" {
|
|
46
|
-
export const setRootElement: (element: any) => void;
|
|
47
|
-
export const listen: <T extends keyof WindowEventMap>(type: T, listener: (this: Window, ev: WindowEventMap[T]) => void) => void;
|
|
48
|
-
export const dispatch: <T extends Event>(event: T) => void;
|
|
49
|
-
}
|
|
50
|
-
declare module "lib/authentication" {
|
|
51
|
-
import { Session } from "services/pluto";
|
|
52
|
-
export enum AuthenticationEvents {
|
|
53
|
-
REQUEST_LOGIN = "amedia_user.authentication.request_login",
|
|
54
|
-
REQUEST_LOGOUT = "amedia_user.authentication.request_logout",
|
|
55
|
-
REQUEST_AUTH_STATE = "amedia_user.authentication.request_auth_state",
|
|
56
|
-
ON_AUTH_STATE_UPDATED = "amedia_user.authentication.on_auth_state_updated"
|
|
57
|
-
}
|
|
58
|
-
global {
|
|
59
|
-
interface WindowEventMap {
|
|
60
|
-
[AuthenticationEvents.REQUEST_LOGIN]: CustomEvent<{
|
|
61
|
-
requestedUrl?: string;
|
|
62
|
-
}>;
|
|
63
|
-
[AuthenticationEvents.REQUEST_LOGOUT]: CustomEvent<{
|
|
64
|
-
requestedUrl?: string;
|
|
65
|
-
}>;
|
|
66
|
-
[AuthenticationEvents.REQUEST_AUTH_STATE]: CustomEvent<Session>;
|
|
67
|
-
[AuthenticationEvents.ON_AUTH_STATE_UPDATED]: CustomEvent<Session>;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
export const listenForAuthenticationRequests: () => void;
|
|
71
|
-
}
|
|
72
|
-
declare module "lib/types" {
|
|
73
|
-
export type UserAttributes = {
|
|
74
|
-
isLoggedIn?: boolean;
|
|
75
|
-
name?: string;
|
|
76
|
-
email?: string;
|
|
77
|
-
phone?: string;
|
|
78
|
-
trackingKey?: string;
|
|
79
|
-
};
|
|
80
|
-
}
|
|
81
|
-
declare module "lib/dataOrganizer" {
|
|
82
|
-
import { UserAttributes } from "lib/types";
|
|
83
|
-
/**
|
|
84
|
-
* Jeg tror ikke vi burde gjøre det slik som dette.
|
|
85
|
-
* Denne funksjonen var hentet ut fra userAttribute kun for å
|
|
86
|
-
* kunne mocke implementasjonen så jeg kunne teste event-callback mekanismen.
|
|
87
|
-
*
|
|
88
|
-
* En implementasjon som dette vil gjøre det vanskelig svare etterhvert som vi får data.
|
|
89
|
-
*/
|
|
90
|
-
export const fetchDataBasedOnAttributes: (attributes: (keyof UserAttributes)[]) => UserAttributes;
|
|
91
|
-
}
|
|
92
|
-
declare module "lib/userAttributes" {
|
|
93
|
-
import { UserAttributes } from "lib/types";
|
|
94
|
-
export enum AmediaUserEvents {
|
|
95
|
-
REQUEST_USER_ATTRIBUTES = "amedia-user.request-user-attributes",
|
|
96
|
-
ON_USER_ATTRIBUTES_UPDATED = "amedia-user.on-user-attributes-updated"
|
|
97
|
-
}
|
|
98
|
-
global {
|
|
99
|
-
interface WindowEventMap {
|
|
100
|
-
[AmediaUserEvents.REQUEST_USER_ATTRIBUTES]: CustomEvent<Array<keyof UserAttributes>>;
|
|
101
|
-
[AmediaUserEvents.ON_USER_ATTRIBUTES_UPDATED]: CustomEvent<UserAttributes>;
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
export const listenForUserAttributeRequests: () => void;
|
|
105
|
-
}
|
|
106
|
-
declare module "index" {
|
|
107
|
-
import { UserAttributes } from "lib/types";
|
|
108
|
-
/**
|
|
109
|
-
* Enables you to subscribe to the user attributes you care about and
|
|
110
|
-
* get notified when something changes
|
|
111
|
-
*
|
|
112
|
-
* If you're wondering about the typing mechanism, here's a good article:
|
|
113
|
-
* https://dev.to/shakyshane/2-ways-to-create-a-union-from-an-array-in-typescript-1kd6
|
|
114
|
-
*
|
|
115
|
-
* @todo We should discuss whether we should support to stop subscribing
|
|
116
|
-
* @todo or resubscribe with different attributes.
|
|
117
|
-
* @param attributes
|
|
118
|
-
* @param onUserUpdated
|
|
119
|
-
*/
|
|
120
|
-
export const subscribeToUserUpdates: <T extends (keyof UserAttributes)[]>(attributes: T, onUserUpdated: (attributes: Pick<UserAttributes, "isLoggedIn" | T[number]>) => void) => void;
|
|
121
|
-
/**
|
|
122
|
-
* This will, if necessary, trigger a redirect to www.aid.no and return
|
|
123
|
-
* to the requestedUrl if supplied or the current url before redirecting away if not.
|
|
124
|
-
* @param requestedUrl
|
|
125
|
-
*/
|
|
126
|
-
export const requestLogin: (requestedUrl?: string | undefined) => void;
|
|
127
|
-
}
|
|
128
|
-
declare module "services/jupiter" {
|
|
129
|
-
import { Array, Literal, Record, Static, String, Number, Union } from 'runtypes';
|
|
130
|
-
export type AccessFeatures = string[];
|
|
131
|
-
export const CheckAccessForPaperSchema: Record<{
|
|
132
|
-
status: Literal<"success">;
|
|
133
|
-
access: Union<[Literal<"granted">, Literal<"denied">]>;
|
|
134
|
-
publication: Record<{
|
|
135
|
-
id: Number;
|
|
136
|
-
domain: String;
|
|
137
|
-
}, false>;
|
|
138
|
-
access_features: Array<String, false>;
|
|
139
|
-
}, false>;
|
|
140
|
-
export type CheckAccessForPaper = Static<typeof CheckAccessForPaperSchema>;
|
|
141
|
-
const GetVstokenSchema: Record<{
|
|
142
|
-
status: Literal<"success">;
|
|
143
|
-
cookie: String;
|
|
144
|
-
access_features: Array<String, false>;
|
|
145
|
-
}, false>;
|
|
146
|
-
export type GetVstoken = Static<typeof GetVstokenSchema>;
|
|
147
|
-
const GetNonUserAccessSchema: Record<{
|
|
148
|
-
status: Literal<"success">;
|
|
149
|
-
vstoken_cookie: String;
|
|
150
|
-
uuid: String;
|
|
151
|
-
access_features: Array<String, false>;
|
|
152
|
-
}, false>;
|
|
153
|
-
export type GetNonUserAccess = Static<typeof GetNonUserAccessSchema>;
|
|
154
|
-
export const checkForAccess: (publicationDomain: string, accessFeatures: AccessFeatures) => Promise<CheckAccessForPaper>;
|
|
155
|
-
export const getVstoken: (publicationDomain: string) => Promise<GetVstoken>;
|
|
156
|
-
export const getNonUserAccess: (publicationDomain: string) => Promise<GetNonUserAccess>;
|
|
157
|
-
}
|
|
158
|
-
declare module "lib/authorization" {
|
|
159
|
-
import { AccessFeatures, CheckAccessForPaper } from "services/jupiter";
|
|
160
|
-
export enum AuthorizationEvents {
|
|
161
|
-
REQUEST_PUBLICATION_ACCESS_STATE_CHECK = "amedia_user.authorization.request_publication_access_state_check",
|
|
162
|
-
ON_PUBLICATION_ACCESS_STATE_UPDATED = "amedia_user.authorization.on_publication_access_state_updated"
|
|
163
|
-
}
|
|
164
|
-
global {
|
|
165
|
-
interface WindowEventMap {
|
|
166
|
-
[AuthorizationEvents.REQUEST_PUBLICATION_ACCESS_STATE_CHECK]: CustomEvent<{
|
|
167
|
-
publicationDomain: string;
|
|
168
|
-
accessFeatures: AccessFeatures;
|
|
169
|
-
}>;
|
|
170
|
-
[AuthorizationEvents.ON_PUBLICATION_ACCESS_STATE_UPDATED]: CustomEvent<CheckAccessForPaper>;
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
export const listenForAuthorizationRequests: () => void;
|
|
174
|
-
}
|
|
175
|
-
declare module "lib/queue" {
|
|
176
|
-
const _default_2: {
|
|
177
|
-
add: (key: string, data: unknown) => void;
|
|
178
|
-
getAll: (key: string) => unknown[];
|
|
179
|
-
};
|
|
180
|
-
export default _default_2;
|
|
181
|
-
}
|
|
182
|
-
declare module "lib/cache/db" {
|
|
183
|
-
import { Engine } from "lib/cache/types";
|
|
184
|
-
export const get: (key: string) => Promise<unknown>;
|
|
185
|
-
export const set: (key: string, data: unknown, expires: number) => Promise<void>;
|
|
186
|
-
export const remove: (key: string) => Promise<void>;
|
|
187
|
-
const _default_3: Engine;
|
|
188
|
-
export default _default_3;
|
|
189
|
-
}
|
|
190
|
-
declare module "services/mercury" {
|
|
191
|
-
const log: {
|
|
192
|
-
error: (message: string, context: {
|
|
193
|
-
[index: string]: any;
|
|
194
|
-
}) => void;
|
|
195
|
-
warn: (message: string, context: {
|
|
196
|
-
[index: string]: any;
|
|
197
|
-
}) => void;
|
|
198
|
-
info: (message: string, context: {
|
|
199
|
-
[index: string]: any;
|
|
200
|
-
}) => void;
|
|
201
|
-
debug: (message: string, context: {
|
|
202
|
-
[index: string]: any;
|
|
203
|
-
}) => void;
|
|
204
|
-
};
|
|
205
|
-
export default log;
|
|
206
|
-
}
|
|
1
|
+
import { UserAttributes } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Enables you to subscribe to the user attributes you care about and
|
|
4
|
+
* get notified when something changes
|
|
5
|
+
*
|
|
6
|
+
* If you're wondering about the typing mechanism, here's a good article:
|
|
7
|
+
* https://dev.to/shakyshane/2-ways-to-create-a-union-from-an-array-in-typescript-1kd6
|
|
8
|
+
*
|
|
9
|
+
* @todo We should discuss whether we should support to stop subscribing
|
|
10
|
+
* @todo or resubscribe with different attributes.
|
|
11
|
+
* @param attributes
|
|
12
|
+
* @param onUserUpdated
|
|
13
|
+
*/
|
|
14
|
+
export declare const subscribeToUserUpdates: <T extends (keyof UserAttributes)[]>(attributes: T, onUserUpdated: (attributes: Pick<UserAttributes, "isLoggedIn" | T[number]>) => void) => void;
|
|
15
|
+
/**
|
|
16
|
+
* This will, if necessary, trigger a redirect to www.aid.no and return
|
|
17
|
+
* to the requestedUrl if supplied or the current url before redirecting away if not.
|
|
18
|
+
* @param requestedUrl
|
|
19
|
+
*/
|
|
20
|
+
export declare const requestLogin: (requestedUrl?: string | undefined) => void;
|
|
21
|
+
export declare const activate: () => Promise<unknown>;
|