next-zero-rpc 0.1.4 → 0.1.6
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 +25 -3
- package/package.json +1 -1
- package/templates/apiClient.ts +10 -14
package/README.md
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
Type-safe fetch for Next.js — zero runtime, zero config, zero dependencies.
|
|
4
4
|
|
|
5
|
+
[](https://codesandbox.io/p/sandbox/github/caocchinh/next-zero-rpc/tree/main/examples/minimal)
|
|
6
|
+
|
|
5
7
|
```bash
|
|
6
8
|
npx next-zero-rpc init
|
|
7
9
|
```
|
|
@@ -507,6 +509,24 @@ Since you own all the source files, you can customize anything:
|
|
|
507
509
|
- **Change the output path** — Update `REGISTRY_FILE` in `update-api-registry.mjs`
|
|
508
510
|
- **Override the generator** — The `withApiRegistry` plugin and `updateApiRegistry()` function are fully yours to modify
|
|
509
511
|
|
|
512
|
+
## Examples
|
|
513
|
+
|
|
514
|
+
### Try it online
|
|
515
|
+
|
|
516
|
+
[](https://codesandbox.io/p/sandbox/github/caocchinh/next-zero-rpc/tree/main/examples/minimal)
|
|
517
|
+
|
|
518
|
+
Opens [`examples/minimal`](https://github.com/caocchinh/next-zero-rpc/tree/main/examples/minimal) — a self-contained Next.js app with a working `apiFetch` client and two example API routes.
|
|
519
|
+
|
|
520
|
+
### Run locally
|
|
521
|
+
|
|
522
|
+
```bash
|
|
523
|
+
# Clone just the example folder (no git history)
|
|
524
|
+
npx degit caocchinh/next-zero-rpc/examples/minimal my-app
|
|
525
|
+
cd my-app
|
|
526
|
+
npm install
|
|
527
|
+
npm run dev
|
|
528
|
+
```
|
|
529
|
+
|
|
510
530
|
## CLI Usage
|
|
511
531
|
|
|
512
532
|
```bash
|
|
@@ -518,9 +538,11 @@ npx next-zero-rpc --help # Show help
|
|
|
518
538
|
|
|
519
539
|
## Requirements
|
|
520
540
|
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
541
|
+
| Requirement | Minimum Version | Reason |
|
|
542
|
+
| ----------- | --------------- | ------ |
|
|
543
|
+
| Next.js | **14.0** | App Router (stable since 14.0) |
|
|
544
|
+
| TypeScript | **4.9** | `satisfies` keyword used in `responses.ts` |
|
|
545
|
+
| Node.js | **18** | Native `fetch` API required by `apiFetch` |
|
|
524
546
|
|
|
525
547
|
## License
|
|
526
548
|
|
package/package.json
CHANGED
package/templates/apiClient.ts
CHANGED
|
@@ -7,33 +7,29 @@ type HttpMethod = "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "HEAD" | "OPTION
|
|
|
7
7
|
// ─── Type Inference ─────────────────────────────────────────────────────────
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
|
-
*
|
|
11
|
-
* Filters out ApiErrorPayload to only return the success payload.
|
|
10
|
+
* Extracts the inner payload type U from a route handler returning NextResponse<U>.
|
|
12
11
|
*/
|
|
13
|
-
type
|
|
12
|
+
type UnwrapNextResponse<T> = T extends (...args: never[]) => infer R
|
|
14
13
|
? Extract<Awaited<R>, NextResponse<unknown>> extends NextResponse<infer U>
|
|
15
|
-
?
|
|
16
|
-
: never
|
|
17
|
-
: never;
|
|
18
|
-
|
|
19
|
-
type InferErrorApiResponse<T, E = never> = T extends (...args: never[]) => infer R
|
|
20
|
-
? Extract<Awaited<R>, NextResponse<unknown>> extends NextResponse<infer U>
|
|
21
|
-
? Extract<U, E>
|
|
14
|
+
? U
|
|
22
15
|
: never
|
|
23
16
|
: never;
|
|
24
17
|
|
|
25
18
|
type ResolveRoute<Path extends string> =
|
|
26
19
|
FindMatchingRoute<Path> extends keyof KnownRoutes ? FindMatchingRoute<Path> : never;
|
|
27
20
|
|
|
21
|
+
type RouteHandler<Path extends string, M extends HttpMethod> =
|
|
22
|
+
KnownRoutes[ResolveRoute<Path>][M & keyof KnownRoutes[ResolveRoute<Path>]];
|
|
23
|
+
|
|
28
24
|
type RouteMethods<Path extends string> = Extract<keyof KnownRoutes[ResolveRoute<Path>], HttpMethod>;
|
|
29
25
|
|
|
30
|
-
type RouteSuccessResult<Path extends string, M extends HttpMethod> =
|
|
31
|
-
|
|
26
|
+
type RouteSuccessResult<Path extends string, M extends HttpMethod> = Exclude<
|
|
27
|
+
UnwrapNextResponse<RouteHandler<Path, M>>,
|
|
32
28
|
ApiErrorPayload<ErrorCode>
|
|
33
29
|
>;
|
|
34
30
|
|
|
35
|
-
type RouteErrorResult<Path extends string, M extends HttpMethod> =
|
|
36
|
-
|
|
31
|
+
type RouteErrorResult<Path extends string, M extends HttpMethod> = Extract<
|
|
32
|
+
UnwrapNextResponse<RouteHandler<Path, M>>,
|
|
37
33
|
ApiErrorPayload<ErrorCode>
|
|
38
34
|
>;
|
|
39
35
|
|