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

next-type-link-gen

v1.0.4

Published

Plugin that converts the internal links of the `next.js` app so that they can be handled type-safely.

Downloads

7

Readme

next-type-link-gen

npm version

next-type-link-gen is a custom hooks generator that makes Next.js internal links type safe.

Installation

npm

$ npm install next-type-link-gen

yarn

$ yarn add next-type-link-gen

Example

import {
  dynamicRoute,
  nextLinksHooksFactory,
  staticRoute,
} from 'next-type-link-gen'

const useNextLinks = nextLinksHooksFactory({
  top: staticRoute('/'),
  name: dynamicRoute<{
    name: string
    a?: string
    b?: number
  }>('/[name]'),
})

const YourLink: React.FC = () => {
  const links = useNextLinks()

  return (
    <div>
      <Link
        // href: /
        href={links.top.toUrl()}
        // if `pathname` in next/router match, true
        current={links.top.isCurrent()}
      >
        Top
      </Link>
      <Link
        // href: /fake_name?a=aaa&b=999
        href={links.name.toUrl({
          name: 'fake_name',
          a: 'aaa',
          b: 999,
        })}
        // if `pathname` in next/router match, true
        current={links.name.isCurrent()}
      >
        User top
      </Link>
    </div>
  )
}

API

staticRoute

create static route. If you want, add parameter type generics.

const rootPage = staticRoute<{
  a?: string
  b?: number
}>('/')

dynamicRoute

create dynamic route. Next.js routing parameter & get parameter type define. Pass the equivalent of Next.js router.pathname as an argument.

const userPage = dynamicRoute<{
  a?: string
  b?: number
}>('/[name]')

nextLinksHooksFactory

nextLinksHooksFactory is custom hooks factory function. Pass an object with ReturnType<typeof staticRoute> or ReturnType<typeof dynamicRoute> as value as an argument.

const useNextLinks = nextLinksHooksFactory({
  top: staticRoute('/'),
  name: dynamicRoute<{
    name: string
    a?: string
    b?: number
  }>('/[name]'),
})

// usage
const links = useNextLinks()
console.log(links.top.toUrl())

UrlScheme

this hooks contain your routing type. if you want your links, [route].toUrl call. toUrl: parameters must be passed as arguments. isCurrent: returns true if it matches the route in Next.js router.pathname. toRouteString: returns route match string.

console.log(links.top.toUrl())
console.log(links.top.isCurrent())
console.log(links.top.toRouteString())

Test

You can get the information of the page directory of Next.js by readNextPagesRoute, and you can also get with linksMapToRouteString the route information you registered, which you can use to test the biosynthesis of links.

import { renderHook } from '@testing-library/react-hooks'
import { linksMapToRouteString, readNextPagesRoute } from 'next-type-link-gen/utils'
import { useNextLinks } from './url'

test('[useNextLinks] routes', () => {
  const { result } = renderHook(useNextLinks)

  const pageUrls = readNextPagesRoute()
  const routeUrls = linksMapToRouteString(result.current)

  expect(pageUrls).toEqual(expect.arrayContaining(routeUrls))
})