Skip to content

Commit

Permalink
🚀 dev Add generator: next actions
Browse files Browse the repository at this point in the history
  • Loading branch information
steeeee0223 committed Jan 24, 2024
1 parent 9d9bea2 commit 4c712bb
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"db:push": "pnpm -F db push",
"db:studio": "pnpm -F db studio",
"dev": "turbo dev --parallel",
"gen:action": "turbo gen next-action",
"gen:package": "turbo gen init",
"gen:story": "turbo gen story",
"gen:ui": "turbo gen component",
Expand Down
Empty file.
2 changes: 2 additions & 0 deletions packages/validators/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ export const CreatePostSchema = z.object({
title: z.string().min(1),
content: z.string().min(1),
});

export * from "./actions";
3 changes: 2 additions & 1 deletion turbo/generators/config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { PlopTypes } from "@turbo/gen";

import { component, init, story } from "./generators";
import { component, init, nextAction, story } from "./generators";

export default function generator(plop: PlopTypes.NodePlopAPI): void {
plop.setGenerator("init", init);
plop.setGenerator("component", component);
plop.setGenerator("next-action", nextAction);
plop.setGenerator("story", story);
}
50 changes: 50 additions & 0 deletions turbo/generators/generators/action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import type { PlopTypes } from "@turbo/gen";

export const nextAction = {
description: "Nextjs Server Action",
prompts: [
{
type: "input",
name: "app",
message: "📁 Which app do you want to add to?",
default: "nextjs",
validate: (value) => !!value || `App name is required`,
},
{
type: "input",
name: "name",
message: "🐫 Action Name:",
default: "create action",
validate: (value) => !!value || `Action name is required`,
},
],
actions: [
{
type: "add",
path: "apps/{{app}}/actions/index.ts",
skipIfExists: true,
},
{
type: "add",
path: "apps/{{app}}/actions/{{dashCase name}}.ts",
templateFile: "templates/action/index.ts.hbs",
abortOnFail: true,
},
{
type: "append",
path: "apps/{{app}}/actions/index.ts",
template: 'export * from "./{{dashCase name}}"',
},
{
type: "add",
path: "packages/validators/src/actions/{{dashCase name}}.ts",
templateFile: "templates/action/schema.ts.hbs",
abortOnFail: true,
},
{
type: "append",
path: "packages/validators/src/actions/index.ts",
template: 'export * from "./{{dashCase name}}"',
},
],
} satisfies PlopTypes.PlopGeneratorConfig;
1 change: 1 addition & 0 deletions turbo/generators/generators/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./action";
export * from "./component";
export * from "./init";
export * from "./story";
26 changes: 26 additions & 0 deletions turbo/generators/templates/action/index.ts.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"use server"

import { worxpace } from "@acme/prisma";
import { type ActionHandler, createSafeAction } from "@acme/ui/lib";
import { {{pascalCase name}}, type {{pascalCase name}}Input } from "@acme/validators";

import { UnauthorizedError, fetchClient } from "~/lib";

const handler: ActionHandler<{{pascalCase name}}Input, any> = async (data) => {
let result;
const {} = data;

try {
const { clientId } = fetchClient();
} catch (error) {
if (error instanceof UnauthorizedError)
return { error: "Unauthorized" };
console.log(`ERROR`, error);
return { error: "Failed to {{lowerCase name}}." };
}

return { data: result };
};

export const {{camelCase name}} = createSafeAction({{pascalCase name}}, handler);

5 changes: 5 additions & 0 deletions turbo/generators/templates/action/schema.ts.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { z } from "zod";

export const {{pascalCase name}} = z.object({});

export type {{pascalCase name}}Input = z.infer<typeof {{pascalCase name}}>;

0 comments on commit 4c712bb

Please sign in to comment.