spinning-router 0.0.2 → 0.0.3

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.
@@ -1,4 +1,3 @@
1
- /// <reference types="react" />
2
1
  export type LocationType = {
3
2
  path: string;
4
3
  match: string;
package/dist/Routes.d.ts CHANGED
@@ -1,5 +1,4 @@
1
- /// <reference types="react" />
2
- export type AsyncComponent = (arg: any) => Promise<JSX.Element>;
1
+ export type AsyncComponent = (parameters: any) => Promise<JSX.Element>;
3
2
  export type Route = {
4
3
  path: string;
5
4
  routes?: Routes;
@@ -15,3 +14,10 @@ export interface Register {
15
14
  export type Path = AllPaths<Register extends {
16
15
  routes: infer Routes;
17
16
  } ? Routes : never>;
17
+ type SplitPathSegments<S extends string, Delimiter extends string = "/", Parts extends string[] = []> = S extends `${infer Head}${Delimiter}${infer Tail}` ? SplitPathSegments<Tail, Delimiter, [...Parts, Head]> : [...Parts, S];
18
+ type FilterColonParams<T> = T extends `:${infer Param}` ? Param : never;
19
+ type PathParameterName<P extends Path> = FilterColonParams<SplitPathSegments<P>[number]>;
20
+ export type PathParameters<P extends Path> = {
21
+ [key in PathParameterName<P>]?: string | number;
22
+ };
23
+ export {};
@@ -60,6 +60,6 @@ const SpinningRouter = ({ routes, errorPage = DefaultErrorPage_1.DefaultErrorPag
60
60
  window.removeEventListener("softRefresh", updateHash);
61
61
  };
62
62
  }, []);
63
- return ((0, jsx_runtime_1.jsxs)(Location_1.Location.Provider, Object.assign({ value: { path: hash, match: "", parameters } }, { children: [element, overlay] })));
63
+ return ((0, jsx_runtime_1.jsxs)(Location_1.Location.Provider, { value: { path: hash, match: "", parameters }, children: [element, overlay] }));
64
64
  };
65
65
  exports.SpinningRouter = SpinningRouter;
@@ -2,5 +2,12 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.DefaultErrorPage = void 0;
4
4
  const jsx_runtime_1 = require("react/jsx-runtime");
5
- const DefaultErrorPage = ({ error }) => (0, jsx_runtime_1.jsxs)("p", { children: ["Unhandled error: ", "" + error] });
5
+ const DefaultErrorPage = ({ error }) => {
6
+ if (error && error.stack && error.message) {
7
+ return ((0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)("h4", { children: error.message }), (0, jsx_runtime_1.jsx)("pre", { children: error.stack })] }));
8
+ }
9
+ else {
10
+ return ((0, jsx_runtime_1.jsx)("div", { children: (0, jsx_runtime_1.jsx)("h4", { children: "" + error }) }));
11
+ }
12
+ };
6
13
  exports.DefaultErrorPage = DefaultErrorPage;
@@ -2,11 +2,11 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.DefaultLoadingIndicator = void 0;
4
4
  const jsx_runtime_1 = require("react/jsx-runtime");
5
- const DefaultLoadingIndicator = () => ((0, jsx_runtime_1.jsx)("div", Object.assign({ style: {
5
+ const DefaultLoadingIndicator = () => ((0, jsx_runtime_1.jsx)("div", { style: {
6
6
  position: "fixed",
7
7
  top: "40%",
8
8
  left: "50%",
9
9
  transform: "translate(-50%, -50%)",
10
10
  opacity: 1
11
- } }, { children: (0, jsx_runtime_1.jsx)("svg", Object.assign({ width: "200", height: "200", viewBox: "0 0 50 50" }, { children: (0, jsx_runtime_1.jsxs)("path", Object.assign({ fill: "#000000", d: "M25.251,6.461c-10.318,0-18.683,8.365-18.683,18.683h4.068c0-8.071,6.543-14.615,14.615-14.615V6.461z" }, { children: [(0, jsx_runtime_1.jsx)("animate", { attributeName: "opacity", values: "0; 0.1; 1.0", dur: "0.5s" }), (0, jsx_runtime_1.jsx)("animateTransform", { attributeType: "xml", attributeName: "transform", type: "rotate", from: "0 25 25", to: "360 25 25", dur: "1s", repeatCount: "indefinite" })] })) })) })));
11
+ }, children: (0, jsx_runtime_1.jsx)("svg", { width: "200", height: "200", viewBox: "0 0 50 50", children: (0, jsx_runtime_1.jsxs)("path", { fill: "#000000", d: "M25.251,6.461c-10.318,0-18.683,8.365-18.683,18.683h4.068c0-8.071,6.543-14.615,14.615-14.615V6.461z", children: [(0, jsx_runtime_1.jsx)("animate", { attributeName: "opacity", values: "0; 0.1; 0.5", dur: "2s" }), (0, jsx_runtime_1.jsx)("animateTransform", { attributeType: "xml", attributeName: "transform", type: "rotate", from: "0 25 25", to: "360 25 25", dur: "1s", repeatCount: "indefinite" })] }) }) }));
12
12
  exports.DefaultLoadingIndicator = DefaultLoadingIndicator;
