Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
f260637
add prettier
hosseinmd Jan 2, 2021
a305329
add default value to create params
hosseinmd Jan 2, 2021
d312360
feat: support start
hosseinmd Feb 16, 2021
05bb9ea
chore: upload to react-route-type
hosseinmd Feb 16, 2021
a5c317f
feat: add useQueryParams
hosseinmd Feb 16, 2021
63bd093
chore: add react to devDependencies
hosseinmd Feb 16, 2021
3f3d21b
feat: add useParams
hosseinmd Feb 16, 2021
f1fd84d
v0.1.0
hosseinmd Feb 16, 2021
b7f7f7e
fix: typescript
hosseinmd Feb 16, 2021
71fb9eb
v0.1.1
hosseinmd Feb 16, 2021
6c02ce4
docs: add documention
hosseinmd Feb 18, 2021
26ba377
refactor: route
hosseinmd Aug 10, 2021
b0f8dc6
v1.0.0
hosseinmd Aug 10, 2021
3b4a046
BREAKING CHANGE: route param
hosseinmd Sep 15, 2021
0dd4bf0
BREAKING CHANGE: route querry
hosseinmd Sep 16, 2021
baccf73
test: hooks
hosseinmd Sep 16, 2021
db2549d
readme
hosseinmd Sep 16, 2021
170e322
v2.0.1
hosseinmd Sep 16, 2021
1d3bf31
readme
hosseinmd Sep 16, 2021
f68ece0
fix: add type
hosseinmd Oct 18, 2021
542811c
v2.0.2
hosseinmd Oct 18, 2021
2af505b
fix: route type
hosseinmd Oct 18, 2021
e80d91f
v2.0.3
hosseinmd Oct 18, 2021
c8a5c0d
fix: react router upgraded
al1rezaaa Nov 10, 2021
f929b69
Merge pull request #1 from polymoly/react-router-v6
polymoly Nov 10, 2021
c3b3ade
fix: removed history types
al1rezaaa Nov 10, 2021
609d995
Merge pull request #1 from polymoly/master
hosseinmd Nov 10, 2021
513f49a
Merge pull request #2 from polymoly/removeHistoryTypesPackage
hosseinmd Nov 10, 2021
e762791
v2.0.4
hosseinmd Nov 10, 2021
a9a654a
feat: add hasNested to template
hosseinmd Nov 26, 2021
21a724a
v2.0.5
hosseinmd Nov 26, 2021
c40e44c
m
1senka Nov 27, 2021
3d5a5e6
Merge pull request #3 from 1senka/feat/packagefixed
hosseinmd Nov 27, 2021
244fd87
Merge branch 'master' of https://github.com/hosseinmd/react-route-type
hosseinmd Nov 27, 2021
ae2cfd9
v2.0.6
hosseinmd Nov 27, 2021
5052cbf
fix: handle nested route
hosseinmd Nov 27, 2021
d6964c6
v2.0.7
hosseinmd Nov 27, 2021
5c45965
BREAKING CHANGE: seprate path from other option
hosseinmd Dec 7, 2021
71c1067
v3.0.0
hosseinmd Dec 7, 2021
26f5e6e
doc: readme
hosseinmd Dec 7, 2021
9f75145
v3.0.1
hosseinmd Dec 7, 2021
0034844
feat: add useMap
hosseinmd Dec 15, 2021
ea70f89
v3.0.2
hosseinmd Dec 15, 2021
8d07eca
doc
hosseinmd Dec 15, 2021
3deaec5
fix: title
hosseinmd Dec 15, 2021
ff7077a
v3.0.3
hosseinmd Dec 15, 2021
c489572
remove auto doc
hosseinmd Dec 15, 2021
def60ec
fix: route create bug
hosseinmd Dec 15, 2021
f9974f8
readme
hosseinmd Dec 15, 2021
3d8c58e
v3.0.4
hosseinmd Dec 15, 2021
7fc0b6d
fix: build
hosseinmd Jan 3, 2022
f068462
v3.0.5
hosseinmd Jan 3, 2022
ba1cc2f
fix: path params is required in response type
hosseinmd Jan 11, 2022
6a3972b
v3.0.6
hosseinmd Jan 11, 2022
a711538
doc
hosseinmd Jan 11, 2022
0890d95
fix: add title to route type
hosseinmd Jan 12, 2022
c132696
v3.0.7
hosseinmd Jan 12, 2022
06c09d2
fix: path params response
hosseinmd Feb 10, 2022
898e2d6
v3.0.8
hosseinmd Feb 10, 2022
dcfa5cc
fix: useMap path
hosseinmd Feb 11, 2022
7309df6
v3.0.9
hosseinmd Feb 11, 2022
54b6dca
BREAK CHANGE: remove hasNested
hosseinmd Feb 11, 2022
ec2e5da
v4.0.0
hosseinmd Feb 11, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 135 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,57 +1,162 @@
# Typesafe-React-Router
# react-route-type

