@icanbwell/bwell-sdk-ts 0.3.2 → 0.4.0-rc.1724688661

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.
@@ -1,4 +1,4 @@
1
1
  /**
2
2
  * This file is automatically generated. Please do not edit this file directly.
3
3
  */
4
- export declare const VERSION = "0.3.2";
4
+ export declare const VERSION = "0.4.0-rc.1724688661";
@@ -1,4 +1,4 @@
1
1
  /**
2
2
  * This file is automatically generated. Please do not edit this file directly.
3
3
  */
4
- export const VERSION = "0.3.2";
4
+ export const VERSION = "0.4.0-rc.1724688661";
@@ -16,7 +16,7 @@ export type RefreshTokensRequest = {
16
16
  refreshToken: string;
17
17
  clientKey: string;
18
18
  };
19
- export type AuthenticateResults = {
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<AuthenticateResults, BaseManagerError>>;
30
- authenticateWithUsernamePassword(authenticateRequest: UsernamePasswordAuthenticateRequest): Promise<BWellTransactionResult<AuthenticateResults, BaseManagerError>>;
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 AuthenticateResults, type OauthAuthenticateRequest, type RefreshTokenResults, type RefreshTokensRequest, type SdkConfigurationResult } from "../../base/identity/index.js";
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<AuthenticateResults, BaseManagerError>>;
12
- authenticateWithUsernamePassword(authenticateRequest: UsernamePasswordAuthenticateRequest): Promise<BWellTransactionResult<AuthenticateResults, BaseManagerError>>;
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 { AuthenticateResults } from "../api/base/identity/index.js";
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<AuthenticateResults, AuthenticateErrors>>;
8
+ authenticate(credentials: Credentials): Promise<BWellTransactionResult<AuthTokens, AuthenticateErrors>>;
9
9
  }
@@ -1,4 +1,4 @@
1
- import { AuthenticateResults, IdentityManager } from "../api/base/identity/index.js";
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<AuthenticateResults, AuthenticateErrors>>;
10
+ authenticate(credentials: Credentials): Promise<BWellTransactionResult<AuthTokens, AuthenticateErrors>>;
11
11
  }
@@ -1,4 +1,4 @@
1
- import { AuthenticateResults, IdentityManager } from "../api/base/identity/identity-manager.js";
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<AuthenticateResults, AuthenticateErrors>>;
11
+ authenticate(credentials: Credentials): Promise<BWellTransactionResult<AuthTokens, AuthenticateErrors>>;
12
12
  }
@@ -3,20 +3,24 @@ 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
  *
