diff --git a/packages/api-client/src/client.ts b/packages/api-client/src/client.ts index cf49f0c0e9..db9bf10d44 100644 --- a/packages/api-client/src/client.ts +++ b/packages/api-client/src/client.ts @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. // -import { type WorkspaceLoginInfo, getClient as getAccountClient } from '@hcengineering/account-client' +import { getClient as getAccountClient } from '@hcengineering/account-client' import client, { clientId } from '@hcengineering/client' import { type Class, @@ -51,6 +51,7 @@ import { createMarkupOperations } from './markup' import { type ConnectOptions, type PlatformClient, WithMarkup } from './types' +import { getWorkspaceToken } from './utils' /** * Create platform client @@ -282,39 +283,3 @@ class PlatformClientImpl implements PlatformClient { await this.close() } } - -export interface WorkspaceToken { - endpoint: string - token: string - workspaceId: WorkspaceUuid - info: WorkspaceLoginInfo -} - -export async function getWorkspaceToken ( - url: string, - options: ConnectOptions, - config?: ServerConfig -): Promise { - config ??= await loadServerConfig(url) - - let token: string | undefined - - if ('token' in options) { - token = options.token - } else { - const { email, password } = options - const loginInfo = await getAccountClient(config.ACCOUNTS_URL).login(email, password) - token = loginInfo.token - } - - if (token === undefined) { - throw new Error('Login failed') - } - - const ws = await getAccountClient(config.ACCOUNTS_URL, token).selectWorkspace(options.workspace) - if (ws === undefined) { - throw new Error('Workspace not found') - } - - return { endpoint: ws.endpoint, token: ws.token, workspaceId: ws.workspace, info: ws } -} diff --git a/packages/api-client/src/index.ts b/packages/api-client/src/index.ts index e6ee3bebb6..b96c8d21d2 100644 --- a/packages/api-client/src/index.ts +++ b/packages/api-client/src/index.ts @@ -19,3 +19,4 @@ export * from './socket' export * from './types' export * from './rest' export * from './config' +export * from './utils' diff --git a/packages/api-client/src/rest/index.ts b/packages/api-client/src/rest/index.ts index 4397c92ecd..371187c27a 100644 --- a/packages/api-client/src/rest/index.ts +++ b/packages/api-client/src/rest/index.ts @@ -13,6 +13,6 @@ // limitations under the License. // -export { createRestClient } from './rest' +export { createRestClient, connectRest } from './rest' export { createRestTxOperations } from './tx' export * from './types' diff --git a/packages/api-client/src/rest/rest.ts b/packages/api-client/src/rest/rest.ts index ccf1a95c16..b545f73a71 100644 --- a/packages/api-client/src/rest/rest.ts +++ b/packages/api-client/src/rest/rest.ts @@ -39,12 +39,19 @@ import { import { PlatformError, unknownError } from '@hcengineering/platform' import type { RestClient } from './types' +import { AuthOptions } from '../types' import { extractJson, withRetry } from './utils' +import { getWorkspaceToken } from '../utils' export function createRestClient (endpoint: string, workspaceId: string, token: string): RestClient { return new RestClientImpl(endpoint, workspaceId, token) } +export async function connectRest (url: string, options: AuthOptions): Promise { + const { endpoint, token, workspaceId } = await getWorkspaceToken(url, options) + return createRestClient(endpoint, workspaceId, token) +} + const rateLimitError = 'rate-limit' function isRLE (err: any): boolean { diff --git a/packages/api-client/src/utils.ts b/packages/api-client/src/utils.ts new file mode 100644 index 0000000000..0d6efe5f3c --- /dev/null +++ b/packages/api-client/src/utils.ts @@ -0,0 +1,55 @@ +// +// Copyright © 2025 Hardcore Engineering Inc. +// +// Licensed under the Eclipse Public License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. You may +// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// +// See the License for the specific language governing permissions and +// limitations under the License. +// + +import { type WorkspaceLoginInfo, getClient as getAccountClient } from '@hcengineering/account-client' +import { WorkspaceUuid } from '@hcengineering/core' +import { AuthOptions } from './types' +import { loadServerConfig, ServerConfig } from './config' + +export interface WorkspaceToken { + endpoint: string + token: string + workspaceId: WorkspaceUuid + info: WorkspaceLoginInfo +} + +export async function getWorkspaceToken ( + url: string, + options: AuthOptions, + config?: ServerConfig +): Promise { + config ??= await loadServerConfig(url) + + let token: string | undefined + + if ('token' in options) { + token = options.token + } else { + const { email, password } = options + const loginInfo = await getAccountClient(config.ACCOUNTS_URL).login(email, password) + token = loginInfo.token + } + + if (token === undefined) { + throw new Error('Login failed') + } + + const ws = await getAccountClient(config.ACCOUNTS_URL, token).selectWorkspace(options.workspace) + if (ws === undefined) { + throw new Error('Workspace not found') + } + + return { endpoint: ws.endpoint, token: ws.token, workspaceId: ws.workspace, info: ws } +}