Skip to content

Commit

Permalink
fix!: rename public API to resolveFont
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Oct 4, 2024
1 parent 71765ec commit 5424d5e
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 42 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const unifont = await createUnifont([
providers.google(),
])

const fonts = await unifont.resolveFontFace('Poppins')
const fonts = await unifont.resolveFont('Poppins')

console.log(fonts)
```
Expand All @@ -44,7 +44,7 @@ const storage = createStorage({

const cachedUnifont = await createUnifont([providers.google()], { storage })

console.log(await cachedUnifont.resolveFontFace('Poppins'))
console.log(await cachedUnifont.resolveFont('Poppins'))

// cached data is stored in `node_modules/.cache/unifont`
```
Expand Down
4 changes: 2 additions & 2 deletions playground/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ const storage = createStorage({
driver: fsDriver({ base: 'node_modules/.cache/unifont' }),
})

const cachedUnifont = await createUnifont([providers.google()], { storage })
const unifont = await createUnifont([providers.google()], { storage })

console.log(await cachedUnifont.resolveFontFace('Poppins'))
console.log(await unifont.resolveFont('Poppins'))
2 changes: 1 addition & 1 deletion playground/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ const unifont = await createUnifont([
providers.google(),
])

const fonts = await unifont.resolveFontFace('Poppins')
const fonts = await unifont.resolveFont('Poppins')

console.log(fonts)
38 changes: 21 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { InitializedProvider, Provider, ResolveFontFacesOptions } from './types'
import type { FontFaceData, InitializedProvider, Provider, ResolveFontOptions } from './types'
import { createAsyncStorage, memoryStorage, type Storage } from './cache'

export * as providers from './providers'
Expand All @@ -8,7 +8,12 @@ export interface UnifontOptions {
storage?: Storage
}

