Skip to content

Commit

Permalink
Merge pull request #491 from uploadcare/feat/is-ready-poll
Browse files Browse the repository at this point in the history
Feat/is-ready-poll
  • Loading branch information
nd0ut authored Oct 30, 2023
2 parents 4a717e1 + 92c4228 commit 316e9bb
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 23 deletions.
4 changes: 3 additions & 1 deletion packages/rest-client/src/tools/addonJobPoller.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { jest } from '@jest/globals'
import { ADDONS_UUID } from '../../test/fixtures'
import { testSettings } from '../../test/helpers'
import { testSettings, uploadClient } from '../../test/helpers'
import { copyFileToLocalStorage } from '../api/file/copyFileToLocalStorage'
import { AddonExecutionStatus } from '../types/AddonExecutionStatus'
import { AddonName } from '../types/AddonName'
Expand All @@ -15,6 +15,8 @@ describe('addonJobPoller', () => {
testSettings
)

await uploadClient.isReadyPoll(copy.result.uuid, testSettings)

const result = await addonJobPoller(
{
addonName: AddonName.UC_CLAMAV_VIRUS_SCAN,
Expand Down
10 changes: 10 additions & 0 deletions packages/upload-client/src/UploadClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { FileFromOptions, uploadFile } from './uploadFile'
import { FileInfo, GroupId, GroupInfo, Token, Url, Uuid } from './api/types'
import { Settings, Sliceable, SupportedFileInput } from './types'
import { GroupFromOptions, uploadFileGroup } from './uploadFileGroup'
import { IsReadyPoolOptions, isReadyPoll } from './tools/isReadyPoll'

/** Populate options with settings. */
const populateOptionsWithSettings = <T>(
Expand Down Expand Up @@ -155,4 +156,13 @@ export default class UploadClient {

return uploadFileGroup(data, populateOptionsWithSettings(options, settings))
}

isReadyPoll(
uuid: Uuid,
options: Partial<IsReadyPoolOptions> = {}
): Promise<FileInfo> {
const settings = this.getSettings()

return isReadyPoll(uuid, populateOptionsWithSettings(options, settings))
}
}
1 change: 1 addition & 0 deletions packages/upload-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,4 @@ export {
ComputableProgressInfo,
UnknownProgressInfo
} from './api/types'
export { isReadyPoll } from './tools/isReadyPoll'
34 changes: 18 additions & 16 deletions packages/upload-client/src/tools/isReadyPoll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import info from '../api/info'
import {
ComputableProgressInfo,
FileInfo,
ProgressCallback
ProgressCallback,
Uuid
} from '../api/types'
import { CustomUserAgent, poll } from '@uploadcare/api-client-utils'

type ArgsIsReadyPool = {
file: string
export type IsReadyPoolOptions = {
publicKey: string
baseURL?: string
source?: string
Expand All @@ -19,21 +19,23 @@ type ArgsIsReadyPool = {
signal?: AbortSignal
}

function isReadyPoll({
file,
publicKey,
baseURL,
source,
integration,
userAgent,
retryThrottledRequestMaxTimes,
retryNetworkErrorMaxTimes,
signal,
onProgress
}: ArgsIsReadyPool): FileInfo | PromiseLike<FileInfo> {
function isReadyPoll(
uuid: Uuid,
{
publicKey,
baseURL,
source,
integration,
userAgent,
retryThrottledRequestMaxTimes,
retryNetworkErrorMaxTimes,
signal,
onProgress
}: IsReadyPoolOptions
): Promise<FileInfo> {
return poll<FileInfo>({
check: (signal) =>
info(file, {
info(uuid, {
publicKey,
baseURL,
signal,
Expand Down
3 changes: 1 addition & 2 deletions packages/upload-client/src/uploadFile/uploadDirect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ export const uploadDirect = (
metadata
})
.then(({ file }) => {
return isReadyPoll({
file,
return isReadyPoll(file, {
publicKey,
baseURL,
source,
Expand Down
3 changes: 1 addition & 2 deletions packages/upload-client/src/uploadFile/uploadFromUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,7 @@ export const uploadFromUrl = (
return result
})
.then((result) =>
isReadyPoll({
file: result.uuid,
isReadyPoll(result.uuid, {
publicKey,
baseURL,
integration,
Expand Down
3 changes: 1 addition & 2 deletions packages/upload-client/src/uploadFile/uploadMultipart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,7 @@ export const uploadMultipart = async (
if (fileInfo.isReady) {
return fileInfo
} else {
return isReadyPoll({
file: fileInfo.uuid,
return isReadyPoll(fileInfo.uuid, {
publicKey,
baseURL,
source,
Expand Down

0 comments on commit 316e9bb

Please sign in to comment.