@xata.io/client 0.21.1 → 0.21.3

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.d.ts CHANGED
@@ -5790,9 +5790,6 @@ type RequiredBy<T, K extends keyof T> = T & {
5790
5790
  [P in K]-?: NonNullable<T[P]>;
5791
5791
  };
5792
5792
  type GetArrayInnerType<T extends readonly any[]> = T[number];
5793
- type FunctionKeys<T> = {
5794
- [K in keyof T]: T[K] extends (...args: any) => any ? K : never;
5795
- }[keyof T];
5796
5793
  type SingleOrArray<T> = T | T[];
5797
5794
  type Dictionary<T> = Record<string, T>;
5798
5795
  type OmitBy<T, K extends keyof T> = T extends any ? Omit<T, K> : never;
@@ -6397,6 +6394,7 @@ type SummarizeResultItem<Record extends XataRecord, Expression extends Dictionar
6397
6394
 
6398
6395
  type BaseOptions<T extends XataRecord> = {
6399
6396
  columns?: SelectableColumn<T>[];
6397
+ consistency?: 'strong' | 'eventual';
6400
6398
  cache?: number;
6401
6399
  fetchOptions?: Record<string, unknown>;
6402
6400
  };
@@ -7698,14 +7696,13 @@ declare class Serializer {
7698
7696
  }
7699
7697
  declare const serialize: <T>(data: T) => string;
7700
7698
  declare const deserialize: <T>(json: string) => T;
7701
- type SerializerResult<T> = T extends Record<string, any> ? Omit<{
7702
- [K in keyof T]: SerializerResult<T[K]>;
7703
- }, FunctionKeys<T>> : T;
7699
+ type SerializerResult<T> = T extends XataRecord ? EditableData<T> : T extends any[] ? SerializerResult<T[number]>[] : T;
7704
7700
 
7705
7701
  type BranchResolutionOptions = {
7706
7702
  databaseURL?: string;
7707
7703
  apiKey?: string;
7708
7704
  fetchImpl?: FetchImpl;
7705
+ clientName?: string;
7709
7706
  };
7710
7707
  declare function getCurrentBranchName(options?: BranchResolutionOptions): Promise<string>;
7711
7708
  declare function getCurrentBranchDetails(options?: BranchResolutionOptions): Promise<DBBranch | null>;
@@ -7791,7 +7788,7 @@ type WorkerRunnerConfig = {
7791
7788
  workspace: string;
7792
7789
  worker: string;
7793
7790
  };
7794
- declare function buildWorkerRunner<XataClient>(config: WorkerRunnerConfig): <WorkerFunction extends (ctx: XataWorkerContext<XataClient>, ...args: any[]) => any>(name: string, _worker: WorkerFunction) => (...args: RemoveFirst<Parameters<WorkerFunction>>) => Promise<SerializerResult<Awaited<ReturnType<WorkerFunction>>>>;
7791
+ declare function buildWorkerRunner<XataClient>(config: WorkerRunnerConfig): <WorkerFunction extends (ctx: XataWorkerContext<XataClient>, ...args: any[]) => any>(name: string, worker: WorkerFunction) => (...args: RemoveFirst<Parameters<WorkerFunction>>) => Promise<SerializerResult<Awaited<ReturnType<WorkerFunction>>>>;
7795
7792
 
7796
7793
  declare class XataError extends Error {
7797
7794
  readonly status: number;
package/dist/index.mjs CHANGED
@@ -295,7 +295,14 @@ enqueue_fn = function(task) {
295
295
  return promise;
296
296
  };
297
297
 
298
- const VERSION = "0.21.1";
298
+ function generateUUID() {
299
+ return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
300
+ const r = Math.random() * 16 | 0, v = c == "x" ? r : r & 3 | 8;
301
+ return v.toString(16);
302
+ });
303
+ }
304
+
305
+ const VERSION = "0.21.3";
299
306
 
