@xata.io/client 0.0.0-alpha.vee88fdc → 0.0.0-alpha.vf2043e7
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 +7 -0
- package/dist/index.cjs +93 -88
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.mjs +93 -88
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
@@ -3174,7 +3174,8 @@ declare type SchemaPluginResult<Schemas extends Record<string, BaseData>> = {
|
|
3174
3174
|
declare class SchemaPlugin<Schemas extends Record<string, BaseData>> extends XataPlugin {
|
3175
3175
|
#private;
|
3176
3176
|
private links?;
|
3177
|
-
|
3177
|
+
private tableNames?;
|
3178
|
+
constructor(links?: LinkDictionary | undefined, tableNames?: string[] | undefined);
|
3178
3179
|
build(options: XataPluginOptions): SchemaPluginResult<Schemas>;
|
3179
3180
|
}
|
3180
3181
|
|
@@ -3238,6 +3239,7 @@ declare type BranchResolutionOptions = {
|
|
3238
3239
|
declare function getCurrentBranchName(options?: BranchResolutionOptions): Promise<string | undefined>;
|
3239
3240
|
declare function getCurrentBranchDetails(options?: BranchResolutionOptions): Promise<DBBranch | null>;
|
3240
3241
|
declare function getDatabaseURL(): string | undefined;
|
3242
|
+
|
3241
3243
|
declare function getAPIKey(): string | undefined;
|
3242
3244
|
|
3243
3245
|
declare class XataError extends Error {
|
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,91 +60,6 @@ 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;
|
118
|
-
}
|
119
|
-
}
|
120
|
-
try {
|
121
|
-
return XATA_BRANCH;
|
122
|
-
} catch (err) {
|
123
|
-
}
|
124
|
-
}
|
125
|
-
function getDatabaseURL() {
|
126
|
-
try {
|
127
|
-
return getEnvVariable("XATA_DATABASE_URL") ?? XATA_DATABASE_URL;
|
128
|
-
} catch (err) {
|
129
|
-
return void 0;
|
130
|
-
}
|
131
|
-
}
|
132
|
-
function getAPIKey() {
|
133
|
-
try {
|
134
|
-
return getEnvVariable("XATA_API_KEY") ?? XATA_API_KEY;
|
135
|
-
} catch (err) {
|
136
|
-
return void 0;
|
137
|
-
}
|
138
|
-
}
|
139
|
-
|
140
63
|
class FetcherError extends Error {
|
141
64
|
constructor(status, data) {
|
142
65
|
super(getMessage(data));
|
@@ -1458,9 +1381,10 @@ var __privateAdd$2 = (obj, member, value) => {
|
|
1458
1381
|
};
|
1459
1382
|
var _tables;
|
1460
1383
|
class SchemaPlugin extends XataPlugin {
|
1461
|
-
constructor(links) {
|
1384
|
+
constructor(links, tableNames) {
|
1462
1385
|
super();
|
1463
1386
|
this.links = links;
|
1387
|
+
this.tableNames = tableNames;
|
1464
1388
|
__privateAdd$2(this, _tables, {});
|
1465
1389
|
}
|
1466
1390
|
build(options) {
|
@@ -1475,6 +1399,9 @@ class SchemaPlugin extends XataPlugin {
|
|
1475
1399
|
return __privateGet$1(this, _tables)[table];
|
1476
1400
|
}
|
1477
1401
|
});
|
1402
|
+
for (const table of this.tableNames ?? []) {
|
1403
|
+
db[table] = new RestRepository({ db, getFetchProps, table, links });
|
1404
|
+
}
|
1478
1405
|
return db;
|
1479
1406
|
}
|
1480
1407
|
}
|
@@ -1538,6 +1465,84 @@ const isBranchStrategyBuilder = (strategy) => {
|
|
1538
1465
|
return typeof strategy === "function";
|
1539
1466
|
};
|
1540
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
|
+
|
1541
1546
|
var __accessCheck = (obj, member, msg) => {
|
1542
1547
|
if (!member.has(obj))
|
1543
1548
|
throw TypeError("Cannot " + msg);
|
@@ -1563,13 +1568,13 @@ var __privateMethod = (obj, member, method) => {
|
|
1563
1568
|
const buildClient = (plugins) => {
|
1564
1569
|
var _branch, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
|
1565
1570
|
return _a = class {
|
1566
|
-
constructor(options = {}, links) {
|
1571
|
+
constructor(options = {}, links, tables) {
|
1567
1572
|
__privateAdd(this, _parseOptions);
|
1568
1573
|
__privateAdd(this, _getFetchProps);
|
1569
1574
|
__privateAdd(this, _evaluateBranch);
|
1570
1575
|
__privateAdd(this, _branch, void 0);
|
1571
1576
|
const safeOptions = __privateMethod(this, _parseOptions, parseOptions_fn).call(this, options);
|
1572
|
-
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) });
|
1573
1578
|
const search = new SearchPlugin(db, links ?? {}).build({
|
1574
1579
|
getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions)
|
1575
1580
|
});
|