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-typed-routes/lib

v0.0.4

Published

NOTE: This package is a WIP and is subject to change.

Downloads

1

Readme

@next-typed-routes/lib

NOTE: This package is a WIP and is subject to change.

This library aims to make using next.js routing and api calls more type safe. It does so by providing a wrapped and/or extended version of hooks, provided by next.js.

Setup

Installation

$ pnpm add @next-typed-routes/lib
# Downloads the library from npm (routing)
# and rust binary release (for route types codegen)

Transpilation of JSX files

This package currently needs to be transpiled by next to use the Link component.

// next.config.mjs

const config = {
  // -- snip
  transpilePackages: ['@next-typed-routes/lib'],
  // -- snip
};

export default config;

Generating routing types

Altough the library works in a route agnostic way without generating types, its purpose is kinda defeated by skipping this step.

# Run the binary to generate the route types
$ pnpm ntr # or use pnpm ntr ~/path/to/project if not in cwd

Parsing project at "/path/to/project"

> Wrote types to @next-typed-routes/lib
> Wrote ts generated output to "/path/to/project/generated/routes.ts"

Utilisation

App router

// ~/app/page.tsx

import { useAppRouter, routers, appRouter } from '@next-typed-routes/lib';

export default () => {
  const router = useAppRouter();
  // Or use one of the alternative ways. The API is still being worked on.
  // const router = routes.appRouter.useRouter()
  // const router = appRouter.useRouter()

  // Specify the current path template in `useParams` and get a typed object back
  const { userId, orderId } = appRouter.useParams('/user/[userId]/[orderId]');

  router.push('/user'); // Works

  // @ts-expect-error
  router.push('/products/[id]/[variant]'); // Needs id & variant parameters

  // Solution:
  router.push('/products/[id]/[variant]', {
    params: {
      id: 'some-uuid',
      variant: 'first',
    },
    // Optional extra properties
    hash: 'optional-hash',
    query: { hello: 'world '}
  }); // `/products/some-uuid/first?hello=world#optional-hash`

  return null;
}

Pages router

// ~/pages/index.tsx

import { usePagesRouter } from '@next-typed-routes/lib';

export default () => {
  const router = usePagesRouter();

  router.push({ path: '/user' }); // Works

  // @ts-expect-error
  router.push({
    path: '/products/[id]/[variant]'
    // TS error: ... Property 'params' is missing in type ... 
  }); // Needs id & variant parameters

  // Solution:
  router.push({
    path: '/products/[id]/[variant]',
    params: {
      id: 'some-uuid',
      variant: 'first',
    },
    // Optional extra properties
    hash: 'optional-hash',
    query: { hello: 'world '}
  }); // `/products/some-uuid/first?hello=world#optional-hash`

  return null;
}

Create URL

// /some/file/.tsx?

import { createUrl } from "@next-typed-routes/lib";

const userUrl = createUrl('/user'); // `/user`

// @ts-expect-error
// Expected 2 arguments, but got 1.
const myUrl = createUrl('/app-router/optional/[[...optional]]');

// Solution:
const myUrl = createUrl('/app-router/optional/[[...optional]]', {
  params: { optional: ['foo', 'bar', 'baz',] }, // { optional?: string[] | undefined; }
  // Optional extra properties
  hash: 'optional-hash',
  query: { hello: 'world ' }
}); // `/app-router/optional/foo/bar/baz?hello=world#optional-hash`