@ankhzet/router 1.2.0 → 1.3.0

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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @ankhzet/router
2
2
 
3
+ ## 1.3.0
4
+
5
+ ### Minor Changes
6
+
7
+ - Fix release
8
+
3
9
  ## 1.2.0
4
10
 
5
11
  ### Minor Changes
@@ -0,0 +1,88 @@
1
+ import { BrowserRouterProps } from 'react-router-dom';
2
+ import { ComponentType } from 'react';
3
+ import { JSX } from 'react/jsx-runtime';
4
+ import { Location as Location_2 } from 'history';
5
+
6
+ declare function make<T, C extends Record<string, RouteNode<any, any>> = {}>(pathname: string, factory: RouteFFactory<T>, children?: C): RouteNode<T, C>;
7
+
8
+ /** @public */
9
+ export declare type PathData<T> = Partial<Location_2<T>> & {
10
+ pathname: string;
11
+ };
12
+
13
+ /** @public */
14
+ declare function route(pathname: string): RouteNode<() => ({
15
+ pathname: string;
16
+ }), {}>;
17
+
18
+ /** @public */
19
+ declare function route<C extends Record<string, RouteNode<any, any>> = {}>(children: C): RouteNode<() => ({
20
+ pathname: string;
21
+ }), C>;
22
+
23
+ /** @public */
24
+ declare function route<C extends Record<string, RouteNode<any, any>> = {}>(pathname: string, children?: C): RouteNode<() => ({
25
+ pathname: string;
26
+ }), C>;
27
+
28
+ /** @public */
29
+ export declare type RouteDescriptor = {
30
+ screen?: ComponentType<any>;
31
+ children?: Record<string, RouteDescriptor>;
32
+ };
33
+
34
+ /** @public */
35
+ export declare type RoutedScreen = {
36
+ screen?: ComponentType<any>;
37
+ route: RouteRoot<any>;
38
+ children?: Record<string, RoutedScreen>;
39
+ };
40
+
41
+ /** @public */
42
+ export declare type RouteFactory<T> = (state?: T) => PathData<T>;
43
+
44
+ /** @public */
45
+ export declare type RouteFFactory<T> = (pathname: string) => RouteFactory<T>;
46
+
47
+ /** @public */
48
+ export declare type RouteNode<T, C extends RoutesChildren<any>> = C & {
49
+ root: RouteRoot<T>;
50
+ };
51
+
52
+ /** @public */
53
+ export declare type RouteRoot<T> = {
54
+ path: string;
55
+ strict?: boolean;
56
+ exact?: boolean;
57
+ } & RouteFactory<T>;
58
+
59
+ /** @public */
60
+ export declare type Routes = RouteNode<any, any>;
61
+
62
+ /** @public */
63
+ export declare type RoutesChildren<C extends Record<string, any>> = {
64
+ [N in keyof C]: C[N] extends Routes ? RouteNode<any, C[N]> : never;
65
+ };
66
+
67
+ /** @public */
68
+ export declare const UIRoute: {
69
+ ({ as: { screen: Component, children, route } }: {
70
+ as: RoutedScreen;
71
+ }): JSX.Element;
72
+ make: typeof make;
73
+ route: typeof route;
74
+ process: <D extends RouteDescriptor>(routes: Routes, routing: D, parent?: string) => D & RoutedScreen;
75
+ };
76
+
77
+ /** @public */
78
+ export declare const UIRouter: ({ root, ...props }: BrowserRouterProps & {
79
+ root: RoutedScreen;
80
+ }) => JSX.Element;
81
+
82
+ export declare const useLocationState: <T>(initial: Partial<T> | T) => Partial<T> | T;
83
+
84
+
85
+ export * from "history";
86
+ export * from "react-router-dom";
87
+
88
+ export { }
package/dist/index.js ADDED
@@ -0,0 +1,74 @@
1
+ import { BrowserRouter as e, Route as t, useLocation as n } from "react-router-dom";
2
+ import { jsx as r } from "react/jsx-runtime";
3
+ export * from "history";
4
+ export * from "react-router-dom";
5
+ //#region src/processRoutes.ts
6
+ var i = (e, t, n = "") => ({
7
+ ...t,
8
+ route: e.root || e,
9
+ children: t.children && Object.fromEntries(Object.entries(t.children).map(([t, r]) => {
10
+ let a = n ? `${n}.${t}` : t, o = e[t];
11
+ if (!o) throw Error(`Nav graph node "${a}" is missing`);
12
+ return [t, i(o, r)];
13
+ }))
14
+ });
15
+ //#endregion
16
+ //#region src/make.ts
17
+ function a(e, t, n = {}) {
18
+ let r = e.replace(/\/?$/, "/"), i = t(r), a = {
19
+ path: r,
20
+ strict: !1,
21
+ exact: !(n && Object.keys(n).length)
22
+ };
23
+ return {
24
+ root: new Proxy(i, {
25
+ apply(e, t, n) {
26
+ return e.apply(t, n);
27
+ },
28
+ ownKeys(e) {
29
+ return [...new Set([...Reflect.ownKeys(e), ...Reflect.ownKeys(a)])];
30
+ },
31
+ has(e, t) {
32
+ return Object.hasOwn(a, t) || Object.hasOwn(e, t);
33
+ },
34
+ getOwnPropertyDescriptor(e, t) {
35
+ let n = Object.getOwnPropertyDescriptor(a, t);
36
+ return n && Object.defineProperty(e, t, n), n || Object.getOwnPropertyDescriptor(e, t);
37
+ },
38
+ get(e, t) {
39
+ return a[t] ?? e[t];
40
+ },
41
+ set(e, t, n, r) {
42
+ return Reflect.set(t in a ? a : e, t, n, r);
43
+ }
44
+ }),
45
+ ...n
46
+ };
47
+ }
48
+ //#endregion
49
+ //#region src/route.ts
50
+ function o(e, t = {}) {
51
+ return typeof e != "string" && (t = e, e = "/"), a(e, (e) => () => ({ pathname: e }), t);
52
+ }
53
+ //#endregion
54
+ //#region src/UIRoute.tsx
55
+ var s = (e) => e.children, c = ({ as: { screen: e = s, children: n, route: i } }) => /* @__PURE__ */ r(t, {
56
+ strict: !1,
57
+ ...i,
58
+ component: n ? void 0 : e,
59
+ children: n && /* @__PURE__ */ r(e, { children: Object.entries(n).map(([e, t]) => /* @__PURE__ */ r(c, { as: t }, e)) })
60
+ });
61
+ c.make = a, c.route = o, c.process = i;
62
+ //#endregion
63
+ //#region src/UIRouter.tsx
64
+ var l = ({ root: t, ...n }) => /* @__PURE__ */ r(e, {
65
+ ...n,
66
+ children: /* @__PURE__ */ r(c, { as: t })
67
+ }), u = (e) => {
68
+ let { state: t } = n();
69
+ return t === void 0 ? e : t;
70
+ };
71
+ //#endregion
72
+ export { c as UIRoute, l as UIRouter, u as useLocationState };
73
+
74
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/processRoutes.ts","../src/make.ts","../src/route.ts","../src/UIRoute.tsx","../src/UIRouter.tsx","../src/useLocationState.ts"],"sourcesContent":["\nimport type { RouteDescriptor, RoutedScreen, Routes } from './interfaces';\n\nexport const processRoutes = <D extends RouteDescriptor>(\n routes: Routes,\n routing: D,\n parent = ''\n): D & RoutedScreen => {\n return {\n ...routing,\n route: routes.root || routes,\n children: routing.children && Object.fromEntries(\n Object\n .entries(routing.children)\n .map(([key, value]) => {\n const path = parent ? `${parent}.${key}` : key;\n const node = routes[key];\n\n if (!node) {\n throw new Error(`Nav graph node \"${path}\" is missing`);\n }\n\n return [key, processRoutes(node, value)];\n }),\n ),\n };\n};\n","import type { RouteFactory, RouteFFactory, RouteNode } from './interfaces';\n\nexport function make<T, C extends Record<string, RouteNode<any, any>> = {}>(\n pathname: string,\n factory: RouteFFactory<T>,\n children: C = {} as any\n): RouteNode<T, C> {\n const normalized = pathname.replace(/\\/?$/, '/');\n const action = factory(normalized);\n const data = {\n path: normalized,\n strict: false,\n exact: !(children && Object.keys(children).length),\n };\n const root = new Proxy(action, {\n apply(target: RouteFactory<T>, thisArg: any, argArray?: any): any {\n return target.apply(thisArg, argArray);\n },\n ownKeys(target: any) {\n return [...new Set([...Reflect.ownKeys(target), ...Reflect.ownKeys(data)])];\n },\n has(target: any, p: PropertyKey): boolean {\n return Object.hasOwn(data, p) || Object.hasOwn(target, p);\n },\n getOwnPropertyDescriptor(target: any, p: PropertyKey): PropertyDescriptor | undefined {\n const dataProp = Object.getOwnPropertyDescriptor(data, p);\n\n if (dataProp) {\n Object.defineProperty(target, p, dataProp);\n }\n\n return dataProp || Object.getOwnPropertyDescriptor(target, p);\n },\n get(target: any, p: keyof typeof data): any {\n return data[p] ?? target[p];\n },\n set(target: any, p: string | symbol, newValue: any, receiver: any): boolean {\n return Reflect.set(p in data ? data : target, p, newValue, receiver);\n },\n });\n\n return { root, ...children } as RouteNode<T, C>;\n}\n","import { make } from './make';\n\nimport type { RouteNode } from './interfaces';\n\n/** @public */\nexport function route(pathname: string): RouteNode<() => ({ pathname: string }), {}>;\n/** @public */\nexport function route<C extends Record<string, RouteNode<any, any>> = {}>(children: C): RouteNode<() => ({ pathname: string }), C>;\n/** @public */\nexport function route<C extends Record<string, RouteNode<any, any>> = {}>(pathname: string, children?: C): RouteNode<() => ({ pathname: string }), C>;\n/** @public */\nexport function route<C extends Record<string, RouteNode<any, any>> = {}>(pathname: string | C, children: C = {} as any) {\n if (typeof pathname !== 'string') {\n children = pathname as C;\n pathname = '/';\n }\n\n return make(pathname, (pathname) => () => ({ pathname }), children);\n}\n","import { PropsWithChildren } from 'react';\nimport { Route } from 'react-router-dom';\nimport type { RoutedScreen } from './interfaces';\nimport { processRoutes } from './processRoutes';\nimport { make } from './make';\nimport { route } from './route';\n\nconst PassThrough = (props: PropsWithChildren) => props.children;\n\n/** @public */\nexport const UIRoute = ({ as: { screen: Component = PassThrough, children, route } }: { as: RoutedScreen }) => (\n <Route strict={false} {...route} component={children ? undefined : Component}>\n {children && (\n <Component>\n {Object.entries(children).map(([key, child]) => (\n <UIRoute key={key} as={child} />\n ))}\n </Component>\n )}\n </Route>\n);\n\nUIRoute.make = make;\nUIRoute.route = route;\nUIRoute.process = processRoutes;\n","import type { BrowserRouterProps } from 'react-router-dom';\nimport { BrowserRouter as Router } from 'react-router-dom';\n\nimport { UIRoute } from './UIRoute';\nimport type { RoutedScreen } from './interfaces';\n\n/** @public */\nexport const UIRouter = ({ root, ...props }: BrowserRouterProps & { root: RoutedScreen }) => {\n return (\n <Router {...props}>\n <UIRoute as={root} />\n </Router>\n );\n};\n","import { useLocation } from 'react-router-dom';\n\nexport const useLocationState = <T>(initial: Partial<T> | T): Partial<T> | T => {\n const { state } = useLocation<T>();\n\n return state === undefined ? initial : state;\n};\n"],"mappings":";;;;;AAGA,IAAa,KACT,GACA,GACA,IAAS,QAEF;CACH,GAAG;CACH,OAAO,EAAO,QAAQ;CACtB,UAAU,EAAQ,YAAY,OAAO,YACjC,OACK,QAAQ,EAAQ,QAAQ,EACxB,KAAK,CAAC,GAAK,OAAW;EACnB,IAAM,IAAO,IAAS,GAAG,E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IAAW,aAAoB,EAAE,YAAS,IAAI,CAAQ;AACtE;;;ACXA,IAAM,KAAe,MAA6B,EAAM,UAG3C,KAAW,EAAE,IAAI,EAAE,QAAQ,IAAY,GAAa,aAAU,iBACvE,kBAAC,GAAD;CAAO,QAAQ;CAAO,GAAI;CAAO,WAAW,IAAW,KAAA,IAAY;WAC9D,KACG,kBAAC,GAAD,EAAA,UACK,OAAO,QAAQ,CAAQ,EAAE,KAAK,CAAC,GAAK,OACjC,kBAAC,GAAD,EAAmB,IAAI,EAAQ,GAAjB,CAAiB,CAClC,EACM,CAAA;AAEZ,CAAA;AAGX,EAAQ,OAAO,GACf,EAAQ,QAAQ,GAChB,EAAQ,UAAU;;;ACjBlB,IAAa,KAAY,EAAE,SAAM,GAAG,QAE5B,kBAAC,GAAD;CAAQ,GAAI;WACR,kBAAC,GAAD,EAAS,IAAI,EAAO,CAAA;AAChB,CAAA,GCTH,KAAuB,MAA4C;CAC5E,IAAM,EAAE,aAAU,EAAe;CAEjC,OAAO,MAAU,KAAA,IAAY,IAAU;AAC3C"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ankhzet/router",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "author": "ankhzet",
5
5
  "description": "Simple router",
6
6
  "license": "MIT",
@@ -11,7 +11,7 @@
11
11
  "package.json",
12
12
  "src/**/*.ts",
13
13
  "src/**/*.tsx",
14
- "build",
14
+ "dist",
15
15
  "*.md"
16
16
  ],
17
17
  "exports": {