@icanbwell/bwell-sdk-ts 0.3.2 → 0.4.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/__version__.d.ts +1 -1
- package/dist/__version__.js +1 -1
- package/dist/api/base/identity/identity-manager.d.ts +3 -3
- package/dist/api/graphql-api/identity/graphql-identity-manager.d.ts +3 -3
- package/dist/auth/auth-strategy.d.ts +2 -2
- package/dist/auth/oauth-strategy.d.ts +2 -2
- package/dist/auth/username-password-strategy.d.ts +2 -2
- package/dist/bwell-sdk/bwell-sdk.d.ts +13 -0
- package/dist/bwell-sdk/bwell-sdk.js +51 -18
- package/dist/index.d.ts +1 -1
- package/dist/tokens/jwt-token-manager.d.ts +2 -0
- package/dist/tokens/jwt-token-manager.js +5 -0
- package/package.json +1 -1
package/dist/__version__.d.ts
CHANGED
package/dist/__version__.js
CHANGED
|
@@ -16,7 +16,7 @@ export type RefreshTokensRequest = {
|
|
|
16
16
|
refreshToken: string;
|
|
17
17
|
clientKey: string;
|
|
18
18
|
};
|
|
19
|
-
export type
|
|
19
|
+
export type AuthTokens = {
|
|
20
20
|
accessToken: string;
|
|
21
21
|
idToken: string;
|
|
22
22
|
refreshToken: string;
|
|
@@ -26,7 +26,7 @@ export type AuthenticateResults = {
|
|
|
26
26
|
*/
|
|
27
27
|
export interface IdentityManager {
|
|
28
28
|
initialize(clientKey: string): Promise<BWellTransactionResult<SdkConfigurationResult, BaseManagerError>>;
|
|
29
|
-
authenticateWithOauth(authenticateRequest: OauthAuthenticateRequest): Promise<BWellTransactionResult<
|
|
30
|
-
authenticateWithUsernamePassword(authenticateRequest: UsernamePasswordAuthenticateRequest): Promise<BWellTransactionResult<
|
|
29
|
+
authenticateWithOauth(authenticateRequest: OauthAuthenticateRequest): Promise<BWellTransactionResult<AuthTokens, BaseManagerError>>;
|
|
30
|
+
authenticateWithUsernamePassword(authenticateRequest: UsernamePasswordAuthenticateRequest): Promise<BWellTransactionResult<AuthTokens, BaseManagerError>>;
|
|
31
31
|
refreshAccessToken(refreshTokensRequest: RefreshTokensRequest): Promise<BWellTransactionResult<RefreshTokenResults, BaseManagerError>>;
|
|
32
32
|
}
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import { type LoggerProvider } from "../../../logger/index.js";
|
|
2
2
|
import { BWellTransactionResult } from "../../../results/index.js";
|
|
3
3
|
import { BaseManagerError } from "../../base/errors.js";
|
|
4
|
-
import { IdentityManager, UsernamePasswordAuthenticateRequest, type
|
|
4
|
+
import { IdentityManager, UsernamePasswordAuthenticateRequest, type AuthTokens, type OauthAuthenticateRequest, type RefreshTokenResults, type RefreshTokensRequest, type SdkConfigurationResult } from "../../base/identity/index.js";
|
|
5
5
|
import { GraphQLManager } from "../graphql-manager/index.js";
|
|
6
6
|
import type { GraphQLSdk } from "../graphql-sdk/index.js";
|
|
7
7
|
export declare class GraphQLIdentityManager extends GraphQLManager implements IdentityManager {
|
|
8
8
|
#private;
|
|
9
9
|
constructor(sdk: GraphQLSdk, loggerProvider?: LoggerProvider);
|
|
10
10
|
initialize(clientKey: string): Promise<BWellTransactionResult<SdkConfigurationResult, BaseManagerError>>;
|
|
11
|
-
authenticateWithOauth(authenticateRequest: OauthAuthenticateRequest): Promise<BWellTransactionResult<
|
|
12
|
-
authenticateWithUsernamePassword(authenticateRequest: UsernamePasswordAuthenticateRequest): Promise<BWellTransactionResult<
|
|
11
|
+
authenticateWithOauth(authenticateRequest: OauthAuthenticateRequest): Promise<BWellTransactionResult<AuthTokens, BaseManagerError>>;
|
|
12
|
+
authenticateWithUsernamePassword(authenticateRequest: UsernamePasswordAuthenticateRequest): Promise<BWellTransactionResult<AuthTokens, BaseManagerError>>;
|
|
13
13
|
refreshAccessToken(refreshTokensRequest: RefreshTokensRequest): Promise<BWellTransactionResult<RefreshTokenResults, BaseManagerError>>;
|
|
14
14
|
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import type { BaseManagerError } from "../api/base/errors.js";
|
|
2
|
-
import type {
|
|
2
|
+
import type { AuthTokens } from "../api/base/identity/index.js";
|
|
3
3
|
import type { InvalidCredentialsTypeError } from "../errors/index.js";
|
|
4
4
|
import type { BWellTransactionResult } from "../results/index.js";
|
|
5
5
|
import type { Credentials } from "./credentials.js";
|
|
6
6
|
export type AuthenticateErrors = InvalidCredentialsTypeError | BaseManagerError;
|
|
7
7
|
export interface AuthStrategy {
|
|
8
|
-
authenticate(credentials: Credentials): Promise<BWellTransactionResult<
|
|
8
|
+
authenticate(credentials: Credentials): Promise<BWellTransactionResult<AuthTokens, AuthenticateErrors>>;
|
|
9
9
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { AuthTokens, IdentityManager } from "../api/base/identity/index.js";
|
|
2
2
|
import { SdkConfig } from "../config/index.js";
|
|
3
3
|
import { LoggerProvider } from "../logger/index.js";
|
|
4
4
|
import { BWellTransactionResult } from "../results/index.js";
|
|
@@ -7,5 +7,5 @@ import { Credentials } from "./credentials.js";
|
|
|
7
7
|
export declare class OAuthStrategy implements AuthStrategy {
|
|
8
8
|
#private;
|
|
9
9
|
constructor(sdkConfig: SdkConfig, identityManager: IdentityManager, loggerProvider?: LoggerProvider);
|
|
10
|
-
authenticate(credentials: Credentials): Promise<BWellTransactionResult<
|
|
10
|
+
authenticate(credentials: Credentials): Promise<BWellTransactionResult<AuthTokens, AuthenticateErrors>>;
|
|
11
11
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { AuthTokens, IdentityManager } from "../api/base/identity/identity-manager.js";
|
|
2
2
|
import { SdkConfig } from "../config/index.js";
|
|
3
3
|
import { LoggerProvider } from "../logger/index.js";
|
|
4
4
|
import { BWellTransactionResult } from "../results/index.js";
|
|
@@ -8,5 +8,5 @@ import { Credentials } from "./credentials.js";
|
|
|
8
8
|
export declare class UsernamePasswordStrategy implements AuthStrategy {
|
|
9
9
|
#private;
|
|
10
10
|
constructor(sdkConfig: SdkConfig, identityManager: IdentityManager, loggerProvider?: LoggerProvider);
|
|
11
|
-
authenticate(credentials: Credentials): Promise<BWellTransactionResult<
|
|
11
|
+
authenticate(credentials: Credentials): Promise<BWellTransactionResult<AuthTokens, AuthenticateErrors>>;
|
|
12
12
|
}
|
|
@@ -3,12 +3,14 @@ import type { ConnectionManager } from "../api/base/connection/connection-manage
|
|
|
3
3
|
import type { DeviceManager } from "../api/base/device/device-manager.js";
|
|
4
4
|
import type { EventManager } from "../api/base/event/event-manager.js";
|
|
5
5
|
import type { HealthManager } from "../api/base/health-data/health-manager.js";
|
|
6
|
+
import type { AuthTokens } from "../api/base/identity/index.js";
|
|
6
7
|
import type { SearchManager } from "../api/base/search/search-manager.js";
|
|
7
8
|
import type { UserManager } from "../api/base/user/user-manager.js";
|
|
8
9
|
import { type Credentials } from "../auth/index.js";
|
|
9
10
|
import { type BWellConfig } from "../config/index.js";
|
|
10
11
|
import { type AuthenticationError, type InvalidClientKeyError, type InvalidTokenError, type OperationOutcomeError } from "../errors/index.js";
|
|
11
12
|
import { BWellTransactionResult } from "../results/index.js";
|
|
13
|
+
export { AuthTokens } from "../api/base/identity/index.js";
|
|
12
14
|
/**
|
|
13
15
|
* The BWell SDK is the main entry point for the SDK.
|
|
14
16
|
*
|
|
@@ -60,6 +62,17 @@ export declare class BWellSDK {
|
|
|
60
62
|
* ```
|
|
61
63
|
*/
|
|
62
64
|
authenticate(credentials: Credentials): Promise<BWellTransactionResult<null, AuthenticationError | InvalidTokenError>>;
|
|
65
|
+
/**
|
|
66
|
+
* Sets auth tokens for a pre-authenticated user. Performs a token refresh to
|
|
67
|
+
* validate the provided tokens.
|
|
68
|
+
*
|
|
69
|
+
* This can be used in place of `authenticate` for times when a user has been
|
|
70
|
+
* authenticated through means other than the SDK.
|
|
71
|
+
*
|
|
72
|
+
* @param {AuthTokens} authTokens - Auth tokens for pre-authenticated user
|
|
73
|
+
* @returns {Promise<BWellTransactionResult<null, OperationOutcomeError | InvalidTokenError>>}
|
|
74
|
+
*/
|
|
75
|
+
setAuthTokens(authTokens: AuthTokens): Promise<BWellTransactionResult<null, OperationOutcomeError | InvalidTokenError>>;
|
|
63
76
|
get health(): HealthManager;
|
|
64
77
|
get user(): UserManager;
|
|
65
78
|
get connection(): ConnectionManager;
|
|
@@ -18,7 +18,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
18
18
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
19
19
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
20
20
|
};
|
|
21
|
-
var _BWellSDK_instances, _BWellSDK_config, _BWellSDK_sdkConfig, _BWellSDK_telemetry, _BWellSDK_loggerFactory, _BWellSDK_apiProviderFactory, _BWellSDK_identityManagerFactory, _BWellSDK_authStrategyFactory, _BWellSDK_loggerProvider, _BWellSDK_apiProvider, _BWellSDK_logger, _BWellSDK_identityManager, _BWellSDK_configManager, _BWellSDK_tokenManager, _BWellSDK_getApiProvider, _BWellSDK_reinitializeBaseDependencies, _BWellSDK_initLoggerProvider, _BWellSDK_initTelemetry, _BWellSDK_initApiProvider;
|
|
21
|
+
var _BWellSDK_instances, _BWellSDK_config, _BWellSDK_sdkConfig, _BWellSDK_telemetry, _BWellSDK_loggerFactory, _BWellSDK_apiProviderFactory, _BWellSDK_identityManagerFactory, _BWellSDK_authStrategyFactory, _BWellSDK_loggerProvider, _BWellSDK_apiProvider, _BWellSDK_logger, _BWellSDK_identityManager, _BWellSDK_configManager, _BWellSDK_tokenManager, _BWellSDK_bootstrapAuth, _BWellSDK_getApiProvider, _BWellSDK_reinitializeBaseDependencies, _BWellSDK_initLoggerProvider, _BWellSDK_initTelemetry, _BWellSDK_initApiProvider;
|
|
22
22
|
import { ApiProviderFactory } from "../api/api-provider-factory.js";
|
|
23
23
|
import { IdentityManagerFactory } from "../api/identity-manager-factory.js";
|
|
24
24
|
import { AuthStrategyFactory } from "../auth/index.js";
|
|
@@ -123,26 +123,35 @@ export class BWellSDK {
|
|
|
123
123
|
return authenticateResults.intoFailure();
|
|
124
124
|
}
|
|
125
125
|
const authResultsData = authenticateResults.data();
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
126
|
+
return __classPrivateFieldGet(this, _BWellSDK_instances, "m", _BWellSDK_bootstrapAuth).call(this, authResultsData);
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Sets auth tokens for a pre-authenticated user. Performs a token refresh to
|
|
131
|
+
* validate the provided tokens.
|
|
132
|
+
*
|
|
133
|
+
* This can be used in place of `authenticate` for times when a user has been
|
|
134
|
+
* authenticated through means other than the SDK.
|
|
135
|
+
*
|
|
136
|
+
* @param {AuthTokens} authTokens - Auth tokens for pre-authenticated user
|
|
137
|
+
* @returns {Promise<BWellTransactionResult<null, OperationOutcomeError | InvalidTokenError>>}
|
|
138
|
+
*/
|
|
139
|
+
setAuthTokens(authTokens) {
|
|
140
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
141
|
+
const bootsrapResult = yield __classPrivateFieldGet(this, _BWellSDK_instances, "m", _BWellSDK_bootstrapAuth).call(this, authTokens);
|
|
142
|
+
if (bootsrapResult.failure()) {
|
|
143
|
+
return bootsrapResult.intoFailure();
|
|
129
144
|
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
}
|
|
134
|
-
// Its difficult to get this to throw an unexpected error in tests
|
|
145
|
+
if (__classPrivateFieldGet(this, _BWellSDK_tokenManager, "f") === undefined) {
|
|
146
|
+
// Token manager is set by #bootstrapAuth so it shouldn't ever be undefined
|
|
147
|
+
// here. We can't imply that through TS though so we need to presence check
|
|
135
148
|
/* istanbul ignore next */
|
|
136
|
-
throw
|
|
149
|
+
throw new Error("TokenManager not set");
|
|
137
150
|
}
|
|
138
|
-
const
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
return tokenManagerInit.intoFailure();
|
|
151
|
+
const refreshResult = yield __classPrivateFieldGet(this, _BWellSDK_tokenManager, "f").refreshAccessToken();
|
|
152
|
+
if (refreshResult.failure()) {
|
|
153
|
+
return refreshResult.intoFailure();
|
|
142
154
|
}
|
|
143
|
-
__classPrivateFieldSet(this, _BWellSDK_tokenManager, tokenManagerInit.data(), "f");
|
|
144
|
-
__classPrivateFieldGet(this, _BWellSDK_instances, "m", _BWellSDK_reinitializeBaseDependencies).call(this);
|
|
145
|
-
__classPrivateFieldSet(this, _BWellSDK_apiProvider, __classPrivateFieldGet(this, _BWellSDK_instances, "m", _BWellSDK_initApiProvider).call(this, __classPrivateFieldGet(this, _BWellSDK_tokenManager, "f")), "f");
|
|
146
155
|
return BWellTransactionResult.success(null);
|
|
147
156
|
});
|
|
148
157
|
}
|
|
@@ -168,7 +177,31 @@ export class BWellSDK {
|
|
|
168
177
|
return __classPrivateFieldGet(this, _BWellSDK_instances, "m", _BWellSDK_getApiProvider).call(this).search;
|
|
169
178
|
}
|
|
170
179
|
}
|
|
171
|
-
_BWellSDK_config = new WeakMap(), _BWellSDK_sdkConfig = new WeakMap(), _BWellSDK_telemetry = new WeakMap(), _BWellSDK_loggerFactory = new WeakMap(), _BWellSDK_apiProviderFactory = new WeakMap(), _BWellSDK_identityManagerFactory = new WeakMap(), _BWellSDK_authStrategyFactory = new WeakMap(), _BWellSDK_loggerProvider = new WeakMap(), _BWellSDK_apiProvider = new WeakMap(), _BWellSDK_logger = new WeakMap(), _BWellSDK_identityManager = new WeakMap(), _BWellSDK_configManager = new WeakMap(), _BWellSDK_tokenManager = new WeakMap(), _BWellSDK_instances = new WeakSet(),
|
|
180
|
+
_BWellSDK_config = new WeakMap(), _BWellSDK_sdkConfig = new WeakMap(), _BWellSDK_telemetry = new WeakMap(), _BWellSDK_loggerFactory = new WeakMap(), _BWellSDK_apiProviderFactory = new WeakMap(), _BWellSDK_identityManagerFactory = new WeakMap(), _BWellSDK_authStrategyFactory = new WeakMap(), _BWellSDK_loggerProvider = new WeakMap(), _BWellSDK_apiProvider = new WeakMap(), _BWellSDK_logger = new WeakMap(), _BWellSDK_identityManager = new WeakMap(), _BWellSDK_configManager = new WeakMap(), _BWellSDK_tokenManager = new WeakMap(), _BWellSDK_instances = new WeakSet(), _BWellSDK_bootstrapAuth = function _BWellSDK_bootstrapAuth(identityTokens) {
|
|
181
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
182
|
+
try {
|
|
183
|
+
const user = parseUserFromIdToken(identityTokens.idToken);
|
|
184
|
+
__classPrivateFieldGet(this, _BWellSDK_sdkConfig, "f").setContextUser(user);
|
|
185
|
+
}
|
|
186
|
+
catch (e) {
|
|
187
|
+
if (BWellError.isBwellError(e)) {
|
|
188
|
+
return BWellTransactionResult.failure(e);
|
|
189
|
+
}
|
|
190
|
+
// Its difficult to get this to throw an unexpected error in tests
|
|
191
|
+
/* istanbul ignore next */
|
|
192
|
+
throw e;
|
|
193
|
+
}
|
|
194
|
+
const tokenManagerConfig = Object.assign(Object.assign({}, identityTokens), { clientKey: __classPrivateFieldGet(this, _BWellSDK_sdkConfig, "f").context.clientKey });
|
|
195
|
+
const tokenManagerInit = yield JWTTokenManager.initialize(tokenManagerConfig, __classPrivateFieldGet(this, _BWellSDK_identityManager, "f"), __classPrivateFieldGet(this, _BWellSDK_loggerProvider, "f"), __classPrivateFieldGet(this, _BWellSDK_config, "f").tokenStorage);
|
|
196
|
+
if (tokenManagerInit.failure()) {
|
|
197
|
+
return tokenManagerInit.intoFailure();
|
|
198
|
+
}
|
|
199
|
+
__classPrivateFieldSet(this, _BWellSDK_tokenManager, tokenManagerInit.data(), "f");
|
|
200
|
+
__classPrivateFieldGet(this, _BWellSDK_instances, "m", _BWellSDK_reinitializeBaseDependencies).call(this);
|
|
201
|
+
__classPrivateFieldSet(this, _BWellSDK_apiProvider, __classPrivateFieldGet(this, _BWellSDK_instances, "m", _BWellSDK_initApiProvider).call(this, __classPrivateFieldGet(this, _BWellSDK_tokenManager, "f")), "f");
|
|
202
|
+
return BWellTransactionResult.success(null);
|
|
203
|
+
});
|
|
204
|
+
}, _BWellSDK_getApiProvider = function _BWellSDK_getApiProvider() {
|
|
172
205
|
if (__classPrivateFieldGet(this, _BWellSDK_apiProvider, "f") === undefined) {
|
|
173
206
|
throw new Error("Unintialized");
|
|
174
207
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ export * from "./api/index.js";
|
|
|
2
2
|
export * from "./errors/index.js";
|
|
3
3
|
export * from "./requests/index.js";
|
|
4
4
|
export { LogLevel, BWellConfig } from "./config/index.js";
|
|
5
|
-
export { BWellSDK } from "./bwell-sdk/bwell-sdk.js";
|
|
5
|
+
export { BWellSDK, AuthTokens } from "./bwell-sdk/bwell-sdk.js";
|
|
6
6
|
export { TokenStorage, InMemoryTokenStorage } from "./tokens/token-storage.js";
|
|
7
7
|
export { BWellQueryResult } from "./results/bwell-query-result.js";
|
|
8
8
|
export { BWellTransactionResult, BWellTransactionFailure, BWellTransactionSuccess, } from "./results/bwell-transaction-result.js";
|
|
@@ -11,6 +11,7 @@ export type TokenManagerConfig = {
|
|
|
11
11
|
};
|
|
12
12
|
export interface TokenManager {
|
|
13
13
|
getAccessToken(): Promise<BWellTransactionResult<string, OperationOutcomeError | InvalidTokenError>>;
|
|
14
|
+
refreshAccessToken(): Promise<BWellTransactionResult<null, OperationOutcomeError | InvalidTokenError>>;
|
|
14
15
|
getIdToken(): Promise<string | undefined>;
|
|
15
16
|
}
|
|
16
17
|
export declare class JWTTokenManager implements TokenManager {
|
|
@@ -29,4 +30,5 @@ export declare class JWTTokenManager implements TokenManager {
|
|
|
29
30
|
static initialize(config: TokenManagerConfig, identManager: IdentityManager, loggerProvider?: LoggerProvider, tokenStorage?: TokenStorage): Promise<BWellTransactionResult<JWTTokenManager, InvalidTokenError>>;
|
|
30
31
|
getAccessToken(): Promise<BWellTransactionResult<string, OperationOutcomeError | InvalidTokenError>>;
|
|
31
32
|
getIdToken(): Promise<string | undefined>;
|
|
33
|
+
refreshAccessToken(): Promise<BWellTransactionResult<null, OperationOutcomeError | InvalidTokenError>>;
|
|
32
34
|
}
|
|
@@ -87,6 +87,11 @@ export class JWTTokenManager {
|
|
|
87
87
|
return yield __classPrivateFieldGet(this, _JWTTokenManager_tokenStorage, "f").get("idToken");
|
|
88
88
|
});
|
|
89
89
|
}
|
|
90
|
+
refreshAccessToken() {
|
|
91
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
92
|
+
return __classPrivateFieldGet(this, _JWTTokenManager_instances, "m", _JWTTokenManager_refreshAccessToken).call(this);
|
|
93
|
+
});
|
|
94
|
+
}
|
|
90
95
|
}
|
|
91
96
|
_a = JWTTokenManager, _JWTTokenManager_identityManager = new WeakMap(), _JWTTokenManager_logger = new WeakMap(), _JWTTokenManager_tokenStorage = new WeakMap(), _JWTTokenManager_instances = new WeakSet(), _JWTTokenManager_parseTokenExpiration = function _JWTTokenManager_parseTokenExpiration(accessToken) {
|
|
92
97
|
try {
|