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

vite-plugin-typed-router

v0.0.1

Published

Provide autocompletion for pages route names generated by vite-plugin-pages router

Downloads

2

Readme

🚗🚦 vite-plugin-typed-router

npm version npm downloads npm downloads

Provide a type safe router to any app with auto-generated typed definitions for route names and autocompletion for route params

Support for now:

| Framework| Support | | ---------| -- | | Vue | ✅ | | React | ❌ |
| Svelte | ❌ |

Features for Vue

  • 🔺 Uses Nuxt 3 file base routing
  • 🏎 Provides a useTypedRouter composable that returns the typed router and an object containing a tree of your routes
  • 🚦 Provides auto-completion and strict typing for route params in push and replace methods

Installation

pnpm i -D vite-plugin-typed-router vue-router
# or
npm install -D vite-plugin-typed-router vue-router

Configuration

First, register the module in the vite.config.ts

import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import TypedRouter from 'vite-plugin-typed-router';

export default defineConfig({
  plugins: [vue(), TypedRouter(/* Options */)],
});

In your main.ts, register the routes

import { createApp } from 'vue';
import App from './App.vue';

import { createRouter, createWebHistory } from 'vue-router';
// Generated routes
import routes from '~pages';

const router = createRouter({
  history: createWebHistory(),
  routes,
});

const app = createApp(App);

app.use(router);
app.mount('#app');

Options:

export interface TypedRouterOptions {
  /** Location of your src directory
   * @default "src"
   */
  srcDir?: string;
  /** Output directory where you cant the files to be saved (ex: "./generated")
   * @default "<srcDir>/generated"
   */
  outDir?: string;
  /** Location of your pages directory
   * @default "<srcDir>/pages"
   */
  pagesDir?: string;
  /** Print the generated routes tree object in output
   * @default "true"
   */
  printRoutesTree?: boolean;
}

Generated files

The module will generate 4 files each time you modify the pages folder :

  • ~/<outDir>/__routes.ts with the global object of the route names inside.
  • ~/<outDir>/__useTypedRouter.ts Composable tu simply access your typed routes
  • ~/<outDir>/typed-router.d.ts containing the global typecript definitions and exports

Usage in Vue/Nuxt

Requirements

You can specify the output dir of the generated files in your configuration. It defaults to <srcDir>/generated

import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import TypedRouter from 'vite-plugin-typed-router';

export default defineConfig({
  plugins: [vue(), TypedRouter({outDir: 'generated'})],
});

How it works

Given this structure

    ├── pages
        ├── index
            ├── content
                ├── [id].vue
            ├── content.vue
            ├── index.vue
            ├── communication.vue
            ├── statistics.vue
            ├── [user].vue
        ├── index.vue
        ├── forgotpassword.vue
        ├── reset-password.vue
    │   └── login.vue
    └── ...

The generated route list will look like this

export const routesNames = {
  forgotpassword: 'forgotpassword' as const,
  login: 'login' as const,
  resetPassword: 'reset-password' as const,
  index: {
    index: 'index' as const,
    communication: 'index-communication' as const,
    content: {
      id: 'index-content-id' as const,
    },
    statistics: 'index-statistics' as const,
    user: 'index-user' as const,
  },
};
export type TypedRouteList =
  | 'forgotpassword'
  | 'login'
  | 'reset-password'
  | 'index'
  | 'index-communication'
  | 'index-content-id'
  | 'index-statistics'
  | 'index-user';

You can disable routesNames object generation if you don't need it with the printRoutesTree option

Usage with useTypedRouter hook

useTypedRouter is an exported composable from nuxt-typed-router. It contains a clone of vue-router but with strictly typed route names and params type-check

<script lang="ts">
// The path here is `~/generated` because I set `outDir: './generated'` in my module options
import { useTypedRouter } from '~/generated';

export default defineComponent({
  setup() {
    // Fully typed
    const { router, routes } = useTypedRouter();

    function navigate() {
      // Autocompletes the name and infer the params
      router.push({ name: routes.index.user, params: { user: 1 } }); // ✅ valid
      router.push({ name: routes.index.user, params: { foo: 1 } }); // ❌ invalid

      router.push({ name: 'index-user', params: { user: 1 } }); // ✅ valid
      router.push({ name: 'index-user', params: { foo: 1 } }); // ❌ invalid
      router.push({ name: 'index-foo-bar' }); // ❌ invalid
    }

    return { navigate };
  },
});
</script>