@bb-labs/next-router 0.0.3 → 0.0.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.
package/README.md CHANGED
@@ -145,7 +145,7 @@ const searchParamsSchema = {
145
145
  };
146
146
 
147
147
  export function MyComponent() {
148
- const { data } = useSearchParams(searchParamsSchema, {
148
+ const { data, error } = useSearchParams(searchParamsSchema, {
149
149
  onSuccess: (data) => {
150
150
  console.log("Valid params:", data.email);
151
151
  },
@@ -155,7 +155,7 @@ export function MyComponent() {
155
155
  },
156
156
  });
157
157
 
158
- if (!data) return <div>Error</div>;
158
+ if (error) return <div>Error: {error}</div>;
159
159
 
160
160
  return <div>Email: {data.email}</div>;
161
161
  }
@@ -164,18 +164,19 @@ export function MyComponent() {
164
164
  ### Hook API
165
165
 
166
166
  ```tsx
167
- const { data } = useRouteParams(schema, options?);
168
- const { data } = useSearchParams(schema, options?);
167
+ const { data, error } = useRouteParams(schema, options?);
168
+ const { data, error } = useSearchParams(schema, options?);
169
169
  ```
170
170
 
171
171
  **Options:**
172
172
 
173
173
  - `onSuccess?: (data: T) => void` — Called when params are successfully validated
174
- - `onError?: (error: Error) => void` — Called when validation fails (defaults to redirecting to "/404")
174
+ - `onError?: (error: z.ZodError) => void` — Called when validation fails (defaults to redirecting to "/404")
175
175
 
176
176
  **Returns:**
177
177
 
178
178
  - `data` — The validated params, or `null` if validation failed
179
+ - `error` — The error message string, or `null` if validation succeeded
179
180
 
180
181
  ---
181
182
 
@@ -1,13 +1,9 @@
1
- import type { InferZodObjectShapeOutput, ZodObjectShape } from "../../utils/types";
1
+ import type { InferZodObjectShapeOutput, T_DataError, ZodObjectShape } from "../../utils/types";
2
2
  import type { OnZodError } from "../../server/z-page/utils/types";
3
3
  type HookOptions<T> = {
4
4
  onSuccess?: (data: T) => void;
5
5
  onError?: OnZodError;
6
6
  };
7
- export declare function useRouteParams<T extends ZodObjectShape>(schema: T, options?: HookOptions<InferZodObjectShapeOutput<T>>): {
8
- data: InferZodObjectShapeOutput<T> | null;
9
- };
10
- export declare function useSearchParams<T extends ZodObjectShape>(schema: T, options?: HookOptions<InferZodObjectShapeOutput<T>>): {
11
- data: InferZodObjectShapeOutput<T> | null;
12
- };
7
+ export declare function useRouteParams<T extends ZodObjectShape>(schema: T, options?: HookOptions<InferZodObjectShapeOutput<T>>): T_DataError<InferZodObjectShapeOutput<T>>;
8
+ export declare function useSearchParams<T extends ZodObjectShape>(schema: T, options?: HookOptions<InferZodObjectShapeOutput<T>>): T_DataError<InferZodObjectShapeOutput<T>>;
13
9
  export {};
@@ -26,7 +26,7 @@ export function useRouteParams(schema, options) {
26
26
  options?.onSuccess?.(result.data);
27
27
  }
28
28
  }, [result, options, router]);
29
- return { data: result.data };
29
+ return result.error ? { data: null, error: result.error.message } : { data: result.data, error: null };
30
30
  }
31
31
  export function useSearchParams(schema, options) {
32
32
  const nextSearchParams = useNextSearchParams();
@@ -53,5 +53,5 @@ export function useSearchParams(schema, options) {
53
53
  options?.onSuccess?.(result.data);
54
54
  }
55
55
  }, [result, options, router]);
56
- return { data: result.data };
56
+ return result.error ? { data: null, error: result.error.message } : { data: result.data, error: null };
57
57
  }
@@ -5,3 +5,10 @@ export type ZodObject<T extends ZodRawShape = ZodRawShape> = z.ZodObject<T>;
5
5
  export type ZodObjectShape = ZodRawShape | ZodObject;
6
6
  /** Infer output type from either raw shape or ZodObject */
7
7
  export type InferZodObjectShapeOutput<T extends ZodObjectShape> = T extends ZodObject<infer S> ? z.output<z.ZodObject<S>> : T extends ZodRawShape ? z.output<z.ZodObject<T>> : never;
8
+ export type T_DataError<T> = {
9
+ data: T;
10
+ error: null;
11
+ } | {
12
+ data: null;
13
+ error: string;
14
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bb-labs/next-router",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "author": "Beepbop",
5
5
  "homepage": "https://github.com/beepbop-labs/next-router",
6
6
  "keywords": [