Skip to content

Commit

Permalink
feat: ✨ add 'fetchRequestInit' option (bubkoo#210)
Browse files Browse the repository at this point in the history
Co-authored-by: huangyilin <huangyilin.ds@bytedance.com>
  • Loading branch information
PerryHuan9 and huangyilin committed Nov 11, 2021
1 parent 30000f0 commit c51da3a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
18 changes: 12 additions & 6 deletions src/embedWebFonts.ts
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
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
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
}

0 comments on commit c51da3a

Please sign in to comment.