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

and-action

v0.1.58

Published

Action Router & Side-kick Form component for Remix apps

Downloads

12

Readme

🎬 And Action

A Feature-Rich API Router for 💿 Remix.run with Typed Forms and ActionData Guards

This library enhances the developer experience by simplifying the management of complex action API routes in Remix applications.

Warning: This library is currently in its early stages and should not be used in production environments.

Inspiration and Background

While developing this library, the author discovered a similar project named Conform. Despite the similarities, the unique features of this library warranted its continuation. The initial inspiration came from a Remix tutorial by Ryan Florence, highlighting a method to control logic branches using a hidden _action input field within action functions. The ambiguous use of the term 'action' in this context led to the introduction of a more descriptive variable named intent, enhancing clarity.

The library also addresses the handling of HTTP methods beyond GET, such as POST, PUT, and DELETE. It encourages developers to structure their code to prevent complexity before it becomes unmanageable.

Form handling was another area of focus. The library aims to maximize type safety and improve the developer experience by streamlining form-related operations in larger applications.

This approach doesn't replace the existing action handling in Remix but offers a refined method for building and managing complex API actions, promoting code reuse and more structured execution across different parts of an application.

Intent

Defining the intent explicitly improves the alignment between your API and client-side operations, leading to more efficient backend integrations.

Intents are defined as simple string enumerations:

export type FooIntents = "foo" | "bar" | "foo.bar";

This facilitates more organized API definitions and makes use of dot notation for clarity in complex domains.

Based on these intents, API actions are configured using the ApiResource type:

export const fooApiServer: ResourceApi<FooIntents, FooContext, FooResponse> = {
    foo,
    bar: async () => ({ data: { answer: "should be bar" } }),
    "foo.bar": async () => ({ data: { answer: "should be foo.bar" } }),
};

Detailed API Action Definitions

The ```ResourceApi structure allows for simple function definitions for methods other than GET:

Note that bar and foo.bar are just simple functions, which will serve all the non GET methods, like POST, PUT, DELETE, etc.

But foo is a bit more complex. Here we do a breakdown of the type of method and isolates the logic for each method:

export const POST: ApiAction<FooContext, FooDataResponse> = async ({context}) => {
    const response: DataResponse<{answer: string; defaultContext: SuperJSONResult;}> = {
        data: {
            answer: "should be foo",
            defaultContext: serialize(context),
        },
    };
    return response;
};

const DELETE: ApiAction<FooContext, Response> = async () => {
    return redirect("/");
};

export const foo: ApiResource<FooContext, FooResponse> = {
    POST,
    DELETE,
};

Note that we are using superjson to serialize the context in the formdata of a form/request. This might change once Remix v3 is released.

The rest of this document is a work in progress.