A collection of types and utility functions to facilitate typesafe routing in React-Router.
A collection of types and utility functions to facilitate typesafe routing in react-router-dom and react-router-native.

`npm i typesafe-react-router`
npm

![vscode](https://i.imgur.com/WQHOWKx.gif "VSCode")
Note: This gif is using the 1.0 array-style API, rather than spread arguments used in 2.0.
`npm i react-route-type`

yarn

`yarn add react-route-type`

## Usage

```tsx
import { route, param } from 'typesafe-react-router';

export enum RouteNames {
HOME = "HOME"
VIEW_ALL = "VIEW_ALL"
VIEW_DETAILS = 'VIEW_DETAILS'
}
import { route } from "react-route-type";

export const Routes = {
[RouteNames.HOME]: route('home');
[RouteNames.VIEW_ALL]: route('view')
[RouteNames.VIEW_DETAILS]: route('view', param('id'))
}
home: route("home"),
view: route("view"),
details: route(["view", ":id"]),
users: route("users", { query: { search: "" } }),
};

const viewDetailsTemplate = Routes[RouteNames.VIEW_DETAILS].template() // -> /view/:id
const viewDetailsCreate = Routes[RouteNames.VIEW_DETAILS].create({ id: '2' }) // -> /view/2
const viewDetailsTemplate = Routes.details.template(); // -> /view/:id
const viewDetailsCreate = Routes.details.create({ id: "2" }); // -> /view/2

const viewDetailsCreateERROR = Routes[RouteNames.VIEW_DETAILS].create({}) // ERROR: property 'id' is missing in type {}
const viewDetailsCreateERROR = Routes.details.create({}); // ERROR: property 'id' is missing in type {}

// Usage with React Router
import { Route, Switch } from 'react-router-dom';
import { Home, Summary, Details } from './components'
import { Route, Switch } from "react-router-dom";
import { Home, Summary, Details } from "./components";
export class App extends React.PureComponent {
render() {
<Switch>
<Route path={Routes[RouteNames.HOME].template()} component={Home} />
<Route path={Routes[RouteNames.VIEW].template()} component={Summary} />
<Route path={Routes[RouteNames.VIEW_DETAILS].template()} component={Details} />
</Switch>
<Route path={Routes.home.template()} component={Home} />
<Route path={Routes.view.template()} component={Summary} />
<Route path={Routes.details.template()} component={Details} />
</Switch>;
}
}

import { Link } from 'react-router-dom';
import { Link } from "react-router-dom";

export class Home extends React.PureComponent {
render() {
<div>
<h1>Welcome Home!</h1>
<Link to={Routes[RouteNames.VIEW_DETAILS].create({ id: '3' })} />
{/* ERROR: property 'id' is missing in type {} */}
<Link to={Routes[RouteNames.VIEW_DETAILS].create({})} />
</div>
<Link to={Routes.details.create({ id: "3" })} />
<Link to={Routes.view.create({})} />
</div>;
}
}
```

## Hooks

#### useParams

Params is required

```js
export const Routes = {
details: route(["view", ":id"]),
};

// "/view/:id"
<Route path={Routes.details.template()} component={Details} />;

export const Details = () => {
const { id } = Routes.details.useParams();
};
```

#### useQueryParam

All property of query is optional

```js
export const View = () => {
const { search } = Routes.view.useQueryParam();
};
```

With Default value

```js
const users = route(["users"], {
query: { withDefault: "default" },
});

export const Users = () => {
const { withDefault } = Routes.view.useQueryParam();

/// withDefault === "default" is true
};
```

## Nested routes

```js
const home = route("home");
const settings = route("settings").createNestedRoutes((parent)=>({
global: parent.route("global");
advanced: parent.route("advanced");
}));

// App.js
function App() {
return (
<Routes>
<Route
path={home.template()} // "/home"
element={<Home />}
/>
<Route
path={settings.root.template()} // "/settings/*"
element={<Settings />}
/>
</Routes>
);
}

// Settings.js
function Settings() {
return (
<Container>
<Conversations />

<Routes>
<Route
path={setting.global.template()} // "global"
element={<Global />}
/>
<Route
path={setting.advanced.template()} // "advanced"
element={<Advanced />}
/>
</Routes>
</Container>
);
}
```

### useMap

This is useful for create breadcrumb

```js
const routeMap = setting.advanced.useMap(); // [{path:"settings",create=()=>"/settings"},{path:"advanced",create=()=>"/settings/advanced"}]

return (
//antd
<Breadcrumb>
{routeMap.map(({ path, create }) => {
<Breadcrumb.Item key={path}>
<a href={create()}></a>
</Breadcrumb.Item>;
})}
</Breadcrumb>
);
```
1 change: 1 addition & 0 deletions doc/.nojekyll
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TypeDoc added this file to prevent GitHub Pages from using Jekyll. You can turn off this behavior by setting the `githubPages` option to false.
149 changes: 149 additions & 0 deletions doc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
react-route-type / [Exports](modules.md)

# react-route-type

A collection of types and utility functions to facilitate typesafe routing in react-router-dom and react-router-native.

npm

`npm i react-route-type`

yarn

`yarn add react-route-type`

## Usage

```tsx
import { route } from "react-route-type";

export const Routes = {
home: route("home"),
view: route("view"),
details: route(["view", ":id"]),
users: route("users", { query: { search: "" } }),
};

const viewDetailsTemplate = Routes.details.template(); // -> /view/:id
const viewDetailsCreate = Routes.details.create({ id: "2" }); // -> /view/2

const viewDetailsCreateERROR = Routes.details.create({}); // ERROR: property 'id' is missing in type {}

// Usage with React Router
import { Route, Switch } from "react-router-dom";
import { Home, Summary, Details } from "./components";
export class App extends React.PureComponent {
render() {
<Switch>
<Route path={Routes.home.template()} component={Home} />
<Route path={Routes.view.template()} component={Summary} />
<Route path={Routes.details.template()} component={Details} />
</Switch>;
}
}

import { Link } from "react-router-dom";

export class Home extends React.PureComponent {
render() {
<div>
<h1>Welcome Home!</h1>
<Link to={Routes.details.create({ id: "3" })} />
<Link to={Routes.view.create({})} />
</div>;
}
}
```

## Hooks

#### useParams

Params is required

```js
export const Routes = {
details: route(["view", ":id"]),
};

// route
<Route path={Routes.details.template()} component={Details} />;

export const Details = () => {
const { id } = Routes.details.useParams();
};
```

#### useQueryParam

All property of query is optional

```js
export const View = () => {
const { search } = Routes.view.useQueryParam();
};
```

With Default value

```js
const users = route(["users"], {
query: { withDefault: "default" },
});

export const Users = () => {
const { withDefault } = Routes.view.useQueryParam();

/// withDefault === "default" is true
};
```

## Nested routes

```js
const home = route("home");
const settings = route("settings", { hasNested: true });
const settingGlobal = settings.route("global");
const settingAdvanced = settings.route("advanced");
// App.js
function App() {
return (
<Routes>
<Route
path={home.template()} // "/home"
element={<Home />}
/>
<Route
path={settings.template()} // "/settings/*"
element={<Messages />}
/>
</Routes>
);
}

// Messages.js
function Messages() {
return (
<Container>
<Conversations />

<Routes>
<Route
path={settingGlobal.template()} // "global"
element={<Global />}
/>
<Route
path={settingAdvanced.template()} // "advanced"
element={<Advanced />}
/>
</Routes>
</Container>
);
}
```

### useMap

```js
const routeMap = settingAdvanced.useMap(); // [{path:"settings",create=()=>"/settings"},{path:"advanced",create=()=>"/settings/advanced"}]
```
47 changes: 47 additions & 0 deletions doc/interfaces/Options.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
[react-route-type](../README.md) / [Exports](../modules.md) / Options

# Interface: Options<Q\>

## Type parameters

| Name | Type |
| :------ | :------ |
| `Q` | extends `QueryParamDefault` |

## Table of contents

### Properties

- [hasNested](Options.md#hasnested)
- [query](Options.md#query)
- [title](Options.md#title)

## Properties

### hasNested

• `Optional` **hasNested**: `boolean`

#### Defined in

[interfaces/types.ts:29](https://github.com/hosseinmd/react-route-type/blob/3deaec5/src/interfaces/types.ts#L29)

___

### query

• `Optional` **query**: `Q`

#### Defined in

[interfaces/types.ts:28](https://github.com/hosseinmd/react-route-type/blob/3deaec5/src/interfaces/types.ts#L28)

___

### title

• `Optional` **title**: `string`

#### Defined in

[interfaces/types.ts:30](https://github.com/hosseinmd/react-route-type/blob/3deaec5/src/interfaces/types.ts#L30)
Loading