typed-next-router 0.0.3 → 0.0.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/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +7 -3
- package/dist/index.d.ts +7 -3
- package/package.json +1 -1
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.tsx","../src/Link.tsx","../src/utils.ts","../src/useTypedRouter.ts","../src/useTypedSearchParams.ts"],"sourcesContent":["export { Link } from './Link';\nexport type { RouteMapShape, RouteRegistry } from './route-registry';\nexport type {\n\tExtractParams,\n\tPathParams,\n\tPathType,\n\tQueryParamName,\n\tQueryParams,\n\tQueryParamValue,\n\tTypedHref,\n} from './types';\nexport { useTypedRouter } from './useTypedRouter';\nexport { useTypedSearchParams } from './useTypedSearchParams';\nexport { buildHref } from './utils';\n","import type { LinkProps as NextLinkProps } from 'next/link';\nimport NextLink from 'next/link';\nimport type { ComponentProps } from 'react';\nimport type { PathType, TypedHref } from './types';\nimport { buildHref } from './utils';\n\ntype LinkProps<P extends PathType> = Omit<NextLinkProps, 'href'> &\n\tOmit<ComponentProps<'a'>, 'href'> & {\n\t\thref: TypedHref<P>;\n\t};\n\nfunction Link<P extends PathType>(props: LinkProps<P>) {\n\tconst { href, children, ...rest } = props;\n\n\tconst builtHref = buildHref(href);\n\n\treturn (\n\t\t<NextLink href={builtHref} {...rest}>\n\t\t\t{children}\n\t\t</NextLink>\n\t);\n}\n\nexport { Link };\n","interface Href {\n path: string;\n pathParams?: Record<string, string>;\n queryParams?: Record<string, string>;\n}\n\nexport function buildHref(href: Href): string {\n let url: string = href.path;\n\n if (href.pathParams) {\n Object.entries(href.pathParams).forEach(([key, value]) => {\n url = url.replace(`[${key}]`, value);\n });\n }\n\n if (href.queryParams && Object.keys(href.queryParams).length > 0) {\n const params = new URLSearchParams(href.queryParams);\n url = `${url}?${params.toString()}`;\n }\n\n return url;\n}\n","import type {\n\tAppRouterInstance,\n\tNavigateOptions,\n\tPrefetchOptions,\n} from 'next/dist/shared/lib/app-router-context.shared-runtime';\nimport { useRouter } from 'next/navigation';\nimport { useCallback } from 'react';\nimport type { PathType, TypedHref } from './types';\nimport { buildHref } from './utils';\n\ntype InternalHref = {\n\tpath: string;\n\tpathParams?: Record<string, string>;\n\tqueryParams?: Record<string, string>;\n};\n\ninterface TypedAppRouterInstance\n\textends Omit<AppRouterInstance, 'push' | 'replace' | 'prefetch'> {\n\tpush: <P extends PathType>(\n\t\thref: TypedHref<P>,\n\t\toptions?: NavigateOptions,\n\t) => void;\n\treplace: <P extends PathType>(\n\t\thref: TypedHref<P>,\n\t\toptions?: NavigateOptions,\n\t) => void;\n\tprefetch: <P extends PathType>(\n\t\thref: TypedHref<P>,\n\t\toptions?: PrefetchOptions,\n\t) => void;\n}\n\nexport const useTypedRouter = (): TypedAppRouterInstance => {\n\tconst router = useRouter();\n\n\tconst push: TypedAppRouterInstance['push'] = useCallback(\n\t\t(href: InternalHref, options) => {\n\t\t\tconst builtHref = buildHref(href);\n\n\t\t\trouter.push(builtHref, options);\n\t\t},\n\t\t[router.push],\n\t);\n\n\tconst replace: TypedAppRouterInstance['replace'] = useCallback(\n\t\t(href: InternalHref, options) => {\n\t\t\tconst builtHref = buildHref(href);\n\n\t\t\trouter.replace(builtHref, options);\n\t\t},\n\t\t[router.replace],\n\t);\n\n\tconst prefetch: TypedAppRouterInstance['prefetch'] = useCallback(\n\t\t(href: InternalHref, options) => {\n\t\t\tconst builtHref = buildHref(href);\n\n\t\t\trouter.prefetch(builtHref, options);\n\t\t},\n\t\t[router.prefetch],\n\t);\n\n\treturn {\n\t\t...router,\n\t\tpush,\n\t\treplace,\n\t\tprefetch,\n\t};\n};\n","import { useSearchParams } from 'next/navigation';\nimport type { PathType, QueryParamName, QueryParamValue } from './types';\n\ninterface TypedReadonlyURLSearchParams<P extends PathType>\n\textends URLSearchParams {\n\tappend<K extends QueryParamName<P>>(\n\t\tname: QueryParamName<P>,\n\t\tvalue: QueryParamValue<P, K>,\n\t): void;\n\tdelete<K extends QueryParamName<P>>(\n\t\tname: K,\n\t\tvalue?: QueryParamValue<P, K>,\n\t): void;\n\tget<K extends QueryParamName<P>>(\n\t\tname: QueryParamName<P>,\n\t): QueryParamValue<P, K> | null;\n\thas<K extends QueryParamName<P>>(\n\t\tname: K,\n\t\tvalue?: QueryParamValue<P, K>,\n\t): boolean;\n\tset<K extends QueryParamName<P>>(name: K, value: QueryParamValue<P, K>): void;\n}\n\nexport const useTypedSearchParams = <P extends PathType>(\n\tpath: P,\n): TypedReadonlyURLSearchParams<P> => {\n\tconst searchParams = useSearchParams();\n\n\treturn searchParams as TypedReadonlyURLSearchParams<P>;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACCA,kBAAqB;;;ACKd,SAAS,UAAU,MAAoB;AAC1C,MAAI,MAAc,KAAK;AAEvB,MAAI,KAAK,YAAY;AACjB,WAAO,QAAQ,KAAK,UAAU,EAAE,QAAQ,CAAC,CAAC,KAAK,KAAK,MAAM;AACtD,YAAM,IAAI,QAAQ,IAAI,GAAG,KAAK,KAAK;AAAA,IACvC,CAAC;AAAA,EACL;AAEA,MAAI,KAAK,eAAe,OAAO,KAAK,KAAK,WAAW,EAAE,SAAS,GAAG;AAC9D,UAAM,SAAS,IAAI,gBAAgB,KAAK,WAAW;AACnD,UAAM,GAAG,GAAG,IAAI,OAAO,SAAS,CAAC;AAAA,EACrC;AAEA,SAAO;AACX;;;ADJE;AANF,SAAS,KAAyB,OAAqB;AACtD,QAAM,EAAE,MAAM,UAAU,GAAG,KAAK,IAAI;AAEpC,QAAM,YAAY,UAAU,IAAI;AAEhC,SACC,4CAAC,YAAAA,SAAA,EAAS,MAAM,WAAY,GAAG,MAC7B,UACF;AAEF;;;AEhBA,wBAA0B;AAC1B,mBAA4B;AA0BrB,IAAM,iBAAiB,MAA8B;AAC3D,QAAM,aAAS,6BAAU;AAEzB,QAAM,WAAuC;AAAA,IAC5C,CAAC,MAAoB,YAAY;AAChC,YAAM,YAAY,UAAU,IAAI;AAEhC,aAAO,KAAK,WAAW,OAAO;AAAA,IAC/B;AAAA,IACA,CAAC,OAAO,IAAI;AAAA,EACb;AAEA,QAAM,cAA6C;AAAA,IAClD,CAAC,MAAoB,YAAY;AAChC,YAAM,YAAY,UAAU,IAAI;AAEhC,aAAO,QAAQ,WAAW,OAAO;AAAA,IAClC;AAAA,IACA,CAAC,OAAO,OAAO;AAAA,EAChB;AAEA,QAAM,eAA+C;AAAA,IACpD,CAAC,MAAoB,YAAY;AAChC,YAAM,YAAY,UAAU,IAAI;AAEhC,aAAO,SAAS,WAAW,OAAO;AAAA,IACnC;AAAA,IACA,CAAC,OAAO,QAAQ;AAAA,EACjB;AAEA,SAAO;AAAA,IACN,GAAG;AAAA,IACH;AAAA,IACA;AAAA,IACA;AAAA,EACD;AACD;;;ACpEA,IAAAC,qBAAgC;AAuBzB,IAAM,uBAAuB,CACnC,SACqC;AACrC,QAAM,mBAAe,oCAAgB;AAErC,SAAO;AACR;","names":["NextLink","import_navigation"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.tsx","../src/Link.tsx","../src/utils.ts","../src/useTypedRouter.ts","../src/useTypedSearchParams.ts"],"sourcesContent":["export { Link } from './Link';\n\nexport type { RouteMapShape, RouteRegistry } from './route-registry';\nexport type {\n\tExtractParams,\n\tPathParams,\n\tPathType,\n\tQueryParamName,\n\tQueryParams,\n\tQueryParamValue,\n\tTypedHref,\n} from './types';\n\nexport { useTypedRouter } from './useTypedRouter';\nexport { useTypedSearchParams } from './useTypedSearchParams';\nexport { buildHref } from './utils';\n","import type { LinkProps as NextLinkProps } from 'next/link';\nimport NextLink from 'next/link';\nimport type { ComponentProps } from 'react';\nimport type { PathType, TypedHref } from './types';\nimport { buildHref } from './utils';\n\ntype LinkProps<P extends PathType> = Omit<NextLinkProps, 'href'> &\n\tOmit<ComponentProps<'a'>, 'href'> & {\n\t\thref: TypedHref<P>;\n\t};\n\nfunction Link<P extends PathType>(props: LinkProps<P>) {\n\tconst { href, children, ...rest } = props;\n\n\tconst builtHref = buildHref(href);\n\n\treturn (\n\t\t<NextLink href={builtHref} {...rest}>\n\t\t\t{children}\n\t\t</NextLink>\n\t);\n}\n\nexport { Link };\n","interface Href {\n path: string;\n pathParams?: Record<string, string>;\n queryParams?: Record<string, string>;\n}\n\nexport function buildHref(href: Href): string {\n let url: string = href.path;\n\n if (href.pathParams) {\n Object.entries(href.pathParams).forEach(([key, value]) => {\n url = url.replace(`[${key}]`, value);\n });\n }\n\n if (href.queryParams && Object.keys(href.queryParams).length > 0) {\n const params = new URLSearchParams(href.queryParams);\n url = `${url}?${params.toString()}`;\n }\n\n return url;\n}\n","import type {\n\tAppRouterInstance,\n\tNavigateOptions,\n\tPrefetchOptions,\n} from 'next/dist/shared/lib/app-router-context.shared-runtime';\nimport { useRouter } from 'next/navigation';\nimport { useCallback } from 'react';\nimport type { PathType, TypedHref } from './types';\nimport { buildHref } from './utils';\n\ntype InternalHref = {\n\tpath: string;\n\tpathParams?: Record<string, string>;\n\tqueryParams?: Record<string, string>;\n};\n\ninterface TypedAppRouterInstance\n\textends Omit<AppRouterInstance, 'push' | 'replace' | 'prefetch'> {\n\tpush: <P extends PathType>(\n\t\thref: TypedHref<P>,\n\t\toptions?: NavigateOptions,\n\t) => void;\n\treplace: <P extends PathType>(\n\t\thref: TypedHref<P>,\n\t\toptions?: NavigateOptions,\n\t) => void;\n\tprefetch: <P extends PathType>(\n\t\thref: TypedHref<P>,\n\t\toptions?: PrefetchOptions,\n\t) => void;\n}\n\nexport const useTypedRouter = (): TypedAppRouterInstance => {\n\tconst router = useRouter();\n\n\tconst push: TypedAppRouterInstance['push'] = useCallback(\n\t\t(href: InternalHref, options) => {\n\t\t\tconst builtHref = buildHref(href);\n\n\t\t\trouter.push(builtHref, options);\n\t\t},\n\t\t[router.push],\n\t);\n\n\tconst replace: TypedAppRouterInstance['replace'] = useCallback(\n\t\t(href: InternalHref, options) => {\n\t\t\tconst builtHref = buildHref(href);\n\n\t\t\trouter.replace(builtHref, options);\n\t\t},\n\t\t[router.replace],\n\t);\n\n\tconst prefetch: TypedAppRouterInstance['prefetch'] = useCallback(\n\t\t(href: InternalHref, options) => {\n\t\t\tconst builtHref = buildHref(href);\n\n\t\t\trouter.prefetch(builtHref, options);\n\t\t},\n\t\t[router.prefetch],\n\t);\n\n\treturn {\n\t\t...router,\n\t\tpush,\n\t\treplace,\n\t\tprefetch,\n\t};\n};\n","import { useSearchParams } from 'next/navigation';\nimport type { PathType, QueryParamName, QueryParamValue } from './types';\n\ninterface TypedReadonlyURLSearchParams<P extends PathType>\n\textends URLSearchParams {\n\tappend<K extends QueryParamName<P>>(\n\t\tname: QueryParamName<P>,\n\t\tvalue: QueryParamValue<P, K>,\n\t): void;\n\tdelete<K extends QueryParamName<P>>(\n\t\tname: K,\n\t\tvalue?: QueryParamValue<P, K>,\n\t): void;\n\tget<K extends QueryParamName<P>>(\n\t\tname: QueryParamName<P>,\n\t): QueryParamValue<P, K> | null;\n\thas<K extends QueryParamName<P>>(\n\t\tname: K,\n\t\tvalue?: QueryParamValue<P, K>,\n\t): boolean;\n\tset<K extends QueryParamName<P>>(name: K, value: QueryParamValue<P, K>): void;\n}\n\nexport const useTypedSearchParams = <P extends PathType>(\n\tpath: P,\n): TypedReadonlyURLSearchParams<P> => {\n\tconst searchParams = useSearchParams();\n\n\treturn searchParams as TypedReadonlyURLSearchParams<P>;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACCA,kBAAqB;;;ACKd,SAAS,UAAU,MAAoB;AAC1C,MAAI,MAAc,KAAK;AAEvB,MAAI,KAAK,YAAY;AACjB,WAAO,QAAQ,KAAK,UAAU,EAAE,QAAQ,CAAC,CAAC,KAAK,K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AACR;","names":["NextLink","import_navigation"]}
|
package/dist/index.d.cts
CHANGED
|
@@ -3,6 +3,10 @@ import { LinkProps as LinkProps$1 } from 'next/link';
|
|
|
3
3
|
import { ComponentProps } from 'react';
|
|
4
4
|
import { AppRouterInstance, NavigateOptions, PrefetchOptions } from 'next/dist/shared/lib/app-router-context.shared-runtime';
|
|
5
5
|
|
|
6
|
+
type Prettify<T> = {
|
|
7
|
+
[K in keyof T]: T[K];
|
|
8
|
+
};
|
|
9
|
+
|
|
6
10
|
interface RouteDefinition {
|
|
7
11
|
pathParams?: readonly string[];
|
|
8
12
|
queryParams?: Record<string, readonly string[]>;
|
|
@@ -20,7 +24,7 @@ type AppRouteMap = RouteRegistry extends {
|
|
|
20
24
|
/**
|
|
21
25
|
* Extracts path types from registered map keys and explicit path registration.
|
|
22
26
|
*/
|
|
23
|
-
type PathType = RegisteredPathType;
|
|
27
|
+
type PathType = (keyof AppRouteMap & string) | RegisteredPathType;
|
|
24
28
|
/**
|
|
25
29
|
* Extracts route-specific parameter types from the registered route map.
|
|
26
30
|
*/
|
|
@@ -53,9 +57,9 @@ type QueryParamValue<P extends PathType, K extends QueryParamName<P>> = [
|
|
|
53
57
|
] extends [never] ? string : ExtractParams<P> extends {
|
|
54
58
|
queryParams: infer QP;
|
|
55
59
|
} ? K extends keyof QP ? QP[K] extends readonly (infer U)[] ? U | (string & object) : string : string : string;
|
|
56
|
-
type TypedHref<P extends PathType> = {
|
|
60
|
+
type TypedHref<P extends PathType> = Prettify<{
|
|
57
61
|
path: P;
|
|
58
|
-
} & PathParams<P> & QueryParams<P
|
|
62
|
+
} & PathParams<P> & QueryParams<P>>;
|
|
59
63
|
|
|
60
64
|
type LinkProps<P extends PathType> = Omit<LinkProps$1, 'href'> & Omit<ComponentProps<'a'>, 'href'> & {
|
|
61
65
|
href: TypedHref<P>;
|
package/dist/index.d.ts
CHANGED
|
@@ -3,6 +3,10 @@ import { LinkProps as LinkProps$1 } from 'next/link';
|
|
|
3
3
|
import { ComponentProps } from 'react';
|
|
4
4
|
import { AppRouterInstance, NavigateOptions, PrefetchOptions } from 'next/dist/shared/lib/app-router-context.shared-runtime';
|
|
5
5
|
|
|
6
|
+
type Prettify<T> = {
|
|
7
|
+
[K in keyof T]: T[K];
|
|
8
|
+
};
|
|
9
|
+
|
|
6
10
|
interface RouteDefinition {
|
|
7
11
|
pathParams?: readonly string[];
|
|
8
12
|
queryParams?: Record<string, readonly string[]>;
|
|
@@ -20,7 +24,7 @@ type AppRouteMap = RouteRegistry extends {
|
|
|
20
24
|
/**
|
|
21
25
|
* Extracts path types from registered map keys and explicit path registration.
|
|
22
26
|
*/
|
|
23
|
-
type PathType = RegisteredPathType;
|
|
27
|
+
type PathType = (keyof AppRouteMap & string) | RegisteredPathType;
|
|
24
28
|
/**
|
|
25
29
|
* Extracts route-specific parameter types from the registered route map.
|
|
26
30
|
*/
|
|
@@ -53,9 +57,9 @@ type QueryParamValue<P extends PathType, K extends QueryParamName<P>> = [
|
|
|
53
57
|
] extends [never] ? string : ExtractParams<P> extends {
|
|
54
58
|
queryParams: infer QP;
|
|
55
59
|
} ? K extends keyof QP ? QP[K] extends readonly (infer U)[] ? U | (string & object) : string : string : string;
|
|
56
|
-
type TypedHref<P extends PathType> = {
|
|
60
|
+
type TypedHref<P extends PathType> = Prettify<{
|
|
57
61
|
path: P;
|
|
58
|
-
} & PathParams<P> & QueryParams<P
|
|
62
|
+
} & PathParams<P> & QueryParams<P>>;
|
|
59
63
|
|
|
60
64
|
type LinkProps<P extends PathType> = Omit<LinkProps$1, 'href'> & Omit<ComponentProps<'a'>, 'href'> & {
|
|
61
65
|
href: TypedHref<P>;
|