Skip to content

Commit

Permalink
feat: support system proxy when fetching fonts/metadata (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe authored Mar 20, 2024
1 parent 1283130 commit 074a627
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 16 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"jiti": "^1.21.0",
"magic-regexp": "^0.8.0",
"magic-string": "^0.30.8",
"ofetch": "^1.3.3",
"node-fetch-native": "^1.6.2",
"ohash": "^1.1.3",
"pathe": "^1.1.2",
"sirv": "^2.0.4",
Expand All @@ -74,6 +74,7 @@
"consola": "^3.2.3",
"eslint": "^8.57.0",
"execa": "^8.0.1",
"ofetch": "^1.3.3",
"nitropack": "^2.9.4",
"nuxt": "^3.11.1",
"nuxt-fonts-devtools": "latest",
Expand All @@ -89,4 +90,4 @@
"resolutions": {
"@nuxt/fonts": "workspace:*"
}
}
}
9 changes: 6 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/assets.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fsp from 'node:fs/promises'
import { addDevServerHandler, useNuxt } from '@nuxt/kit'
import { eventHandler, createError, lazyEventHandler } from 'h3'
import { fetch } from 'ofetch'
import { fetch } from 'node-fetch-native/proxy'
import chalk from 'chalk'
import { defu } from 'defu'
import type { NitroConfig } from 'nitropack'
Expand Down
26 changes: 26 additions & 0 deletions src/fetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import defu from 'defu'
import { fetch } from 'node-fetch-native/proxy'
import { joinURL, withQuery } from 'ufo'

interface Mini$FetchOptions extends RequestInit {
baseURL?: string
responseType?: 'json' | 'arrayBuffer'
query?: Record<string, any>
}

const mini$fetch = <T = unknown> (url: string, options?: Mini$FetchOptions) => {
if (options?.baseURL) {
url = joinURL(options.baseURL, url)
}
if (options?.query) {
url = withQuery(url, options.query)
}
return fetch(url, options)
.then(r => options?.responseType === 'json' ? r.json() : options?.responseType === 'arrayBuffer' ? r.arrayBuffer() : r.text()) as Promise<T>
}

export const $fetch = Object.assign(mini$fetch, {
create: (defaults?: Mini$FetchOptions) => <T = unknown> (url: string, options?: Mini$FetchOptions) => mini$fetch<T>(url, defu(options, defaults))
})

export { fetch }
4 changes: 2 additions & 2 deletions src/providers/adobe.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { $fetch } from 'ofetch'
import { hash } from 'ohash'

import type { FontProvider, ResolveFontFacesOptions } from '../types'
import { extractFontFaceData, addLocalFallbacks } from '../css/parse'
import { cachedData } from '../cache'
import { $fetch } from '../fetch'
import { logger } from '../logger'

interface ProviderOption {
Expand Down Expand Up @@ -106,7 +106,7 @@ async function getFontDetails (family: string, variants: ResolveFontFacesOptions
styles.push(style)
}
if (styles.length === 0) { continue }
const css = await fontCSSAPI(`${fonts.kits[kit]!.id}.css`)
const css = await fontCSSAPI<string>(`${fonts.kits[kit]!.id}.css`)

// Adobe uses slugs instead of names in its CSS to define its font faces,
// so we need to first transform names into slugs.
Expand Down
4 changes: 2 additions & 2 deletions src/providers/bunny.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { $fetch } from 'ofetch'
import { hash } from 'ohash'

import type { FontProvider, ResolveFontFacesOptions } from '../types'
import { extractFontFaceData, addLocalFallbacks } from '../css/parse'
import { cachedData } from '../cache'
import { $fetch } from '../fetch'
import { logger } from '../logger'

export default {
Expand Down Expand Up @@ -75,7 +75,7 @@ async function getFontDetails (family: string, variants: ResolveFontFacesOptions

const resolvedVariants = weights.flatMap(w => [...styles].map(s => `${w}${s}`))

const css = await fontAPI('/css', {
const css = await fontAPI<string>('/css', {
query: {
family: id + ':' + resolvedVariants.join(',')
}
Expand Down
5 changes: 3 additions & 2 deletions src/providers/fontshare.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { $fetch } from 'ofetch'
import { hash } from 'ohash'

import type { FontProvider, ResolveFontFacesOptions } from '../types'
import { extractFontFaceData, addLocalFallbacks } from '../css/parse'
import { cachedData } from '../cache'
import { $fetch } from '../fetch'
import { logger } from '../logger'

export default {
Expand Down Expand Up @@ -69,6 +69,7 @@ async function initialiseFontMeta () {
let chunk
do {
chunk = await fontAPI<{ fonts: FontshareFontMeta[], has_more: boolean }>('/fonts', {
responseType: 'json',
query: {
offset,
limit: 100
Expand Down Expand Up @@ -105,7 +106,7 @@ async function getFontDetails (family: string, variants: ResolveFontFacesOptions

if (numbers.length === 0) return []

const css = await fontAPI(`/css?f[]=${font.slug + '@' + numbers.join(',')}`)
const css = await fontAPI<string>(`/css?f[]=${font.slug + '@' + numbers.join(',')}`)

// TODO: support subsets and axes
return addLocalFallbacks(family, extractFontFaceData(css))
Expand Down
2 changes: 1 addition & 1 deletion src/providers/fontsource.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { $fetch } from 'ofetch'
import { hash } from 'ohash'

import type { FontProvider, NormalizedFontFaceData, ResolveFontFacesOptions } from '../types'
import { addLocalFallbacks } from '../css/parse'
import { cachedData } from '../cache'
import { $fetch } from '../fetch'
import { logger } from '../logger'

export default {
Expand Down
6 changes: 3 additions & 3 deletions src/providers/google.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { $fetch } from 'ofetch'
import { hash } from 'ohash'

import type { FontProvider, ResolveFontFacesOptions } from '../types'
import { extractFontFaceData, addLocalFallbacks } from '../css/parse'
import { cachedData } from '../cache'
import { $fetch } from '../fetch'
import { logger } from '../logger'

export default {
Expand Down Expand Up @@ -46,7 +46,7 @@ interface FontIndexMeta {
let fonts: FontIndexMeta[]

async function fetchFontMetadata () {
return await $fetch<{ familyMetadataList: FontIndexMeta[] }>('https://fonts.google.com/metadata/fonts')
return await $fetch<{ familyMetadataList: FontIndexMeta[] }>('https://fonts.google.com/metadata/fonts', { responseType: 'json' })
.then(r => r.familyMetadataList)
}

Expand Down Expand Up @@ -84,7 +84,7 @@ async function getFontDetails (family: string, variants: ResolveFontFacesOptions
let css = ''

for (const extension in userAgents) {
css += await $fetch('/css2', {
css += await $fetch<string>('/css2', {
baseURL: 'https://fonts.googleapis.com',
headers: { 'user-agent': userAgents[extension as keyof typeof userAgents] },
query: {
Expand Down

0 comments on commit 074a627

Please sign in to comment.