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: add 'fetchRequestInit' option #210

Merged
merged 1 commit into from
Nov 11, 2021
Merged
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
18 changes: 12 additions & 6 deletions src/embedWebFonts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function fetchCSS(url: string): Promise<void | Metadata> {
return deferred
}

async function embedFonts(meta: Metadata): Promise<string> {
async function embedFonts(meta: Metadata, options: Options): Promise<string> {
return meta.cssText.then((raw: string) => {
let cssText = raw
const regexUrl = /url\(["']?([^"')]+)["']?\)/g
Expand All @@ -40,7 +40,7 @@ async function embedFonts(meta: Metadata): Promise<string> {

// eslint-disable-next-line promise/no-nesting
return window
.fetch(url)
.fetch(url, options.fetchRequestInit)
.then((res) => res.blob())
.then(
(blob) =>
Expand Down Expand Up @@ -116,6 +116,7 @@ function parseCSS(source: string) {

async function getCSSRules(
styleSheets: CSSStyleSheet[],
options: Options,
): Promise<CSSStyleRule[]> {
const ret: CSSStyleRule[] = []
const deferreds: Promise<number | void>[] = []
Expand All @@ -130,7 +131,9 @@ async function getCSSRules(
let importIndex = index + 1
const url = (item as CSSImportRule).href
const deferred = fetchCSS(url)
.then((metadata) => (metadata ? embedFonts(metadata) : ''))
.then((metadata) =>
metadata ? embedFonts(metadata, options) : '',
)
.then((cssText) =>
parseCSS(cssText).forEach((rule) => {
try {
Expand Down Expand Up @@ -162,7 +165,9 @@ async function getCSSRules(
if (sheet.href != null) {
deferreds.push(
fetchCSS(sheet.href)
.then((metadata) => (metadata ? embedFonts(metadata) : ''))
.then((metadata) =>
metadata ? embedFonts(metadata, options) : '',
)
.then((cssText) =>
parseCSS(cssText).forEach((rule) => {
inline.insertRule(rule, sheet.cssRules.length)
Expand Down Expand Up @@ -209,22 +214,23 @@ function getWebFontRules(cssRules: CSSStyleRule[]): CSSStyleRule[] {

async function parseWebFontRules<T extends HTMLElement>(
node: T,
options: Options,
): Promise<CSSRule[]> {
return new Promise((resolve, reject) => {
if (node.ownerDocument == null) {
reject(new Error('Provided element is not within a Document'))
}
resolve(toArray(node.ownerDocument.styleSheets))
})
.then((styleSheets: CSSStyleSheet[]) => getCSSRules(styleSheets))
.then((styleSheets: CSSStyleSheet[]) => getCSSRules(styleSheets, options))
.then(getWebFontRules)
}

export async function getWebFontCSS<T extends HTMLElement>(
node: T,
options: Options,
): Promise<string> {
return parseWebFontRules(node)
return parseWebFontRules(node, options)
.then((rules) =>
Promise.all(
rules.map((rule) => {
Expand Down
2 changes: 1 addition & 1 deletion src/getBlobFromURL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export function getBlobFromURL(
}

const deferred = window
.fetch(url)
.fetch(url, options.fetchRequestInit)
.then((res) =>
// eslint-disable-next-line promise/no-nesting
res.blob().then((blob) => ({
Expand Down
7 changes: 7 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,11 @@ export interface Options {
* A string indicating the image format. The default type is image/png; that type is also used if the given type isn't supported.
*/
type?: string

/**
*
*the second parameter of window.fetch (Promise<Response> fetch(input[, init]))
*
*/
fetchRequestInit?: RequestInit
}