@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/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # @xata.io/client
2
2
 
3
+ ## 0.8.4
4
+
5
+ ### Patch Changes
6
+
7
+ - dd958a4: Fix search results return type
8
+ - f5ec686: Make XataApiClientOptions optional
9
+
3
10
  ## 0.8.3
4
11
 
5
12
  ### Patch Changes
package/dist/index.cjs CHANGED
@@ -47,6 +47,14 @@ async function getGitBranch() {
47
47
  }
48
48
  }
49
49
 
50
+ function getAPIKey() {
51
+ try {
52
+ return getEnvVariable("XATA_API_KEY") ?? XATA_API_KEY;
53
+ } catch (err) {
54
+ return void 0;
55
+ }
56
+ }
57
+
50
58
  function getFetchImplementation(userFetch) {
51
59
  const globalFetch = typeof fetch !== "undefined" ? fetch : void 0;
52
60
  const fetchImpl = userFetch ?? globalFetch;
@@ -56,88 +64,34 @@ function getFetchImplementation(userFetch) {
56
64
  return fetchImpl;
57
65
  }
58
66
 
59
- const envBranchNames = [
60
- "XATA_BRANCH",
61
- "VERCEL_GIT_COMMIT_REF",
62
- "CF_PAGES_BRANCH",
63
- "BRANCH"
64
- ];
65
- const defaultBranch = "main";
66
- async function getCurrentBranchName(options) {
67
- const env = await getBranchByEnvVariable();
68
- if (env)
69
- return env;
70
- const branch = await getGitBranch();
71
- if (!branch)
72
- return defaultBranch;
73
- const details = await getDatabaseBranch(branch, options);
74
- if (details)
75
- return branch;
76
- return defaultBranch;
77
- }
78
- async function getCurrentBranchDetails(options) {
79
- const env = await getBranchByEnvVariable();
80
- if (env)
81
- return getDatabaseBranch(env, options);
82
- const branch = await getGitBranch();
83
- if (!branch)
84
- return getDatabaseBranch(defaultBranch, options);
85
- const details = await getDatabaseBranch(branch, options);
86
- if (details)
87
- return details;
88
- return getDatabaseBranch(defaultBranch, options);
89
- }
90
- async function getDatabaseBranch(branch, options) {
91
- const databaseURL = options?.databaseURL || getDatabaseURL();
92
- const apiKey = options?.apiKey || getAPIKey();
93
- if (!databaseURL)
94
- throw new Error("A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely");
95
- if (!apiKey)
96
- throw new Error("An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely");
97
- const [protocol, , host, , database] = databaseURL.split("/");
98
- const [workspace] = host.split(".");
99
- const dbBranchName = `${database}:${branch}`;
100
- try {
101
- return await getBranchDetails({
102
- apiKey,
103
- apiUrl: databaseURL,
104
- fetchImpl: getFetchImplementation(options?.fetchImpl),
105
- workspacesApiUrl: `${protocol}//${host}`,
106
- pathParams: {
107
- dbBranchName,
108
- workspace
109
- }
110
- });
111
- } catch (err) {
112
- if (isObject(err) && err.status === 404)
113
- return null;
114
- throw err;
115
- }
116
- }
117
- function getBranchByEnvVariable() {
118
- for (const name of envBranchNames) {
119
- const value = getEnvVariable(name);
120
- if (value) {
121
- return value;
67
+ class FetcherError extends Error {
68
+ constructor(status, data) {
69
+ super(getMessage(data));
70
+ this.status = status;
71
+ this.errors = isBulkError(data) ? data.errors : void 0;
72
+ if (data instanceof Error) {
73
+ this.stack = data.stack;
74
+ this.cause = data.cause;
122
75
  }
123
76
  }
124
- try {
125
- return XATA_BRANCH;
126
- } catch (err) {
127
- }
128
77
  }
129
- function getDatabaseURL() {
130
- try {
131
- return getEnvVariable("XATA_DATABASE_URL") ?? XATA_DATABASE_URL;
132
- } catch (err) {
133
- return void 0;
134
- }
78
+ function isBulkError(error) {
79
+ return isObject(error) && Array.isArray(error.errors);
135
80
  }
136
- function getAPIKey() {
137
- try {
138
- return getEnvVariable("XATA_API_KEY") ?? XATA_API_KEY;
139
- } catch (err) {
140
- return void 0;
81
+ function isErrorWithMessage(error) {
82
+ return isObject(error) && isString(error.message);
83
+ }
84
+ function getMessage(data) {
85
+ if (data instanceof Error) {
86
+ return data.message;
87
+ } else if (isString(data)) {
88
+ return data;
89
+ } else if (isErrorWithMessage(data)) {
90
+ return data.message;
91
+ } else if (isBulkError(data)) {
92
+ return "Bulk operation failed";
93
+ } else {
94
+ return "Unexpected error";
141
95
  }
142
96
  }
143
97
 
@@ -195,33 +149,20 @@ async function fetch$1({
195
149
  if (response.ok) {
196
150
  return jsonResponse;
197
151
  }
198
- const { message = "Unknown error", errors } = jsonResponse;
199
- throw new FetcherError({ message, status: response.status, errors });
152
+ throw new FetcherError(response.status, jsonResponse);
200
153
  } catch (error) {
201
- const message = hasMessage(error) ? error.message : "Unknown network error";
202
- const parent = error instanceof Error ? error : void 0;
203
- throw new FetcherError({ message, status: response.status }, parent);
204
- }
205
- }
206
- const hasMessage = (error) => {
207
- return isObject(error) && isString(error.message);
208
- };
209
- class FetcherError extends Error {
210
- constructor(data, parent) {
211
- super(data.message);
212
- this.status = data.status;
213
- this.errors = data.errors;
214
- if (parent) {
215
- this.stack = parent.stack;
216
- this.cause = parent.cause;
217
- }
154
+ throw new FetcherError(response.status, error);
218
155
  }
219
156
  }
220
157
 
221
158
  const getUser = (variables) => fetch$1({ url: "/user", method: "get", ...variables });
222
159
  const updateUser = (variables) => fetch$1({ url: "/user", method: "put", ...variables });
223
160
  const deleteUser = (variables) => fetch$1({ url: "/user", method: "delete", ...variables });
224
- const getUserAPIKeys = (variables) => fetch$1({ url: "/user/keys", method: "get", ...variables });
161
+ const getUserAPIKeys = (variables) => fetch$1({
162
+ url: "/user/keys",
163
+ method: "get",
164
+ ...variables
165
+ });
225
166
  const createUserAPIKey = (variables) => fetch$1({
226
167
  url: "/user/keys/{keyName}",
227
168
  method: "post",
@@ -232,8 +173,16 @@ const deleteUserAPIKey = (variables) => fetch$1({
232
173
  method: "delete",
233
174
  ...variables
234
175
  });
235
- const createWorkspace = (variables) => fetch$1({ url: "/workspaces", method: "post", ...variables });
236
- const getWorkspacesList = (variables) => fetch$1({ url: "/workspaces", method: "get", ...variables });
176
+ const createWorkspace = (variables) => fetch$1({
177
+ url: "/workspaces",
178
+ method: "post",
179
+ ...variables
180
+ });
181
+ const getWorkspacesList = (variables) => fetch$1({
182
+ url: "/workspaces",
183
+ method: "get",
184
+ ...variables
185
+ });
237
186
  const getWorkspace = (variables) => fetch$1({
238
187
  url: "/workspaces/{workspaceId}",
239
188
  method: "get",
@@ -254,21 +203,13 @@ const getWorkspaceMembersList = (variables) => fetch$1({
254
203
  method: "get",
255
204
  ...variables
256
205
  });
257
- const updateWorkspaceMemberRole = (variables) => fetch$1({
258
- url: "/workspaces/{workspaceId}/members/{userId}",
259
- method: "put",
260
- ...variables
261
- });
206
+ const updateWorkspaceMemberRole = (variables) => fetch$1({ url: "/workspaces/{workspaceId}/members/{userId}", method: "put", ...variables });
262
207
  const removeWorkspaceMember = (variables) => fetch$1({
263
208
  url: "/workspaces/{workspaceId}/members/{userId}",
264
209
  method: "delete",
265
210
  ...variables
266
211
  });
267
- const inviteWorkspaceMember = (variables) => fetch$1({
268
- url: "/workspaces/{workspaceId}/invites",
269
- method: "post",
270
- ...variables
271
- });
212
+ const inviteWorkspaceMember = (variables) => fetch$1({ url: "/workspaces/{workspaceId}/invites", method: "post", ...variables });
272
213
  const cancelWorkspaceMemberInvite = (variables) => fetch$1({
273
214
  url: "/workspaces/{workspaceId}/invites/{inviteId}",
274
215
  method: "delete",
@@ -330,16 +271,8 @@ const getBranchMetadata = (variables) => fetch$1({
330
271
  ...variables
331
272
  });
332
273
  const getBranchMigrationHistory = (variables) => fetch$1({ url: "/db/{dbBranchName}/migrations", method: "get", ...variables });
333
- const executeBranchMigrationPlan = (variables) => fetch$1({
334
- url: "/db/{dbBranchName}/migrations/execute",
335
- method: "post",
336
- ...variables
337
- });
338
- const getBranchMigrationPlan = (variables) => fetch$1({
339
- url: "/db/{dbBranchName}/migrations/plan",
340
- method: "post",
341
- ...variables
342
- });
274
+ const executeBranchMigrationPlan = (variables) => fetch$1({ url: "/db/{dbBranchName}/migrations/execute", method: "post", ...variables });
275
+ const getBranchMigrationPlan = (variables) => fetch$1({ url: "/db/{dbBranchName}/migrations/plan", method: "post", ...variables });
343
276
  const getBranchStats = (variables) => fetch$1({
344
277
  url: "/db/{dbBranchName}/stats",
345
278
  method: "get",
@@ -413,11 +346,7 @@ const getRecord = (variables) => fetch$1({
413
346
  method: "get",
414
347
  ...variables
415
348
  });
416
- const bulkInsertTableRecords = (variables) => fetch$1({
417
- url: "/db/{dbBranchName}/tables/{tableName}/bulk",
418
- method: "post",
419
- ...variables
420
- });
349
+ const bulkInsertTableRecords = (variables) => fetch$1({ url: "/db/{dbBranchName}/tables/{tableName}/bulk", method: "post", ...variables });
421
350
  const queryTable = (variables) => fetch$1({
422
351
  url: "/db/{dbBranchName}/tables/{tableName}/query",
423
352
  method: "post",
@@ -527,7 +456,7 @@ var __privateSet$4 = (obj, member, value, setter) => {
527
456
  };
528
457
  var _extraProps, _namespaces;
529
458
  class XataApiClient {
530
- constructor(options) {
459
+ constructor(options = {}) {
531
460
  __privateAdd$6(this, _extraProps, void 0);
532
461
  __privateAdd$6(this, _namespaces, {});
533
462
  const provider = options.host ?? "production";
@@ -1456,9 +1385,10 @@ var __privateAdd$2 = (obj, member, value) => {
1456
1385
  };
1457
1386
  var _tables;
1458
1387
  class SchemaPlugin extends XataPlugin {
1459
- constructor(links) {
1388
+ constructor(links, tableNames) {
1460
1389
  super();
1461
1390
  this.links = links;
1391
+ this.tableNames = tableNames;
1462
1392
  __privateAdd$2(this, _tables, {});
1463
1393
  }
1464
1394
  build(options) {
@@ -1473,6 +1403,9 @@ class SchemaPlugin extends XataPlugin {
1473
1403
  return __privateGet$1(this, _tables)[table];
1474
1404
  }
1475
1405
  });
1406
+ for (const table of this.tableNames ?? []) {
1407
+ db[table] = new RestRepository({ db, getFetchProps, table, links });
1408
+ }
1476
1409
  return db;
1477
1410
  }
1478
1411
  }
@@ -1536,6 +1469,84 @@ const isBranchStrategyBuilder = (strategy) => {
1536
1469
  return typeof strategy === "function";
1537
1470
  };
1538
1471
 
1472
+ const envBranchNames = [
1473
+ "XATA_BRANCH",
1474
+ "VERCEL_GIT_COMMIT_REF",
1475
+ "CF_PAGES_BRANCH",
1476
+ "BRANCH"
1477
+ ];
1478
+ const defaultBranch = "main";
1479
+ async function getCurrentBranchName(options) {
1480
+ const env = await getBranchByEnvVariable();
1481
+ if (env)
1482
+ return env;
1483
+ const branch = await getGitBranch();
1484
+ if (!branch)
1485
+ return defaultBranch;
1486
+ const details = await getDatabaseBranch(branch, options);
1487
+ if (details)
1488
+ return branch;
1489
+ return defaultBranch;
1490
+ }
1491
+ async function getCurrentBranchDetails(options) {
1492
+ const env = await getBranchByEnvVariable();
1493
+ if (env)
1494
+ return getDatabaseBranch(env, options);
1495
+ const branch = await getGitBranch();
1496
+ if (!branch)
1497
+ return getDatabaseBranch(defaultBranch, options);
1498
+ const details = await getDatabaseBranch(branch, options);
1499
+ if (details)
1500
+ return details;
1501
+ return getDatabaseBranch(defaultBranch, options);
1502
+ }
1503
+ async function getDatabaseBranch(branch, options) {
1504
+ const databaseURL = options?.databaseURL || getDatabaseURL();
1505
+ const apiKey = options?.apiKey || getAPIKey();
1506
+ if (!databaseURL)
1507
+ throw new Error("A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely");
1508
+ if (!apiKey)
1509
+ throw new Error("An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely");
1510
+ const [protocol, , host, , database] = databaseURL.split("/");
1511
+ const [workspace] = host.split(".");
1512
+ const dbBranchName = `${database}:${branch}`;
1513
+ try {
1514
+ return await getBranchDetails({
1515
+ apiKey,
1516
+ apiUrl: databaseURL,
1517
+ fetchImpl: getFetchImplementation(options?.fetchImpl),
1518
+ workspacesApiUrl: `${protocol}//${host}`,
1519
+ pathParams: {
1520
+ dbBranchName,
1521
+ workspace
1522
+ }
1523
+ });
1524
+ } catch (err) {
1525
+ if (isObject(err) && err.status === 404)
1526
+ return null;
1527
+ throw err;
1528
+ }
1529
+ }
1530
+ function getBranchByEnvVariable() {
1531
+ for (const name of envBranchNames) {
1532
+ const value = getEnvVariable(name);
1533
+ if (value) {
1534
+ return value;
1535
+ }
1536
+ }
1537
+ try {
1538
+ return XATA_BRANCH;
1539
+ } catch (err) {
1540
+ }
1541
+ }
1542
+ function getDatabaseURL() {
1543
+ try {
1544
+ return getEnvVariable("XATA_DATABASE_URL") ?? XATA_DATABASE_URL;
1545
+ } catch (err) {
1546
+ return void 0;
1547
+ }
1548
+ }
1549
+
1539
1550
  var __accessCheck = (obj, member, msg) => {
1540
1551
  if (!member.has(obj))
1541
1552
  throw TypeError("Cannot " + msg);
@@ -1561,13 +1572,13 @@ var __privateMethod = (obj, member, method) => {
1561
1572
  const buildClient = (plugins) => {
1562
1573
  var _branch, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
1563
1574
  return _a = class {
1564
- constructor(options = {}, links) {
1575
+ constructor(options = {}, links, tables) {
1565
1576
  __privateAdd(this, _parseOptions);
1566
1577
  __privateAdd(this, _getFetchProps);
1567
1578
  __privateAdd(this, _evaluateBranch);
1568
1579
  __privateAdd(this, _branch, void 0);
1569
1580
  const safeOptions = __privateMethod(this, _parseOptions, parseOptions_fn).call(this, options);
1570
- const db = new SchemaPlugin(links).build({ getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions) });
1581
+ const db = new SchemaPlugin(links, tables).build({ getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions) });
1571
1582
  const search = new SearchPlugin(db, links ?? {}).build({
1572
1583
  getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions)
1573
1584
  });