Skip to content

Commit

Permalink
refactor: upgrade TypeSchema to v0.13 (#81)
Browse files Browse the repository at this point in the history
This commit upgrades TypeSchema from v0.12 to v0.13. v0.13 is a major refactor of the library, which switched to a modular design. From now on, next-safe-action will use @typeschema/main to just include the common adapter logic without actually bundling adapters. This keeps the bundle size small, while allowing users to choose the specific validation libraries (and adapters) they want to use.
  • Loading branch information
TheEdoRan authored Mar 11, 2024
1 parent da5284e commit 9238190
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 99 deletions.
212 changes: 137 additions & 75 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions packages/example-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@
"license": "MIT",
"author": "Edoardo Ranghieri",
"dependencies": {
"lucide-react": "^0.354.0",
"@hookform/resolvers": "^3.3.4",
"@typeschema/valibot": "^0.13.4",
"@typeschema/zod": "^0.13.3",
"lucide-react": "^0.354.0",
"next": "14.1.3",
"next-safe-action": "file:../next-safe-action",
"react": "18.2.0",
"react-dom": "18.2.0",
"valibot": "^0.30.0",
"react-hook-form": "^7.51.0",
"valibot": "^0.30.0",
"zod": "^3.22.4",
"zod-form-data": "^2.0.2"
},
Expand Down
3 changes: 2 additions & 1 deletion packages/next-safe-action/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"devDependencies": {
"@types/node": "^20.11.25",
"@types/react": "^18.2.64",
"@typeschema/zod": "^0.13.3",
"@typescript-eslint/eslint-plugin": "^7.1.1",
"@typescript-eslint/parser": "^7.1.1",
"eslint": "^8.57.0",
Expand All @@ -73,6 +74,6 @@
"url": "https://github.com/TheEdoRan/next-safe-action.git"
},
"dependencies": {
"@decs/typeschema": "^0.12.2"
"@typeschema/main": "^0.13.7"
}
}
2 changes: 1 addition & 1 deletion packages/next-safe-action/src/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use client";

import type { InferIn, Schema } from "@decs/typeschema";
import type { InferIn, Schema } from "@typeschema/main";
import { isNotFoundError } from "next/dist/client/components/not-found.js";
import { isRedirectError } from "next/dist/client/components/redirect.js";
import * as React from "react";
Expand Down
8 changes: 4 additions & 4 deletions packages/next-safe-action/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Infer, InferIn, Schema } from "@decs/typeschema";
import { wrap } from "@decs/typeschema";
import type { Infer, InferIn, Schema } from "@typeschema/main";
import { validate } from "@typeschema/main";
import { isNotFoundError } from "next/dist/client/components/not-found.js";
import { isRedirectError } from "next/dist/client/components/redirect.js";
import type { ErrorList, Extend, MaybePromise, SchemaErrors } from "./utils";
Expand Down Expand Up @@ -84,10 +84,10 @@ export const createSafeActionClient = <Context, MiddlewareData>(
// all the invalid fields provided.
return async (clientInput) => {
try {
const parsedInput = await wrap(schema).validate(clientInput);
const parsedInput = await validate(schema, clientInput);

// If schema validation fails.
if ("issues" in parsedInput) {
if (!parsedInput.success) {
return {
validationErrors: buildValidationErrors<S>(parsedInput.issues),
};
Expand Down
3 changes: 2 additions & 1 deletion packages/next-safe-action/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Schema, ValidationIssue } from "@decs/typeschema";
import type { ValidationIssue } from "@typeschema/core";
import type { Schema } from "@typeschema/main";
import type { ValidationErrors } from ".";

export const isError = (error: unknown): error is Error => error instanceof Error;
Expand Down
20 changes: 5 additions & 15 deletions website/docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,27 @@ This is the documentation for the current version of the library (7.x.x).
:::

:::info Requirements
- v4: Next.js >= 13.4.2, v5: Next.js >= 14.0.0
- Next.js >= 14.0.0
- TypeScript >= 5.0.0
- pre-v6: Zod >= 3.0.0, from v6: a validation library supported by [TypeSchema](https://typeschema.com/#coverage)
- a validation library supported by [TypeSchema](https://typeschema.com/#coverage)
:::

**next-safe-action** provides a typesafe Server Actions implementation for Next.js App Router.

## Validation libraries support

We will use Zod as our validation library in this documentation, but since version 6 of next-safe-action, you can use your validation library of choice, or even multiple and custom ones at the same time, thanks to the **TypeSchema** library. You can find supported libraries [here](https://typeschema.com/#coverage).
We will use Zod as our validation library in this documentation, but since version 6 of next-safe-action, you can use your validation library of choice, or even multiple and custom ones at the same time, thanks to the **TypeSchema** library. Note that we also need to install the related TypeSchema adapter for our validation library of choice. You can find supported libraries and related adapters [here](https://typeschema.com/#coverage).

## Installation

For Next.js >= 14, use the following command:
For Next.js >= 14, assuming you want to use Zod as your validation library, use the following command:

```bash npm2yarn
npm i next-safe-action
```

For Next.js 13, use the following command:

```bash npm2yarn
npm i next-safe-action@v4 zod
npm i next-safe-action zod @typeschema/zod
```

## Usage

:::note
If you're still using Next.js 13 with next-safe-action v4, you need to enable `serverActions` flag under the `experimental` object in next.config.js file. Find out more [here](/docs/migrations/v4-to-v5).
:::

### 1. Instantiate a new client

You can create a new client with the following code:
Expand Down

0 comments on commit 9238190

Please sign in to comment.