Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]:add with-server-actions example and bug resistant package to execute them safely #1319

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions examples/with-server-actions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# SolidStart

Everything you need to build a Solid project, powered by [`solid-start`](https://start.solidjs.com);

## Creating a project

```bash
# create a new project in the current directory
npm init solid@latest

# create a new project in my-app
npm init solid@latest my-app
```

## Developing

Once you've created a project and installed dependencies with `npm install` (or `pnpm install` or `yarn`), start a development server:

```bash
npm run dev

# or start the server and open the app in a new browser tab
npm run dev -- --open
```

## Building

Solid apps are built with _adapters_, which optimise your project for deployment to different environments.

By default, `npm run build` will generate a Node app that you can run with `npm start`. To use a different adapter, add it to the `devDependencies` in `package.json` and specify in your `vite.config.js`.
22 changes: 22 additions & 0 deletions examples/with-server-actions/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "example-with-server-actions",
"type": "module",
"scripts": {
"dev": "vinxi dev",
"build": "vinxi build",
"start": "vinxi start"
},
"dependencies": {
"@solidjs/meta": "^0.29.2",
"@solidjs/router": "^0.11.5",
"@solidjs/start": "^0.5.2",
"solid-js": "^1.8.14",
"vinxi": "^0.2.1",
"zod": "^3.22.4",
"solid-safe-action": "^1.0.0"

},
"engines": {
"node": ">=18"
}
}
Binary file added examples/with-server-actions/public/favicon.ico
Binary file not shown.
24 changes: 24 additions & 0 deletions examples/with-server-actions/src/actions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"use server";

import { createSafeAction } from "solid-safe-action";
import { simulateDatabaseCall } from "~/lib/mock";
import { CreateTitle } from "~/lib/schema";
import { InputType, ReturnType } from "~/lib/types";

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

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

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


return { data: item };
};

export const createTitle = createSafeAction(CreateTitle, handler);
39 changes: 39 additions & 0 deletions examples/with-server-actions/src/app.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
body {
font-family: Gordita, Roboto, Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
}

a {
margin-right: 1rem;
}

main {
text-align: center;
padding: 1em;
margin: 0 auto;
}

h1 {
color: #335d92;
text-transform: uppercase;
font-size: 4rem;
font-weight: 100;
line-height: 1.1;
margin: 4rem auto;
max-width: 14rem;
}

p {
max-width: 14rem;
margin: 2rem auto;
line-height: 1.35;
}

@media (min-width: 480px) {
h1 {
max-width: none;
}

p {
max-width: none;
}
}
23 changes: 23 additions & 0 deletions examples/with-server-actions/src/app.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// @refresh reload
import { MetaProvider, Title } from "@solidjs/meta";
import { Router } from "@solidjs/router";
import { FileRoutes } from "@solidjs/start";
import { Suspense } from "solid-js";
import "./app.css";

export default function App() {
return (
<Router
root={props => (
<MetaProvider>
<Title>SolidStart - Basic</Title>
<a href="/">Index</a>
<a href="/about">About</a>
<Suspense>{props.children}</Suspense>
</MetaProvider>
)}
>
<FileRoutes />
</Router>
);
}
21 changes: 21 additions & 0 deletions examples/with-server-actions/src/components/ActionButton.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
.actionbtn {
font-family: inherit;
font-size: inherit;
padding: 1em 2em;
color: #335d92;
background-color: rgba(68, 107, 158, 0.1);
border-radius: 2em;
border: 2px solid rgba(68, 107, 158, 0);
outline: none;
width: 200px;
font-variant-numeric: tabular-nums;
cursor: pointer;
}

.actionbtn:focus {
border: 2px solid #335d92;
}

.actionbtn:active {
background-color: rgba(68, 107, 158, 0.2);
}
27 changes: 27 additions & 0 deletions examples/with-server-actions/src/components/ActionButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use client'

import { useSafeAction } from "solid-safe-action";
import { createTitle } from "~/actions/index";
import "./ActionButton.css";
const ActionButton = () => {
const { execute, isLoading, } = useSafeAction(createTitle, {
onSuccess: (data) => {
window.alert(`Success: ${JSON.stringify(data, null, 2)}`);
},
onError: (error) => {
window.alert("Error: " + error);
}
});

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

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

export default ActionButton
3 changes: 3 additions & 0 deletions examples/with-server-actions/src/entry-client.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { mount, StartClient } from "@solidjs/start/client";

mount(() => <StartClient />, document.getElementById("app"));
20 changes: 20 additions & 0 deletions examples/with-server-actions/src/entry-server.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { createHandler, StartServer } from "@solidjs/start/server";

export default createHandler(() => (
<StartServer
document={({ assets, children, scripts }) => (
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" href="/favicon.ico" />
{assets}
</head>
<body>
<div id="app">{children}</div>
{scripts}
</body>
</html>
)}
/>
));
1 change: 1 addition & 0 deletions examples/with-server-actions/src/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="@solidjs/start/env" />
7 changes: 7 additions & 0 deletions examples/with-server-actions/src/lib/mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const simulateDatabaseCall = (data: { title: string }): Promise<{ title: string }> => {
return new Promise((resolve) => {
setTimeout(() => {
resolve({ title: data.title });
}, 1000);
});
};
8 changes: 8 additions & 0 deletions examples/with-server-actions/src/lib/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { z } from "zod";

export const CreateTitle = z.object({
title: z.string({
required_error: "Title is required",
invalid_type_error: "Title is required"
}).min(3, { message: 'Title too short' }),
});
8 changes: 8 additions & 0 deletions examples/with-server-actions/src/lib/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { z } from "zod";


import { ActionState } from "solid-safe-action";
import { CreateTitle } from "./schema";

export type InputType = z.infer<typeof CreateTitle>;
export type ReturnType = ActionState<InputType, { title: string; }>;
19 changes: 19 additions & 0 deletions examples/with-server-actions/src/routes/[...404].tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Title } from "@solidjs/meta";
import { HttpStatusCode } from "@solidjs/start";

export default function NotFound() {
return (
<main>
<Title>Not Found</Title>
<HttpStatusCode code={404} />
<h1>Page Not Found</h1>
<p>
Visit{" "}
<a href="https://start.solidjs.com" target="_blank">
start.solidjs.com
</a>{" "}
to learn how to build SolidStart apps.
</p>
</main>
);
}
10 changes: 10 additions & 0 deletions examples/with-server-actions/src/routes/about.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Title } from "@solidjs/meta";

export default function Home() {
return (
<main>
<Title>About</Title>
<h1>About</h1>
</main>
);
}
20 changes: 20 additions & 0 deletions examples/with-server-actions/src/routes/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Title } from "@solidjs/meta";
import ActionButton from "~/components/ActionButton";


export default function Home() {
return (
<main>
<Title>Hello World</Title>
<h1>Hello world!</h1>
<ActionButton/>
<p>
Visit{" "}
<a href="https://start.solidjs.com" target="_blank">
start.solidjs.com
</a>{" "}
to learn how to build SolidStart apps.
</p>
</main>
);
}
19 changes: 19 additions & 0 deletions examples/with-server-actions/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"jsx": "preserve",
"jsxImportSource": "solid-js",
"allowJs": true,
"strict": true,
"noEmit": true,
"types": ["vinxi/client"],
"isolatedModules": true,
"paths": {
"~/*": ["./src/*"]
}
}
}
3 changes: 3 additions & 0 deletions examples/with-server-actions/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { defineConfig } from "@solidjs/start/config";

export default defineConfig({});