From 0ba9d662ef7a76186425c78b7699d7f92fa6c7ad Mon Sep 17 00:00:00 2001 From: Evan Carothers Date: Fri, 6 Dec 2024 02:36:11 -0600 Subject: [PATCH] feat(chromium): extra documentation and inclusion of header function in all fetch calls --- README.md | 19 +++++++++++++++++++ src/chromium/converters/html.converter.ts | 3 ++- src/chromium/screenshots/html.screenshot.ts | 3 ++- .../screenshots/markdown.screenshot.ts | 3 ++- src/chromium/screenshots/url.screenshot.ts | 3 ++- src/main.config.ts | 1 + 6 files changed, 28 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 3f95d781..286ff5ab 100644 --- a/README.md +++ b/README.md @@ -143,6 +143,25 @@ Chromiumly.configure({ }); ``` +### Advanced Authentication / Headers + +You can optionally pass in an async function to the client config to pass custom headers into the underlying `fetch` call to your Gotenberg instance. + +This is useful for cases where you might need to do things like custom service account auth (if hosting on Google cloud run, for example), include custom proxy headers, ec. + +Ex: + +```typescript +Chromiumly.configure({ + endpoint: "https://my-gotenberg-service-1234.us-central1.run.app", + addCustomHeadersFn: async () => ({ + Authorization: `Bearer ${await generateIdToken()}`, + }), +}); +``` + +_Note: if you include username/password the created `Authorization` heaer will overwrite any you supply from the custom headers function._ + ## Modules Chromiumly introduces different classes that serve as wrappers to diff --git a/src/chromium/converters/html.converter.ts b/src/chromium/converters/html.converter.ts index c6d7f01c..b951a010 100644 --- a/src/chromium/converters/html.converter.ts +++ b/src/chromium/converters/html.converter.ts @@ -108,7 +108,8 @@ export class HtmlConverter extends Converter { this.endpoint, data, Chromiumly.getGotenbergApiBasicAuthUsername(), - Chromiumly.getGotenbergApiBasicAuthPassword() + Chromiumly.getGotenbergApiBasicAuthPassword(), + await Chromiumly.getAddCustomHeadersFn()() ); } } diff --git a/src/chromium/screenshots/html.screenshot.ts b/src/chromium/screenshots/html.screenshot.ts index 3fd8a368..408e5501 100644 --- a/src/chromium/screenshots/html.screenshot.ts +++ b/src/chromium/screenshots/html.screenshot.ts @@ -81,7 +81,8 @@ export class HtmlScreenshot extends Screenshot { this.endpoint, data, Chromiumly.getGotenbergApiBasicAuthUsername(), - Chromiumly.getGotenbergApiBasicAuthPassword() + Chromiumly.getGotenbergApiBasicAuthPassword(), + await Chromiumly.getAddCustomHeadersFn()() ); } } diff --git a/src/chromium/screenshots/markdown.screenshot.ts b/src/chromium/screenshots/markdown.screenshot.ts index 9ada1513..fb641a9d 100644 --- a/src/chromium/screenshots/markdown.screenshot.ts +++ b/src/chromium/screenshots/markdown.screenshot.ts @@ -85,7 +85,8 @@ export class MarkdownScreenshot extends Screenshot { this.endpoint, data, Chromiumly.getGotenbergApiBasicAuthUsername(), - Chromiumly.getGotenbergApiBasicAuthPassword() + Chromiumly.getGotenbergApiBasicAuthPassword(), + await Chromiumly.getAddCustomHeadersFn()() ); } } diff --git a/src/chromium/screenshots/url.screenshot.ts b/src/chromium/screenshots/url.screenshot.ts index a1be61f9..4b9811f1 100644 --- a/src/chromium/screenshots/url.screenshot.ts +++ b/src/chromium/screenshots/url.screenshot.ts @@ -82,7 +82,8 @@ export class UrlScreenshot extends Screenshot { this.endpoint, data, Chromiumly.getGotenbergApiBasicAuthUsername(), - Chromiumly.getGotenbergApiBasicAuthPassword() + Chromiumly.getGotenbergApiBasicAuthPassword(), + await Chromiumly.getAddCustomHeadersFn()() ); } } diff --git a/src/main.config.ts b/src/main.config.ts index 608611f7..e3f61b91 100644 --- a/src/main.config.ts +++ b/src/main.config.ts @@ -121,6 +121,7 @@ export class Chromiumly { * @param {string} config.endpoint - The Gotenberg service endpoint. * @param {string} [config.username] - The username for basic authentication. * @param {string} [config.password] - The password for basic authentication. + * @param {() => Promise>} [config.addCustomHeadersFn] - Optional async function to add custom headers to fetch */ public static configure(config: { endpoint: string;