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

contexta

v1.0.1

Published

A React-inspired Context API for Node.js with async/await support. Supported in nodejs, bun, and deno via async-local-storage.

Downloads

300

Readme

contexta

A React-inspired Context API for Node.js with async/await support. Supported in nodejs, bun, and deno via async-local-storage.

Usage

Imagine you want to access the currently authenticated user across various functions without passing the user object through every function parameter. With contexta, you can effortlessly manage and access contextual data throughout your Node.js application, even in asynchronous workflows.

Create a Context

Create a context for the viewer (currently authenticated user). This context will hold the user information and provide a way to access it throughout your application.

// ViewerContext.ts
import createContext from 'contexta';

type User = {
  id: string;
  name: string;
};

const ViewerContext = createContext<User | null>(null);

export default ViewerContext;

Create a Hook to Access the Context

// useViewer.ts
import useContext from 'contexta';
import ViewerContext from './ViewerContext';

export default function useViewer(): User | null {
  return useContext(ViewerContext);
}

Integrate contexta with Express Middleware

Set up an Express middleware that loads the viewer from the request (e.g., from an authorization header) and sets it in the context. This ensures that the user information is accessible throughout the request lifecycle without manually passing it to every function.

// server.ts
import express from 'express';
import ViewerContext from './ViewerContext';
import useViewer from './useViewer';
import { performAction } from './someService';

const app = express();
const port = 3000;

async function fetchUserByToken(token: string): Promise<User | null> {
  // Simulate a database lookup
  if (token === 'valid-token') {
    return { id: '1', name: 'Alice' };
  }
  return null;
}

app.use(async (req, res, next) => {
  const authHeader = req.headers.authorization;
  let viewer: User | null = null;

  if (authHeader) {
    viewer = await fetchUserByToken(authHeader);
  }

  // Run the ViewerContext with the loaded viewer
  ViewerContext.run(viewer, () => {
    next();
  });
});

// Example route handler
app.get('/', async (req, res) => {
  const user = useViewer();
  await performAction(); // Example asynchronous operation

  if (user) {
    res.send(`Hello, ${user.name}!`);
  } else {
    res.send('Hello, Guest!');
  }
});

app.listen(port, () => {
  console.log(`Server is running on http://localhost:${port}`);
});