Skip to content

Commit

Permalink
better dx
Browse files Browse the repository at this point in the history
  • Loading branch information
luismeyer committed Jan 11, 2024
1 parent e4cc523 commit 0a95db9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
21 changes: 15 additions & 6 deletions packages/blob/src/put.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ export function createPutMethod<
}) {
return async function put<TPath extends string>(
pathname: TPath,
body: TPath extends `${infer _rest}/`
? undefined
bodyOrOptions: TPath extends `${string}/`
? T
:
| string
| Readable
Expand All @@ -54,14 +54,23 @@ export function createPutMethod<
| FormData
| ReadableStream
| File,
options?: T,
optionsInput?: T,
): Promise<PutBlobResult> {
if (!pathname) {
throw new BlobError('pathname is required');
}

if (!body && !pathname.endsWith('/')) {
throw new BlobError('body is required');
const isEmptyFolder = pathname.endsWith('/');

// avoid using the options as body
const body = isEmptyFolder ? undefined : (bodyOrOptions as BodyInit);

// when no body is required options are the second argument
const options = isEmptyFolder ? (bodyOrOptions as T) : optionsInput;

// runtime check for non TS users that provide all three args
if (bodyOrOptions && optionsInput && isEmptyFolder) {
throw new BlobError('body is not allowed for creating empty folders');
}

if (!options) {
Expand Down Expand Up @@ -107,7 +116,7 @@ export function createPutMethod<

const blobApiResponse = await fetch(getApiUrl(`/${pathname}`), {
method: 'PUT',
body: body as BodyInit,
body,
headers,
// required in order to stream some body types to Cloudflare
// currently only supported in Node.js, we may have to feature detect this
Expand Down
2 changes: 1 addition & 1 deletion test/next/src/app/vercel/blob/script.mts
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ async function listFolders() {
async function createFolder() {
const start = Date.now();

const blob = await vercelBlob.put('foolder/', undefined, {
const blob = await vercelBlob.put('foolder/', {
access: 'public',
});

Expand Down

0 comments on commit 0a95db9

Please sign in to comment.