Add CompatPromise conditional type

This commit is contained in:
Jonas Kello 2019-04-19 19:00:03 +02:00 committed by Stan Silvert
parent d593ac3e6f
commit 96f13e15ca

View file

@ -26,7 +26,7 @@ export = Keycloak;
* Creates a new Keycloak client instance.
* @param config Path to a JSON config file or a plain config object.
*/
declare function Keycloak(config?: string|{}): Keycloak.KeycloakInstance;
declare function Keycloak<TPromise extends Keycloak.PromiseType = undefined>(config?: string|{}): Keycloak.KeycloakInstance<TPromise>;
declare namespace Keycloak {
type KeycloakAdapterName = 'cordova' | 'cordova-native' |'default' | any;
@ -244,11 +244,19 @@ declare namespace Keycloak {
// export interface KeycloakUserInfo {}
/**
* Conditional CompatPromise type in order to support
* both legacy promises and native promises as return types.
*/
type PromiseType = KeycloakPromiseType | undefined;
type CompatPromise<TPromiseType extends PromiseType, TSuccess, TError> =
TPromiseType extends KeycloakPromiseType ? Promise<TSuccess> : KeycloakPromise<TSuccess, TError>;
/**
* A client for the Keycloak authentication server.
* @see {@link https://keycloak.gitbooks.io/securing-client-applications-guide/content/topics/oidc/javascript-adapter.html|Keycloak JS adapter documentation}
*/
interface KeycloakInstance {
interface KeycloakInstance<TPromise extends PromiseType = undefined> {
/**
* Is true if the user is authenticated, false otherwise.
*/
@ -413,32 +421,32 @@ declare namespace Keycloak {
* @param initOptions Initialization options.
* @returns A promise to set functions to be invoked on success or error.
*/
init(initOptions: KeycloakInitOptions): KeycloakPromise<boolean, KeycloakError>;
init(initOptions: KeycloakInitOptions): CompatPromise<TPromise, boolean, KeycloakError>;
/**
* Redirects to login form.
* @param options Login options.
*/
login(options?: KeycloakLoginOptions): KeycloakPromise<void, void>;
login(options?: KeycloakLoginOptions): CompatPromise<TPromise, void, void>;
/**
* Redirects to logout.
* @param options Logout options.
* @param options.redirectUri Specifies the uri to redirect to after logout.
*/
logout(options?: any): KeycloakPromise<void, void>;
logout(options?: any): CompatPromise<TPromise, void, void>;
/**
* Redirects to registration form.
* @param options Supports same options as Keycloak#login but `action` is
* set to `'register'`.
*/
register(options?: any): KeycloakPromise<void, void>;
register(options?: any): CompatPromise<TPromise, void, void>;
/**
* Redirects to the Account Management Console.
*/
accountManagement(): KeycloakPromise<void, void>;
accountManagement(): CompatPromise<TPromise, void, void>;
/**
* Returns the URL to login form.
@ -490,7 +498,7 @@ declare namespace Keycloak {
* alert('Failed to refresh the token, or the session has expired');
* });
*/
updateToken(minValidity: number): KeycloakPromise<boolean, boolean>;
updateToken(minValidity: number): CompatPromise<TPromise, boolean, boolean>;
/**
* Clears authentication state, including tokens. This can be useful if
@ -517,11 +525,11 @@ declare namespace Keycloak {
* Loads the user's profile.
* @returns A promise to set functions to be invoked on success or error.
*/
loadUserProfile(): KeycloakPromise<KeycloakProfile, void>;
loadUserProfile(): CompatPromise<TPromise, KeycloakProfile, void>;
/**
* @private Undocumented.
*/
loadUserInfo(): KeycloakPromise<{}, void>;
loadUserInfo(): CompatPromise<TPromise, {}, void>;
}
}