@xata.io/client
Advanced tools
Comparing version 0.0.0-alpha.b8b17fa to 0.0.0-alpha.bb895a6
@@ -7,3 +7,3 @@ import type * as Types from './components'; | ||
export interface XataApiClientOptions { | ||
fetchImpl: FetchImpl; | ||
fetch: FetchImpl; | ||
apiKey: string; | ||
@@ -58,3 +58,3 @@ host?: HostProvider; | ||
getBranchDetails(workspace: Schemas.WorkspaceID, database: Schemas.DBName, branch: Schemas.BranchName): Promise<Schemas.DBBranch>; | ||
createBranch(workspace: Schemas.WorkspaceID, database: Schemas.DBName, branch: Schemas.BranchName, from?: string, options?: Types.CreateBranchRequestBody): Promise<undefined>; | ||
createBranch(workspace: Schemas.WorkspaceID, database: Schemas.DBName, branch: Schemas.BranchName, from?: string, options?: Types.CreateBranchRequestBody): Promise<void>; | ||
deleteBranch(workspace: Schemas.WorkspaceID, database: Schemas.DBName, branch: Schemas.BranchName): Promise<void>; | ||
@@ -61,0 +61,0 @@ updateBranchMetadata(workspace: Schemas.WorkspaceID, database: Schemas.DBName, branch: Schemas.BranchName, metadata?: Schemas.BranchMetadata): Promise<void>; |
@@ -22,3 +22,3 @@ "use strict"; | ||
_XataApiClient_extraProps.set(this, void 0); | ||
const fetchImpl = typeof fetch !== 'undefined' ? fetch : options.fetchImpl; | ||
const fetchImpl = typeof fetch !== 'undefined' ? fetch : options.fetch; | ||
if (!fetchImpl) { | ||
@@ -25,0 +25,0 @@ /** @todo add a link after docs exist */ |
@@ -23,14 +23,11 @@ "use strict"; | ||
const url = typeof workspacesApiUrl === 'string' ? `${workspacesApiUrl}${path}` : workspacesApiUrl(path, pathParams); | ||
// Node.js on localhost won't resolve localhost subdomains unless mapped in /etc/hosts | ||
// So, instead, we use localhost without subdomains, but will add a Host header | ||
if (typeof window === 'undefined' && url.includes('localhost:')) { | ||
return url.replace('{workspaceId}.', ''); | ||
} | ||
return url.replace('{workspaceId}', pathParams.workspace); | ||
} | ||
function hostHeaderForWorkspace(url) { | ||
// The host header is needed by Node.js on localhost. | ||
// It is ignored by fetch() in the frontend | ||
function hostHeader(url) { | ||
var _a; | ||
const pattern = /.*:\/\/(?<host>[^/]+).*/; | ||
const { groups } = (_a = pattern.exec(url)) !== null && _a !== void 0 ? _a : {}; | ||
return groups === null || groups === void 0 ? void 0 : groups.host; | ||
return (groups === null || groups === void 0 ? void 0 : groups.host) ? { Host: groups.host } : {}; | ||
} | ||
@@ -40,7 +37,10 @@ function fetch({ url: path, method, body, headers, pathParams, queryParams, fetchImpl, apiKey, apiUrl, workspacesApiUrl }) { | ||
const baseUrl = buildBaseUrl({ path, workspacesApiUrl, pathParams, apiUrl }); | ||
const url = resolveUrl(baseUrl, queryParams, pathParams); | ||
const fullUrl = resolveUrl(baseUrl, queryParams, pathParams); | ||
// Node.js on localhost won't resolve localhost subdomains unless mapped in /etc/hosts | ||
// So, instead, we use localhost without subdomains, but will add a Host header | ||
const url = fullUrl.includes('localhost') ? fullUrl.replace(/^[^.]+\./, 'http://') : fullUrl; | ||
const response = yield fetchImpl(url, { | ||
method: method.toUpperCase(), | ||
body: body ? JSON.stringify(body) : undefined, | ||
headers: Object.assign(Object.assign(Object.assign({ 'Content-Type': 'application/json' }, headers), { Authorization: `Bearer ${apiKey}` }), ((pathParams === null || pathParams === void 0 ? void 0 : pathParams.workspace) ? { Host: hostHeaderForWorkspace(url) } : {})) | ||
headers: Object.assign(Object.assign(Object.assign({ 'Content-Type': 'application/json' }, headers), hostHeader(fullUrl)), { Authorization: `Bearer ${apiKey}` }) | ||
}); | ||
@@ -47,0 +47,0 @@ // No content |
@@ -19,2 +19,3 @@ import { FetchImpl } from './api/fetcher'; | ||
abstract update(id: string, object: Partial<T>): Promise<T>; | ||
abstract upsert(id: string, object: Partial<T>): Promise<T>; | ||
abstract delete(id: string): void; | ||
@@ -26,6 +27,7 @@ abstract query<R extends XataRecord, Options extends QueryOptions<T>>(query: Query<T, R>, options: Options): Promise<Page<T, typeof options['columns'] extends SelectableColumn<T>[] ? Select<T, typeof options['columns'][number]> : R>>; | ||
constructor(client: BaseClient<any>, table: string); | ||
create(object: T): Promise<T>; | ||
create(object: Selectable<T>): Promise<T>; | ||
createMany(objects: T[]): Promise<T[]>; | ||
read(recordId: string): Promise<T | null>; | ||
update(recordId: string, object: Partial<T>): Promise<T>; | ||
upsert(recordId: string, object: Partial<T>): Promise<T>; | ||
delete(recordId: string): Promise<void>; | ||
@@ -32,0 +34,0 @@ query<R extends XataRecord, Options extends QueryOptions<T>>(query: Query<T, R>, options: Options): Promise<Page<T, typeof options['columns'] extends SelectableColumn<T>[] ? Select<T, typeof options['columns'][number]> : R>>; |
@@ -69,3 +69,14 @@ "use strict"; | ||
const record = transformObjectLinks(object); | ||
const response = yield (0, api_1.insertRecord)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f") }, body: record }, fetchProps)); | ||
const response = object.id | ||
? yield (0, api_1.insertRecordWithID)(Object.assign({ pathParams: { | ||
workspace: '{workspaceId}', | ||
dbBranchName: '{dbBranch}', | ||
tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"), | ||
recordId: object.id | ||
}, body: record }, fetchProps)) | ||
: yield (0, api_1.insertRecord)(Object.assign({ pathParams: { | ||
workspace: '{workspaceId}', | ||
dbBranchName: '{dbBranch}', | ||
tableName: __classPrivateFieldGet(this, _RestRepository_table, "f") | ||
}, body: record }, fetchProps)); | ||
const finalObject = yield this.read(response.id); | ||
@@ -101,7 +112,19 @@ if (!finalObject) { | ||
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this); | ||
const response = yield (0, api_1.insertRecordWithID)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"), recordId }, body: object }, fetchProps)); | ||
// TODO: Review this, not sure we are properly initializing the object | ||
return __classPrivateFieldGet(this, _RestRepository_client, "f").initObject(__classPrivateFieldGet(this, _RestRepository_table, "f"), response); | ||
const response = yield (0, api_1.updateRecordWithID)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"), recordId }, body: object }, fetchProps)); | ||
const item = yield this.read(response.id); | ||
if (!item) | ||
throw new Error('The server failed to save the record'); | ||
return item; | ||
}); | ||
} | ||
upsert(recordId, object) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this); | ||
const response = yield (0, api_1.upsertRecordWithID)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"), recordId }, body: object }, fetchProps)); | ||
const item = yield this.read(response.id); | ||
if (!item) | ||
throw new Error('The server failed to save the record'); | ||
return item; | ||
}); | ||
} | ||
delete(recordId) { | ||
@@ -116,3 +139,3 @@ return __awaiter(this, void 0, void 0, function* () { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const data = query.getData(); | ||
const data = query.getQueryOptions(); | ||
const body = { | ||
@@ -119,0 +142,0 @@ filter: Object.values(data.filter).some(Boolean) ? data.filter : undefined, |
@@ -9,3 +9,12 @@ import { XataRecord } from '..'; | ||
}; | ||
export declare class Page<T extends XataRecord, R extends XataRecord> { | ||
export interface Paginable<T extends XataRecord, R extends XataRecord = T> { | ||
meta: PaginationQueryMeta; | ||
records: R[]; | ||
nextPage(size?: number, offset?: number): Promise<Page<T, R>>; | ||
previousPage(size?: number, offset?: number): Promise<Page<T, R>>; | ||
firstPage(size?: number, offset?: number): Promise<Page<T, R>>; | ||
lastPage(size?: number, offset?: number): Promise<Page<T, R>>; | ||
hasNextPage(): boolean; | ||
} | ||
export declare class Page<T extends XataRecord, R extends XataRecord> implements Paginable<T, R> { | ||
#private; | ||
@@ -12,0 +21,0 @@ readonly meta: PaginationQueryMeta; |
import { XataRecord, Repository } from '..'; | ||
import { FilterExpression, SortExpression, PageConfig, ColumnsFilter } from '../api/schemas'; | ||
import { DeepConstraint, FilterConstraints, SortDirection, SortFilter } from './filters'; | ||
import { PaginationOptions, Page } from './pagination'; | ||
import { PaginationOptions, Page, Paginable, PaginationQueryMeta } from './pagination'; | ||
import { Selectable, SelectableColumn, Select } from './selection'; | ||
export declare type QueryOptions<T> = { | ||
export declare type QueryOptions<T extends XataRecord> = { | ||
page?: PaginationOptions; | ||
@@ -11,3 +11,3 @@ columns?: Extract<keyof Selectable<T>, string>[]; | ||
}; | ||
export declare type QueryTableData = { | ||
export declare type QueryTableOptions = { | ||
filter: FilterExpression; | ||
@@ -18,6 +18,8 @@ sort?: SortExpression; | ||
}; | ||
export declare class Query<T extends XataRecord, R extends XataRecord = T> { | ||
export declare class Query<T extends XataRecord, R extends XataRecord = T> implements Paginable<T, R> { | ||
#private; | ||
constructor(repository: Repository<T> | null, table: string, data: Partial<QueryTableData>, parent?: Partial<QueryTableData>); | ||
getData(): QueryTableData; | ||
readonly meta: PaginationQueryMeta; | ||
readonly records: R[]; | ||
constructor(repository: Repository<T> | null, table: string, data: Partial<QueryTableOptions>, parent?: Partial<QueryTableOptions>); | ||
getQueryOptions(): QueryTableOptions; | ||
any(...queries: Query<T, R>[]): Query<T, R>; | ||
@@ -41,4 +43,6 @@ all(...queries: Query<T, R>[]): Query<T, R>; | ||
nextPage(size?: number, offset?: number): Promise<Page<T, R>>; | ||
previousPage(size?: number, offset?: number): Promise<Page<T, R>>; | ||
firstPage(size?: number, offset?: number): Promise<Page<T, R>>; | ||
lastPage(size?: number, offset?: number): Promise<Page<T, R>>; | ||
hasNextPage(): boolean; | ||
} |
@@ -51,2 +51,5 @@ "use strict"; | ||
_Query_data.set(this, { filter: {} }); | ||
// Implements pagination | ||
this.meta = { page: { cursor: 'start', more: true } }; | ||
this.records = []; | ||
__classPrivateFieldSet(this, _Query_table, table, "f"); | ||
@@ -74,19 +77,19 @@ if (repository) { | ||
} | ||
getData() { | ||
getQueryOptions() { | ||
return __classPrivateFieldGet(this, _Query_data, "f"); | ||
} | ||
any(...queries) { | ||
const $any = (0, lang_1.compact)(queries.map((query) => query.getData().filter.$any)).flat(); | ||
const $any = (0, lang_1.compact)(queries.map((query) => query.getQueryOptions().filter.$any)).flat(); | ||
return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $any } }, __classPrivateFieldGet(this, _Query_data, "f")); | ||
} | ||
all(...queries) { | ||
const $all = (0, lang_1.compact)(queries.map((query) => query.getData().filter.$all)).flat(); | ||
const $all = (0, lang_1.compact)(queries.map((query) => query.getQueryOptions().filter.$all)).flat(); | ||
return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $all } }, __classPrivateFieldGet(this, _Query_data, "f")); | ||
} | ||
not(...queries) { | ||
const $not = (0, lang_1.compact)(queries.map((query) => query.getData().filter.$not)).flat(); | ||
const $not = (0, lang_1.compact)(queries.map((query) => query.getQueryOptions().filter.$not)).flat(); | ||
return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $not } }, __classPrivateFieldGet(this, _Query_data, "f")); | ||
} | ||
none(...queries) { | ||
const $none = (0, lang_1.compact)(queries.map((query) => query.getData().filter.$none)).flat(); | ||
const $none = (0, lang_1.compact)(queries.map((query) => query.getQueryOptions().filter.$none)).flat(); | ||
return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $none } }, __classPrivateFieldGet(this, _Query_data, "f")); | ||
@@ -170,2 +173,7 @@ } | ||
} | ||
previousPage(size, offset) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return this.firstPage(size, offset); | ||
}); | ||
} | ||
firstPage(size, offset) { | ||
@@ -181,3 +189,6 @@ return __awaiter(this, void 0, void 0, function* () { | ||
} | ||
hasNextPage() { | ||
return this.meta.page.more; | ||
} | ||
} | ||
exports.Query = Query; |
import { XataRecord } from '..'; | ||
import { PartialBy } from '../util/lang'; | ||
import { StringKeys, UnionToIntersection, Values } from '../util/types'; | ||
@@ -7,9 +8,4 @@ import { Query } from './query'; | ||
}; | ||
declare type OmitQueries<T> = { | ||
[key in keyof T as T[key] extends Query<any> ? never : key]: T[key]; | ||
}; | ||
declare type OmitMethods<T> = { | ||
[key in keyof T as T[key] extends Function ? never : key]: T[key]; | ||
}; | ||
export declare type Selectable<T> = Omit<OmitQueries<OmitMethods<T>>, 'id' | 'xata'>; | ||
declare type InternalProperties = 'xata' | 'read' | 'update' | 'delete'; | ||
export declare type Selectable<T extends XataRecord> = Omit<PartialBy<T, 'id'>, InternalProperties>; | ||
export declare type SelectableColumn<O> = '*' | (O extends Array<unknown> ? never : O extends Record<string, any> ? '*' | Values<{ | ||
@@ -16,0 +12,0 @@ [K in StringKeys<O>]: O[K] extends Record<string, any> ? `${K}.${SelectableColumn<O[K]>}` : K; |
export declare function compact<T>(arr: Array<T | null | undefined>): T[]; | ||
export declare type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>; |
{ | ||
"name": "@xata.io/client", | ||
"version": "0.0.0-alpha.b8b17fa", | ||
"version": "0.0.0-alpha.bb895a6", | ||
"description": "Xata.io SDK for TypeScript and JavaScript", | ||
@@ -23,3 +23,3 @@ "main": "./dist/index.js", | ||
"homepage": "https://github.com/xataio/client-ts/blob/main/client/README.md", | ||
"gitHead": "b8b17fa338fdbdc560de1ef3cc5810581e493437" | ||
"gitHead": "bb895a60519a73aa0c7ce3a00ee41f39c5e35118" | ||
} |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
173301
4105
5