-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
400 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,18 @@ | ||
# Specify files that shouldn't be modified by Fern | ||
|
||
README.md | ||
|
||
## Support content type on form data | ||
src/core/form-data-utils | ||
src/Client.ts | ||
src/core/fetcher | ||
|
||
## Change response signature | ||
src/wrapper | ||
src/index.ts | ||
|
||
# Helper | ||
src/generateFromHtml.ts | ||
|
||
## Tests | ||
tests/custom.test.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { Fileforge } from "index"; | ||
import { FileforgeClient, ResponseObject } from "./wrapper/FileforgeClient"; | ||
import mime from "mime-types"; | ||
|
||
export interface Asset { | ||
path: string; | ||
content: string; | ||
} | ||
export interface PathBuffer { | ||
path: string; | ||
content: Buffer; | ||
} | ||
|
||
export type AssetOrPathBuffer = Asset | PathBuffer; | ||
|
||
export interface DocumentInput { | ||
html: string; | ||
fileName?: string; | ||
test?: boolean; | ||
host?: boolean; | ||
expiresAt?: Date; | ||
files?: AssetOrPathBuffer[]; | ||
} | ||
|
||
export async function generateFromHtml(client: FileforgeClient, document: DocumentInput): Promise<ResponseObject> { | ||
const files: AssetOrPathBuffer[] = document.files ?? []; | ||
files.push({ path: "/index.html", content: document.html }); | ||
|
||
const test: boolean = document.test ?? true; | ||
const save: boolean = document.host ?? false; | ||
|
||
const optionsToUpload: Fileforge.GenerateRequestOptions = { | ||
test: test, | ||
host: save, | ||
expiresAt: document.expiresAt ?? new Date(Date.now() + 24 * 60 * 60 * 1000), | ||
fileName: document.fileName ?? "document", | ||
}; | ||
|
||
const htmlBlob = new Blob([document.html], { type: "text/html" }); | ||
const htmlFile = new File([htmlBlob], "index.html", { type: "text/html" }); | ||
|
||
let filesToUpload = [htmlFile]; | ||
|
||
files.forEach((asset) => { | ||
if (asset.content) { | ||
const assetType = mime.lookup(asset.path) || "application/octet-stream"; | ||
|
||
const fileBlob = new Blob([asset.content], { type: assetType }); | ||
const file = new File([fileBlob], asset.path, { type: assetType }); | ||
filesToUpload.push(file); | ||
} | ||
}); | ||
|
||
return await client.generate(filesToUpload, { | ||
options: optionsToUpload, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * as Fileforge from "./api"; | ||
export { FileforgeClient } from "./Client"; | ||
export { FileforgeClient, ResponseStream, ResponseURL } from "./wrapper/FileforgeClient"; | ||
export { FileforgeEnvironment } from "./environments"; | ||
export { FileforgeError, FileforgeTimeoutError } from "./errors"; | ||
export { generateFromHtml } from "./generateFromHtml"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { FileforgeClient as FernClient } from "../Client"; | ||
import * as fs from "fs"; | ||
import * as Fileforge from "../api/index"; | ||
import * as core from "../core"; | ||
import stream, { Stream } from "stream"; | ||
|
||
export interface ResponseURL { | ||
url: string; | ||
} | ||
|
||
export interface ResponseStream { | ||
file: stream.Readable; | ||
} | ||
|
||
export type ResponseObject = ResponseStream | ResponseURL; | ||
|
||
export class FileforgeClient { | ||
private client: FernClient; | ||
|
||
constructor(options: FernClient.Options) { | ||
this.client = new FernClient(options); | ||
} | ||
|
||
/** | ||
* Generates a PDF document from web assets. | ||
* @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?: FernClient.RequestOptions | ||
): Promise<ResponseObject> { | ||
const generated = await this.client.generate(files, request, requestOptions); | ||
// read all contents | ||
const chunks: any[] = []; | ||
for await (let chunk of generated) { | ||
chunks.push(chunk); | ||
} | ||
const value = Buffer.concat(chunks); | ||
// try json parse | ||
try { | ||
return JSON.parse(value.toString()) as ResponseObject; | ||
} catch {} | ||
// return file | ||
const { Readable } = await import("node:stream"); | ||
return { file: Readable.from(chunks) }; | ||
} | ||
|
||
/** | ||
* @throws {@link Fileforge.BadRequestError} | ||
* @throws {@link Fileforge.UnauthorizedError} | ||
* @throws {@link Fileforge.InternalServerError} | ||
*/ | ||
public async merge( | ||
files: File[] | fs.ReadStream[], | ||
request: Fileforge.MergeRequest, | ||
requestOptions?: FernClient.RequestOptions | ||
): Promise<ResponseObject> { | ||
const merged = await this.client.merge(files, request, requestOptions); | ||
// read all contents | ||
const chunks: any[] = []; | ||
for await (let chunk of merged) { | ||
chunks.push(chunk); | ||
} | ||
const value = Buffer.concat(chunks); | ||
// try json parse | ||
try { | ||
return JSON.parse(value.toString()) as ResponseObject; | ||
} catch {} | ||
// return file | ||
const { Readable } = await import("node:stream"); | ||
return { file: Readable.from(chunks) }; | ||
} | ||
} |
Oops, something went wrong.