Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added fetchRequestInitGenerator option #443

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,25 @@ When supplied, the toCanvas function will return a blob matching the given image

Defaults to `image/png`

### fetchRequestInitGenerator

```ts
(url: string) => RequestInit | undefined
```

This function is intended to provide a convenient way to customize the RequestInit configuration for fetching data from a specific URL.
For example,

```ts
htmlToImage.toCanvas(node, {
fetchRequestInitGenerator: (url: string) => {
url.includes('/internal')
? { credentials: 'include' }
: undefined
}
});
```

## Browsers

Only standard lib is currently used, but make sure your browser supports:
Expand Down
5 changes: 4 additions & 1 deletion src/dataurl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,12 @@ export async function resourceToDataURL(

let dataURL: string
try {
const requestInit = options.fetchRequestInit
? options.fetchRequestInit
: options.fetchRequestInitGenerator?.(resourceUrl)
const content = await fetchAsDataURL(
resourceUrl,
options.fetchRequestInit,
requestInit,
({ res, result }) => {
if (!contentType) {
// eslint-disable-next-line no-param-reassign
Expand Down
15 changes: 7 additions & 8 deletions src/embed-webfonts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,13 @@
url = new URL(url, data.url).href
}

return fetchAsDataURL<[string, string]>(
url,
options.fetchRequestInit,
({ result }) => {
cssText = cssText.replace(loc, `url(${result})`)
return [loc, result]
},
)
const requestInit = options.fetchRequestInit

Check warning on line 38 in src/embed-webfonts.ts

View check run for this annotation

Codecov / codecov/patch

src/embed-webfonts.ts#L38

Added line #L38 was not covered by tests
? options.fetchRequestInit
: options.fetchRequestInitGenerator?.(url)
return fetchAsDataURL<[string, string]>(url, requestInit, ({ result }) => {
cssText = cssText.replace(loc, `url(${result})`)
return [loc, result]

Check warning on line 43 in src/embed-webfonts.ts

View check run for this annotation

Codecov / codecov/patch

src/embed-webfonts.ts#L41-L43

Added lines #L41 - L43 were not covered by tests
})
})

return Promise.all(loadFonts).then(() => cssText)
Expand Down
8 changes: 8 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,12 @@ export interface Options {
*
*/
fetchRequestInit?: RequestInit

/**
* Function that generates a RequestInit based on a given URL.
* This function is intended to provide a convenient way to customize the RequestInit configuration for
* fetching the data from a specific URL.
*
*/
fetchRequestInitGenerator?: (url: string) => RequestInit | undefined
}
15 changes: 15 additions & 0 deletions test/spec/options.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,19 @@ describe('work with options', () => {
.then(done)
.catch(done)
})

it('should support fetchRequestInitGenerator', (done) => {
bootstrap('images/node.html', 'images/style.css')
.then(
assertTextRendered(['PNG', 'JPG'], {
fetchRequestInitGenerator: (url: string) => {
return url.includes("/test")
? { credentials: 'include' } as RequestInit
: undefined
},
}),
)
.then(done)
.catch(done)
})
})
Loading