@andco/sdk 0.0.1

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.
Files changed (63) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +176 -0
  3. package/dist/auth.d.ts +71 -0
  4. package/dist/auth.d.ts.map +1 -0
  5. package/dist/auth.js +98 -0
  6. package/dist/browser/controller.d.ts +76 -0
  7. package/dist/browser/controller.d.ts.map +1 -0
  8. package/dist/browser/controller.js +215 -0
  9. package/dist/browser/frame.d.ts +65 -0
  10. package/dist/browser/frame.d.ts.map +1 -0
  11. package/dist/browser/frame.js +237 -0
  12. package/dist/browser/index.d.ts +52 -0
  13. package/dist/browser/index.d.ts.map +1 -0
  14. package/dist/browser/index.js +117 -0
  15. package/dist/browser/popup.d.ts +105 -0
  16. package/dist/browser/popup.d.ts.map +1 -0
  17. package/dist/browser/popup.js +177 -0
  18. package/dist/cli/index.d.ts +47 -0
  19. package/dist/cli/index.d.ts.map +1 -0
  20. package/dist/cli/index.js +72 -0
  21. package/dist/cli/server.d.ts +11 -0
  22. package/dist/cli/server.d.ts.map +1 -0
  23. package/dist/cli/server.js +91 -0
  24. package/dist/client.d.ts +135 -0
  25. package/dist/client.d.ts.map +1 -0
  26. package/dist/client.js +210 -0
  27. package/dist/config.d.ts +82 -0
  28. package/dist/config.d.ts.map +1 -0
  29. package/dist/config.js +109 -0
  30. package/dist/credentials.d.ts +76 -0
  31. package/dist/credentials.d.ts.map +1 -0
  32. package/dist/credentials.js +0 -0
  33. package/dist/errors.d.ts +165 -0
  34. package/dist/errors.d.ts.map +1 -0
  35. package/dist/errors.js +197 -0
  36. package/dist/index.d.ts +13 -0
  37. package/dist/index.d.ts.map +1 -0
  38. package/dist/index.js +11 -0
  39. package/dist/intents.d.ts +89 -0
  40. package/dist/intents.d.ts.map +1 -0
  41. package/dist/intents.js +157 -0
  42. package/dist/oauth.d.ts +147 -0
  43. package/dist/oauth.d.ts.map +1 -0
  44. package/dist/oauth.js +348 -0
  45. package/dist/presenter.d.ts +36 -0
  46. package/dist/presenter.d.ts.map +1 -0
  47. package/dist/presenter.js +1 -0
  48. package/dist/rest.d.ts +58 -0
  49. package/dist/rest.d.ts.map +1 -0
  50. package/dist/rest.js +48 -0
  51. package/dist/server/index.d.ts +21 -0
  52. package/dist/server/index.d.ts.map +1 -0
  53. package/dist/server/index.js +27 -0
  54. package/dist/server-metadata.generated.d.ts +4 -0
  55. package/dist/server-metadata.generated.d.ts.map +1 -0
  56. package/dist/server-metadata.generated.js +49 -0
  57. package/dist/session-store.d.ts +83 -0
  58. package/dist/session-store.d.ts.map +1 -0
  59. package/dist/session-store.js +186 -0
  60. package/dist/storage.d.ts +61 -0
  61. package/dist/storage.d.ts.map +1 -0
  62. package/dist/storage.js +47 -0
  63. package/package.json +56 -0
