Skip to content

Commit 39f3fa5

Browse files
committed
Release 0.0.13
1 parent 29e907c commit 39f3fa5

23 files changed

+356
-521
lines changed

src/Client.ts

+60-214
Large diffs are not rendered by default.

src/api/client/requests/ConvertDocxRequest.ts

-38
This file was deleted.

src/api/client/requests/GenerateRequest.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
* This file was auto-generated by Fern from our API Definition.
33
*/
44

5-
import * as FileForge from "../../index";
5+
import * as Fileforge from "../../index";
66

77
/**
88
* @example
99
* {}
1010
*/
1111
export interface GenerateRequest {
1212
/** Conversion options. This field is required even if empty. */
13-
options: FileForge.GenerateRequestOptions;
13+
options: Fileforge.GenerateRequestOptions;
1414
}

src/api/client/requests/MergeRequest.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
* This file was auto-generated by Fern from our API Definition.
33
*/
44

5-
import * as FileForge from "../../index";
5+
import * as Fileforge from "../../index";
66

77
/**
88
* @example
99
* {}
1010
*/
1111
export interface MergeRequest {
12-
options: FileForge.MergeRequestOptions;
12+
options: Fileforge.MergeRequestOptions;
1313
}

src/api/client/requests/index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
export { type ConvertDocxRequest } from "./ConvertDocxRequest";
21
export { type GenerateRequest } from "./GenerateRequest";
32
export { type MergeRequest } from "./MergeRequest";

src/api/errors/BadGatewayError.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
*/
44

55
import * as errors from "../../errors/index";
6-
import * as FileForge from "../index";
6+
import * as Fileforge from "../index";
77

