@xata.io/client 0.0.0-alpha.vf05617f → 0.0.0-alpha.vf170c2c
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.cjs +133 -126
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +513 -2
- package/dist/index.mjs +133 -126
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
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
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
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
|
|
130
|
-
|
|
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
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
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
|
-
|
|
199
|
-
throw new FetcherError({ message, status: response.status, errors });
|
|
152
|
+
throw new FetcherError(response.status, jsonResponse);
|
|
200
153
|
} catch (error) {
|
|
201
|
-
|
|
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({
|
|
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({
|
|
236
|
-
|
|
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
|
-
|
|
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";
|
|
@@ -1536,6 +1465,84 @@ const isBranchStrategyBuilder = (strategy) => {
|
|
|
1536
1465
|
return typeof strategy === "function";
|
|
1537
1466
|
};
|
|
1538
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
|
+
|
|
1539
1546
|
var __accessCheck = (obj, member, msg) => {
|
|
1540
1547
|
if (!member.has(obj))
|
|
1541
1548
|
throw TypeError("Cannot " + msg);
|