15
- * It is responsible for initializing the SDK, authenticating the user, and providing access to all of the SDK's functionality via the manager classes (see
16
- [Interfaces](../README.md#interfaces)).
17
+ * It is responsible for initializing the SDK, authenticating the user, and providing
18
+ * access to all of the SDK's functionality via the manager classes (see [Interfaces](../README.md#interfaces)).
17
19
  *
18
20
  * @example
19
- * Simple initialization with oauth authentication and only required config properties. Note that the SDK must be initialized and authenticated for most data access methods to work properly.
21
+ * Simple initialization with oauth authentication and only required config properties.
22
+ * Note that the SDK must be initialized and authenticated for most data accessx
23
+ * methods to work properly.
20
24
  * ```typescript
21
25
  * const sdk = new BWellSDK({
22
26
  * clientKey: "YOUR_CLIENT_KEY",
@@ -60,6 +64,17 @@ export declare class BWellSDK {
60
64
  * ```
61
65
  */
62
66
  authenticate(credentials: Credentials): Promise<BWellTransactionResult<null, AuthenticationError | InvalidTokenError>>;
67
+ /**
68
+ * Sets auth tokens for a pre-authenticated user. Performs a token refresh to
69
+ * validate the provided tokens.
70
+ *
71
+ * This can be used in place of `authenticate` for times when a user has been
72
+ * authenticated through means other than the SDK.
73
+ *
74
+ * @param {AuthTokens} authTokens - Auth tokens for pre-authenticated user
75
+ * @returns {Promise<BWellTransactionResult<null, OperationOutcomeError | InvalidTokenError>>}
76
+ */
77
+ setAuthTokens(authTokens: AuthTokens): Promise<BWellTransactionResult<null, OperationOutcomeError | InvalidTokenError>>;
63
78
  get health(): HealthManager;
64
79
  get user(): UserManager;
65
80
  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";
@@ -32,11 +32,13 @@ import { parseUserFromIdToken } from "../utils/index.js";
32
32
  /**
33
33
  * The BWell SDK is the main entry point for the SDK.
34
34
  *
35
- * It is responsible for initializing the SDK, authenticating the user, and providing access to all of the SDK's functionality via the manager classes (see
36
- [Interfaces](../README.md#interfaces)).
35
+ * It is responsible for initializing the SDK, authenticating the user, and providing
36
+ * access to all of the SDK's functionality via the manager classes (see [Interfaces](../README.md#interfaces)).
37
37
  *
38
38
  * @example
39
- * Simple initialization with oauth authentication and only required config properties. Note that the SDK must be initialized and authenticated for most data access methods to work properly.
39
+ * Simple initialization with oauth authentication and only required config properties.
40
+ * Note that the SDK must be initialized and authenticated for most data accessx
41
+ * methods to work properly.
40
42
  * ```typescript
41
43
  * const sdk = new BWellSDK({
42
44
  * clientKey: "YOUR_CLIENT_KEY",
@@ -123,26 +125,35 @@ export class BWellSDK {
123
125
  return authenticateResults.intoFailure();
124
126
  }
125
127
  const authResultsData = authenticateResults.data();
126
- try {
127
- const user = parseUserFromIdToken(authResultsData.idToken);
128
- __classPrivateFieldGet(this, _BWellSDK_sdkConfig, "f").setContextUser(user);
128
+ return __classPrivateFieldGet(this, _BWellSDK_instances, "m", _BWellSDK_bootstrapAuth).call(this, authResultsData);
129
+ });
130
+ }
131
+ /**
132
+ * Sets auth tokens for a pre-authenticated user. Performs a token refresh to
133
+ * validate the provided tokens.
134
+ *
135
+ * This can be used in place of `authenticate` for times when a user has been
136
+ * authenticated through means other than the SDK.
137
+ *
138
+ * @param {AuthTokens} authTokens - Auth tokens for pre-authenticated user
139
+ * @returns {Promise<BWellTransactionResult<null, OperationOutcomeError | InvalidTokenError>>}
140
+ */
141
+ setAuthTokens(authTokens) {
142
+ return __awaiter(this, void 0, void 0, function* () {
143
+ const bootsrapResult = yield __classPrivateFieldGet(this, _BWellSDK_instances, "m", _BWellSDK_bootstrapAuth).call(this, authTokens);
144
+ if (bootsrapResult.failure()) {
145
+ return bootsrapResult.intoFailure();
129
146
  }
130
- catch (e) {
131
- if (BWellError.isBwellError(e)) {
132
- return BWellTransactionResult.failure(e);
133
- }
134
- // Its difficult to get this to throw an unexpected error in tests
147
+ if (__classPrivateFieldGet(this, _BWellSDK_tokenManager, "f") === undefined) {
148
+ // Token manager is set by #bootstrapAuth so it shouldn't ever be undefined
149
+ // here. We can't imply that through TS though so we need to presence check
135
150
  /* istanbul ignore next */
136
- throw e;
151
+ throw new Error("TokenManager not set");
137
152
  }
138
- const tokenManagerConfig = Object.assign(Object.assign({}, authResultsData), { clientKey: __classPrivateFieldGet(this, _BWellSDK_sdkConfig, "f").context.clientKey });
139
- const tokenManagerInit = yield JWTTokenManager.initialize(tokenManagerConfig, __classPrivateFieldGet(this, _BWellSDK_identityManager, "f"), __classPrivateFieldGet(this, _BWellSDK_loggerProvider, "f"), __classPrivateFieldGet(this, _BWellSDK_config, "f").tokenStorage);
140
- if (tokenManagerInit.failure()) {
141
- return tokenManagerInit.intoFailure();
153
+ const refreshResult = yield __classPrivateFieldGet(this, _BWellSDK_tokenManager, "f").refreshAccessToken();
154
+ if (refreshResult.failure()) {
155
+ return refreshResult.intoFailure();
142
156
  }
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
157
  return BWellTransactionResult.success(null);
147
158
  });
148
159
  }
@@ -168,7 +179,31 @@ export class BWellSDK {
168
179
  return __classPrivateFieldGet(this, _BWellSDK_instances, "m", _BWellSDK_getApiProvider).call(this).search;
169
180
  }
170
181
  }
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(), _BWellSDK_getApiProvider = function _BWellSDK_getApiProvider() {
182
+ _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) {
183
+ return __awaiter(this, void 0, void 0, function* () {
184
+ try {
185
+ const user = parseUserFromIdToken(identityTokens.idToken);
186
+ __classPrivateFieldGet(this, _BWellSDK_sdkConfig, "f").setContextUser(user);
187
+ }
188
+ catch (e) {
189
+ if (BWellError.isBwellError(e)) {
190
+ return BWellTransactionResult.failure(e);
191
+ }
192
+ // Its difficult to get this to throw an unexpected error in tests
193
+ /* istanbul ignore next */
194
+ throw e;
195
+ }
196
+ const tokenManagerConfig = Object.assign(Object.assign({}, identityTokens), { clientKey: __classPrivateFieldGet(this, _BWellSDK_sdkConfig, "f").context.clientKey });
197
+ const tokenManagerInit = yield JWTTokenManager.initialize(tokenManagerConfig, __classPrivateFieldGet(this, _BWellSDK_identityManager, "f"), __classPrivateFieldGet(this, _BWellSDK_loggerProvider, "f"), __classPrivateFieldGet(this, _BWellSDK_config, "f").tokenStorage);
198
+ if (tokenManagerInit.failure()) {
199
+ return tokenManagerInit.intoFailure();
200
+ }
201
+ __classPrivateFieldSet(this, _BWellSDK_tokenManager, tokenManagerInit.data(), "f");
202
+ __classPrivateFieldGet(this, _BWellSDK_instances, "m", _BWellSDK_reinitializeBaseDependencies).call(this);
203
+ __classPrivateFieldSet(this, _BWellSDK_apiProvider, __classPrivateFieldGet(this, _BWellSDK_instances, "m", _BWellSDK_initApiProvider).call(this, __classPrivateFieldGet(this, _BWellSDK_tokenManager, "f")), "f");
204
+ return BWellTransactionResult.success(null);
205
+ });
206
+ }, _BWellSDK_getApiProvider = function _BWellSDK_getApiProvider() {
172
207
  if (__classPrivateFieldGet(this, _BWellSDK_apiProvider, "f") === undefined) {
173
208
  throw new Error("Unintialized");
174
209
  }
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 {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@icanbwell/bwell-sdk-ts",
3
- "version": "0.3.2",
3
+ "version": "0.4.0-rc.1724688661",
4
4
  "description": "b.well TypeScript SDK",
5
5
  "license": "UNLICENSED",
6
6
  "type": "module",