@cosystem/router 0.0.2
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/LICENSE +21 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +55 -0
- package/dist/index.js.map +1 -0
- package/package.json +39 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Coaction
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Plugin, ProviderInput, Token } from "@cosystem/core";
|
|
2
|
+
|
|
3
|
+
//#region src/index.d.ts
|
|
4
|
+
interface RouteLocation {
|
|
5
|
+
readonly path: string;
|
|
6
|
+
readonly search: string;
|
|
7
|
+
readonly hash: string;
|
|
8
|
+
}
|
|
9
|
+
interface Router {
|
|
10
|
+
readonly current: RouteLocation;
|
|
11
|
+
navigate(to: string | RouteLocation): void;
|
|
12
|
+
subscribe(listener: (location: RouteLocation) => void): () => void;
|
|
13
|
+
}
|
|
14
|
+
interface RouterOptions {
|
|
15
|
+
readonly initialPath?: string;
|
|
16
|
+
}
|
|
17
|
+
declare const RouterToken: Token<Router>;
|
|
18
|
+
declare function createMemoryRouter(options?: RouterOptions): Router;
|
|
19
|
+
declare function createRouterPlugin(router: Router): Plugin;
|
|
20
|
+
declare function provideRouter(router?: Router): ProviderInput;
|
|
21
|
+
declare function parseLocation(value: string): RouteLocation;
|
|
22
|
+
//#endregion
|
|
23
|
+
export { RouteLocation, Router, RouterOptions, RouterToken, createMemoryRouter, createRouterPlugin, parseLocation, provideRouter };
|
|
24
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/index.ts"],"mappings":";;;UAEiB,aAAA;EAAA,SACN,IAAA;EAAA,SACA,MAAA;EAAA,SACA,IAAA;AAAA;AAAA,UAGM,MAAA;EAAA,SACN,OAAA,EAAS,aAAA;EAClB,QAAA,CAAS,EAAA,WAAa,aAAA;EACtB,SAAA,CAAU,QAAA,GAAW,QAAA,EAAU,aAAA;AAAA;AAAA,UAGhB,aAAA;EAAA,SACN,WAAA;AAAA;AAAA,cAGE,WAAA,EAAa,KAAA,CAAM,MAAA;AAAA,iBAEhB,kBAAA,CAAmB,OAAA,GAAS,aAAA,GAAqB,MAAA;AAAA,iBAwBjD,kBAAA,CAAmB,MAAA,EAAQ,MAAA,GAAS,MAAA;AAAA,iBAepC,aAAA,CAAc,MAAA,GAAQ,MAAA,GAAgC,aAAA;AAAA,iBAItD,aAAA,CAAc,KAAA,WAAgB,aAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { provide, token } from "@cosystem/core";
|
|
2
|
+
//#region src/index.ts
|
|
3
|
+
const RouterToken = token("CoSystem Router");
|
|
4
|
+
function createMemoryRouter(options = {}) {
|
|
5
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
6
|
+
let current = parseLocation(options.initialPath ?? "/");
|
|
7
|
+
return {
|
|
8
|
+
get current() {
|
|
9
|
+
return current;
|
|
10
|
+
},
|
|
11
|
+
navigate(to) {
|
|
12
|
+
current = typeof to === "string" ? parseLocation(to) : to;
|
|
13
|
+
for (const listener of listeners) listener(current);
|
|
14
|
+
},
|
|
15
|
+
subscribe(listener) {
|
|
16
|
+
listeners.add(listener);
|
|
17
|
+
return () => {
|
|
18
|
+
listeners.delete(listener);
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
function createRouterPlugin(router) {
|
|
24
|
+
let unsubscribe;
|
|
25
|
+
return {
|
|
26
|
+
name: "cosystem:router",
|
|
27
|
+
setup() {
|
|
28
|
+
unsubscribe = router.subscribe(() => void 0);
|
|
29
|
+
},
|
|
30
|
+
dispose() {
|
|
31
|
+
unsubscribe?.();
|
|
32
|
+
unsubscribe = void 0;
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
function provideRouter(router = createMemoryRouter()) {
|
|
37
|
+
return provide(RouterToken, { useValue: router });
|
|
38
|
+
}
|
|
39
|
+
function parseLocation(value) {
|
|
40
|
+
const hashIndex = value.indexOf("#");
|
|
41
|
+
const withoutHash = hashIndex === -1 ? value : value.slice(0, hashIndex);
|
|
42
|
+
const hash = hashIndex === -1 ? "" : value.slice(hashIndex);
|
|
43
|
+
const searchIndex = withoutHash.indexOf("?");
|
|
44
|
+
const path = searchIndex === -1 ? withoutHash : withoutHash.slice(0, searchIndex);
|
|
45
|
+
const search = searchIndex === -1 ? "" : withoutHash.slice(searchIndex);
|
|
46
|
+
return {
|
|
47
|
+
hash,
|
|
48
|
+
path: path === "" ? "/" : path,
|
|
49
|
+
search
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
//#endregion
|
|
53
|
+
export { RouterToken, createMemoryRouter, createRouterPlugin, parseLocation, provideRouter };
|
|
54
|
+
|
|
55
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["import { provide, token, type Plugin, type ProviderInput, type Token } from \"@cosystem/core\";\n\nexport interface RouteLocation {\n readonly path: string;\n readonly search: string;\n readonly hash: string;\n}\n\nexport interface Router {\n readonly current: RouteLocation;\n navigate(to: string | RouteLocation): void;\n subscribe(listener: (location: RouteLocation) => void): () => void;\n}\n\nexport interface RouterOptions {\n readonly initialPath?: string;\n}\n\nexport const RouterToken: Token<Router> = token<Router>(\"CoSystem Router\");\n\nexport function createMemoryRouter(options: RouterOptions = {}): Router {\n const listeners = new Set<(location: RouteLocation) => void>();\n let current = parseLocation(options.initialPath ?? \"/\");\n\n return {\n get current() {\n return current;\n },\n navigate(to) {\n current = typeof to === \"string\" ? parseLocation(to) : to;\n\n for (const listener of listeners) {\n listener(current);\n }\n },\n subscribe(listener) {\n listeners.add(listener);\n return () => {\n listeners.delete(listener);\n };\n },\n };\n}\n\nexport function createRouterPlugin(router: Router): Plugin {\n let unsubscribe: (() => void) | undefined;\n\n return {\n name: \"cosystem:router\",\n setup() {\n unsubscribe = router.subscribe(() => undefined);\n },\n dispose() {\n unsubscribe?.();\n unsubscribe = undefined;\n },\n };\n}\n\nexport function provideRouter(router: Router = createMemoryRouter()): ProviderInput {\n return provide(RouterToken, { useValue: router });\n}\n\nexport function parseLocation(value: string): RouteLocation {\n const hashIndex = value.indexOf(\"#\");\n const withoutHash = hashIndex === -1 ? value : value.slice(0, hashIndex);\n const hash = hashIndex === -1 ? \"\" : value.slice(hashIndex);\n const searchIndex = withoutHash.indexOf(\"?\");\n const path = searchIndex === -1 ? withoutHash : withoutHash.slice(0, searchIndex);\n const search = searchIndex === -1 ? \"\" : withoutHash.slice(searchIndex);\n\n return {\n hash,\n path: path === \"\" ? \"/\" : path,\n search,\n };\n}\n"],"mappings":";;AAkBA,MAAa,cAA6B,MAAc,iBAAiB;AAEzE,SAAgB,mBAAmB,UAAyB,CAAC,GAAW;CACtE,MAAM,4BAAY,IAAI,IAAuC;CAC7D,IAAI,UAAU,cAAc,QAAQ,eAAe,GAAG;CAEtD,OAAO;EACL,IAAI,UAAU;GACZ,OAAO;EACT;EACA,SAAS,IAAI;GACX,UAAU,OAAO,OAAO,WAAW,cAAc,EAAE,IAAI;GAEvD,KAAK,MAAM,YAAY,WACrB,SAAS,OAAO;EAEpB;EACA,UAAU,UAAU;GAClB,UAAU,IAAI,QAAQ;GACtB,aAAa;IACX,UAAU,OAAO,QAAQ;GAC3B;EACF;CACF;AACF;AAEA,SAAgB,mBAAmB,QAAwB;CACzD,IAAI;CAEJ,OAAO;EACL,MAAM;EACN,QAAQ;GACN,cAAc,OAAO,gBAAgB,KAAA,CAAS;EAChD;EACA,UAAU;GACR,cAAc;GACd,cAAc,KAAA;EAChB;CACF;AACF;AAEA,SAAgB,cAAc,SAAiB,mBAAmB,GAAkB;CAClF,OAAO,QAAQ,aAAa,EAAE,UAAU,OAAO,CAAC;AAClD;AAEA,SAAgB,cAAc,OAA8B;CAC1D,MAAM,YAAY,MAAM,QAAQ,GAAG;CACnC,MAAM,cAAc,cAAc,KAAK,QAAQ,MAAM,MAAM,GAAG,SAAS;CACvE,MAAM,OAAO,cAAc,KAAK,KAAK,MAAM,MAAM,SAAS;CAC1D,MAAM,cAAc,YAAY,QAAQ,GAAG;CAC3C,MAAM,OAAO,gBAAgB,KAAK,cAAc,YAAY,MAAM,GAAG,WAAW;CAChF,MAAM,SAAS,gBAAgB,KAAK,KAAK,YAAY,MAAM,WAAW;CAEtE,OAAO;EACL;EACA,MAAM,SAAS,KAAK,MAAM;EAC1B;CACF;AACF"}
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cosystem/router",
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "Router integration primitives for CoSystem.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"files": [
|
|
7
|
+
"dist"
|
|
8
|
+
],
|
|
9
|
+
"type": "module",
|
|
10
|
+
"sideEffects": false,
|
|
11
|
+
"module": "./dist/index.js",
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"import": "./dist/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"access": "public"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@cosystem/core": "0.0.2"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"tsdown": "0.22.3",
|
|
27
|
+
"typescript": "6.0.3",
|
|
28
|
+
"vitest": "4.1.9",
|
|
29
|
+
"@cosystem/tsconfig": "0.0.2"
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "tsdown",
|
|
33
|
+
"clean": "rm -rf coverage dist .turbo",
|
|
34
|
+
"dev": "tsdown --watch",
|
|
35
|
+
"test": "cd ../.. && vitest run --project @cosystem/router",
|
|
36
|
+
"test:coverage": "cd ../.. && vitest run --coverage --project @cosystem/router",
|
|
37
|
+
"typecheck": "tsc -p tsconfig.json --noEmit"
|
|
38
|
+
}
|
|
39
|
+
}
|