Skip to content

Commit

Permalink
chore(internal): let toFile helper accept promises to objects with …
Browse files Browse the repository at this point in the history
…name/type properties (#63)
  • Loading branch information
stainless-bot authored and RobertCraigie committed Jul 13, 2023
1 parent 04e303c commit 93f9af2
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/_shims/formdata.node.d.ts
Expand Up @@ -10,6 +10,7 @@ type EndingType = 'native' | 'transparent';

export interface BlobPropertyBag {
endings?: EndingType;
/** MIME type, e.g., "text/plain" */
type?: string;
}

Expand Down
10 changes: 5 additions & 5 deletions src/uploads.ts
Expand Up @@ -88,7 +88,7 @@ export type ToFileInput = Uploadable | Exclude<BlobPart, string> | AsyncIterable

/**
* Helper for creating a {@link File} to pass to an SDK upload method from a variety of different data formats
* @param bits the raw content of the file. Can be an {@link Uploadable}, {@link BlobPart}, or {@link AsyncIterable} of {@link BlobPart}s
* @param value the raw content of the file. Can be an {@link Uploadable}, {@link BlobPart}, or {@link AsyncIterable} of {@link BlobPart}s
* @param {string=} name the name of the file. If omitted, toFile will try to determine a file name from bits if possible
* @param {Object=} options additional properties
* @param {string=} options.type the MIME type of the content
Expand All @@ -100,6 +100,9 @@ export async function toFile(
name?: string | null | undefined,
options: FilePropertyBag | undefined = {},
): Promise<FileLike> {
// If it's a promise, resolve it.
value = await value;

if (isResponseLike(value)) {
const blob = await value.blob();
name ||= new URL(value.url).pathname.split(/[\\/]/).pop() ?? 'unknown_file';
Expand All @@ -121,10 +124,7 @@ export async function toFile(
return new File(bits, name, options);
}

async function getBytes(value: ToFileInput | PromiseLike<ToFileInput>): Promise<Array<BlobPart>> {
// resolve input promise or promiselike object
value = await value;

async function getBytes(value: ToFileInput): Promise<Array<BlobPart>> {
let parts: Array<BlobPart> = [];
if (
typeof value === 'string' ||
Expand Down

0 comments on commit 93f9af2

Please sign in to comment.