Skip to content

Commit eab2c11

Browse files
task: add generic type to router context
1 parent e162116 commit eab2c11

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export type { AppQueryOptions, AppMutationOptions, AppInfiniteQueryOptions } fro
1818

1919
// Config
2020
export { OpenApiRouter } from "./lib/config/router.context";
21+
export type { RouterContextValue } from "./lib/config/router.context";
2122
export { OpenApiQueryConfig } from "./lib/config/queryConfig.context";
2223

2324
// Auth

src/lib/config/router.context.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
import { type PropsWithChildren, createContext, use, useMemo } from "react";
22

3-
interface RouterProviderProps {
4-
replace: (url: string) => void;
3+
export interface RouterContextValue<TUrl = string> {
4+
replace: (url: TUrl) => void;
55
}
66

77
export namespace OpenApiRouter {
8-
const Context = createContext<RouterProviderProps | null>(null);
8+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
9+
const Context = createContext<RouterContextValue<any> | null>(null);
910

10-
export const Provider = ({ children, replace }: PropsWithChildren<RouterProviderProps>) => {
11+
export const Provider = <TUrl = string,>({ children, replace }: PropsWithChildren<RouterContextValue<TUrl>>) => {
1112
const value = useMemo(() => ({ replace }), [replace]);
1213

1314
return <Context value={value}>{children}</Context>;
1415
};
1516

16-
export const useRouter = () => {
17+
export function useRouter<TUrl = string>(): RouterContextValue<TUrl> {
1718
const context = use(Context);
1819

1920
if (!context) {
2021
throw new Error("useRouter must be used within an OpenApiRouter.Provider");
2122
}
2223

2324
return context;
24-
};
25+
}
2526
}

0 commit comments

Comments
 (0)