Skip to content

Commit

Permalink
Release 0.0.13
Browse files Browse the repository at this point in the history
  • Loading branch information
fern-api[bot] committed May 27, 2024
1 parent 29e907c commit 39f3fa5
Show file tree
Hide file tree
Showing 23 changed files with 356 additions and 521 deletions.
274 changes: 60 additions & 214 deletions src/Client.ts

Large diffs are not rendered by default.

38 changes: 0 additions & 38 deletions src/api/client/requests/ConvertDocxRequest.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/api/client/requests/GenerateRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
* This file was auto-generated by Fern from our API Definition.
*/

import * as FileForge from "../../index";
import * as Fileforge from "../../index";

/**
* @example
* {}
*/
export interface GenerateRequest {
/** Conversion options. This field is required even if empty. */
options: FileForge.GenerateRequestOptions;
options: Fileforge.GenerateRequestOptions;
}
4 changes: 2 additions & 2 deletions src/api/client/requests/MergeRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
* This file was auto-generated by Fern from our API Definition.
*/

import * as FileForge from "../../index";
import * as Fileforge from "../../index";

/**
* @example
* {}
*/
export interface MergeRequest {
options: FileForge.MergeRequestOptions;
options: Fileforge.MergeRequestOptions;
}
1 change: 0 additions & 1 deletion src/api/client/requests/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export { type ConvertDocxRequest } from "./ConvertDocxRequest";
export { type GenerateRequest } from "./GenerateRequest";
export { type MergeRequest } from "./MergeRequest";
6 changes: 3 additions & 3 deletions src/api/errors/BadGatewayError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
*/

import * as errors from "../../errors/index";
import * as FileForge from "../index";
import * as Fileforge from "../index";

