Skip to content

Commit

Permalink
Release 0.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
fern-api[bot] committed May 7, 2024
1 parent 3f7cb42 commit 289acca
Show file tree
Hide file tree
Showing 28 changed files with 354 additions and 22 deletions.
68 changes: 53 additions & 15 deletions src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ import * as environments from "./environments";
import * as core from "./core";
import * as fs from "fs";
import * as FileForge from "./api/index";
import * as stream from "stream";
import { default as FormData } from "form-data";
import urlJoin from "url-join";
import * as errors from "./errors/index";
import * as serializers from "./serialization/index";
import { Pdf } from "./api/resources/pdf/client/Client";

export declare namespace FileForgeClient {
interface Options {
Expand All @@ -28,27 +31,23 @@ export class FileForgeClient {
constructor(protected readonly _options: FileForgeClient.Options) {}

/**
* @param {File[] | fs.ReadStream[]} files
* @param {FileForge.GenerateRequest} request
* @param {FileForgeClient.RequestOptions} requestOptions - Request-specific configuration.
*
* @example
* await fileForge.generate([fs.createReadStream("/path/to/your/file")], {
* options: {}
* })
* @throws {@link FileForge.BadRequestError}
* @throws {@link FileForge.UnauthorizedError}
* @throws {@link FileForge.InternalServerError}
* @throws {@link FileForge.BadGatewayError}
*/
public async generate(
files: File[] | fs.ReadStream[],
request: FileForge.GenerateRequest,
requestOptions?: FileForgeClient.RequestOptions
): Promise<void> {
): Promise<stream.Readable> {
const _request = new FormData();
_request.append("options", JSON.stringify(request.options));
for (const _file of files) {
_request.append("files", _file);
}

const _response = await core.fetcher({
const _response = await core.fetcher<stream.Readable>({
url: urlJoin(
(await core.Supplier.get(this._options.environment)) ?? environments.FileForgeEnvironment.Default,
"pdf/generate/"
Expand All @@ -65,18 +64,51 @@ export class FileForgeClient {
},
contentType: "multipart/form-data; boundary=" + _request.getBoundary(),
body: _request,
responseType: "streaming",
timeoutMs: requestOptions?.timeoutInSeconds != null ? requestOptions.timeoutInSeconds * 1000 : 60000,
maxRetries: requestOptions?.maxRetries,
});
if (_response.ok) {
return;
return _response.body;
}

if (_response.error.reason === "status-code") {
throw new errors.FileForgeError({
statusCode: _response.error.statusCode,
body: _response.error.body,
});
switch (_response.error.statusCode) {
case 400:
throw new FileForge.BadRequestError(
await serializers.ErrorSchema.parseOrThrow(_response.error.body, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
breadcrumbsPrefix: ["response"],
})
);
case 401:
throw new FileForge.UnauthorizedError(
await serializers.ErrorSchema.parseOrThrow(_response.error.body, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
breadcrumbsPrefix: ["response"],
})
);
case 500:
throw new FileForge.InternalServerError(_response.error.body);
case 502:
throw new FileForge.BadGatewayError(
await serializers.ErrorSchema.parseOrThrow(_response.error.body, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
breadcrumbsPrefix: ["response"],
})
);
default:
throw new errors.FileForgeError({
statusCode: _response.error.statusCode,
body: _response.error.body,
});
}
}

switch (_response.error.reason) {
Expand All @@ -94,6 +126,12 @@ export class FileForgeClient {
}
}

protected _pdf: Pdf | undefined;

public get pdf(): Pdf {
return (this._pdf ??= new Pdf(this._options));
}

protected async _getAuthorizationHeader(): Promise<string | undefined> {
return core.BasicAuth.toAuthorizationHeader({
username: await core.Supplier.get(this._options.username),
Expand Down
4 changes: 1 addition & 3 deletions src/api/client/requests/GenerateRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import * as FileForge from "../../index";

/**
* @example
* {
* options: {}
* }
* {}
*/
export interface GenerateRequest {
options: FileForge.GenerateRequestOptions;
Expand Down
17 changes: 17 additions & 0 deletions src/api/errors/BadGatewayError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* This file was auto-generated by Fern from our API Definition.
*/

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

export class BadGatewayError extends errors.FileForgeError {
constructor(body: FileForge.ErrorSchema) {
super({
message: "BadGatewayError",
statusCode: 502,
body: body,
});
Object.setPrototypeOf(this, BadGatewayError.prototype);
}
}
17 changes: 17 additions & 0 deletions src/api/errors/BadRequestError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* This file was auto-generated by Fern from our API Definition.
*/

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

export class BadRequestError extends errors.FileForgeError {
constructor(body: FileForge.ErrorSchema) {
super({
message: "BadRequestError",
statusCode: 400,
body: body,
});
Object.setPrototypeOf(this, BadRequestError.prototype);
}
}
16 changes: 16 additions & 0 deletions src/api/errors/InternalServerError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* This file was auto-generated by Fern from our API Definition.
*/

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

export class InternalServerError extends errors.FileForgeError {
constructor(body?: unknown) {
super({
message: "InternalServerError",
statusCode: 500,
body: body,
});
Object.setPrototypeOf(this, InternalServerError.prototype);
}
}
17 changes: 17 additions & 0 deletions src/api/errors/UnauthorizedError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* This file was auto-generated by Fern from our API Definition.
*/

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

export class UnauthorizedError extends errors.FileForgeError {
constructor(body: FileForge.ErrorSchema) {
super({
message: "UnauthorizedError",
statusCode: 401,
body: body,
});
Object.setPrototypeOf(this, UnauthorizedError.prototype);
}
}
4 changes: 4 additions & 0 deletions src/api/errors/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from "./BadRequestError";
export * from "./UnauthorizedError";
export * from "./InternalServerError";
export * from "./BadGatewayError";
2 changes: 2 additions & 0 deletions src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export * from "./resources";
export * from "./types";
export * from "./errors";
export * from "./client";
3 changes: 3 additions & 0 deletions src/api/resources/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * as pdf from "./pdf";
export * from "./pdf/types";
export * from "./pdf/client/requests";
124 changes: 124 additions & 0 deletions src/api/resources/pdf/client/Client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/**
* This file was auto-generated by Fern from our API Definition.
*/

import * as environments from "../../../../environments";
import * as core from "../../../../core";
import * as fs from "fs";
import * as FileForge from "../../../index";
import * as stream from "stream";
import { default as FormData } from "form-data";
import urlJoin from "url-join";
import * as errors from "../../../../errors/index";
import * as serializers from "../../../../serialization/index";

export declare namespace Pdf {
interface Options {
environment?: core.Supplier<environments.FileForgeEnvironment | string>;
username: core.Supplier<string>;
password: core.Supplier<string>;
apiKey: core.Supplier<string>;
}

interface RequestOptions {
timeoutInSeconds?: number;
maxRetries?: number;
}
}

export class Pdf {
constructor(protected readonly _options: Pdf.Options) {}

/**
* @throws {@link FileForge.BadRequestError}
* @throws {@link FileForge.UnauthorizedError}
* @throws {@link FileForge.InternalServerError}
*/
public async postPdfMerge(
files: File[] | fs.ReadStream[],
request: FileForge.PostPdfMergeRequest,
requestOptions?: Pdf.RequestOptions
): Promise<stream.Readable> {
const _request = new FormData();
_request.append("options", JSON.stringify(request.options));
for (const _file of files) {
_request.append("files", _file);
}

const _response = await core.fetcher<stream.Readable>({
url: urlJoin(
(await core.Supplier.get(this._options.environment)) ?? environments.FileForgeEnvironment.Default,
"pdf/merge/"
),
method: "POST",
headers: {
Authorization: await this._getAuthorizationHeader(),
"X-API-Key": await core.Supplier.get(this._options.apiKey),
"X-Fern-Language": "JavaScript",
"X-Fern-SDK-Name": "fileforge",
"X-Fern-SDK-Version": "0.0.0",
"X-Fern-Runtime": core.RUNTIME.type,
"X-Fern-Runtime-Version": core.RUNTIME.version,
},
contentType: "multipart/form-data; boundary=" + _request.getBoundary(),
body: _request,
responseType: "streaming",
timeoutMs: requestOptions?.timeoutInSeconds != null ? requestOptions.timeoutInSeconds * 1000 : 60000,
maxRetries: requestOptions?.maxRetries,
});
if (_response.ok) {
return _response.body;
}

if (_response.error.reason === "status-code") {
switch (_response.error.statusCode) {
case 400:
throw new FileForge.BadRequestError(
await serializers.ErrorSchema.parseOrThrow(_response.error.body, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
breadcrumbsPrefix: ["response"],
})
);
case 401:
throw new FileForge.UnauthorizedError(
await serializers.ErrorSchema.parseOrThrow(_response.error.body, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
breadcrumbsPrefix: ["response"],
})
);
case 500:
throw new FileForge.InternalServerError(_response.error.body);
default:
throw new errors.FileForgeError({
statusCode: _response.error.statusCode,
body: _response.error.body,
});
}
}

switch (_response.error.reason) {
case "non-json":
throw new errors.FileForgeError({
statusCode: _response.error.statusCode,
body: _response.error.rawBody,
});
case "timeout":
throw new errors.FileForgeTimeoutError();
case "unknown":
throw new errors.FileForgeError({
message: _response.error.errorMessage,
});
}
}

protected async _getAuthorizationHeader(): Promise<string | undefined> {
return core.BasicAuth.toAuthorizationHeader({
username: await core.Supplier.get(this._options.username),
password: await core.Supplier.get(this._options.password),
});
}
}
1 change: 1 addition & 0 deletions src/api/resources/pdf/client/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./requests";
13 changes: 13 additions & 0 deletions src/api/resources/pdf/client/requests/PostPdfMergeRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* This file was auto-generated by Fern from our API Definition.
*/

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

/**
* @example
* {}
*/
export interface PostPdfMergeRequest {
options: FileForge.PostPdfMergeRequestOptions;
}
1 change: 1 addition & 0 deletions src/api/resources/pdf/client/requests/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { type PostPdfMergeRequest } from "./PostPdfMergeRequest";
2 changes: 2 additions & 0 deletions src/api/resources/pdf/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./types";
export * from "./client";
5 changes: 5 additions & 0 deletions src/api/resources/pdf/types/PostPdfMergeRequestOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* This file was auto-generated by Fern from our API Definition.
*/

export interface PostPdfMergeRequestOptions {}
1 change: 1 addition & 0 deletions src/api/resources/pdf/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./PostPdfMergeRequestOptions";
15 changes: 15 additions & 0 deletions src/api/types/ErrorSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* This file was auto-generated by Fern from our API Definition.
*/

/**
* Generic error response schema
*/
export interface ErrorSchema {
/** The HTTP status code */
statusCode: number;
/** A machine-readable error code */
code: string;
/** A human-readable message */
message: string;
}
9 changes: 8 additions & 1 deletion src/api/types/GenerateRequestOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,12 @@
*/

export interface GenerateRequestOptions {
world?: string;
/** Generate a test document instead of a production document. The generated document will contain a watermark. Defaults to true. */
test?: boolean;
/** If enabled, the document will be hosted by FileForge and a presigned URL will be returned. */
host?: boolean;
/** If host is enabled, the expiration date of the presigned URL. Defaults to 7 days from now. Cannot exceed 7 days from now. */
expiresAt?: Date;
/** The name of the generated PDF file. Defaults to document. The file name should not contain extensions nor path traversals. */
fileName?: string;
}
1 change: 1 addition & 0 deletions src/api/types/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./GenerateRequestOptions";
export * from "./ErrorSchema";
Loading

0 comments on commit 289acca

Please sign in to comment.