Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
molefrog committed Nov 3, 2023
2 parents a1bcc4f + f5441c6 commit a92135b
Show file tree
Hide file tree
Showing 38 changed files with 565 additions and 612 deletions.
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"size-limit": [
{
"path": "packages/wouter/esm/index.js",
"limit": "2000 B",
"limit": "2500 B",
"ignore": [
"react",
"use-sync-external-store"
Expand All @@ -37,6 +37,7 @@
{
"path": "packages/wouter/esm/use-browser-location.js",
"limit": "1000 B",
"import": "{ useBrowserLocation }",
"ignore": [
"react",
"use-sync-external-store"
Expand Down Expand Up @@ -69,6 +70,7 @@
{
"path": "packages/wouter-preact/esm/use-browser-location.js",
"limit": "1000 B",
"import": "{ useBrowserLocation }",
"ignore": [
"preact",
"preact/hooks"
Expand Down Expand Up @@ -132,7 +134,7 @@
"@rollup/plugin-alias": "^5.0.0",
"@rollup/plugin-node-resolve": "^15.0.2",
"@rollup/plugin-replace": "^5.0.2",
"@size-limit/preset-small-lib": "^6.0.4",
"@size-limit/preset-small-lib": "^10.0.1",
"@testing-library/react": "^14.0.0",
"@types/babel__core": "^7.20.2",
"@types/react": "^18.2.0",
Expand All @@ -154,7 +156,7 @@
"react-test-renderer": "^18.2.0",
"rimraf": "^3.0.2",
"rollup": "^3.7.4",
"size-limit": "^6.0.4",
"size-limit": "^10.0.1",
"typescript": "5.0.4",
"vitest": "^0.34.3"
}
Expand Down
5 changes: 3 additions & 2 deletions packages/wouter-preact/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ export default defineConfig([
input: [
"wouter",
"wouter/use-browser-location",
"wouter/memory-location",
"wouter/use-hash-location",
"wouter/memory-location",
],
external: ["preact", "preact/hooks", "regexparam", "mitt"],
external: ["preact", "preact/hooks", "mitt"],

output: {
dir: "esm",
Expand All @@ -21,6 +21,7 @@ export default defineConfig([
alias({
entries: {
"./react-deps.js": "./src/preact-deps.js",
regexparam: "wouter/src/regexparam.js",
},
}),
],
Expand Down
4 changes: 1 addition & 3 deletions packages/wouter-preact/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,5 @@
"jsx": "react-jsx",
"jsxImportSource": "preact",
"types": ["preact"]
},
// ignore legacy type test
"exclude": ["./types/type-specs.tsx"]
}
}
30 changes: 17 additions & 13 deletions packages/wouter-preact/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@ import {
BaseLocationHook,
HookReturnValue,
HookNavigationOptions,
LocationHook,
} from "./use-browser-location";
BaseSearchHook,
} from "./location-hook";
import { BrowserLocationHook, BrowserSearchHook } from "./use-browser-location";

import { RouterObject, RouterOptions } from "./router";

// re-export some types from these modules
export { Path, BaseLocationHook, LocationHook } from "./use-browser-location";
export { Path, BaseLocationHook, BaseSearchHook } from "./location-hook";
export * from "./router";

import { RouteParams } from "regexparam";
import { RouteParams } from "./regexparam";

/**
* Route patterns and parameters
Expand Down Expand Up @@ -75,28 +76,27 @@ export function Route<
* Components: <Link /> & <Redirect />
*/

export type NavigationalProps<H extends BaseLocationHook = LocationHook> = (
| { to: Path; href?: never }
| { href: Path; to?: never }
) &
export type NavigationalProps<
H extends BaseLocationHook = BrowserLocationHook
> = ({ to: Path; href?: never } | { href: Path; to?: never }) &
HookNavigationOptions<H>;

export type LinkProps<H extends BaseLocationHook = LocationHook> = Omit<
export type LinkProps<H extends BaseLocationHook = BrowserLocationHook> = Omit<
JSX.HTMLAttributes,
"href"
> &
NavigationalProps<H>;

export type RedirectProps<H extends BaseLocationHook = LocationHook> =
export type RedirectProps<H extends BaseLocationHook = BrowserLocationHook> =
NavigationalProps<H> & {
children?: never;
};

export function Redirect<H extends BaseLocationHook = LocationHook>(
export function Redirect<H extends BaseLocationHook = BrowserLocationHook>(
props: RedirectProps<H>,
context?: any
): VNode<any> | null;
export function Link<H extends BaseLocationHook = LocationHook>(
export function Link<H extends BaseLocationHook = BrowserLocationHook>(
props: LinkProps<H>,
context?: any
): VNode<any> | null;
Expand Down Expand Up @@ -135,9 +135,13 @@ export function useRoute<
): Match<T extends DefaultParams ? T : RouteParams<RoutePath>>;

export function useLocation<
H extends BaseLocationHook = LocationHook
H extends BaseLocationHook = BrowserLocationHook
>(): HookReturnValue<H>;

export function useSearch<
H extends BaseSearchHook = BrowserSearchHook
>(): ReturnType<H>;

export function useParams<T = undefined>(): T extends string
? RouteParams<T>
: T extends undefined
Expand Down
34 changes: 34 additions & 0 deletions packages/wouter-preact/types/location-hook.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Foundation: useLocation and paths
*/

export type Path = string;

export type SearchString = string;

// the base useLocation hook type. Any custom hook (including the
// default one) should inherit from it.
export type BaseLocationHook = (
...args: any[]
) => [Path, (path: Path, ...args: any[]) => any];

export type BaseSearchHook = (...args: any[]) => SearchString;

/*
* Utility types that operate on hook
*/

// Returns the type of the location tuple of the given hook.
export type HookReturnValue<H extends BaseLocationHook> = ReturnType<H>;

// Returns the type of the navigation options that hook's push function accepts.
export type HookNavigationOptions<H extends BaseLocationHook> =
HookReturnValue<H>[1] extends (
path: Path,
options: infer R,
...rest: any[]
) => any
? R extends { [k: string]: any }
? R
: {}
: {};
11 changes: 8 additions & 3 deletions packages/wouter-preact/types/memory-location.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { BaseLocationHook, navigate, Path } from "./use-browser-location";
import { BaseLocationHook, Path } from "./location-hook";

type HookReturnValue = { hook: BaseLocationHook; navigate: typeof navigate };
type StubHistory = { history: Path[] };
type Navigate<S = any> = (
to: Path,
options?: { replace?: boolean; state?: S }
) => void;

type HookReturnValue = { hook: BaseLocationHook; navigate: Navigate };
type StubHistory = { history: Path[]; reset: () => void };

export function memoryLocation(options?: {
path?: Path;
Expand Down
29 changes: 29 additions & 0 deletions packages/wouter-preact/types/regexparam.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export function parse(
route: string,
loose?: boolean
): {
keys: string[];
pattern: RegExp;
};

export function parse(route: RegExp): {
keys: false;
pattern: RegExp;
};

export type RouteParams<T extends string> =
T extends `${infer Prev}/*/${infer Rest}`
? RouteParams<Prev> & { wild: string } & RouteParams<Rest>
: T extends `${string}:${infer P}?/${infer Rest}`
? { [K in P]?: string } & RouteParams<Rest>
: T extends `${string}:${infer P}/${infer Rest}`
? { [K in P]: string } & RouteParams<Rest>
: T extends `${string}:${infer P}?`
? { [K in P]?: string }
: T extends `${string}:${infer P}`
? { [K in P]: string }
: T extends `${string}*`
? { wild: string }
: T extends `${string}*?`
? { wild?: string }
: {};
11 changes: 10 additions & 1 deletion packages/wouter-preact/types/router.d.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
import { Path, BaseLocationHook } from "./use-browser-location";
import {
Path,
SearchString,
BaseLocationHook,
BaseSearchHook,
} from "./location-hook";

export type Parser = (route: Path) => { pattern: RegExp; keys: string[] };

// the object returned from `useRouter`
export interface RouterObject {
readonly hook: BaseLocationHook;
readonly searchHook: BaseSearchHook;
readonly base: Path;
readonly ownBase: Path;
readonly parser: Parser;
readonly parent?: RouterObject;
readonly ssrPath?: Path;
readonly ssrSearch?: SearchString;
}

// basic options to construct a router
export type RouterOptions = {
hook?: BaseLocationHook;
searchHook?: BaseSearchHook;
base?: Path;
parser?: Parser;
parent?: RouterObject;
ssrPath?: Path;
ssrSearch?: SearchString;
};
Loading

0 comments on commit a92135b

Please sign in to comment.