8-
export class BadGatewayError extends errors.FileForgeError {
9-
constructor(body: FileForge.ErrorSchema) {
8+
export class BadGatewayError extends errors.FileforgeError {
9+
constructor(body: Fileforge.ErrorSchema) {
1010
super({
1111
message: "BadGatewayError",
1212
statusCode: 502,

src/api/errors/BadRequestError.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
*/
44

55
import * as errors from "../../errors/index";
6-
import * as FileForge from "../index";
6+
import * as Fileforge from "../index";
77

8-
export class BadRequestError extends errors.FileForgeError {
9-
constructor(body: FileForge.ErrorSchema) {
8+
export class BadRequestError extends errors.FileforgeError {
9+
constructor(body: Fileforge.ErrorSchema) {
1010
super({
1111
message: "BadRequestError",
1212
statusCode: 400,

src/api/errors/InternalServerError.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import * as errors from "../../errors/index";
66

7-
export class InternalServerError extends errors.FileForgeError {
7+
export class InternalServerError extends errors.FileforgeError {
88
constructor(body?: unknown) {
99
super({
1010
message: "InternalServerError",

src/api/errors/UnauthorizedError.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
*/
44

55
import * as errors from "../../errors/index";
6-
import * as FileForge from "../index";
6+
import * as Fileforge from "../index";
77

8-
export class UnauthorizedError extends errors.FileForgeError {
9-
constructor(body: FileForge.ErrorSchema) {
8+
export class UnauthorizedError extends errors.FileforgeError {
9+
constructor(body: Fileforge.ErrorSchema) {
1010
super({
1111
message: "UnauthorizedError",
1212
statusCode: 401,

src/api/types/ConvertDocxRequestOptions.ts

-33
This file was deleted.

src/api/types/index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
export * from "./ConvertDocxRequestOptions";
21
export * from "./GenerateRequestOptions";
32
export * from "./MergeRequestOptions";
43
export * from "./ErrorSchema";

src/core/fetcher/Fetcher.ts

+88-14
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";
@@ -16,6 +15,7 @@ export declare namespace Fetcher {
1615
timeoutMs?: number;
1716
maxRetries?: number;
1817
withCredentials?: boolean;
18+
abortSignal?: AbortSignal;
1919
responseType?: "json" | "blob" | "streaming" | "text";
2020
}
2121

@@ -67,13 +67,28 @@ async function fetcherImpl<R = unknown>(args: Fetcher.Args): Promise<APIResponse
6767
: args.url;
6868

6969
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;
70+
const maybeStringifyBody = (body: any) => {
71+
if (body instanceof Uint8Array) {
72+
return body;
73+
} else {
74+
return JSON.stringify(body);
75+
}
76+
};
77+
78+
if (RUNTIME.type === "node") {
79+
if (args.body instanceof (await import("formdata-node")).FormData) {
80+
// @ts-expect-error
81+
body = args.body;
82+
} else {
83+
body = maybeStringifyBody(args.body);
84+
}
7585
} else {
76-
body = JSON.stringify(args.body);
86+
if (args.body instanceof (await import("form-data")).default) {
87+
// @ts-expect-error
88+
body = args.body;
89+
} else {
90+
body = maybeStringifyBody(args.body);
91+
}
7792
}
7893

7994
// In Node.js environments, the SDK always uses`node-fetch`.
@@ -89,21 +104,33 @@ async function fetcherImpl<R = unknown>(args: Fetcher.Args): Promise<APIResponse
89104
: ((await import("node-fetch")).default as any);
90105

91106
const makeRequest = async (): Promise<Response> => {
92-
const controller = new AbortController();
93-
let abortId = undefined;
107+
const signals: AbortSignal[] = [];
108+
109+
// Add timeout signal
110+
let timeoutAbortId: NodeJS.Timeout | undefined = undefined;
94111
if (args.timeoutMs != null) {
95-
abortId = setTimeout(() => controller.abort(), args.timeoutMs);
112+
const { signal, abortId } = getTimeoutSignal(args.timeoutMs);
113+
timeoutAbortId = abortId;
114+
signals.push(signal);
96115
}
116+
117+
// Add arbitrary signal
118+
if (args.abortSignal != null) {
119+
signals.push(args.abortSignal);
120+
}
121+
97122
const response = await fetchFn(url, {
98123
method: args.method,
99124
headers,
100125
body,
101-
signal: controller.signal,
126+
signal: anySignal(signals),
102127
credentials: args.withCredentials ? "include" : undefined,
103128
});
104-
if (abortId != null) {
105-
clearTimeout(abortId);
129+
130+
if (timeoutAbortId != null) {
131+
clearTimeout(timeoutAbortId);
106132
}
133+
107134
return response;
108135
};
109136

@@ -167,7 +194,15 @@ async function fetcherImpl<R = unknown>(args: Fetcher.Args): Promise<APIResponse
167194
};
168195
}
169196
} catch (error) {
170-
if (error instanceof Error && error.name === "AbortError") {
197+
if (args.abortSignal != null && args.abortSignal.aborted) {
198+
return {
199+
ok: false,
200+
error: {
201+
reason: "unknown",
202+
errorMessage: "The user aborted a request",
203+
},
204+
};
205+
} else if (error instanceof Error && error.name === "AbortError") {
171206
return {
172207
ok: false,
173208
error: {
@@ -194,4 +229,43 @@ async function fetcherImpl<R = unknown>(args: Fetcher.Args): Promise<APIResponse
194229
}
195230
}
196231

232+
const TIMEOUT = "timeout";
233+
234+
function getTimeoutSignal(timeoutMs: number): { signal: AbortSignal; abortId: NodeJS.Timeout } {
235+
const controller = new AbortController();
236+
const abortId = setTimeout(() => controller.abort(TIMEOUT), timeoutMs);
237+
return { signal: controller.signal, abortId };
238+
}
239+
240+
/**
241+
* Returns an abort signal that is getting aborted when
242+
* at least one of the specified abort signals is aborted.
243+
*
244+
* Requires at least node.js 18.
245+
*/
246+
function anySignal(...args: AbortSignal[] | [AbortSignal[]]): AbortSignal {
247+
// Allowing signals to be passed either as array
248+
// of signals or as multiple arguments.
249+
const signals = <AbortSignal[]>(args.length === 1 && Array.isArray(args[0]) ? args[0] : args);
250+
251+
const controller = new AbortController();
252+
253+
for (const signal of signals) {
254+
if (signal.aborted) {
255+
// Exiting early if one of the signals
256+
// is already aborted.
257+
controller.abort((signal as any)?.reason);
258+
break;
259+
}
260+
261+
// Listening for signals and removing the listeners
262+
// when at least one symbol is aborted.
263+
signal.addEventListener("abort", () => controller.abort((signal as any)?.reason), {
264+
signal: controller.signal,
265+
});
266+
}
267+
268+
return controller.signal;
269+
}
270+
197271
export const fetcher: FetchFunction = fetcherImpl;

src/environments.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
* This file was auto-generated by Fern from our API Definition.
33
*/
44

5-
export const FileForgeEnvironment = {
5+
export const FileforgeEnvironment = {
66
Default: "https://api.fileforge.com",
77
} as const;
88

9-
export type FileForgeEnvironment = typeof FileForgeEnvironment.Default;
9+
export type FileforgeEnvironment = typeof FileforgeEnvironment.Default;

src/errors/FileForgeError.ts renamed to src/errors/FileforgeError.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
* This file was auto-generated by Fern from our API Definition.
33
*/
44

5-
export class FileForgeError extends Error {
5+
export class FileforgeError extends Error {
66
readonly statusCode?: number;
77
readonly body?: unknown;
88

99
constructor({ message, statusCode, body }: { message?: string; statusCode?: number; body?: unknown }) {
1010
super(buildMessage({ message, statusCode, body }));
11-
Object.setPrototypeOf(this, FileForgeError.prototype);
11+
Object.setPrototypeOf(this, FileforgeError.prototype);
1212
if (statusCode != null) {
1313
this.statusCode = statusCode;
1414
}

src/errors/FileForgeTimeoutError.ts renamed to src/errors/FileforgeTimeoutError.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
* This file was auto-generated by Fern from our API Definition.
33
*/
44

5-
export class FileForgeTimeoutError extends Error {
5+
export class FileforgeTimeoutError extends Error {
66
constructor() {
77
super("Timeout");
8-
Object.setPrototypeOf(this, FileForgeTimeoutError.prototype);
8+
Object.setPrototypeOf(this, FileforgeTimeoutError.prototype);
99
}
1010
}

src/errors/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export { FileForgeError } from "./FileForgeError";
2-
export { FileForgeTimeoutError } from "./FileForgeTimeoutError";
1+
export { FileforgeError } from "./FileforgeError";
2+
export { FileforgeTimeoutError } from "./FileforgeTimeoutError";

src/index.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export * as FileForge from "./api";
2-
export { FileForgeClient } from "./Client";
3-
export { FileForgeEnvironment } from "./environments";
4-
export { FileForgeError, FileForgeTimeoutError } from "./errors";
1+
export * as Fileforge from "./api";
2+
export { FileforgeClient } from "./Client";
3+
export { FileforgeEnvironment } from "./environments";
4+
export { FileforgeError, FileforgeTimeoutError } from "./errors";

src/serialization/types/ConvertDocxRequestOptions.ts

-20
This file was deleted.

src/serialization/types/ErrorSchema.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
*/
44

55
import * as serializers from "../index";
6-
import * as FileForge from "../../api/index";
6+
import * as Fileforge from "../../api/index";
77
import * as core from "../../core";
88

9-
export const ErrorSchema: core.serialization.ObjectSchema<serializers.ErrorSchema.Raw, FileForge.ErrorSchema> =
9+
export const ErrorSchema: core.serialization.ObjectSchema<serializers.ErrorSchema.Raw, Fileforge.ErrorSchema> =
1010
core.serialization.object({
1111
statusCode: core.serialization.number(),
1212
code: core.serialization.string(),

0 commit comments

Comments
 (0)