@withone/connect 0.12.2 → 0.13.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.
@@ -1,30 +1,22 @@
1
- import { type CompleteAuthorizationInput, type CompleteAuthorizationResult, type OneConnectServerConfig, type OneConnectTokens, type PlatformAction, type ReachableConnection, type RefreshIfExpiringOptions, type RunActionInput, type RunActionResult, type StartAuthorizationInput, type StartAuthorizationResult } from "./types";
1
+ import { type CompleteAuthorizationInput, type CompleteAuthorizationResult, type OneConnectKeyConfig, type OneConnectMode, type OneConnectServerConfig, type OneConnectTokenConfig, type OneConnectTokens, type PlatformAction, type ReachableConnection, type RefreshIfExpiringOptions, type RunActionInput, type RunActionResult, type StartAuthorizationInput, type StartAuthorizationResult } from "./types";
2
2
  export * from "./types";
3
+ export { encodeUserReference, parseUserReference } from "./key";
3
4
  export { refreshTokenExpiresAt, tenancyHeaders, tokenScopes } from "./oauth";
4
- export interface OneConnect {
5
+ /** What an app does with One in either mode. */
6
+ export interface OneConnectClient {
7
+ /** How this client holds each user's grant. */
8
+ readonly mode: OneConnectMode;
5
9
  /** The authorize leg: where to send the browser and the cookie to set. */
6
10
  startAuthorization: (input?: StartAuthorizationInput) => StartAuthorizationResult;
7
- /** The callback leg: verifies state, exchanges the code, stores the
8
- * tokens, and says where to send the browser next. Never throws for
9
- * a failed flow; read `outcome`. */
11
+ /** The callback leg: verifies state, exchanges the code, stores what
12
+ * the mode keeps, and says where to send the browser next. Never
13
+ * throws for a failed flow; read `outcome`. */
10
14
  completeAuthorization: (input: CompleteAuthorizationInput) => Promise<CompleteAuthorizationResult>;
11
- /** Whether the user has tokens stored. */
15
+ /** Whether the app has something stored for the user. In key mode One
16
+ * confirms the consent on each call, so a user who revoked still
17
+ * reads as connected here until a call throws `reconnect_required`. */
12
18
  isConnected: (userId: string) => Promise<boolean>;
13
- /** A live access token, refreshed first when it is about to expire. */
14
- getAccessToken: (userId: string) => Promise<string>;
15
- /** The stored tokens, for display. Null when not connected. */
16
- getTokens: (userId: string) => Promise<OneConnectTokens | null>;
17
- /** Refreshes now and returns the new pair. When another caller rotated
18
- * the pair a moment earlier, returns that pair instead of rotating it
19
- * a second time. */
20
- refreshTokens: (userId: string) => Promise<OneConnectTokens>;
21
- /** Refreshes only when the access token or the refresh token expires
22
- * within `withinMs`, and returns the pair that is current afterwards.
23
- * For background jobs: a frequent run keeps access tokens warm, and a
24
- * daily run with a window of days keeps idle users' 30-day refresh
25
- * tokens from running out. */
26
- refreshIfExpiring: (userId: string, options?: RefreshIfExpiringOptions) => Promise<OneConnectTokens>;
27
- /** Drops the app's copy of the tokens. The user revokes the grant
19
+ /** Drops the app's copy of what it stored. The user revokes the grant
28
20
  * itself from their One dashboard. */
29
21
  disconnect: (userId: string) => Promise<void>;
30
22
  /** The connections the grant reaches, each with its access. */
@@ -37,4 +29,32 @@ export interface OneConnect {
37
29
  /** Any authenticated request to One's /v1 API, headers handled. */
38
30
  fetch: (userId: string, path: string, init?: RequestInit) => Promise<Response>;
39
31
  }
40
- export declare function createOneConnect(config: OneConnectServerConfig): OneConnect;
32
+ /** The client key mode returns. */
33
+ export interface OneConnectKeyClient extends OneConnectClient {
34
+ readonly mode: "key";
35
+ /** One's permanent id for this user (`cu_…`), or null when they have
36
+ * never connected. */
37
+ getConnectUserId: (userId: string) => Promise<string | null>;
38
+ }
39
+ /** The client token mode returns. */
40
+ export interface OneConnect extends OneConnectClient {
41
+ readonly mode: "token";
42
+ /** A live access token, refreshed first when it is about to expire. */
43
+ getAccessToken: (userId: string) => Promise<string>;
44
+ /** The stored tokens, for display. Null when not connected. */
45
+ getTokens: (userId: string) => Promise<OneConnectTokens | null>;
46
+ /** Refreshes now and returns the new pair. When another caller rotated
47
+ * the pair a moment earlier, returns that pair instead of rotating it
48
+ * a second time. */
49
+ refreshTokens: (userId: string) => Promise<OneConnectTokens>;
50
+ /** Refreshes only when the access token or the refresh token expires
51
+ * within `withinMs`, and returns the pair that is current afterwards.
52
+ * For the app's scheduled job: a daily run with a window of days
53
+ * renews each user's pair before the 30-day refresh token runs out. */
54
+ refreshIfExpiring: (userId: string, options?: RefreshIfExpiringOptions) => Promise<OneConnectTokens>;
55
+ }
56
+ /** Key mode: a connect key for the app and one permanent id per user. */
57
+ export declare function createOneConnect(config: OneConnectKeyConfig): OneConnectKeyClient;
58
+ /** Token mode: an access and a refresh token per user, kept fresh. */
59
+ export declare function createOneConnect(config: OneConnectTokenConfig): OneConnect;
60
+ export declare function createOneConnect(config: OneConnectServerConfig): OneConnectKeyClient | OneConnect;