-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extracts fetchWorkspaceSyncPayload() function to its own file
- Loading branch information
1 parent
f9d1d5b
commit 85b1ce4
Showing
2 changed files
with
47 additions
and
45 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
apps/meteor/app/cloud/server/functions/syncWorkspace/fetchWorkspaceSyncPayload.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import type { Cloud, Serialized } from '@rocket.chat/core-typings'; | ||
import { serverFetch as fetch } from '@rocket.chat/server-fetch'; | ||
import { v, compile } from 'suretype'; | ||
|
||
import { CloudWorkspaceConnectionError } from '../../../../../lib/errors/CloudWorkspaceConnectionError'; | ||
import { settings } from '../../../../settings/server'; | ||
|
||
const workspaceSyncPayloadSchema = v.object({ | ||
workspaceId: v.string().required(), | ||
publicKey: v.string(), | ||
license: v.string().required(), | ||
}); | ||
|
||
const assertWorkspaceSyncPayload = compile(workspaceSyncPayloadSchema); | ||
|
||
export async function fetchWorkspaceSyncPayload({ | ||
token, | ||
data, | ||
}: { | ||
token: string; | ||
data: Cloud.WorkspaceSyncRequestPayload; | ||
}): Promise<Serialized<Cloud.WorkspaceSyncResponse>> { | ||
const workspaceRegistrationClientUri = settings.get<string>('Cloud_Workspace_Registration_Client_Uri'); | ||
const response = await fetch(`${workspaceRegistrationClientUri}/sync`, { | ||
method: 'POST', | ||
headers: { | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
body: data, | ||
}); | ||
|
||
if (!response.ok) { | ||
try { | ||
const { error } = await response.json(); | ||
throw new CloudWorkspaceConnectionError(`Failed to connect to Rocket.Chat Cloud: ${error}`); | ||
} catch (error) { | ||
throw new CloudWorkspaceConnectionError(`Failed to connect to Rocket.Chat Cloud: ${response.statusText}`); | ||
} | ||
} | ||
|
||
const payload = await response.json(); | ||
|
||
assertWorkspaceSyncPayload(payload); | ||
|
||
return payload; | ||
} |
46 changes: 1 addition & 45 deletions
46
apps/meteor/app/cloud/server/functions/syncWorkspace/syncCloudData.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters