@midsummer-ai/sdk 0.1.0-next.900 → 0.1.0-staging.3100

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 (40) hide show
  1. package/auth-1W-SzpD3.mjs +441 -0
  2. package/build-DQo-JrlN.d.mts +120 -0
  3. package/bytes-C2pcyl9u.mjs +280 -0
  4. package/{connection-CDk7H1FL.mjs → connection-Cu2GSOaR.mjs} +171 -31
  5. package/database/build.d.mts +2 -2
  6. package/database/build.mjs +1404 -117
  7. package/database/client.d.mts +85 -9
  8. package/database/client.mjs +550 -32
  9. package/database/contract.d.mts +92 -2
  10. package/database/contract.mjs +2179 -3
  11. package/database/errors.d.mts +1 -22
  12. package/database/react.d.mts +133 -0
  13. package/database/react.mjs +488 -0
  14. package/database/server.d.mts +2 -3
  15. package/database/server.mjs +3 -320
  16. package/database/stand-in.d.mts +95 -9
  17. package/database/stand-in.mjs +1436 -116
  18. package/errors-x2vXKi0u.d.mts +25 -0
  19. package/index-DNNXNny7.d.mts +410 -0
  20. package/key-BrRdFY-n.mjs +11 -0
  21. package/package.json +114 -1
  22. package/schema-CTHQMRsW.mjs +217 -0
  23. package/server-B5HK2Ou9.d.mts +574 -0
  24. package/server-EDRbF2u-.mjs +1893 -0
  25. package/{store-BzI9at5x.mjs → store-lIZ5GxVv.mjs} +1 -1
  26. package/{tabs-D-P8_ci_.mjs → tabs-zCh8kceo.mjs} +1 -1
  27. package/vector/contract.d.mts +1 -1
  28. package/vector/contract.mjs +773 -1
  29. package/vector/index.d.mts +2 -2
  30. package/vector/index.mjs +71 -3
  31. package/vector/stand-in.d.mts +1 -1
  32. package/vector/stand-in.mjs +326 -23
  33. package/vector/wire.d.mts +11 -2
  34. package/vector/wire.mjs +179 -14
  35. package/build-SQ44jwSa.d.mts +0 -41
  36. package/index-DdoK0y7D.d.mts +0 -192
  37. package/schema-BF-Y1EVu.mjs +0 -142
  38. package/schema-DS8x1Dn8.mjs +0 -115
  39. package/schema-f7E9uzUn.d.mts +0 -74
  40. package/server-C7O7CICU.d.mts +0 -182
@@ -1,4 +1,4 @@
1
- import { DatabaseError, DatabaseErrorCode } from "./errors.mjs";
1
+ import { n as DatabaseErrorCode, t as DatabaseError } from "../errors-x2vXKi0u.mjs";
2
2
  //#region src/database/client.d.ts
3
3
  /** A function's reference, as `api.<file>.<export>` holds it, typed by its arguments and its answer. */
4
4
  interface Reference<Kind extends "query" | "mutation" | "action", Args = any, Returns = any> {
@@ -9,19 +9,25 @@ interface Reference<Kind extends "query" | "mutation" | "action", Args = any, Re
9
9
  }
10
10
  type ArgsOf<F> = F extends Reference<any, infer A, any> ? A : never;
11
11
  type ReturnsOf<F> = F extends Reference<any, any, infer R> ? R : never;
12
+ /** A function's arguments, which may be left out when it takes none or only optional ones. */
13
+ type ArgsFor<F> = {} extends ArgsOf<F> ? [args?: ArgsOf<F>] : [args: ArgsOf<F>];
14
+ type Tagged = {
15
+ readonly tag?: string;
16
+ };
12
17
  interface Database {
13
- query<F extends Reference<"query">>(fn: F, args: ArgsOf<F>): Promise<ReturnsOf<F>>;
18
+ query<F extends Reference<"query">>(fn: F, ...args: ArgsFor<F>): Promise<ReturnsOf<F>>;
14
19
  /**
15
20
  * `tag` identifies this mutation for retries: sent again under it, it applies once while the Database
16
21
  * still keeps the tag, which is for the client's last 64 writes until it has been idle 24 hours. A
17
22
  * tag is made when not given. Give each mutation a tag of its own, and never rely on one past that.
18
23
  */
19
- mutation<F extends Reference<"mutation">>(fn: F, args: ArgsOf<F>, options?: {
20
- readonly tag?: string;
21
- }): Promise<ReturnsOf<F>>;
22
- action<F extends Reference<"action">>(fn: F, args: ArgsOf<F>): Promise<ReturnsOf<F>>;
23
- /** Gives `onValue` the query's value now and after every change; returns a stop. */
24
- watch<F extends Reference<"query">>(fn: F, args: ArgsOf<F>, onValue: (value: ReturnsOf<F>) => void): () => void;
24
+ 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>>;
25
+ action<F extends Reference<"action">>(fn: F, ...args: ArgsFor<F>): Promise<ReturnsOf<F>>;
26
+ /**
27
+ * Gives `onValue` the query's value now and after every change, and `onError` the `DatabaseError`
28
+ * when the Database refuses it; returns a stop. A query that returns nothing gives `null`.
29
+ */
30
+ watch<F extends Reference<"query">>(fn: F, args: ArgsOf<F>, onValue: (value: ReturnsOf<F>) => void, onError?: (error: DatabaseError) => void): () => void;
25
31
  close(): void;
26
32
  }
