Skip to content

Commit 33c0aec

Browse files
committed
SPR-20295 add require to web apis
1 parent 7820f27 commit 33c0aec

File tree

1 file changed

+96
-92
lines changed

1 file changed

+96
-92
lines changed

src/ps-web-apis.ts

+96-92
Original file line numberDiff line numberDiff line change
@@ -1,123 +1,127 @@
11
import { provide } from "./apiprovide";
22

3-
function requireApi<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-
},
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+
},
2222

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
3030

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-
});
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+
}
3938
});
39+
});
4040
}
4141

4242
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;
4646
}
4747

4848
export interface PurchaseData {
49-
entitlements: [string];
49+
entitlements: [string];
5050
}
5151
export type FetchOptions = RequestInit & { timeout?: number };
5252

5353
/**
5454
* Custom fetch interface which includes the possibility to customize timeouts for fetch requests
5555
*/
56-
export type Fetch = (input: RequestInfo, init?: FetchOptions) => Promise<Response>;
56+
export type Fetch = (
57+
input: RequestInfo,
58+
init?: FetchOptions
59+
) => Promise<Response>;
5760

5861
export interface WhoamiV1 {
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>;
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>;
109112
}
110113

111114
export interface UtilsV1 {
112-
fetchWithTimeout: Fetch;
115+
fetchWithTimeout: Fetch;
113116
}
114117

115118
export function whoamiV1(): Promise<WhoamiV1> {
116-
return requireApi("whoami:v1");
119+
return requirePackage("whoami:v1");
117120
}
118121

119122
export function utilsV1(): Promise<UtilsV1> {
120-
return requireApi("utils:v1");
123+
return requirePackage("utils:v1");
121124
}
122125

123126
export const provideApi = provide;
127+
export const requireApi = requirePackage;

0 commit comments

Comments
 (0)