Skip to content

Commit 4c93d79

Browse files
authored
fix: TypeScript type definitions for Blob, File, FormData (#1150)
1 parent eb1ab0d commit 4c93d79

File tree

1 file changed

+91
-8
lines changed

1 file changed

+91
-8
lines changed

types/globals.d.ts

Lines changed: 91 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,19 +1147,102 @@ and limitations under the License.
11471147
* Copyright (c) Fastly Corporation, under the same license as the rest of the file.
11481148
*/
11491149

1150+
type BlobPart = BufferSource | Blob | string;
1151+
type EndingType = "native" | "transparent";
1152+
type FormDataEntryValue = File | string;
1153+
1154+
interface BlobPropertyBag {
1155+
endings?: EndingType;
1156+
type?: string;
1157+
}
1158+
1159+
interface FilePropertyBag extends BlobPropertyBag {
1160+
lastModified?: number;
1161+
}
1162+
1163+
/**
1164+
* A file-like object of immutable, raw data. Blobs represent data that isn't necessarily in a JavaScript-native format. The File interface is based on Blob, inheriting blob functionality and expanding it to support files on the user's system.
1165+
*
1166+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob)
1167+
*/
1168+
interface Blob {
1169+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/size) */
1170+
readonly size: number;
1171+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/type) */
1172+
readonly type: string;
1173+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/arrayBuffer) */
1174+
arrayBuffer(): Promise<ArrayBuffer>;
1175+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/slice) */
1176+
slice(start?: number, end?: number, contentType?: string): Blob;
1177+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/stream) */
1178+
stream(): ReadableStream<Uint8Array>;
1179+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/text) */
1180+
text(): Promise<string>;
1181+
}
1182+
1183+
declare var Blob: {
1184+
prototype: Blob;
1185+
new(blobParts?: BlobPart[], options?: BlobPropertyBag): Blob;
1186+
};
1187+
1188+
/**
1189+
* Provides information about files and allows JavaScript in a web page to access their content.
1190+
*
1191+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/File)
1192+
*/
1193+
interface File extends Blob {
1194+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/File/lastModified) */
1195+
readonly lastModified: number;
1196+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/File/name) */
1197+
readonly name: string;
1198+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/File/webkitRelativePath) */
1199+
readonly webkitRelativePath: string;
1200+
}
1201+
1202+
declare var File: {
1203+
prototype: File;
1204+
new(fileBits: BlobPart[], fileName: string, options?: FilePropertyBag): File;
1205+
};
1206+
1207+
/**
1208+
* Provides a way to easily construct a set of key/value pairs representing form fields and their values, which can then be easily sent using the XMLHttpRequest.send() method. It uses the same format a form would use if the encoding type were set to "multipart/form-data".
1209+
*
1210+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/FormData)
1211+
*/
1212+
interface FormData {
1213+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/FormData/append) */
1214+
append(name: string, value: string | Blob): void;
1215+
append(name: string, value: string): void;
1216+
append(name: string, blobValue: Blob, filename?: string): void;
1217+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/FormData/delete) */
1218+
delete(name: string): void;
1219+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/FormData/get) */
1220+
get(name: string): FormDataEntryValue | null;
1221+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/FormData/getAll) */
1222+
getAll(name: string): FormDataEntryValue[];
1223+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/FormData/has) */
1224+
has(name: string): boolean;
1225+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/FormData/set) */
1226+
set(name: string, value: string | Blob): void;
1227+
set(name: string, value: string): void;
1228+
set(name: string, blobValue: Blob, filename?: string): void;
1229+
forEach(callbackfn: (value: FormDataEntryValue, key: string, parent: FormData) => void, thisArg?: any): void;
1230+
}
1231+
1232+
declare var FormData: {
1233+
prototype: FormData;
1234+
new(form?: any/*form?: HTMLFormElement, submitter?: HTMLElement | null*/): FormData;
1235+
};
1236+
11501237
/**
11511238
* Used within the
11521239
* [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request/Request) and
11531240
* [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response/Response) constructors.
11541241
* ({@linkcode Request}, and {@linkcode Response})
11551242
* @group Fetch API
11561243
*/
1157-
declare type BodyInit =
1158-
| ReadableStream
1159-
| ArrayBufferView
1160-
| ArrayBuffer
1161-
| URLSearchParams
1162-
| string;
1244+
declare type BodyInit = ReadableStream | XMLHttpRequestBodyInit;
1245+
declare type XMLHttpRequestBodyInit = Blob | BufferSource | FormData | URLSearchParams | string;
11631246

11641247
/**
11651248
* Body for Fetch HTTP Requests and Responses
@@ -1171,8 +1254,8 @@ declare interface Body {
11711254
readonly body: ReadableStream<Uint8Array> | null;
11721255
readonly bodyUsed: boolean;
11731256
arrayBuffer(): Promise<ArrayBuffer>;
1174-
// blob(): Promise<Blob>;
1175-
// formData(): Promise<FormData>;
1257+
blob(): Promise<Blob>;
1258+
formData(): Promise<FormData>;
11761259
json(): Promise<any>;
11771260
text(): Promise<string>;
11781261
}

0 commit comments

Comments
 (0)