export class BadGatewayError extends errors.FileForgeError {
constructor(body: FileForge.ErrorSchema) {
export class BadGatewayError extends errors.FileforgeError {
constructor(body: Fileforge.ErrorSchema) {
super({
message: "BadGatewayError",
statusCode: 502,
Expand Down
6 changes: 3 additions & 3 deletions src/api/errors/BadRequestError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
*/

import * as errors from "../../errors/index";
import * as FileForge from "../index";
import * as Fileforge from "../index";

export class BadRequestError extends errors.FileForgeError {
constructor(body: FileForge.ErrorSchema) {
export class BadRequestError extends errors.FileforgeError {
constructor(body: Fileforge.ErrorSchema) {
super({
message: "BadRequestError",
statusCode: 400,
Expand Down
2 changes: 1 addition & 1 deletion src/api/errors/InternalServerError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

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

export class InternalServerError extends errors.FileForgeError {
export class InternalServerError extends errors.FileforgeError {
constructor(body?: unknown) {
super({
message: "InternalServerError",
Expand Down
6 changes: 3 additions & 3 deletions src/api/errors/UnauthorizedError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
*/

import * as errors from "../../errors/index";
import * as FileForge from "../index";
import * as Fileforge from "../index";

export class UnauthorizedError extends errors.FileForgeError {
constructor(body: FileForge.ErrorSchema) {
export class UnauthorizedError extends errors.FileforgeError {
constructor(body: Fileforge.ErrorSchema) {
super({
message: "UnauthorizedError",
statusCode: 401,
Expand Down
33 changes: 0 additions & 33 deletions src/api/types/ConvertDocxRequestOptions.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/api/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from "./ConvertDocxRequestOptions";
export * from "./GenerateRequestOptions";
export * from "./MergeRequestOptions";
export * from "./ErrorSchema";
102 changes: 88 additions & 14 deletions src/core/fetcher/Fetcher.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { default as FormData } from "form-data";
import qs from "qs";
import { RUNTIME } from "../runtime";
import { APIResponse } from "./APIResponse";
Expand All @@ -16,6 +15,7 @@ export declare namespace Fetcher {
timeoutMs?: number;
maxRetries?: number;
withCredentials?: boolean;
abortSignal?: AbortSignal;
responseType?: "json" | "blob" | "streaming" | "text";
}

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

let body: BodyInit | undefined = undefined;
if (args.body instanceof FormData) {
// @ts-expect-error
body = args.body;
} else if (args.body instanceof Uint8Array) {
body = args.body;
const maybeStringifyBody = (body: any) => {
if (body instanceof Uint8Array) {
return body;
} else {
return JSON.stringify(body);
}
};

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

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

const makeRequest = async (): Promise<Response> => {
const controller = new AbortController();
let abortId = undefined;
const signals: AbortSignal[] = [];

// Add timeout signal
let timeoutAbortId: NodeJS.Timeout | undefined = undefined;
if (args.timeoutMs != null) {
abortId = setTimeout(() => controller.abort(), args.timeoutMs);
const { signal, abortId } = getTimeoutSignal(args.timeoutMs);
timeoutAbortId = abortId;
signals.push(signal);
}

// Add arbitrary signal
if (args.abortSignal != null) {
signals.push(args.abortSignal);
}

const response = await fetchFn(url, {
method: args.method,
headers,
body,
signal: controller.signal,
signal: anySignal(signals),
credentials: args.withCredentials ? "include" : undefined,
});
if (abortId != null) {
clearTimeout(abortId);

if (timeoutAbortId != null) {
clearTimeout(timeoutAbortId);
}

return response;
};

Expand Down Expand Up @@ -167,7 +194,15 @@ async function fetcherImpl<R = unknown>(args: Fetcher.Args): Promise<APIResponse
};
}
} catch (error) {
if (error instanceof Error && error.name === "AbortError") {
if (args.abortSignal != null && args.abortSignal.aborted) {
return {
ok: false,
error: {
reason: "unknown",
errorMessage: "The user aborted a request",
},
};
} else if (error instanceof Error && error.name === "AbortError") {
return {
ok: false,
error: {
Expand All @@ -194,4 +229,43 @@ async function fetcherImpl<R = unknown>(args: Fetcher.Args): Promise<APIResponse
}
}

const TIMEOUT = "timeout";

function getTimeoutSignal(timeoutMs: number): { signal: AbortSignal; abortId: NodeJS.Timeout } {
const controller = new AbortController();
const abortId = setTimeout(() => controller.abort(TIMEOUT), timeoutMs);
return { signal: controller.signal, abortId };
}

/**
* Returns an abort signal that is getting aborted when
* at least one of the specified abort signals is aborted.
*
* Requires at least node.js 18.
*/
function anySignal(...args: AbortSignal[] | [AbortSignal[]]): AbortSignal {
// Allowing signals to be passed either as array
// of signals or as multiple arguments.
const signals = <AbortSignal[]>(args.length === 1 && Array.isArray(args[0]) ? args[0] : args);

const controller = new AbortController();

for (const signal of signals) {
if (signal.aborted) {
// Exiting early if one of the signals
// is already aborted.
controller.abort((signal as any)?.reason);
break;
}

// Listening for signals and removing the listeners
// when at least one symbol is aborted.
signal.addEventListener("abort", () => controller.abort((signal as any)?.reason), {
signal: controller.signal,
});
}

return controller.signal;
}

export const fetcher: FetchFunction = fetcherImpl;
4 changes: 2 additions & 2 deletions src/environments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
* This file was auto-generated by Fern from our API Definition.
*/

export const FileForgeEnvironment = {
export const FileforgeEnvironment = {
Default: "https://api.fileforge.com",
} as const;

export type FileForgeEnvironment = typeof FileForgeEnvironment.Default;
export type FileforgeEnvironment = typeof FileforgeEnvironment.Default;
4 changes: 2 additions & 2 deletions src/errors/FileForgeError.ts → src/errors/FileforgeError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
* This file was auto-generated by Fern from our API Definition.
*/

export class FileForgeError extends Error {
export class FileforgeError extends Error {
readonly statusCode?: number;
readonly body?: unknown;

constructor({ message, statusCode, body }: { message?: string; statusCode?: number; body?: unknown }) {
super(buildMessage({ message, statusCode, body }));
Object.setPrototypeOf(this, FileForgeError.prototype);
Object.setPrototypeOf(this, FileforgeError.prototype);
if (statusCode != null) {
this.statusCode = statusCode;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
* This file was auto-generated by Fern from our API Definition.
*/

export class FileForgeTimeoutError extends Error {
export class FileforgeTimeoutError extends Error {
constructor() {
super("Timeout");
Object.setPrototypeOf(this, FileForgeTimeoutError.prototype);
Object.setPrototypeOf(this, FileforgeTimeoutError.prototype);
}
}
4 changes: 2 additions & 2 deletions src/errors/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { FileForgeError } from "./FileForgeError";
export { FileForgeTimeoutError } from "./FileForgeTimeoutError";
export { FileforgeError } from "./FileforgeError";
export { FileforgeTimeoutError } from "./FileforgeTimeoutError";
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * as FileForge from "./api";
export { FileForgeClient } from "./Client";
export { FileForgeEnvironment } from "./environments";
export { FileForgeError, FileForgeTimeoutError } from "./errors";
export * as Fileforge from "./api";
export { FileforgeClient } from "./Client";
export { FileforgeEnvironment } from "./environments";
export { FileforgeError, FileforgeTimeoutError } from "./errors";
20 changes: 0 additions & 20 deletions src/serialization/types/ConvertDocxRequestOptions.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/serialization/types/ErrorSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
*/

import * as serializers from "../index";
import * as FileForge from "../../api/index";
import * as Fileforge from "../../api/index";
import * as core from "../../core";

export const ErrorSchema: core.serialization.ObjectSchema<serializers.ErrorSchema.Raw, FileForge.ErrorSchema> =
export const ErrorSchema: core.serialization.ObjectSchema<serializers.ErrorSchema.Raw, Fileforge.ErrorSchema> =
core.serialization.object({
statusCode: core.serialization.number(),
code: core.serialization.string(),
Expand Down
Loading

0 comments on commit 39f3fa5

Please sign in to comment.