@real-router/ssr-data-plugin 0.1.8 → 0.1.10
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/dist/cjs/index.d.ts +1 -1
- package/dist/cjs/index.d.ts.map +1 -0
- package/dist/esm/index.d.mts +1 -1
- package/dist/esm/index.d.mts.map +1 -0
- package/package.json +7 -5
- package/src/constants.ts +3 -0
- package/src/factory.ts +43 -0
- package/src/index.ts +11 -0
- package/src/types.ts +5 -0
- package/src/validation.ts +21 -0
package/dist/cjs/index.d.ts
CHANGED
|
@@ -12,7 +12,7 @@ declare module "@real-router/core" {
|
|
|
12
12
|
interface Router {
|
|
13
13
|
getRouteData: (state?: State) => unknown;
|
|
14
14
|
}
|
|
15
|
-
}
|
|
15
|
+
} //# sourceMappingURL=index.d.ts.map
|
|
16
16
|
//#endregion
|
|
17
17
|
export { type DataLoaderFn, type DataLoaderMap, ssrDataPluginFactory };
|
|
18
18
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../../src/types.ts","../../src/factory.ts","../../src/index.ts"],"mappings":";;;KAEY,YAAA,IAAgB,MAAA,EAAQ,MAAA,KAAW,OAAA;AAAA,KAEnC,aAAA,GAAgB,MAAA,SAAe,YAAA;;;iBCG3B,oBAAA,CAAqB,OAAA,EAAS,aAAA,GAAgB,aAAA;;;;YCAlD,MAAA;IACR,YAAA,GAAe,KAAA,GAAQ,KAAA;EAAA;AAAA"}
|
package/dist/esm/index.d.mts
CHANGED
|
@@ -12,7 +12,7 @@ declare module "@real-router/core" {
|
|
|
12
12
|
interface Router {
|
|
13
13
|
getRouteData: (state?: State) => unknown;
|
|
14
14
|
}
|
|
15
|
-
}
|
|
15
|
+
} //# sourceMappingURL=index.d.ts.map
|
|
16
16
|
//#endregion
|
|
17
17
|
export { type DataLoaderFn, type DataLoaderMap, ssrDataPluginFactory };
|
|
18
18
|
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../../src/types.ts","../../src/factory.ts","../../src/index.ts"],"mappings":";;;KAEY,YAAA,IAAgB,MAAA,EAAQ,MAAA,KAAW,OAAA;AAAA,KAEnC,aAAA,GAAgB,MAAA,SAAe,YAAA;;;iBCG3B,oBAAA,CAAqB,OAAA,EAAS,aAAA,GAAgB,aAAA;;;;YCAlD,MAAA;IACR,YAAA,GAAe,KAAA,GAAQ,KAAA;EAAA;AAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@real-router/ssr-data-plugin",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.10",
|
|
4
4
|
"type": "commonjs",
|
|
5
5
|
"description": "SSR per-route data loading plugin for Real-Router",
|
|
6
6
|
"main": "./dist/cjs/index.js",
|
|
@@ -8,7 +8,6 @@
|
|
|
8
8
|
"types": "./dist/esm/index.d.mts",
|
|
9
9
|
"exports": {
|
|
10
10
|
".": {
|
|
11
|
-
"development": "./src/index.ts",
|
|
12
11
|
"types": {
|
|
13
12
|
"import": "./dist/esm/index.d.mts",
|
|
14
13
|
"require": "./dist/cjs/index.d.ts"
|
|
@@ -18,7 +17,8 @@
|
|
|
18
17
|
}
|
|
19
18
|
},
|
|
20
19
|
"files": [
|
|
21
|
-
"dist"
|
|
20
|
+
"dist",
|
|
21
|
+
"src"
|
|
22
22
|
],
|
|
23
23
|
"repository": {
|
|
24
24
|
"type": "git",
|
|
@@ -36,14 +36,16 @@
|
|
|
36
36
|
"homepage": "https://github.com/greydragon888/real-router",
|
|
37
37
|
"sideEffects": false,
|
|
38
38
|
"peerDependencies": {
|
|
39
|
-
"@real-router/core": "^0.
|
|
39
|
+
"@real-router/core": "^0.46.0"
|
|
40
40
|
},
|
|
41
41
|
"scripts": {
|
|
42
42
|
"test": "vitest",
|
|
43
|
+
"test:properties": "vitest run --config vitest.config.properties.mts",
|
|
44
|
+
"test:stress": "vitest --config vitest.config.stress.mts --run",
|
|
43
45
|
"build": "tsdown --config-loader unrun",
|
|
44
46
|
"type-check": "tsc --noEmit",
|
|
45
47
|
"lint": "eslint --cache --ext .ts src/ tests/ --fix --max-warnings 0",
|
|
46
|
-
"lint:package": "
|
|
48
|
+
"lint:package": "publint",
|
|
47
49
|
"lint:types": "attw --pack .",
|
|
48
50
|
"build:dist-only": "tsdown --config-loader unrun"
|
|
49
51
|
}
|
package/src/constants.ts
ADDED
package/src/factory.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { getPluginApi } from "@real-router/core/api";
|
|
2
|
+
|
|
3
|
+
import { validateLoaders } from "./validation";
|
|
4
|
+
|
|
5
|
+
import type { DataLoaderMap } from "./types";
|
|
6
|
+
import type { State, PluginFactory, Plugin } from "@real-router/core";
|
|
7
|
+
|
|
8
|
+
export function ssrDataPluginFactory(loaders: DataLoaderMap): PluginFactory {
|
|
9
|
+
validateLoaders(loaders);
|
|
10
|
+
|
|
11
|
+
return (router): Plugin => {
|
|
12
|
+
const api = getPluginApi(router);
|
|
13
|
+
const dataStore = new WeakMap<State, unknown>();
|
|
14
|
+
|
|
15
|
+
const removeStartInterceptor = api.addInterceptor(
|
|
16
|
+
"start",
|
|
17
|
+
async (next, path) => {
|
|
18
|
+
const state = await next(path);
|
|
19
|
+
|
|
20
|
+
if (Object.hasOwn(loaders, state.name)) {
|
|
21
|
+
dataStore.set(state, await loaders[state.name](state.params));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return state;
|
|
25
|
+
},
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
const removeExtensions = api.extendRouter({
|
|
29
|
+
getRouteData(state?: State): unknown {
|
|
30
|
+
const routeState = state ?? router.getState();
|
|
31
|
+
|
|
32
|
+
return routeState ? (dataStore.get(routeState) ?? null) : null;
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
return {
|
|
37
|
+
teardown() {
|
|
38
|
+
removeStartInterceptor();
|
|
39
|
+
removeExtensions();
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { State } from "@real-router/core";
|
|
2
|
+
|
|
3
|
+
export type { DataLoaderMap, DataLoaderFn } from "./types";
|
|
4
|
+
|
|
5
|
+
export { ssrDataPluginFactory } from "./factory";
|
|
6
|
+
|
|
7
|
+
declare module "@real-router/core" {
|
|
8
|
+
interface Router {
|
|
9
|
+
getRouteData: (state?: State) => unknown;
|
|
10
|
+
}
|
|
11
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { ERROR_PREFIX } from "./constants";
|
|
2
|
+
|
|
3
|
+
import type { DataLoaderMap } from "./types";
|
|
4
|
+
|
|
5
|
+
export function validateLoaders(
|
|
6
|
+
loaders: unknown,
|
|
7
|
+
): asserts loaders is DataLoaderMap {
|
|
8
|
+
if (loaders === null || typeof loaders !== "object") {
|
|
9
|
+
throw new TypeError(`${ERROR_PREFIX} loaders must be a non-null object`);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
for (const [key, value] of Object.entries(
|
|
13
|
+
loaders as Record<string, unknown>,
|
|
14
|
+
)) {
|
|
15
|
+
if (typeof value !== "function") {
|
|
16
|
+
throw new TypeError(
|
|
17
|
+
`${ERROR_PREFIX} loader for route "${key}" must be a function`,
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|