package/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright 2026 Andco
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,176 @@
1
+ # `@andco/sdk`
2
+
3
+ The core AndCo SDK: one client shape (`AndcoClient`) with a builder per runtime — browser,
4
+ server, and CLI. There is no separate `@andco/sdk-js` package; this package is the universal
5
+ one, and its `./browser`, `./server`, and `./cli` subpaths are how it adapts to each runtime.
6
+
7
+ ## Install
8
+
9
+ ```sh
10
+ pnpm add @andco/sdk
11
+ ```
12
+
13
+ ## Runtime contract
14
+
15
+ `@andco/sdk` targets any runtime with Web-standard `fetch`, `Request`, `Response`, `URL`, and
16
+ Web Crypto. Supply the ones a runtime lacks through `globals`:
17
+
18
+ ```ts
19
+ import { AndcoClient } from "@andco/sdk";
20
+
21
+ const andco = new AndcoClient({
22
+ clientId,
23
+ globals: { fetch: myFetch, crypto: myRandomSource },
24
+ });
25
+ ```
26
+
27
+ ## Creating an instance
28
+
29
+ Use the builder for your runtime, not `AndcoClient` directly, unless you have a reason to:
30
+
31
+ ```ts
32
+ // Browser
33
+ import { createAndcoInstanceForBrowser } from "@andco/sdk/browser";
34
+
35
+ const andco = createAndcoInstanceForBrowser({
36
+ clientId: "your-public-oauth-client-id",
37
+ redirectTo: "https://app.example.com/auth/callback",
38
+ initialScopes: ["openid", "email", "profile"],
39
+ });
40
+ ```
41
+
42
+ ```ts
43
+ // Server — holds no signed-in user; credentials arrive per request through `with(...)`.
44
+ import { createAndcoInstanceForServer } from "@andco/sdk/server";
45
+
46
+ export const andco = createAndcoInstanceForServer({
47
+ clientId: process.env.ANDCO_CLIENT_ID!,
48
+ clientSecret: process.env.ANDCO_CLIENT_SECRET!,
49
+ });
50
+ ```
51
+
52
+ ```ts
53
+ // CLI — presents the authorization from a terminal via a loopback receiver.
54
+ import { createAndcoInstanceForCLI } from "@andco/sdk/cli";
55
+
56
+ const andco = createAndcoInstanceForCLI({
57
+ clientId,
58
+ storage: keychain,
59
+ redirectTo: "http://127.0.0.1:0/callback",
60
+ });
61
+ const { data: session } = await andco.auth.signIn();
62
+ ```
63
+
64
+ Every builder returns an `AndcoClient`. There is no `AndcoSessionClient` — that type was removed;
65
+ `AndcoClient` (unauthenticated arm) and `AndcoClientAuthed` (authenticated arm) are the only two
66
+ client shapes now.
67
+
68
+ ## Public, no credentials involved
69
+
70
+ An `AndcoClient` works before any session exists — exactly what a public resource, such as the
71
+ permission catalog, expects:
72
+
73
+ ```ts
74
+ const { data, error } = await andco.rest.http.GET("/catalog/permissions");
75
+ if (error) throw error;
76
+ ```
77
+
78
+ ## Binding credentials
79
+
80
+ Two ways to get an `AndcoClientAuthed`:
81
+
82
+ - `andco.with(credentials)` binds *explicit* credentials for one unit of work. It accepts
83
+ `AndcoCredentials`, an `AndcoSession`, or a bare access-token string — the bare string exists
84
+ for the common server case where a handler already pulled a token out of its own session:
85
+
86
+ ```ts
87
+ const bound = andco.with(await accessTokenFromCookie(request.headers));
88
+ const { data } = await bound.rest.http.GET("/accounts");
89
+ ```
90
+
91
+ - `andco.authorized()` returns the instance's own session, seen as authorized — a fresh reference
92
+ every call, sharing every collaborator (`rest`, `intents`, `auth`, `session`) with the instance
93
+ by reference. This is the seam a framework adapter reaches for in the browser:
94
+
95
+ ```ts
96
+ const andcoView = session ? instance.authorized() : instance;
97
+ ```
98
+
99
+ `AndcoCredentials` is the entire contract the SDK needs for authorization:
100
+
101
+ ```ts
102
+ export interface AndcoCredentials {
103
+ accessTokenFor(resource: string): Promise<string | null>;
104
+ }
105
+ ```
106
+
107
+ Any object with that one method works — a Better Auth account, a Passport session, a row in your
108
+ own table, or a plain closure — without adopting the SDK's own persistence.
109
+
110
+ ## Resource Server Definitions
111
+
112
+ `AndcoBoundClient` was removed. In its place, a Resource Server Definition is any object that
113
+ implements:
114
+
115
+ ```ts
116
+ interface AndcoResourceServer<Client> {
117
+ readonly id: string;
118
+ readonly resource: string;
119
+ use(credentials: AndcoCredentials): Client;
120
+ }
121
+ ```
122
+
123
+ `use()` takes `AndcoCredentials`, not a client, which is what keeps the extension point
124
+ one-sided: a third-party Resource Server binds against one method and needs no knowledge of
125
+ Andco's own client shape or transport. `andco.with(session)` and `andco.authorized()` both
126
+ satisfy `AndcoCredentials` and can be passed straight to `use(...)`.
127
+
128
+ ## Intents
129
+
130
+ `andco.intents` is domain-neutral: it only knows how to create, read, execute, and observe an
131
+ Intent generically. It does not know about deposits, withdrawals, or any other domain concept —
132
+ those belong to whatever Resource Server Definition owns that domain, composed over this same
133
+ surface (see [Resource Server Definitions](#resource-server-definitions) above).
134
+
135
+ ```ts
136
+ const { data: intent, error } = await andco.intents.create("deposit", depositInput, {
137
+ idempotencyKey: "deposit:8472",
138
+ });
139
+ if (error) throw error;
140
+ ```
141
+
142
+ - `create(type, input, options?)` — creates an Intent of the given type; `type` is sent as the
143
+ body's own `type`, so the input never repeats it, and the response's `type` is checked against it.
144
+ Resolves to `Result<AndcoIntent>` — the Intent union of Andco's own contract — unless a
145
+ third-party Resource Server Definition names its own response type.
146
+ - `get(intentId)` — reads the authoritative state of one Intent, as `Result<AndcoIntent>` by the
147
+ same rule.
148
+ - `execute(intentId)` — executes an Intent whose Grant permits it.
149
+ - `events(intentId, after?, signal?)` — reads one page of durable Intent facts.
150
+ - `subscribe(subject, read, decode, handler, options)` — a durable, paged subscription loop, with
151
+ its own checkpoint and poll-interval reconciliation. Returns an unsubscribe function.
152
+
153
+ Present the resulting intent with `@andco/sdk-react`, `@andco/sdk-vue`, `@andco/sdk-svelte`, or
154
+ `@andco/sdk-script`. The browser must retrieve the authoritative intent state after presentation;
155
+ callback parameters are correlation data, not financial state.
156
+
157
+ ## Exports
158
+
159
+ - `@andco/sdk` — `AndcoClient`, `AndcoAuth`, `AndcoConfig`, credential types and helpers,
160
+ `AndcoIntents`, `AndcoOAuth`, `AndcoRest`, `AndcoSessionStore`, storage adapters, and error
161
+ types. See `packages/sdk/src/index.ts` for the exhaustive list.
162
+ - `@andco/sdk/browser` — `createAndcoInstanceForBrowser`, `browserPresenter`, the popup/iframe
163
+ bridge, and the Andco Button controller.
164
+ - `@andco/sdk/server` — `createAndcoInstanceForServer`.
165
+ - `@andco/sdk/cli` — `createAndcoInstanceForCLI`, `AndcoCliPresenter`.
166
+
167
+ There is intentionally no wildcard subpath export. This prevents internal files and runtime-only
168
+ implementations from becoming accidental public API.
169
+
170
+ ## Security checklist
171
+
172
+ - Never place confidential OAuth client secrets in browser code.
173
+ - Use a distinct OAuth client for each environment.
174
+ - Request the smallest scopes needed by the current operation.
175
+ - Preserve idempotency keys across retries of the same financial operation.
176
+ - Treat all returned identity, account, and profile fields as personal data.
package/dist/auth.d.ts ADDED
@@ -0,0 +1,71 @@
1
+ import { type AndCoRandomSource } from "@andco/protocol";
2
+ import type { AndcoSession } from "./credentials.js";
3
+ import { Result } from "./errors.js";
4
+ import type { AndcoAuthorizationOptions, AndcoAuthorizationRequest, AndcoOAuth } from "./oauth.js";
5
+ import type { AndcoPresentation, AndcoPresenter } from "./presenter.js";
6
+ import type { AndcoSessionStore } from "./session-store.js";
7
+ export type AndcoSignInOptions = AndcoAuthorizationOptions & {
8
+ presentation?: AndcoPresentation;
9
+ };
10
+ export type AndcoAuthOptions = {
11
+ oauth: AndcoOAuth;
12
+ store: AndcoSessionStore;
13
+ presenter: AndcoPresenter;
14
+ /** Generates the Presentation ID. Injected so a test can make one deterministic. */
15
+ randomId?: () => string;
16
+ /** Random source for the Presentation ID, threaded from the client's `globals.crypto`. */
17
+ crypto?: AndCoRandomSource;
18
+ /**
19
+ * Persists a transaction before it is presented.
20
+ *
21
+ * A redirect presentation replaces the document, so the PKCE verifier has to survive somewhere
22
+ * the next page load can find it. Without this, redirect sign-in returns with a code and nothing
23
+ * to exchange it against.
24
+ */
25
+ persistTransaction?: (request: AndcoAuthorizationRequest) => void | Promise<void>;
26
+ };
27
+ /**
28
+ * Sign-in for a runtime that owns exactly one session.
29
+ *
30
+ * This exists only where a presenter and a store are both present, which is why a server instance
31
+ * does not have it: a server holds no session of its own and binds one per request instead.
32
+ *
33
+ * Every step is delegated. The protocol is the OAuth client's, the presentation is the presenter's,
34
+ * and persistence is the store's. This class only orders them.
35
+ *
36
+ * @example
37
+ * ```ts
38
+ * const { data: session, error } = await andco.auth.signIn({
39
+ * presentation: "popup",
40
+ * authorizations: [bank.authorization()],
41
+ * });
42
+ * ```
43
+ */
44
+ export declare class AndcoAuth {
45
+ #private;
46
+ constructor(options: AndcoAuthOptions);
47
+ /**
48
+ * Runs one authorization end to end.
49
+ *
50
+ * A redirect presentation replaces the document, so it answers `null` rather than a session: the
51
+ * result arrives on the next page load, through the store's own callback consumption.
52
+ */
53
+ signIn(options?: AndcoSignInOptions): Promise<Result<AndcoSession | null>>;
54
+ /** Forgets the local session. Revoking the Grant at Andco is a separate, server-side concern. */
55
+ signOut(): Promise<Result<void>>;
56
+ /**
57
+ * Observes session changes. The return value is the unsubscribe, so binding this to a framework
58
+ * effect is a single line in React, Vue, and Svelte alike.
59
+ *
60
+ * @example
61
+ * ```ts
62
+ * useEffect(() => andco.auth.onChange(setSession), [andco]);
63
+ * ```
64
+ */
65
+ onChange(listener: (session: AndcoSession | null) => void): () => void;
66
+ /** The current session, waiting for the initial load if it has not finished. */
67
+ getSession(): Promise<Result<AndcoSession | null>>;
68
+ /** Refreshes now rather than waiting for a token request to find the session stale. */
69
+ refresh(): Promise<Result<AndcoSession>>;
70
+ }
71
+ //# sourceMappingURL=auth.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../src/auth.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,iBAAiB,EAAe,MAAM,iBAAiB,CAAC;AACtE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AACrD,OAAO,EAAqB,MAAM,EAAE,MAAM,aAAa,CAAC;AACxD,OAAO,KAAK,EAAE,yBAAyB,EAAE,yBAAyB,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AACnG,OAAO,KAAK,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AACxE,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AAE5D,MAAM,MAAM,kBAAkB,GAAG,yBAAyB,GAAG;IAC3D,YAAY,CAAC,EAAE,iBAAiB,CAAC;CAClC,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG;IAC7B,KAAK,EAAE,UAAU,CAAC;IAClB,KAAK,EAAE,iBAAiB,CAAC;IACzB,SAAS,EAAE,cAAc,CAAC;IAC1B,oFAAoF;IACpF,QAAQ,CAAC,EAAE,MAAM,MAAM,CAAC;IACxB,0FAA0F;IAC1F,MAAM,CAAC,EAAE,iBAAiB,CAAC;IAC3B;;;;;;OAMG;IACH,kBAAkB,CAAC,EAAE,CAAC,OAAO,EAAE,yBAAyB,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACnF,CAAC;AAEF;;;;;;;;;;;;;;;;GAgBG;AACH,qBAAa,SAAS;;gBAGD,OAAO,EAAE,gBAAgB;IAI5C;;;;;OAKG;IACU,MAAM,CAAC,OAAO,GAAE,kBAAuB,GAAG,OAAO,CAAC,MAAM,CAAC,YAAY,GAAG,IAAI,CAAC,CAAC;IA4B3F,iGAAiG;IACpF,OAAO,IAAI,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAI7C;;;;;;;;OAQG;IACI,QAAQ,CAAC,QAAQ,EAAE,CAAC,OAAO,EAAE,YAAY,GAAG,IAAI,KAAK,IAAI,GAAG,MAAM,IAAI;IAK7E,gFAAgF;IACnE,UAAU,IAAI,OAAO,CAAC,MAAM,CAAC,YAAY,GAAG,IAAI,CAAC,CAAC;IAO/D,uFAAuF;IAC1E,OAAO,IAAI,OAAO,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC;CAWtD"}
package/dist/auth.js ADDED
@@ -0,0 +1,98 @@
1
+ import { RANDOM_UUID } from "@andco/protocol";
2
+ import { ANDCO_ERROR_CODES, Result } from "./errors.js";
3
+ /**
4
+ * Sign-in for a runtime that owns exactly one session.
5
+ *
6
+ * This exists only where a presenter and a store are both present, which is why a server instance
7
+ * does not have it: a server holds no session of its own and binds one per request instead.
8
+ *
9
+ * Every step is delegated. The protocol is the OAuth client's, the presentation is the presenter's,
10
+ * and persistence is the store's. This class only orders them.
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * const { data: session, error } = await andco.auth.signIn({
15
+ * presentation: "popup",
16
+ * authorizations: [bank.authorization()],
17
+ * });
18
+ * ```
19
+ */
20
+ export class AndcoAuth {
21
+ #options;
22
+ constructor(options) {
23
+ this.#options = options;
24
+ }
25
+ /**
26
+ * Runs one authorization end to end.
27
+ *
28
+ * A redirect presentation replaces the document, so it answers `null` rather than a session: the
29
+ * result arrives on the next page load, through the store's own callback consumption.
30
+ */
31
+ async signIn(options = {}) {
32
+ const { oauth, presenter, store } = this.#options;
33
+ const presentationId = this.#options.randomId ? this.#options.randomId() : RANDOM_UUID(this.#options.crypto);
34
+ const prepared = await oauth.createAuthorizationRequest(options);
35
+ if (prepared.error)
36
+ return Result.fail(prepared.error);
37
+ await this.#options.persistTransaction?.(prepared.data);
38
+ const returnTo = new URL(prepared.data.redirectTo);
39
+ const presented = await presenter.present({
40
+ url: prepared.data.authorizationUrl,
41
+ presentation: options.presentation ?? "popup",
42
+ returnTo,
43
+ presentationId,
44
+ });
45
+ if (presented.error)
46
+ return Result.fail(presented.error);
47
+ // Either the document was replaced, or the user dismissed. Neither is an error.
48
+ if (!presented.data)
49
+ return Result.ok(null);
50
+ const exchanged = await oauth.exchangeCallback({ callbackUrl: presented.data, request: prepared.data });
51
+ if (exchanged.error)
52
+ return Result.fail(exchanged.error);
53
+ const written = await store.set(exchanged.data);
54
+ if (written.error)
55
+ return Result.fail(written.error);
56
+ return Result.ok(exchanged.data);
57
+ }
58
+ /** Forgets the local session. Revoking the Grant at Andco is a separate, server-side concern. */
59
+ async signOut() {
60
+ return this.#options.store.set(null);
61
+ }
62
+ /**
63
+ * Observes session changes. The return value is the unsubscribe, so binding this to a framework
64
+ * effect is a single line in React, Vue, and Svelte alike.
65
+ *
66
+ * @example
67
+ * ```ts
68
+ * useEffect(() => andco.auth.onChange(setSession), [andco]);
69
+ * ```
70
+ */
71
+ onChange(listener) {
72
+ const store = this.#options.store;
73
+ return store.subscribe(() => listener(store.getSnapshot() ?? null));
74
+ }
75
+ /** The current session, waiting for the initial load if it has not finished. */
76
+ async getSession() {
77
+ const store = this.#options.store;
78
+ const loaded = await store.ready;
79
+ if (loaded.error)
80
+ return Result.fail(loaded.error);
81
+ return Result.ok(store.getSnapshot() ?? null);
82
+ }
83
+ /** Refreshes now rather than waiting for a token request to find the session stale. */
84
+ async refresh() {
85
+ const store = this.#options.store;
86
+ await store.ready;
87
+ const current = store.getSnapshot();
88
+ if (!current?.refreshToken)
89
+ return Result.fail(ANDCO_ERROR_CODES.SESSION_MISSING);
90
+ const refreshed = await this.#options.oauth.refresh(current.refreshToken);
91
+ if (refreshed.error)
92
+ return Result.fail(refreshed.error);
93
+ const written = await store.set(refreshed.data);
94
+ if (written.error)
95
+ return Result.fail(written.error);
96
+ return Result.ok(refreshed.data);
97
+ }
98
+ }
@@ -0,0 +1,76 @@
1
+ import type { AndCoClickEvent, AndCoErrorEvent, AndCoReadyEvent, AndCoThemeMode } from "@andco/protocol";
2
+ import type { AndcoClient } from "../client.js";
3
+ import { AndcoError } from "../errors.js";
4
+ import type { AndcoAuthorizationOptions } from "../oauth.js";
5
+ import type { AndcoPresentation } from "../presenter.js";
6
+ /** The hosted workflow an iframe presents. */
7
+ export type AndcoButtonFlow = "authorization" | "intent";
8
+ /** Observable lifecycle of one hosted Andco surface. */
9
+ export type AndcoButtonStatus = "idle" | "connecting" | "ready" | "authorizing" | "complete" | "dismissed" | "error";
10
+ export type AndcoButtonSnapshot = {
11
+ status: AndcoButtonStatus;
12
+ iframeURL: string;
13
+ ready: boolean;
14
+ error: AndcoError | null;
15
+ };
16
+ export type AndcoButtonControllerOptions = {
17
+ /** The instance whose configuration, protocol, and session this surface acts on. */
18
+ client: AndcoClient;
19
+ flow?: AndcoButtonFlow;
20
+ /** Widget release to load. Defaults to the release the SDK was built against. */
21
+ release?: number;
22
+ /** Changes while the application runs, unlike endpoints and versions. */
23
+ themeMode?: AndCoThemeMode;
24
+ locale?: string;
25
+ disabled?: boolean;
26
+ busy?: boolean;
27
+ /** How the surface presents its authorization. The hosted control needs to know before it opens. */
28
+ presentation?: AndcoPresentation;
29
+ /** Request-local authorization options, applied when the user activates the surface. */
30
+ authorization?: AndcoAuthorizationOptions;
31
+ onReady?: (event: AndCoReadyEvent) => void;
32
+ onActivate?: (event: AndCoClickEvent) => void;
33
+ onComplete?: () => void;
34
+ onDismiss?: () => void;
35
+ onError?: (error: AndcoError, event?: AndCoErrorEvent) => void;
36
+ };
37
+ type Listener = (snapshot: AndcoButtonSnapshot) => void;
38
+ /**
39
+ * Coordinates one hosted Andco iframe without owning how a framework renders it.
40
+ *
41
+ * The iframe is Andco-deployed and cross-origin, which is what the Hosted Intent Button buys: the
42
+ * visible control's DOM and code are not the Project's, and the popup handle never enters Project
43
+ * JavaScript. It is not an authorization boundary and does not protect Project tokens.
44
+ *
45
+ * Render `snapshot.iframeURL` in your platform's own iframe element, then hand that element to
46
+ * `connect()` once it mounts.
47
+ *
48
+ * @example
49
+ * ```ts
50
+ * const controller = new AndcoButtonController({ client: andco, onComplete: reload });
51
+ * const stop = controller.subscribe((snapshot) => setState(snapshot));
52
+ * const disconnect = controller.connect(iframeElement);
53
+ * ```
54
+ */
55
+ export declare class AndcoButtonController {
56
+ #private;
57
+ constructor(options: AndcoButtonControllerOptions);
58
+ get snapshot(): AndcoButtonSnapshot;
59
+ /** Observes lifecycle changes. The return value is the unsubscribe. */
60
+ subscribe(listener: Listener): () => void;
61
+ /** Attaches a mounted iframe. The return value disconnects it. */
62
+ connect(iframe: HTMLIFrameElement): () => void;
63
+ disconnect(): void;
64
+ /**
65
+ * Returns the surface to its ready state so it can be activated again.
66
+ *
67
+ * A bridge that ended in `"error"` — a timed-out or failed handshake — has already settled
68
+ * permanently and silently ignores every message after that, including a late `"ready"`. Resetting
69
+ * the status without rebuilding it would show a working button over a bridge that can never
70
+ * recover, so this reconnects against the last-connected iframe instead.
71
+ */
72
+ restart(): void;
73
+ destroy(): void;
74
+ }
75
+ export {};
76
+ //# sourceMappingURL=controller.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"controller.d.ts","sourceRoot":"","sources":["../../src/browser/controller.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,eAAe,EACf,eAAe,EAIf,eAAe,EACf,cAAc,EACf,MAAM,iBAAiB,CAAC;AAEzB,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAChD,OAAO,EAAqB,UAAU,EAAE,MAAM,cAAc,CAAC;AAC7D,OAAO,KAAK,EAAE,yBAAyB,EAA6B,MAAM,aAAa,CAAC;AACxF,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAGzD,8CAA8C;AAC9C,MAAM,MAAM,eAAe,GAAG,eAAe,GAAG,QAAQ,CAAC;AAEzD,wDAAwD;AACxD,MAAM,MAAM,iBAAiB,GAAG,MAAM,GAAG,YAAY,GAAG,OAAO,GAAG,aAAa,GAAG,UAAU,GAAG,WAAW,GAAG,OAAO,CAAC;AAErH,MAAM,MAAM,mBAAmB,GAAG;IAChC,MAAM,EAAE,iBAAiB,CAAC;IAC1B,SAAS,EAAE,MAAM,CAAC;IAClB,KAAK,EAAE,OAAO,CAAC;IACf,KAAK,EAAE,UAAU,GAAG,IAAI,CAAC;CAC1B,CAAC;AAEF,MAAM,MAAM,4BAA4B,GAAG;IACzC,oFAAoF;IACpF,MAAM,EAAE,WAAW,CAAC;IACpB,IAAI,CAAC,EAAE,eAAe,CAAC;IACvB,iFAAiF;IACjF,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,yEAAyE;IACzE,SAAS,CAAC,EAAE,cAAc,CAAC;IAC3B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,oGAAoG;IACpG,YAAY,CAAC,EAAE,iBAAiB,CAAC;IACjC,wFAAwF;IACxF,aAAa,CAAC,EAAE,yBAAyB,CAAC;IAC1C,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,eAAe,KAAK,IAAI,CAAC;IAC3C,UAAU,CAAC,EAAE,CAAC,KAAK,EAAE,eAAe,KAAK,IAAI,CAAC;IAC9C,UAAU,CAAC,EAAE,MAAM,IAAI,CAAC;IACxB,SAAS,CAAC,EAAE,MAAM,IAAI,CAAC;IACvB,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,UAAU,EAAE,KAAK,CAAC,EAAE,eAAe,KAAK,IAAI,CAAC;CAChE,CAAC;AAEF,KAAK,QAAQ,GAAG,CAAC,QAAQ,EAAE,mBAAmB,KAAK,IAAI,CAAC;AAExD;;;;;;;;;;;;;;;;GAgBG;AACH,qBAAa,qBAAqB;;gBAcb,OAAO,EAAE,4BAA4B;IAKxD,IAAW,QAAQ,IAAI,mBAAmB,CAEzC;IAED,uEAAuE;IAChE,SAAS,CAAC,QAAQ,EAAE,QAAQ,GAAG,MAAM,IAAI;IAQhD,kEAAkE;IAC3D,OAAO,CAAC,MAAM,EAAE,iBAAiB,GAAG,MAAM,IAAI;IAiD9C,UAAU,IAAI,IAAI;IAKzB;;;;;;;OAOG;IACI,OAAO,IAAI,IAAI;IAgBf,OAAO,IAAI,IAAI;CA8FvB"}