terrascale 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +143 -0
- package/package.json +159 -0
- package/sdk-current-contract.json +27 -0
- package/sdk-route-manifest.json +67 -0
- package/src/admin.js +9 -0
- package/src/better-auth.js +14 -0
- package/src/config.js +121 -0
- package/src/database-codec.js +845 -0
- package/src/database-types.js +237 -0
- package/src/database-view.js +422 -0
- package/src/database.js +420 -0
- package/src/discovery.js +374 -0
- package/src/http.js +887 -0
- package/src/index.js +79 -0
- package/src/local/authentication.js +47 -0
- package/src/local/better-auth.js +517 -0
- package/src/local/cli.js +51 -0
- package/src/local/context.js +23 -0
- package/src/local/environment.js +109 -0
- package/src/local/index.js +204 -0
- package/src/local/router.js +999 -0
- package/src/local/server.js +664 -0
- package/src/local/store.js +530 -0
- package/src/local/test-environment.js +74 -0
- package/src/management-contracts.js +72 -0
- package/src/management.js +12 -0
- package/src/native-origin.js +75 -0
- package/src/postgres.js +494 -0
- package/src/react/core.js +743 -0
- package/src/react/index.js +99 -0
- package/src/result.js +251 -0
- package/src/schema.js +366 -0
- package/src/sql.js +996 -0
- package/src/svelte/index.js +129 -0
- package/src/tanstack/index.js +511 -0
- package/src/ts-auth-discovery.js +190 -0
- package/src/ts-auth.js +3497 -0
- package/types/admin.d.ts +6 -0
- package/types/better-auth.d.ts +8 -0
- package/types/config.d.ts +58 -0
- package/types/database-codec.d.ts +111 -0
- package/types/database-types.d.ts +213 -0
- package/types/database-view.d.ts +183 -0
- package/types/database.d.ts +98 -0
- package/types/discovery.d.ts +114 -0
- package/types/http.d.ts +46 -0
- package/types/index.d.ts +52 -0
- package/types/local/authentication.d.ts +11 -0
- package/types/local/better-auth.d.ts +33 -0
- package/types/local/cli.d.ts +2 -0
- package/types/local/context.d.ts +14 -0
- package/types/local/environment.d.ts +23 -0
- package/types/local/index.d.ts +94 -0
- package/types/local/router.d.ts +66 -0
- package/types/local/server.d.ts +54 -0
- package/types/local/store.d.ts +106 -0
- package/types/local/test-environment.d.ts +25 -0
- package/types/management-contracts.d.ts +44 -0
- package/types/management.d.ts +6 -0
- package/types/native-origin.d.ts +23 -0
- package/types/postgres.d.ts +123 -0
- package/types/react/core.d.ts +366 -0
- package/types/react/index.d.ts +54 -0
- package/types/result.d.ts +161 -0
- package/types/schema.d.ts +145 -0
- package/types/sql.d.ts +288 -0
- package/types/svelte/index.d.ts +81 -0
- package/types/tanstack/index.d.ts +165 -0
- package/types/ts-auth-discovery.d.ts +11 -0
- package/types/ts-auth.d.ts +1826 -0
package/src/config.js
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { isDataApiHostname } from "./native-origin.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Shared fetch signature; native database and hosted identity credentials stay distinct.
|
|
5
|
+
* @typedef {(input: RequestInfo | URL, init?: RequestInit) => Promise<Response>} FetchLike
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* @typedef {string | (() => Promise<string | undefined> | string | undefined)} AccessTokenProvider
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* `requestTimeoutMs`: one deadline covers discovery, credentials, requests, bodies and retry delays.
|
|
13
|
+
* @typedef {{
|
|
14
|
+
* readonly apiBaseUrl: string;
|
|
15
|
+
* readonly accessToken?: AccessTokenProvider;
|
|
16
|
+
* readonly fetch?: FetchLike;
|
|
17
|
+
* readonly maxRetryAttempts?: number | undefined;
|
|
18
|
+
* readonly retryDelayMs?: number | undefined;
|
|
19
|
+
* readonly requestTimeoutMs?: number | undefined;
|
|
20
|
+
* }} TsAuthRuntimeConfig
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @typedef {{
|
|
25
|
+
* readonly apiBaseUrl: string;
|
|
26
|
+
* readonly maxRetryAttempts: number;
|
|
27
|
+
* readonly retryDelayMs: number;
|
|
28
|
+
* readonly requestTimeoutMs: number;
|
|
29
|
+
* }} ResolvedTsAuthRuntimeConfig
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @param {TsAuthRuntimeConfig} config
|
|
34
|
+
* @returns {ResolvedTsAuthRuntimeConfig}
|
|
35
|
+
*/
|
|
36
|
+
export function resolveTsAuthRuntimeConfig(config) {
|
|
37
|
+
if (Object.hasOwn(config, "defaultHeaders"))
|
|
38
|
+
throw new TypeError("defaultHeaders is not supported.");
|
|
39
|
+
const maxRetryAttempts = config.maxRetryAttempts ?? 0;
|
|
40
|
+
if (
|
|
41
|
+
!Number.isInteger(maxRetryAttempts) ||
|
|
42
|
+
maxRetryAttempts < 0 ||
|
|
43
|
+
maxRetryAttempts > 8
|
|
44
|
+
) {
|
|
45
|
+
throw new TypeError("maxRetryAttempts must be an integer between 0 and 8.");
|
|
46
|
+
}
|
|
47
|
+
const retryDelayMs = config.retryDelayMs ?? 250;
|
|
48
|
+
if (
|
|
49
|
+
!Number.isFinite(retryDelayMs) ||
|
|
50
|
+
retryDelayMs < 0 ||
|
|
51
|
+
retryDelayMs > 30_000
|
|
52
|
+
) {
|
|
53
|
+
throw new TypeError(
|
|
54
|
+
"retryDelayMs must be a finite number between 0 and 30000.",
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
const requestTimeoutMs = config.requestTimeoutMs ?? 30_000;
|
|
58
|
+
if (
|
|
59
|
+
!Number.isInteger(requestTimeoutMs) ||
|
|
60
|
+
requestTimeoutMs < 0 ||
|
|
61
|
+
requestTimeoutMs > 300_000
|
|
62
|
+
) {
|
|
63
|
+
throw new TypeError(
|
|
64
|
+
"requestTimeoutMs must be an integer between 0 and 300000.",
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
const apiBaseUrl = config.apiBaseUrl.trim().replace(/\/+$/u, "");
|
|
68
|
+
/** @type {URL} */
|
|
69
|
+
let url;
|
|
70
|
+
try {
|
|
71
|
+
url = new URL(apiBaseUrl);
|
|
72
|
+
} catch {
|
|
73
|
+
return { apiBaseUrl, maxRetryAttempts, retryDelayMs, requestTimeoutMs };
|
|
74
|
+
}
|
|
75
|
+
if (isDataApiHostname(url.hostname)) {
|
|
76
|
+
throw new TypeError("Database API hosts cannot be used as an auth origin.");
|
|
77
|
+
}
|
|
78
|
+
for (const key of url.searchParams.keys()) {
|
|
79
|
+
if (
|
|
80
|
+
/(?:auth|cookie|credential|key|pass(?:word)?|secret|session|token)/iu.test(
|
|
81
|
+
key,
|
|
82
|
+
)
|
|
83
|
+
)
|
|
84
|
+
throw new TypeError(
|
|
85
|
+
"ts-auth origins cannot contain secret-bearing query parameters.",
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
if (
|
|
89
|
+
url.username !== "" ||
|
|
90
|
+
url.password !== "" ||
|
|
91
|
+
url.pathname !== "/" ||
|
|
92
|
+
url.search !== "" ||
|
|
93
|
+
url.hash !== ""
|
|
94
|
+
) {
|
|
95
|
+
throw new TypeError(
|
|
96
|
+
"The ts-auth apiBaseUrl must be a bare origin without credentials, a path, query, or fragment.",
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
const hostname = url.hostname.toLowerCase();
|
|
100
|
+
const loopback = ["localhost", "127.0.0.1", "[::1]"].includes(hostname);
|
|
101
|
+
const testTransport =
|
|
102
|
+
config.fetch !== undefined &&
|
|
103
|
+
(hostname === "test" || hostname.endsWith(".test"));
|
|
104
|
+
if (
|
|
105
|
+
url.protocol !== "https:" &&
|
|
106
|
+
!(url.protocol === "http:" && (loopback || testTransport))
|
|
107
|
+
) {
|
|
108
|
+
throw new TypeError(
|
|
109
|
+
"ts-auth endpoints require HTTPS except explicit loopback/test transports.",
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
return { apiBaseUrl, maxRetryAttempts, retryDelayMs, requestTimeoutMs };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* @param {AccessTokenProvider | undefined} provider
|
|
117
|
+
* @returns {Promise<string | undefined>}
|
|
118
|
+
*/
|
|
119
|
+
export async function resolveAccessToken(provider) {
|
|
120
|
+
return typeof provider === "function" ? await provider() : provider;
|
|
121
|
+
}
|