@@ -2,5 +2,5 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.DefaultNotFoundPage = void 0;
4
4
  const jsx_runtime_1 = require("react/jsx-runtime");
5
- const DefaultNotFoundPage = () => (0, jsx_runtime_1.jsx)("p", { children: "Not found" });
5
+ const DefaultNotFoundPage = () => (0, jsx_runtime_1.jsx)("h4", { children: "Not found" });
6
6
  exports.DefaultNotFoundPage = DefaultNotFoundPage;
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export { link } from "./link";
2
+ export { unsafeLink } from "./link";
2
3
  export { navigate } from "./navigate";
3
4
  export { AsyncComponent, Route, Routes, Path, Register } from "./Routes";
4
5
  export { softRefresh } from "./softRefresh";
package/dist/index.js CHANGED
@@ -1,8 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Location = exports.SpinningRouter = exports.softRefresh = exports.navigate = exports.link = void 0;
3
+ exports.Location = exports.SpinningRouter = exports.softRefresh = exports.navigate = exports.unsafeLink = exports.link = void 0;
4
4
  var link_1 = require("./link");
5
5
  Object.defineProperty(exports, "link", { enumerable: true, get: function () { return link_1.link; } });
6
+ var link_2 = require("./link");
7
+ Object.defineProperty(exports, "unsafeLink", { enumerable: true, get: function () { return link_2.unsafeLink; } });
6
8
  var navigate_1 = require("./navigate");
7
9
  Object.defineProperty(exports, "navigate", { enumerable: true, get: function () { return navigate_1.navigate; } });
8
10
  var softRefresh_1 = require("./softRefresh");
package/dist/link.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- import { Path } from "./Routes";
2
- export declare const link: (path: Path, values?: {
3
- [key: string]: string | number;
4
- }) => string;
1
+ import { Path, PathParameters } from "./Routes";
2
+ export declare const unsafeLink: (path: string, values?: any) => string;
3
+ export declare const link: <P extends Path>(path: P, values?: PathParameters<P>) => string;
package/dist/link.js CHANGED
@@ -1,11 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.link = void 0;
4
- const link = (path, values = {}) => {
3
+ exports.link = exports.unsafeLink = void 0;
4
+ const Location_1 = require("./Location");
5
+ const react_1 = require("react");
6
+ const unsafeLink = (path, values) => {
5
7
  let result = [];
6
8
  for (let part of path.split("/")) {
7
9
  if (part.startsWith(":")) {
8
- result.push(encodeURIComponent(values[part.substring(1)]));
10
+ const key = part.substring(1);
11
+ let value = values && values[key] !== undefined ? values[key] : (0, react_1.useContext)(Location_1.Location).parameters[key];
12
+ result.push(encodeURIComponent(value));
9
13
  }
10
14
  else {
11
15
  result.push(part);
@@ -13,4 +17,5 @@ const link = (path, values = {}) => {
13
17
  }
14
18
  return "#" + result.join("/");
15
19
  };
16
- exports.link = link;
20
+ exports.unsafeLink = unsafeLink;
21
+ exports.link = exports.unsafeLink;
@@ -1,4 +1,3 @@
1
- /// <reference types="react" />
2
1
  import { Routes } from "./Routes";
3
2
  type Match = {
4
3
  path: string;
@@ -15,7 +15,10 @@ const doMatchRoute = (routes, querySplitted, parameters) => __awaiter(void 0, vo
15
15
  const pathSplitted = r.path.split("/").filter(e => e != "");
16
16
  if ((r.routes && pathSplitted.length <= querySplitted.length) || pathSplitted.length == querySplitted.length) {
17
17
  if (pathSplitted.every((e, i) => e.startsWith(":") || e == querySplitted[i])) {
18
- parameters = Object.assign(Object.assign({}, parameters), Object.fromEntries(pathSplitted.filter(e => e.startsWith(":")).map((e, i) => [e.substring(1), decodeURIComponent(querySplitted[i])])));
18
+ parameters = Object.assign(Object.assign({}, parameters), Object.fromEntries(pathSplitted
19
+ .map((pathPart, i) => ({ pathPart, queryPath: querySplitted[i] }))
20
+ .filter(({ pathPart }) => pathPart.startsWith(":"))
21
+ .map(({ pathPart, queryPath }) => [pathPart.substring(1), decodeURIComponent(queryPath)])));
19
22
  if (r.routes) {
20
23
  let subRoute = yield doMatchRoute(r.routes, querySplitted.slice(pathSplitted.length), parameters);
21
24
  return {
@@ -1,4 +1,2 @@
1
- import { Path } from "./Routes";
2
- export declare const navigate: (path: Path, values?: {
3
- [key: string]: string | number;
4
- }) => void;
1
+ import { Path, PathParameters } from "./Routes";
2
+ export declare const navigate: <P extends Path>(path: P, values?: PathParameters<P>) => void;
package/dist/navigate.js CHANGED
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.navigate = void 0;
4
4
  const link_1 = require("./link");
5
- const navigate = (path, values = {}) => {
5
+ const navigate = (path, values) => {
6
6
  window.location.hash = (0, link_1.link)(path, values);
7
7
  };
8
8
  exports.navigate = navigate;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "spinning-router",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "n/a",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",