-
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.
Modifies Fetcher to return something else than a stream, writes helpe…
…r for generate function
- Loading branch information
Showing
10 changed files
with
606 additions
and
419 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
Binary file not shown.
Binary file not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,86 @@ | ||
import * as environments from "./environments"; | ||
import * as core from "./core"; | ||
import urlJoin from "url-join"; | ||
import * as errors from "./errors/index"; | ||
import * as fs from "fs"; | ||
import * as FileForge from "./api/index"; | ||
import * as stream from "stream"; | ||
import { default as FormData } from "form-data"; | ||
import * as serializers from "./serialization/index"; | ||
import mime from "mime-types"; | ||
import { FileForgeClient } from "Client"; | ||
|
||
export interface Asset { | ||
path: string; | ||
content: string; | ||
} | ||
export interface PathBuffer { | ||
path: string; | ||
content: Buffer; | ||
} | ||
|
||
type AssetOrPathBuffer = Asset | PathBuffer; | ||
|
||
|
||
export interface DocumentInput { | ||
html: string; | ||
fileName?: string; | ||
test?: boolean; | ||
host?: boolean; | ||
expiresAt?: Date; | ||
files?: AssetOrPathBuffer[]; | ||
} | ||
|
||
|
||
/** | ||
* Generates a PDF document from web assets. | ||
* @throws {@link FileForge.BadRequestError} | ||
* @throws {@link FileForge.UnauthorizedError} | ||
* @throws {@link FileForge.InternalServerError} | ||
* @throws {@link FileForge.BadGatewayError} | ||
*/ | ||
export async function generate_from_html( | ||
client: FileForgeClient, | ||
document: DocumentInput | ||
):Promise<any>{ | ||
|
||
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
Oops, something went wrong.