|
1 | 1 | import { provide } from "./apiprovide";
|
2 | 2 |
|
3 | 3 | function requirePackage<T>(name: string): Promise<T> {
|
4 |
| - // -- START -- static loader |
5 |
| - const unresolvedPackages = {} as any; |
6 |
| - const providedPackages = {} as any; |
7 |
| - const loaderName = "pssmasloader"; |
8 |
| - // set or reuse existing loader implementation |
9 |
| - const loader = ((window as any)[loaderName] = (window as any)[loaderName] || { |
10 |
| - // Requires packageName and returns it via callback |
11 |
| - require(packageName: string, cb: any) { |
12 |
| - const pack = providedPackages[packageName]; |
13 |
| - if (pack !== undefined) { |
14 |
| - // -- will callback directly if required functionality was already provided |
15 |
| - cb(pack, null); |
16 |
| - } else { |
17 |
| - // -- will queue callbacks if required functionality is not yet available |
18 |
| - unresolvedPackages[packageName] = unresolvedPackages[packageName] || []; |
19 |
| - unresolvedPackages[packageName].push(cb); |
20 |
| - } |
21 |
| - }, |
| 4 | + // -- START -- static loader |
| 5 | + const unresolvedPackages = {} as any; |
| 6 | + const providedPackages = {} as any; |
| 7 | + const loaderName = "pssmasloader"; |
| 8 | + // set or reuse existing loader implementation |
| 9 | + const loader = ((window as any)[loaderName] = (window as any)[loaderName] || { |
| 10 | + // Requires packageName and returns it via callback |
| 11 | + require(packageName: string, cb: any) { |
| 12 | + const pack = providedPackages[packageName]; |
| 13 | + if (pack !== undefined) { |
| 14 | + // -- will callback directly if required functionality was already provided |
| 15 | + cb(pack, null); |
| 16 | + } else { |
| 17 | + // -- will queue callbacks if required functionality is not yet available |
| 18 | + unresolvedPackages[packageName] = unresolvedPackages[packageName] || []; |
| 19 | + unresolvedPackages[packageName].push(cb); |
| 20 | + } |
| 21 | + }, |
22 | 22 |
|
23 |
| - // private state |
24 |
| - _: { |
25 |
| - u: unresolvedPackages, |
26 |
| - p: providedPackages, |
27 |
| - }, |
28 |
| - }); |
29 |
| - // -- END -- static loader |
| 23 | + // private state |
| 24 | + _: { |
| 25 | + u: unresolvedPackages, |
| 26 | + p: providedPackages |
| 27 | + } |
| 28 | + }); |
| 29 | + // -- END -- static loader |
30 | 30 |
|
31 |
| - return new Promise((resolve, reject) => { |
32 |
| - loader.require(name, (res: T, error: any) => { |
33 |
| - if (error) { |
34 |
| - reject(error); |
35 |
| - } else { |
36 |
| - resolve(res); |
37 |
| - } |
| 31 | + return new Promise((resolve, reject) => { |
| 32 | + loader.require(name, (res: T, error: any) => { |
| 33 | + if (error) { |
| 34 | + reject(error); |
| 35 | + } else { |
| 36 | + resolve(res); |
| 37 | + } |
| 38 | + }); |
38 | 39 | });
|
39 |
| - }); |
40 | 40 | }
|
41 | 41 |
|
42 | 42 | export interface WhoamiUserInfo {
|
43 |
| - user_id: string; |
44 |
| - first_name?: string; |
45 |
| - last_name?: string; |
| 43 | + user_id: string; |
| 44 | + first_name?: string; |
| 45 | + last_name?: string; |
46 | 46 | }
|
47 | 47 |
|
48 | 48 | export interface PurchaseData {
|
49 |
| - entitlements: [string]; |
| 49 | + entitlements: [string]; |
50 | 50 | }
|
51 | 51 | export type FetchOptions = RequestInit & { timeout?: number };
|
52 | 52 |
|
53 | 53 | /**
|
54 | 54 | * Custom fetch interface which includes the possibility to customize timeouts for fetch requests
|
55 | 55 | */
|
56 |
| -export type Fetch = ( |
57 |
| - input: RequestInfo, |
58 |
| - init?: FetchOptions |
59 |
| -) => Promise<Response>; |
| 56 | +export type Fetch = (input: RequestInfo, init?: FetchOptions) => Promise<Response>; |
60 | 57 |
|
61 | 58 | export interface WhoamiV1 {
|
62 |
| - /** |
63 |
| - * will assert valid not outdated session before fetch will be done. backend credentials will be added automatically |
64 |
| - * an error is resolved if session is invalid and not refeshable (= user logged out) |
65 |
| - * Important: as of version 1.9.9 all requests are timeout after 5s by default. |
66 |
| - * Can be changed by adding the field timeout to the FetchOptions Interface |
67 |
| - */ |
68 |
| - authorizedFetch: Fetch; |
69 |
| - /** |
70 |
| - * gives information if user is currently loggedin from ui perspective |
71 |
| - */ |
72 |
| - isLoggedIn(): boolean; |
73 |
| - /** |
74 |
| - * will assert valid not outdated session before promise is resolved |
75 |
| - * an error is resolved if session is invalid and not refeshable (= user logged out) |
76 |
| - */ |
77 |
| - ensureUserHasAuthorization(): Promise<void>; |
78 |
| - /** |
79 |
| - * will start login-process (e.g. go to sso-login) |
80 |
| - */ |
81 |
| - doLogin(additionalParameter?: Map<string, string[]>): void; |
82 |
| - /** |
83 |
| - * will start registration-process (e.g. go to sso-register) |
84 |
| - */ |
85 |
| - doRegister(additionalParameter?: Map<string, string[]>): void; |
86 |
| - /** |
87 |
| - * will start logout-process (e.g. go to sso-logout) |
88 |
| - */ |
89 |
| - doLogout(additionalParameter?: Map<string, string[]>): void; |
90 |
| - /** |
91 |
| - * will start logout-process and redirect user to portal homepage afterwards (e.g. go to sso-logout) |
92 |
| - */ |
93 |
| - doLogoutToHome(additionalParameter?: Map<string, string[]>): void; |
94 |
| - /** |
95 |
| - * will update access token and therefore content entitlements to current state |
96 |
| - */ |
97 |
| - forceAccessTokenRefresh(): Promise<void>; |
98 |
| - /** |
99 |
| - * will request userinfo from whoami backend |
100 |
| - * @return {WhoamiUserInfo} some relevant userdata |
101 |
| - */ |
102 |
| - getUserInfo(): Promise<WhoamiUserInfo>; |
103 |
| - /** |
104 |
| - * will request customer pseudo id for currently logged user from consent backend |
105 |
| - * @param clientId The string identifier of the client for which the customer id is requested. |
106 |
| - */ |
107 |
| - getCustomerId(clientId: string): Promise<string>; |
108 |
| - /** |
109 |
| - * will provide unsafe purchase data |
110 |
| - */ |
111 |
| - getUnsafePurchaseData(): Promise<PurchaseData>; |
| 59 | + /** |
| 60 | + * will assert valid not outdated session before fetch will be done. backend credentials will be added automatically |
| 61 | + * an error is resolved if session is invalid and not refeshable (= user logged out) |
| 62 | + * Important: as of version 1.9.9 all requests are timeout after 5s by default. |
| 63 | + * Can be changed by adding the field timeout to the FetchOptions Interface |
| 64 | + */ |
| 65 | + authorizedFetch: Fetch; |
| 66 | + /** |
| 67 | + * gives information if user is currently loggedin from ui perspective |
| 68 | + */ |
| 69 | + isLoggedIn(): boolean; |
| 70 | + /** |
| 71 | + * will assert valid not outdated session before promise is resolved |
| 72 | + * an error is resolved if session is invalid and not refeshable (= user logged out) |
| 73 | + */ |
| 74 | + ensureUserHasAuthorization(): Promise<void>; |
| 75 | + /** |
| 76 | + * will start login-process (e.g. go to sso-login) |
| 77 | + */ |
| 78 | + doLogin(additionalParameter?: Map<string, string[]>): void; |
| 79 | + /** |
| 80 | + * will start registration-process (e.g. go to sso-register) |
| 81 | + */ |
| 82 | + doRegister(additionalParameter?: Map<string, string[]>): void; |
| 83 | + /** |
| 84 | + * will start logout-process (e.g. go to sso-logout) |
| 85 | + */ |
| 86 | + doLogout(additionalParameter?: Map<string, string[]>): void; |
| 87 | + /** |
| 88 | + * will start logout-process and redirect user to portal homepage afterwards (e.g. go to sso-logout) |
| 89 | + */ |
| 90 | + doLogoutToHome(additionalParameter?: Map<string, string[]>): void; |
| 91 | + /** |
| 92 | + * will update access token and therefore content entitlements to current state |
| 93 | + */ |
| 94 | + forceAccessTokenRefresh(): Promise<void>; |
| 95 | + /** |
| 96 | + * will request userinfo from whoami backend |
| 97 | + * @return {WhoamiUserInfo} some relevant userdata |
| 98 | + */ |
| 99 | + getUserInfo(): Promise<WhoamiUserInfo>; |
| 100 | + /** |
| 101 | + * will request customer pseudo id for currently logged user from consent backend |
| 102 | + * @param clientId The string identifier of the client for which the customer id is requested. |
| 103 | + */ |
| 104 | + getCustomerId(clientId: string): Promise<string>; |
| 105 | + /** |
| 106 | + * will provide unsafe purchase data |
| 107 | + */ |
| 108 | + getUnsafePurchaseData(): Promise<PurchaseData>; |
112 | 109 | }
|
113 | 110 |
|
114 | 111 | export interface UtilsV1 {
|
115 |
| - fetchWithTimeout: Fetch; |
| 112 | + fetchWithTimeout: Fetch; |
116 | 113 | }
|
117 | 114 |
|
118 | 115 | export function whoamiV1(): Promise<WhoamiV1> {
|
119 |
| - return requirePackage("whoami:v1"); |
| 116 | + return requirePackage("whoami:v1"); |
120 | 117 | }
|
121 | 118 |
|
122 | 119 | export function utilsV1(): Promise<UtilsV1> {
|
123 |
| - return requirePackage("utils:v1"); |
| 120 | + return requirePackage("utils:v1"); |
124 | 121 | }
|
125 | 122 |
|
126 | 123 | export const provideApi = provide;
|
|
0 commit comments