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

kea-ajax

v1.0.2

Published

Kea-ajax is a simplified version of [kea-loaders](https://github.com/keajs/kea-loaders) plugin.

Downloads

7

Readme

KEA-AJAX

Kea-ajax is a simplified version of kea-loaders plugin.

Installation

npm install kea-ajax
import { ajax } from 'kea-ajax';

const blogLogic = kea([
    actions({ 
        setBlog: (blog) => ({blog})
    }),
    ajax(({actions}) => ({
        load: async () => {
            const blog = await axois.get(`/blog`);
            actions.setBlog(blog)
        }
    })),
    reducers({
        blog: [
            null as null | Blog,
            {
                setBlog: (_, {blog}) => blog
            }
        ]
    })
])

Unlike the kea-loaders plugin, kea-ajax plugin does not automatically update reducers for you. You have to manually call actions inside the callback (actions.setBlog(blog) above). This is allows more clear code where you can see what's happening by reading the code. It also encourages calling multiple actions within the callback.

Each property of the ajax definitions adds the following actions and reducers to the logic.

// actions
load
loadStart
loadSuccess
loadError

// reducers
loadAjax

Using with React

Let's first write a little advanced logic. The following postsLogic fetches blog posts from an API. There are two reducers (states): posts saves the posts list. hasMore saves whether there are more posts to load.

const postsLogic = kea([
    actions({ 
        setPosts: (post: Post) => ({post}),
        setHasMore: (hasMore: hasMore) => ({hasMore})
    }),
    ajax(({actions}) => ({
        load: async () => {
            /**
             * API Response
             * data = {
             *     posts: Post[],
             *     hasMore: boolean
             * }
             */
            const data = await axois.get(`/posts`);
            
            actions.setPosts(data.posts)
            actions.setHasMore(data.hasMore)
        }
    })),
    reducers({
        posts: [
            [] as Post[],
            {
                setPosts: (_, {blog}) => blog
            }
        ],
        hasMore: [
            false,
            {
                setHasMore: (_, {hasMore}) => hasMore
            }
        ]
    }),
    events(({actions}) => {
        afterMount: actions.load
    })
])
import { useValues } from 'kea';

export default function Posts() {
    
    const { loadAjax, posts } = useValues(postsLogic)
    
    return loadAjax.status === 'loading' ? <Loader /> : <Posts posts={posts} />
    
}

Here, the load actions will be called when the component is mounted, because we added it to events in the logic. loadAjax is a reducer automatically set by the ajax plugin. The following is the interface of the loadAjax reducer.

interface KeaAjaxObject {
    status: null | 'loading' | 'success' | 'error',
    error: string | null,
}

You can use the loadAjax to show a loader, or even show an error component with the error message when the status is error.

Calling actions manually

Let's see an example where you call an ajax action on a button click in React.

const usersLogic = kea([
    actions({
        addUser: (user: User) => ({user}),
    }),
    ajax(({actions}) => ({
        createUser: async ({name, email} : {name: string, email: string}) => {
            const user = await axois.post('/user', {name, email})
            actions.addUser(user)
        }
    })),
    reducers({
        users: [
            [],
            {
                addUser: (state, {user}) => [...state, user]
            }
        ]
    })
])
function App() {
    
    const { createUser } = useActions(usersLogic)
    
    function handleClick() {
        createUser({
            name: "New User",
            email: "[email protected]"
        })
    }
    
    // you can render a loading icon while the user is being created, by using `createUserAjax` value.
    
    return <button onClick={handleClick}>Add User</button>
    
}