Skip to content

Commit

Permalink
feat: Added fetchRequestInitGenerator option
Browse files Browse the repository at this point in the history
--

In scenarios where elements contain multiple URLs, including both internal and public links, there arises a need to supply distinct RequestInit configurations for various fetch requests.

Presently, the limitation lies in the fact that we can only provide a single fetchRequestInit, and this configuration is globally applied to all fetch calls for that element.

This proposed feature aims to enhance flexibility by enabling the provision of providing RequestInit configurations tailored to individual fetch requests based on their respective URLs. This enhancement ensures more granular control over the customization of fetch request behaviors, facilitating better handling of internal and public URLs.
  • Loading branch information
grijeshsaini committed Dec 15, 2023
1 parent ebb91a6 commit e1a1f9b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
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 @@ async function embedFonts(data: Metadata, options: Options): Promise<string> {
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)
})
})

0 comments on commit e1a1f9b

Please sign in to comment.