rpc4next 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +38 -7
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -40,7 +40,6 @@ export async function GET(
40
40
  segmentData: { params: Promise<{ id: string }> }
41
41
  ) {
42
42
  const { id } = await segmentData.params;
43
- // RPCとしてresponseの戻り値の推論が機能するのは、対象となる `route.ts` の HTTPメソッドハンドラ内で`NextResponse.json()` をしている物のみになります
44
43
  return NextResponse.json({ name: `User ${id}` });
45
44
  }
46
45
  ```
@@ -51,8 +50,7 @@ export async function GET(
51
50
 
52
51
  ### 3. Generate Type Definitions with CLI
53
52
 
54
- CLI を利用して、Next.js のルート構造から型安全な RPC クライアントの定義を自動生成します。
55
- 以下のコマンドを実行すると、`route.ts` と `page.tsx` ファイルの両方を走査し、型定義ファイル(outputPathに指定したファイル)が生成されます。
53
+ CLI を利用して、Next.js のルート構造から型安全な RPC クライアントの定義を自動生成します。
56
54
 
57
55
  ```bash
58
56
  npx rpc4next <baseDir> <outputPath>
@@ -64,14 +62,14 @@ npx rpc4next <baseDir> <outputPath>
64
62
  #### オプション
65
63
 
66
64
  - **ウォッチモード**
67
- ファイル変更を検知して自動的に再生成する場合は `--watch` オプションを付けます。
65
+ ファイル変更を検知して自動的に再生成する場合は `--watch` or `-w` オプションを付けます。
68
66
 
69
67
  ```bash
70
68
  npx rpc4next <baseDir> <outputPath> --watch
71
69
  ```
72
70
 
73
71
  - **パラメータ型ファイルの生成**
74
- 各ルートに対して個別のパラメータ型定義ファイルを生成する場合は、`--generate-params-types` オプションにファイル名を指定します。
72
+ 各ルートに対して個別のパラメータ型定義ファイルを生成する場合は、`--params-file` or `-p` オプションにファイル名を指定します。
75
73
 
76
74
  ```bash
77
75
  npx rpc4next <baseDir> <outputPath> --generate-params-types <paramsFileName>
@@ -115,6 +113,41 @@ export default async function Page() {
115
113
 
116
114
  ---
117
115
 
116
+ ## ✅ さらに型安全にしたい場合 `honolike` + `createRouteHandler` による Next.js の型安全強化
117
+
118
+ さらに `honolike` をベースとした `createRouteHandler()` を組み合わせることで、
119
+
120
+ ### 📌 主なメリット
121
+
122
+ 1. **レスポンス型安全**
123
+
124
+ - ステータス、Content-Type、Body がすべて型で保証される
125
+ - クライアントは受け取るレスポンス型を完全に推論可能
126
+
127
+ 2. **クライアント側補完強化**
128
+
129
+ - `status`, `content-type`, `json()`, `text()` などがステータスに応じて適切に補完される
130
+
131
+ 3. **サーバー側 params / query も型安全**
132
+ - `routeHandlerFactory()` を使えば、`params`, `query` も型推論可能
133
+
134
+ ---
135
+
136
+ ### ✅ 基本的な使い方
137
+
138
+ ```ts
139
+ const createRouteHandler = routeHandlerFactory((err, rc) =>
140
+ rc.text("error", { status: 400 })
141
+ );
142
+
143
+ const { POST } = createRouteHandler().post(
144
+ async (rc) => rc.json("json response"),
145
+ async (rc) => rc.text("plain text")
146
+ );
147
+ ```
148
+
149
+ これだけで、POST リクエストの返り値が、responseの内容(json,textなど)、status,contenttypeが型付けされるようになります。
150
+
118
151
  ## 🚧 Requirements
119
152
 
120
153
  - Next.js 14+ (App Router 使用)
@@ -125,5 +158,3 @@ export default async function Page() {
125
158
  ## 💼 License
126
159
 
127
160
  MIT
128
-
129
- ---
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rpc4next",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "Inspired by Hono RPC and Pathpida, rpc4next brings a lightweight and intuitive RPC solution to Next.js, making server-client communication seamless",
5
5
  "author": "watanabe-1",
6
6
  "license": "MIT",