Skip to content

Commit 3c8cd66

Browse files
committed
patch sdk to get it working
1 parent 3c8e36e commit 3c8cd66

File tree

9 files changed

+255
-229
lines changed

9 files changed

+255
-229
lines changed

.github/workflows/ci.yml

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,53 +3,53 @@ name: ci
33
on: [push]
44

55
jobs:
6-
compile:
7-
runs-on: ubuntu-latest
6+
compile:
7+
runs-on: ubuntu-latest
88

9-
steps:
10-
- name: Checkout repo
11-
uses: actions/checkout@v3
9+
steps:
10+
- name: Checkout repo
11+
uses: actions/checkout@v3
1212

13-
- name: Set up node
14-
uses: actions/setup-node@v3
13+
- name: Set up node
14+
uses: actions/setup-node@v3
1515

16-
- name: Compile
17-
run: yarn && yarn build
16+
- name: Compile
17+
run: yarn && yarn build
1818

19-
test:
20-
runs-on: ubuntu-latest
19+
test:
20+
runs-on: ubuntu-latest
2121

22-
steps:
23-
- name: Checkout repo
24-
uses: actions/checkout@v3
22+
steps:
23+
- name: Checkout repo
24+
uses: actions/checkout@v3
2525

26-
- name: Set up node
27-
uses: actions/setup-node@v3
26+
- name: Set up node
27+
uses: actions/setup-node@v3
2828

29-
- name: Compile
30-
run: yarn && yarn test
29+
- name: Compile
30+
run: yarn && yarn test
3131

32-
publish:
33-
needs: [ compile, test ]
34-
if: github.event_name == 'push' && contains(github.ref, 'refs/tags/')
35-
runs-on: ubuntu-latest
32+
publish:
33+
needs: [compile, test]
34+
if: github.event_name == 'push' && contains(github.ref, 'refs/tags/')
35+
runs-on: ubuntu-latest
3636

37-
steps:
38-
- name: Checkout repo
39-
uses: actions/checkout@v3
37+
steps:
38+
- name: Checkout repo
39+
uses: actions/checkout@v3
4040

41-
- name: Set up node
42-
uses: actions/setup-node@v3
41+
- name: Set up node
42+
uses: actions/setup-node@v3
4343

44-
- name: Install dependencies
45-
run: yarn install
44+
- name: Install dependencies
45+
run: yarn install
4646

47-
- name: Build
48-
run: yarn build
47+
- name: Build
48+
run: yarn build
4949

50-
- name: Publish to npm
51-
run: |
52-
npm config set //registry.npmjs.org/:_authToken ${NPM_TOKEN}
53-
npm publish --access public
54-
env:
55-
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
50+
- name: Publish to npm
51+
run: |
52+
npm config set //registry.npmjs.org/:_authToken ${NPM_TOKEN}
53+
npm publish --access public
54+
env:
55+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

README.md

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,24 @@ yarn add fileforge
1919
## Usage
2020

2121
```typescript
22-
import { FileForgeClient, FileForge } from 'guesty';
22+
import { FileForgeClient, FileForge } from "guesty";
2323
import * as fs from "fs";
2424

2525
const fileforge = new FileForgeClient({
26-
apiKey: "...",
26+
apiKey: "...",
2727
});
2828

2929
await fileforge.generate({
30-
files: [
31-
fs.readStream("index.html"),
32-
],
30+
files: [fs.readStream("index.html")],
3331
options: {
34-
fileName: "output.pdf"
35-
}
32+
fileName: "output.pdf",
33+
},
3634
});
3735
```
3836

3937
## Exception Handling
4038

41-
When the API returns a non-success status code (4xx or 5xx response),
39+
When the API returns a non-success status code (4xx or 5xx response),
4240
a subclass of [FileForgeError](./src/errors/FileForgeError.ts) will be thrown:
4341

4442
```ts
@@ -48,9 +46,9 @@ try {
4846
await fileforge.generate(...);
4947
} catch (err) {
5048
if (err instanceof FileForgeError) {
51-
console.log(err.statusCode);
49+
console.log(err.statusCode);
5250
console.log(err.message);
53-
console.log(err.body);
51+
console.log(err.body);
5452
}
5553
}
5654
```
@@ -63,11 +61,11 @@ than the configured retry limit (default: 2).
6361

6462
A request is deemed retriable when any of the following HTTP status codes is returned:
6563

