@treeseed/sdk 0.4.2 → 0.4.3
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/dist/d1-http.d.ts +38 -0
- package/dist/d1-http.js +95 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -0
- package/dist/platform/environment.d.ts +1 -1
- package/dist/platform/environment.js +1 -0
- package/package.json +5 -1
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { D1DatabaseLike, D1PreparedStatementLike } from './types/cloudflare.ts';
|
|
2
|
+
type D1QueryResult<T = Record<string, unknown>> = {
|
|
3
|
+
success?: boolean;
|
|
4
|
+
results?: T[];
|
|
5
|
+
meta?: Record<string, unknown>;
|
|
6
|
+
};
|
|
7
|
+
export interface CloudflareHttpD1DatabaseOptions {
|
|
8
|
+
accountId: string;
|
|
9
|
+
databaseId: string;
|
|
10
|
+
apiToken: string;
|
|
11
|
+
fetchImpl?: typeof fetch;
|
|
12
|
+
}
|
|
13
|
+
declare class CloudflareHttpD1PreparedStatement implements D1PreparedStatementLike {
|
|
14
|
+
private readonly endpoint;
|
|
15
|
+
private readonly apiToken;
|
|
16
|
+
private readonly fetchImpl;
|
|
17
|
+
private readonly query;
|
|
18
|
+
private bindings;
|
|
19
|
+
constructor(endpoint: string, apiToken: string, fetchImpl: typeof fetch, query: string);
|
|
20
|
+
bind(...values: unknown[]): this;
|
|
21
|
+
private execute;
|
|
22
|
+
run(): Promise<D1QueryResult<Record<string, unknown>>[]>;
|
|
23
|
+
all<T = Record<string, unknown>>(): Promise<{
|
|
24
|
+
results: T[];
|
|
25
|
+
}>;
|
|
26
|
+
first<T = Record<string, unknown>>(): Promise<NonNullable<T> | null>;
|
|
27
|
+
raw<T = unknown[]>(): Promise<T[]>;
|
|
28
|
+
}
|
|
29
|
+
export declare class CloudflareHttpD1Database implements D1DatabaseLike {
|
|
30
|
+
private readonly options;
|
|
31
|
+
private readonly endpoint;
|
|
32
|
+
private readonly fetchImpl;
|
|
33
|
+
constructor(options: CloudflareHttpD1DatabaseOptions);
|
|
34
|
+
prepare(query: string): CloudflareHttpD1PreparedStatement;
|
|
35
|
+
exec(query: string): Promise<D1QueryResult<Record<string, unknown>>[]>;
|
|
36
|
+
}
|
|
37
|
+
export declare function interpolateD1Query(query: string, bindings: unknown[]): string;
|
|
38
|
+
export {};
|
package/dist/d1-http.js
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
function toSqlValue(value) {
|
|
2
|
+
if (value === null || value === void 0) {
|
|
3
|
+
return "NULL";
|
|
4
|
+
}
|
|
5
|
+
if (typeof value === "number") {
|
|
6
|
+
return String(value);
|
|
7
|
+
}
|
|
8
|
+
if (typeof value === "boolean") {
|
|
9
|
+
return value ? "1" : "0";
|
|
10
|
+
}
|
|
11
|
+
return `'${String(value).replace(/'/g, "''")}'`;
|
|
12
|
+
}
|
|
13
|
+
function interpolateBindings(query, values) {
|
|
14
|
+
let result = query;
|
|
15
|
+
for (const value of values) {
|
|
16
|
+
result = result.replace(/\?/, toSqlValue(value));
|
|
17
|
+
}
|
|
18
|
+
return result;
|
|
19
|
+
}
|
|
20
|
+
class CloudflareHttpD1PreparedStatement {
|
|
21
|
+
constructor(endpoint, apiToken, fetchImpl, query) {
|
|
22
|
+
this.endpoint = endpoint;
|
|
23
|
+
this.apiToken = apiToken;
|
|
24
|
+
this.fetchImpl = fetchImpl;
|
|
25
|
+
this.query = query;
|
|
26
|
+
}
|
|
27
|
+
endpoint;
|
|
28
|
+
apiToken;
|
|
29
|
+
fetchImpl;
|
|
30
|
+
query;
|
|
31
|
+
bindings = [];
|
|
32
|
+
bind(...values) {
|
|
33
|
+
this.bindings = values;
|
|
34
|
+
return this;
|
|
35
|
+
}
|
|
36
|
+
async execute() {
|
|
37
|
+
const response = await this.fetchImpl(this.endpoint, {
|
|
38
|
+
method: "POST",
|
|
39
|
+
headers: {
|
|
40
|
+
authorization: `Bearer ${this.apiToken}`,
|
|
41
|
+
"content-type": "application/json"
|
|
42
|
+
},
|
|
43
|
+
body: JSON.stringify({
|
|
44
|
+
sql: this.query,
|
|
45
|
+
params: this.bindings
|
|
46
|
+
})
|
|
47
|
+
});
|
|
48
|
+
const payload = await response.json().catch(() => ({}));
|
|
49
|
+
if (!response.ok || payload.success === false || payload.errors?.length) {
|
|
50
|
+
const message = payload.errors?.[0]?.message || `Cloudflare D1 request failed with ${response.status}.`;
|
|
51
|
+
throw new Error(message);
|
|
52
|
+
}
|
|
53
|
+
return payload.result ?? [];
|
|
54
|
+
}
|
|
55
|
+
async run() {
|
|
56
|
+
return this.execute();
|
|
57
|
+
}
|
|
58
|
+
async all() {
|
|
59
|
+
const result = await this.execute();
|
|
60
|
+
return {
|
|
61
|
+
results: result.flatMap((entry) => entry.results ?? [])
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
async first() {
|
|
65
|
+
const { results } = await this.all();
|
|
66
|
+
return results[0] ?? null;
|
|
67
|
+
}
|
|
68
|
+
async raw() {
|
|
69
|
+
const { results } = await this.all();
|
|
70
|
+
return results.map((entry) => Object.values(entry));
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
class CloudflareHttpD1Database {
|
|
74
|
+
constructor(options) {
|
|
75
|
+
this.options = options;
|
|
76
|
+
this.endpoint = `https://api.cloudflare.com/client/v4/accounts/${options.accountId}/d1/database/${options.databaseId}/query`;
|
|
77
|
+
this.fetchImpl = options.fetchImpl ?? fetch;
|
|
78
|
+
}
|
|
79
|
+
options;
|
|
80
|
+
endpoint;
|
|
81
|
+
fetchImpl;
|
|
82
|
+
prepare(query) {
|
|
83
|
+
return new CloudflareHttpD1PreparedStatement(this.endpoint, this.options.apiToken, this.fetchImpl, query);
|
|
84
|
+
}
|
|
85
|
+
async exec(query) {
|
|
86
|
+
return this.prepare(query).run();
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
function interpolateD1Query(query, bindings) {
|
|
90
|
+
return interpolateBindings(query, bindings);
|
|
91
|
+
}
|
|
92
|
+
export {
|
|
93
|
+
CloudflareHttpD1Database,
|
|
94
|
+
interpolateD1Query
|
|
95
|
+
};
|
package/dist/index.d.ts
CHANGED
|
@@ -19,4 +19,5 @@ export type * from './operations-types.ts';
|
|
|
19
19
|
export type * from './workflow.ts';
|
|
20
20
|
export type { AgentDatabase } from './d1-store.ts';
|
|
21
21
|
export type { D1DatabaseLike, D1PreparedStatementLike } from './types/cloudflare.ts';
|
|
22
|
+
export { CloudflareHttpD1Database } from './d1-http.ts';
|
|
22
23
|
export type * from './remote.ts';
|
package/dist/index.js
CHANGED
|
@@ -47,9 +47,11 @@ import {
|
|
|
47
47
|
import { TreeseedOperationsSdk } from "./operations/runtime.js";
|
|
48
48
|
import { TreeseedWorkflowSdk } from "./workflow.js";
|
|
49
49
|
import { getTreeseedVerifyDriverStatus, runTreeseedVerifyDriver } from "./verification.js";
|
|
50
|
+
import { CloudflareHttpD1Database } from "./d1-http.js";
|
|
50
51
|
export {
|
|
51
52
|
AgentSdk,
|
|
52
53
|
BUILTIN_MODEL_REGISTRY,
|
|
54
|
+
CloudflareHttpD1Database,
|
|
53
55
|
CloudflareQueuePullClient,
|
|
54
56
|
ContentGraphRuntime,
|
|
55
57
|
DEFAULT_GRAPH_RANKING_PROVIDER,
|
|
@@ -2,7 +2,7 @@ import type { TreeseedDeployConfig, TreeseedTenantConfig } from './contracts.ts'
|
|
|
2
2
|
import { type LoadedTreeseedPluginEntry } from './plugins.ts';
|
|
3
3
|
export declare const TREESEED_ENVIRONMENT_SCOPES: readonly ["local", "staging", "prod"];
|
|
4
4
|
export declare const TREESEED_ENVIRONMENT_REQUIREMENTS: readonly ["required", "conditional", "optional"];
|
|
5
|
-
export declare const TREESEED_ENVIRONMENT_TARGETS: readonly ["local-file", "wrangler-dev-vars", "github-secret", "github-variable", "cloudflare-secret", "cloudflare-var", "railway-secret", "config-file"];
|
|
5
|
+
export declare const TREESEED_ENVIRONMENT_TARGETS: readonly ["local-file", "wrangler-dev-vars", "github-secret", "github-variable", "cloudflare-secret", "cloudflare-var", "railway-secret", "railway-var", "config-file"];
|
|
6
6
|
export declare const TREESEED_ENVIRONMENT_PURPOSES: readonly ["dev", "save", "deploy", "destroy", "config"];
|
|
7
7
|
export declare const TREESEED_ENVIRONMENT_SENSITIVITY: readonly ["secret", "plain", "derived"];
|
|
8
8
|
export type TreeseedEnvironmentScope = (typeof TREESEED_ENVIRONMENT_SCOPES)[number];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@treeseed/sdk",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.3",
|
|
4
4
|
"description": "Shared Treeseed SDK for content-backed and D1-backed object models.",
|
|
5
5
|
"license": "AGPL-3.0-only",
|
|
6
6
|
"repository": {
|
|
@@ -196,6 +196,10 @@
|
|
|
196
196
|
"types": "./dist/wrangler-d1.d.ts",
|
|
197
197
|
"default": "./dist/wrangler-d1.js"
|
|
198
198
|
},
|
|
199
|
+
"./d1-http": {
|
|
200
|
+
"types": "./dist/d1-http.d.ts",
|
|
201
|
+
"default": "./dist/d1-http.js"
|
|
202
|
+
},
|
|
199
203
|
"./stores/cursor-store": {
|
|
200
204
|
"types": "./dist/stores/cursor-store.d.ts",
|
|
201
205
|
"default": "./dist/stores/cursor-store.js"
|