@surf-ai/sdk 0.1.5-beta → 1.0.0-alpha.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/dist/db/index.js CHANGED
@@ -1,25 +1,46 @@
1
- // src/data/client.ts
2
- var DEFAULT_PUBLIC_URL = "https://api.ask.surf/gateway/v1";
3
- function env(surfName, legacyName) {
4
- return process.env[surfName] || process.env[legacyName];
5
- }
6
- function resolveConfig() {
7
- const proxyBase = env("SURF_SANDBOX_PROXY_BASE", "DATA_PROXY_BASE");
8
- if (proxyBase) {
9
- return { baseUrl: proxyBase, headers: {} };
10
- }
11
- const gatewayUrl = env("SURF_DEPLOYED_GATEWAY_URL", "GATEWAY_URL");
12
- const appToken = env("SURF_DEPLOYED_APP_TOKEN", "APP_TOKEN");
13
- if (gatewayUrl && appToken) {
14
- return {
15
- baseUrl: `${gatewayUrl.replace(/\/$/, "")}/gateway/v1`,
16
- headers: { Authorization: `Bearer ${appToken}` }
17
- };
1
+ // src/core/config.ts
2
+ var DEFAULT_API_BASE_URL = "https://api.ask.surf/gateway/v1";
3
+ function trimTrailingSlashes(value) {
4
+ return String(value || "").replace(/\/+$/, "");
5
+ }
6
+ function readSurfApiConfig() {
7
+ return {
8
+ baseUrl: trimTrailingSlashes(process.env.SURF_API_BASE_URL || DEFAULT_API_BASE_URL),
9
+ apiKey: process.env.SURF_API_KEY
10
+ };
11
+ }
12
+ function requireSurfApiConfig() {
13
+ const config = readSurfApiConfig();
14
+ if (!config.apiKey) {
15
+ throw new Error("SURF_API_KEY is required");
18
16
  }
19
- return { baseUrl: DEFAULT_PUBLIC_URL, headers: {} };
17
+ return { baseUrl: config.baseUrl, apiKey: config.apiKey };
18
+ }
19
+
20
+ // src/core/transport.ts
21
+ function sleep(ms) {
22
+ return new Promise((resolve) => setTimeout(resolve, ms));
20
23
  }
21
24
  function normalizePath(path) {
22
- return String(path || "").replace(/^\/+/, "").replace(/^(?:proxy\/)+/, "");
25
+ return String(path || "").replace(/^\/+/, "");
26
+ }
27
+ function buildUrl(path, params) {
28
+ const { baseUrl } = requireSurfApiConfig();
29
+ const url = new URL(`${baseUrl}/${normalizePath(path)}`);
30
+ if (params) {
31
+ for (const [key, value] of Object.entries(params)) {
32
+ if (value != null) {
33
+ url.searchParams.set(key, String(value));
34
+ }
35
+ }
36
+ }
37
+ return url.toString();
38
+ }
39
+ function buildHeaders(extra) {
40
+ const { apiKey } = requireSurfApiConfig();
41
+ const headers = new Headers(extra);
42
+ headers.set("Authorization", `Bearer ${apiKey}`);
43
+ return headers;
23
44
  }
24
45
  async function fetchJson(url, init, retries = 1) {
25
46
  for (let attempt = 0; attempt <= retries; attempt++) {
@@ -29,39 +50,44 @@ async function fetchJson(url, init, retries = 1) {
29
50
  throw new Error(`API error ${res.status}: ${text2.slice(0, 200)}`);
30
51
  }
31
52
  const text = await res.text();
32
- if (text) return JSON.parse(text);
33
- if (attempt < retries) await new Promise((r) => setTimeout(r, 1e3));
53
+ if (text) {
54
+ return JSON.parse(text);
55
+ }
56
+ if (attempt < retries) {
57
+ await sleep(1e3);
58
+ }
34
59
  }
35
60
  throw new Error(`Empty response from ${url}`);
36
61
  }
37
- async function get(path, params) {
38
- const config = resolveConfig();
39
- const cleaned = {};
40
- if (params) {
41
- for (const [k, v] of Object.entries(params)) {
42
- if (v != null) cleaned[k] = String(v);
43
- }
44
- }
45
- const qs = Object.keys(cleaned).length ? "?" + new URLSearchParams(cleaned).toString() : "";
46
- return fetchJson(`${config.baseUrl}/${normalizePath(path)}${qs}`, {
47
- headers: config.headers
62
+ async function getJson(path, params) {
63
+ return fetchJson(buildUrl(path, params), {
64
+ headers: buildHeaders()
48
65
  });
49
66
  }
50
- async function post(path, body) {
51
- const config = resolveConfig();
52
- return fetchJson(`${config.baseUrl}/${normalizePath(path)}`, {
67
+ async function postJson(path, body) {
68
+ return fetchJson(buildUrl(path), {
53
69
  method: "POST",
54
- headers: { ...config.headers, "Content-Type": "application/json" },
70
+ headers: buildHeaders({
71
+ "Content-Type": "application/json"
72
+ }),
55
73
  body: body ? JSON.stringify(body) : void 0
56
74
  });
57
75
  }
58
76
 
77
+ // src/data/client.ts
78
+ async function get(path, params) {
79
+ return getJson(path, params);
80
+ }
81
+ async function post(path, body) {
82
+ return postJson(path, body);
83
+ }
84
+
59
85
  // src/db/index.ts
60
86
  async function dbProvision() {
61
87
  return post("db/provision");
62
88
  }
63
89
  async function dbQuery(sql, params, options) {
64
- return post("db/query", { sql, params, method: options?.arrayMode ? "all" : "execute" });
90
+ return post("db/query", { sql, params, arrayMode: options?.arrayMode ?? false });
65
91
  }
66
92
  async function dbTables() {
67
93
  return get("db/tables");