Skip to content

Commit a57920a

Browse files
chore(internal): let toFile helper accept promises to objects with name/type properties (#63)
1 parent 9f80423 commit a57920a

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

src/_shims/formdata.node.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type EndingType = 'native' | 'transparent';
1010

1111
export interface BlobPropertyBag {
1212
endings?: EndingType;
13+
/** MIME type, e.g., "text/plain" */
1314
type?: string;
1415
}
1516

src/uploads.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export type ToFileInput = Uploadable | Exclude<BlobPart, string> | AsyncIterable
8888

8989
/**
9090
* Helper for creating a {@link File} to pass to an SDK upload method from a variety of different data formats
91-
* @param bits the raw content of the file. Can be an {@link Uploadable}, {@link BlobPart}, or {@link AsyncIterable} of {@link BlobPart}s
91+
* @param value the raw content of the file. Can be an {@link Uploadable}, {@link BlobPart}, or {@link AsyncIterable} of {@link BlobPart}s
9292
* @param {string=} name the name of the file. If omitted, toFile will try to determine a file name from bits if possible
9393
* @param {Object=} options additional properties
9494
* @param {string=} options.type the MIME type of the content
@@ -100,6 +100,9 @@ export async function toFile(
100100
name?: string | null | undefined,
101101
options: FilePropertyBag | undefined = {},
102102
): Promise<FileLike> {
103+
// If it's a promise, resolve it.
104+
value = await value;
105+
103106
if (isResponseLike(value)) {
104107
const blob = await value.blob();
105108
name ||= new URL(value.url).pathname.split(/[\\/]/).pop() ?? 'unknown_file';
@@ -121,10 +124,7 @@ export async function toFile(
121124
return new File(bits, name, options);
122125
}
123126

124-
async function getBytes(value: ToFileInput | PromiseLike<ToFileInput>): Promise<Array<BlobPart>> {
125-
// resolve input promise or promiselike object
126-
value = await value;
127-
127+
async function getBytes(value: ToFileInput): Promise<Array<BlobPart>> {
128128
let parts: Array<BlobPart> = [];
129129
if (
130130
typeof value === 'string' ||

0 commit comments

Comments
 (0)