Name: {{ user?.name }}
Email: {{ user?.email }}
Role: {{ user?.role }}
Loading...
{{ greeting }}
``` -------------------------------- ### Define Complex Procedure with Zod Objects Source: https://github.com/andresberrios/nuxt-procedures/blob/main/README.md Defines a complex procedure 'users/create' using Zod objects for input and output validation. It demonstrates accessing Nuxt event context and using a hypothetical `useDB` utility. ```typescript import { z } from "zod"; export default defineProcedure({ input: z.object({ name: z.string(), email: z.string().email(), role: z.enum(["admin", "user"]).default("user"), }), output: z.object({ id: z.string(), name: z.string(), email: z.string(), }), handler: async ({ input, event }) => { const headers = getRequestHeaders(event); console.log(headers); const db = await useDB(event); const newUser = await db.user.create({ data: input, }); return newUser; }, }); ``` -------------------------------- ### Call Nuxt Procedures API Client in Vue Component Source: https://context7.com/andresberrios/nuxt-procedures/llms.txt Demonstrates how to call server-side procedures directly from a Vue component using the generated `apiClient`. It covers calling simple and complex procedures, including nested ones, and shows how to handle potential validation errors. ```typescript // In a Vue component script async function handleSubmit() { try { // Simple procedure call with string input const greeting = await apiClient.hello.call("World"); console.log(greeting); // "Hello, World!" // Complex procedure call with object input const newUser = await apiClient.users.create.call({ name: "John Doe", email: "john@example.com", age: 30, role: "user", metadata: { registrationSource: "web", referralCode: "REF123", }, }); console.log(newUser); // { // id: "550e8400-e29b-41d4-a716-446655440000", // name: "John Doe", // email: "john@example.com", // role: "user", // createdAt: Date object // } // Nested procedure calls (mirrors folder structure) const result = await apiClient.foo.bar.call("input data"); } catch (error) { // Validation errors throw with detailed Zod error messages if (error.statusCode === 400) { console.error("Validation failed:", error.data); } } } ``` -------------------------------- ### Define Server Procedure with Complex Zod Schemas in Nuxt Source: https://context7.com/andresberrios/nuxt-procedures/llms.txt Defines a server-side procedure using complex Zod object schemas for input and output, supporting nested objects, enums, optional fields, and defaults. This is useful for handling form submissions and database operations. The handler function receives validated input and can access the database via the event context. ```typescript // server/procedures/users/create.ts import { z } from "zod"; export default defineProcedure({ input: z.object({ name: z.string().min(2).max(50), email: z.string().email(), age: z.number().int().positive().optional(), role: z.enum(["admin", "user", "moderator"]) .default("user"), metadata: z.object({ registrationSource: z.string(), referralCode: z.string().optional(), }), }), output: z.object({ id: z.string().uuid(), name: z.string(), email: z.string(), role: z.enum(["admin", "user", "moderator"]), createdAt: z.date(), }), handler: async ({ input, event }) => { // Access database through event context const db = await useDB(event); // Input is fully typed and validated const newUser = await db.user.create({ data: { name: input.name, email: input.email, age: input.age, role: input.role, }, }); // Return value is validated against output schema return { id: newUser.id, name: newUser.name, email: newUser.email, role: newUser.role, createdAt: newUser.createdAt, }; }, }); ``` -------------------------------- ### Handle Complex Data Types with SuperJSON Serialization in TypeScript Source: https://context7.com/andresberrios/nuxt-procedures/llms.txt Demonstrates how Nuxt Procedures automatically serializes and deserializes complex data types like Date, Set, and Map using SuperJSON. This eliminates the need for manual conversion between client and server, ensuring type safety and seamless data handling. ```typescript // server/procedures/complexData.ts import { z } from "zod"; export default defineProcedure({ input: z.object({ date: z.date(), tags: z.set(z.string()), preferences: z.map(z.string(), z.any()), }), output: z.object({ processedAt: z.date(), tagCount: z.number(), settings: z.map(z.string(), z.any()), }), handler: async ({ input }) => { return { processedAt: new Date(), tagCount: input.tags.size, settings: new Map([ ["theme", "dark"], ["notifications", true], ]), }; }, }); // Client usage - complex types work seamlessly const result = await apiClient.complexData.call({ date: new Date("2025-01-15"), tags: new Set(["typescript", "nuxt", "vue"]), preferences: new Map([["language", "en"], ["timezone", "UTC"]]), }); console.log(result.processedAt instanceof Date); // true console.log(result.settings instanceof Map); // true ``` -------------------------------- ### Use `call` for Direct API Calls Source: https://github.com/andresberrios/nuxt-procedures/blob/main/README.md Illustrates using the `apiClient.procedureName.call` method for direct API calls without state management by Nuxt. This is useful for actions triggered by user events like form submissions. ```typescript // Calling the simple procedure const greeting = await apiClient.hello.call("World"); // greeting is "Hello, World!" // Calling the complex procedure const newUser = await apiClient.users.create.call({ name: "Andres", email: "andres@example.com", }); // newUser is { id: '...', name: 'Andres', email: 'andres@example.com' } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.