@xata.io/client 0.0.0-alpha.vf05617f → 0.0.0-alpha.vf38f30b

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/index.mjs CHANGED
@@ -43,6 +43,14 @@ async function getGitBranch() {
43
43
  }
44
44
  }
45
45
 
46
+ function getAPIKey() {
47
+ try {
48
+ return getEnvVariable("XATA_API_KEY") ?? XATA_API_KEY;
49
+ } catch (err) {
50
+ return void 0;
51
+ }
52
+ }
53
+
46
54
  function getFetchImplementation(userFetch) {
47
55
  const globalFetch = typeof fetch !== "undefined" ? fetch : void 0;
48
56
  const fetchImpl = userFetch ?? globalFetch;
@@ -52,88 +60,34 @@ function getFetchImplementation(userFetch) {
52
60
  return fetchImpl;
53
61
  }
54
62
 
55
- const envBranchNames = [
56
- "XATA_BRANCH",
57
- "VERCEL_GIT_COMMIT_REF",
58
- "CF_PAGES_BRANCH",
59
- "BRANCH"
60
- ];
61
- const defaultBranch = "main";
62
- async function getCurrentBranchName(options) {
63
- const env = await getBranchByEnvVariable();
64
- if (env)
65
- return env;
66
- const branch = await getGitBranch();
67
- if (!branch)
68
- return defaultBranch;
69
- const details = await getDatabaseBranch(branch, options);
70
- if (details)
71
- return branch;
72
- return defaultBranch;
73
- }
74
- async function getCurrentBranchDetails(options) {
75
- const env = await getBranchByEnvVariable();
76
- if (env)
77
- return getDatabaseBranch(env, options);
78
- const branch = await getGitBranch();
79
- if (!branch)
80
- return getDatabaseBranch(defaultBranch, options);
81
- const details = await getDatabaseBranch(branch, options);
82
- if (details)
83
- return details;
84
- return getDatabaseBranch(defaultBranch, options);
85
- }
86
- async function getDatabaseBranch(branch, options) {
87
- const databaseURL = options?.databaseURL || getDatabaseURL();
88
- const apiKey = options?.apiKey || getAPIKey();
89
- if (!databaseURL)
90
- throw new Error("A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely");
91
- if (!apiKey)
92
- throw new Error("An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely");
93
- const [protocol, , host, , database] = databaseURL.split("/");
94
- const [workspace] = host.split(".");
95
- const dbBranchName = `${database}:${branch}`;
96
- try {
97
- return await getBranchDetails({
98
- apiKey,
99
- apiUrl: databaseURL,
100
- fetchImpl: getFetchImplementation(options?.fetchImpl),
101
- workspacesApiUrl: `${protocol}//${host}`,
102
- pathParams: {
103
- dbBranchName,
104
- workspace
105
- }
106
- });
107
- } catch (err) {
108
- if (isObject(err) && err.status === 404)
109
- return null;
110
- throw err;
111
- }
112
- }
113
- function getBranchByEnvVariable() {
114
- for (const name of envBranchNames) {
115
- const value = getEnvVariable(name);
116
- if (value) {
117
- return value;
63
+ class FetcherError extends Error {
64
+ constructor(status, data) {
65
+ super(getMessage(data));
66
+ this.status = status;
67
+ this.errors = isBulkError(data) ? data.errors : void 0;
68
+ if (data instanceof Error) {
69
+ this.stack = data.stack;
70
+ this.cause = data.cause;
118
71
  }
119
72
  }
120
- try {
121
- return XATA_BRANCH;
122
- } catch (err) {
123
- }
124
73
  }
125
- function getDatabaseURL() {
126
- try {
127
- return getEnvVariable("XATA_DATABASE_URL") ?? XATA_DATABASE_URL;
128
- } catch (err) {
129
- return void 0;
130
- }
74
+ function isBulkError(error) {
75
+ return isObject(error) && Array.isArray(error.errors);
131
76
  }
132
- function getAPIKey() {
133
- try {
134
- return getEnvVariable("XATA_API_KEY") ?? XATA_API_KEY;
135
- } catch (err) {
136
- return void 0;
77
+ function isErrorWithMessage(error) {
78
+ return isObject(error) && isString(error.message);
79
+ }
80
+ function getMessage(data) {
81
+ if (data instanceof Error) {
82
+ return data.message;
83
+ } else if (isString(data)) {
84
+ return data;
85
+ } else if (isErrorWithMessage(data)) {
86
+ return data.message;
87
+ } else if (isBulkError(data)) {
88
+ return "Bulk operation failed";
89
+ } else {
90
+ return "Unexpected error";
137
91
  }
138
92
  }
139
93
 
@@ -191,33 +145,20 @@ async function fetch$1({
191
145
  if (response.ok) {
192
146
  return jsonResponse;
193
147
  }
194
- const { message = "Unknown error", errors } = jsonResponse;
195
- throw new FetcherError({ message, status: response.status, errors });
148
+ throw new FetcherError(response.status, jsonResponse);
196
149
  } catch (error) {
197
- const message = hasMessage(error) ? error.message : "Unknown network error";
198
- const parent = error instanceof Error ? error : void 0;
199
- throw new FetcherError({ message, status: response.status }, parent);
200
- }
201
- }
202
- const hasMessage = (error) => {
203
- return isObject(error) && isString(error.message);
204
- };
205
- class FetcherError extends Error {
206
- constructor(data, parent) {
207
- super(data.message);
208
- this.status = data.status;
209
- this.errors = data.errors;
210
- if (parent) {
211
- this.stack = parent.stack;
212
- this.cause = parent.cause;
213
- }
150
+ throw new FetcherError(response.status, error);
214
151
  }
215
152
  }
216
153
 
217
154
  const getUser = (variables) => fetch$1({ url: "/user", method: "get", ...variables });
218
155
  const updateUser = (variables) => fetch$1({ url: "/user", method: "put", ...variables });
219
156
  const deleteUser = (variables) => fetch$1({ url: "/user", method: "delete", ...variables });
220
- const getUserAPIKeys = (variables) => fetch$1({ url: "/user/keys", method: "get", ...variables });
157
+ const getUserAPIKeys = (variables) => fetch$1({
158
+ url: "/user/keys",
159
+ method: "get",
160
+ ...variables
161
+ });
221
162
  const createUserAPIKey = (variables) => fetch$1({
222
163
  url: "/user/keys/{keyName}",
223
164
  method: "post",
@@ -228,8 +169,16 @@ const deleteUserAPIKey = (variables) => fetch$1({
228
169
  method: "delete",
229
170
  ...variables
230
171
  });
231
- const createWorkspace = (variables) => fetch$1({ url: "/workspaces", method: "post", ...variables });
232
- const getWorkspacesList = (variables) => fetch$1({ url: "/workspaces", method: "get", ...variables });
172
+ const createWorkspace = (variables) => fetch$1({
173
+ url: "/workspaces",
174
+ method: "post",
175
+ ...variables
176
+ });
177
+ const getWorkspacesList = (variables) => fetch$1({
178
+ url: "/workspaces",
179
+ method: "get",
180
+ ...variables
181
+ });
233
182
  const getWorkspace = (variables) => fetch$1({
234
183
  url: "/workspaces/{workspaceId}",
235
184
  method: "get",
@@ -250,21 +199,13 @@ const getWorkspaceMembersList = (variables) => fetch$1({
250
199
  method: "get",
251
200
  ...variables
252
201
  });
253
- const updateWorkspaceMemberRole = (variables) => fetch$1({
254
- url: "/workspaces/{workspaceId}/members/{userId}",
255
- method: "put",
256
- ...variables
257
- });
202
+ const updateWorkspaceMemberRole = (variables) => fetch$1({ url: "/workspaces/{workspaceId}/members/{userId}", method: "put", ...variables });
258
203
  const removeWorkspaceMember = (variables) => fetch$1({
259
204
  url: "/workspaces/{workspaceId}/members/{userId}",
260
205
  method: "delete",
261
206
  ...variables
262
207
  });
263
- const inviteWorkspaceMember = (variables) => fetch$1({
264
- url: "/workspaces/{workspaceId}/invites",
265
- method: "post",
266
- ...variables
267
- });
208
+ const inviteWorkspaceMember = (variables) => fetch$1({ url: "/workspaces/{workspaceId}/invites", method: "post", ...variables });
268
209
  const cancelWorkspaceMemberInvite = (variables) => fetch$1({
269
210
  url: "/workspaces/{workspaceId}/invites/{inviteId}",
270
211
  method: "delete",
@@ -326,16 +267,8 @@ const getBranchMetadata = (variables) => fetch$1({
326
267
  ...variables
327
268
  });
328
269
  const getBranchMigrationHistory = (variables) => fetch$1({ url: "/db/{dbBranchName}/migrations", method: "get", ...variables });
329
- const executeBranchMigrationPlan = (variables) => fetch$1({
330
- url: "/db/{dbBranchName}/migrations/execute",
331
- method: "post",
332
- ...variables
333
- });
334
- const getBranchMigrationPlan = (variables) => fetch$1({
335
- url: "/db/{dbBranchName}/migrations/plan",
336
- method: "post",
337
- ...variables
338
- });
270
+ const executeBranchMigrationPlan = (variables) => fetch$1({ url: "/db/{dbBranchName}/migrations/execute", method: "post", ...variables });
271
+ const getBranchMigrationPlan = (variables) => fetch$1({ url: "/db/{dbBranchName}/migrations/plan", method: "post", ...variables });
339
272
  const getBranchStats = (variables) => fetch$1({
340
273
  url: "/db/{dbBranchName}/stats",
341
274
  method: "get",
@@ -409,11 +342,7 @@ const getRecord = (variables) => fetch$1({
409
342
  method: "get",
410
343
  ...variables
411
344
  });
412
- const bulkInsertTableRecords = (variables) => fetch$1({
413
- url: "/db/{dbBranchName}/tables/{tableName}/bulk",
414
- method: "post",
415
- ...variables
416
- });
345
+ const bulkInsertTableRecords = (variables) => fetch$1({ url: "/db/{dbBranchName}/tables/{tableName}/bulk", method: "post", ...variables });
417
346
  const queryTable = (variables) => fetch$1({
418
347
  url: "/db/{dbBranchName}/tables/{tableName}/query",
419
348
  method: "post",
@@ -523,7 +452,7 @@ var __privateSet$4 = (obj, member, value, setter) => {
523
452
  };
524
453
  var _extraProps, _namespaces;
525
454
  class XataApiClient {
526
- constructor(options) {
455
+ constructor(options = {}) {
527
456
  __privateAdd$6(this, _extraProps, void 0);
528
457
  __privateAdd$6(this, _namespaces, {});
529
458
  const provider = options.host ?? "production";
@@ -1452,9 +1381,10 @@ var __privateAdd$2 = (obj, member, value) => {
1452
1381
  };
1453
1382
  var _tables;
1454
1383
  class SchemaPlugin extends XataPlugin {
1455
- constructor(links) {
1384
+ constructor(links, tableNames) {
1456
1385
  super();
1457
1386
  this.links = links;
1387
+ this.tableNames = tableNames;
1458
1388
  __privateAdd$2(this, _tables, {});
1459
1389
  }
1460
1390
  build(options) {
@@ -1469,6 +1399,9 @@ class SchemaPlugin extends XataPlugin {
1469
1399
  return __privateGet$1(this, _tables)[table];
1470
1400
  }
1471
1401
  });
1402
+ for (const table of this.tableNames ?? []) {
1403
+ db[table] = new RestRepository({ db, getFetchProps, table, links });
1404
+ }
1472
1405
  return db;
1473
1406
  }
1474
1407
  }
@@ -1532,6 +1465,84 @@ const isBranchStrategyBuilder = (strategy) => {
1532
1465
  return typeof strategy === "function";
1533
1466
  };
1534
1467
 
1468
+ const envBranchNames = [
1469
+ "XATA_BRANCH",
1470
+ "VERCEL_GIT_COMMIT_REF",
1471
+ "CF_PAGES_BRANCH",
1472
+ "BRANCH"
1473
+ ];
1474
+ const defaultBranch = "main";
1475
+ async function getCurrentBranchName(options) {
1476
+ const env = await getBranchByEnvVariable();
1477
+ if (env)
1478
+ return env;
1479
+ const branch = await getGitBranch();
1480
+ if (!branch)
1481
+ return defaultBranch;
1482
+ const details = await getDatabaseBranch(branch, options);
1483
+ if (details)
1484
+ return branch;
1485
+ return defaultBranch;
1486
+ }
1487
+ async function getCurrentBranchDetails(options) {
1488
+ const env = await getBranchByEnvVariable();
1489
+ if (env)
1490
+ return getDatabaseBranch(env, options);
1491
+ const branch = await getGitBranch();
1492
+ if (!branch)
1493
+ return getDatabaseBranch(defaultBranch, options);
1494
+ const details = await getDatabaseBranch(branch, options);
1495
+ if (details)
1496
+ return details;
1497
+ return getDatabaseBranch(defaultBranch, options);
1498
+ }
1499
+ async function getDatabaseBranch(branch, options) {
1500
+ const databaseURL = options?.databaseURL || getDatabaseURL();
1501
+ const apiKey = options?.apiKey || getAPIKey();
1502
+ if (!databaseURL)
1503
+ throw new Error("A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely");
1504
+ if (!apiKey)
1505
+ throw new Error("An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely");
1506
+ const [protocol, , host, , database] = databaseURL.split("/");
1507
+ const [workspace] = host.split(".");
1508
+ const dbBranchName = `${database}:${branch}`;
1509
+ try {
1510
+ return await getBranchDetails({
1511
+ apiKey,
1512
+ apiUrl: databaseURL,
1513
+ fetchImpl: getFetchImplementation(options?.fetchImpl),
1514
+ workspacesApiUrl: `${protocol}//${host}`,
1515
+ pathParams: {
1516
+ dbBranchName,
1517
+ workspace
1518
+ }
1519
+ });
1520
+ } catch (err) {
1521
+ if (isObject(err) && err.status === 404)
1522
+ return null;
1523
+ throw err;
1524
+ }
1525
+ }
1526
+ function getBranchByEnvVariable() {
1527
+ for (const name of envBranchNames) {
1528
+ const value = getEnvVariable(name);
1529
+ if (value) {
1530
+ return value;
1531
+ }
1532
+ }
1533
+ try {
1534
+ return XATA_BRANCH;
1535
+ } catch (err) {
1536
+ }
1537
+ }
1538
+ function getDatabaseURL() {
1539
+ try {
1540
+ return getEnvVariable("XATA_DATABASE_URL") ?? XATA_DATABASE_URL;
1541
+ } catch (err) {
1542
+ return void 0;
1543
+ }
1544
+ }
1545
+
1535
1546
  var __accessCheck = (obj, member, msg) => {
1536
1547
  if (!member.has(obj))
1537
1548
  throw TypeError("Cannot " + msg);
@@ -1557,13 +1568,13 @@ var __privateMethod = (obj, member, method) => {
1557
1568
  const buildClient = (plugins) => {
1558
1569
  var _branch, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
1559
1570
  return _a = class {
1560
- constructor(options = {}, links) {
1571
+ constructor(options = {}, links, tables) {
1561
1572
  __privateAdd(this, _parseOptions);
1562
1573
  __privateAdd(this, _getFetchProps);
1563
1574
  __privateAdd(this, _evaluateBranch);
1564
1575
  __privateAdd(this, _branch, void 0);
1565
1576
  const safeOptions = __privateMethod(this, _parseOptions, parseOptions_fn).call(this, options);
1566
- const db = new SchemaPlugin(links).build({ getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions) });
1577
+ const db = new SchemaPlugin(links, tables).build({ getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions) });
1567
1578
  const search = new SearchPlugin(db, links ?? {}).build({
1568
1579
  getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions)
1569
1580
  });