300
307
  class ErrorWithCause extends Error {
301
308
  constructor(message, options) {
@@ -306,7 +313,7 @@ class FetcherError extends ErrorWithCause {
306
313
  constructor(status, data, requestId) {
307
314
  super(getMessage(data));
308
315
  this.status = status;
309
- this.errors = isBulkError(data) ? data.errors : void 0;
316
+ this.errors = isBulkError(data) ? data.errors : [{ message: getMessage(data), status }];
310
317
  this.requestId = requestId;
311
318
  if (data instanceof Error) {
312
319
  this.stack = data.stack;
@@ -371,11 +378,12 @@ function hostHeader(url) {
371
378
  const { groups } = pattern.exec(url) ?? {};
372
379
  return groups?.host ? { Host: groups.host } : {};
373
380
  }
381
+ const defaultClientID = generateUUID();
374
382
  async function fetch$1({
375
383
  url: path,
376
384
  method,
377
385
  body,
378
- headers,
386
+ headers: customHeaders,
379
387
  pathParams,
380
388
  queryParams,
381
389
  fetchImpl,
@@ -406,20 +414,21 @@ async function fetch$1({
406
414
  ["version", VERSION],
407
415
  isDefined(clientName) ? ["service", clientName] : void 0
408
416
  ]).map(([key, value]) => `${key}=${value}`).join("; ");
417
+ const headers = {
418
+ "Accept-Encoding": "identity",
419
+ "Content-Type": "application/json",
420
+ "X-Xata-Client-ID": clientID ?? defaultClientID,
421
+ "X-Xata-Session-ID": sessionID ?? generateUUID(),
422
+ "X-Xata-Agent": xataAgent,
423
+ ...customHeaders,
424
+ ...hostHeader(fullUrl),
425
+ Authorization: `Bearer ${apiKey}`
426
+ };
409
427
  const response = await pool.request(url, {
410
428
  ...fetchOptions,
411
429
  method: method.toUpperCase(),
412
430
  body: body ? JSON.stringify(body) : void 0,
413
- headers: {
414
- "Accept-Encoding": "identity",
415
- "Content-Type": "application/json",
416
- "X-Xata-Client-ID": clientID ?? "",
417
- "X-Xata-Session-ID": sessionID ?? "",
418
- "X-Xata-Agent": xataAgent,
419
- ...headers,
420
- ...hostHeader(fullUrl),
421
- Authorization: `Bearer ${apiKey}`
422
- },
431
+ headers,
423
432
  signal
424
433
  });
425
434
  const { host, protocol } = parseUrl(response.url);
@@ -846,12 +855,12 @@ function parseProviderString(provider = "production") {
846
855
  function parseWorkspacesUrlParts(url) {
847
856
  if (!isString(url))
848
857
  return null;
849
- const regex = /(?:https:\/\/)?([^.]+)(?:\.([^.]+))?\.xata\.sh.*/;
850
- const regexStaging = /(?:https:\/\/)?([^.]+)\.staging(?:\.([^.]+))?\.xatabase\.co.*/;
858
+ const regex = /(?:https:\/\/)?([^.]+)(?:\.([^.]+))\.xata\.sh.*/;
859
+ const regexStaging = /(?:https:\/\/)?([^.]+)\.staging(?:\.([^.]+))\.xatabase\.co.*/;
851
860
  const match = url.match(regex) || url.match(regexStaging);
852
861
  if (!match)
853
862
  return null;
854
- return { workspace: match[1], region: match[2] ?? "eu-west-1" };
863
+ return { workspace: match[1], region: match[2] };
855
864
  }
856
865
 
857
866
  var __accessCheck$7 = (obj, member, msg) => {
@@ -880,6 +889,7 @@ class XataApiClient {
880
889
  const provider = options.host ?? "production";
881
890
  const apiKey = options.apiKey ?? getAPIKey();
882
891
  const trace = options.trace ?? defaultTrace;
892
+ const clientID = generateUUID();
883
893
  if (!apiKey) {
884
894
  throw new Error("Could not resolve a valid apiKey");
885
895
  }
@@ -889,7 +899,8 @@ class XataApiClient {
889
899
  fetchImpl: getFetchImplementation(options.fetch),
890
900
  apiKey,
891
901
  trace,
892
- clientName: options.clientName
902
+ clientName: options.clientName,
903
+ clientID
893
904
  });
894
905
  }
895
906
  get user() {
@@ -1894,13 +1905,6 @@ class XataApiPlugin {
1894
1905
  class XataPlugin {
1895
1906
  }
1896
1907
 
1897
- function generateUUID() {
1898
- return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
1899
- const r = Math.random() * 16 | 0, v = c == "x" ? r : r & 3 | 8;
1900
- return v.toString(16);
1901
- });
1902
- }
1903
-
1904
1908
  function cleanFilter(filter) {
1905
1909
  if (!filter)
1906
1910
  return void 0;
@@ -2048,6 +2052,7 @@ const _Query = class {
2048
2052
  __privateGet$5(this, _data).filter.$none = data.filter?.$none ?? parent?.filter?.$none;
2049
2053
  __privateGet$5(this, _data).sort = data.sort ?? parent?.sort;
2050
2054
  __privateGet$5(this, _data).columns = data.columns ?? parent?.columns;
2055
+ __privateGet$5(this, _data).consistency = data.consistency ?? parent?.consistency;
2051
2056
  __privateGet$5(this, _data).pagination = data.pagination ?? parent?.pagination;
2052
2057
  __privateGet$5(this, _data).cache = data.cache ?? parent?.cache;
2053
2058
  __privateGet$5(this, _data).fetchOptions = data.fetchOptions ?? parent?.fetchOptions;
@@ -2608,7 +2613,8 @@ class RestRepository extends Query {
2608
2613
  filter: cleanFilter(data.filter),
2609
2614
  sort: data.sort !== void 0 ? buildSortFilter(data.sort) : void 0,
2610
2615
  page: data.pagination,
2611
- columns: data.columns ?? ["*"]
2616
+ columns: data.columns ?? ["*"],
2617
+ consistency: data.consistency
2612
2618
  },
2613
2619
  fetchOptions: data.fetchOptions,
2614
2620
  ...fetchProps
@@ -2636,6 +2642,7 @@ class RestRepository extends Query {
2636
2642
  filter: cleanFilter(data.filter),
2637
2643
  sort: data.sort !== void 0 ? buildSortFilter(data.sort) : void 0,
2638
2644
  columns: data.columns,
2645
+ consistency: data.consistency,
2639
2646
  page: data.pagination?.size !== void 0 ? { size: data.pagination?.size } : void 0,
2640
2647
  summaries,
2641
2648
  summariesFilter
@@ -3198,12 +3205,8 @@ const isBranchStrategyBuilder = (strategy) => {
3198
3205
 
3199
3206
  async function getCurrentBranchName(options) {
3200
3207
  const { branch, envBranch } = getEnvironment();
3201
- if (branch) {
3202
- const details = await getDatabaseBranch(branch, options);
3203
- if (details)
3204
- return branch;
3205
- console.warn(`Branch ${branch} not found in Xata. Ignoring...`);
3206
- }
3208
+ if (branch)
3209
+ return branch;
3207
3210
  const gitBranch = envBranch || await getGitBranch();
3208
3211
  return resolveXataBranch(gitBranch, options);
3209
3212
  }
@@ -3235,7 +3238,8 @@ async function resolveXataBranch(gitBranch, options) {
3235
3238
  workspacesApiUrl: `${protocol}//${host}`,
3236
3239
  pathParams: { dbName, workspace, region },
3237
3240
  queryParams: { gitBranch, fallbackBranch },
3238
- trace: defaultTrace
3241
+ trace: defaultTrace,
3242
+ clientName: options?.clientName
3239
3243
  });
3240
3244
  return branch;
3241
3245
  }
@@ -3355,7 +3359,12 @@ const buildClient = (plugins) => {
3355
3359
  const cache = options?.cache ?? new SimpleCache({ defaultQueryTTL: 0 });
3356
3360
  const trace = options?.trace ?? defaultTrace;
3357
3361
  const clientName = options?.clientName;
3358
- const branch = async () => options?.branch !== void 0 ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
3362
+ const branch = async () => options?.branch !== void 0 ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({
3363
+ apiKey,
3364
+ databaseURL,
3365
+ fetchImpl: options?.fetch,
3366
+ clientName: options?.clientName
3367
+ });
3359
3368
  if (!apiKey) {
3360
3369
  throw new Error("Option apiKey is required");
3361
3370
  }
@@ -3477,7 +3486,7 @@ const deserialize = (json) => {
3477
3486
  };
3478
3487
 
3479
3488
  function buildWorkerRunner(config) {
3480
- return function xataWorker(name, _worker) {
3489
+ return function xataWorker(name, worker) {
3481
3490
  return async (...args) => {
3482
3491
  const url = process.env.NODE_ENV === "development" ? `http://localhost:64749/${name}` : `https://dispatcher.xata.workers.dev/${config.workspace}/${config.worker}/${name}`;
3483
3492
  const result = await fetch(url, {