refactor: simplify storage adapter handler pattern#16231
Merged
JarrodMFlesch merged 11 commits intomainfrom Apr 10, 2026
Merged
refactor: simplify storage adapter handler pattern#16231JarrodMFlesch merged 11 commits intomainfrom
JarrodMFlesch merged 11 commits intomainfrom
Conversation
Replace factory function pattern with simple helper functions:
- handleDelete.ts: deleteFile({ bucket, prefix, filename })
- handleUpload.ts: uploadFile({ bucket, buffer, filename, ... })
- staticHandler.ts: getFile({ bucket, collection, filename, ... })
adapter.ts maps the plugin interface to these helpers, keeping
the indirection minimal and the helper signatures clean.
- Convert factory functions to simple helper functions: - getHandleDelete → deleteFile - getHandleUpload → uploadFile - getStaticHandler → getFile - getGenerateUrl → generateURL - Create adapter.ts with createVercelBlobAdapter that maps plugin interface to helpers - Update index.ts to use createVercelBlobAdapter
Convert factory functions to simple helper functions and introduce createUploadthingAdapter to map the plugin interface to the helpers.
Convert factory functions to simple helper functions: - handleUpload.ts: uploadFile() with multipart upload logic preserved - handleDelete.ts: deleteFile() - staticHandler.ts: getFile() - generateURL.ts: generateURL() Create adapter.ts with createS3Adapter() that maps plugin interface to the helper functions. Update index.ts to use the new adapter. The multipart upload threshold (50MB) and parallel upload logic using @aws-sdk/lib-storage Upload class are preserved exactly.
Contributor
📦 esbuild Bundle Analysis for payloadThis analysis was generated by esbuild-bundle-analyzer. 🤖
Largest pathsThese visualization shows top 20 largest paths in the bundle.Meta file: packages/next/meta_index.json, Out file: esbuild/index.js
Meta file: packages/payload/meta_index.json, Out file: esbuild/index.js
Meta file: packages/payload/meta_shared.json, Out file: esbuild/exports/shared.js
Meta file: packages/richtext-lexical/meta_client.json, Out file: esbuild/exports/client_optimized/index.js
Meta file: packages/ui/meta_client.json, Out file: esbuild/exports/client_optimized/index.js
Meta file: packages/ui/meta_shared.json, Out file: esbuild/exports/shared_optimized/index.js
DetailsNext to the size is how much the size has increased or decreased compared with the base branch of this PR.
|
paulpopus
approved these changes
Apr 10, 2026
Contributor
|
🚀 This is included in version v3.83.0 |
milamer
pushed a commit
to milamer/payload
that referenced
this pull request
Apr 20, 2026
## Summary
- Replace factory function pattern (`getHandleUpload`,
`getHandleDelete`, etc.) with simple helper functions
- Create `adapter.ts` in each storage package that maps the plugin
interface to the helpers
- Fix R2 prefix bug: use OR logic (`filePrefix || prefix`) to match
`handleUpload` behavior
- Remove redundant `async/await` on pass-through handlers
I think this approach makes it easier to trace backwards to the
cloud-storage plugin the arguments that are actually passed into the
adapter methods.
## Changes
**Pattern change (all 6 storage adapters):**
Before (factory functions):
```typescript
export const getHandleDelete = ({ bucket }: Args): HandleDelete => {
return async ({ doc: { prefix = '' }, filename }) => {
await bucket.delete(path.posix.join(prefix, filename))
}
}
```
After (simple functions + adapter mapping):
```typescript
// handleDelete.ts
export async function deleteFile({ bucket, prefix, filename }: DeleteArgs): Promise<void> {
await bucket.delete(path.posix.join(prefix, filename))
}
// adapter.ts
export function createR2Adapter({ bucket, clientUploads }: CreateR2AdapterArgs): Adapter {
return ({ collection, prefix = '' }): GeneratedAdapter => ({
name: 'r2',
clientUploads,
handleDelete: ({ doc: { prefix: docPrefix = '' }, filename }) =>
deleteFile({ bucket, filename, prefix: docPrefix }),
handleUpload: ({ data, file }) =>
uploadFile({
bucket,
buffer: file.buffer,
filename: file.filename,
mimeType: file.mimeType,
prefix: data.prefix || prefix,
}),
staticHandler: (
req,
{ headers, params: { clientUploadContext, filename, prefix: prefixQueryParam } },
) =>
getFile({
bucket,
clientUploadContext,
collection,
filename,
incomingHeaders: headers,
prefix,
prefixQueryParam,
req,
}),
})
}
```
**Bug fix (storage-r2):**
The prefix query param feature (payloadcms#15844) introduced inconsistent logic in
R2:
- `handleUpload`: uses `data.prefix || prefix` (OR logic)
- `staticHandler`: was using `prefix + filePrefix` (AND logic)
Fixed to use consistent OR logic: `filePrefix || prefix`
## Packages affected
- `@payloadcms/storage-r2`
- `@payloadcms/storage-s3`
- `@payloadcms/storage-gcs`
- `@payloadcms/storage-azure`
- `@payloadcms/storage-vercel-blob`
- `@payloadcms/storage-uploadthing`
This was referenced May 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
getHandleUpload,getHandleDelete, etc.) with simple helper functionsadapter.tsin each storage package that maps the plugin interface to the helpersfilePrefix || prefix) to matchhandleUploadbehaviorasync/awaiton pass-through handlersI think this approach makes it easier to trace backwards to the cloud-storage plugin the arguments that are actually passed into the adapter methods.
Changes
Pattern change (all 6 storage adapters):
Before (factory functions):
After (simple functions + adapter mapping):
Bug fix (storage-r2):
The prefix query param feature (#15844) introduced inconsistent logic in R2:
handleUpload: usesdata.prefix || prefix(OR logic)staticHandler: was usingprefix + filePrefix(AND logic)Fixed to use consistent OR logic:
filePrefix || prefixPackages affected
@payloadcms/storage-r2@payloadcms/storage-s3@payloadcms/storage-gcs@payloadcms/storage-azure@payloadcms/storage-vercel-blob@payloadcms/storage-uploadthing