rpc4next 0.1.4 → 0.1.5
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.
- package/README.md +18 -6
- package/dist/cli/{route-scanner.js → core/route-scanner.js} +1 -1
- package/dist/cli/{scan-utils.d.ts → core/scan-utils.d.ts} +1 -1
- package/dist/cli/core/types.d.ts +2 -0
- package/dist/cli/core/types.js +2 -0
- package/dist/cli/generator.js +1 -1
- package/dist/cli/types.d.ts +0 -2
- package/dist/cli/watcher.js +1 -1
- package/package.json +1 -1
- /package/dist/cli/{cache.d.ts → core/cache.d.ts} +0 -0
- /package/dist/cli/{cache.js → core/cache.js} +0 -0
- /package/dist/cli/{constants.d.ts → core/constants.d.ts} +0 -0
- /package/dist/cli/{constants.js → core/constants.js} +0 -0
- /package/dist/cli/{generate-path-structure.d.ts → core/generate-path-structure.d.ts} +0 -0
- /package/dist/cli/{generate-path-structure.js → core/generate-path-structure.js} +0 -0
- /package/dist/cli/{path-utils.d.ts → core/path-utils.d.ts} +0 -0
- /package/dist/cli/{path-utils.js → core/path-utils.js} +0 -0
- /package/dist/cli/{route-scanner.d.ts → core/route-scanner.d.ts} +0 -0
- /package/dist/cli/{scan-utils.js → core/scan-utils.js} +0 -0
- /package/dist/cli/{type-utils.d.ts → core/type-utils.d.ts} +0 -0
- /package/dist/cli/{type-utils.js → core/type-utils.js} +0 -0
package/README.md
CHANGED
|
@@ -9,7 +9,7 @@ Inspired by Hono RPC and Pathpida, **rpc4next** automatically generates a type-s
|
|
|
9
9
|
## ✨ Features
|
|
10
10
|
|
|
11
11
|
- ✅ 既存の `app/**/route.ts` および `app/**/page.tsx` を活用するため、新たなハンドラファイルの作成は不要
|
|
12
|
-
- ✅
|
|
12
|
+
- ✅ ルート、パラメータ、クエリパラメータ、 リクエストボディ、レスポンスの型安全なクライアント生成
|
|
13
13
|
- ✅ 最小限のセットアップで、カスタムサーバー不要
|
|
14
14
|
- ✅ 動的ルート(`[id]`、`[...slug]` など)に対応
|
|
15
15
|
- ✅ CLI による自動クライアント用型定義生成
|
|
@@ -30,20 +30,30 @@ npm install rpc4next
|
|
|
30
30
|
### 2. Define API Routes in Next.js
|
|
31
31
|
|
|
32
32
|
Next.js プロジェクト内の既存の `app/**/route.ts` と `app/**/page.tsx` ファイルをそのまま利用できます。
|
|
33
|
+
さらに、クエリパラメータ(searchParams)の型安全性を有効にするには、対象のファイル内で `Query` または `OptionalQuery` 型を定義し、`export` してください。
|
|
33
34
|
|
|
34
35
|
```ts
|
|
35
36
|
// app/api/user/[id]/route.ts
|
|
36
37
|
import { NextRequest, NextResponse } from "next/server";
|
|
37
38
|
|
|
39
|
+
// searchParams用の型定義
|
|
40
|
+
export type Query = {
|
|
41
|
+
q: string; // 必須
|
|
42
|
+
page?: number; // 任意
|
|
43
|
+
};
|
|
44
|
+
|
|
38
45
|
export async function GET(
|
|
39
46
|
req: NextRequest,
|
|
40
47
|
segmentData: { params: Promise<{ id: string }> }
|
|
41
48
|
) {
|
|
42
49
|
const { id } = await segmentData.params;
|
|
43
|
-
|
|
50
|
+
const q = req.nextUrl.searchParams.get("q");
|
|
51
|
+
return NextResponse.json({ id, q });
|
|
44
52
|
}
|
|
45
53
|
```
|
|
46
54
|
|
|
55
|
+
🚩 Query or OptionalQuery 型を export することで、searchParams の型も自動的にクライアントに反映されます。
|
|
56
|
+
|
|
47
57
|
- **RPCとしてresponseの戻り値の推論が機能するのは、対象となる `route.ts` の HTTPメソッドハンドラ内で`NextResponse.json()` をしている物のみになります**
|
|
48
58
|
|
|
49
59
|
---
|
|
@@ -102,14 +112,16 @@ export const rpc = createClient<PathStructure>();
|
|
|
102
112
|
import { rpc } from "@/lib/rpcClient";
|
|
103
113
|
|
|
104
114
|
export default async function Page() {
|
|
105
|
-
const res = await rpc.api.user._id("123").$get(
|
|
115
|
+
const res = await rpc.api.user._id("123").$get({
|
|
116
|
+
query: { q: "hello", page: 1 },
|
|
117
|
+
});
|
|
106
118
|
const json = await res.json();
|
|
107
|
-
return <div>{json.
|
|
119
|
+
return <div>{json.q}</div>;
|
|
108
120
|
}
|
|
109
121
|
```
|
|
110
122
|
|
|
111
123
|
- エディタの補完機能により、利用可能なエンドポイントが自動的に表示されます。
|
|
112
|
-
- リクエストの構造(params,
|
|
124
|
+
- リクエストの構造(params, query)はサーバーコードから推論され、レスポンスも型安全に扱えます。
|
|
113
125
|
|
|
114
126
|
---
|
|
115
127
|
|
|
@@ -126,7 +138,7 @@ export default async function Page() {
|
|
|
126
138
|
|
|
127
139
|
2. **クライアント側補完強化**
|
|
128
140
|
|
|
129
|
-
- `status`, `content-type`, `json()`, `text()`
|
|
141
|
+
- `status`, `content-type`, `json()`, `text()` などが適切に補完される
|
|
130
142
|
|
|
131
143
|
3. **サーバー側 params / query も型安全**
|
|
132
144
|
- `routeHandlerFactory()` を使えば、`params`, `query` も型推論可能
|
|
@@ -12,7 +12,7 @@ const cache_1 = require("./cache");
|
|
|
12
12
|
const constants_1 = require("./constants");
|
|
13
13
|
const scan_utils_1 = require("./scan-utils");
|
|
14
14
|
const type_utils_1 = require("./type-utils");
|
|
15
|
-
const constants_2 = require("
|
|
15
|
+
const constants_2 = require("../../lib/constants");
|
|
16
16
|
const endPointFileNames = new Set(constants_1.END_POINT_FILE_NAMES);
|
|
17
17
|
const hasTargetFiles = (dirPath) => {
|
|
18
18
|
// Return cached result if available
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { HttpMethod } from "
|
|
1
|
+
import { HttpMethod } from "../../lib/types";
|
|
2
2
|
export declare const createImportAlias: (type: string, key: string) => string;
|
|
3
3
|
export declare const scanFile: <T extends string | undefined>(outputFile: string, inputFile: string, findCallBack: (fileContents: string) => T, typeCallBack: (type: NonNullable<T>, importAlias: string) => string) => {
|
|
4
4
|
importName: string;
|
package/dist/cli/generator.js
CHANGED
|
@@ -5,7 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.generate = void 0;
|
|
7
7
|
const fs_1 = __importDefault(require("fs"));
|
|
8
|
-
const generate_path_structure_1 = require("./generate-path-structure");
|
|
8
|
+
const generate_path_structure_1 = require("./core/generate-path-structure");
|
|
9
9
|
const generate = ({ baseDir, outputPath, paramsFileName, logger, }) => {
|
|
10
10
|
logger.info("Generating...");
|
|
11
11
|
const { pathStructure, paramsTypes } = (0, generate_path_structure_1.generatePages)(outputPath, baseDir);
|
package/dist/cli/types.d.ts
CHANGED
package/dist/cli/watcher.js
CHANGED
|
@@ -5,7 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.setupWatcher = void 0;
|
|
7
7
|
const chokidar_1 = __importDefault(require("chokidar"));
|
|
8
|
-
const cache_1 = require("./cache");
|
|
8
|
+
const cache_1 = require("./core/cache");
|
|
9
9
|
const debounce_1 = require("./debounce");
|
|
10
10
|
const setupWatcher = (baseDir, onGenerate, logger) => {
|
|
11
11
|
logger.info(`Watching ${baseDir}...`);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rpc4next",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
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",
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|