Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat(preset-web-fonts): add customFetch option (#2000)
* feat(preset-web-fonts):add customRequest option

* chore: update

* Update packages/preset-web-fonts/README.md

Co-authored-by: 洪布斯 <q951686662@qq.com>

* chore: update

* chore: rename to `customFetch`

Co-authored-by: Frozen FIsh <76603360+sudongyuer@users.noreply.github.com>
Co-authored-by: 洪布斯 <q951686662@qq.com>
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
4 people committed Dec 19, 2022
1 parent 8b3bb65 commit dad2e0f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
26 changes: 26 additions & 0 deletions packages/preset-web-fonts/README.md
Expand Up @@ -75,6 +75,32 @@ Currently supported Providers:

PR welcome to add more providers 🙌

### Custom fetch function

Use your own function to fetch font source.

```ts
import presetWebFonts from '@unocss/preset-web-fonts'
import presetUno from '@unocss/preset-uno'
import axios from 'axios'
import ProxyAgent from 'proxy-agent'

Unocss({
presets: [
presetUno(),
presetWebFonts({
// use axios with an https proxy
customFetch: (url: string) => axios.get(url, { httpsAgent: new ProxyAgent('https://localhost:7890') }),
provider: 'google',
fonts: {
sans: 'Roboto',
mono: ['Fira Code', 'Fira Mono:400,700'],
},
}),
],
})
```

## Configuration

Refer to the [type definition](https://github.com/unocss/unocss/blob/main/packages/preset-web-fonts/src/types.ts#L4) for all configurations available.
Expand Down
18 changes: 10 additions & 8 deletions packages/preset-web-fonts/src/index.ts
Expand Up @@ -35,6 +35,7 @@ const preset = (options: WebFontsOptions = {}): Preset<any> => {
extendTheme = true,
inlineImports = true,
themeKey = 'fontFamily',
customFetch,
} = options

const fontObject = Object.fromEntries(
Expand All @@ -48,14 +49,15 @@ const preset = (options: WebFontsOptions = {}): Preset<any> => {
async function importUrl(url: string) {
if (inlineImports) {
if (!importCache[url]) {
const { $fetch } = await import('ohmyfetch')
importCache[url] = $fetch(url, { headers: {}, retry: 3 })
.catch((e) => {
console.error('Failed to fetch web fonts')
console.error(e)
if (typeof process !== 'undefined' && process.env.CI)
throw e
})
const promise = customFetch
? customFetch(url)
: (await import('ohmyfetch')).$fetch(url, { headers: {}, retry: 3 })
importCache[url] = promise.catch((e) => {
console.error('Failed to fetch web fonts')
console.error(e)
if (typeof process !== 'undefined' && process.env.CI)
throw e
})
}
return await importCache[url]
}
Expand Down
7 changes: 7 additions & 0 deletions packages/preset-web-fonts/src/types.ts
Expand Up @@ -42,6 +42,13 @@ export interface WebFontsOptions {
* @default true
*/
inlineImports?: boolean

/**
* Custom fetch function
*
* @default undefined
*/
customFetch?: (url: string) => Promise<any>
}

export interface Provider {
Expand Down

0 comments on commit dad2e0f

Please sign in to comment.