feat(storage-vercel-blob): allow fallback to disk if token not set (#10005)

Previously with `@payloadcms/plugin-storage-blob`, if token was not set,
the plugin would throw an error. This caused a less-than-ideal developer
experience.

With this change, if the `token` value is undefined:
- Local storage will be used as a fallback
- The error will no longer be thrown.
This commit is contained in:
Elliot DeNolf
2024-12-16 16:40:22 -05:00
committed by GitHub
parent f54e180370
commit fa49e04cf8
2 changed files with 11 additions and 8 deletions

View File

@@ -52,8 +52,10 @@ export type VercelBlobStorageOptions = {
* Vercel Blob storage read/write token
*
* Usually process.env.BLOB_READ_WRITE_TOKEN set by Vercel
*
* If unset, the plugin will be disabled and will fallback to local storage
*/
token: string
token: string | undefined
}
const defaultUploadOptions: Partial<VercelBlobStorageOptions> = {
@@ -68,14 +70,11 @@ type VercelBlobStoragePlugin = (vercelBlobStorageOpts: VercelBlobStorageOptions)
export const vercelBlobStorage: VercelBlobStoragePlugin =
(options: VercelBlobStorageOptions) =>
(incomingConfig: Config): Config => {
if (options.enabled === false) {
// If the plugin is disabled or no token is provided, do not enable the plugin
if (options.enabled === false || !options.token) {
return incomingConfig
}
if (!options.token) {
throw new Error('The token argument is required for the Vercel Blob adapter.')
}
// Parse storeId from token
const storeId = options.token.match(/^vercel_blob_rw_([a-z\d]+)_[a-z\d]+$/i)?.[1]?.toLowerCase()
@@ -136,10 +135,15 @@ function vercelBlobStorageInternal(
): Adapter {
return ({ collection, prefix }): GeneratedAdapter => {
const { access, addRandomSuffix, baseUrl, cacheControlMaxAge, token } = options
if (!token) {
throw new Error('Vercel Blob storage token is required')
}
return {
name: 'vercel-blob',
generateURL: getGenerateUrl({ baseUrl, prefix }),
handleDelete: getHandleDelete({ baseUrl, prefix, token: options.token }),
handleDelete: getHandleDelete({ baseUrl, prefix, token }),
handleUpload: getHandleUpload({
access,
addRandomSuffix,