@squadbase/vantage-sdk 0.1.2-dev1.5721c18 → 0.1.2-dev1.5764814
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 +128 -1
- package/dist/client.d.ts +131 -0
- package/dist/client.js +213 -0
- package/dist/server.js +4 -1
- package/package.json +9 -5
package/README.md
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
# @squadbase/vantage-sdk
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
SDK for Squadbase Vantage apps.
|
|
4
|
+
|
|
5
|
+
- `@squadbase/vantage-sdk/server` — for `server/api/**` handlers (`connection`)
|
|
6
|
+
- `@squadbase/vantage-sdk/client` — for pages running in the browser (`runDefinition`, `runQuery`)
|
|
4
7
|
|
|
5
8
|
## connection
|
|
6
9
|
|
|
@@ -62,5 +65,129 @@ const res = await fetch(`${baseUrl}/queries/${QUERY_ID}?since=2026-01-01`, {
|
|
|
62
65
|
## Notes
|
|
63
66
|
|
|
64
67
|
- The same code works in a preview and in a deployed app. Outside Squadbase, `connection` throws.
|
|
68
|
+
- Call `connection` with the `ctx` of the request being handled, and send the query while handling it.
|
|
69
|
+
`headers` then carries who opened the app, so the Query's run history can show the viewer's name.
|
|
65
70
|
- Credentials, upstream URLs, and database connection details belong to the Connection and the
|
|
66
71
|
Query. A handler never holds them, and never inlines SQL or imports a database client.
|
|
72
|
+
|
|
73
|
+
## runDefinition (client)
|
|
74
|
+
|
|
75
|
+
`runDefinition` runs a function definition from a page and returns its output. It posts to
|
|
76
|
+
`/_sqcore/definitions/{definitionId}/execute` on the app's own origin with the viewer's session, so
|
|
77
|
+
the page holds no credentials.
|
|
78
|
+
|
|
79
|
+
```ts
|
|
80
|
+
import { DefinitionExecutionError, runDefinition } from "@squadbase/vantage-sdk/client";
|
|
81
|
+
|
|
82
|
+
type Row = { region: string; revenue: number };
|
|
83
|
+
|
|
84
|
+
try {
|
|
85
|
+
const rows = await runDefinition<Row[]>("3f1c9a2e-0000-4000-8000-000000000001", {
|
|
86
|
+
since: "2026-01-01",
|
|
87
|
+
});
|
|
88
|
+
} catch (error) {
|
|
89
|
+
if (error instanceof DefinitionExecutionError && error.kind === "invalidParams") {
|
|
90
|
+
// ...
|
|
91
|
+
}
|
|
92
|
+
throw error;
|
|
93
|
+
}
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
- **Write the definition id as a string literal** in the call. The build reads these calls
|
|
97
|
+
statically to list the definitions a page uses, and a published app can only run the definitions
|
|
98
|
+
on that list. An id held in a variable or built from parts, a namespace import, or passing the
|
|
99
|
+
function around as a value fails `vantage check` / `vantage build` (`UNTRACEABLE_SDK_CALL`). A value that is not a UUID is rejected before anything is sent (`kind: "invalidId"`).
|
|
100
|
+
- The output is not validated. `TOutput` is your declaration — keep it in line with the
|
|
101
|
+
definition's output.
|
|
102
|
+
- **It never retries.** Read-only definitions are retried by Squadbase when the runtime fails; do
|
|
103
|
+
not wrap the call in your own retry loop.
|
|
104
|
+
- Pass `{ signal }` as the third argument to cancel. A cancelled call rejects with the `AbortError`
|
|
105
|
+
from `fetch` as is, not with `DefinitionExecutionError`.
|
|
106
|
+
|
|
107
|
+
Every other failure is a `DefinitionExecutionError` with `kind`, `status` (HTTP status, or `null`
|
|
108
|
+
when no response came back), `errorCode` (Squadbase's error code, or `null`), and `message`:
|
|
109
|
+
|
|
110
|
+
| `kind` | Meaning |
|
|
111
|
+
|---|---|
|
|
112
|
+
| `invalidId` | The id is not a UUID (nothing was sent) |
|
|
113
|
+
| `unavailable` | Function definitions are not available in this environment |
|
|
114
|
+
| `unauthorized` | The app is private and the viewer is not signed in, or the session expired. A public app never returns this: anyone who can open it, signed in or not, runs the call as an anonymous viewer |
|
|
115
|
+
| `forbidden` | The viewer may not access this app |
|
|
116
|
+
| `notFound` | The definition does not exist or this app cannot use it |
|
|
117
|
+
| `invalidParams` | The arguments do not match the definition or are too large |
|
|
118
|
+
| `timeout` | The definition did not finish in time |
|
|
119
|
+
| `limitExceeded` | The definition exceeded an execution limit (calls, memory, ...) |
|
|
120
|
+
| `queryFailed` | A Query called by the definition failed |
|
|
121
|
+
| `definitionError` | The definition itself failed |
|
|
122
|
+
| `serverError` | Squadbase could not run the definition |
|
|
123
|
+
| `network` | No response came back (offline, connection dropped) |
|
|
124
|
+
| `unknown` | Any other response |
|
|
125
|
+
|
|
126
|
+
## runQuery (client)
|
|
127
|
+
|
|
128
|
+
`runQuery` runs a saved Query from a page and returns its columns and rows. It posts to
|
|
129
|
+
`/_sqcore/queries/{queryId}/execute` on the app's own origin with the viewer's session, so the page
|
|
130
|
+
holds no credentials and the app needs no `server/api` handler for it.
|
|
131
|
+
|
|
132
|
+
In a **public app**, `runQuery` and `runDefinition` also work for viewers who are not signed in:
|
|
133
|
+
anyone who can open the page can run the Queries and definitions the published build calls, with
|
|
134
|
+
any arguments. Only call a Query or definition from a public page if its result is fine to show to
|
|
135
|
+
anyone for every argument value.
|
|
136
|
+
|
|
137
|
+
```ts
|
|
138
|
+
import { QueryExecutionError, runQuery } from "@squadbase/vantage-sdk/client";
|
|
139
|
+
|
|
140
|
+
type Row = { region: string; revenue: number };
|
|
141
|
+
|
|
142
|
+
try {
|
|
143
|
+
const { columns, rows } = await runQuery<Row>("3f1c9a2e-0000-4000-8000-000000000002", {
|
|
144
|
+
since: "2026-01-01",
|
|
145
|
+
});
|
|
146
|
+
} catch (error) {
|
|
147
|
+
if (error instanceof QueryExecutionError && error.kind === "notCallable") {
|
|
148
|
+
// an HTTP or writing Query: call it from server/api instead
|
|
149
|
+
}
|
|
150
|
+
throw error;
|
|
151
|
+
}
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
- The result is `{ columns, rows }`: `columns` is an array of `{ name, type }`, and each row is an
|
|
155
|
+
object keyed by column name. Rows are not validated — `TRow` is your declaration.
|
|
156
|
+
- **Only read-only SQL Queries can be run from a page.** HTTP Queries and Queries that write (or
|
|
157
|
+
whose SQL cannot be confirmed to be read-only) fail with `kind: "notCallable"`; call those from a
|
|
158
|
+
`server/api` handler with `connection`.
|
|
159
|
+
- **Write the Query id as a string literal**, exactly like `runDefinition`: the build lists these
|
|
160
|
+
ids, and a published app can only run the Queries on that list (`UNTRACEABLE_SDK_CALL` otherwise).
|
|
161
|
+
When a page picks between Queries, write each id in its own call.
|
|
162
|
+
- **It never retries**, and `{ signal }` cancels the same way as `runDefinition`.
|
|
163
|
+
|
|
164
|
+
Every other failure is a `QueryExecutionError` with the same fields as `DefinitionExecutionError`:
|
|
165
|
+
|
|
166
|
+
| `kind` | Meaning |
|
|
167
|
+
|---|---|
|
|
168
|
+
| `invalidId` | The id is not a UUID (nothing was sent) |
|
|
169
|
+
| `unavailable` | Running Queries from a page is not available in this environment |
|
|
170
|
+
| `unauthorized` | The app is private and the viewer is not signed in, or the session expired. A public app never returns this: anyone who can open it, signed in or not, runs the call as an anonymous viewer |
|
|
171
|
+
| `forbidden` | The viewer may not access this app |
|
|
172
|
+
| `notFound` | The Query does not exist or this app cannot use it |
|
|
173
|
+
| `notCallable` | The Query cannot be run from a page (an HTTP Query, or one that writes) |
|
|
174
|
+
| `invalidParams` | The request was rejected |
|
|
175
|
+
| `timeout` | The Query did not finish in time |
|
|
176
|
+
| `limitExceeded` | The result is too large |
|
|
177
|
+
| `queryFailed` | The data source returned an error (details are not shown to viewers) |
|
|
178
|
+
| `serverError` | Squadbase could not run the Query |
|
|
179
|
+
| `network` | No response came back (offline, connection dropped) |
|
|
180
|
+
| `unknown` | Any other response |
|
|
181
|
+
|
|
182
|
+
The kinds both functions share are exported as `SqcoreErrorKind`.
|
|
183
|
+
|
|
184
|
+
## For maintainers
|
|
185
|
+
|
|
186
|
+
- The SDK is published to npm. Do not depend on `@squadbase/dashboard-types` at runtime (it is a
|
|
187
|
+
restricted GitHub Packages package); keep the few types the SDK needs in the SDK itself.
|
|
188
|
+
- `client` is built for the browser (`platform: "browser"` in `tsup.config.ts`) and type-checked
|
|
189
|
+
without Node types (`tsconfig.client.json`). Do not use Node built-ins or `process` under
|
|
190
|
+
`src/client/`.
|
|
191
|
+
- Calls to `/_sqcore/*` share `src/client/http.ts` (same-origin POST, no retries, the UUID check,
|
|
192
|
+
the shared error kinds and the status-based fallback). The mapping from each endpoint's error
|
|
193
|
+
codes to `kind` lives next to each call (`src/client/definition.ts`, `src/client/query.ts`).
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 失敗の理由の種類のうち、`runDefinition` と `runQuery` に共通のもの。
|
|
3
|
+
*
|
|
4
|
+
* - `invalidId`: ID が UUID の形をしていない(送る前に断る)
|
|
5
|
+
* - `unavailable`: この環境では使えない
|
|
6
|
+
* - `unauthorized`: 閲覧者のセッションが無い・切れている
|
|
7
|
+
* - `forbidden`: このアプリへのアクセスが許されていない
|
|
8
|
+
* - `notFound`: 無い、またはこのアプリからは使えない(区別して返さない)
|
|
9
|
+
* - `invalidParams`: 引数が受け付けられない
|
|
10
|
+
* - `timeout`: 時間内に終わらなかった
|
|
11
|
+
* - `limitExceeded`: 上限(応答の大きさ・呼び出しの回数・メモリなど)を超えた
|
|
12
|
+
* - `queryFailed`: 保存クエリ(データソースでの実行)が失敗した
|
|
13
|
+
* - `serverError`: Squadbase 側の問題で実行できなかった
|
|
14
|
+
* - `network`: 応答が返らなかった(オフライン・接続の切断など)
|
|
15
|
+
* - `unknown`: 上のどれにも当てはまらない応答
|
|
16
|
+
*/
|
|
17
|
+
type SqcoreErrorKind = "invalidId" | "unavailable" | "unauthorized" | "forbidden" | "notFound" | "invalidParams" | "timeout" | "limitExceeded" | "queryFailed" | "serverError" | "network" | "unknown";
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* 関数の定義の実行が失敗した理由の種類。`runQuery` と共通のもの(`SqcoreErrorKind`)に加えて:
|
|
21
|
+
*
|
|
22
|
+
* - `definitionError`: 定義の中で失敗した(例外・宣言していないものの呼び出し・参照先が消えている)
|
|
23
|
+
*
|
|
24
|
+
* 共通のもののうち、`notFound` は「定義が無い、またはこのアプリからは使えない」、`invalidParams` は
|
|
25
|
+
* 「引数が定義の宣言と合わない・大きすぎる」、`queryFailed` は「定義から呼んだ保存クエリが失敗した」。
|
|
26
|
+
*/
|
|
27
|
+
type DefinitionExecutionErrorKind = SqcoreErrorKind | "definitionError";
|
|
28
|
+
/** `runDefinition` が投げるエラー。中断(AbortSignal)だけは、このクラスにせずそのまま投げる */
|
|
29
|
+
declare class DefinitionExecutionError extends Error {
|
|
30
|
+
name: string;
|
|
31
|
+
readonly kind: DefinitionExecutionErrorKind;
|
|
32
|
+
/** HTTP ステータス。応答が返らなかった・送る前に断ったときは null */
|
|
33
|
+
readonly status: number | null;
|
|
34
|
+
/** backend のエラーコード(応答の `errorCode`。例: `executionTimeout`)。無ければ null */
|
|
35
|
+
readonly errorCode: string | null;
|
|
36
|
+
constructor(init: {
|
|
37
|
+
kind: DefinitionExecutionErrorKind;
|
|
38
|
+
message: string;
|
|
39
|
+
status?: number | null;
|
|
40
|
+
errorCode?: string | null;
|
|
41
|
+
cause?: unknown;
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
type RunDefinitionOptions = {
|
|
45
|
+
/** 中断に使う。中断されたときは `fetch` の AbortError をそのまま投げる */
|
|
46
|
+
signal?: AbortSignal;
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* 関数の定義を動かして、出力を返す。画面(ブラウザ)から呼ぶ。
|
|
50
|
+
*
|
|
51
|
+
* ```ts
|
|
52
|
+
* const rows = await runDefinition<Row[]>("3f1c9a2e-0000-4000-8000-000000000001", {
|
|
53
|
+
* since: "2026-01-01",
|
|
54
|
+
* });
|
|
55
|
+
* ```
|
|
56
|
+
*
|
|
57
|
+
* - `definitionId` は**文字列リテラルで直接書く**。ビルドがこの呼び出しを静的に読み、画面から呼ぶ
|
|
58
|
+
* 定義の一覧を作るため、変数や組み立てた文字列で渡すと一覧から漏れる
|
|
59
|
+
* - アプリと同じオリジンの `/_sqcore/definitions/:definitionId/execute` へ、閲覧者のセッションで送る
|
|
60
|
+
* - 失敗しても**やり直さない**。読み取りのやり直しは backend が行う
|
|
61
|
+
* - 失敗は `DefinitionExecutionError`(`kind` で理由を見分ける)。`signal` で中断したときだけは、
|
|
62
|
+
* AbortError をそのまま投げる
|
|
63
|
+
*
|
|
64
|
+
* 出力の型は検査しない。`TOutput` は呼ぶ側の宣言で、定義の出力の宣言と合わせて書く。
|
|
65
|
+
*/
|
|
66
|
+
declare function runDefinition<TOutput = unknown>(definitionId: string, params?: Record<string, unknown>, options?: RunDefinitionOptions): Promise<TOutput>;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* 保存クエリの実行が失敗した理由の種類。`runDefinition` と共通のもの(`SqcoreErrorKind`)に加えて:
|
|
70
|
+
*
|
|
71
|
+
* - `notCallable`: 画面からは呼べない種類の保存クエリ(HTTP の保存クエリ・書き込みの SQL)。
|
|
72
|
+
* 画面から呼べるのは、読み取りと判定できる SQL の保存クエリだけ
|
|
73
|
+
*
|
|
74
|
+
* 共通のもののうち、`notFound` は「保存クエリが無い、またはこのアプリからは使えない」、
|
|
75
|
+
* `limitExceeded` は「結果が大きすぎる」、`queryFailed` は「データソースでの実行が失敗した」。
|
|
76
|
+
*/
|
|
77
|
+
type QueryExecutionErrorKind = SqcoreErrorKind | "notCallable";
|
|
78
|
+
/** `runQuery` が投げるエラー。中断(AbortSignal)だけは、このクラスにせずそのまま投げる */
|
|
79
|
+
declare class QueryExecutionError extends Error {
|
|
80
|
+
name: string;
|
|
81
|
+
readonly kind: QueryExecutionErrorKind;
|
|
82
|
+
/** HTTP ステータス。応答が返らなかった・送る前に断ったときは null */
|
|
83
|
+
readonly status: number | null;
|
|
84
|
+
/** backend のエラーコード(応答の `errorCode`。例: `executionTimeout`)。無ければ null */
|
|
85
|
+
readonly errorCode: string | null;
|
|
86
|
+
constructor(init: {
|
|
87
|
+
kind: QueryExecutionErrorKind;
|
|
88
|
+
message: string;
|
|
89
|
+
status?: number | null;
|
|
90
|
+
errorCode?: string | null;
|
|
91
|
+
cause?: unknown;
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
type RunQueryOptions = {
|
|
95
|
+
/** 中断に使う。中断されたときは `fetch` の AbortError をそのまま投げる */
|
|
96
|
+
signal?: AbortSignal;
|
|
97
|
+
};
|
|
98
|
+
/** 結果の列 */
|
|
99
|
+
type QueryColumn = {
|
|
100
|
+
name: string;
|
|
101
|
+
type: string;
|
|
102
|
+
};
|
|
103
|
+
/** 保存クエリの結果。`rows` は列名をキーにしたオブジェクトの並び */
|
|
104
|
+
type QueryResult<TRow = Record<string, unknown>> = {
|
|
105
|
+
columns: QueryColumn[];
|
|
106
|
+
rows: TRow[];
|
|
107
|
+
};
|
|
108
|
+
/**
|
|
109
|
+
* 保存クエリを実行して、列と行を返す。画面(ブラウザ)から呼ぶ。
|
|
110
|
+
*
|
|
111
|
+
* ```ts
|
|
112
|
+
* const { rows } = await runQuery<{ region: string; total: number }>(
|
|
113
|
+
* "3f1c9a2e-0000-4000-8000-000000000002",
|
|
114
|
+
* { since: "2026-01-01" },
|
|
115
|
+
* );
|
|
116
|
+
* ```
|
|
117
|
+
*
|
|
118
|
+
* - `queryId` は**文字列リテラルで直接書く**。ビルドがこの呼び出しを静的に読み、画面から呼ぶ
|
|
119
|
+
* 保存クエリの一覧を作るため、変数や組み立てた文字列で渡すと一覧から漏れる
|
|
120
|
+
* - 呼べるのは、読み取りと判定できる SQL の保存クエリだけ。HTTP の保存クエリと書き込みは
|
|
121
|
+
* `notCallable` で断られる(サーバー側の `server/api` から呼ぶ)
|
|
122
|
+
* - アプリと同じオリジンの `/_sqcore/queries/:queryId/execute` へ、閲覧者のセッションで送る
|
|
123
|
+
* - 失敗しても**やり直さない**
|
|
124
|
+
* - 失敗は `QueryExecutionError`(`kind` で理由を見分ける)。`signal` で中断したときだけは、
|
|
125
|
+
* AbortError をそのまま投げる
|
|
126
|
+
*
|
|
127
|
+
* 行の型は検査しない。`TRow` は呼ぶ側の宣言で、保存クエリの列と合わせて書く。
|
|
128
|
+
*/
|
|
129
|
+
declare function runQuery<TRow = Record<string, unknown>>(queryId: string, params?: Record<string, unknown>, options?: RunQueryOptions): Promise<QueryResult<TRow>>;
|
|
130
|
+
|
|
131
|
+
export { DefinitionExecutionError, type DefinitionExecutionErrorKind, type QueryColumn, QueryExecutionError, type QueryExecutionErrorKind, type QueryResult, type RunDefinitionOptions, type RunQueryOptions, type SqcoreErrorKind, runDefinition, runQuery };
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
// src/client/http.ts
|
|
2
|
+
async function postSqcore(path, body, signal) {
|
|
3
|
+
let response;
|
|
4
|
+
let text;
|
|
5
|
+
try {
|
|
6
|
+
response = await fetch(path, {
|
|
7
|
+
method: "POST",
|
|
8
|
+
credentials: "same-origin",
|
|
9
|
+
headers: { "content-type": "application/json" },
|
|
10
|
+
body: JSON.stringify(body),
|
|
11
|
+
signal
|
|
12
|
+
});
|
|
13
|
+
text = await response.text();
|
|
14
|
+
} catch (error) {
|
|
15
|
+
if (signal?.aborted) {
|
|
16
|
+
throw error;
|
|
17
|
+
}
|
|
18
|
+
return { type: "network", cause: error };
|
|
19
|
+
}
|
|
20
|
+
return {
|
|
21
|
+
type: "response",
|
|
22
|
+
status: response.status,
|
|
23
|
+
ok: response.ok,
|
|
24
|
+
body: parseJson(text)
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function parseJson(text) {
|
|
28
|
+
try {
|
|
29
|
+
return { json: true, value: JSON.parse(text) };
|
|
30
|
+
} catch {
|
|
31
|
+
return { json: false };
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
var UUID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
35
|
+
function isUuid(id) {
|
|
36
|
+
return UUID_PATTERN.test(id);
|
|
37
|
+
}
|
|
38
|
+
function errorBodyOf(body) {
|
|
39
|
+
if (!body.json || !isRecord(body.value)) {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
const { error, errorCode, message } = body.value;
|
|
43
|
+
return {
|
|
44
|
+
error: typeof error === "string" ? error : void 0,
|
|
45
|
+
errorCode: typeof errorCode === "string" ? errorCode : void 0,
|
|
46
|
+
message: typeof message === "string" ? message : void 0
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
function kindOfFailure(status, error, category, kinds) {
|
|
50
|
+
if (error?.error === category && error.errorCode != null) {
|
|
51
|
+
const kind = kinds[error.errorCode];
|
|
52
|
+
if (kind != null) {
|
|
53
|
+
return kind;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
if (error?.error === "request_error") {
|
|
57
|
+
return "invalidParams";
|
|
58
|
+
}
|
|
59
|
+
if (status === 401) {
|
|
60
|
+
return "unauthorized";
|
|
61
|
+
}
|
|
62
|
+
if (status === 403) {
|
|
63
|
+
return "forbidden";
|
|
64
|
+
}
|
|
65
|
+
if (status >= 500) {
|
|
66
|
+
return "serverError";
|
|
67
|
+
}
|
|
68
|
+
return "unknown";
|
|
69
|
+
}
|
|
70
|
+
function isRecord(value) {
|
|
71
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// src/client/definition.ts
|
|
75
|
+
var DefinitionExecutionError = class extends Error {
|
|
76
|
+
name = "DefinitionExecutionError";
|
|
77
|
+
kind;
|
|
78
|
+
/** HTTP ステータス。応答が返らなかった・送る前に断ったときは null */
|
|
79
|
+
status;
|
|
80
|
+
/** backend のエラーコード(応答の `errorCode`。例: `executionTimeout`)。無ければ null */
|
|
81
|
+
errorCode;
|
|
82
|
+
constructor(init) {
|
|
83
|
+
super(init.message, init.cause === void 0 ? void 0 : { cause: init.cause });
|
|
84
|
+
this.kind = init.kind;
|
|
85
|
+
this.status = init.status ?? null;
|
|
86
|
+
this.errorCode = init.errorCode ?? null;
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
async function runDefinition(definitionId, params = {}, options = {}) {
|
|
90
|
+
if (!isUuid(definitionId)) {
|
|
91
|
+
throw new DefinitionExecutionError({
|
|
92
|
+
kind: "invalidId",
|
|
93
|
+
message: `definition id must be a UUID: ${JSON.stringify(definitionId)}`
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
const result = await postSqcore(
|
|
97
|
+
`/_sqcore/definitions/${definitionId}/execute`,
|
|
98
|
+
{ params },
|
|
99
|
+
options.signal
|
|
100
|
+
);
|
|
101
|
+
if (result.type === "network") {
|
|
102
|
+
throw new DefinitionExecutionError({
|
|
103
|
+
kind: "network",
|
|
104
|
+
message: "the definition could not be reached",
|
|
105
|
+
cause: result.cause
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
const { status, ok, body } = result;
|
|
109
|
+
if (ok) {
|
|
110
|
+
if (body.json && isRecord(body.value) && "output" in body.value) {
|
|
111
|
+
return body.value.output;
|
|
112
|
+
}
|
|
113
|
+
throw new DefinitionExecutionError({
|
|
114
|
+
kind: "unknown",
|
|
115
|
+
message: `unexpected response from the definition (status ${status})`,
|
|
116
|
+
status
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
const error = errorBodyOf(body);
|
|
120
|
+
throw new DefinitionExecutionError({
|
|
121
|
+
kind: kindOfFailure(status, error, "function_definition_error", FUNCTION_DEFINITION_ERROR_KINDS),
|
|
122
|
+
message: error?.message ?? `the definition failed (status ${status})`,
|
|
123
|
+
status,
|
|
124
|
+
errorCode: error?.errorCode ?? null
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
var FUNCTION_DEFINITION_ERROR_KINDS = {
|
|
128
|
+
featureUnavailable: "unavailable",
|
|
129
|
+
notFound: "notFound",
|
|
130
|
+
paramsTooLarge: "invalidParams",
|
|
131
|
+
executionTimeout: "timeout",
|
|
132
|
+
executionLimitExceeded: "limitExceeded",
|
|
133
|
+
executionOutOfMemory: "limitExceeded",
|
|
134
|
+
executionQueryFailed: "queryFailed",
|
|
135
|
+
executionNotDeclared: "definitionError",
|
|
136
|
+
executionDefinitionError: "definitionError",
|
|
137
|
+
definitionBroken: "definitionError",
|
|
138
|
+
executionFailed: "serverError",
|
|
139
|
+
executionNotConfigured: "serverError"
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
// src/client/query.ts
|
|
143
|
+
var QueryExecutionError = class extends Error {
|
|
144
|
+
name = "QueryExecutionError";
|
|
145
|
+
kind;
|
|
146
|
+
/** HTTP ステータス。応答が返らなかった・送る前に断ったときは null */
|
|
147
|
+
status;
|
|
148
|
+
/** backend のエラーコード(応答の `errorCode`。例: `executionTimeout`)。無ければ null */
|
|
149
|
+
errorCode;
|
|
150
|
+
constructor(init) {
|
|
151
|
+
super(init.message, init.cause === void 0 ? void 0 : { cause: init.cause });
|
|
152
|
+
this.kind = init.kind;
|
|
153
|
+
this.status = init.status ?? null;
|
|
154
|
+
this.errorCode = init.errorCode ?? null;
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
async function runQuery(queryId, params = {}, options = {}) {
|
|
158
|
+
if (!isUuid(queryId)) {
|
|
159
|
+
throw new QueryExecutionError({
|
|
160
|
+
kind: "invalidId",
|
|
161
|
+
message: `query id must be a UUID: ${JSON.stringify(queryId)}`
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
const result = await postSqcore(
|
|
165
|
+
`/_sqcore/queries/${queryId}/execute`,
|
|
166
|
+
{ params },
|
|
167
|
+
options.signal
|
|
168
|
+
);
|
|
169
|
+
if (result.type === "network") {
|
|
170
|
+
throw new QueryExecutionError({
|
|
171
|
+
kind: "network",
|
|
172
|
+
message: "the query could not be reached",
|
|
173
|
+
cause: result.cause
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
const { status, ok, body } = result;
|
|
177
|
+
if (ok) {
|
|
178
|
+
if (body.json && isRecord(body.value) && Array.isArray(body.value.rows)) {
|
|
179
|
+
const columns = body.value.columns;
|
|
180
|
+
return {
|
|
181
|
+
columns: Array.isArray(columns) ? columns : [],
|
|
182
|
+
rows: body.value.rows
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
throw new QueryExecutionError({
|
|
186
|
+
kind: "unknown",
|
|
187
|
+
message: `unexpected response from the query (status ${status})`,
|
|
188
|
+
status
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
const error = errorBodyOf(body);
|
|
192
|
+
throw new QueryExecutionError({
|
|
193
|
+
kind: kindOfFailure(status, error, "query_error", QUERY_ERROR_KINDS),
|
|
194
|
+
message: error?.message ?? `the query failed (status ${status})`,
|
|
195
|
+
status,
|
|
196
|
+
errorCode: error?.errorCode ?? null
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
var QUERY_ERROR_KINDS = {
|
|
200
|
+
featureUnavailable: "unavailable",
|
|
201
|
+
notFound: "notFound",
|
|
202
|
+
notCallableFromScreen: "notCallable",
|
|
203
|
+
executionTimeout: "timeout",
|
|
204
|
+
executionLimitExceeded: "limitExceeded",
|
|
205
|
+
executionFailed: "queryFailed",
|
|
206
|
+
executionUnavailable: "serverError"
|
|
207
|
+
};
|
|
208
|
+
export {
|
|
209
|
+
DefinitionExecutionError,
|
|
210
|
+
QueryExecutionError,
|
|
211
|
+
runDefinition,
|
|
212
|
+
runQuery
|
|
213
|
+
};
|
package/dist/server.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
var CREDENTIAL_HEADER = "x-squadbase-machine-credential";
|
|
3
3
|
var CREDENTIAL_ENV = "INTERNAL_SQUADBASE_MACHINE_CREDENTIAL";
|
|
4
4
|
var BASE_URL_ENV = "INTERNAL_SQUADBASE_CONNECTOR_GATEWAY_URL";
|
|
5
|
+
var VIEWER_ATTRIBUTION_HEADER = "x-squadbase-viewer-attribution";
|
|
5
6
|
var readEnv = (ctx, name) => {
|
|
6
7
|
const value = ctx.env?.[name] ?? process.env[name];
|
|
7
8
|
return value != null && value !== "" ? value : void 0;
|
|
@@ -24,13 +25,15 @@ async function connection(ctx, connectionId) {
|
|
|
24
25
|
"Connections are unavailable here. Run this app in a Squadbase preview or a deployed project."
|
|
25
26
|
);
|
|
26
27
|
}
|
|
28
|
+
const viewerAttribution = ctx.request.headers.get(VIEWER_ATTRIBUTION_HEADER);
|
|
27
29
|
return {
|
|
28
30
|
baseUrl: baseUrl.replace(/\/+$/, ""),
|
|
29
31
|
headers: {
|
|
30
32
|
Authorization: `Bearer ${credential}`,
|
|
31
33
|
// コネクション単位で同じ worker へ振り分けさせ、接続プールを使い回す。
|
|
32
34
|
// `x-squadbase-` 名前空間なので、gateway が上流へ転送する経路では確実に落ちる (SQPD-1985)。
|
|
33
|
-
"x-squadbase-tenant": connectionId
|
|
35
|
+
"x-squadbase-tenant": connectionId,
|
|
36
|
+
...viewerAttribution != null && viewerAttribution !== "" ? { [VIEWER_ATTRIBUTION_HEADER]: viewerAttribution } : {}
|
|
34
37
|
}
|
|
35
38
|
};
|
|
36
39
|
}
|
package/package.json
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@squadbase/vantage-sdk",
|
|
3
|
-
"version": "0.1.2-dev1.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.1.2-dev1.5764814",
|
|
4
|
+
"description": "SDK for Squadbase Vantage apps (server handlers and the browser)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
7
7
|
"./server": {
|
|
8
8
|
"import": "./dist/server.js",
|
|
9
9
|
"types": "./dist/server.d.ts"
|
|
10
|
+
},
|
|
11
|
+
"./client": {
|
|
12
|
+
"import": "./dist/client.js",
|
|
13
|
+
"types": "./dist/client.d.ts"
|
|
10
14
|
}
|
|
11
15
|
},
|
|
12
16
|
"files": [
|
|
@@ -25,10 +29,10 @@
|
|
|
25
29
|
"vite": "^8.3.4",
|
|
26
30
|
"vitest": "^5.0.5"
|
|
27
31
|
},
|
|
28
|
-
"gitHead": "
|
|
32
|
+
"gitHead": "5764814cb35fe4ce90e4266d5242413a906e3880",
|
|
29
33
|
"scripts": {
|
|
30
|
-
"build": "tsup",
|
|
31
|
-
"type-check": "tsc",
|
|
34
|
+
"build": "rm -rf dist && tsup",
|
|
35
|
+
"type-check": "tsc && tsc -p tsconfig.client.json",
|
|
32
36
|
"test": "vitest run"
|
|
33
37
|
}
|
|
34
38
|
}
|