diff --git a/.changeset/light-cycles-repair.md b/.changeset/light-cycles-repair.md new file mode 100644 index 000000000..61c123457 --- /dev/null +++ b/.changeset/light-cycles-repair.md @@ -0,0 +1,5 @@ +--- +"@xata.io/client": major +--- + +Make XataApiClient to use ES Proxies diff --git a/cli/src/base.ts b/cli/src/base.ts index a2921fc3e..ba3906034 100644 --- a/cli/src/base.ts +++ b/cli/src/base.ts @@ -281,7 +281,7 @@ export abstract class BaseCommand extends Command { message: 'New workspace name' }); if (!name) return this.error('No workspace name provided'); - const workspace = await xata.api.workspaces.createWorkspace({ data: { name } }); + const workspace = await xata.api.workspaces.createWorkspace({ body: { name } }); return workspace.id; } else if (workspaces.workspaces.length === 1) { const workspace = workspaces.workspaces[0].id; @@ -309,7 +309,9 @@ export abstract class BaseCommand extends Command { options: { allowCreate?: boolean } = {} ): Promise<{ name: string; region: string }> { const xata = await this.getXataClient(); - const { databases: dbs = [] } = await xata.api.database.getDatabaseList({ workspace }); + const { databases: dbs = [] } = await xata.api.databases.getDatabaseList({ + pathParams: { workspaceId: workspace } + }); if (dbs.length > 0) { const choices = dbs.map((db) => ({ @@ -355,7 +357,9 @@ export abstract class BaseCommand extends Command { } = {} ): Promise { const xata = await this.getXataClient(); - const { branches = [] } = await xata.api.branches.getBranchList({ workspace, region, database }); + const { branches = [] } = await xata.api.branch.getBranchList({ + pathParams: { workspace, region, dbName: database } + }); const EMPTY_CHOICE = '$empty'; const CREATE_CHOICE = '$create'; @@ -421,7 +425,7 @@ export abstract class BaseCommand extends Command { ); if (!name) return this.error('No database name provided'); - const { regions } = await xata.api.database.listRegions({ workspace }); + const { regions } = await xata.api.databases.listRegions({ pathParams: { workspaceId: workspace } }); const { region } = await this.prompt( { type: 'select', @@ -434,7 +438,10 @@ export abstract class BaseCommand extends Command { ); if (!region) return this.error('No region selected'); - const result = await xata.api.database.createDatabase({ workspace, database: name, data: { region } }); + const result = await xata.api.databases.createDatabase({ + pathParams: { workspaceId: workspace, dbName: name }, + body: { region } + }); return { name: result.databaseName, region }; } @@ -455,9 +462,12 @@ export abstract class BaseCommand extends Command { }); if (!from) { - await xata.api.branches.createBranch({ workspace, region, database, branch: name }); + await xata.api.branch.createBranch({ pathParams: { workspace, region, dbBranchName: `${database}:${name}` } }); } else { - await xata.api.branches.createBranch({ workspace, region, database, branch: name, from }); + await xata.api.branch.createBranch({ + pathParams: { workspace, region, dbBranchName: `${database}:${name}` }, + body: { from } + }); } return name; @@ -566,11 +576,8 @@ export abstract class BaseCommand extends Command { async deploySchema(workspace: string, region: string, database: string, branch: string, schema: Schemas.Schema) { const xata = await this.getXataClient(); const compare = await xata.api.migrations.compareBranchWithUserSchema({ - workspace, - region, - database, - branch, - schema + pathParams: { workspace, region, dbBranchName: `${database}:${branch}` }, + body: { schema } }); if (compare.edits.operations.length === 0) { @@ -587,7 +594,10 @@ export abstract class BaseCommand extends Command { }); if (!confirm) return this.exit(1); - await xata.api.migrations.applyBranchSchemaEdit({ workspace, region, database, branch, edits: compare.edits }); + await xata.api.migrations.applyBranchSchemaEdit({ + pathParams: { workspace, region, dbBranchName: `${database}:${branch}` }, + body: { edits: compare.edits } + }); } } diff --git a/cli/src/commands/branch/create.ts b/cli/src/commands/branch/create.ts index 3dac4c9c4..6d25500b7 100644 --- a/cli/src/commands/branch/create.ts +++ b/cli/src/commands/branch/create.ts @@ -32,7 +32,10 @@ export default class BranchCreate extends BaseCommand { const { from } = flags; try { - const result = await xata.api.branches.createBranch({ workspace, region, database, branch, from }); + const result = await xata.api.branch.createBranch({ + pathParams: { workspace, region, dbBranchName: `${database}:${branch}` }, + body: { from } + }); if (this.jsonEnabled()) return result; diff --git a/cli/src/commands/branch/delete.ts b/cli/src/commands/branch/delete.ts index 1f865dfed..1b230a658 100644 --- a/cli/src/commands/branch/delete.ts +++ b/cli/src/commands/branch/delete.ts @@ -37,7 +37,7 @@ export default class BranchDelete extends BaseCommand { if (!confirm) return this.exit(1); if (confirm !== branch) return this.error('The branch name did not match'); - await xata.api.branches.deleteBranch({ workspace, region, database, branch }); + await xata.api.branch.deleteBranch({ pathParams: { workspace, region, dbBranchName: `${database}:${branch}` } }); if (this.jsonEnabled()) return {}; diff --git a/cli/src/commands/branch/list.ts b/cli/src/commands/branch/list.ts index 2c32536e3..f1d3c603c 100644 --- a/cli/src/commands/branch/list.ts +++ b/cli/src/commands/branch/list.ts @@ -19,7 +19,7 @@ export default class BranchList extends BaseCommand { const { workspace, region, database } = await this.getParsedDatabaseURL(flags.db); const xata = await this.getXataClient(); - const { branches } = await xata.api.branches.getBranchList({ workspace, region, database }); + const { branches } = await xata.api.branch.getBranchList({ pathParams: { workspace, region, dbName: database } }); if (this.jsonEnabled()) return branches; diff --git a/cli/src/commands/codegen/index.ts b/cli/src/commands/codegen/index.ts index 425579618..4d8e3ad7f 100644 --- a/cli/src/commands/codegen/index.ts +++ b/cli/src/commands/codegen/index.ts @@ -87,7 +87,9 @@ export default class Codegen extends BaseCommand { flags.db, flags.branch ); - const branchDetails = await xata.api.branches.getBranchDetails({ workspace, region, database, branch }); + const branchDetails = await xata.api.branch.getBranchDetails({ + pathParams: { workspace, region, dbBranchName: `${database}:${branch}` } + }); const { schema } = branchDetails; const codegenBranch = flags['inject-branch'] ? branch : undefined; diff --git a/cli/src/commands/dbs/delete.ts b/cli/src/commands/dbs/delete.ts index 708a92bd4..a7a3302ff 100644 --- a/cli/src/commands/dbs/delete.ts +++ b/cli/src/commands/dbs/delete.ts @@ -39,7 +39,7 @@ export default class DatabasesDelete extends BaseCommand if (!confirm) return this.exit(1); if (confirm !== database) return this.error('The database name did not match'); - await xata.api.database.deleteDatabase({ workspace, database }); + await xata.api.databases.deleteDatabase({ pathParams: { workspaceId: workspace, dbName: database } }); if (this.jsonEnabled()) return {}; diff --git a/cli/src/commands/dbs/list.ts b/cli/src/commands/dbs/list.ts index bbcfdf8d5..104f7c5bf 100644 --- a/cli/src/commands/dbs/list.ts +++ b/cli/src/commands/dbs/list.ts @@ -25,7 +25,9 @@ export default class DatabasesList extends BaseCommand { (await this.getWorkspace()); const xata = await this.getXataClient(); - const { databases: dbs = [] } = await xata.api.database.getDatabaseList({ workspace }); + const { databases: dbs = [] } = await xata.api.databases.getDatabaseList({ + pathParams: { workspaceId: workspace } + }); if (this.jsonEnabled()) return dbs; diff --git a/cli/src/commands/dbs/rename.ts b/cli/src/commands/dbs/rename.ts index 0ce21e160..244ae83d2 100644 --- a/cli/src/commands/dbs/rename.ts +++ b/cli/src/commands/dbs/rename.ts @@ -43,7 +43,10 @@ export default class DatabasesRename extends BaseCommand if (!confirm) return this.exit(1); if (confirm !== database) return this.error('The database name did not match'); - await xata.api.database.renameDatabase({ workspace, database, newName }); + await xata.api.databases.renameDatabase({ + pathParams: { workspaceId: workspace, dbName: database }, + body: { newName } + }); if (this.jsonEnabled()) return {}; diff --git a/cli/src/commands/diff/index.ts b/cli/src/commands/diff/index.ts index 1ec3443a9..5139dfdac 100644 --- a/cli/src/commands/diff/index.ts +++ b/cli/src/commands/diff/index.ts @@ -39,19 +39,12 @@ export default class Diff extends BaseCommand { const apiRequest = args.branch && args.base ? xata.api.migrations.compareBranchSchemas({ - workspace, - region, - database, - branch: args.branch, - compare: args.base + pathParams: { workspace, region, dbBranchName: `${database}:${args.branch}`, branchName: args.base }, + body: {} }) : xata.api.migrations.compareBranchWithUserSchema({ - workspace, - region, - database, - branch, - schema: { tables: [] }, - schemaOperations + pathParams: { workspace, region, dbBranchName: `${database}:${branch}` }, + body: { schema: { tables: [] }, schemaOperations } }); const { diff --git a/cli/src/commands/import/csv.ts b/cli/src/commands/import/csv.ts index 9613cce09..c1d63740c 100644 --- a/cli/src/commands/import/csv.ts +++ b/cli/src/commands/import/csv.ts @@ -210,24 +210,20 @@ export default class ImportCSV extends BaseCommand { }): Promise { const xata = await this.getXataClient(); const { workspace, region, database, branch } = await this.parseDatabase(); - const { schema: existingSchema } = await xata.api.branches.getBranchDetails({ - workspace, - region, - database, - branch + const { schema: existingSchema } = await xata.api.branch.getBranchDetails({ + pathParams: { workspace, region, dbBranchName: `${database}:${branch}` } }); + const newSchema = { tables: [ ...existingSchema.tables.filter((t) => t.name !== table), { name: table, columns: columns.filter((c) => c.name !== 'id') } ] }; + const { edits } = await xata.api.migrations.compareBranchWithUserSchema({ - workspace, - region, - database, - branch: 'main', - schema: newSchema + pathParams: { workspace, region, dbBranchName: `${database}:main` }, + body: { schema: newSchema } }); if (edits.operations.length > 0) { const destructiveOperations = edits.operations @@ -267,7 +263,11 @@ export default class ImportCSV extends BaseCommand { if (!applyMigrations) { process.exit(1); } - await xata.api.migrations.applyBranchSchemaEdit({ workspace, region, database, branch, edits }); + + await xata.api.migrations.applyBranchSchemaEdit({ + pathParams: { workspace, region, dbBranchName: `${database}:${branch}` }, + body: { edits } + }); } } } diff --git a/cli/src/commands/init/index.ts b/cli/src/commands/init/index.ts index 5733624ba..9214f6b34 100644 --- a/cli/src/commands/init/index.ts +++ b/cli/src/commands/init/index.ts @@ -187,7 +187,7 @@ export default class Init extends BaseCommand { if (this.projectConfig?.codegen?.output) { const { schema: currentSchema } = await ( await this.getXataClient() - ).api.branches.getBranchDetails({ workspace, database, region, branch }); + ).api.branch.getBranchDetails({ pathParams: { workspace, region, dbBranchName: `${database}:${branch}` } }); const hasTables = currentSchema?.tables && currentSchema?.tables.length > 0; const hasColumns = currentSchema?.tables.some((t) => t.columns.length > 0); @@ -434,7 +434,7 @@ export default class Init extends BaseCommand { let retries = 0; while (retries++ < maxRetries) { try { - await xata.api.branches.getBranchList({ workspace, region, database }); + await xata.api.branch.getBranchList({ pathParams: { workspace, region, dbName: database } }); return; } catch (err) { if (err instanceof Error && err.message.includes('Invalid API key')) { diff --git a/cli/src/commands/pull/index.ts b/cli/src/commands/pull/index.ts index 650d24cd7..fd82edbfa 100644 --- a/cli/src/commands/pull/index.ts +++ b/cli/src/commands/pull/index.ts @@ -43,13 +43,12 @@ export default class Pull extends BaseCommand { ); const { logs } = await xata.api.migrations.getBranchSchemaHistory({ - workspace, - region, - database, - branch, - // TODO: Fix pagination in the API to start from last known migration and not from the beginning - // Also paginate until we get all migrations - page: { size: 200 } + pathParams: { workspace, region, dbBranchName: `${database}:${branch}` }, + body: { + // TODO: Fix pagination in the API to start from last known migration and not from the beginning + // Also paginate until we get all migrations + page: { size: 200 } + } }); if (flags.force) { diff --git a/cli/src/commands/push/index.ts b/cli/src/commands/push/index.ts index 36b01bc37..7ba31eddf 100644 --- a/cli/src/commands/push/index.ts +++ b/cli/src/commands/push/index.ts @@ -33,13 +33,12 @@ export default class Push extends BaseCommand { ); const { logs } = await xata.api.migrations.getBranchSchemaHistory({ - workspace, - region, - database, - branch, - // TODO: Fix pagination in the API to start from last known migration and not from the beginning - // Also paginate until we get all migrations - page: { size: 200 } + pathParams: { workspace, region, dbBranchName: `${database}:${branch}` }, + body: { + // TODO: Fix pagination in the API to start from last known migration and not from the beginning + // Also paginate until we get all migrations + page: { size: 200 } + } }); const localMigrationFiles = await getLocalMigrationFiles(); @@ -70,7 +69,10 @@ export default class Push extends BaseCommand { if (!confirm) return this.exit(1); // TODO: Check for errors and print them - await xata.api.migrations.pushBranchMigrations({ workspace, region, database, branch, migrations: newMigrations }); + await xata.api.migrations.pushBranchMigrations({ + pathParams: { workspace, region, dbBranchName: `${database}:${branch}` }, + body: { migrations: newMigrations } + }); this.log(`Pushed ${newMigrations.length} migrations to ${branch}`); } diff --git a/cli/src/commands/random-data/index.ts b/cli/src/commands/random-data/index.ts index 95fde3a0e..54f8c1053 100644 --- a/cli/src/commands/random-data/index.ts +++ b/cli/src/commands/random-data/index.ts @@ -29,7 +29,9 @@ export default class RandomData extends BaseCommand { const { workspace, region, database, branch } = await this.getParsedDatabaseURLWithBranch(flags.db, flags.branch); const xata = await this.getXataClient(); - const branchDetails = await xata.api.branches.getBranchDetails({ workspace, region, database, branch }); + const branchDetails = await xata.api.branch.getBranchDetails({ + pathParams: { workspace, region, dbBranchName: `${database}:${branch}` } + }); if (!branchDetails) { this.error('Could not resolve the current branch'); } @@ -51,12 +53,8 @@ export default class RandomData extends BaseCommand { const records = generateRandomData(table, totalRecords); await xata.api.records.bulkInsertTableRecords({ - workspace, - region, - database, - branch, - table: table.name, - records + pathParams: { workspace, region, dbBranchName: `${database}:${branch}`, tableName: table.name }, + body: { records: records as any[] } }); this.info( diff --git a/cli/src/commands/rebase/index.ts b/cli/src/commands/rebase/index.ts index 646b72eac..3f034c18d 100644 --- a/cli/src/commands/rebase/index.ts +++ b/cli/src/commands/rebase/index.ts @@ -36,13 +36,12 @@ export default class Rebase extends BaseCommand { this.info(`Rebase command is experimental, use with caution`); const { logs } = await xata.api.migrations.getBranchSchemaHistory({ - workspace, - region, - database, - branch, - // TODO: Fix pagination in the API to start from last known migration and not from the beginning - // Also paginate until we get all migrations - page: { size: 200 } + pathParams: { workspace, region, dbBranchName: `${database}:${branch}` }, + body: { + // TODO: Fix pagination in the API to start from last known migration and not from the beginning + // Also paginate until we get all migrations + page: { size: 200 } + } }); const remoteMigrationFiles = commitToMigrationFile(logs); diff --git a/cli/src/commands/schema/dump.ts b/cli/src/commands/schema/dump.ts index 35a45a541..e3eac0621 100644 --- a/cli/src/commands/schema/dump.ts +++ b/cli/src/commands/schema/dump.ts @@ -22,7 +22,9 @@ export default class SchemaDump extends BaseCommand { const { workspace, region, database, branch } = await this.getParsedDatabaseURLWithBranch(flags.db, flags.branch); const xata = await this.getXataClient(); - const branchDetails = await xata.api.branches.getBranchDetails({ workspace, region, database, branch }); + const branchDetails = await xata.api.branch.getBranchDetails({ + pathParams: { workspace, region, dbBranchName: `${database}:${branch}` } + }); if (!branchDetails) return this.error('Could not resolve the current branch'); if (!flags.file) { ux.styledJSON(branchDetails.schema); diff --git a/cli/src/commands/schema/edit.ts b/cli/src/commands/schema/edit.ts index 4fc20fe88..4f860663d 100644 --- a/cli/src/commands/schema/edit.ts +++ b/cli/src/commands/schema/edit.ts @@ -145,7 +145,9 @@ Beware that this can lead to ${chalk.bold( this.branch = branch; const xata = await this.getXataClient(); - const branchDetails = await xata.api.branches.getBranchDetails({ workspace, region, database, branch }); + const branchDetails = await xata.api.branch.getBranchDetails({ + pathParams: { workspace, region, dbBranchName: `${database}:${branch}` } + }); if (!branchDetails) this.error('Could not get the schema from the current branch'); if (flags.source) { @@ -806,11 +808,8 @@ vectorDimension: \${vectorDimension} } await xata.api.migrations.applyBranchSchemaEdit({ - workspace, - region, - database, - branch, - edits + pathParams: { workspace, region, dbBranchName: `${database}:${branch}` }, + body: { edits } }); this.success('Migration completed!'); diff --git a/cli/src/commands/schema/upload.ts b/cli/src/commands/schema/upload.ts index b37a37592..58ecaeac4 100644 --- a/cli/src/commands/schema/upload.ts +++ b/cli/src/commands/schema/upload.ts @@ -33,7 +33,9 @@ export default class UploadSchema extends BaseCommand { const xata = await this.getXataClient(); if (flags['create-only']) { - const { schema } = await xata.api.branches.getBranchDetails({ workspace, region, database, branch }); + const { schema } = await xata.api.branch.getBranchDetails({ + pathParams: { workspace, region, dbBranchName: `${database}:${branch}` } + }); if (schema.tables.length > 0) { this.info( 'Schema already exists. `xata schema upload --init` will only initialize the schema if it does not already exist.' @@ -48,11 +50,8 @@ export default class UploadSchema extends BaseCommand { } const { edits } = await xata.api.migrations.compareBranchWithUserSchema({ - workspace, - region, - database, - branch, - schema + pathParams: { workspace, region, dbBranchName: `${database}:${branch}` }, + body: { schema } }); if (edits.operations.length === 0) { @@ -71,6 +70,9 @@ export default class UploadSchema extends BaseCommand { }); if (!confirm) return this.exit(1); - await xata.api.migrations.applyBranchSchemaEdit({ workspace, region, database, branch, edits }); + await xata.api.migrations.applyBranchSchemaEdit({ + pathParams: { workspace, region, dbBranchName: `${database}:${branch}` }, + body: { edits } + }); } } diff --git a/cli/src/commands/shell/index.ts b/cli/src/commands/shell/index.ts index 7cf219c3f..e8c76807c 100644 --- a/cli/src/commands/shell/index.ts +++ b/cli/src/commands/shell/index.ts @@ -45,7 +45,9 @@ export default class Shell extends BaseCommand { const tempFile = path.join(__dirname, `xata-${Date.now()}.mjs`); try { const xata = await this.getXataClient(); - const branchDetails = await xata.api.branches.getBranchDetails({ workspace, region, database, branch }); + const branchDetails = await xata.api.branch.getBranchDetails({ + pathParams: { workspace, region, dbBranchName: `${database}:${branch}` } + }); const { schema } = branchDetails; const { javascript } = await generate({ language: 'javascript', databaseURL, schema }); diff --git a/cli/src/commands/workspace/create.ts b/cli/src/commands/workspace/create.ts index 0c7bf3541..de24b4960 100644 --- a/cli/src/commands/workspace/create.ts +++ b/cli/src/commands/workspace/create.ts @@ -26,7 +26,7 @@ export default class WorkspaceCreate extends BaseCommand const xata = await this.getXataClient(); - const result = await xata.api.workspaces.createWorkspace({ data: { name: workspace } }); + const result = await xata.api.workspaces.createWorkspace({ body: { name: workspace } }); if (this.jsonEnabled()) return result; diff --git a/cli/src/commands/workspace/delete.ts b/cli/src/commands/workspace/delete.ts index 02763e73a..719f2bd9b 100644 --- a/cli/src/commands/workspace/delete.ts +++ b/cli/src/commands/workspace/delete.ts @@ -35,7 +35,7 @@ export default class WorkspaceDelete extends BaseCommand if (!confirm) return this.exit(1); if (confirm !== workspace) return this.error('The workspace name did not match'); - await xata.api.workspaces.deleteWorkspace({ workspace }); + await xata.api.workspaces.deleteWorkspace({ pathParams: { workspaceId: workspace } }); if (this.jsonEnabled()) return {}; diff --git a/packages/client/src/api/client.test.ts b/packages/client/src/api/client.test.ts new file mode 100644 index 000000000..b6ac59138 --- /dev/null +++ b/packages/client/src/api/client.test.ts @@ -0,0 +1,26 @@ +import { describe, expect, test } from 'vitest'; +import { XataApiClient } from './client'; +import { operationsByTag } from './components'; + +const xata = new XataApiClient({ apiKey: 'fake-api-key' }); + +describe('API Proxy types', () => { + test('returns functions for all defined namespace operations', () => { + for (const namespace of Object.keys(operationsByTag)) { + const operationsInNamespace = operationsByTag[namespace as keyof typeof operationsByTag]; + for (const operation of Object.keys(operationsInNamespace)) { + expect(operationsInNamespace[operation as keyof typeof operationsInNamespace]).toBeInstanceOf(Function); + } + } + }); + + test('returns undefined for undefined namespaces', () => { + // @ts-expect-error Not a valid namespace + expect(xata.undefinedNamespace).toBeUndefined(); + }); + + test('returns undefined for undefined namespace operations', () => { + // @ts-expect-error Not a valid operation + expect(xata.authentication.undefinedOperation).toBeUndefined(); + }); +}); diff --git a/packages/client/src/api/client.ts b/packages/client/src/api/client.ts index 1b629c51e..67eb2c989 100644 --- a/packages/client/src/api/client.ts +++ b/packages/client/src/api/client.ts @@ -1,14 +1,11 @@ import { defaultTrace, TraceFunction } from '../schema/tracing'; import { getAPIKey } from '../util/environment'; import { FetchImpl, getFetchImplementation } from '../util/fetch'; +import { RequiredKeys } from '../util/types'; import { generateUUID } from '../util/uuid'; -import type * as Components from './components'; -import type * as Types from './components'; import { operationsByTag } from './components'; import type { FetcherExtraProps } from './fetcher'; import { getHostUrl, HostProvider } from './providers'; -import type * as Responses from './responses'; -import type * as Schemas from './schemas'; export type ApiExtraProps = Omit; @@ -21,1907 +18,72 @@ export interface XataApiClientOptions { xataAgentExtra?: Record; } -export class XataApiClient { - #extraProps: ApiExtraProps; - #namespaces: Partial<{ - user: UserApi; - authentication: AuthenticationApi; - workspaces: WorkspaceApi; - invites: InvitesApi; - database: DatabaseApi; - branches: BranchApi; - migrations: MigrationsApi; - migrationRequests: MigrationRequestsApi; - tables: TableApi; - records: RecordsApi; - files: FilesApi; - searchAndFilter: SearchAndFilterApi; - }> = {}; - - constructor(options: XataApiClientOptions = {}) { - const provider = options.host ?? 'production'; - const apiKey = options.apiKey ?? getAPIKey(); - const trace = options.trace ?? defaultTrace; - const clientID = generateUUID(); - - if (!apiKey) { - throw new Error('Could not resolve a valid apiKey'); - } - - this.#extraProps = { - apiUrl: getHostUrl(provider, 'main'), - workspacesApiUrl: getHostUrl(provider, 'workspaces'), - fetch: getFetchImplementation(options.fetch), - apiKey, - trace, - clientName: options.clientName, - xataAgentExtra: options.xataAgentExtra, - clientID - }; - } - - public get user() { - if (!this.#namespaces.user) this.#namespaces.user = new UserApi(this.#extraProps); - return this.#namespaces.user; - } - - public get authentication() { - if (!this.#namespaces.authentication) this.#namespaces.authentication = new AuthenticationApi(this.#extraProps); - return this.#namespaces.authentication; - } - - public get workspaces() { - if (!this.#namespaces.workspaces) this.#namespaces.workspaces = new WorkspaceApi(this.#extraProps); - return this.#namespaces.workspaces; - } - - public get invites() { - if (!this.#namespaces.invites) this.#namespaces.invites = new InvitesApi(this.#extraProps); - return this.#namespaces.invites; - } - - public get database() { - if (!this.#namespaces.database) this.#namespaces.database = new DatabaseApi(this.#extraProps); - return this.#namespaces.database; - } - - public get branches() { - if (!this.#namespaces.branches) this.#namespaces.branches = new BranchApi(this.#extraProps); - return this.#namespaces.branches; - } - - public get migrations() { - if (!this.#namespaces.migrations) this.#namespaces.migrations = new MigrationsApi(this.#extraProps); - return this.#namespaces.migrations; - } - - public get migrationRequests() { - if (!this.#namespaces.migrationRequests) - this.#namespaces.migrationRequests = new MigrationRequestsApi(this.#extraProps); - return this.#namespaces.migrationRequests; - } - - public get tables() { - if (!this.#namespaces.tables) this.#namespaces.tables = new TableApi(this.#extraProps); - return this.#namespaces.tables; - } - - public get records() { - if (!this.#namespaces.records) this.#namespaces.records = new RecordsApi(this.#extraProps); - return this.#namespaces.records; - } - - public get files() { - if (!this.#namespaces.files) this.#namespaces.files = new FilesApi(this.#extraProps); - return this.#namespaces.files; - } - - public get searchAndFilter() { - if (!this.#namespaces.searchAndFilter) this.#namespaces.searchAndFilter = new SearchAndFilterApi(this.#extraProps); - return this.#namespaces.searchAndFilter; - } -} - -class UserApi { - constructor(private extraProps: ApiExtraProps) {} - - public getUser(): Promise { - return operationsByTag.users.getUser({ ...this.extraProps }); - } - - public updateUser({ user }: { user: Schemas.User }): Promise { - return operationsByTag.users.updateUser({ body: user, ...this.extraProps }); - } - - public deleteUser(): Promise { - return operationsByTag.users.deleteUser({ ...this.extraProps }); - } -} - -class AuthenticationApi { - constructor(private extraProps: ApiExtraProps) {} - - public getUserAPIKeys(): Promise { - return operationsByTag.authentication.getUserAPIKeys({ ...this.extraProps }); - } - - public createUserAPIKey({ name }: { name: Schemas.APIKeyName }): Promise { - return operationsByTag.authentication.createUserAPIKey({ - pathParams: { keyName: name }, - ...this.extraProps - }); - } - - public deleteUserAPIKey({ name }: { name: Schemas.APIKeyName }): Promise { - return operationsByTag.authentication.deleteUserAPIKey({ - pathParams: { keyName: name }, - ...this.extraProps - }); - } -} - -class WorkspaceApi { - constructor(private extraProps: ApiExtraProps) {} - - public getWorkspacesList(): Promise { - return operationsByTag.workspaces.getWorkspacesList({ ...this.extraProps }); - } - - public createWorkspace({ data }: { data: Schemas.WorkspaceMeta }): Promise { - return operationsByTag.workspaces.createWorkspace({ - body: data, - ...this.extraProps - }); - } - - public getWorkspace({ workspace }: { workspace: Schemas.WorkspaceID }): Promise { - return operationsByTag.workspaces.getWorkspace({ - pathParams: { workspaceId: workspace }, - ...this.extraProps - }); - } - - public updateWorkspace({ - workspace, - update - }: { - workspace: Schemas.WorkspaceID; - update: Schemas.WorkspaceMeta; - }): Promise { - return operationsByTag.workspaces.updateWorkspace({ - pathParams: { workspaceId: workspace }, - body: update, - ...this.extraProps - }); - } - - public deleteWorkspace({ workspace }: { workspace: Schemas.WorkspaceID }): Promise { - return operationsByTag.workspaces.deleteWorkspace({ - pathParams: { workspaceId: workspace }, - ...this.extraProps - }); - } - - public getWorkspaceMembersList({ workspace }: { workspace: Schemas.WorkspaceID }): Promise { - return operationsByTag.workspaces.getWorkspaceMembersList({ - pathParams: { workspaceId: workspace }, - ...this.extraProps - }); - } - - public updateWorkspaceMemberRole({ - workspace, - user, - role - }: { - workspace: Schemas.WorkspaceID; - user: Schemas.UserID; - role: Schemas.Role; - }): Promise { - return operationsByTag.workspaces.updateWorkspaceMemberRole({ - pathParams: { workspaceId: workspace, userId: user }, - body: { role }, - ...this.extraProps - }); - } - - public removeWorkspaceMember({ - workspace, - user - }: { - workspace: Schemas.WorkspaceID; - user: Schemas.UserID; - }): Promise { - return operationsByTag.workspaces.removeWorkspaceMember({ - pathParams: { workspaceId: workspace, userId: user }, - ...this.extraProps - }); - } -} - -class InvitesApi { - constructor(private extraProps: ApiExtraProps) {} - - public inviteWorkspaceMember({ - workspace, - email, - role - }: { - workspace: Schemas.WorkspaceID; - email: string; - role: Schemas.Role; - }): Promise { - return operationsByTag.invites.inviteWorkspaceMember({ - pathParams: { workspaceId: workspace }, - body: { email, role }, - ...this.extraProps - }); - } - - public updateWorkspaceMemberInvite({ - workspace, - invite, - role - }: { - workspace: Schemas.WorkspaceID; - invite: Schemas.InviteID; - role: Schemas.Role; - }): Promise { - return operationsByTag.invites.updateWorkspaceMemberInvite({ - pathParams: { workspaceId: workspace, inviteId: invite }, - body: { role }, - ...this.extraProps - }); - } - - public cancelWorkspaceMemberInvite({ - workspace, - invite - }: { - workspace: Schemas.WorkspaceID; - invite: Schemas.InviteID; - }): Promise { - return operationsByTag.invites.cancelWorkspaceMemberInvite({ - pathParams: { workspaceId: workspace, inviteId: invite }, - ...this.extraProps - }); - } - - public acceptWorkspaceMemberInvite({ - workspace, - key - }: { - workspace: Schemas.WorkspaceID; - key: Schemas.InviteKey; - }): Promise { - return operationsByTag.invites.acceptWorkspaceMemberInvite({ - pathParams: { workspaceId: workspace, inviteKey: key }, - ...this.extraProps - }); - } - - public resendWorkspaceMemberInvite({ - workspace, - invite - }: { - workspace: Schemas.WorkspaceID; - invite: Schemas.InviteID; - }): Promise { - return operationsByTag.invites.resendWorkspaceMemberInvite({ - pathParams: { workspaceId: workspace, inviteId: invite }, - ...this.extraProps - }); - } -} - -class BranchApi { - constructor(private extraProps: ApiExtraProps) {} - - public getBranchList({ - workspace, - region, - database - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - }): Promise { - return operationsByTag.branch.getBranchList({ - pathParams: { workspace, region, dbName: database }, - ...this.extraProps - }); - } - - public getBranchDetails({ - workspace, - region, - database, - branch - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - branch: Schemas.BranchName; - }): Promise { - return operationsByTag.branch.getBranchDetails({ - pathParams: { workspace, region, dbBranchName: `${database}:${branch}` }, - ...this.extraProps - }); - } - - public createBranch({ - workspace, - region, - database, - branch, - from, - metadata - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - branch: Schemas.BranchName; - from?: string; - metadata?: Schemas.BranchMetadata; - }): Promise { - return operationsByTag.branch.createBranch({ - pathParams: { workspace, region, dbBranchName: `${database}:${branch}` }, - body: { from, metadata }, - ...this.extraProps - }); - } - - public deleteBranch({ - workspace, - region, - database, - branch - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - branch: Schemas.BranchName; - }): Promise { - return operationsByTag.branch.deleteBranch({ - pathParams: { workspace, region, dbBranchName: `${database}:${branch}` }, - ...this.extraProps - }); - } - - public copyBranch({ - workspace, - region, - database, - branch, - destinationBranch, - limit - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - branch: Schemas.BranchName; - destinationBranch: Schemas.BranchName; - limit?: number; - }): Promise { - return operationsByTag.branch.copyBranch({ - pathParams: { workspace, region, dbBranchName: `${database}:${branch}` }, - body: { destinationBranch, limit }, - ...this.extraProps - }); - } - - public updateBranchMetadata({ - workspace, - region, - database, - branch, - metadata - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - branch: Schemas.BranchName; - metadata: Schemas.BranchMetadata; - }): Promise { - return operationsByTag.branch.updateBranchMetadata({ - pathParams: { workspace, region, dbBranchName: `${database}:${branch}` }, - body: metadata, - ...this.extraProps - }); - } - - public getBranchMetadata({ - workspace, - region, - database, - branch - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - branch: Schemas.BranchName; - }): Promise { - return operationsByTag.branch.getBranchMetadata({ - pathParams: { workspace, region, dbBranchName: `${database}:${branch}` }, - ...this.extraProps - }); - } - - public getBranchStats({ - workspace, - region, - database, - branch - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - branch: Schemas.BranchName; - }): Promise { - return operationsByTag.branch.getBranchStats({ - pathParams: { workspace, region, dbBranchName: `${database}:${branch}` }, - ...this.extraProps - }); - } - - public getGitBranchesMapping({ - workspace, - region, - database - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - }): Promise { - return operationsByTag.branch.getGitBranchesMapping({ - pathParams: { workspace, region, dbName: database }, - ...this.extraProps - }); - } - - public addGitBranchesEntry({ - workspace, - region, - database, - gitBranch, - xataBranch - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - gitBranch: string; - xataBranch: Schemas.BranchName; - }): Promise { - return operationsByTag.branch.addGitBranchesEntry({ - pathParams: { workspace, region, dbName: database }, - body: { gitBranch, xataBranch }, - ...this.extraProps - }); - } - - public removeGitBranchesEntry({ - workspace, - region, - database, - gitBranch - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - gitBranch: string; - }): Promise { - return operationsByTag.branch.removeGitBranchesEntry({ - pathParams: { workspace, region, dbName: database }, - queryParams: { gitBranch }, - ...this.extraProps - }); - } - - public resolveBranch({ - workspace, - region, - database, - gitBranch, - fallbackBranch - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - gitBranch?: string; - fallbackBranch?: string; - }): Promise { - return operationsByTag.branch.resolveBranch({ - pathParams: { workspace, region, dbName: database }, - queryParams: { gitBranch, fallbackBranch }, - ...this.extraProps - }); - } -} - -class TableApi { - constructor(private extraProps: ApiExtraProps) {} - - public createTable({ - workspace, - region, - database, - branch, - table - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - branch: Schemas.BranchName; - table: Schemas.TableName; - }): Promise { - return operationsByTag.table.createTable({ - pathParams: { workspace, region, dbBranchName: `${database}:${branch}`, tableName: table }, - ...this.extraProps - }); - } - - public deleteTable({ - workspace, - region, - database, - branch, - table - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - branch: Schemas.BranchName; - table: Schemas.TableName; - }): Promise { - return operationsByTag.table.deleteTable({ - pathParams: { workspace, region, dbBranchName: `${database}:${branch}`, tableName: table }, - ...this.extraProps - }); - } - - public updateTable({ - workspace, - region, - database, - branch, - table, - update - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - branch: Schemas.BranchName; - table: Schemas.TableName; - update: Types.UpdateTableRequestBody; - }): Promise { - return operationsByTag.table.updateTable({ - pathParams: { workspace, region, dbBranchName: `${database}:${branch}`, tableName: table }, - body: update, - ...this.extraProps - }); - } - - public getTableSchema({ - workspace, - region, - database, - branch, - table - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - branch: Schemas.BranchName; - table: Schemas.TableName; - }): Promise { - return operationsByTag.table.getTableSchema({ - pathParams: { workspace, region, dbBranchName: `${database}:${branch}`, tableName: table }, - ...this.extraProps - }); - } - - public setTableSchema({ - workspace, - region, - database, - branch, - table, - schema - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - branch: Schemas.BranchName; - table: Schemas.TableName; - schema: Types.SetTableSchemaRequestBody; - }): Promise { - return operationsByTag.table.setTableSchema({ - pathParams: { workspace, region, dbBranchName: `${database}:${branch}`, tableName: table }, - body: schema, - ...this.extraProps - }); - } - - public getTableColumns({ - workspace, - region, - database, - branch, - table - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - branch: Schemas.BranchName; - table: Schemas.TableName; - }): Promise { - return operationsByTag.table.getTableColumns({ - pathParams: { workspace, region, dbBranchName: `${database}:${branch}`, tableName: table }, - ...this.extraProps - }); - } - - public addTableColumn({ - workspace, - region, - database, - branch, - table, - column - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - branch: Schemas.BranchName; - table: Schemas.TableName; - column: Schemas.Column; - }): Promise { - return operationsByTag.table.addTableColumn({ - pathParams: { workspace, region, dbBranchName: `${database}:${branch}`, tableName: table }, - body: column, - ...this.extraProps - }); - } - - public getColumn({ - workspace, - region, - database, - branch, - table, - column - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - branch: Schemas.BranchName; - table: Schemas.TableName; - column: Schemas.ColumnName; - }): Promise { - return operationsByTag.table.getColumn({ - pathParams: { workspace, region, dbBranchName: `${database}:${branch}`, tableName: table, columnName: column }, - ...this.extraProps - }); - } - - public updateColumn({ - workspace, - region, - database, - branch, - table, - column, - update - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - branch: Schemas.BranchName; - table: Schemas.TableName; - column: Schemas.ColumnName; - update: Types.UpdateColumnRequestBody; - }): Promise { - return operationsByTag.table.updateColumn({ - pathParams: { workspace, region, dbBranchName: `${database}:${branch}`, tableName: table, columnName: column }, - body: update, - ...this.extraProps - }); - } - - public deleteColumn({ - workspace, - region, - database, - branch, - table, - column - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - branch: Schemas.BranchName; - table: Schemas.TableName; - column: Schemas.ColumnName; - }): Promise { - return operationsByTag.table.deleteColumn({ - pathParams: { workspace, region, dbBranchName: `${database}:${branch}`, tableName: table, columnName: column }, - ...this.extraProps - }); - } -} - -class RecordsApi { - constructor(private extraProps: ApiExtraProps) {} - - public insertRecord({ - workspace, - region, - database, - branch, - table, - record, - columns - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - branch: Schemas.BranchName; - table: Schemas.TableName; - record: Record; - columns?: Schemas.ColumnsProjection; - }): Promise { - return operationsByTag.records.insertRecord({ - pathParams: { workspace, region, dbBranchName: `${database}:${branch}`, tableName: table }, - queryParams: { columns }, - body: record, - ...this.extraProps - }); - } - - public getRecord({ - workspace, - region, - database, - branch, - table, - id, - columns - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - branch: Schemas.BranchName; - table: Schemas.TableName; - id: Schemas.RecordID; - columns?: Schemas.ColumnsProjection; - }): Promise { - return operationsByTag.records.getRecord({ - pathParams: { workspace, region, dbBranchName: `${database}:${branch}`, tableName: table, recordId: id }, - queryParams: { columns }, - ...this.extraProps - }); - } - - public insertRecordWithID({ - workspace, - region, - database, - branch, - table, - id, - record, - columns, - createOnly, - ifVersion - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - branch: Schemas.BranchName; - table: Schemas.TableName; - id: Schemas.RecordID; - record: Record; - columns?: Schemas.ColumnsProjection; - createOnly?: boolean; - ifVersion?: number; - }): Promise { - return operationsByTag.records.insertRecordWithID({ - pathParams: { workspace, region, dbBranchName: `${database}:${branch}`, tableName: table, recordId: id }, - queryParams: { columns, createOnly, ifVersion }, - body: record, - ...this.extraProps - }); - } - - public updateRecordWithID({ - workspace, - region, - database, - branch, - table, - id, - record, - columns, - ifVersion - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - branch: Schemas.BranchName; - table: Schemas.TableName; - id: Schemas.RecordID; - record: Record; - columns?: Schemas.ColumnsProjection; - ifVersion?: number; - }): Promise { - return operationsByTag.records.updateRecordWithID({ - pathParams: { workspace, region, dbBranchName: `${database}:${branch}`, tableName: table, recordId: id }, - queryParams: { columns, ifVersion }, - body: record, - ...this.extraProps - }); - } - - public upsertRecordWithID({ - workspace, - region, - database, - branch, - table, - id, - record, - columns, - ifVersion - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - branch: Schemas.BranchName; - table: Schemas.TableName; - id: Schemas.RecordID; - record: Record; - columns?: Schemas.ColumnsProjection; - ifVersion?: number; - }): Promise { - return operationsByTag.records.upsertRecordWithID({ - pathParams: { workspace, region, dbBranchName: `${database}:${branch}`, tableName: table, recordId: id }, - queryParams: { columns, ifVersion }, - body: record, - ...this.extraProps - }); - } - - public deleteRecord({ - workspace, - region, - database, - branch, - table, - id, - columns - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - branch: Schemas.BranchName; - table: Schemas.TableName; - id: Schemas.RecordID; - columns?: Schemas.ColumnsProjection; - }): Promise { - return operationsByTag.records.deleteRecord({ - pathParams: { workspace, region, dbBranchName: `${database}:${branch}`, tableName: table, recordId: id }, - queryParams: { columns }, - ...this.extraProps - }); - } - - public bulkInsertTableRecords({ - workspace, - region, - database, - branch, - table, - records, - columns - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - branch: Schemas.BranchName; - table: Schemas.TableName; - records: Record[]; - columns?: Schemas.ColumnsProjection; - }): Promise { - return operationsByTag.records.bulkInsertTableRecords({ - pathParams: { workspace, region, dbBranchName: `${database}:${branch}`, tableName: table }, - queryParams: { columns }, - body: { records }, - ...this.extraProps - }); - } - - public branchTransaction({ - workspace, - region, - database, - branch, - operations - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - branch: Schemas.BranchName; - operations: Schemas.TransactionOperation[]; - }): Promise { - return operationsByTag.records.branchTransaction({ - pathParams: { workspace, region, dbBranchName: `${database}:${branch}` }, - body: { operations }, - ...this.extraProps - }); - } -} - -class FilesApi { - constructor(private extraProps: ApiExtraProps) {} - - public getFileItem({ - workspace, - region, - database, - branch, - table, - record, - column, - fileId - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - branch: Schemas.BranchName; - table: Schemas.TableName; - record: Schemas.RecordID; - column: Schemas.ColumnName; - fileId: string; - }): Promise { - return operationsByTag.files.getFileItem({ - pathParams: { - workspace, - region, - dbBranchName: `${database}:${branch}`, - tableName: table, - recordId: record, - columnName: column, - fileId - }, - ...this.extraProps - }); - } - - public putFileItem({ - workspace, - region, - database, - branch, - table, - record, - column, - fileId, - file - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - branch: Schemas.BranchName; - table: Schemas.TableName; - record: Schemas.RecordID; - column: Schemas.ColumnName; - fileId: string; - file: any; - }): Promise { - return operationsByTag.files.putFileItem({ - pathParams: { - workspace, - region, - dbBranchName: `${database}:${branch}`, - tableName: table, - recordId: record, - columnName: column, - fileId - }, - // @ts-ignore - body: file, - ...this.extraProps - }); - } - - public deleteFileItem({ - workspace, - region, - database, - branch, - table, - record, - column, - fileId - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - branch: Schemas.BranchName; - table: Schemas.TableName; - record: Schemas.RecordID; - column: Schemas.ColumnName; - fileId: string; - }): Promise { - return operationsByTag.files.deleteFileItem({ - pathParams: { - workspace, - region, - dbBranchName: `${database}:${branch}`, - tableName: table, - recordId: record, - columnName: column, - fileId - }, - ...this.extraProps - }); - } - - public getFile({ - workspace, - region, - database, - branch, - table, - record, - column - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - branch: Schemas.BranchName; - table: Schemas.TableName; - record: Schemas.RecordID; - column: Schemas.ColumnName; - }): Promise { - return operationsByTag.files.getFile({ - pathParams: { - workspace, - region, - dbBranchName: `${database}:${branch}`, - tableName: table, - recordId: record, - columnName: column - }, - ...this.extraProps - }); - } - - public putFile({ - workspace, - region, - database, - branch, - table, - record, - column, - file - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - branch: Schemas.BranchName; - table: Schemas.TableName; - record: Schemas.RecordID; - column: Schemas.ColumnName; - file: Blob; - }): Promise { - return operationsByTag.files.putFile({ - pathParams: { - workspace, - region, - dbBranchName: `${database}:${branch}`, - tableName: table, - recordId: record, - columnName: column - }, - body: file, - ...this.extraProps - }); - } - - public deleteFile({ - workspace, - region, - database, - branch, - table, - record, - column - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - branch: Schemas.BranchName; - table: Schemas.TableName; - record: Schemas.RecordID; - column: Schemas.ColumnName; - }): Promise { - return operationsByTag.files.deleteFile({ - pathParams: { - workspace, - region, - dbBranchName: `${database}:${branch}`, - tableName: table, - recordId: record, - columnName: column - }, - ...this.extraProps - }); - } - - public fileAccess({ - workspace, - region, - fileId, - verify - }: { - workspace: Schemas.WorkspaceID; - region: string; - fileId: string; - verify?: Schemas.FileSignature; - }): Promise { - return operationsByTag.files.fileAccess({ - pathParams: { - workspace, - region, - fileId - }, - queryParams: { verify }, - ...this.extraProps - }); - } -} - -class SearchAndFilterApi { - constructor(private extraProps: ApiExtraProps) {} - - public queryTable({ - workspace, - region, - database, - branch, - table, - filter, - sort, - page, - columns, - consistency - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - branch: Schemas.BranchName; - table: Schemas.TableName; - filter?: Schemas.FilterExpression; - sort?: Schemas.SortExpression; - page?: Schemas.PageConfig; - columns?: Schemas.ColumnsProjection; - consistency?: 'strong' | 'eventual'; - }): Promise { - return operationsByTag.searchAndFilter.queryTable({ - pathParams: { workspace, region, dbBranchName: `${database}:${branch}`, tableName: table }, - body: { filter, sort, page, columns, consistency }, - ...this.extraProps - }); - } - - public searchTable({ - workspace, - region, - database, - branch, - table, - query, - fuzziness, - target, - prefix, - filter, - highlight, - boosters - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - branch: Schemas.BranchName; - table: Schemas.TableName; - query: string; - fuzziness?: Schemas.FuzzinessExpression; - target?: Schemas.TargetExpression; - prefix?: Schemas.PrefixExpression; - filter?: Schemas.FilterExpression; - highlight?: Schemas.HighlightExpression; - boosters?: Schemas.BoosterExpression[]; - }): Promise { - return operationsByTag.searchAndFilter.searchTable({ - pathParams: { workspace, region, dbBranchName: `${database}:${branch}`, tableName: table }, - body: { query, fuzziness, target, prefix, filter, highlight, boosters }, - ...this.extraProps - }); - } - - public searchBranch({ - workspace, - region, - database, - branch, - tables, - query, - fuzziness, - prefix, - highlight - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - branch: Schemas.BranchName; - tables?: ( - | string - | { - table: string; - filter?: Schemas.FilterExpression; - target?: Schemas.TargetExpression; - boosters?: Schemas.BoosterExpression[]; +type UserProps = { + headers?: Record; +}; + +type XataApiProxy = { + [Tag in keyof typeof operationsByTag]: { + [Method in keyof (typeof operationsByTag)[Tag]]: (typeof operationsByTag)[Tag][Method] extends infer Operation extends ( + ...args: any + ) => any + ? Omit[0], keyof ApiExtraProps> extends infer Params + ? RequiredKeys extends never + ? (params?: Params & UserProps) => ReturnType + : (params: Params & UserProps) => ReturnType + : never + : never; + }; +}; + +const buildApiClient = () => + class { + constructor(options: XataApiClientOptions = {}) { + const provider = options.host ?? 'production'; + const apiKey = options.apiKey ?? getAPIKey(); + const trace = options.trace ?? defaultTrace; + const clientID = generateUUID(); + + if (!apiKey) { + throw new Error('Could not resolve a valid apiKey'); + } + + const extraProps: ApiExtraProps = { + apiUrl: getHostUrl(provider, 'main'), + workspacesApiUrl: getHostUrl(provider, 'workspaces'), + fetch: getFetchImplementation(options.fetch), + apiKey, + trace, + clientName: options.clientName, + xataAgentExtra: options.xataAgentExtra, + clientID + }; + + return new Proxy(this, { + get: (_target, namespace: keyof typeof operationsByTag) => { + if (operationsByTag[namespace] === undefined) { + return undefined; + } + + return new Proxy( + {}, + { + get: (_target, operation: keyof (typeof operationsByTag)[keyof typeof operationsByTag]) => { + if (operationsByTag[namespace][operation] === undefined) { + return undefined; + } + + const method = operationsByTag[namespace][operation] as any; + + return async (params: Record) => { + return await method({ ...params, ...extraProps }); + }; + } + } + ); } - )[]; - query: string; - fuzziness?: Schemas.FuzzinessExpression; - prefix?: Schemas.PrefixExpression; - highlight?: Schemas.HighlightExpression; - }): Promise { - return operationsByTag.searchAndFilter.searchBranch({ - pathParams: { workspace, region, dbBranchName: `${database}:${branch}` }, - body: { tables, query, fuzziness, prefix, highlight }, - ...this.extraProps - }); - } - - public vectorSearchTable({ - workspace, - region, - database, - branch, - table, - queryVector, - column, - similarityFunction, - size, - filter - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - branch: Schemas.BranchName; - table: Schemas.TableName; - queryVector: number[]; - column: string; - similarityFunction?: string; - size?: number; - filter?: Schemas.FilterExpression; - }): Promise { - return operationsByTag.searchAndFilter.vectorSearchTable({ - pathParams: { workspace, region, dbBranchName: `${database}:${branch}`, tableName: table }, - body: { queryVector, column, similarityFunction, size, filter }, - ...this.extraProps - }); - } - - public askTable({ - workspace, - region, - database, - branch, - table, - options - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - branch: Schemas.BranchName; - table: Schemas.TableName; - options: Components.AskTableRequestBody; - }): Promise { - return operationsByTag.searchAndFilter.askTable({ - pathParams: { workspace, region, dbBranchName: `${database}:${branch}`, tableName: table }, - body: { ...options }, - ...this.extraProps - }); - } - - public askTableSession({ - workspace, - region, - database, - branch, - table, - sessionId, - message - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - branch: Schemas.BranchName; - table: Schemas.TableName; - sessionId: string; - message: string; - }): Promise { - return operationsByTag.searchAndFilter.askTableSession({ - pathParams: { workspace, region, dbBranchName: `${database}:${branch}`, tableName: table, sessionId }, - body: { message }, - ...this.extraProps - }); - } - - public summarizeTable({ - workspace, - region, - database, - branch, - table, - filter, - columns, - summaries, - sort, - summariesFilter, - page, - consistency - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - branch: Schemas.BranchName; - table: Schemas.TableName; - filter?: Schemas.FilterExpression; - columns?: Schemas.ColumnsProjection; - summaries?: Schemas.SummaryExpressionList; - sort?: Schemas.SortExpression; - summariesFilter?: Schemas.FilterExpression; - page?: { size?: number }; - consistency?: 'strong' | 'eventual'; - }): Promise { - return operationsByTag.searchAndFilter.summarizeTable({ - pathParams: { workspace, region, dbBranchName: `${database}:${branch}`, tableName: table }, - body: { filter, columns, summaries, sort, summariesFilter, page, consistency }, - ...this.extraProps - }); - } - - public aggregateTable({ - workspace, - region, - database, - branch, - table, - filter, - aggs - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - branch: Schemas.BranchName; - table: Schemas.TableName; - filter?: Schemas.FilterExpression; - aggs?: Schemas.AggExpressionMap; - }): Promise { - return operationsByTag.searchAndFilter.aggregateTable({ - pathParams: { workspace, region, dbBranchName: `${database}:${branch}`, tableName: table }, - body: { filter, aggs }, - ...this.extraProps - }); - } -} - -class MigrationRequestsApi { - constructor(private extraProps: ApiExtraProps) {} - - public queryMigrationRequests({ - workspace, - region, - database, - filter, - sort, - page, - columns - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - filter?: Schemas.FilterExpression; - sort?: Schemas.SortExpression; - page?: Schemas.PageConfig; - columns?: Schemas.ColumnsProjection; - }): Promise { - return operationsByTag.migrationRequests.queryMigrationRequests({ - pathParams: { workspace, region, dbName: database }, - body: { filter, sort, page, columns }, - ...this.extraProps - }); - } - - public createMigrationRequest({ - workspace, - region, - database, - migration - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - migration: Components.CreateMigrationRequestRequestBody; - }): Promise { - return operationsByTag.migrationRequests.createMigrationRequest({ - pathParams: { workspace, region, dbName: database }, - body: migration, - ...this.extraProps - }); - } - - public getMigrationRequest({ - workspace, - region, - database, - migrationRequest - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - migrationRequest: Schemas.MigrationRequestNumber; - }): Promise { - return operationsByTag.migrationRequests.getMigrationRequest({ - pathParams: { workspace, region, dbName: database, mrNumber: migrationRequest }, - ...this.extraProps - }); - } - - public updateMigrationRequest({ - workspace, - region, - database, - migrationRequest, - update - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - migrationRequest: Schemas.MigrationRequestNumber; - update: Components.UpdateMigrationRequestRequestBody; - }): Promise { - return operationsByTag.migrationRequests.updateMigrationRequest({ - pathParams: { workspace, region, dbName: database, mrNumber: migrationRequest }, - body: update, - ...this.extraProps - }); - } - - public listMigrationRequestsCommits({ - workspace, - region, - database, - migrationRequest, - page - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - migrationRequest: Schemas.MigrationRequestNumber; - page?: { after?: string; before?: string; size?: number }; - }): Promise { - return operationsByTag.migrationRequests.listMigrationRequestsCommits({ - pathParams: { workspace, region, dbName: database, mrNumber: migrationRequest }, - body: { page }, - ...this.extraProps - }); - } - - public compareMigrationRequest({ - workspace, - region, - database, - migrationRequest - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - migrationRequest: Schemas.MigrationRequestNumber; - }): Promise { - return operationsByTag.migrationRequests.compareMigrationRequest({ - pathParams: { workspace, region, dbName: database, mrNumber: migrationRequest }, - ...this.extraProps - }); - } - - public getMigrationRequestIsMerged({ - workspace, - region, - database, - migrationRequest - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - migrationRequest: Schemas.MigrationRequestNumber; - }): Promise { - return operationsByTag.migrationRequests.getMigrationRequestIsMerged({ - pathParams: { workspace, region, dbName: database, mrNumber: migrationRequest }, - ...this.extraProps - }); - } - - public mergeMigrationRequest({ - workspace, - region, - database, - migrationRequest - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - migrationRequest: Schemas.MigrationRequestNumber; - }): Promise { - return operationsByTag.migrationRequests.mergeMigrationRequest({ - pathParams: { workspace, region, dbName: database, mrNumber: migrationRequest }, - ...this.extraProps - }); - } -} - -class MigrationsApi { - constructor(private extraProps: ApiExtraProps) {} - - public getBranchMigrationHistory({ - workspace, - region, - database, - branch, - limit, - startFrom - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - branch: Schemas.BranchName; - limit?: number; - startFrom?: string; - }): Promise { - return operationsByTag.migrations.getBranchMigrationHistory({ - pathParams: { workspace, region, dbBranchName: `${database}:${branch}` }, - body: { limit, startFrom }, - ...this.extraProps - }); - } - - public getBranchMigrationPlan({ - workspace, - region, - database, - branch, - schema - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - branch: Schemas.BranchName; - schema: Schemas.Schema; - }): Promise { - return operationsByTag.migrations.getBranchMigrationPlan({ - pathParams: { workspace, region, dbBranchName: `${database}:${branch}` }, - body: schema, - ...this.extraProps - }); - } - - public executeBranchMigrationPlan({ - workspace, - region, - database, - branch, - plan - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - branch: Schemas.BranchName; - plan: Types.ExecuteBranchMigrationPlanRequestBody; - }): Promise { - return operationsByTag.migrations.executeBranchMigrationPlan({ - pathParams: { workspace, region, dbBranchName: `${database}:${branch}` }, - body: plan, - ...this.extraProps - }); - } - - public getBranchSchemaHistory({ - workspace, - region, - database, - branch, - page - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - branch: Schemas.BranchName; - page?: { after?: string; before?: string; size?: number }; - }): Promise { - return operationsByTag.migrations.getBranchSchemaHistory({ - pathParams: { workspace, region, dbBranchName: `${database}:${branch}` }, - body: { page }, - ...this.extraProps - }); - } - - public compareBranchWithUserSchema({ - workspace, - region, - database, - branch, - schema, - schemaOperations, - branchOperations - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - branch: Schemas.BranchName; - schema: Schemas.Schema; - schemaOperations?: Schemas.MigrationOp[]; - branchOperations?: Schemas.MigrationOp[]; - }): Promise { - return operationsByTag.migrations.compareBranchWithUserSchema({ - pathParams: { workspace, region, dbBranchName: `${database}:${branch}` }, - body: { schema, schemaOperations, branchOperations }, - ...this.extraProps - }); - } - - public compareBranchSchemas({ - workspace, - region, - database, - branch, - compare, - sourceBranchOperations, - targetBranchOperations - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - branch: Schemas.BranchName; - compare: Schemas.BranchName; - sourceBranchOperations?: Schemas.MigrationOp[]; - targetBranchOperations?: Schemas.MigrationOp[]; - }): Promise { - return operationsByTag.migrations.compareBranchSchemas({ - pathParams: { workspace, region, dbBranchName: `${database}:${branch}`, branchName: compare }, - body: { sourceBranchOperations, targetBranchOperations }, - ...this.extraProps - }); - } - - public updateBranchSchema({ - workspace, - region, - database, - branch, - migration - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - branch: Schemas.BranchName; - migration: Schemas.Migration; - }): Promise { - return operationsByTag.migrations.updateBranchSchema({ - pathParams: { workspace, region, dbBranchName: `${database}:${branch}` }, - body: migration, - ...this.extraProps - }); - } - - public previewBranchSchemaEdit({ - workspace, - region, - database, - branch, - data - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - branch: Schemas.BranchName; - data: { edits?: Schemas.SchemaEditScript }; - }): Promise { - return operationsByTag.migrations.previewBranchSchemaEdit({ - pathParams: { workspace, region, dbBranchName: `${database}:${branch}` }, - body: data, - ...this.extraProps - }); - } - - public applyBranchSchemaEdit({ - workspace, - region, - database, - branch, - edits - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - branch: Schemas.BranchName; - edits: Schemas.SchemaEditScript; - }): Promise { - return operationsByTag.migrations.applyBranchSchemaEdit({ - pathParams: { workspace, region, dbBranchName: `${database}:${branch}` }, - body: { edits }, - ...this.extraProps - }); - } - - public pushBranchMigrations({ - workspace, - region, - database, - branch, - migrations - }: { - workspace: Schemas.WorkspaceID; - region: string; - database: Schemas.DBName; - branch: Schemas.BranchName; - migrations: Schemas.MigrationObject[]; - }): Promise { - return operationsByTag.migrations.pushBranchMigrations({ - pathParams: { workspace, region, dbBranchName: `${database}:${branch}` }, - body: { migrations }, - ...this.extraProps - }); - } -} - -class DatabaseApi { - constructor(private extraProps: ApiExtraProps) {} - - public getDatabaseList({ workspace }: { workspace: Schemas.WorkspaceID }): Promise { - return operationsByTag.databases.getDatabaseList({ - pathParams: { workspaceId: workspace }, - ...this.extraProps - }); - } - - public createDatabase({ - workspace, - database, - data, - headers - }: { - workspace: Schemas.WorkspaceID; - database: Schemas.DBName; - data: Components.CreateDatabaseRequestBody; - headers?: Record; - }): Promise { - return operationsByTag.databases.createDatabase({ - pathParams: { workspaceId: workspace, dbName: database }, - body: data, - headers, - ...this.extraProps - }); - } - - public deleteDatabase({ - workspace, - database - }: { - workspace: Schemas.WorkspaceID; - database: Schemas.DBName; - }): Promise { - return operationsByTag.databases.deleteDatabase({ - pathParams: { workspaceId: workspace, dbName: database }, - ...this.extraProps - }); - } - - public getDatabaseMetadata({ - workspace, - database - }: { - workspace: Schemas.WorkspaceID; - database: Schemas.DBName; - }): Promise { - return operationsByTag.databases.getDatabaseMetadata({ - pathParams: { workspaceId: workspace, dbName: database }, - ...this.extraProps - }); - } - - public updateDatabaseMetadata({ - workspace, - database, - metadata - }: { - workspace: Schemas.WorkspaceID; - database: Schemas.DBName; - metadata: Schemas.DatabaseMetadata; - }): Promise { - return operationsByTag.databases.updateDatabaseMetadata({ - pathParams: { workspaceId: workspace, dbName: database }, - body: metadata, - ...this.extraProps - }); - } - - public renameDatabase({ - workspace, - database, - newName - }: { - workspace: Schemas.WorkspaceID; - database: Schemas.DBName; - newName: Schemas.DBName; - }): Promise { - return operationsByTag.databases.renameDatabase({ - pathParams: { workspaceId: workspace, dbName: database }, - body: { newName }, - ...this.extraProps - }); - } - - public getDatabaseGithubSettings({ - workspace, - database - }: { - workspace: Schemas.WorkspaceID; - database: Schemas.DBName; - }): Promise { - return operationsByTag.databases.getDatabaseGithubSettings({ - pathParams: { workspaceId: workspace, dbName: database }, - ...this.extraProps - }); - } - - public updateDatabaseGithubSettings({ - workspace, - database, - settings - }: { - workspace: Schemas.WorkspaceID; - database: Schemas.DBName; - settings: Schemas.DatabaseGithubSettings; - }): Promise { - return operationsByTag.databases.updateDatabaseGithubSettings({ - pathParams: { workspaceId: workspace, dbName: database }, - body: settings, - ...this.extraProps - }); - } - - public deleteDatabaseGithubSettings({ - workspace, - database - }: { - workspace: Schemas.WorkspaceID; - database: Schemas.DBName; - }): Promise { - return operationsByTag.databases.deleteDatabaseGithubSettings({ - pathParams: { workspaceId: workspace, dbName: database }, - ...this.extraProps - }); - } + }); + } + } as unknown as { new (options?: XataApiClientOptions): XataApiProxy }; - public listRegions({ workspace }: { workspace: Schemas.WorkspaceID }): Promise { - return operationsByTag.databases.listRegions({ - pathParams: { workspaceId: workspace }, - ...this.extraProps - }); - } -} +export class XataApiClient extends buildApiClient() {} diff --git a/packages/client/src/util/types.ts b/packages/client/src/util/types.ts index 95c0bc8c0..7fc3d8b63 100644 --- a/packages/client/src/util/types.ts +++ b/packages/client/src/util/types.ts @@ -65,3 +65,11 @@ type Narrowable = string | number | bigint | boolean; type Try = A1 extends A2 ? A1 : Catch; export type Narrow = Try>; + +export type RequiredKeys = { + [K in keyof T]-?: {} extends Pick ? never : K; +}[keyof T]; + +export type FlattenObject = { + [K in keyof T]: T[K]; +}[keyof T]; diff --git a/test/integration/query.test.ts b/test/integration/query.test.ts index a22e26810..ef1916ca6 100644 --- a/test/integration/query.test.ts +++ b/test/integration/query.test.ts @@ -601,14 +601,12 @@ describe('integration tests', () => { }); test('Pagination default value', async () => { - await api.tables.createTable({ workspace, region, database, branch: 'main', table: 'planes' }); - await api.tables.setTableSchema({ - workspace, - region, - database, - branch: 'main', - table: 'planes', - schema: { columns: [{ name: 'name', type: 'string' }] } + await api.table.createTable({ + pathParams: { workspace, region, dbBranchName: `${database}:main`, tableName: 'planes' } + }); + await api.table.setTableSchema({ + pathParams: { workspace, region, dbBranchName: `${database}:main`, tableName: 'planes' }, + body: { columns: [{ name: 'name', type: 'string' }] } }); const planes = Array.from({ length: PAGINATION_DEFAULT_SIZE + 50 }, (_, index) => ({ name: `Plane ${index}` })); diff --git a/test/integration/smoke.test.ts b/test/integration/smoke.test.ts index 4ba084ad0..6742e63e4 100644 --- a/test/integration/smoke.test.ts +++ b/test/integration/smoke.test.ts @@ -22,7 +22,7 @@ describe('API Client Integration Tests', () => { test('Create, get and delete workspace with new apiKey', async () => { const workspaceName = getWorkspaceName(); - const newApiKey = await api.authentication.createUserAPIKey({ name: `${workspaceName}-key` }); + const newApiKey = await api.authentication.createUserAPIKey({ pathParams: { keyName: `${workspaceName}-key` } }); expect(newApiKey).toBeDefined(); expect(newApiKey.name).toBe(`${workspaceName}-key`); @@ -31,7 +31,7 @@ describe('API Client Integration Tests', () => { const newApi = new XataApiClient({ apiKey: newApiKey.key, host }); const { id: workspace, name } = await newApi.workspaces.createWorkspace({ - data: { name: workspaceName, slug: `${workspaceName}-slug` } + body: { name: workspaceName, slug: `${workspaceName}-slug` } }); await waitForReplication(newApi, workspace); @@ -39,51 +39,41 @@ describe('API Client Integration Tests', () => { expect(workspace).toBeDefined(); expect(name).toBe(workspaceName); - const foo = await newApi.workspaces.getWorkspace({ workspace }); + const foo = await newApi.workspaces.getWorkspace({ pathParams: { workspaceId: workspace } }); expect(foo.id).toBe(workspace); expect(foo.slug).toBe(`${workspaceName}-slug`); - const bar = await newApi.workspaces.getWorkspace({ workspace }); + const bar = await newApi.workspaces.getWorkspace({ pathParams: { workspaceId: workspace } }); expect(bar.id).toBe(workspace); expect(bar.slug).toBe(`${workspaceName}-slug`); - const { databaseName: database } = await newApi.database.createDatabase({ - workspace, - database: `data-${workspace}`, - data: { region } + const { databaseName: database } = await newApi.databases.createDatabase({ + pathParams: { workspaceId: workspace, dbName: `data-${workspace}` }, + body: { region } }); await waitForReplication(newApi, workspace, database); - await newApi.branches.createBranch({ workspace, region, database, branch: 'branch' }); - await newApi.tables.createTable({ workspace, region, database, branch: 'branch', table: 'table' }); - await newApi.tables.setTableSchema({ - workspace, - region, - database, - branch: 'branch', - table: 'table', - schema: { columns: [{ name: 'email', type: 'string' }] } + await newApi.branch.createBranch({ + pathParams: { workspace, region, dbBranchName: `${database}:branch` } + }); + await newApi.table.createTable({ + pathParams: { workspace, region, dbBranchName: `${database}:branch`, tableName: 'table' } + }); + await newApi.table.setTableSchema({ + pathParams: { workspace, region, dbBranchName: `${database}:branch`, tableName: 'table' }, + body: { columns: [{ name: 'email', type: 'string' }] } }); const { id } = await newApi.records.insertRecord({ - workspace, - region, - database, - branch: 'branch', - table: 'table', - record: { email: 'example@foo.bar' } + pathParams: { workspace, region, dbBranchName: `${database}:branch`, tableName: 'table' }, + body: { email: 'example@foo.bar' } }); const record = await newApi.records.getRecord({ - workspace, - region, - database, - branch: 'branch', - table: 'table', - id + pathParams: { workspace, region, dbBranchName: `${database}:branch`, tableName: 'table', recordId: id } }); expect(record.id).toBeDefined(); @@ -92,56 +82,45 @@ describe('API Client Integration Tests', () => { await waitForSearchIndexing(newApi, workspace, database); const search = await newApi.searchAndFilter.searchTable({ - workspace, - region, - database, - branch: 'branch', - table: 'table', - query: 'example' + pathParams: { workspace, region, dbBranchName: `${database}:branch`, tableName: 'table' }, + body: { query: 'example' } }); expect(search.totalCount).toEqual(1); expect(search.records[0].id).toEqual(id); const failedSearch = await newApi.searchAndFilter.searchTable({ - workspace, - region, - database, - branch: 'branch', - table: 'table', - query: 'random' + pathParams: { workspace, region, dbBranchName: `${database}:branch`, tableName: 'table' }, + body: { query: 'random' } }); expect(failedSearch.totalCount).toEqual(0); expect(failedSearch.records).toEqual([]); - await api.authentication.deleteUserAPIKey({ name: newApiKey.name }); + await api.authentication.deleteUserAPIKey({ pathParams: { keyName: newApiKey.name } }); await waitFailInReplication(newApi, workspace, database); await expect( newApi.records.getRecord({ - workspace, - region, - database, - branch: 'branch', - table: 'table', - id + pathParams: { workspace, region, dbBranchName: `${database}:branch`, tableName: 'table', recordId: id } }) ).rejects.toHaveProperty('message'); - await api.workspaces.deleteWorkspace({ workspace }); + await api.workspaces.deleteWorkspace({ pathParams: { workspaceId: workspace } }); - await expect(api.workspaces.getWorkspace({ workspace })).rejects.toHaveProperty('message'); + await expect(api.workspaces.getWorkspace({ pathParams: { workspaceId: workspace } })).rejects.toHaveProperty( + 'message' + ); }); }); async function waitForReplication(api: XataApiClient, workspace: string, database?: string): Promise { try { if (database === undefined) { - await api.database.getDatabaseList({ workspace }); + await api.databases.getDatabaseList({ pathParams: { workspaceId: workspace } }); } else { - await api.branches.getBranchList({ workspace, database, region }); + await api.branch.getBranchList({ pathParams: { workspace, region, dbName: database } }); } } catch (error) { await new Promise((resolve) => setTimeout(resolve, 2000)); @@ -151,7 +130,7 @@ async function waitForReplication(api: XataApiClient, workspace: string, databas async function waitFailInReplication(api: XataApiClient, workspace: string, database: string): Promise { try { - await api.branches.getBranchList({ workspace, database, region }); + await api.branch.getBranchList({ pathParams: { workspace, region, dbName: database } }); await new Promise((resolve) => setTimeout(resolve, 2000)); return await waitFailInReplication(api, workspace, database); @@ -163,12 +142,8 @@ async function waitFailInReplication(api: XataApiClient, workspace: string, data async function waitForSearchIndexing(api: XataApiClient, workspace: string, database: string): Promise { try { const { aggs } = await api.searchAndFilter.aggregateTable({ - workspace, - database, - region, - branch: 'branch', - table: 'table', - aggs: { total: { count: '*' } } + pathParams: { workspace, region, dbBranchName: `${database}:branch`, tableName: 'table' }, + body: { aggs: { total: { count: '*' } } } }); if (aggs?.total === 1) return; diff --git a/test/utils/setup.ts b/test/utils/setup.ts index 4aad29bc7..f12fcb50f 100644 --- a/test/utils/setup.ts +++ b/test/utils/setup.ts @@ -73,10 +73,9 @@ export async function setUpTestEnvironment( const id = Date.now().toString(36); const api = new XataApiClient({ apiKey, fetch, host, clientName: 'sdk-tests' }); - const { databaseName: database } = await api.database.createDatabase({ - workspace, - database: `sdk-integration-test-${prefix}-${id}`, - data: { region }, + const { databaseName: database } = await api.databases.createDatabase({ + pathParams: { workspaceId: workspace, dbName: `sdk-integration-test-${prefix}-${id}` }, + body: { region }, headers: { 'X-Xata-Files': 'true' } }); @@ -93,14 +92,14 @@ export async function setUpTestEnvironment( }; const { edits } = await api.migrations.compareBranchWithUserSchema({ - workspace, - region, - database, - branch: 'main', - schema + pathParams: { workspace, region, dbBranchName: `${database}:main` }, + body: { schema } }); - await api.migrations.applyBranchSchemaEdit({ workspace, region, database, branch: 'main', edits }); + await api.migrations.applyBranchSchemaEdit({ + pathParams: { workspace, region, dbBranchName: `${database}:main` }, + body: { edits } + }); let span: Span | undefined; @@ -110,7 +109,7 @@ export async function setUpTestEnvironment( }, afterAll: async () => { try { - await api.database.deleteDatabase({ workspace, database }); + await api.databases.deleteDatabase({ pathParams: { workspaceId: workspace, dbName: database } }); } catch (e) { // Ignore error, delete database during ES snapshot fails console.error('Delete database failed', e);