next-zero-rpc 0.1.3 → 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 +1 -1
- package/package.json +1 -1
- package/templates/apiClient.ts +10 -14
package/README.md
CHANGED
|
@@ -388,7 +388,7 @@ Type-safe fetch wrapper that returns Go-style `[data, error]` tuples.
|
|
|
388
388
|
- `Path` — validated against `KnownRoutes` at compile time
|
|
389
389
|
- `Method` — restricted to only the HTTP methods exported by the matched route
|
|
390
390
|
|
|
391
|
-
**Returns:** `Promise<[SuccessType, null] | [null, ErrorType |
|
|
391
|
+
**Returns:** `Promise<[SuccessType, null] | [null, ErrorType | ApiErrorPayload<"system:unknown-error">]>`
|
|
392
392
|
|
|
393
393
|
- **Success:** `[data, null]` where `data` is inferred from the route handler's `createApiSuccess` calls
|
|
394
394
|
- **Error:** `[null, error]` where `error.code` is narrowed to only the error codes used in that route's `createApiError` calls, plus `"system:unknown-error"` as a fallback
|
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
|
|