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 🙏

© 2026 – Pkg Stats / Ryan Hefner

alten-react-http-hook

v0.0.4

Published

A package for executing async functions (with http requests in mind) while providing functionality for caching and type checks.

Downloads

21

Readme

Alten React useHttpHook

This package provides an easy way to manage the state from Promises (with http requests in mind) using a single hook. It provides an easy way to cache results in either local, or session storage for increased performance. Use the dependency array to trigger a new http/promise request, just like you're used to with useState!

Examples

Most minimal use

import React from "react";
import { useHttpHook } from "alten-react-http-hook";

const getTodos = async () => {
    return yourHttpLib.get(`/todos`);
}

function App() {
    
    const { result, error, loading } = useHttpHook(getTodos);

    if (loading) return <p>loading...</p>
    
    return (
        <div className="App">
            { result?.map( todo => <span key={todo.id}>{todo.title}</span>)}
        </div>
    );
}

With types and typechecking

import React from "react";
import { useHttpHook } from "alten-react-http-hook";

interface Todo {
    userId: number;
    id: number;
    title:string;
    completed: boolean;
}

const isATodo = (obj: any): obj is Todo => {
    return 'id' in obj && 'message' in obj;
}

const getTodos = async (): Promise<Todo[]> => {
    return yourHttpLib.get(`/todos`);
}

function App() {
    // The first param is the http request (or any other promise), the second param is an object with configuration.
    const { result, error, loading } = useHttpHook<Todo[]>(() => getTodos(), {typeCheck: isATodo});

    if (loading) return <p>loading...</p>
    
    return (
        <div className="App">
            { result?.map( todo => <span key={todo.id}>{todo.title}</span>)}
        </div>
    );
}

With caching

import React from "react";
import { useHttpHook } from "alten-react-http-hook";

interface Todo {
    userId: number;
    id: number;
    title:string;
    completed: boolean;
}

const isATodo = (obj: any): obj is Todo => {
    return 'id' in obj && 'message' in obj;
}

const getTodos = async (): Promise<Todo[]> => {
    return yourHttpLib.get(`/todos`);
}

function App() {
    
    const { result, error, loading } = useHttpHook<Todo[]>(
        getTodos,
        {
            // Typecheck to see if result is a single todo. Will currently fail because we are getting a list of todos.
            typeCheck: isATodo,
            cache: {
                // Will cache result.
                cacheResult: true,
                // can be found in session or localstorage under this key.
                cacheKey: "todos",
                // If the storage is older than the current time + 20000 seconds, the result will be refetched.
                cacheExpires: 20000,
                // use local storage istead of session storage so the cache persists through app visits.
                useLocalStorage: true
            }
        });

    if (loading) return <p>loading...</p>
    
    return (
        <div className="App">
            { result?.map( todo => <span key={todo.id}>{todo.title}</span>)}
        </div>
    );
}

Use onSuccess and onError to take action when a promise resolves. If a typecheck fails, the onError will also run.

import React from "react";
import { useHttpHook } from "alten-react-http-hook";

interface Todo {
    userId: number;
    id: number;
    title:string;
    completed: boolean;
}

const getTodos = async (id: number): Promise<Todo[]> => {
    return yourHttpLib.get(`/todos/${id}`);
}

function App() {
    
    const { result, error, loading } = useHttpHook<Todo[]>(
        () => getTodos(1),
        // Extra Configuration
        {
            // The result will be of Todo[]
            onSuccess: (result: Todo[]) => {
                console.log("The request succeeded, do whatever you want with the response.");
            },
            onError: () => {
                console.log("Failed! What now?");
            }
        }
    );

    if (loading) return <p>loading...</p>
    
    return (
        <div className="App">
            { result?.map( todo => <span key={todo.id}>{todo.title}</span>)}
        </div>
    );
}

Now let's combine everything we've seen, and add a dependency array for automatic reloads.

import React from "react";
import { useHttpHook } from "alten-react-http-hook";

interface Todo {
    userId: number;
    id: number;
    title:string;
    completed: boolean;
}

const isATodo = (obj: any): obj is Todo => {
    return 'id' in obj && 'message' in obj;
}

const getTodos = async (): Promise<Todo[]> => {
    return yourHttpLib.get(`/todos`);
}
const getTodoById = async (id: number): Promise<Todo> => {
    return yourHttpLib.get(`/todos/${id}`);
}

function App() {
    
    const [id, setId] = useState<number>(1);
    const todos = useHttpHook<Todo[]>(
        getTodos,
        {
            cache: {
                // Will cache result.
                cacheResult: true,
                // can be found in session or localstorage under this key.
                cacheKey: "todos",
                // If the storage is older than the current time + 20000 seconds, the result will be refetched.
                cacheExpires: 20000,
                // use local storage istead of session storage so the cache persists through app visits.
                useLocalStorage: true
            }
        });
    // We add the id property to the dependency array, so that every time it changes we fetch the selected todo. 
    const todo = useHttpHook<Todo>(() => getTodo(id), {typeCheck: isATodo}, [id]);

    if (loading) return <p>loading...</p>
    
    return (
        <div className="App">
            { todos.result?.map( todo => {
                return (
                    // Every time we click on a todo, we load the todo details beacuse the id has been added to the dependency array.. 
                    <span key={todo.result?.id} onClick={() => setId(todo.id)}>
                        {todo.result?.title}
                    </span>
                )
            })}

            {/* Every time we click on t */}
            <div>
                {todo.result?.title}
            </div>
        </div>
    );
}