Skip to content

Commit

Permalink
chore: rename to customFetch
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Dec 19, 2022
1 parent 4891d4c commit 9bc5fa4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 31 deletions.
23 changes: 5 additions & 18 deletions packages/preset-web-fonts/README.md
Expand Up @@ -73,9 +73,11 @@ Currently supported Providers:
- `bunny` - [Privacy-Friendly Google Fonts](https://fonts.bunny.net/)
- `fontshare` - [Quality Font Service by ITF](https://www.fontshare.com/)

### Custom request function
PR welcome to add more providers 🙌

### Custom fetch function

Use your own request to fetch font source
Use your own function to fetch font source.

```ts
import presetWebFonts from '@unocss/preset-web-fonts'
Expand All @@ -88,32 +90,17 @@ Unocss({
presetUno(),
presetWebFonts({
// use axios with an https proxy
customRequest: (url: string) => axios.get(url, { httpsAgent: new ProxyAgent('https://localhost:7890') }),
customFetch: (url: string) => axios.get(url, { httpsAgent: new ProxyAgent('https://localhost:7890') }),
provider: 'google',
fonts: {
sans: 'Roboto',
mono: ['Fira Code', 'Fira Mono:400,700'],
// custom ones
lobster: 'Lobster',
lato: [
{
name: 'Lato',
weights: ['400', '700'],
italic: true,
},
{
name: 'sans-serif',
provider: 'none',
},
],
},
}),
],
})
```

PR welcome to add more providers 🙌

## 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
21 changes: 10 additions & 11 deletions packages/preset-web-fonts/src/index.ts
Expand Up @@ -35,7 +35,7 @@ const preset = (options: WebFontsOptions = {}): Preset<any> => {
extendTheme = true,
inlineImports = true,
themeKey = 'fontFamily',
customRequest,
customFetch,
} = options

const fontObject = Object.fromEntries(
Expand All @@ -49,16 +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] = (customRequest
? customRequest(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
4 changes: 2 additions & 2 deletions packages/preset-web-fonts/src/types.ts
Expand Up @@ -44,11 +44,11 @@ export interface WebFontsOptions {
inlineImports?: boolean

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

export interface Provider {
Expand Down

0 comments on commit 9bc5fa4

Please sign in to comment.