next-zero-rpc 0.1.1 → 0.1.3
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 +3 -3
- package/package.json +2 -2
- package/templates/responses.ts +14 -10
package/README.md
CHANGED
|
@@ -369,11 +369,11 @@ HTTP_STATUS_ERROR.GATEWAY_TIMEOUT; // 504
|
|
|
369
369
|
|
|
370
370
|
| Function | Signature | Description |
|
|
371
371
|
| ------------------------- | ------------------------------------------------------------------------------ | ------------------------------------- |
|
|
372
|
-
| `createApiError<C>` | `(code: C, statusCode,
|
|
372
|
+
| `createApiError<C>` | `(code: C, statusCode, options?) → NextResponse<ApiErrorPayload<C>>` | Create a typed API error response |
|
|
373
373
|
| `createApiSuccess<T>` | `(data: T, statusCode?) → NextResponse<T>` | Create a typed API success response |
|
|
374
374
|
| `createApiSuccess` | `(undefined, NO_CONTENT) → NextResponse<undefined>` | Overload for 204 No Content |
|
|
375
375
|
| `isApiErrorPayload` | `(payload: unknown) → payload is ApiErrorPayload<ErrorCode>` | Runtime type guard for error payloads |
|
|
376
|
-
| `createServiceError` | `(code,
|
|
376
|
+
| `createServiceError` | `(code, options?) → [null, ServiceError]` | Go-style error for server actions |
|
|
377
377
|
| `createServiceSuccess<T>` | `(data?: T) → [T \| undefined, null]` | Go-style success for server actions |
|
|
378
378
|
| `assertNever` | `(value: never) → never` | Compile-time exhaustiveness guard |
|
|
379
379
|
|
|
@@ -480,7 +480,7 @@ export async function getUser(userId: string) {
|
|
|
480
480
|
const user = await db.users.find(userId);
|
|
481
481
|
|
|
482
482
|
if (!user) {
|
|
483
|
-
return createServiceError("resource:not-found",
|
|
483
|
+
return createServiceError("resource:not-found", { message: "User not found" });
|
|
484
484
|
}
|
|
485
485
|
|
|
486
486
|
return createServiceSuccess({ id: user.id, name: user.name });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "next-zero-rpc",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "Type-safe fetch for Next.js — zero runtime, zero config, zero dependencies.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"nextjs",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"type": "git",
|
|
26
26
|
"url": "https://github.com/caocchinh/next-zero-rpc"
|
|
27
27
|
},
|
|
28
|
-
"homepage": "https://next-zero-rpc.vercel.app
|
|
28
|
+
"homepage": "https://next-zero-rpc.vercel.app",
|
|
29
29
|
"bugs": {
|
|
30
30
|
"url": "https://github.com/caocchinh/next-zero-rpc/issues"
|
|
31
31
|
},
|
package/templates/responses.ts
CHANGED
|
@@ -132,19 +132,21 @@ export interface ApiErrorPayload<C extends ErrorCode> {
|
|
|
132
132
|
*
|
|
133
133
|
* @example
|
|
134
134
|
* return createApiError("auth:unauthorized", HTTP_STATUS_ERROR.UNAUTHORIZED);
|
|
135
|
-
* return createApiError("auth:unauthorized", 401,
|
|
135
|
+
* return createApiError("auth:unauthorized", 401, { message: "Custom message" });
|
|
136
136
|
*/
|
|
137
137
|
export function createApiError<C extends ErrorCode>(
|
|
138
138
|
code: C,
|
|
139
139
|
statusCode: ErrorHttpStatusCode,
|
|
140
|
-
|
|
141
|
-
|
|
140
|
+
options?: {
|
|
141
|
+
details?: Record<string, string[]>;
|
|
142
|
+
message?: string;
|
|
143
|
+
},
|
|
142
144
|
): NextResponse<ApiErrorPayload<C>> {
|
|
143
145
|
return NextResponse.json(
|
|
144
146
|
{
|
|
145
147
|
code,
|
|
146
|
-
details,
|
|
147
|
-
message,
|
|
148
|
+
details: options?.details,
|
|
149
|
+
message: options?.message,
|
|
148
150
|
},
|
|
149
151
|
{
|
|
150
152
|
status: statusCode,
|
|
@@ -210,19 +212,21 @@ type ServiceResponse<S, E = ServiceError> = [S, null] | [null, E];
|
|
|
210
212
|
*
|
|
211
213
|
* @example
|
|
212
214
|
* return createServiceError("validation:invalid-payload");
|
|
213
|
-
* return createServiceError("validation:invalid-payload",
|
|
215
|
+
* return createServiceError("validation:invalid-payload", { message: "Custom message" });
|
|
214
216
|
*/
|
|
215
217
|
export function createServiceError(
|
|
216
218
|
code: ErrorCode,
|
|
217
|
-
|
|
218
|
-
|
|
219
|
+
options?: {
|
|
220
|
+
details?: Record<string, string[]>;
|
|
221
|
+
message?: string;
|
|
222
|
+
},
|
|
219
223
|
): ServiceResponse<null, ServiceError> {
|
|
220
224
|
return [
|
|
221
225
|
null,
|
|
222
226
|
{
|
|
223
227
|
code,
|
|
224
|
-
details,
|
|
225
|
-
message: message ?? code,
|
|
228
|
+
details: options?.details,
|
|
229
|
+
message: options?.message ?? code,
|
|
226
230
|
},
|
|
227
231
|
];
|
|
228
232
|
}
|