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

solid-safe-action

v1.0.0

Published

This package is designed to handle mutations in your solid-start app efficiently. It ensures type safety, input validation, and handles server errors and success states, providing a reliable and safe way to execute Solidjs Server Actions.

Downloads

3

Readme

solid-safe-action

This package is designed to handle mutations in your solid-start app efficiently. It ensures type safety, input validation, and handles server errors and success states, providing a reliable and safe way to execute Solidjs Server Actions.

Installation

You can install this package via npm or yarn:

npm install solid-safe-action
# or
yarn add  solid-safe-action

Usage Example

Here is an example of how to use solid-safe-action in a SolidJS application:

src/actions/index.ts

First, create a safe action:


'use server'
import { createSafeAction } from "solid-safe-action";
import { simulateDatabaseCall } from "~/lib/mock"; // mock db call
import { CreateForm } from "~/lib/schema"; // zod schema for validation 
import { InputType, ReturnType } from "~/lib/types"; // type infer from zod schema and type of your action state 

const handler = async (data: InputType): Promise<ReturnType> => {
  const { title } = data;
  let item;

  try {
    // Mock db call 
    item = await simulateDatabaseCall({ title})

  } catch (error) {
    return { error: 'Failed to create!' };
  }


  return { data: item };
};

export const createForm = createSafeAction(CreateForm, handler);

src/lib/schema.ts

Define types based on the Zod schema:


import { z } from "zod";

export const CreateForm = z.object({
  title: z.string({
    required_error: "Title is required",
    invalid_type_error: "Title is required"
  }).min(4, { message: 'Title too short' }),
});

src/lib/types.ts

Define types based on the Zod schema:

import { z } from "zod";

import { ActionState } from "~/lib/create-safe-action";

import { CreateForm } from "./schema";//

export type InputType = z.infer<typeof CreateForm>;
export type ReturnType = ActionState<InputType, { title: string; }>;

src/routes/index.tsx

Finally, use the useSafeAction hook to execute the action:

import { useSafeAction } from "solid-safe-action";
import { createForm } from "~/actions/index;

const ExampleButton = () => {
  const { execute, isLoading, } = useSafeAction(createForm, {
    onSuccess: (data) => {
      window.alert(`Success: ${JSON.stringify(data, null, 2)}`);
    },
    onError: (error) => {
      window.alert("Error: " + error);
    }
  });

  const onClick = (title: string) => {
    execute({ title });
  };

  return ( 
    <button disabled={isLoading()} onClick={() => onClick("Button")}>
      Click to make a database call using server action
    </button>
  );
};

export default ExampleButton;