nexa-router 0.1.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.
@@ -0,0 +1,6 @@
1
+ import type { Guard } from './route';
2
+ export declare function createGuards(): {
3
+ beforeEach: (guard: Guard) => void;
4
+ runGuards: (to: string, from: string) => Promise<boolean>;
5
+ };
6
+ //# sourceMappingURL=guards.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"guards.d.ts","sourceRoot":"","sources":["../src/guards.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAEpC,wBAAgB,YAAY;wBAGC,KAAK;oBAIH,MAAM,QAAQ,MAAM,KAAG,OAAO,CAAC,OAAO,CAAC;EAQrE"}
package/dist/guards.js ADDED
@@ -0,0 +1,15 @@
1
+ export function createGuards() {
2
+ const guards = [];
3
+ function beforeEach(guard) {
4
+ guards.push(guard);
5
+ }
6
+ async function runGuards(to, from) {
7
+ for (const guard of guards) {
8
+ if (!(await guard(to, from)))
9
+ return false;
10
+ }
11
+ return true;
12
+ }
13
+ return { beforeEach, runGuards };
14
+ }
15
+ //# sourceMappingURL=guards.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"guards.js","sourceRoot":"","sources":["../src/guards.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,YAAY;IAC1B,MAAM,MAAM,GAAY,EAAE,CAAA;IAE1B,SAAS,UAAU,CAAC,KAAY;QAC9B,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;IACpB,CAAC;IAED,KAAK,UAAU,SAAS,CAAC,EAAU,EAAE,IAAY;QAC/C,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;YAC3B,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,EAAE,EAAE,IAAI,CAAC,CAAC;gBAAE,OAAO,KAAK,CAAA;QAC5C,CAAC;QACD,OAAO,IAAI,CAAA;IACb,CAAC;IAED,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,CAAA;AAClC,CAAC"}
@@ -0,0 +1,18 @@
1
+ import { type RouteDef, type Guard } from './route';
2
+ export type { RouteDef, Guard } from './route';
3
+ export { matchPath } from './route';
4
+ export { Link } from './link';
5
+ export { createGuards } from './guards';
6
+ type RouterConfig = {
7
+ routes: RouteDef[];
8
+ mode?: 'hash' | 'history';
9
+ guards?: {
10
+ beforeEach: Guard[];
11
+ };
12
+ };
13
+ export declare function createRouter(config: RouterConfig): {
14
+ navigate: (path: string) => void;
15
+ currentRoute: import("nexa-reactivity").Signal<RouteDef | null>;
16
+ currentPath: import("nexa-reactivity").Signal<string>;
17
+ };
18
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAa,KAAK,QAAQ,EAAE,KAAK,KAAK,EAAE,MAAM,SAAS,CAAA;AAE9D,YAAY,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAC9C,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;AACnC,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAA;AAC7B,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAA;AAEvC,KAAK,YAAY,GAAG;IAClB,MAAM,EAAE,QAAQ,EAAE,CAAA;IAClB,IAAI,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IACzB,MAAM,CAAC,EAAE;QAAE,UAAU,EAAE,KAAK,EAAE,CAAA;KAAE,CAAA;CACjC,CAAA;AAED,wBAAgB,YAAY,CAAC,MAAM,EAAE,YAAY;qBAGvB,MAAM;;;EA0B/B"}
package/dist/index.js ADDED
@@ -0,0 +1,34 @@
1
+ import { signal, computed } from 'nexa-reactivity';
2
+ import { matchPath } from './route';
3
+ export { matchPath } from './route';
4
+ export { Link } from './link';
5
+ export { createGuards } from './guards';
6
+ export function createRouter(config) {
7
+ const currentPath = signal(window.location.pathname);
8
+ function navigate(path) {
9
+ if (config.mode === 'hash') {
10
+ window.location.hash = `#${path}`;
11
+ }
12
+ else {
13
+ window.history.pushState({}, '', path);
14
+ }
15
+ currentPath.value = path;
16
+ }
17
+ const currentRoute = computed(() => {
18
+ const route = config.routes.find(r => {
19
+ if (r.path === '*')
20
+ return true;
21
+ return matchPath(r.path, currentPath.value);
22
+ });
23
+ return route ?? null;
24
+ });
25
+ window.addEventListener('popstate', () => {
26
+ currentPath.value = window.location.pathname;
27
+ });
28
+ return {
29
+ navigate,
30
+ currentRoute,
31
+ currentPath,
32
+ };
33
+ }
34
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAA;AAClD,OAAO,EAAE,SAAS,EAA6B,MAAM,SAAS,CAAA;AAG9D,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;AACnC,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAA;AAC7B,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAA;AAQvC,MAAM,UAAU,YAAY,CAAC,MAAoB;IAC/C,MAAM,WAAW,GAAG,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAA;IAEpD,SAAS,QAAQ,CAAC,IAAY;QAC5B,IAAI,MAAM,CAAC,IAAI,KAAK,MAAM,EAAE,CAAC;YAC3B,MAAM,CAAC,QAAQ,CAAC,IAAI,GAAG,IAAI,IAAI,EAAE,CAAA;QACnC,CAAC;aAAM,CAAC;YACN,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,EAAE,EAAE,EAAE,EAAE,IAAI,CAAC,CAAA;QACxC,CAAC;QACD,WAAW,CAAC,KAAK,GAAG,IAAI,CAAA;IAC1B,CAAC;IAED,MAAM,YAAY,GAAG,QAAQ,CAAC,GAAG,EAAE;QACjC,MAAM,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE;YACnC,IAAI,CAAC,CAAC,IAAI,KAAK,GAAG;gBAAE,OAAO,IAAI,CAAA;YAC/B,OAAO,SAAS,CAAC,CAAC,CAAC,IAAI,EAAE,WAAW,CAAC,KAAK,CAAC,CAAA;QAC7C,CAAC,CAAC,CAAA;QACF,OAAO,KAAK,IAAI,IAAI,CAAA;IACtB,CAAC,CAAC,CAAA;IAEF,MAAM,CAAC,gBAAgB,CAAC,UAAU,EAAE,GAAG,EAAE;QACvC,WAAW,CAAC,KAAK,GAAG,MAAM,CAAC,QAAQ,CAAC,QAAQ,CAAA;IAC9C,CAAC,CAAC,CAAA;IAEF,OAAO;QACL,QAAQ;QACR,YAAY;QACZ,WAAW;KACZ,CAAA;AACH,CAAC"}
package/dist/link.d.ts ADDED
@@ -0,0 +1,12 @@
1
+ export declare function Link(props: {
2
+ to: string;
3
+ children?: any;
4
+ }, ctx: any): {
5
+ tag: string;
6
+ props: {
7
+ href: string;
8
+ onClick: (e: Event) => void;
9
+ };
10
+ children: any;
11
+ };
12
+ //# sourceMappingURL=link.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"link.d.ts","sourceRoot":"","sources":["../src/link.ts"],"names":[],"mappings":"AAAA,wBAAgB,IAAI,CAAC,KAAK,EAAE;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,EAAE,GAAG,CAAA;CAAE,EAAE,GAAG,EAAE,GAAG;;;;qBAKjD,KAAK;;;EAOvB"}
package/dist/link.js ADDED
@@ -0,0 +1,14 @@
1
+ export function Link(props, ctx) {
2
+ return {
3
+ tag: 'a',
4
+ props: {
5
+ href: props.to,
6
+ onClick: (e) => {
7
+ e.preventDefault();
8
+ ctx.navigate(props.to);
9
+ },
10
+ },
11
+ children: props.children,
12
+ };
13
+ }
14
+ //# sourceMappingURL=link.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"link.js","sourceRoot":"","sources":["../src/link.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,IAAI,CAAC,KAAqC,EAAE,GAAQ;IAClE,OAAO;QACL,GAAG,EAAE,GAAG;QACR,KAAK,EAAE;YACL,IAAI,EAAE,KAAK,CAAC,EAAE;YACd,OAAO,EAAE,CAAC,CAAQ,EAAE,EAAE;gBACpB,CAAC,CAAC,cAAc,EAAE,CAAA;gBAClB,GAAG,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC,CAAA;YACxB,CAAC;SACF;QACD,QAAQ,EAAE,KAAK,CAAC,QAAQ;KACzB,CAAA;AACH,CAAC"}
@@ -0,0 +1,8 @@
1
+ import type { Component } from 'nexa-runtime';
2
+ export type RouteDef = {
3
+ path: string;
4
+ component: Component;
5
+ };
6
+ export type Guard = (to: string, from: string) => boolean | Promise<boolean>;
7
+ export declare function matchPath(pattern: string, path: string): boolean;
8
+ //# sourceMappingURL=route.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"route.d.ts","sourceRoot":"","sources":["../src/route.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AAE7C,MAAM,MAAM,QAAQ,GAAG;IACrB,IAAI,EAAE,MAAM,CAAA;IACZ,SAAS,EAAE,SAAS,CAAA;CACrB,CAAA;AAED,MAAM,MAAM,KAAK,GAAG,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,KAAK,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAA;AAE5E,wBAAgB,SAAS,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAYhE"}
package/dist/route.js ADDED
@@ -0,0 +1,14 @@
1
+ export function matchPath(pattern, path) {
2
+ const patternParts = pattern.split('/');
3
+ const pathParts = path.split('/');
4
+ if (patternParts.length !== pathParts.length)
5
+ return false;
6
+ for (let i = 0; i < patternParts.length; i++) {
7
+ if (patternParts[i].startsWith(':'))
8
+ continue;
9
+ if (patternParts[i] !== pathParts[i])
10
+ return false;
11
+ }
12
+ return true;
13
+ }
14
+ //# sourceMappingURL=route.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"route.js","sourceRoot":"","sources":["../src/route.ts"],"names":[],"mappings":"AASA,MAAM,UAAU,SAAS,CAAC,OAAe,EAAE,IAAY;IACrD,MAAM,YAAY,GAAG,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;IACvC,MAAM,SAAS,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;IAEjC,IAAI,YAAY,CAAC,MAAM,KAAK,SAAS,CAAC,MAAM;QAAE,OAAO,KAAK,CAAA;IAE1D,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,YAAY,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QAC7C,IAAI,YAAY,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,GAAG,CAAC;YAAE,SAAQ;QAC7C,IAAI,YAAY,CAAC,CAAC,CAAC,KAAK,SAAS,CAAC,CAAC,CAAC;YAAE,OAAO,KAAK,CAAA;IACpD,CAAC;IAED,OAAO,IAAI,CAAA;AACb,CAAC"}
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "nexa-router",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js",
12
+ "default": "./dist/index.js"
13
+ }
14
+ },
15
+ "dependencies": {
16
+ "nexa-reactivity": "0.1.0",
17
+ "nexa-runtime": "0.1.0"
18
+ },
19
+ "files": [
20
+ "dist"
21
+ ],
22
+ "publishConfig": {
23
+ "access": "public"
24
+ },
25
+ "scripts": {
26
+ "build": "tsc",
27
+ "clean": "rm -rf dist"
28
+ }
29
+ }