{"id":19606328,"url":"https://github.com/kalmonj/zod-server-actions","last_synced_at":"2025-04-27T19:32:59.472Z","repository":{"id":247174124,"uuid":"822876774","full_name":"KalmonJ/zod-server-actions","owner":"KalmonJ","description":"Simple utility library to create server actions in Next.js","archived":false,"fork":false,"pushed_at":"2025-01-30T01:21:06.000Z","size":433,"stargazers_count":6,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-15T14:50:51.647Z","etag":null,"topics":["nextjs14","reactjs","server","zod"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/zod-server-actions","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/KalmonJ.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-07-02T02:25:57.000Z","updated_at":"2025-01-30T01:21:09.000Z","dependencies_parsed_at":"2024-08-23T15:09:07.078Z","dependency_job_id":"00825fb2-d15d-40b5-bb49-042f49205bd7","html_url":"https://github.com/KalmonJ/zod-server-actions","commit_stats":null,"previous_names":["kalmonj/zod-server-actions"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KalmonJ%2Fzod-server-actions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KalmonJ%2Fzod-server-actions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KalmonJ%2Fzod-server-actions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KalmonJ%2Fzod-server-actions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/KalmonJ","download_url":"https://codeload.github.com/KalmonJ/zod-server-actions/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251196373,"owners_count":21550943,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["nextjs14","reactjs","server","zod"],"created_at":"2024-11-11T10:04:44.199Z","updated_at":"2025-04-27T19:32:59.197Z","avatar_url":"https://github.com/KalmonJ.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ActionHandler Library\n\nThe `ActionHandler` library provides a typesafe and robust way to manage and execute asynchronous actions with input validation, output validation, and retry logic. This library leverages `zod` for schema validation and includes functionality to create and handle actions in a structured manner.\n\n## Features\n\n- **Input and Output Validation**: Define schemas for input and output using `zod` for type-safe validation.\n- **Retry Logic**: Automatically retry actions on failure with configurable attempts and delays.\n- **Context Management**: Optionally provide context for actions using a factory function.\n- **Flexible Action Handling**: Create handlers for actions with specified input and output types.\n\n## Installation\n\nTo use the `ActionHandler` library, first install the required dependencies:\n\n```bash\nnpm install zod\n```\n\n## Usage\n\n### Creating an Action Handler\n\nUse the `createActionHandler` function to initialize a new action handler with configuration options.\n\n```typescript\nimport { createActionHandler } from \"zod-server-actions\";\n```\n\n### Example\n\nHere's a step-by-step guide to creating an action handler:\n\n1. **Define Schemas**\n\n   Define the input and output schemas using `zod`.\n\n   ```typescript\n   import { z } from \"zod\";\n\n   const InputSchema = z.object({\n     name: z.string(),\n     age: z.number().min(0),\n   });\n\n   const OutputSchema = z.object({\n     greeting: z.string(),\n   });\n   ```\n\n2. **Create Action Handler**\n\n   Use `createActionHandler` to initialize your action handler.\n\n   ```typescript\n   const actionHandler = createActionHandler({\n     maximumAttempts: 3,\n     delay: 1000,\n     contextFn: async () =\u003e {\n       return { user: \"contextData\" }; // Example context data\n     },\n   });\n   ```\n\n3. **Define the Handler Function**\n\n   Create a handler function that will process the input and return the output.\n\n   ```typescript\n   const action = handler\n     .input(InputSchema)\n     .output(OutputSchema)\n     .handler(async () =\u003e {\n       // create user and return data\n     });\n   ```\n\n4. **Execute the Action**\n\n   Call the action with the appropriate input.\n\n   ```typescript\n   const result = await action({ name: \"Alice\", age: 30 });\n   console.log(result);\n   ```\n\n## API\n\n### `createActionHandler(config)`\n\nCreates a new `ActionHandler` instance with the specified configuration.\n\n**Parameters:**\n\n- `config` (object): Configuration for the action handler.\n  - `maximumAttempts` (number): Number of retry attempts (optional, default: 0).\n  - `delay` (number): Delay between retries in milliseconds (optional, default: 0).\n  - `contextFn` (function): Asynchronous function that returns context data (optional).\n\n**Returns:**\n\n- `ActionHandler`: An instance of `ActionHandler`.\n\n### `ActionHandler`\n\n**Methods:**\n\n- `input\u003cS extends ZodTypeAny\u003e(schema: S)`: Defines the input schema for the action handler.\n- `output\u003cS extends ZodTypeAny\u003e(schema: S)`: Defines the output schema for the action handler.\n- `retry({ maximumAttempts, delay }: RetryProps)`: Configures retry logic with maximum attempts and delay.\n- `handler\u003cR\u003e(callback: HandlerFn\u003cT, R, TContext\u003e)`: Registers the handler function to process the action.\n\n### `ActionHandler Properties`\n\n- `input`: Input schema.\n- `output`: Output schema.\n- `maximumAttempts`: Number of retry attempts.\n- `delay`: Delay between retries.\n- `contextFn`: Context factory function.\n\n## Examples\n\n### Basic Example\n\n```typescript\nimport { createActionHandler } from \"zod-server-actions\";\nimport { z } from \"zod\";\n\nconst InputSchema = z.object({\n  name: z.string(),\n  age: z.number().min(0),\n});\n\nconst OutputSchema = z.object({\n  greeting: z.string(),\n});\n\nconst actionHandler = createActionHandler({\n  maximumAttempts: 3,\n  delay: 1000,\n});\n\nconst handler = async (input, context) =\u003e {\n  return {\n    greeting: `Hello, ${input.name}!`,\n  };\n};\n\nconst action = actionHandler\n  .input(InputSchema)\n  .output(OutputSchema)\n  .handler(handler);\n\n(async () =\u003e {\n  const result = await action({ name: \"Bob\", age: 25 });\n  console.log(result);\n})();\n```\n\n## Contributing\n\nFeel free to submit issues or pull requests to improve the library. Contributions are welcome!\n\n## Contact\n\nFor questions or support, please contact kalmonkk69@gmail.com.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkalmonj%2Fzod-server-actions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkalmonj%2Fzod-server-actions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkalmonj%2Fzod-server-actions/lists"}