66-
- [408](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/408) (Timeout)
67-
- [429](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429) (Too Many Requests)
68-
- [5XX](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500) (Internal Server Errors)
69-
70-
Use the `maxRetries` request option to configure this behavior.
64+
- [408](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/408) (Timeout)
65+
- [429](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429) (Too Many Requests)
66+
- [5XX](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500) (Internal Server Errors)
67+
68+
Use the `maxRetries` request option to configure this behavior.
7169

7270
```ts
7371
const response = await fileforge.generate(..., {
@@ -77,8 +75,8 @@ const response = await fileforge.generate(..., {
7775

7876
## Timeouts
7977

80-
The SDK defaults to a 60 second timout. Use the `timeoutInSeconds` option to
81-
configure this behavior.
78+
The SDK defaults to a 60 second timout. Use the `timeoutInSeconds` option to
79+
configure this behavior.
8280

8381
```ts
8482
const response = await fileforge.generate(..., {
@@ -88,23 +86,23 @@ const response = await fileforge.generate(..., {
8886

8987
## Runtime compatiblity
9088

91-
The SDK defaults to `node-fetch` but will use the global fetch client if present. The SDK
92-
works in the following runtimes:
89+
The SDK defaults to `node-fetch` but will use the global fetch client if present. The SDK
90+
works in the following runtimes:
9391

9492
The following runtimes are supported:
9593

96-
- Node.js 18+
97-
- Vercel
98-
- Cloudflare Workers
99-
- Deno v1.25+
100-
- Bun 1.0+
101-
- React Native
94+
- Node.js 18+
95+
- Vercel
96+
- Cloudflare Workers
97+
- Deno v1.25+
98+
- Bun 1.0+
99+
- React Native
102100

103101
### Customizing Fetch client
104102

105-
The SDK provides a way for you to customize the underlying HTTP client / Fetch function. If you're
106-
running in an unsupported environment, this provides a way for you to break the glass and
107-
ensure the SDK works.
103+
The SDK provides a way for you to customize the underlying HTTP client / Fetch function. If you're
104+
running in an unsupported environment, this provides a way for you to break the glass and
105+
ensure the SDK works.
108106

109107
```ts
110108
import { FileForge } from 'fileforge';
@@ -117,16 +115,16 @@ const guesty = new FileForge({
117115

118116
## Beta status
119117

120-
This SDK is in beta, and there may be breaking changes between versions without a major version update.
121-
Therefore, we recommend pinning the package version to a specific version in your package.json file.
122-
This way, you can install the same version each time without breaking changes unless you are
118+
This SDK is in beta, and there may be breaking changes between versions without a major version update.
119+
Therefore, we recommend pinning the package version to a specific version in your package.json file.
120+
This way, you can install the same version each time without breaking changes unless you are
123121
intentionally looking for the latest version.
124122

125123
## Contributing
126124

127-
While we value open-source contributions to this SDK, this library is generated programmatically.
128-
Additions made directly to this library would have to be moved over to our generation code,
129-
otherwise they would be overwritten upon the next generated release. Feel free to open a
130-
PR as a proof of concept, but know that we will not be able to merge it as-is.
125+
While we value open-source contributions to this SDK, this library is generated programmatically.
126+
Additions made directly to this library would have to be moved over to our generation code,
127+
otherwise they would be overwritten upon the next generated release. Feel free to open a
128+
PR as a proof of concept, but know that we will not be able to merge it as-is.
131129

132130
We suggest [opening an issue](https://github.com/FlatFilers/flatfile-node/issues) first to discuss with us!

src/Client.ts

Lines changed: 21 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,22 @@ export class FileForgeClient {
3838
request: FileForge.GenerateRequest,
3939
requestOptions?: FileForgeClient.RequestOptions
4040
): Promise<stream.Readable> {
41-
const _request = new core.FormDataWrapper();
42-
await _request.append("options", JSON.stringify(request.options), { contentType: "application/json"});
41+
const _request = core.newFormData();
42+
const options = await serializers.GenerateRequestOptions.jsonOrThrow(request.options, {
43+
unrecognizedObjectKeys: "passthrough",
44+
allowUnrecognizedUnionMembers: false,
45+
allowUnrecognizedEnumValues: false,
46+
breadcrumbsPrefix: [""],
47+
});
48+
console.log(options);
49+
await _request.append(
50+
"options",
51+
JSON.stringify(options),
52+
{ contentType: "application/json" }
53+
);
4354
for (const _file of files) {
4455
await _request.append("files", _file);
4556
}
46-
47-
const _maybeEncodedRequest = _request.getRequest();
4857
const _response = await core.fetcher<stream.Readable>({
4958
url: urlJoin(
5059
(await core.Supplier.get(this._options.environment)) ?? environments.FileForgeEnvironment.Default,
@@ -58,54 +67,24 @@ export class FileForgeClient {
5867
"X-Fern-SDK-Version": "0.0.1",
5968
"X-Fern-Runtime": core.RUNTIME.type,
6069
"X-Fern-Runtime-Version": core.RUNTIME.version,
61-
...(await _maybeEncodedRequest.getHeaders()),
70+
...(await _request.getHeaders()),
6271
},
63-
body: await _maybeEncodedRequest.getBody(),
72+
body: await _request.getBody(),
6473
responseType: "streaming",
6574
timeoutMs: requestOptions?.timeoutInSeconds != null ? requestOptions.timeoutInSeconds * 1000 : 60000,
6675
maxRetries: requestOptions?.maxRetries,
6776
});
77+
console.log("_response", JSON.stringify(_response))
6878
if (_response.ok) {
79+
console.log("_response.body", _response.body);
6980
return _response.body;
7081
}
7182

7283
if (_response.error.reason === "status-code") {
73-
switch (_response.error.statusCode) {
74-
case 400:
75-
throw new FileForge.BadRequestError(
76-
await serializers.ErrorSchema.parseOrThrow(_response.error.body, {
77-
unrecognizedObjectKeys: "passthrough",
78-
allowUnrecognizedUnionMembers: true,
79-
allowUnrecognizedEnumValues: true,
80-
breadcrumbsPrefix: ["response"],
81-
})
82-
);
83-
case 401:
84-
throw new FileForge.UnauthorizedError(
85-
await serializers.ErrorSchema.parseOrThrow(_response.error.body, {
86-
unrecognizedObjectKeys: "passthrough",
87-
allowUnrecognizedUnionMembers: true,
88-
allowUnrecognizedEnumValues: true,
89-
breadcrumbsPrefix: ["response"],
90-
})
91-
);
92-
case 500:
93-
throw new FileForge.InternalServerError(_response.error.body);
94-
case 502:
95-
throw new FileForge.BadGatewayError(
96-
await serializers.ErrorSchema.parseOrThrow(_response.error.body, {
97-
unrecognizedObjectKeys: "passthrough",
98-
allowUnrecognizedUnionMembers: true,
99-
allowUnrecognizedEnumValues: true,
100-
breadcrumbsPrefix: ["response"],
101-
})
102-
);
103-
default:
104-
throw new errors.FileForgeError({
105-
statusCode: _response.error.statusCode,
106-
body: _response.error.body,
107-
});
108-
}
84+
throw new errors.FileForgeError({
85+
statusCode: _response.error.statusCode,
86+
body: _response.error.body,
87+
});
10988
}
11089

11190
switch (_response.error.reason) {

src/core/fetcher/Fetcher.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { default as FormData } from "form-data";
21
import qs from "qs";
32
import { RUNTIME } from "../runtime";
43
import { APIResponse } from "./APIResponse";
@@ -67,13 +66,28 @@ async function fetcherImpl<R = unknown>(args: Fetcher.Args): Promise<APIResponse
6766
: args.url;
6867

6968
let body: BodyInit | undefined = undefined;
70-
if (args.body instanceof FormData) {
71-
// @ts-expect-error
72-
body = args.body;
73-
} else if (args.body instanceof Uint8Array) {
74-
body = args.body;
69+
const maybeStringifyBody = (body: any) => {
70+
if (body instanceof Uint8Array) {
71+
return body;
72+
} else {
73+
return JSON.stringify(body);
74+
}
75+
};
76+
77+
if (RUNTIME.type === "node") {
78+
if (args.body instanceof (await import("formdata-node")).FormData) {
79+
// @ts-expect-error
80+
body = args.body;
81+
} else {
82+
body = maybeStringifyBody(args.body);
83+
}
7584
} else {
76-
body = JSON.stringify(args.body);
85+
if (args.body instanceof (await import("form-data")).default) {
86+
// @ts-expect-error
87+
body = args.body;
88+
} else {
89+
body = maybeStringifyBody(args.body);
90+
}
7791
}
7892

7993
// In Node.js environments, the SDK always uses`node-fetch`.
@@ -194,4 +208,4 @@ async function fetcherImpl<R = unknown>(args: Fetcher.Args): Promise<APIResponse
194208
}
195209
}
196210

197-
export const fetcher: FetchFunction = fetcherImpl;
211+
export const fetcher: FetchFunction = fetcherImpl;

0 commit comments

Comments
 (0)