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

remix-action-router

v0.0.8

Published

Declarative action matching for Remix actions

Downloads

13

Readme

remix-action-router

Declarative action matching for Remix actions

remix-action-router is a small utility that allows setting up your Remix actions as route files. It takes Remix's declarative approach to network calls and applies it to route actions.

GitHub Workflow Status npm

Getting Started

Let's assume we have a route component in app/routes/item/$id.tsx that handles basic CRUD logic for shopping cart items.

// filename: app/routes/item/$id.tsx

export function action({ request }) {
  const formData = await request.formData();
  const actionName = formData.get("_action");

  switch (actionName) {
    case "create": {
      // create logic
    }
    case "delete": {
      // delete logic
    }
    case "update": {
      // update logic
    }
  }
}

With remix-action-router you can move the logic of each action to a specific file under the routes/actions__ folder and have remix-action-router take care of the action matching logic.

The folder structure must match your routes structure in such a way that each route file becomes its own folder, and each action becomes a file under said folder.

Effectively, your routes folder will look like this:

app/
└── routes/
    ├── actions__/
    │   └── item/
    │       └── $id/
    │           ├── create.ts
    │           ├── delete.ts
    │           └── update.ts
    └── item/
        └── $id.tsx

Back to app/routes/item/$id.tsx, you can replace your action function definition with:

// filename: app/routes/item/$id.tsx

import { callAction } from "remix-action-router";

export const action = callAction;

Each of your actions files (eg. app/routes/actions__/item/$id/create.ts) should export a default function. This function should include the action logic.

For example:

// filename: app/routes/actions__/item/$id/create.ts
import type { ActionRouteFunction } from "remix-action-router";

const create: ActionRouteFunction = ({ formData }) => {
  return prisma.item.create({ data: ... });
};

export default create;

Configuration

The callAction method accepts a second parameter that is a configuration object.

// filename: app/routes/item/$id.tsx

export function action(args) {
  return callAction(args, { actionName: "customActionName" });
}

The options are:

actionName

By default, remix-action-router will use the value in the _action field on the request's formData property. This can be overridden using the actionName config field.

actionsRoute

By default, remix-action-router will search for action files under the app/routes/actions__ directory. This option allows configuring the path for the actions. Note that the action files route is relative to app/routes.

Caveats

remix-action-router uses the _action FormData field to determine which action to run. To get that value, remix-action-router must call await request.formData().

Because of that, calling await request.formData() in your action functions will result in an error. To overcome this, remix-action-router will add the formData object as an argument to your action functions.

So, instead of Remix's usual action arguments:

export interface DataFunctionArgs {
  request: Request;
  context: AppLoadContext;
  params: Params;
}

You get arguments that look like so:

interface ActionArgs {
  request: Request;
  context: AppLoadContext;
  params: Params;
  formData: FormData;
}

All other params are left untouched.