Skip to content

Commit d3a40bb

Browse files
authored
Add connectRest helper to api-client (#8756)
Signed-off-by: Nikolay Marchuk <[email protected]>
1 parent 3ffce26 commit d3a40bb

File tree

5 files changed

+66
-38
lines changed

5 files changed

+66
-38
lines changed

packages/api-client/src/client.ts

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414
//
15-
import { type WorkspaceLoginInfo, getClient as getAccountClient } from '@hcengineering/account-client'
15+
import { getClient as getAccountClient } from '@hcengineering/account-client'
1616
import client, { clientId } from '@hcengineering/client'
1717
import {
1818
type Class,
@@ -51,6 +51,7 @@ import {
5151
createMarkupOperations
5252
} from './markup'
5353
import { type ConnectOptions, type PlatformClient, WithMarkup } from './types'
54+
import { getWorkspaceToken } from './utils'
5455

5556
/**
5657
* Create platform client
@@ -282,39 +283,3 @@ class PlatformClientImpl implements PlatformClient {
282283
await this.close()
283284
}
284285
}
285-
286-
export interface WorkspaceToken {
287-
endpoint: string
288-
token: string
289-
workspaceId: WorkspaceUuid
290-
info: WorkspaceLoginInfo
291-
}
292-
293-
export async function getWorkspaceToken (
294-
url: string,
295-
options: ConnectOptions,
296-
config?: ServerConfig
297-
): Promise<WorkspaceToken> {
298-
config ??= await loadServerConfig(url)
299-
300-
let token: string | undefined
301-
302-
if ('token' in options) {
303-
token = options.token
304-
} else {
305-
const { email, password } = options
306-
const loginInfo = await getAccountClient(config.ACCOUNTS_URL).login(email, password)
307-
token = loginInfo.token
308-
}
309-
310-
if (token === undefined) {
311-
throw new Error('Login failed')
312-
}
313-
314-
const ws = await getAccountClient(config.ACCOUNTS_URL, token).selectWorkspace(options.workspace)
315-
if (ws === undefined) {
316-
throw new Error('Workspace not found')
317-
}
318-
319-
return { endpoint: ws.endpoint, token: ws.token, workspaceId: ws.workspace, info: ws }
320-
}

packages/api-client/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ export * from './socket'
1919
export * from './types'
2020
export * from './rest'
2121
export * from './config'
22+
export * from './utils'

packages/api-client/src/rest/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@
1313
// limitations under the License.
1414
//
1515

16-
export { createRestClient } from './rest'
16+
export { createRestClient, connectRest } from './rest'
1717
export { createRestTxOperations } from './tx'
1818
export * from './types'

packages/api-client/src/rest/rest.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,19 @@ import {
3939
import { PlatformError, unknownError } from '@hcengineering/platform'
4040

4141
import type { RestClient } from './types'
42+
import { AuthOptions } from '../types'
4243
import { extractJson, withRetry } from './utils'
44+
import { getWorkspaceToken } from '../utils'
4345

4446
export function createRestClient (endpoint: string, workspaceId: string, token: string): RestClient {
4547
return new RestClientImpl(endpoint, workspaceId, token)
4648
}
4749

50+
export async function connectRest (url: string, options: AuthOptions): Promise<RestClient> {
51+
const { endpoint, token, workspaceId } = await getWorkspaceToken(url, options)
52+
return createRestClient(endpoint, workspaceId, token)
53+
}
54+
4855
const rateLimitError = 'rate-limit'
4956

5057
function isRLE (err: any): boolean {

packages/api-client/src/utils.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//
2+
// Copyright © 2025 Hardcore Engineering Inc.
3+
//
4+
// Licensed under the Eclipse Public License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License. You may
6+
// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
//
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
16+
import { type WorkspaceLoginInfo, getClient as getAccountClient } from '@hcengineering/account-client'
17+
import { WorkspaceUuid } from '@hcengineering/core'
18+
import { AuthOptions } from './types'
19+
import { loadServerConfig, ServerConfig } from './config'
20+
21+
export interface WorkspaceToken {
22+
endpoint: string
23+
token: string
24+
workspaceId: WorkspaceUuid
25+
info: WorkspaceLoginInfo
26+
}
27+
28+
export async function getWorkspaceToken (
29+
url: string,
30+
options: AuthOptions,
31+
config?: ServerConfig
32+
): Promise<WorkspaceToken> {
33+
config ??= await loadServerConfig(url)
34+
35+
let token: string | undefined
36+
37+
if ('token' in options) {
38+
token = options.token
39+
} else {
40+
const { email, password } = options
41+
const loginInfo = await getAccountClient(config.ACCOUNTS_URL).login(email, password)
42+
token = loginInfo.token
43+
}
44+
45+
if (token === undefined) {
46+
throw new Error('Login failed')
47+
}
48+
49+
const ws = await getAccountClient(config.ACCOUNTS_URL, token).selectWorkspace(options.workspace)
50+
if (ws === undefined) {
51+
throw new Error('Workspace not found')
52+
}
53+
54+
return { endpoint: ws.endpoint, token: ws.token, workspaceId: ws.workspace, info: ws }
55+
}

0 commit comments

Comments
 (0)