27
33
  type ConnectOptions = {
@@ -56,5 +62,75 @@ interface BoundDatabases {
56
62
  }>;
57
63
  }
58
64
  declare function connect(options: ConnectOptions): Promise<Database>;
65
+ /**
66
+ * What `setAuth` is given: the signed-in user's token from their sign-in provider, or null when no one is
67
+ * signed in. `forceRefreshToken` asks for a new one, never one held: the last is about to expire, or was refused.
68
+ */
69
+ type FetchToken = (options: {
70
+ readonly forceRefreshToken: boolean;
71
+ }) => Promise<string | null | undefined>;
72
+ /** Where a client's sign-in stands: loading until the Database has taken or refused the token it was given. */
73
+ interface AuthState {
74
+ readonly isLoading: boolean;
75
+ readonly isAuthenticated: boolean;
76
+ }
77
+ /**
78
+ * A Database for an app whose users sign in with their own provider: made at once (`new DatabaseClient(url)`),
79
+ * connected on first use or on `setAuth`, and connected again, its watches with it, whenever the sign-in
80
+ * changes. A token the Database refuses (or a `fetchToken` that throws) leaves the client connected as no one,
81
+ * and `onChange(false)` tells the app. The client keeps one connection; `close()` ends it.
82
+ */
83
+ declare class DatabaseClient implements Database {
84
+ #private;
85
+ /** `url`: where the Database serves, as `midsummer db show` gives it, or the development server's address. */
86
+ constructor(url: string);
87
+ /** Signs in with the tokens `fetchToken` gives, now and whenever one is about to expire; `onChange` hears whether the Database took them. */
88
+ setAuth(fetchToken: FetchToken, onChange?: (isAuthenticated: boolean, error?: unknown) => void): void;
89
+ /** Signs out: the client connects again as no one. */
90
+ clearAuth(): void;
91
+ /** Whether the client is signed in, and whether that is still being decided. */
92
+ get authState(): AuthState;
93
+ /** Hears every change of `authState`; returns a stop. */
94
+ onAuthChange(listener: (state: AuthState) => void): () => void;
95
+ query<F extends Reference<"query">>(fn: F, args: ArgsOf<F>): Promise<ReturnsOf<F>>;
96
+ mutation<F extends Reference<"mutation">>(fn: F, args: ArgsOf<F>, options?: {
97
+ readonly tag?: string;
98
+ }): Promise<ReturnsOf<F>>;
99
+ action<F extends Reference<"action">>(fn: F, args: ArgsOf<F>): Promise<ReturnsOf<F>>;
100
+ /** 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. */
101
+ watch<F extends Reference<"query">>(fn: F, args: ArgsOf<F>, onValue: (value: ReturnsOf<F>) => void, onError?: (error: DatabaseError) => void): () => void;
102
+ close(): void;
103
+ }
104
+ /**
105
+ * Auth0: the signed-in user's ID token, whose `aud` is your application's client ID, the `applicationID`
106
+ * `db/auth.config.ts` names beside your tenant's `domain`. Give it auth0-react's `getAccessTokenSilently`, or
107
+ * auth0-spa-js's `client.getTokenSilently.bind(client)`.
108
+ *
109
+ * db.setAuth(auth0(getAccessTokenSilently))
110
+ */
111
+ declare function auth0(getTokenSilently: (options: {
112
+ detailedResponse: true;
113
+ cacheMode: "on" | "off";
114
+ }) => Promise<unknown>): FetchToken;
115
+ /**
116
+ * Clerk: a token of the JWT template `template`, which sets `aud` to the `applicationID` `db/auth.config.ts`
117
+ * names beside your Clerk issuer (its Frontend API address). Give it clerk-react's `useAuth().getToken`, or
118
+ * clerk-js's `clerk.session.getToken`.
119
+ *
120
+ * db.setAuth(clerk(getToken, { template: "midsummer" }))
121
+ */
122
+ declare function clerk(getToken: (options: {
123
+ template?: string;
124
+ skipCache?: boolean;
125
+ }) => Promise<string | null>, options?: {
126
+ readonly template?: string;
127
+ }): FetchToken;
128
+ /**
129
+ * Any OIDC sign-in: `getToken` returns the user's ID token (a new one when `forceRefresh`), or null when no one
130
+ * is signed in. With oidc-client-ts:
131
+ *
132
+ * db.setAuth(oidc(async (fresh) => (fresh ? await manager.signinSilent() : await manager.getUser())?.id_token))
133
+ */
134
+ declare function oidc(getToken: (forceRefresh: boolean) => Promise<string | null | undefined> | string | null | undefined): FetchToken;
59
135
  //#endregion
60
- export { BoundDatabases, ConnectOptions, Database, DatabaseError, type DatabaseErrorCode, Reference, connect };
136
+ export { AuthState, BoundDatabases, ConnectOptions, Database, DatabaseClient, DatabaseError, type DatabaseErrorCode, FetchToken, Reference, auth0, clerk, connect, oidc };