From e140644df4e86341a8ee387cc4add8308f9b3aac Mon Sep 17 00:00:00 2001 From: Aleksandr Grenishin Date: Fri, 20 Jan 2023 12:43:16 +0400 Subject: [PATCH] feat(upload-client): accept `auto` as `store` value (#460) --- packages/upload-client/src/api/base.ts | 4 ++-- packages/upload-client/src/api/fromUrl.ts | 3 ++- packages/upload-client/src/api/multipartStart.ts | 3 ++- packages/upload-client/src/tools/getStoreValue.ts | 9 +++++++-- packages/upload-client/src/types.ts | 4 +++- packages/upload-client/src/uploadFile/uploadDirect.ts | 4 ++-- packages/upload-client/src/uploadFile/uploadFile.ts | 4 ++-- packages/upload-client/src/uploadFile/uploadMultipart.ts | 4 ++-- packages/upload-client/test/tools/getStoreValue.test.ts | 4 ++++ 9 files changed, 26 insertions(+), 13 deletions(-) diff --git a/packages/upload-client/src/api/base.ts b/packages/upload-client/src/api/base.ts index 4cd86e736..30a8f0237 100644 --- a/packages/upload-client/src/api/base.ts +++ b/packages/upload-client/src/api/base.ts @@ -16,7 +16,7 @@ import { FailedResponse } from '../request/types' import { getContentType } from '../tools/getContentType' import { getFileName } from '../tools/getFileName' import { getStoreValue } from '../tools/getStoreValue' -import { SupportedFileInput } from '../types' +import { StoreValue, SupportedFileInput } from '../types' import { ProgressCallback, Uuid } from './types' export type BaseResponse = { @@ -32,7 +32,7 @@ export type BaseOptions = { baseURL?: string secureSignature?: string secureExpire?: string - store?: boolean + store?: StoreValue contentType?: string signal?: AbortSignal diff --git a/packages/upload-client/src/api/fromUrl.ts b/packages/upload-client/src/api/fromUrl.ts index e2bf63520..a80ecb841 100644 --- a/packages/upload-client/src/api/fromUrl.ts +++ b/packages/upload-client/src/api/fromUrl.ts @@ -14,6 +14,7 @@ import { getUserAgent } from '../tools/getUserAgent' import { UploadClientError } from '../tools/errors' import { retryIfFailed } from '../tools/retryIfFailed' import { getStoreValue } from '../tools/getStoreValue' +import { StoreValue } from '../types' export enum TypeEnum { Token = 'token', @@ -57,7 +58,7 @@ export type FromUrlOptions = { publicKey: string baseURL?: string - store?: boolean + store?: StoreValue fileName?: string checkForUrlDuplicates?: boolean saveUrlForRecurrentUploads?: boolean diff --git a/packages/upload-client/src/api/multipartStart.ts b/packages/upload-client/src/api/multipartStart.ts index 91737bf45..a150b97ab 100644 --- a/packages/upload-client/src/api/multipartStart.ts +++ b/packages/upload-client/src/api/multipartStart.ts @@ -18,6 +18,7 @@ import { getUserAgent } from '../tools/getUserAgent' import { retryIfFailed } from '../tools/retryIfFailed' import { UploadClientError } from '../tools/errors' import { getStoreValue } from '../tools/getStoreValue' +import { StoreValue } from '../types' export type MultipartStartOptions = { publicKey: string @@ -26,7 +27,7 @@ export type MultipartStartOptions = { baseURL?: string secureSignature?: string secureExpire?: string - store?: boolean + store?: StoreValue multipartChunkSize?: number signal?: AbortSignal source?: string diff --git a/packages/upload-client/src/tools/getStoreValue.ts b/packages/upload-client/src/tools/getStoreValue.ts index 60a025282..82c41a8e8 100644 --- a/packages/upload-client/src/tools/getStoreValue.ts +++ b/packages/upload-client/src/tools/getStoreValue.ts @@ -1,3 +1,8 @@ -export function getStoreValue(store?: 'auto' | boolean): 'auto' | '0' | '1' { - return typeof store === 'undefined' ? 'auto' : store ? '1' : '0' +import { StoreValue } from '../types' + +export function getStoreValue(store?: StoreValue): 'auto' | '0' | '1' { + if (typeof store === 'undefined' || store === 'auto') { + return 'auto' + } + return store ? '1' : '0' } diff --git a/packages/upload-client/src/types.ts b/packages/upload-client/src/types.ts index 117efb55a..969a9cf08 100644 --- a/packages/upload-client/src/types.ts +++ b/packages/upload-client/src/types.ts @@ -1,5 +1,7 @@ import { CustomUserAgent } from '@uploadcare/api-client-utils' +export type StoreValue = 'auto' | boolean + export interface DefaultSettings { baseCDN: string baseURL: string @@ -18,7 +20,7 @@ export interface Settings extends Partial { publicKey: string fileName?: string contentType?: string - store?: boolean + store?: StoreValue secureSignature?: string secureExpire?: string integration?: string diff --git a/packages/upload-client/src/uploadFile/uploadDirect.ts b/packages/upload-client/src/uploadFile/uploadDirect.ts index b7e7ff37a..61008c172 100644 --- a/packages/upload-client/src/uploadFile/uploadDirect.ts +++ b/packages/upload-client/src/uploadFile/uploadDirect.ts @@ -4,7 +4,7 @@ import { UploadcareFile } from '../tools/UploadcareFile' import { CustomUserAgent, Metadata } from '@uploadcare/api-client-utils' import { ProgressCallback } from '../api/types' -import { SupportedFileInput } from '../types' +import { StoreValue, SupportedFileInput } from '../types' export type DirectOptions = { publicKey: string @@ -13,7 +13,7 @@ export type DirectOptions = { baseURL?: string secureSignature?: string secureExpire?: string - store?: boolean + store?: StoreValue contentType?: string signal?: AbortSignal diff --git a/packages/upload-client/src/uploadFile/uploadFile.ts b/packages/upload-client/src/uploadFile/uploadFile.ts index 4d01635a3..47eb1fd95 100644 --- a/packages/upload-client/src/uploadFile/uploadFile.ts +++ b/packages/upload-client/src/uploadFile/uploadFile.ts @@ -10,7 +10,7 @@ import { getFileSize } from '../tools/getFileSize' import { isFileData } from '../tools/isFileData' import { isMultipart } from '../tools/isMultipart' import { UploadcareFile } from '../tools/UploadcareFile' -import { SupportedFileInput } from '../types' +import { StoreValue, SupportedFileInput } from '../types' import { isUrl, isUuid } from './types' import { uploadMultipart } from './uploadMultipart' @@ -21,7 +21,7 @@ export type FileFromOptions = { baseURL?: string secureSignature?: string secureExpire?: string - store?: boolean + store?: StoreValue signal?: AbortSignal onProgress?: ProgressCallback diff --git a/packages/upload-client/src/uploadFile/uploadMultipart.ts b/packages/upload-client/src/uploadFile/uploadMultipart.ts index 90dd4f72f..23e690002 100644 --- a/packages/upload-client/src/uploadFile/uploadMultipart.ts +++ b/packages/upload-client/src/uploadFile/uploadMultipart.ts @@ -19,7 +19,7 @@ import { import { getContentType } from '../tools/getContentType' import { getFileName } from '../tools/getFileName' import { getFileSize } from '../tools/getFileSize' -import { SupportedFileInput } from '../types' +import { StoreValue, SupportedFileInput } from '../types' export type MultipartOptions = { publicKey: string @@ -30,7 +30,7 @@ export type MultipartOptions = { baseURL?: string secureSignature?: string secureExpire?: string - store?: boolean + store?: StoreValue signal?: AbortSignal onProgress?: ProgressCallback source?: string diff --git a/packages/upload-client/test/tools/getStoreValue.test.ts b/packages/upload-client/test/tools/getStoreValue.test.ts index 127e40e11..707c9085a 100644 --- a/packages/upload-client/test/tools/getStoreValue.test.ts +++ b/packages/upload-client/test/tools/getStoreValue.test.ts @@ -5,6 +5,10 @@ describe('getStoreValue', () => { expect(getStoreValue()).toEqual('auto') }) + it('should return auto if store is auto', () => { + expect(getStoreValue('auto')).toEqual('auto') + }) + it('should return `1` if store is true', () => { expect(getStoreValue(true)).toEqual('1') })