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

tempeh

v4.3.0

Published

type safe route builder with search params, params hook and wrapper fetcher for Next.js app directory.

Downloads

441

Readme

Tempeh

Docs: https://tempeh-docs.vercel.app

Important

Typesafe server actions support, and useRouter with type safety.

https://tempeh-docs.vercel.app/server-actions

It consists of a wrapper fetcher factory function that you can use to create fetcher functions for your need.

let's understand with the example.

Suppose we have a Next.js app where we have a dynamic page for a workspace details that takes workspaceId as a dynamic param with the help of Zod library.

Important: You need to have zod installed for this.

Traditionally how you would do that is basically:

<Link href={`/workspace/${workspaceId}`}>
  <Button>Workspace</Button>
</Link>

Now here you get little to no safety plus it breaks the dry principle. Here's how you can do it in a fully type safe manner.

Tempeh is Typescript utility library to manage your routes in a declarative manner

To get started with Tempeh, you need to define a global config. This design pattern is chosen for following reasons:

  • Tempeh stores all your routes under the hood to keep track of params and search params and for parsing them on demand.
  • Tempeh also stores all your additional base urls so that you can always decide which url to forward to in a type-safe manner.
  • Tempeh also takes certain configuration props (formatting of zod errors) to make sure your errors are more readable.

Here we define our root schema:

// Path: src/routes.ts

// You should only have a single `createRoute` constant across your function as this will make sure you do not have same named routes multiple times. It tracks the params internally.

const { createRoute } = routeBuilder.getInstance({
  additionalBaseUrls: {
    API: "https://api.example.com",
  },
  defaultBaseUrl: "/",
  formattedValidationErrors: true,
});

Root config has returned us with a createRouter utility function, this function is what we use to define our individual route configs.

// Importing from `global.route.ts`
export const WorkspaceRoute = createRoute({
  fn: ({ workspaceId }) => `/workspace/${workspaceId}`,
  name: "WorkspaceRoute",
  paramsSchema: object({
    workspaceId: string(),
  }),
  baseUrl: "API",
  searchParamsSchema: object({
    withOwner: boolean(),
  }),
});

console.log(
  WorkspaceRoute({ workspaceId: "123" }, { search: { withOwner: true } })
);

// result: https://api.example.com/workspace/123?withOwner=true
// Path: src/index.ts
// import { WorkspaceRoute } from "./routes.ts";

<Link href={WorkspaceRoute({ workspaceId: "123" })}>
  <Button>Workspace</Button>
</Link>

Or you can use our declarative routing approach as following:

<WorkspaceRoute.Link params={{ workspaceId: "123" }}>
  <Button>Workspace</Button>
</WorkspaceRoute.Link>

Package also serves the custom fetcher function generator to generate small async fetcher functions. Example:

// this fetcher creates a function to fetch the todo from json mock api
const getTodo = createEndPoint({
  httpMethod: "GET",
  path: createRoute({
    name: "getTodos",
    fn: ({ todoId }) => `/todos/${todoId}`,
    baseUrl: "https://jsonplaceholder.typicode.com", // will throw an error if custom baseRoute is not valid url
    paramsSchema: object({ todoId: number() }),
  }),
  SafeResponse: true,
  responseSchema: object({
    userId: number(),
    id: number(),
    title: string(),
    completed: boolean(),
  }),
  requestConfig: {
    headers: {
      "x-custom-header": "custom-value",
    },
  },
});

// iife to invoke the function and display result on console
(async () => {
  const todos = await getTodo({
    params: {
      todoId: 1,
    },
  });

  console.log(todos);
})();