Skip to content

Commit

Permalink
feat(chromium): add support for custom headers function
Browse files Browse the repository at this point in the history
  • Loading branch information
ecaroth committed Dec 6, 2024
1 parent 7e1f048 commit 02166bd
Show file tree
Hide file tree
Showing 7 changed files with 849 additions and 1,135 deletions.
3 changes: 0 additions & 3 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npx --no -- commitlint --edit ${1}
3 changes: 2 additions & 1 deletion src/chromium/converters/markdown.converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ export class MarkdownConverter extends Converter {
this.endpoint,
data,
Chromiumly.getGotenbergApiBasicAuthUsername(),
Chromiumly.getGotenbergApiBasicAuthPassword()
Chromiumly.getGotenbergApiBasicAuthPassword(),
await Chromiumly.getAddCustomHeadersFn()()
);
}
}
3 changes: 2 additions & 1 deletion src/chromium/converters/url.converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ export class UrlConverter extends Converter {
this.endpoint,
data,
Chromiumly.getGotenbergApiBasicAuthUsername(),
Chromiumly.getGotenbergApiBasicAuthPassword()
Chromiumly.getGotenbergApiBasicAuthPassword(),
await Chromiumly.getAddCustomHeadersFn()()
);
}
}
9 changes: 7 additions & 2 deletions src/common/gotenberg.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,21 @@ export class GotenbergUtils {
* @param {string} username - The username for basic authentication.
* @param {string} password - The password for basic authentication.
* @param {FormData} data - The FormData object to be sent in the POST request.
* @param {Record<string, string>} customHeaders - List of custom headers to include in the fetch
* @returns {Promise<Buffer>} A Promise that resolves to the response body as a Buffer.
* @throws {Error} Throws an error if the HTTP response status is not OK.
*/
public static async fetch(
endpoint: string,
data: FormData,
username?: string,
password?: string
password?: string,
customHeaders: Record<string, string> = {}
): Promise<Buffer> {
const headers: Record<string, string> = { ...data.getHeaders() };
const headers: Record<string, string> = {
...data.getHeaders(),
...customHeaders
};

if (username && password) {
const authHeader =
Expand Down
3 changes: 2 additions & 1 deletion src/libre-office/libre-office.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ export class LibreOffice {
endpoint,
data,
Chromiumly.getGotenbergApiBasicAuthUsername(),
Chromiumly.getGotenbergApiBasicAuthPassword()
Chromiumly.getGotenbergApiBasicAuthPassword(),
await Chromiumly.getAddCustomHeadersFn()()
);
}
}
22 changes: 22 additions & 0 deletions src/main.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ export class Chromiumly {
private static gotenbergApiBasicAuthPassword: string | undefined =
Gotenberg.password;

/**
* An async function that returns a set of headers to use for the fetch call to gotenberg service
* @type {string | undefined}
*/
private static addCustomHeadersFn:
| (() => Promise<Record<string, string>>)
| undefined = undefined;

/**
* The path for Chromium-related conversions.
* @type {string}
Expand Down Expand Up @@ -118,6 +126,7 @@ export class Chromiumly {
endpoint: string;
username?: string;
password?: string;
addCustomHeadersFn?: () => Promise<Record<string, string>>;
}): void {
this.gotenbergEndpoint = config.endpoint;

Expand All @@ -127,6 +136,9 @@ export class Chromiumly {
if (config.password !== undefined) {
this.gotenbergApiBasicAuthPassword = config.password;
}
if (config.addCustomHeadersFn !== undefined) {
this.addCustomHeadersFn = config.addCustomHeadersFn;
}
}

/**
Expand Down Expand Up @@ -156,4 +168,14 @@ export class Chromiumly {
public static getGotenbergApiBasicAuthPassword(): string | undefined {
return this.gotenbergApiBasicAuthPassword;
}

/**
* Gets the function to build/send custom headers with fetch
* @returns () => Promise<Record<string, string>>
*/
public static getAddCustomHeadersFn(): () => Promise<
Record<string, string>
> {
return this.addCustomHeadersFn ?? (async () => ({}));
}
}
Loading

0 comments on commit 02166bd

Please sign in to comment.