npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

google-fonts-src-getter

v1.0.2

Published

A getter of the src attribute when querying the Google Fonts API.

Downloads

203

Readme

Google Fonts src getter

A getter of the src attribute when querying the Google Fonts API.

The problem

Nowadays, Google Fonts API provides the @font-face code needed to import fonts to any website. It is good because you don't need to download the font file and include it in your project: it's enough to add a <link> tag to your HTML and point it to the desired API URL built for your needs.

Example:

<link
  rel="stylesheet"
  href="https://fonts.googleapis.com/css?family=Tangerine"
/>

There are several libraries out there where they help to build such URLs to get the @font-face code.

With careful attention, every @font-face code has an attribute src, where this is the actual URL that has the font to be downloaded.

@font-face {
  font-family: 'Poppins';
  font-style: normal;
  font-weight: 600;
  src: url(https://fonts.gstatic.com/s/poppins/v21/pxiByp8kv8JHgFVrLEj6V1s.ttf)
    format('truetype');
}

Whenever I'd need this src attribute dynamically in my JS code, I found myself calling the Google Fonts API by hand, copying, and pasting the URL in the configurations file I've worked on. It was a pain in the ass.

In this way, it comes google-fonts-src-getter: by defining the fonts you want to get the src attribute, it builds the API URL to get the @font-face code, parses it, and returns the src that relies in it. Simples as that!

How to use

Install it with:

npm i google-fonts-src-getter

You can easily call the library by importing the function getGoogleFontData with your desired fonts:

import { getGoogleFontData } from 'google-fonts-src-getter'

const data = await getGoogleFontData({
  fonts: [
    {
      family: 'Poppins',
      style: 'italic',
      weight: 600,
    },
  ],
})

console.log(data)

How you'd get from the raw API response:

@font-face {
  font-family: 'Poppins';
  font-style: italic;
  font-weight: 600;
  src: url(https://fonts.gstatic.com/s/poppins/v21/pxiDyp8kv8JHgFVrJJLmr19lEA.ttf)
    format('truetype');
}
@font-face {
  font-family: 'Poppins';
  font-style: normal;
  font-weight: 600;
  src: url(https://fonts.gstatic.com/s/poppins/v21/pxiByp8kv8JHgFVrLEj6V1s.ttf)
    format('truetype');
}

How you'll get in data:

{
  fonts: [
    {
      fontFamily: 'Poppins',
      fontStyle: 'italic',
      fontWeight: 600,
      fontDisplay: undefined,
      subset: 'latin',
      src: 'https://fonts.gstatic.com/s/poppins/v21/pxiDyp8kv8JHgFVrJJLmr19lEA.ttf',
      format: 'truetype'
    },
    {
      fontFamily: 'Poppins',
      fontStyle: 'normal',
      fontWeight: 600,
      fontDisplay: undefined,
      subset: 'latin',
      src: 'https://fonts.gstatic.com/s/poppins/v21/pxiByp8kv8JHgFVrLEj6V1s.ttf',
      format: 'truetype'
    }
  ],
  url: 'https://fonts.googleapis.com/css2?family=Poppins%3Aital%2Cwght%400%2C600%3B1%2C600'
}

Limitations

Currently, there are no support for font effects. They will come in a future version of the package.