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

use-octokit

v0.0.4

Published

A fully-typed data-fetching hook for the Github API. Built on top of Octokit] and SWR. Use this hook inside a React component for a type-safe, data-fetching experience with caching, polling, and more.

Downloads

5

Readme

🐙 use-octokit

A fully-typed data-fetching hook for the Github API. Built on top of Octokit and SWR.

Use this hook inside a React component for a type-safe, data-fetching experience with caching, polling, and more.

💡 Checkout an example of use-octokit inside a Next.js app.

📡 Install

npm install use-octokit

yarn add use-octokit

pnpm add use-octokit

👋 Hello there! Follow me @linesofcode or visit linesofcode.dev for more cool projects like this one.

🚀 Getting Started

After you've obtained a github auth token, you can use the useOctokit hook to fetch data from the Github API.

The function inputs and outputs are all type-safe and the auto-complete in your IDE should kick-in to list all the available GitHub API endpoints and parameters.

You can also use the OctokitProvider to set the auth token for all the useOctokit calls in your app. It also accepts an octokit instance if you want to use your own.

Conditional fetching is supported by passing undefined as the first argument to useOctokit or by omitting the auth config.

Remember this is an SWR hook, so you can use all the SWR config options to customize the fetching behavior or nest it within your own SWR providers.

React hook example

import { useOctokit } from 'use-octokit';

// call the hook inside a React component

const user = useOctokit('users', 'getAuthenticated', undefined, {
  auth: session.data?.user?.accessToken,
});

// The above is fully-typed SWR response object with the data, error and isLoading properties.
// user.isLoading
// user.data.avatar_url

const [page, setPage] = useState(0);

const repos = useOctokit(
  'search',
  'repos',
  {
    sort: 'updated',
    q: 'nextjs',
    page,
  },
  {
    auth: session.data?.user?.accessToken,
  },
  {
    refreshInterval: page === 0 ? 1000 * 10 : 0,
  }
);

// The final argument is an optional SWR config object, in the example above the repos will be refetched every 10 seconds on the first page.
// repos.isLoading
// repos.data.items[0].full_name

if (repos.isLoading || user.isLoading) {
  return <div>Loading...</div>;
}

React context provider example

import { OctokitProvider } from 'use-octokit';

// inside a React component render method

// pass an auth token to the provider to use it for all the useOctokit calls in your app
return (
  <OctokitProvider auth={session.data?.user?.accessToken}>
    {children}
  </OctokitProvider>
);

😅 Do you have problems consistently typing "octokit" without typos like I do? All the exports have a "github" alias, so you can use useGithub instead of useOctokit if you need.