Skip to content

Commit

Permalink
feat(upload-client): export isReadyPoll helper to wait for the file…
Browse files Browse the repository at this point in the history
… readiness
  • Loading branch information
nd0ut committed Oct 30, 2023
1 parent 4a717e1 commit 12cf9b1
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 22 deletions.
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 12cf9b1

Please sign in to comment.