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 🙏

© 2025 – Pkg Stats / Ryan Hefner

uzeful

v0.0.2

Published

Hooks for the backend

Downloads

10

Readme

uzeful

Hooks for the backend.

Features

  • DX improvements
  • state management
  • logger
  • unified error handling interface

uze allows you to access the context of a request with zero hassel in backend environments and provides helpers for logging, error handling & managing state

// route handler for getting user info
export default async function getUserInfo() {
  const { request } = uzeContext();
  
  const db = await uzeDatabase();
  const user = await db.execute("...");

  return Response.json({
    user,
  })
}

Installation

npm install uze

Getting Started

Cloudflare Workers

import {createUze} from "uzeful";
import type {D1Database, Request} from "@cloudflare/workers-types";

export interface Env {
  DB: D1Database;
}

const uze = createUze<Env, Request>();

// hook to use in all of your route handlers
export const uzeContext = uze.hooks.uzeContext;

// code that processes requests
const handler = async (): Promise<Response> => {
  const context = uzeContext();
...
}

export default {
  fetch: async (req: Request, env: Env, ctx: any) => {
    return await uze.handle(
      {
        request: req,
        env,
        waitUntil: ctx.waitUntil,
        rawContext: ctx,
      },
      handler
    );
  },
};

With router


import {createRouter} from "uzeful/router";

const router = createRouter()
  .all("*", () => {
    return Response.json({message: "Hello World"});
  });

export default {
  fetch: async (req: Request, env: Env, ctx: any) => {
    return await uze.handle(
      {
        request: req,
        env,
        waitUntil: ctx.waitUntil,
        rawContext: ctx,
      },
      router.handler,
    );
  },
};

Usage

Making Hooks

Making a hook is as simple as making a function with the prefix uze. No magic required. As long as you run these functions within the handler function you pass to uze you will be able to access the current request context.

export const uzeDatabase = async () => {
  const { env } = uzeContext();
  return env.DB;
}

After Hooks

You can use the uzeAfter hook to run code after the response has been created. This can be useful when you want to add headers to a response, such as CORS.

import {uzeAfter} from "uzeful";
import {createRouter} from "uzeful/router";

const router = createRouter()
  .all("*", async () => {
    uzeAfter((response) => {
      response.headers.set("Access-Control-Allow-Origin", "*");
    });
  })
  .get("/users/:id", userHandler);

State Management

A lot of the time you want to manage state related to a single request. uze provides a way to do this with useState.

import {uzeState, createStateKey} from "uzeful";

export interface UserAccount {
  id: string;
  name: string;
}

const USER_ACCOUNT_KEY = createStateKey<UserAccount>("user-account");

export default async function getUserInfo() {
  const [getUserAccount, setUserAccount] = uzeState(USER_ACCOUNT_KEY);

  let userAccount = await getUserAccount();
  if (!userAccount) {
    userAccount = await fetchUserAccount();
    setUserAccount(userAccount);
  }

  return Response.json({
    userAccount,
  });
}

With defaults

import {uzeState, createStateKey} from "uzeful";

const EVENTS_KEY = createStateKey<string[]>("events", () => ["defaultEvent"]);

export default async function getUserInfo() {
  const [getEvents] = uzeState(EVENTS_KEY);

  let events = await getEvents();
  events.push("newEvent");
  console.log(events); // ["defaultEvent", "newEvent"]

...
}

Error Handling

Uze exposes SendableError which provides a unified interface to handle errors. For the full documentation: https://www.npmjs.com/package/sendable-error

import {SendableError} from "uzeful";

export default async function getUserInfo() {
  const db = await uzeDatabase();

  const user = await db.execute("...");

  if (!user) {
    throw new SendableError({
      message: "User not found",
      status: 404,
      public: true,
      code: "users/not-found"
    });
  }

...
}

Note: all errors are private by default. This means the response body will contain and obfuscated error. To make an error public, set the public property to true.

Router

uze provides a wrapper around itty-router's AutoRouter to provide a simple way to define routes.

See the full documentation here: https://itty.dev/itty-router/routers/autorouter


import {createRouter} from "uzeful/router";

const router = createRouter()
  .all("*", () => {
    return Response.json({message: "Hello World"});
  });