-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d96b8f0
commit a820cb1
Showing
6 changed files
with
241 additions
and
46 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { RUNTIME } from "../runtime"; | ||
|
||
interface CrossPlatformFormData { | ||
append(key: string, value: any): void; | ||
} | ||
|
||
class FormDataRequestBody { | ||
private fd: any; | ||
private encoder: any; | ||
|
||
constructor(fd: any) { | ||
this.fd = fd; | ||
} | ||
|
||
async setup(): Promise<void> { | ||
if (this.encoder == null && RUNTIME.type === "node") { | ||
this.encoder = new (await import("form-data-encoder")).FormDataEncoder(this.fd); | ||
} | ||
} | ||
|
||
/** | ||
* @returns the multipart form data request | ||
*/ | ||
public async getBody(): Promise<any> { | ||
if (RUNTIME.type !== "node") { | ||
return this.fd; | ||
} else { | ||
if (this.encoder == null) { | ||
await this.setup(); | ||
} | ||
return (await import("node:stream")).Readable.from(this.encoder); | ||
} | ||
} | ||
|
||
/** | ||
* @returns headers that need to be added to the multipart form data request | ||
*/ | ||
public async getHeaders(): Promise<Record<string, string>> { | ||
if (RUNTIME.type !== "node") { | ||
return {}; | ||
} else { | ||
if (this.encoder == null) { | ||
await this.setup(); | ||
} | ||
return { | ||
"Content-Length": this.encoder.length, | ||
}; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* FormDataWrapper is a utility to make form data | ||
* requests across both Browser and Node.js runtimes. | ||
*/ | ||
export class FormDataWrapper { | ||
private fd: CrossPlatformFormData | undefined; | ||
|
||
public async append(name: string, value: any): Promise<void> { | ||
if (this.fd == null) { | ||
if (RUNTIME.type === "node") { | ||
this.fd = new (await import("formdata-node")).FormData(); | ||
} else { | ||
this.fd = new (await import("form-data")).default(); | ||
} | ||
} | ||
this.fd.append(name, value); | ||
} | ||
|
||
public getRequest(): FormDataRequestBody { | ||
return new FormDataRequestBody(this.fd); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export { newFormData } from "./FormData"; | ||
export * from "./FormDataWrapper"; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
export * from "./fetcher"; | ||
export * from "./runtime"; | ||
export * from "./auth"; | ||
export * from "./form-data-utils"; | ||
export * from "./runtime"; | ||
export * as serialization from "./schemas"; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -637,6 +637,11 @@ | |
"@types/tough-cookie" "*" | ||
parse5 "^7.0.0" | ||
|
||
"@types/mime-types@^2.1.4": | ||
version "2.1.4" | ||
resolved "https://registry.yarnpkg.com/@types/mime-types/-/mime-types-2.1.4.tgz#93a1933e24fed4fb9e4adc5963a63efcbb3317a2" | ||
integrity sha512-lfU4b34HOri+kAY5UheuFMWPDOI+OPceBSHZKp69gEyTL/mmJ4cnU6Y/rlme3UL3GyOn6Y42hyIEw0/q8sWx5w== | ||
|
||
"@types/[email protected]": | ||
version "2.6.9" | ||
resolved "https://registry.yarnpkg.com/@types/node-fetch/-/node-fetch-2.6.9.tgz#15f529d247f1ede1824f7e7acdaa192d5f28071e" | ||
|
@@ -1903,11 +1908,6 @@ [email protected]: | |
import-local "^3.0.2" | ||
jest-cli "^29.7.0" | ||
|
||
[email protected]: | ||
version "3.7.2" | ||
resolved "https://registry.yarnpkg.com/js-base64/-/js-base64-3.7.2.tgz#816d11d81a8aff241603d19ce5761e13e41d7745" | ||
integrity sha512-NnRs6dsyqUXejqk/yv2aiXlAvOs56sLkX6nUdeaNezI5LFFLlsZjOThmwnrcwh5ZZRwZlCMnVAY3CvhIhoVEKQ== | ||
|
||
js-tokens@^4.0.0: | ||
version "4.0.0" | ||
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499" | ||
|
@@ -2039,7 +2039,7 @@ [email protected]: | |
resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.52.0.tgz#bbabcdc02859f4987301c856e3387ce5ec43bf70" | ||
integrity sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg== | ||
|
||
mime-types@^2.1.12: | ||
mime-types@^2.1.12, mime-types@^2.1.35: | ||
version "2.1.35" | ||
resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.35.tgz#381a871b62a734450660ae3deee44813f70d959a" | ||
integrity sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw== | ||
|