@midsummer-ai/sdk 0.1.0-staging.3608 → 0.1.0-staging.3620

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,155 +1,3 @@
1
1
  import { n as DatabaseErrorCode, t as DatabaseError } from "../errors-x2vXKi0u.mjs";
2
- //#region src/database/client.d.ts
3
- /** A function's reference, as `api.<file>.<export>` holds it, typed by its arguments and its answer. */
4
- interface Reference<Kind extends "query" | "mutation" | "action", Args = any, Returns = any> {
5
- readonly name: string;
6
- readonly kind: Kind;
7
- readonly __args?: Args;
8
- readonly __returns?: Returns;
9
- }
10
- /** The arguments a function's reference takes, and what it returns. */
11
- type ArgsOf<F> = F extends Reference<any, infer A, any> ? A : never;
12
- type ReturnsOf<F> = F extends Reference<any, any, infer R> ? R : never;
13
- /** A function's arguments, which may be left out when it takes none or only optional ones. */
14
- type ArgsFor<F> = {} extends ArgsOf<F> ? [args?: ArgsOf<F>] : [args: ArgsOf<F>];
15
- type Tagged = {
16
- readonly tag?: string;
17
- };
18
- interface Database {
19
- query<F extends Reference<"query">>(fn: F, ...args: ArgsFor<F>): Promise<ReturnsOf<F>>;
20
- /**
21
- * `tag` identifies this mutation for retries: sent again under it, it applies once while the Database
22
- * still keeps the tag, which is for the client's last 64 writes until it has been idle 24 hours. A
23
- * tag is made when not given. Give each mutation a tag of its own, and never rely on one past that.
24
- */
25
- mutation<F extends Reference<"mutation">>(fn: F, ...args: {} extends ArgsOf<F> ? [args?: ArgsOf<F>, options?: Tagged | undefined] : [args: ArgsOf<F>, options?: Tagged | undefined]): Promise<ReturnsOf<F>>;
26
- action<F extends Reference<"action">>(fn: F, ...args: ArgsFor<F>): Promise<ReturnsOf<F>>;
27
- /**
28
- * Gives `onValue` the query's value now and after every change, and `onError` the `DatabaseError`
29
- * when the Database refuses it; returns a stop. A query that returns nothing gives `null`.
30
- */
31
- watch<F extends Reference<"query">>(fn: F, args: ArgsOf<F>, onValue: (value: ReturnsOf<F>) => void, onError?: (error: DatabaseError) => void): () => void;
32
- close(): void;
33
- }
34
- type ConnectOptions = {
35
- /** Where the Database serves, as `midsummer db show` gives it, or the development server's address. */
36
- readonly url: string;
37
- /** A token for this Database. */
38
- readonly token?: string;
39
- /** A new token, asked for before the one held runs out and when it is refused. */
40
- readonly refresh?: () => Promise<string> | string;
41
- } | {
42
- /** A Database Key, `mdbk_…`: exchanged for a token and the Database's address. */
43
- readonly key: string;
44
- /** The platform's API, where a key is exchanged. */
45
- readonly api?: string;
46
- } | {
47
- /** The name the run's Environment binds the Database under: in CI, an Agent's run or `midsummer dev`. */
48
- readonly binding: string;
49
- /** The platform's API; the one the run's Workload token came from when left out. */
50
- readonly api?: string;
51
- } | {
52
- /** `context.database`, the Databases a Deployment's Environment binds. */
53
- readonly database: BoundDatabases;
54
- /** The name the Environment binds the Database under, such as `main`. */
55
- readonly binding: string;
56
- };
57
- /** What Midsummer hands a Deployment as `context.database`: a token and address for a Database its Environment binds. */
58
- interface BoundDatabases {
59
- token(binding: string): Promise<{
60
- readonly token: string;
61
- readonly url: string;
62
- readonly expiresAt: number;
63
- }>;
64
- }
65
- /**
66
- * A token for one signed-in user of your app, for your server to hand to that user's browser, which connects with
67
- * it (`@midsummer-ai/sdk/react`'s `DatabaseProvider`, or `connect({ url, token, refresh })`). Your server keeps the
68
- * database key and identifies the user by your own id for them; your functions see that id as `ctx.auth.subject`. The
69
- * token runs public functions only, and only queries for a `read` key. It lasts ten minutes: the browser asks your
70
- * server again before then.
71
- *
72
- * app.get("/db-token", async (c) => c.json(await userToken({ key: process.env.DATABASE_KEY!, as: session.userId })))
73
- */
74
- declare function userToken(options: {
75
- readonly key: string;
76
- readonly as: string;
77
- readonly api?: string;
78
- }): Promise<{
79
- readonly token: string;
80
- readonly url: string;
81
- readonly expiresAt: number;
82
- }>;
83
- declare function connect(options: ConnectOptions): Promise<Database>;
84
- /**
85
- * What `setAuth` is given: the signed-in user's token from their sign-in provider, or null when no one is
86
- * signed in. `forceRefreshToken` asks for a new one, never one held: the last is about to expire, or was refused.
87
- */
88
- type FetchToken = (options: {
89
- readonly forceRefreshToken: boolean;
90
- }) => Promise<string | null | undefined>;
91
- /** Where a client's sign-in stands: loading until the Database has taken or refused the token it was given. */
92
- interface AuthState {
93
- readonly isLoading: boolean;
94
- readonly isAuthenticated: boolean;
95
- }
96
- /**
97
- * A Database for an app whose users sign in with their own provider: made at once (`new DatabaseClient(url)`),
98
- * connected on first use or on `setAuth`, and connected again, its watches with it, whenever the sign-in
99
- * changes. A token the Database refuses (or a `fetchToken` that throws) leaves the client connected as no one,
100
- * and `onChange(false)` tells the app. The client keeps one connection; `close()` ends it.
101
- */
102
- declare class DatabaseClient implements Database {
103
- #private;
104
- /** `url`: where the Database serves, as `midsummer db show` gives it, or the development server's address. */
105
- constructor(url: string);
106
- /** Signs in with the tokens `fetchToken` gives, now and whenever one is about to expire; `onChange` hears whether the Database took them. */
107
- setAuth(fetchToken: FetchToken, onChange?: (isAuthenticated: boolean, error?: unknown) => void): void;
108
- /** Signs out: the client connects again as no one. */
109
- clearAuth(): void;
110
- /** Whether the client is signed in, and whether that is still being decided. */
111
- get authState(): AuthState;
112
- /** Hears every change of `authState`; returns a stop. */
113
- onAuthChange(listener: (state: AuthState) => void): () => void;
114
- query<F extends Reference<"query">>(fn: F, args: ArgsOf<F>): Promise<ReturnsOf<F>>;
115
- mutation<F extends Reference<"mutation">>(fn: F, args: ArgsOf<F>, options?: {
116
- readonly tag?: string;
117
- }): Promise<ReturnsOf<F>>;
118
- action<F extends Reference<"action">>(fn: F, args: ArgsOf<F>): Promise<ReturnsOf<F>>;
119
- /** As `Database.watch`; it goes on across sign-ins, given the value the new caller sees. `onError` hears a connection that could not be made. */
120
- watch<F extends Reference<"query">>(fn: F, args: ArgsOf<F>, onValue: (value: ReturnsOf<F>) => void, onError?: (error: DatabaseError) => void): () => void;
121
- close(): void;
122
- }
123
- /**
124
- * Auth0: the signed-in user's ID token, whose `aud` is your application's client ID, the `applicationID`
125
- * `db/auth.config.ts` names beside your tenant's `domain`. Give it auth0-react's `getAccessTokenSilently`, or
126
- * auth0-spa-js's `client.getTokenSilently.bind(client)`.
127
- *
128
- * db.setAuth(auth0(getAccessTokenSilently))
129
- */
130
- declare function auth0(getTokenSilently: (options: {
131
- detailedResponse: true;
132
- cacheMode: "on" | "off";
133
- }) => Promise<unknown>): FetchToken;
134
- /**
135
- * Clerk: a token of the JWT template `template`, which sets `aud` to the `applicationID` `db/auth.config.ts`
136
- * names beside your Clerk issuer (its Frontend API address). Give it clerk-react's `useAuth().getToken`, or
137
- * clerk-js's `clerk.session.getToken`.
138
- *
139
- * db.setAuth(clerk(getToken, { template: "midsummer" }))
140
- */
141
- declare function clerk(getToken: (options: {
142
- template?: string;
143
- skipCache?: boolean;
144
- }) => Promise<string | null>, options?: {
145
- readonly template?: string;
146
- }): FetchToken;
147
- /**
148
- * Any OIDC sign-in: `getToken` returns the user's ID token (a new one when `forceRefresh`), or null when no one
149
- * is signed in. With oidc-client-ts:
150
- *
151
- * db.setAuth(oidc(async (fresh) => (fresh ? await manager.signinSilent() : await manager.getUser())?.id_token))
152
- */
153
- declare function oidc(getToken: (forceRefresh: boolean) => Promise<string | null | undefined> | string | null | undefined): FetchToken;
154
- //#endregion
2
+ import { a as Database, c as Reference, d as clerk, f as connect, i as ConnectOptions, l as ReturnsOf, m as userToken, n as AuthState, o as DatabaseClient, p as oidc, r as BoundDatabases, s as FetchToken, t as ArgsOf, u as auth0 } from "../client-BshDU1b2.mjs";
155
3
  export { ArgsOf, AuthState, BoundDatabases, ConnectOptions, Database, DatabaseClient, DatabaseError, type DatabaseErrorCode, FetchToken, Reference, ReturnsOf, auth0, clerk, connect, oidc, userToken };