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

uri-manager

v1.0.0

Published

Create URI schema and serialize type safe (support subpath, relative path)

Downloads

57

Readme

uri-manager

image

URI serialize and match data, use safety type! Successfully integration to react-router-v6!

Example

import { URISchema } from 'uri-manager';

const product = new URISchema('/product');
const productList = product.createSubPath<{ search?: string; }>('/list');
const productDetail = product.createSubPath('/:productId');

// serialize
productList.serialize(); // "/product"
productList.serialize({ search: 'keyword' }); // "/product?search=keyword"
productDetail.serailize({ productId: '10' }); // "product/10"

// match
productDetail.match('/foo/bar'); // null
productDetail.match('/product/15'); // { productId: 15 }

// deep sub path serialize
const media = new URISchema('/media');
const mediaDetail = media.createSubPath('/:mediaId');
const mediaDetailComments = mediaDetail.createSubPath('/comments');
const mediaDetailCommentDetail = mediaDetailComments.createSubPath('/:commentId');

media.serialize(); // "/media"
mediaDetail.serialize({ mediaId: '10' }); // "/media/10"
mediaDetailComments.serialize({ mediaId: '15' }); // "/media/15/comments"
mediaDetailCommentDetail.serialize({ mediaId: '20', commentId: '25' }); // "/media/20/comments/25"

Features

Manually definition type with query string

import { URISchema, ParamsFromPath } from 'uri-manager';

const productList = new URISchema<{ search?: string; }>('/product');

productList.serialize(); // ✅ "/product"
productList.serialize({ search: 'foo' }); // ✅ "/product?search=foo"

const productDetail = new URISchema<ParamsFromPath<'/product/:productId'> & { tag: string }>('/product/:productId(\\d+)');

productDetail.serialize({ productId: 10, tag: 'bar' }); // ✅ "/product/10?tag=bar"

Using defaultParams to setting params

import { URISchema } from 'uri-manager';

const filter = new URISchema({
  template: '/filter/:category?',
  defaultParams: {
    category: 'ALL'
  }
});

filter.serialize(); // ✅ "/filter/ALL"
filter.serialize({ category: 'FEATURES' }); // ✅ "/filter/FEATURES"

Using baseSchema to create app schema

import { URISchema } from 'uri-manager';

const viewSchema = new URISchema<{ view: string }>({ baseSchema: 'weverseshop://weverseshop.benx.co' });

viewSchema.serialize({ view: 'noticeDetail' }); // ✅ "weverseshop://weverseshop.benx.co/?view=noticeDetail"

Integration with react-router

This version(v1.0.0) is integration to react-router-v6

import React from 'react';
import { Route, Routes, useParams, Outlet } from 'react-router';
import { URISchema, GetURISchemaParamKeys } from 'uri-manager';

const product = new URISchema('/product');
const productDetailRoute = product.createSubPath('/product/:productId');

const ProductDetail = () => {
  const { productId } = useParams<GetURISchemaParamKeys<typeof productDetailRoute>>();
  return (
    <h1>Product detail id : {productId}</h1>
  );
};

declare function ProductLayout(): React.FC;
declare function ProductList(): React.FC;

const App = () => {
  return (
    <Routes>
      <Route path={product.relativePath} element={
        <ProductLayout>
          <Outlet />
        </ProductLayout>
      }>
        <Route index element={<ProductList />} />
        <Route path={productDetailRoute.relativePath} element={<ProductDetail />} />
      </Route>
    </Routes>
  )
};