Skip to content

Commit

Permalink
Fixes issues with isomorphic params
Browse files Browse the repository at this point in the history
  • Loading branch information
Titou325 committed Jun 4, 2024
1 parent b5883da commit 65b129a
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 97 deletions.
133 changes: 82 additions & 51 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions packages/typescript/package.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
{
"name": "@fileforge/client",
"version": "0.1.9",
"version": "0.1.10",
"scripts": {
"build": "tsc --module es2022 --outDir dist/esm && tsc --module Node16 --moduleResolution Node16 --outDir dist/cjs"
},
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"types": "dist/esm/index.d.ts",
"dependencies": {
"form-data": "4.0.0",
"form-data-encoder": "^4.0.2",
"formdata-node": "^6.0.3",
"mime-types": "^2.1.35",
"node-fetch": "2.7.0",
"node-fetch": "^3.3.2",
"qs": "6.11.2",
"stream-mime-type": "^2.0.0",
"url-join": "4.0.1"
},
"devDependencies": {
"@types/node-fetch": "^2.6.11",
"@types/qs": "^6.9.15",
"@types/url-join": "^4.0.3",
"@web-std/file": "^3.0.3",
Expand Down
23 changes: 7 additions & 16 deletions packages/typescript/src/client/_patches/core/fetcher/Fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,23 +84,14 @@ async function fetcherImpl<R = unknown>(
}
};

if (RUNTIME.type === "node") {
if (
args.body instanceof (await import("formdata-node")).FormData ||
args.body instanceof Readable
) {
// @ts-expect-error
body = args.body;
} else {
body = maybeStringifyBody(args.body);
}
if (
args.body instanceof (await import("formdata-node")).FormData ||
args.body instanceof Readable
) {
// @ts-expect-error
body = args.body;
} else {
if (args.body instanceof (await import("form-data")).default) {
// @ts-expect-error
body = args.body;
} else {
body = maybeStringifyBody(args.body);
}
body = maybeStringifyBody(args.body);
}

// In Node.js environments, the SDK always uses`node-fetch`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,7 @@ export class FormDataWrapper {

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 = new (await import("formdata-node")).FormData();
}

if (name === "options" && typeof value === "string") {
Expand Down
23 changes: 7 additions & 16 deletions packages/typescript/src/client/codegen/core/fetcher/Fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,23 +84,14 @@ async function fetcherImpl<R = unknown>(
}
};

if (RUNTIME.type === "node") {
if (
args.body instanceof (await import("formdata-node")).FormData ||
args.body instanceof Readable
) {
// @ts-expect-error
body = args.body;
} else {
body = maybeStringifyBody(args.body);
}
if (
args.body instanceof (await import("formdata-node")).FormData ||
args.body instanceof Readable
) {
// @ts-expect-error
body = args.body;
} else {
if (args.body instanceof (await import("form-data")).default) {
// @ts-expect-error
body = args.body;
} else {
body = maybeStringifyBody(args.body);
}
body = maybeStringifyBody(args.body);
}

// In Node.js environments, the SDK always uses`node-fetch`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,7 @@ export class FormDataWrapper {

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 = new (await import("formdata-node")).FormData();
}

if (name === "options" && typeof value === "string") {
Expand Down
12 changes: 12 additions & 0 deletions packages/typescript/src/test/browser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import { describe, it, expect } from "vitest";
import { FileforgeClient } from "@/client";
import { fileFromPath } from "formdata-node/file-from-path";

describe("browser", () => {
it("should work", () => {
Expand All @@ -16,4 +17,15 @@ describe("browser", () => {
}),
).toBeInstanceOf(FileforgeClient);
});

it("should accept file", async () => {
const ff = new FileforgeClient({
apiKey: process.env.FILEFORGE_API_KEY,
});

await ff.pdf.form.mark(
await fileFromPath(__dirname + "/samples/form.pdf"),
{},
);
});
});

0 comments on commit 65b129a

Please sign in to comment.