Skip to content

Commit c51da3a

Browse files
PerryHuan9huangyilin
and
huangyilin
authored
feat: ✨ add 'fetchRequestInit' option (#210)
Co-authored-by: huangyilin <[email protected]>
1 parent 30000f0 commit c51da3a

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

src/embedWebFonts.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function fetchCSS(url: string): Promise<void | Metadata> {
2727
return deferred
2828
}
2929

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

4141
// eslint-disable-next-line promise/no-nesting
4242
return window
43-
.fetch(url)
43+
.fetch(url, options.fetchRequestInit)
4444
.then((res) => res.blob())
4545
.then(
4646
(blob) =>
@@ -116,6 +116,7 @@ function parseCSS(source: string) {
116116

117117
async function getCSSRules(
118118
styleSheets: CSSStyleSheet[],
119+
options: Options,
119120
): Promise<CSSStyleRule[]> {
120121
const ret: CSSStyleRule[] = []
121122
const deferreds: Promise<number | void>[] = []
@@ -130,7 +131,9 @@ async function getCSSRules(
130131
let importIndex = index + 1
131132
const url = (item as CSSImportRule).href
132133
const deferred = fetchCSS(url)
133-
.then((metadata) => (metadata ? embedFonts(metadata) : ''))
134+
.then((metadata) =>
135+
metadata ? embedFonts(metadata, options) : '',
136+
)
134137
.then((cssText) =>
135138
parseCSS(cssText).forEach((rule) => {
136139
try {
@@ -162,7 +165,9 @@ async function getCSSRules(
162165
if (sheet.href != null) {
163166
deferreds.push(
164167
fetchCSS(sheet.href)
165-
.then((metadata) => (metadata ? embedFonts(metadata) : ''))
168+
.then((metadata) =>
169+
metadata ? embedFonts(metadata, options) : '',
170+
)
166171
.then((cssText) =>
167172
parseCSS(cssText).forEach((rule) => {
168173
inline.insertRule(rule, sheet.cssRules.length)
@@ -209,22 +214,23 @@ function getWebFontRules(cssRules: CSSStyleRule[]): CSSStyleRule[] {
209214

210215
async function parseWebFontRules<T extends HTMLElement>(
211216
node: T,
217+
options: Options,
212218
): Promise<CSSRule[]> {
213219
return new Promise((resolve, reject) => {
214220
if (node.ownerDocument == null) {
215221
reject(new Error('Provided element is not within a Document'))
216222
}
217223
resolve(toArray(node.ownerDocument.styleSheets))
218224
})
219-
.then((styleSheets: CSSStyleSheet[]) => getCSSRules(styleSheets))
225+
.then((styleSheets: CSSStyleSheet[]) => getCSSRules(styleSheets, options))
220226
.then(getWebFontRules)
221227
}
222228

223229
export async function getWebFontCSS<T extends HTMLElement>(
224230
node: T,
225231
options: Options,
226232
): Promise<string> {
227-
return parseWebFontRules(node)
233+
return parseWebFontRules(node, options)
228234
.then((rules) =>
229235
Promise.all(
230236
rules.map((rule) => {

src/getBlobFromURL.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export function getBlobFromURL(
6363
}
6464

6565
const deferred = window
66-
.fetch(url)
66+
.fetch(url, options.fetchRequestInit)
6767
.then((res) =>
6868
// eslint-disable-next-line promise/no-nesting
6969
res.blob().then((blob) => ({

src/options.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,11 @@ export interface Options {
7979
* A string indicating the image format. The default type is image/png; that type is also used if the given type isn't supported.
8080
*/
8181
type?: string
82+
83+
/**
84+
*
85+
*the second parameter of window.fetch (Promise<Response> fetch(input[, init]))
86+
*
87+
*/
88+
fetchRequestInit?: RequestInit
8289
}

0 commit comments

Comments
 (0)