export const defaultResolveOptions: ResolveFontFacesOptions = {
export type { ResolveFontOptions } from './types'
export interface Unifont {
resolveFont: (fontFamily: string, options?: ResolveFontOptions, providers?: string[]) => Promise<{ fonts: FontFaceData[] }>
}

export const defaultResolveOptions: ResolveFontOptions = {
weights: ['400'],
styles: ['normal', 'italic'] as const,
subsets: [
Expand All @@ -22,7 +27,7 @@ export const defaultResolveOptions: ResolveFontFacesOptions = {
],
}

export async function createUnifont(providers: Provider[], options?: UnifontOptions) {
export async function createUnifont(providers: Provider[], options?: UnifontOptions): Promise<Unifont> {
const stack: Record<string, InitializedProvider> = {}
const unifontContext = {
storage: createAsyncStorage(options?.storage ?? memoryStorage()),
Expand All @@ -31,9 +36,8 @@ export async function createUnifont(providers: Provider[], options?: UnifontOpti
for (const provider of providers) {
try {
const initializedProvider = await provider(unifontContext)
if (initializedProvider) {
if (initializedProvider)
stack[provider._name] = initializedProvider
}
}
catch (err) {
console.error(`Could not initialize provider \`${provider._name}\`. \`unifont\` will not be able to process fonts provided by this provider.`, err)
Expand All @@ -42,25 +46,25 @@ export async function createUnifont(providers: Provider[], options?: UnifontOpti

const allProviders = Object.keys(stack)

async function resolveFontFace(fontFamily: string, options = defaultResolveOptions, providers = allProviders) {
async function resolveFont(fontFamily: string, options = defaultResolveOptions, providers = allProviders) {
for (const id of providers) {
const provider = stack[id]
if (provider?.resolveFontFaces) {
try {
const result = await provider.resolveFontFaces(fontFamily, options)
if (result) {
return result
}
}
catch (err) {
console.error(`Could not resolve font face for \`${fontFamily}\` from \`${id}\` provider.`, err)
}
if (!provider?.resolveFont)
continue

try {
const result = await provider.resolveFont(fontFamily, options)
if (result)
return result
}
catch (err) {
console.error(`Could not resolve font face for \`${fontFamily}\` from \`${id}\` provider.`, err)
}
}
return { fonts: [] }
}

return {
resolveFontFace,
resolveFont,
}
}
6 changes: 3 additions & 3 deletions src/providers/adobe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { hash } from 'ohash'

import { extractFontFaceData } from '../css/parse'
import { $fetch } from '../fetch'
import { defineFontProvider, type ResolveFontFacesOptions } from '../types'
import { defineFontProvider, type ResolveFontOptions } from '../types'

interface ProviderOption {
id: string[] | string
Expand Down Expand Up @@ -39,7 +39,7 @@ export default defineFontProvider<ProviderOption>('adobe', async (options, ctx)
}
}))

async function getFontDetails(family: string, options: ResolveFontFacesOptions) {
async function getFontDetails(family: string, options: ResolveFontOptions) {
options.weights = options.weights.map(String)

for (const kit of fonts.kits) {
Expand Down Expand Up @@ -73,7 +73,7 @@ export default defineFontProvider<ProviderOption>('adobe', async (options, ctx)
}

return {
async resolveFontFaces(family, options) {
async resolveFont(family, options) {
if (!familyMap.has(family)) {
return
}
Expand Down
6 changes: 3 additions & 3 deletions src/providers/bunny.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { hash } from 'ohash'

import { extractFontFaceData } from '../css/parse'
import { $fetch } from '../fetch'
import { defineFontProvider, type ResolveFontFacesOptions } from '../types'
import { defineFontProvider, type ResolveFontOptions } from '../types'

const fontAPI = $fetch.create({ baseURL: 'https://fonts.bunny.net' })

Expand All @@ -14,7 +14,7 @@ export default defineFontProvider('bunny', async (_options, ctx) => {
familyMap.set(family.familyName, id)
}

async function getFontDetails(family: string, options: ResolveFontFacesOptions) {
async function getFontDetails(family: string, options: ResolveFontOptions) {
const id = familyMap.get(family) as keyof typeof fonts
const font = fonts[id]!
const weights = options.weights.filter(weight => font.weights.includes(Number(weight)))
Expand All @@ -40,7 +40,7 @@ export default defineFontProvider('bunny', async (_options, ctx) => {
}

return {
async resolveFontFaces(fontFamily, defaults) {
async resolveFont(fontFamily, defaults) {
if (!familyMap.has(fontFamily)) {
return
}
Expand Down
6 changes: 3 additions & 3 deletions src/providers/fontshare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { hash } from 'ohash'

import { extractFontFaceData } from '../css/parse'
import { $fetch } from '../fetch'
import { defineFontProvider, type ResolveFontFacesOptions } from '../types'
import { defineFontProvider, type ResolveFontOptions } from '../types'

const fontAPI = $fetch.create({ baseURL: 'https://api.fontshare.com/v2' })
export default defineFontProvider('fontshare', async (_options, ctx) => {
Expand Down Expand Up @@ -30,7 +30,7 @@ export default defineFontProvider('fontshare', async (_options, ctx) => {
fontshareFamilies.add(font.name)
}

async function getFontDetails(family: string, options: ResolveFontFacesOptions) {
async function getFontDetails(family: string, options: ResolveFontOptions) {
// https://api.fontshare.com/v2/css?f[]=alpino@300
const font = fonts.find(f => f.name === family)!
const numbers: number[] = []
Expand All @@ -54,7 +54,7 @@ export default defineFontProvider('fontshare', async (_options, ctx) => {
}

return {
async resolveFontFaces(fontFamily, defaults) {
async resolveFont(fontFamily, defaults) {
if (!fontshareFamilies.has(fontFamily)) {
return
}
Expand Down
6 changes: 3 additions & 3 deletions src/providers/fontsource.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { hash } from 'ohash'

import { $fetch } from '../fetch'
import { defineFontProvider, type FontFaceData, type ResolveFontFacesOptions } from '../types'
import { defineFontProvider, type FontFaceData, type ResolveFontOptions } from '../types'

const fontAPI = $fetch.create({ baseURL: 'https://api.fontsource.org/v1' })

Expand All @@ -13,7 +13,7 @@ export default defineFontProvider('fontsource', async (_options, ctx) => {
familyMap.set(meta.family, meta)
}

async function getFontDetails(family: string, options: ResolveFontFacesOptions) {
async function getFontDetails(family: string, options: ResolveFontOptions) {
const font = familyMap.get(family)!
const weights = options.weights.filter(weight => font.weights.includes(Number(weight)))
const styles = options.styles.filter(style => font.styles.includes(style))
Expand Down Expand Up @@ -60,7 +60,7 @@ export default defineFontProvider('fontsource', async (_options, ctx) => {
}

return {
async resolveFontFaces(fontFamily, options) {
async resolveFont(fontFamily, options) {
if (!familyMap.has(fontFamily)) {
return
}
Expand Down
6 changes: 3 additions & 3 deletions src/providers/google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { hash } from 'ohash'

import { extractFontFaceData } from '../css/parse'
import { $fetch } from '../fetch'
import { defineFontProvider, type ResolveFontFacesOptions } from '../types'
import { defineFontProvider, type ResolveFontOptions } from '../types'

export default defineFontProvider('google', async (_options, ctx) => {
const googleFonts = await ctx.storage.getItem('google:meta.json', () => $fetch<{ familyMetadataList: FontIndexMeta[] }>('https://fonts.google.com/metadata/fonts', { responseType: 'json' }).then(r => r.familyMetadataList))
Expand All @@ -21,7 +21,7 @@ export default defineFontProvider('google', async (_options, ctx) => {
// svg: 'Mozilla/4.0 (iPad; CPU OS 4_0_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/4.1 Mobile/9A405 Safari/7534.48.3',
}

async function getFontDetails(family: string, options: ResolveFontFacesOptions) {
async function getFontDetails(family: string, options: ResolveFontOptions) {
const font = googleFonts.find(font => font.family === family)!
const styles = [...new Set(options.styles.map(i => styleMap[i]))].sort()

Expand Down Expand Up @@ -52,7 +52,7 @@ export default defineFontProvider('google', async (_options, ctx) => {
}

return {
async resolveFontFaces(fontFamily, options) {
async resolveFont(fontFamily, options) {
if (!googleFonts.some(font => font.family === fontFamily)) {
return
}
Expand Down
2 changes: 1 addition & 1 deletion src/providers/googleicons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export default defineFontProvider('googleicons', async (_options, ctx) => {
}

return {
async resolveFontFaces(fontFamily, defaults) {
async resolveFont(fontFamily, defaults) {
if (!googleIcons.includes(fontFamily)) {
return
}
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface ProviderContext {
}
}

export interface ResolveFontFacesOptions {
export interface ResolveFontOptions {
weights: string[]
styles: Array<'normal' | 'italic' | 'oblique'>
// TODO: improve support and support unicode range
Expand Down Expand Up @@ -51,7 +51,7 @@ export interface FontFaceData {
}

export interface InitializedProvider {
resolveFontFaces: (family: string, options: ResolveFontFacesOptions) => Awaitable<undefined | {
resolveFont: (family: string, options: ResolveFontOptions) => Awaitable<undefined | {
/**
* Return data used to generate @font-face declarations.
* @see https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face
Expand Down
4 changes: 2 additions & 2 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createUnifont, providers } from '../src'
describe('unifont', () => {
it('works with no providers', async () => {
const unifont = await createUnifont([])
const { fonts } = await unifont.resolveFontFace('Poppins')
const { fonts } = await unifont.resolveFont('Poppins')
expect(fonts).toMatchInlineSnapshot(`[]`)
})

Expand All @@ -21,7 +21,7 @@ describe('unifont', () => {
providers.google(),
])

const { fonts } = await unifont.resolveFontFace('Poppins')
const { fonts } = await unifont.resolveFont('Poppins')

expect(fonts).toHaveLength(6)
})
Expand Down

0 comments on commit 5424d5e

Please sign in to comment.