@authoric/js 0.0.0-stage → 0.1.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.
- package/LICENSE +21 -0
- package/README.md +94 -3
- package/bin/authoric.mjs +345 -0
- package/dist/cjs/app-actions.js +40 -0
- package/dist/cjs/app-actions.js.map +1 -0
- package/dist/cjs/auth.js +335 -0
- package/dist/cjs/auth.js.map +1 -0
- package/dist/cjs/client.js +121 -0
- package/dist/cjs/client.js.map +1 -0
- package/dist/cjs/cookies.js +137 -0
- package/dist/cjs/cookies.js.map +1 -0
- package/dist/cjs/data.js +197 -0
- package/dist/cjs/data.js.map +1 -0
- package/dist/cjs/errors.js +70 -0
- package/dist/cjs/errors.js.map +1 -0
- package/dist/cjs/files.js +188 -0
- package/dist/cjs/files.js.map +1 -0
- package/dist/cjs/generated/error-codes.js +435 -0
- package/dist/cjs/generated/error-codes.js.map +1 -0
- package/dist/cjs/index.js +31 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/realtime.js +191 -0
- package/dist/cjs/realtime.js.map +1 -0
- package/dist/cjs/routes.js +113 -0
- package/dist/cjs/routes.js.map +1 -0
- package/dist/cjs/session.js +430 -0
- package/dist/cjs/session.js.map +1 -0
- package/dist/cjs/storage-adapters.js +83 -0
- package/dist/cjs/storage-adapters.js.map +1 -0
- package/dist/cjs/teams.js +152 -0
- package/dist/cjs/teams.js.map +1 -0
- package/dist/cjs/transport.js +189 -0
- package/dist/cjs/transport.js.map +1 -0
- package/dist/cjs/types.js +7 -0
- package/dist/cjs/types.js.map +1 -0
- package/dist/esm/app-actions.d.ts +32 -0
- package/dist/esm/app-actions.d.ts.map +1 -0
- package/dist/esm/app-actions.js +37 -0
- package/dist/esm/app-actions.js.map +1 -0
- package/dist/esm/auth.d.ts +259 -0
- package/dist/esm/auth.d.ts.map +1 -0
- package/dist/esm/auth.js +331 -0
- package/dist/esm/auth.js.map +1 -0
- package/dist/esm/client.d.ts +82 -0
- package/dist/esm/client.d.ts.map +1 -0
- package/dist/esm/client.js +115 -0
- package/dist/esm/client.js.map +1 -0
- package/dist/esm/cookies.d.ts +66 -0
- package/dist/esm/cookies.d.ts.map +1 -0
- package/dist/esm/cookies.js +129 -0
- package/dist/esm/cookies.js.map +1 -0
- package/dist/esm/data.d.ts +211 -0
- package/dist/esm/data.d.ts.map +1 -0
- package/dist/esm/data.js +191 -0
- package/dist/esm/data.js.map +1 -0
- package/dist/esm/errors.d.ts +61 -0
- package/dist/esm/errors.d.ts.map +1 -0
- package/dist/esm/errors.js +63 -0
- package/dist/esm/errors.js.map +1 -0
- package/dist/esm/files.d.ts +76 -0
- package/dist/esm/files.d.ts.map +1 -0
- package/dist/esm/files.js +184 -0
- package/dist/esm/files.js.map +1 -0
- package/dist/esm/generated/error-codes.d.ts +5 -0
- package/dist/esm/generated/error-codes.d.ts.map +1 -0
- package/dist/esm/generated/error-codes.js +432 -0
- package/dist/esm/generated/error-codes.js.map +1 -0
- package/dist/esm/index.d.ts +22 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +9 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/realtime.d.ts +33 -0
- package/dist/esm/realtime.d.ts.map +1 -0
- package/dist/esm/realtime.js +186 -0
- package/dist/esm/realtime.js.map +1 -0
- package/dist/esm/routes.d.ts +75 -0
- package/dist/esm/routes.d.ts.map +1 -0
- package/dist/esm/routes.js +108 -0
- package/dist/esm/routes.js.map +1 -0
- package/dist/esm/session.d.ts +99 -0
- package/dist/esm/session.d.ts.map +1 -0
- package/dist/esm/session.js +425 -0
- package/dist/esm/session.js.map +1 -0
- package/dist/esm/storage-adapters.d.ts +31 -0
- package/dist/esm/storage-adapters.d.ts.map +1 -0
- package/dist/esm/storage-adapters.js +76 -0
- package/dist/esm/storage-adapters.js.map +1 -0
- package/dist/esm/teams.d.ts +157 -0
- package/dist/esm/teams.d.ts.map +1 -0
- package/dist/esm/teams.js +146 -0
- package/dist/esm/teams.js.map +1 -0
- package/dist/esm/transport.d.ts +56 -0
- package/dist/esm/transport.d.ts.map +1 -0
- package/dist/esm/transport.js +185 -0
- package/dist/esm/transport.js.map +1 -0
- package/dist/esm/types.d.ts +203 -0
- package/dist/esm/types.d.ts.map +1 -0
- package/dist/esm/types.js +6 -0
- package/dist/esm/types.js.map +1 -0
- package/package.json +54 -6
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { AuthClient } from "./auth.js";
|
|
2
|
+
import { ActionClient, TableClient, type GenericTable } from "./data.js";
|
|
3
|
+
import { FilesClient } from "./files.js";
|
|
4
|
+
import { type BroadcastChannelLike } from "./session.js";
|
|
5
|
+
import { type StorageOption } from "./storage-adapters.js";
|
|
6
|
+
import { type DebugEvent } from "./transport.js";
|
|
7
|
+
import { type CookieMethods, type SessionCookieSettings } from "./cookies.js";
|
|
8
|
+
import { BillingClient, OrgsClient, RolesClient } from "./teams.js";
|
|
9
|
+
declare const SERVER_MODE: unique symbol;
|
|
10
|
+
/**
|
|
11
|
+
* The shape a generated types file describes (`npx @authoric/js types`). Without one, every table is
|
|
12
|
+
* `Record<string, unknown>`.
|
|
13
|
+
*/
|
|
14
|
+
export interface GenericDatabase {
|
|
15
|
+
tables: Record<string, GenericTable>;
|
|
16
|
+
}
|
|
17
|
+
export interface ClientOptions {
|
|
18
|
+
/** The API's public address, such as `https://api.authoric.ai`. https, except on localhost. */
|
|
19
|
+
url: string;
|
|
20
|
+
/** Your app's uid (the console shows it on the app's page). Not a secret. */
|
|
21
|
+
appUid: string;
|
|
22
|
+
/**
|
|
23
|
+
* Where the signed-in session is kept between page loads: "local" (the default in a browser),
|
|
24
|
+
* "session", "memory" (the default elsewhere), or your own adapter.
|
|
25
|
+
*/
|
|
26
|
+
storage?: StorageOption;
|
|
27
|
+
/** A fetch implementation (tests, or a runtime without a global one). */
|
|
28
|
+
fetch?: typeof fetch;
|
|
29
|
+
/** The longest the SDK waits on a server's Retry-After before throwing instead. Default 60 seconds. */
|
|
30
|
+
maxRetryWaitMs?: number;
|
|
31
|
+
/**
|
|
32
|
+
* Log each request: method, route template, status, code, duration, request id. Never a header,
|
|
33
|
+
* a body, a token or a query value. `true` logs to the console.
|
|
34
|
+
*/
|
|
35
|
+
debug?: boolean | ((event: DebugEvent) => void);
|
|
36
|
+
/** Cross-tab coordination channel; null turns it off. Defaults to a BroadcastChannel in browsers. */
|
|
37
|
+
broadcastChannel?: BroadcastChannelLike | null;
|
|
38
|
+
}
|
|
39
|
+
export declare class AuthoricClient<Database extends GenericDatabase = GenericDatabase> {
|
|
40
|
+
readonly auth: AuthClient;
|
|
41
|
+
readonly storage: FilesClient;
|
|
42
|
+
/** The user's organizations, their members and invitations. */
|
|
43
|
+
readonly orgs: OrgsClient;
|
|
44
|
+
/** Roles in the user's org. */
|
|
45
|
+
readonly roles: RolesClient;
|
|
46
|
+
/** Buying the app's products. */
|
|
47
|
+
readonly billing: BillingClient;
|
|
48
|
+
readonly appUid: string;
|
|
49
|
+
readonly url: string;
|
|
50
|
+
private readonly transport;
|
|
51
|
+
private readonly session;
|
|
52
|
+
constructor(options: ClientOptions & {
|
|
53
|
+
[SERVER_MODE]?: true;
|
|
54
|
+
});
|
|
55
|
+
/** A table's rows, typed by your generated types file when you pass one to `createClient`. */
|
|
56
|
+
from<Name extends Extract<keyof Database["tables"], string>>(table: Name): TableClient<Database["tables"][Name], Database["tables"]>;
|
|
57
|
+
/** A custom endpoint action on a table, such as `action("invoices", "/approve")`. */
|
|
58
|
+
action<Input = Record<string, unknown>, Output = unknown>(table: string, path: string, options?: {
|
|
59
|
+
method?: "GET" | "POST" | "PATCH" | "DELETE";
|
|
60
|
+
}): ActionClient<Input, Output>;
|
|
61
|
+
/** Stop cross-tab listening. For tests and short-lived clients; a page's client lives as long as the page. */
|
|
62
|
+
close(): void;
|
|
63
|
+
}
|
|
64
|
+
/** Create a client for one app. */
|
|
65
|
+
export declare const createClient: <Database extends GenericDatabase = GenericDatabase>(options: ClientOptions) => AuthoricClient<Database>;
|
|
66
|
+
export interface ServerClientOptions extends Omit<ClientOptions, "storage" | "broadcastChannel"> {
|
|
67
|
+
/** This request's cookies: how your framework gets, sets and removes one. */
|
|
68
|
+
cookies: CookieMethods;
|
|
69
|
+
/** Overrides for the session cookie (httpOnly, secure, sameSite, path, domain, maxAge). */
|
|
70
|
+
cookie?: SessionCookieSettings;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* A client for YOUR server, keeping the user's session in a cookie on your own domain so a page your
|
|
74
|
+
* server renders knows who is asking. Create one PER REQUEST, from that request's cookies: a client
|
|
75
|
+
* shared across requests would hand one user's session to the next.
|
|
76
|
+
*
|
|
77
|
+
* What the cookie remembers is not proof of who the user is (a cookie is input the user can edit):
|
|
78
|
+
* call `auth.getUser()`, which asks the API, before you trust it.
|
|
79
|
+
*/
|
|
80
|
+
export declare const createServerClient: <Database extends GenericDatabase = GenericDatabase>(options: ServerClientOptions) => AuthoricClient<Database>;
|
|
81
|
+
export {};
|
|
82
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACvC,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,KAAK,YAAY,EAAE,MAAM,WAAW,CAAC;AAEzE,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AACzC,OAAO,EAAgC,KAAK,oBAAoB,EAAE,MAAM,cAAc,CAAC;AACvF,OAAO,EAAkB,KAAK,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAC3E,OAAO,EAAa,KAAK,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAE5D,OAAO,EAAiB,KAAK,aAAa,EAAE,KAAK,qBAAqB,EAAE,MAAM,cAAc,CAAC;AAC7F,OAAO,EAAE,aAAa,EAAE,UAAU,EA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C9F,IAAI,CAAC,IAAI,SAAS,OAAO,CAAC,MAAM,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC,EAC1D,KAAK,EAAE,IAAI,GACT,WAAW,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC,IAAI,CAAC,EAAE,QAAQ,CAAC,QAAQ,CAAC,CAAC;IAI5D,qFAAqF;IACrF,MAAM,CAAC,KAAK,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,OAAO,EACvD,KAAK,EAAE,MAAM,EACb,IAAI,EAAE,MAAM,EACZ,OAAO,GAAE;QAAE,MAAM,CAAC,EAAE,KAAK,GAAG,MAAM,GAAG,OAAO,GAAG,QAAQ,CAAA;KAAO,GAC5D,YAAY,CAAC,KAAK,EAAE,MAAM,CAAC;IAI9B,8GAA8G;IAC9G,KAAK;CAGL;AAED,mCAAmC;AACnC,eAAO,MAAM,YAAY,GAAI,QAAQ,SAAS,eAAe,GAAG,eAAe,EAAE,SAAS,aAAa,6BACjE,CAAC;AAEvC,MAAM,WAAW,mBAAoB,SAAQ,IAAI,CAAC,aAAa,EAAE,SAAS,GAAG,kBAAkB,CAAC;IAC/F,6EAA6E;IAC7E,OAAO,EAAE,aAAa,CAAC;IACvB,2FAA2F;IAC3F,MAAM,CAAC,EAAE,qBAAqB,CAAC;CAC/B;AAED;;;;;;;GAOG;AACH,eAAO,MAAM,kBAAkB,GAAI,QAAQ,SAAS,eAAe,GAAG,eAAe,EAAE,SAAS,mBAAmB,6BAUlH,CAAC"}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { AuthClient } from "./auth.js";
|
|
2
|
+
import { ActionClient, TableClient } from "./data.js";
|
|
3
|
+
import { clientError } from "./errors.js";
|
|
4
|
+
import { FilesClient } from "./files.js";
|
|
5
|
+
import { SECRET_SHAPE, SessionManager } from "./session.js";
|
|
6
|
+
import { resolveStorage } from "./storage-adapters.js";
|
|
7
|
+
import { Transport } from "./transport.js";
|
|
8
|
+
import { ROUTES } from "./routes.js";
|
|
9
|
+
import { cookieStorage } from "./cookies.js";
|
|
10
|
+
import { BillingClient, OrgsClient, RolesClient } from "./teams.js";
|
|
11
|
+
// Server mode (createServerClient) is reached only through that function: the flag is a symbol this
|
|
12
|
+
// module never exports, so no option a browser caller can pass makes the SDK store an access token.
|
|
13
|
+
const SERVER_MODE = Symbol("authoric.serverMode");
|
|
14
|
+
// A uid, of any version: the console's own isUid rule. Nothing checks the version (root CLAUDE.md).
|
|
15
|
+
const UID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
16
|
+
const normalizeUrl = (value) => {
|
|
17
|
+
let parsed;
|
|
18
|
+
try {
|
|
19
|
+
parsed = new URL(value);
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
throw clientError("AUTHORIC_INVALID_CONFIG", "`url` must be the API's full address, such as https://api.authoric.ai.");
|
|
23
|
+
}
|
|
24
|
+
const local = parsed.hostname === "localhost" || parsed.hostname === "127.0.0.1" || parsed.hostname === "[::1]";
|
|
25
|
+
if (parsed.protocol !== "https:" && !(local && parsed.protocol === "http:")) {
|
|
26
|
+
throw clientError("AUTHORIC_INVALID_CONFIG", "`url` must use https: tokens never travel in the clear.");
|
|
27
|
+
}
|
|
28
|
+
if (parsed.username || parsed.password) {
|
|
29
|
+
throw clientError("AUTHORIC_INVALID_CONFIG", "`url` must not carry credentials.");
|
|
30
|
+
}
|
|
31
|
+
if (parsed.search || parsed.hash) {
|
|
32
|
+
throw clientError("AUTHORIC_INVALID_CONFIG", "`url` is the API's address alone, with no query or fragment.");
|
|
33
|
+
}
|
|
34
|
+
return `${parsed.origin}${parsed.pathname.replace(/\/+$/, "")}`;
|
|
35
|
+
};
|
|
36
|
+
export class AuthoricClient {
|
|
37
|
+
constructor(options) {
|
|
38
|
+
if (!options || typeof options !== "object")
|
|
39
|
+
throw clientError("AUTHORIC_INVALID_CONFIG", "createClient needs { url, appUid }.");
|
|
40
|
+
if (typeof options.appUid !== "string" || SECRET_SHAPE.test(options.appUid)) {
|
|
41
|
+
throw clientError("AUTHORIC_SECRET_IN_CLIENT", "`appUid` is the app's uid, never a key. Authoric secrets (`au_…`) belong on a server; this client needs only the app uid.");
|
|
42
|
+
}
|
|
43
|
+
if (!UID.test(options.appUid)) {
|
|
44
|
+
throw clientError("AUTHORIC_INVALID_CONFIG", "`appUid` must be your app's uid. The client SDK speaks for an app's own users, never the platform.");
|
|
45
|
+
}
|
|
46
|
+
this.appUid = options.appUid.toLowerCase();
|
|
47
|
+
this.url = normalizeUrl(options.url);
|
|
48
|
+
const fetchImpl = options.fetch ?? (globalThis.fetch ? globalThis.fetch.bind(globalThis) : undefined);
|
|
49
|
+
if (!fetchImpl)
|
|
50
|
+
throw clientError("AUTHORIC_INVALID_CONFIG", "No fetch is available; pass one as `fetch`.");
|
|
51
|
+
const debug = options.debug === true
|
|
52
|
+
? (event) => console.debug("[authoric]", event)
|
|
53
|
+
: typeof options.debug === "function"
|
|
54
|
+
? options.debug
|
|
55
|
+
: null;
|
|
56
|
+
this.transport = new Transport({
|
|
57
|
+
baseUrl: this.url,
|
|
58
|
+
appUid: this.appUid,
|
|
59
|
+
fetch: fetchImpl,
|
|
60
|
+
maxRetryWaitMs: options.maxRetryWaitMs ?? 60000,
|
|
61
|
+
debug,
|
|
62
|
+
});
|
|
63
|
+
this.session = new SessionManager({
|
|
64
|
+
appUid: this.appUid,
|
|
65
|
+
storage: resolveStorage(options.storage),
|
|
66
|
+
channel: options[SERVER_MODE] ? null : options.broadcastChannel,
|
|
67
|
+
persistAccessToken: options[SERVER_MODE] === true,
|
|
68
|
+
exchange: (refreshToken, activeOrgUid) => this.transport.request(ROUTES.refresh, {
|
|
69
|
+
auth: "none",
|
|
70
|
+
body: activeOrgUid ? { refreshToken, activeOrgUid } : { refreshToken },
|
|
71
|
+
}),
|
|
72
|
+
});
|
|
73
|
+
this.transport.setTokenSource({
|
|
74
|
+
getAccessToken: () => this.session.getAccessToken(),
|
|
75
|
+
refresh: (opts) => this.session.refresh(opts),
|
|
76
|
+
end: (reason) => this.session.end(reason),
|
|
77
|
+
});
|
|
78
|
+
this.auth = new AuthClient(this.transport, this.session);
|
|
79
|
+
this.storage = new FilesClient(this.transport);
|
|
80
|
+
this.orgs = new OrgsClient(this.transport, this.session);
|
|
81
|
+
this.roles = new RolesClient(this.transport);
|
|
82
|
+
this.billing = new BillingClient(this.transport);
|
|
83
|
+
}
|
|
84
|
+
/** A table's rows, typed by your generated types file when you pass one to `createClient`. */
|
|
85
|
+
from(table) {
|
|
86
|
+
return new TableClient(this.transport, this.session, table);
|
|
87
|
+
}
|
|
88
|
+
/** A custom endpoint action on a table, such as `action("invoices", "/approve")`. */
|
|
89
|
+
action(table, path, options = {}) {
|
|
90
|
+
return new ActionClient(this.transport, table, path, options.method ?? "POST");
|
|
91
|
+
}
|
|
92
|
+
/** Stop cross-tab listening. For tests and short-lived clients; a page's client lives as long as the page. */
|
|
93
|
+
close() {
|
|
94
|
+
this.session.close();
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
/** Create a client for one app. */
|
|
98
|
+
export const createClient = (options) => new AuthoricClient(options);
|
|
99
|
+
/**
|
|
100
|
+
* A client for YOUR server, keeping the user's session in a cookie on your own domain so a page your
|
|
101
|
+
* server renders knows who is asking. Create one PER REQUEST, from that request's cookies: a client
|
|
102
|
+
* shared across requests would hand one user's session to the next.
|
|
103
|
+
*
|
|
104
|
+
* What the cookie remembers is not proof of who the user is (a cookie is input the user can edit):
|
|
105
|
+
* call `auth.getUser()`, which asks the API, before you trust it.
|
|
106
|
+
*/
|
|
107
|
+
export const createServerClient = (options) => {
|
|
108
|
+
const cookies = options?.cookies;
|
|
109
|
+
if (!cookies || typeof cookies.get !== "function" || typeof cookies.set !== "function" || typeof cookies.remove !== "function") {
|
|
110
|
+
throw clientError("AUTHORIC_INVALID_CONFIG", "createServerClient needs this request's cookies: { get(name), set(name, value, options), remove(name, options) }.");
|
|
111
|
+
}
|
|
112
|
+
const { cookies: _cookies, cookie, ...rest } = options;
|
|
113
|
+
return new AuthoricClient({ ...rest, storage: cookieStorage(cookies, cookie), [SERVER_MODE]: true });
|
|
114
|
+
};
|
|
115
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACvC,OAAO,EAAE,YAAY,EAAE,WAAW,EAAqB,MAAM,WAAW,CAAC;AACzE,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AACzC,OAAO,EAAE,YAAY,EAAE,cAAc,EAA6B,MAAM,cAAc,CAAC;AACvF,OAAO,EAAE,cAAc,EAAsB,MAAM,uBAAuB,CAAC;AAC3E,OAAO,EAAE,SAAS,EAAmB,MAAM,gBAAgB,CAAC;AAC5D,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,aAAa,EAAkD,MAAM,cAAc,CAAC;AAC7F,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAEpE,oGAAoG;AACpG,oGAAoG;AACpG,MAAM,WAAW,GAAG,MAAM,CAAC,qBAAqB,CAAC,CAAC;AAiClD,oGAAoG;AACpG,MAAM,GAAG,GAAG,iEAAiE,CAAC;AAE9E,MAAM,YAAY,GAAG,CAAC,KAAa,EAAU,EAAE;IAC9C,IAAI,MAAW,CAAC;IAChB,IAAI,CAAC;QACJ,MAAM,GAAG,IAAI,GAAG,CAAC,KAAK,CAAC,CAAC;IACzB,CAAC;IAAC,MAAM,CAAC;QACR,MAAM,WAAW,CAAC,yBAAyB,EAAE,wEAAwE,CAAC,CAAC;IACxH,CAAC;IACD,MAAM,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,EAAE;QAE9D,OAAO,IAAI,YAAY,CAAgB,IAAI,CAAC,SAAS,EAAE,KAAK,EAAE,IAAI,EAAE,OAAO,CAAC,MAAM,IAAI,MAAM,CAAC,CAAC;IAC/F,CAAC;IAED,8GAA8G;IAC9G,KAAK;QACJ,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;IACtB,CAAC;CACD;AAED,mCAAmC;AACnC,MAAM,CAAC,MAAM,YAAY,GAAG,CAAqD,OAAsB,EAAE,EAAE,CAC1G,IAAI,cAAc,CAAW,OAAO,CAAC,CAAC;AASvC;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAqD,OAA4B,EAAE,EAAE;IACtH,MAAM,OAAO,GAAG,OAAO,EAAE,OAAO,CAAC;IACjC,IAAI,CAAC,OAAO,IAAI,OAAO,OAAO,CAAC,GAAG,KAAK,UAAU,IAAI,OAAO,OAAO,CAAC,GAAG,KAAK,UAAU,IAAI,OAAO,OAAO,CAAC,MAAM,KAAK,UAAU,EAAE,CAAC;QAChI,MAAM,WAAW,CAChB,yBAAyB,EACzB,mHAAmH,CACnH,CAAC;IACH,CAAC;IACD,MAAM,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,OAAO,CAAC;IACvD,OAAO,IAAI,cAAc,CAAW,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,aAAa,CAAC,OAAO,EAAE,MAAM,CAAC,EAAE,CAAC,WAAW,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC;AAChH,CAAC,CAAC"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { SessionStorageAdapter } from "./storage-adapters.js";
|
|
2
|
+
/** How the SDK asks a cookie to be written. `maxAge` is in seconds (Express takes milliseconds). */
|
|
3
|
+
export interface CookieOptions {
|
|
4
|
+
path: string;
|
|
5
|
+
/** Seconds. 0 removes the cookie. */
|
|
6
|
+
maxAge: number;
|
|
7
|
+
httpOnly: boolean;
|
|
8
|
+
secure: boolean;
|
|
9
|
+
sameSite: "lax" | "strict" | "none";
|
|
10
|
+
domain?: string;
|
|
11
|
+
}
|
|
12
|
+
/** The three things the SDK needs from your framework, for the current request. */
|
|
13
|
+
export interface CookieMethods {
|
|
14
|
+
get(name: string): string | null | undefined | Promise<string | null | undefined>;
|
|
15
|
+
set(name: string, value: string, options: CookieOptions): void | Promise<void>;
|
|
16
|
+
remove(name: string, options: CookieOptions): void | Promise<void>;
|
|
17
|
+
}
|
|
18
|
+
/** Overrides for the session cookie's defaults. */
|
|
19
|
+
export interface SessionCookieSettings {
|
|
20
|
+
/**
|
|
21
|
+
* Hide the cookie from JavaScript on the page. Stronger against script injection, but then the
|
|
22
|
+
* browser half of the SDK cannot use this session, and every call goes through your server.
|
|
23
|
+
* Default false.
|
|
24
|
+
*/
|
|
25
|
+
httpOnly?: boolean;
|
|
26
|
+
/** Default true. Set false only for plain-http local development. */
|
|
27
|
+
secure?: boolean;
|
|
28
|
+
/** Default "lax": the cookie is not sent on cross-site form posts. */
|
|
29
|
+
sameSite?: "lax" | "strict" | "none";
|
|
30
|
+
/** Default "/". */
|
|
31
|
+
path?: string;
|
|
32
|
+
/** Default none: the cookie belongs to the exact host that set it. */
|
|
33
|
+
domain?: string;
|
|
34
|
+
/** Seconds the cookie lasts. Default 30 days, the session's idle window. */
|
|
35
|
+
maxAge?: number;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* The cookie's name for a storage key. `__Host-` when the cookie can carry it (Secure, Path=/, no
|
|
39
|
+
* Domain): the browser then refuses one set by a subdomain or over plain http, so a sibling site
|
|
40
|
+
* cannot plant a session.
|
|
41
|
+
*/
|
|
42
|
+
export declare const sessionCookieName: (key: string, options: CookieOptions) => string;
|
|
43
|
+
/** A session storage adapter over a request's cookies. Used by createServerClient. */
|
|
44
|
+
export declare const cookieStorage: (cookies: CookieMethods, settings?: SessionCookieSettings) => SessionStorageAdapter;
|
|
45
|
+
/**
|
|
46
|
+
* A `Set-Cookie` header value, for a runtime with no cookie API: `headers.append("Set-Cookie",
|
|
47
|
+
* serializeCookie(name, value, options))`. The name and value the SDK passes are already safe text.
|
|
48
|
+
*/
|
|
49
|
+
export declare const serializeCookie: (name: string, value: string, options: CookieOptions) => string;
|
|
50
|
+
/** One cookie from a `Cookie` request header, for a runtime with no cookie API. */
|
|
51
|
+
export declare const readCookie: (cookieHeader: string | null | undefined, name: string) => string | null;
|
|
52
|
+
type HeaderBag = {
|
|
53
|
+
get(name: string): string | null;
|
|
54
|
+
} | Record<string, string | string[] | undefined>;
|
|
55
|
+
/**
|
|
56
|
+
* Whether a request to YOUR server that acts with the session cookie came from your own pages
|
|
57
|
+
* (cross-site request forgery). Reads (GET, HEAD, OPTIONS) always pass: never change anything on
|
|
58
|
+
* one. A write passes only when its `Origin` (or, without one, its `Referer`) is one of `allowed`.
|
|
59
|
+
* SameSite=Lax already keeps the cookie off cross-site form posts; this is the second check.
|
|
60
|
+
*/
|
|
61
|
+
export declare const isSameOriginRequest: (request: {
|
|
62
|
+
method?: string;
|
|
63
|
+
headers: HeaderBag;
|
|
64
|
+
}, allowed: string | readonly string[]) => boolean;
|
|
65
|
+
export {};
|
|
66
|
+
//# sourceMappingURL=cookies.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cookies.d.ts","sourceRoot":"","sources":["../../src/cookies.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,uBAAuB,CAAC;AAEnE,oGAAoG;AACpG,MAAM,WAAW,aAAa;IAC7B,IAAI,EAAE,MAAM,CAAC;IACb,qCAAqC;IACrC,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,OAAO,CAAC;IAClB,MAAM,EAAE,OAAO,CAAC;IAChB,QAAQ,EAAE,KAAK,GAAG,QAAQ,GAAG,MAAM,CAAC;IACpC,MAAM,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,mFAAmF;AAC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S,GAAG;IAAE,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAA;CAAE,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,SAAS,CAAC,CAAC;AAkBtG;;;;;GAKG;AACH,eAAO,MAAM,mBAAmB,GAC/B,SAAS;IAAE,MAAM,CAAC,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,SAAS,CAAA;CAAE,EAChD,SAAS,MAAM,GAAG,SAAS,MAAM,EAAE,KACjC,OAMF,CAAC"}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// A server's session, kept in a cookie on the APP's OWN domain (notes/client-sdk-plan.md §2.2).
|
|
2
|
+
//
|
|
3
|
+
// Browsers send a site only its own cookies, so a session a server can read on a full page load must
|
|
4
|
+
// live in a cookie the tenant's server sets, on the tenant's domain, never one set by the Authoric
|
|
5
|
+
// API. The SDK runs on that server (createServerClient) and keeps the session here instead of
|
|
6
|
+
// localStorage. The Authoric API itself still never reads a cookie: it stays bearer-only.
|
|
7
|
+
//
|
|
8
|
+
// Framework-neutral on purpose: every server framework can get, set and remove a cookie, so the
|
|
9
|
+
// developer hands the SDK those three things for the CURRENT request. `serializeCookie` is there for
|
|
10
|
+
// a runtime with no cookie API of its own (a raw Node or Fetch handler).
|
|
11
|
+
import { clientError } from "./errors.js";
|
|
12
|
+
const THIRTY_DAYS = 30 * 24 * 60 * 60;
|
|
13
|
+
// A cookie (name, value and attributes) must stay under about 4096 bytes in every browser.
|
|
14
|
+
const MAX_COOKIE_BYTES = 4000;
|
|
15
|
+
const resolveOptions = (settings = {}) => ({
|
|
16
|
+
path: settings.path ?? "/",
|
|
17
|
+
maxAge: settings.maxAge ?? THIRTY_DAYS,
|
|
18
|
+
httpOnly: settings.httpOnly === true,
|
|
19
|
+
secure: settings.secure !== false,
|
|
20
|
+
sameSite: settings.sameSite ?? "lax",
|
|
21
|
+
...(settings.domain ? { domain: settings.domain } : {}),
|
|
22
|
+
});
|
|
23
|
+
/**
|
|
24
|
+
* The cookie's name for a storage key. `__Host-` when the cookie can carry it (Secure, Path=/, no
|
|
25
|
+
* Domain): the browser then refuses one set by a subdomain or over plain http, so a sibling site
|
|
26
|
+
* cannot plant a session.
|
|
27
|
+
*/
|
|
28
|
+
export const sessionCookieName = (key, options) => {
|
|
29
|
+
const base = key.replace(/[^A-Za-z0-9_-]/g, "-");
|
|
30
|
+
return options.secure && options.path === "/" && !options.domain ? `__Host-${base}` : base;
|
|
31
|
+
};
|
|
32
|
+
// base64url of the UTF-8 bytes: the value is only [A-Za-z0-9_-], which every framework writes and reads
|
|
33
|
+
// back unchanged, whether or not it URL-encodes cookie values itself.
|
|
34
|
+
const toBase64Url = (text) => {
|
|
35
|
+
const bytes = new TextEncoder().encode(text);
|
|
36
|
+
let binary = "";
|
|
37
|
+
for (const byte of bytes)
|
|
38
|
+
binary += String.fromCharCode(byte);
|
|
39
|
+
return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
40
|
+
};
|
|
41
|
+
const fromBase64Url = (value) => {
|
|
42
|
+
try {
|
|
43
|
+
const padded = value.replace(/-/g, "+").replace(/_/g, "/").padEnd(Math.ceil(value.length / 4) * 4, "=");
|
|
44
|
+
const binary = atob(padded);
|
|
45
|
+
return new TextDecoder().decode(Uint8Array.from(binary, (ch) => ch.charCodeAt(0)));
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
/** A session storage adapter over a request's cookies. Used by createServerClient. */
|
|
52
|
+
export const cookieStorage = (cookies, settings = {}) => {
|
|
53
|
+
const options = resolveOptions(settings);
|
|
54
|
+
return {
|
|
55
|
+
getItem: async (key) => {
|
|
56
|
+
const raw = await cookies.get(sessionCookieName(key, options));
|
|
57
|
+
return typeof raw === "string" && raw ? fromBase64Url(raw) : null;
|
|
58
|
+
},
|
|
59
|
+
setItem: async (key, value) => {
|
|
60
|
+
const name = sessionCookieName(key, options);
|
|
61
|
+
const encoded = toBase64Url(value);
|
|
62
|
+
if (name.length + encoded.length + 120 > MAX_COOKIE_BYTES) {
|
|
63
|
+
throw clientError("AUTHORIC_COOKIE_TOO_LARGE", "This session does not fit in one cookie (about 4 KB).");
|
|
64
|
+
}
|
|
65
|
+
await cookies.set(name, encoded, options);
|
|
66
|
+
},
|
|
67
|
+
removeItem: async (key) => {
|
|
68
|
+
await cookies.remove(sessionCookieName(key, options), { ...options, maxAge: 0 });
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* A `Set-Cookie` header value, for a runtime with no cookie API: `headers.append("Set-Cookie",
|
|
74
|
+
* serializeCookie(name, value, options))`. The name and value the SDK passes are already safe text.
|
|
75
|
+
*/
|
|
76
|
+
export const serializeCookie = (name, value, options) => {
|
|
77
|
+
const parts = [`${name}=${value}`, `Path=${options.path}`, `Max-Age=${Math.max(0, Math.floor(options.maxAge))}`];
|
|
78
|
+
if (options.domain)
|
|
79
|
+
parts.push(`Domain=${options.domain}`);
|
|
80
|
+
parts.push(`SameSite=${options.sameSite.charAt(0).toUpperCase()}${options.sameSite.slice(1)}`);
|
|
81
|
+
if (options.secure)
|
|
82
|
+
parts.push("Secure");
|
|
83
|
+
if (options.httpOnly)
|
|
84
|
+
parts.push("HttpOnly");
|
|
85
|
+
return parts.join("; ");
|
|
86
|
+
};
|
|
87
|
+
/** One cookie from a `Cookie` request header, for a runtime with no cookie API. */
|
|
88
|
+
export const readCookie = (cookieHeader, name) => {
|
|
89
|
+
for (const part of String(cookieHeader || "").split(";")) {
|
|
90
|
+
const index = part.indexOf("=");
|
|
91
|
+
if (index < 0)
|
|
92
|
+
continue;
|
|
93
|
+
if (part.slice(0, index).trim() === name)
|
|
94
|
+
return part.slice(index + 1).trim();
|
|
95
|
+
}
|
|
96
|
+
return null;
|
|
97
|
+
};
|
|
98
|
+
const header = (headers, name) => {
|
|
99
|
+
if (typeof headers.get === "function")
|
|
100
|
+
return headers.get(name);
|
|
101
|
+
const record = headers;
|
|
102
|
+
const value = record[name] ?? record[name.toLowerCase()];
|
|
103
|
+
return Array.isArray(value) ? (value[0] ?? null) : (value ?? null);
|
|
104
|
+
};
|
|
105
|
+
const originOf = (value) => {
|
|
106
|
+
if (!value)
|
|
107
|
+
return null;
|
|
108
|
+
try {
|
|
109
|
+
return new URL(value).origin;
|
|
110
|
+
}
|
|
111
|
+
catch {
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
/**
|
|
116
|
+
* Whether a request to YOUR server that acts with the session cookie came from your own pages
|
|
117
|
+
* (cross-site request forgery). Reads (GET, HEAD, OPTIONS) always pass: never change anything on
|
|
118
|
+
* one. A write passes only when its `Origin` (or, without one, its `Referer`) is one of `allowed`.
|
|
119
|
+
* SameSite=Lax already keeps the cookie off cross-site form posts; this is the second check.
|
|
120
|
+
*/
|
|
121
|
+
export const isSameOriginRequest = (request, allowed) => {
|
|
122
|
+
const method = String(request.method || "GET").toUpperCase();
|
|
123
|
+
if (method === "GET" || method === "HEAD" || method === "OPTIONS")
|
|
124
|
+
return true;
|
|
125
|
+
const allowList = (Array.isArray(allowed) ? allowed : [allowed]).map((v) => originOf(String(v))).filter(Boolean);
|
|
126
|
+
const from = originOf(header(request.headers, "origin")) ?? originOf(header(request.headers, "referer"));
|
|
127
|
+
return Boolean(from) && allowList.includes(from);
|
|
128
|
+
};
|
|
129
|
+
//# sourceMappingURL=cookies.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cookies.js","sourceRoot":"","sources":["../../src/cookies.ts"],"names":[],"mappings":"AAAA,gGAAgG;AAChG,EAAE;AACF,qGAAqG;AACrG,mGAAmG;AACnG,8FAA8F;AAC9F,0FAA0F;AAC1F,EAAE;AACF,gGAAgG;AAChG,qGAAqG;AACrG,yEAAyE;AACzE,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,C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ions, type Subscription } from "./realtime.js";
|
|
2
|
+
import type { SessionManager } from "./session.js";
|
|
3
|
+
import type { Transport } from "./transport.js";
|
|
4
|
+
import type { ChangeEvent, ListResult } from "./types.js";
|
|
5
|
+
/** The shape of one table in a generated types file (`npx @authoric/js types`). */
|
|
6
|
+
export interface TableShape {
|
|
7
|
+
Row: Record<string, unknown>;
|
|
8
|
+
Insert: Record<string, unknown>;
|
|
9
|
+
Update: Record<string, unknown>;
|
|
10
|
+
/** Each reference as a relation, `include`-able: its name and the table it points at. */
|
|
11
|
+
Relations?: Record<string, string>;
|
|
12
|
+
}
|
|
13
|
+
export interface GenericTable {
|
|
14
|
+
Row: Record<string, unknown>;
|
|
15
|
+
Insert: Record<string, unknown>;
|
|
16
|
+
Update: Record<string, unknown>;
|
|
17
|
+
Relations?: Record<string, string>;
|
|
18
|
+
}
|
|
19
|
+
/** The tables of a generated `Database`, by name. */
|
|
20
|
+
export type GenericTables = Record<string, GenericTable>;
|
|
21
|
+
type FilterValue<V> = Exclude<NonNullable<V>, "[redacted]">;
|
|
22
|
+
/** Comparison operators for one column (helpers/filterDsl.js on the server). */
|
|
23
|
+
export interface Operators<V> {
|
|
24
|
+
$eq?: V | null;
|
|
25
|
+
$ne?: V | null;
|
|
26
|
+
$gt?: V;
|
|
27
|
+
$gte?: V;
|
|
28
|
+
$lt?: V;
|
|
29
|
+
$lte?: V;
|
|
30
|
+
/** Any of these values (up to 100). */
|
|
31
|
+
$in?: V[];
|
|
32
|
+
/** Inclusive range: `[low, high]`. */
|
|
33
|
+
$between?: [V, V];
|
|
34
|
+
/** The column contains this text, literally (no wildcards). Follows the column's collation. */
|
|
35
|
+
$contains?: string;
|
|
36
|
+
/** The column starts with this text, literally. */
|
|
37
|
+
$startsWith?: string;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Which rows: `{ status: "open" }` (equality), `{ due_at: null }` (IS NULL), `{ total: { $gte: 100 } }`,
|
|
41
|
+
* combined with `$and` / `$or`. A reference is filtered by its `<name>_uid` and uids. At most 20
|
|
42
|
+
* conditions, nested 3 deep. A redacted column cannot be filtered on.
|
|
43
|
+
*/
|
|
44
|
+
export type Where<Row> = {
|
|
45
|
+
[K in keyof Row]?: FilterValue<Row[K]> | null | Operators<FilterValue<Row[K]>>;
|
|
46
|
+
} & {
|
|
47
|
+
$and?: Where<Row>[];
|
|
48
|
+
$or?: Where<Row>[];
|
|
49
|
+
};
|
|
50
|
+
/** A column to sort by (ascending), or `{ column, dir }`. `<name>_uid` sorts by the reference. */
|
|
51
|
+
export type OrderBy<Row> = Extract<keyof Row, string> | {
|
|
52
|
+
column: Extract<keyof Row, string>;
|
|
53
|
+
dir?: "asc" | "desc";
|
|
54
|
+
};
|
|
55
|
+
type RelationsOf<T> = T extends {
|
|
56
|
+
Relations: infer R;
|
|
57
|
+
} ? (R extends Record<string, string> ? R : never) : Record<string, string>;
|
|
58
|
+
/**
|
|
59
|
+
* The relation names a table can `include`: a generated table's own (none when its `Relations` is
|
|
60
|
+
* `Record<string, never>`), any name for an untyped table.
|
|
61
|
+
*/
|
|
62
|
+
export type RelationName<T> = T extends {
|
|
63
|
+
Relations: infer R;
|
|
64
|
+
} ? string extends keyof R ? [R[string]] extends [never] ? never : string : Extract<keyof R, string> : string;
|
|
65
|
+
type RowOf<Tables, Name> = Name extends keyof Tables ? (Tables[Name] extends {
|
|
66
|
+
Row: infer R;
|
|
67
|
+
} ? R : Record<string, unknown>) : Record<string, unknown>;
|
|
68
|
+
/** The rows `include` adds: each relation's row, or `null` where there is none or it cannot be read. */
|
|
69
|
+
export type Included<T, Tables, I extends string> = {
|
|
70
|
+
[K in I]: (K extends keyof RelationsOf<T> ? RowOf<Tables, RelationsOf<T>[K]> : Record<string, unknown>) | null;
|
|
71
|
+
};
|
|
72
|
+
/** A row as a list returns it: the columns asked for (and `uid`), plus each included relation. */
|
|
73
|
+
export type ListedRow<T extends GenericTable, Tables, C extends keyof T["Row"], I extends string> = Pick<T["Row"], C | Extract<"uid", keyof T["Row"]>> & Included<T, Tables, I>;
|
|
74
|
+
export interface ListOptions<Row, C extends keyof Row = keyof Row, I extends string = never> {
|
|
75
|
+
where?: Where<Row>;
|
|
76
|
+
/** Up to 3 columns. Not with `after`. */
|
|
77
|
+
order?: OrderBy<Row> | OrderBy<Row>[];
|
|
78
|
+
/** Return only these columns (`uid` always comes back). */
|
|
79
|
+
columns?: readonly C[];
|
|
80
|
+
/** Up to 3 relations: each row's referenced row, read under that table's own permissions. */
|
|
81
|
+
include?: readonly I[];
|
|
82
|
+
/** Rows per page: 50 by default, at most 1,000. */
|
|
83
|
+
limit?: number;
|
|
84
|
+
offset?: number;
|
|
85
|
+
/**
|
|
86
|
+
* Keyset paging in creation order: `null` (or `""`) for the first page, then the previous page's
|
|
87
|
+
* `paging.nextCursor`. The way to walk a large table: a deep page costs what the first does.
|
|
88
|
+
*/
|
|
89
|
+
after?: string | null;
|
|
90
|
+
/** Also count every matching row into `paging.total` (a second query on the server). */
|
|
91
|
+
total?: boolean;
|
|
92
|
+
signal?: AbortSignal;
|
|
93
|
+
}
|
|
94
|
+
/** One aggregate measure. `column` is optional for `count`. */
|
|
95
|
+
export interface Measure<Row> {
|
|
96
|
+
op: "count" | "count_distinct" | "sum" | "avg" | "min" | "max";
|
|
97
|
+
column?: Extract<keyof Row, string>;
|
|
98
|
+
as?: string;
|
|
99
|
+
}
|
|
100
|
+
export interface AggregateSpec<Row> {
|
|
101
|
+
measures: Measure<Row>[];
|
|
102
|
+
groupBy?: Array<{
|
|
103
|
+
column: Extract<keyof Row, string>;
|
|
104
|
+
truncate?: "year" | "quarter" | "month" | "week" | "day" | "hour";
|
|
105
|
+
bucket?: number;
|
|
106
|
+
lower?: boolean;
|
|
107
|
+
}>;
|
|
108
|
+
filters?: Where<Row>;
|
|
109
|
+
having?: Record<string, Operators<number>>;
|
|
110
|
+
/** `"<as> DESC"`. */
|
|
111
|
+
sort?: string;
|
|
112
|
+
/** At most 1,000 groups. */
|
|
113
|
+
limit?: number;
|
|
114
|
+
}
|
|
115
|
+
export interface AggregateResult {
|
|
116
|
+
rows: Array<Record<string, unknown>>;
|
|
117
|
+
meta: {
|
|
118
|
+
groups: number;
|
|
119
|
+
matchedGroups: number | null;
|
|
120
|
+
hasMore: boolean;
|
|
121
|
+
truncated: boolean;
|
|
122
|
+
limit: number;
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
export interface WriteOptions {
|
|
126
|
+
/**
|
|
127
|
+
* Makes the write safe to send again: the server runs it once and answers a repeat with the first
|
|
128
|
+
* answer, for 24 hours. The SDK sends a fresh random key with every write unless you pass your
|
|
129
|
+
* own, so its own retries never write twice. Pass `false` to send none.
|
|
130
|
+
*/
|
|
131
|
+
idempotencyKey?: string | false;
|
|
132
|
+
signal?: AbortSignal;
|
|
133
|
+
}
|
|
134
|
+
export declare const newIdempotencyKey: () => string;
|
|
135
|
+
export declare class TableClient<T extends GenericTable = GenericTable, Tables = GenericTables> {
|
|
136
|
+
private readonly transport;
|
|
137
|
+
private readonly session;
|
|
138
|
+
readonly table: string;
|
|
139
|
+
constructor(transport: Transport, session: SessionManager, table: string);
|
|
140
|
+
/**
|
|
141
|
+
* One page of rows (the API's `{ items, paging }`): 50 unless you ask for more, at most 1,000.
|
|
142
|
+
* `columns` narrows each row; `include` adds each relation's row beside its `<name>_uid`.
|
|
143
|
+
*/
|
|
144
|
+
list<C extends keyof T["Row"] = keyof T["Row"], I extends RelationName<T> = never>(options?: ListOptions<T["Row"], C, I>): Promise<ListResult<ListedRow<T, Tables, C, I>>>;
|
|
145
|
+
/**
|
|
146
|
+
* Every matching row, a page at a time, without holding them all at once. Without `order` it walks
|
|
147
|
+
* the table by keyset (creation order), which stays correct and fast however large the table is.
|
|
148
|
+
*/
|
|
149
|
+
rows<C extends keyof T["Row"] = keyof T["Row"], I extends RelationName<T> = never>(options?: Omit<ListOptions<T["Row"], C, I>, "offset" | "after" | "total"> & {
|
|
150
|
+
pageSize?: number;
|
|
151
|
+
}): AsyncGenerator<ListedRow<T, Tables, C, I>>;
|
|
152
|
+
/** Counts, sums, averages, minimums and maximums, grouped, computed on the server. */
|
|
153
|
+
aggregate(spec: AggregateSpec<T["Row"]>, options?: {
|
|
154
|
+
signal?: AbortSignal;
|
|
155
|
+
}): Promise<AggregateResult>;
|
|
156
|
+
/** How many rows match (`where` optional). */
|
|
157
|
+
count(where?: Where<T["Row"]>, options?: {
|
|
158
|
+
signal?: AbortSignal;
|
|
159
|
+
}): Promise<number>;
|
|
160
|
+
/** One row by its uid. */
|
|
161
|
+
get(uid: string, options?: {
|
|
162
|
+
signal?: AbortSignal;
|
|
163
|
+
}): Promise<T["Row"]>;
|
|
164
|
+
/** Create a row. Answers `{ uid, ...fields as sent }`. A reference is sent as `<name>_uid`. */
|
|
165
|
+
create(values: T["Insert"], options?: WriteOptions): Promise<{
|
|
166
|
+
uid: string;
|
|
167
|
+
} & Partial<T["Row"]>>;
|
|
168
|
+
/** Change a row. Answers the row as it now stands. */
|
|
169
|
+
update(uid: string, patch: T["Update"], options?: WriteOptions): Promise<T["Row"]>;
|
|
170
|
+
delete(uid: string, options?: WriteOptions): Promise<{
|
|
171
|
+
msg: string;
|
|
172
|
+
}>;
|
|
173
|
+
/** Create many rows. Answers their uids, in the order given. */
|
|
174
|
+
createMany(rows: T["Insert"][], options?: WriteOptions): Promise<{
|
|
175
|
+
count: number;
|
|
176
|
+
uids: string[];
|
|
177
|
+
}>;
|
|
178
|
+
/** Change many rows, each by its uid. */
|
|
179
|
+
updateMany(changes: Array<{
|
|
180
|
+
uid: string;
|
|
181
|
+
values: T["Update"];
|
|
182
|
+
}>, options?: WriteOptions): Promise<{
|
|
183
|
+
count: number;
|
|
184
|
+
affectedRows: number;
|
|
185
|
+
}>;
|
|
186
|
+
/** Delete many rows by uid. */
|
|
187
|
+
deleteMany(uids: string[], options?: WriteOptions): Promise<{
|
|
188
|
+
count: number;
|
|
189
|
+
affectedRows: number;
|
|
190
|
+
}>;
|
|
191
|
+
/**
|
|
192
|
+
* Live changes to this table, as this user is allowed to see them: the server re-reads every
|
|
193
|
+
* changed row as the subscriber (policy, org scope, redaction), and skips rows they cannot read.
|
|
194
|
+
* Subscribing needs the same permission as listing.
|
|
195
|
+
*/
|
|
196
|
+
subscribe(handler: (event: ChangeEvent<T["Row"]>) => void, options?: SubscribeOptions): Subscription;
|
|
197
|
+
}
|
|
198
|
+
/** A custom endpoint action on a table (`table_meta.config.actions`). */
|
|
199
|
+
export declare class ActionClient<Input = Record<string, unknown>, Output = unknown> {
|
|
200
|
+
private readonly transport;
|
|
201
|
+
readonly table: string;
|
|
202
|
+
readonly path: string;
|
|
203
|
+
readonly method: "GET" | "POST" | "PATCH" | "DELETE";
|
|
204
|
+
constructor(transport: Transport, table: string, path: string, method?: "GET" | "POST" | "PATCH" | "DELETE");
|
|
205
|
+
call(input?: Input, options?: {
|
|
206
|
+
signal?: AbortSignal;
|
|
207
|
+
anonymous?: boolean;
|
|
208
|
+
}): Promise<Output>;
|
|
209
|
+
}
|
|
210
|
+
export {};
|
|
211
|
+
//# sourceMappingURL=data.d.ts.map
|