update sdk usage

This commit is contained in:
FTCHD
2026-06-05 21:13:41 +03:00
parent a11b1d2bfd
commit c38b19880c
2 changed files with 73 additions and 21 deletions
+17 -21
View File
@@ -8,7 +8,7 @@ import type { JobItem } from '../../types/jobs'
import type { FlagValue } from '../../types/rclone' import type { FlagValue } from '../../types/rclone'
import { getFsInfo } from '../format' import { getFsInfo } from '../format'
import { restartActiveRclone, runRcloneCli } from './cli' import { restartActiveRclone, runRcloneCli } from './cli'
import rclone from './client' import rclone, { rcloneAsync } from './client'
import { parseRcloneOptions } from './common' import { parseRcloneOptions } from './common'
const RE_BACKSLASH = /\\/g const RE_BACKSLASH = /\\/g
@@ -19,7 +19,6 @@ const RE_WINDOWS_DRIVE_LETTER = /^[a-zA-Z]:$/
export async function startDryRun<T>(operation: () => Promise<T>): Promise<T> { export async function startDryRun<T>(operation: () => Promise<T>): Promise<T> {
await rclone('/options/set', { await rclone('/options/set', {
// @ts-ignore
body: { body: {
main: { DryRun: true }, main: { DryRun: true },
}, },
@@ -34,7 +33,6 @@ export async function startDryRun<T>(operation: () => Promise<T>): Promise<T> {
return result return result
} finally { } finally {
await rclone('/options/set', { await rclone('/options/set', {
// @ts-ignore
body: { body: {
main: { DryRun: false }, main: { DryRun: false },
}, },
@@ -186,7 +184,7 @@ export async function startCopy({
const dstOptions = const dstOptions =
options.remotes && dstRemoteName && dstRemoteName in options.remotes options.remotes && dstRemoteName && dstRemoteName in options.remotes
? (JSON.parse(options.remotes[dstRemoteName] as unknown as string) as any) ? options.remotes[dstRemoteName]
: undefined : undefined
for (const source of sources) { for (const source of sources) {
@@ -213,7 +211,7 @@ export async function startCopy({
const srcOptions = const srcOptions =
options.remotes && srcRemoteName && srcRemoteName in options.remotes options.remotes && srcRemoteName && srcRemoteName in options.remotes
? (JSON.parse(options.remotes[srcRemoteName] as unknown as string) as any) ? options.remotes[srcRemoteName]
: undefined : undefined
if (srcType === 'folder') { if (srcType === 'folder') {
@@ -318,7 +316,7 @@ export async function startMove({
const dstOptions = const dstOptions =
options.remotes && dstRemoteName && dstRemoteName in options.remotes options.remotes && dstRemoteName && dstRemoteName in options.remotes
? (JSON.parse(options.remotes[dstRemoteName] as unknown as string) as any) ? options.remotes[dstRemoteName]
: undefined : undefined
for (const source of sources) { for (const source of sources) {
@@ -340,7 +338,7 @@ export async function startMove({
const srcOptions = const srcOptions =
options.remotes && srcRemoteName && srcRemoteName in options.remotes options.remotes && srcRemoteName && srcRemoteName in options.remotes
? (JSON.parse(options.remotes[srcRemoteName] as unknown as string) as any) ? options.remotes[srcRemoteName]
: undefined : undefined
if (srcType === 'folder') { if (srcType === 'folder') {
@@ -640,7 +638,7 @@ export async function startMount({
const srcOptions = const srcOptions =
options.remotes && srcRemoteName && srcRemoteName in options.remotes options.remotes && srcRemoteName && srcRemoteName in options.remotes
? (JSON.parse(options.remotes[srcRemoteName] as unknown as string) as any) ? options.remotes[srcRemoteName]
: undefined : undefined
if (destination === '*' && currentPlatform === 'windows') { if (destination === '*' && currentPlatform === 'windows') {
@@ -662,7 +660,7 @@ export async function startMount({
retries: 3, retries: 3,
} }
) )
return response?.mountPoint as string | undefined return response?.mountPoint
} }
const { const {
@@ -837,17 +835,17 @@ export async function startBisync({
const srcOptions = const srcOptions =
options.remotes && srcRemoteName && srcRemoteName in options.remotes options.remotes && srcRemoteName && srcRemoteName in options.remotes
? (JSON.parse(options.remotes[srcRemoteName] as unknown as string) as any) ? options.remotes[srcRemoteName]
: undefined : undefined
const dstOptions = const dstOptions =
options.remotes && dstRemoteName && dstRemoteName in options.remotes options.remotes && dstRemoteName && dstRemoteName in options.remotes
? (JSON.parse(options.remotes[dstRemoteName] as unknown as string) as any) ? options.remotes[dstRemoteName]
: undefined : undefined
const r = await pRetry( const r = await pRetry(
async () => async () =>
await rclone('/sync/bisync', { await rcloneAsync('/sync/bisync', {
params: { params: {
query: { query: {
path1: serializeOptions(srcFullDirPath, { path1: serializeOptions(srcFullDirPath, {
@@ -857,7 +855,6 @@ export async function startBisync({
path2: serializeOptions(dstFullDirPath, { path2: serializeOptions(dstFullDirPath, {
remote: dstOptions, remote: dstOptions,
}), }),
_async: true,
...(options.outer && Object.keys(options.outer).length > 0 ...(options.outer && Object.keys(options.outer).length > 0
? Object.fromEntries( ? Object.fromEntries(
Object.entries(options.outer).map(([key, value]) => [ Object.entries(options.outer).map(([key, value]) => [
@@ -886,7 +883,7 @@ export async function startBisync({
await rclone('/job/status', { await rclone('/job/status', {
params: { params: {
query: { query: {
jobid: r.jobid!, jobid: r.jobid,
}, },
}, },
}), }),
@@ -940,17 +937,17 @@ export async function startSync({
const srcOptions = const srcOptions =
options.remotes && srcRemoteName && srcRemoteName in options.remotes options.remotes && srcRemoteName && srcRemoteName in options.remotes
? (JSON.parse(options.remotes[srcRemoteName] as unknown as string) as any) ? options.remotes[srcRemoteName]
: undefined : undefined
const dstOptions = const dstOptions =
options.remotes && dstRemoteName && dstRemoteName in options.remotes options.remotes && dstRemoteName && dstRemoteName in options.remotes
? (JSON.parse(options.remotes[dstRemoteName] as unknown as string) as any) ? options.remotes[dstRemoteName]
: undefined : undefined
const r = await pRetry( const r = await pRetry(
async () => async () =>
await rclone('/sync/sync', { await rcloneAsync('/sync/sync', {
params: { params: {
query: { query: {
srcFs: serializeOptions(srcFullDirPath, { srcFs: serializeOptions(srcFullDirPath, {
@@ -961,7 +958,6 @@ export async function startSync({
remote: dstOptions, remote: dstOptions,
}), }),
createEmptySrcDirs: true, createEmptySrcDirs: true,
_async: true,
}, },
}, },
}), }),
@@ -982,7 +978,7 @@ export async function startSync({
await rclone('/job/status', { await rclone('/job/status', {
params: { params: {
query: { query: {
jobid: r.jobid!, jobid: r.jobid,
}, },
}, },
}), }),
@@ -1058,7 +1054,7 @@ export async function startDelete({
const srcOptions = const srcOptions =
options.remotes && srcRemoteName && srcRemoteName in options.remotes options.remotes && srcRemoteName && srcRemoteName in options.remotes
? (JSON.parse(options.remotes[srcRemoteName] as unknown as string) as any) ? options.remotes[srcRemoteName]
: undefined : undefined
if (srcType === 'folder') { if (srcType === 'folder') {
@@ -1136,7 +1132,7 @@ export async function startPurge({
const srcOptions = const srcOptions =
options.remotes && srcRemoteName && srcRemoteName in options.remotes options.remotes && srcRemoteName && srcRemoteName in options.remotes
? (JSON.parse(options.remotes[srcRemoteName] as unknown as string) as any) ? options.remotes[srcRemoteName]
: undefined : undefined
const jobParams: Parameters<typeof startBatch>[0][number] = { const jobParams: Parameters<typeof startBatch>[0][number] = {
+56
View File
@@ -1,6 +1,7 @@
import { fetch as tauriFetch } from '@tauri-apps/plugin-http' import { fetch as tauriFetch } from '@tauri-apps/plugin-http'
import pRetry from 'p-retry' import pRetry from 'p-retry'
import createRCDClient, { import createRCDClient, {
type AsyncJobResponse,
type OpenApiMethodResponse, type OpenApiMethodResponse,
type OpenApiClient, type OpenApiClient,
type OpenApiClientPathsWithMethod, type OpenApiClientPathsWithMethod,
@@ -110,3 +111,58 @@ export default async function rclone<
return result.data as OpenApiMethodResponse<typeof client, 'post', Path, Init> return result.data as OpenApiMethodResponse<typeof client, 'post', Path, Init>
} }
export async function rcloneAsync<
Path extends OpenApiClientPathsWithMethod<RCDClient, 'post'>,
Init extends OpenApiMaybeOptionalInit<Paths[Path], 'post'> = OpenApiMaybeOptionalInit<
Paths[Path],
'post'
>,
>(
path: Path,
...init: InitParam<Init>
): Promise<AsyncJobResponse> {
console.log('[rclone] ASYNC REQUEST', path, {
params: init[0]?.params,
body: init[0]?.body,
})
const client = await pRetry(() => getClient(), {
'maxTimeout': 500,
})
if (!client) {
console.error('[rclone] ERROR: Failed to get client after retries', path)
throw new Error('Failed to get client after retries')
}
const result = await client.ASYNC(path, ...(init as [any]))
if (result?.error) {
console.error('[rclone] ERROR', path, { error: result.error })
const message =
typeof result.error === 'string' ? result.error : JSON.stringify(result.error)
throw new Error(message)
}
const data = result.data as { error?: unknown } | undefined
if (data?.error) {
console.error('[rclone] DATA ERROR', path, { error: data.error })
const message = typeof data.error === 'string' ? data.error : JSON.stringify(data.error)
throw new Error(message)
}
if (!result.response.ok) {
console.error('[rclone] HTTP ERROR', path, {
status: result.response.status,
statusText: result.response.statusText,
})
throw new Error(`${result.response.status} ${result.response.statusText}`)
}
console.log('[rclone] ASYNC RESPONSE', path, { hasData: !!result.data })
return result.data as AsyncJobResponse
}