@squadbase/vantage-sdk 0.1.0-dev.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/README.md ADDED
@@ -0,0 +1,66 @@
1
+ # @squadbase/vantage-sdk
2
+
3
+ Server-side SDK for Squadbase Vantage apps. Import it from `server/api/**` handlers.
4
+
5
+ ## connection
6
+
7
+ `connection` resolves where to send a request and how to authenticate it. You send the request
8
+ yourself with `fetch`.
9
+
10
+ ```ts
11
+ import type { ApiContext } from "@squadbase/vantage/server";
12
+ import { HttpError } from "@squadbase/vantage/server";
13
+ import { connection } from "@squadbase/vantage-sdk/server";
14
+
15
+ const CONNECTION_ID = "...";
16
+ const QUERY_ID = "...";
17
+
18
+ export async function GET(ctx: ApiContext) {
19
+ const { baseUrl, headers } = await connection(ctx, CONNECTION_ID);
20
+
21
+ const res = await fetch(`${baseUrl}/queries/${QUERY_ID}`, {
22
+ method: "POST",
23
+ headers: { "Content-Type": "application/json", ...headers },
24
+ body: JSON.stringify({ params: {} }),
25
+ });
26
+ if (!res.ok) {
27
+ throw new HttpError(502, `query execution failed: ${res.status}`);
28
+ }
29
+
30
+ const { columns, rows } = await res.json();
31
+ return Response.json({ columns, rows });
32
+ }
33
+ ```
34
+
35
+ `baseUrl` already includes the version prefix and has no trailing slash, so append paths directly.
36
+ Spread `headers` into the request headers.
37
+
38
+ ## Running a saved Query
39
+
40
+ A Query is addressed by its id alone: `POST {baseUrl}/queries/{queryId}`. The Connection is
41
+ resolved from the Query, so never put a connection id in the path — pass it to `connection`.
42
+
43
+ **SQL Queries** take their arguments in `params` and return `{ columns, rows }`:
44
+
45
+ ```ts
46
+ body: JSON.stringify({ params: { since: "2026-01-01" } });
47
+ // → { columns: [{ name, type? }], rows: [{ ... }] }
48
+ ```
49
+
50
+ **HTTP Queries** take the shape the Query itself declares — not wrapped in `params` — with
51
+ query-string values on the URL, and pass the upstream service's response straight through
52
+ (including non-2xx, so check `res.ok`):
53
+
54
+ ```ts
55
+ const res = await fetch(`${baseUrl}/queries/${QUERY_ID}?since=2026-01-01`, {
56
+ method: "POST",
57
+ headers: { "Content-Type": "application/json", ...headers },
58
+ body: JSON.stringify({ customerId: "c_123" }),
59
+ });
60
+ ```
61
+
62
+ ## Notes
63
+
64
+ - The same code works in a preview and in a deployed app. Outside Squadbase, `connection` throws.
65
+ - Credentials, upstream URLs, and database connection details belong to the Connection and the
66
+ Query. A handler never holds them, and never inlines SQL or imports a database client.
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Vantage の API ハンドラが受け取るコンテキストのうち、この SDK が読む部分。
3
+ * `ApiContext` はこの形を満たすのでそのまま渡せる。
4
+ */
5
+ interface RequestContext {
6
+ request: Request;
7
+ env?: Record<string, string | undefined>;
8
+ }
9
+ interface Connection {
10
+ /** コネクションを読むエンドポイントのベース URL。末尾にスラッシュは付かない。 */
11
+ baseUrl: string;
12
+ /** 認証とルーティングのために、このコネクション宛のリクエストへ載せるヘッダー。 */
13
+ headers: Record<string, string>;
14
+ }
15
+ /**
16
+ * コネクションを読むための宛先と認証情報を解決する。リクエストの送信は呼び出し側が行う。
17
+ *
18
+ * ```ts
19
+ * const { baseUrl, headers } = await connection(ctx, CONNECTION_ID)
20
+ * const res = await fetch(`${baseUrl}/queries/${QUERY_ID}`, {
21
+ * method: "POST",
22
+ * headers: { "Content-Type": "application/json", ...headers },
23
+ * body: JSON.stringify({ params: {} }),
24
+ * })
25
+ * ```
26
+ */
27
+ declare function connection(ctx: RequestContext, connectionId: string): Promise<Connection>;
28
+
29
+ export { type Connection, type RequestContext, connection };
package/dist/server.js ADDED
@@ -0,0 +1,38 @@
1
+ // src/server/connection.ts
2
+ var CREDENTIAL_HEADER = "x-squadbase-machine-credential";
3
+ var CREDENTIAL_ENV = "INTERNAL_SQUADBASE_MACHINE_CREDENTIAL";
4
+ var BASE_URL_ENV = "INTERNAL_SQUADBASE_CONNECTOR_GATEWAY_URL";
5
+ var readEnv = (ctx, name) => {
6
+ const value = ctx.env?.[name] ?? process.env[name];
7
+ return value != null && value !== "" ? value : void 0;
8
+ };
9
+ var resolveCredential = (ctx) => {
10
+ const fromHeader = ctx.request.headers.get(CREDENTIAL_HEADER);
11
+ if (fromHeader != null && fromHeader !== "") {
12
+ return fromHeader;
13
+ }
14
+ return readEnv(ctx, CREDENTIAL_ENV);
15
+ };
16
+ async function connection(ctx, connectionId) {
17
+ if (!connectionId) {
18
+ throw new Error("connection() requires a connection id.");
19
+ }
20
+ const credential = resolveCredential(ctx);
21
+ const baseUrl = readEnv(ctx, BASE_URL_ENV);
22
+ if (credential == null || baseUrl == null) {
23
+ throw new Error(
24
+ "Connections are unavailable here. Run this app in a Squadbase preview or a deployed project."
25
+ );
26
+ }
27
+ return {
28
+ baseUrl: baseUrl.replace(/\/+$/, ""),
29
+ headers: {
30
+ Authorization: `Bearer ${credential}`,
31
+ // コネクション単位で同じ worker へ振り分けさせ、接続プールを使い回す。
32
+ "x-tenant": connectionId
33
+ }
34
+ };
35
+ }
36
+ export {
37
+ connection
38
+ };
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "@squadbase/vantage-sdk",
3
+ "version": "0.1.0-dev.0",
4
+ "description": "Server-side SDK for Squadbase Vantage apps",
5
+ "type": "module",
6
+ "exports": {
7
+ "./server": {
8
+ "import": "./dist/server.js",
9
+ "types": "./dist/server.d.ts"
10
+ }
11
+ },
12
+ "files": [
13
+ "dist"
14
+ ],
15
+ "publishConfig": {
16
+ "access": "public",
17
+ "registry": "https://registry.npmjs.org"
18
+ },
19
+ "scripts": {
20
+ "build": "tsup",
21
+ "type-check": "tsc",
22
+ "test": "vitest run"
23
+ },
24
+ "author": "Squadbase",
25
+ "license": "MIT",
26
+ "devDependencies": {
27
+ "@types/node": "^22.15.0",
28
+ "tsup": "^8.4.0",
29
+ "typescript": "~5.7.0",
30
+ "vitest": "^4.1.6"
31
+ }
32
+ }