@real-router/ssr-data-plugin 0.1.7 → 0.1.8
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/package.json +4 -5
- package/src/constants.ts +0 -3
- package/src/factory.ts +0 -43
- package/src/index.ts +0 -11
- package/src/types.ts +0 -5
- package/src/validation.ts +0 -21
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.8",
|
|
4
4
|
"type": "commonjs",
|
|
5
5
|
"description": "SSR per-route data loading plugin for Real-Router",
|
|
6
6
|
"main": "./dist/cjs/index.js",
|
|
@@ -18,8 +18,7 @@
|
|
|
18
18
|
}
|
|
19
19
|
},
|
|
20
20
|
"files": [
|
|
21
|
-
"dist"
|
|
22
|
-
"src"
|
|
21
|
+
"dist"
|
|
23
22
|
],
|
|
24
23
|
"repository": {
|
|
25
24
|
"type": "git",
|
|
@@ -37,14 +36,14 @@
|
|
|
37
36
|
"homepage": "https://github.com/greydragon888/real-router",
|
|
38
37
|
"sideEffects": false,
|
|
39
38
|
"peerDependencies": {
|
|
40
|
-
"@real-router/core": "^0.45.
|
|
39
|
+
"@real-router/core": "^0.45.1"
|
|
41
40
|
},
|
|
42
41
|
"scripts": {
|
|
43
42
|
"test": "vitest",
|
|
44
43
|
"build": "tsdown --config-loader unrun",
|
|
45
44
|
"type-check": "tsc --noEmit",
|
|
46
45
|
"lint": "eslint --cache --ext .ts src/ tests/ --fix --max-warnings 0",
|
|
47
|
-
"lint:package": "publint",
|
|
46
|
+
"lint:package": "bash ../../scripts/publint-filter.sh",
|
|
48
47
|
"lint:types": "attw --pack .",
|
|
49
48
|
"build:dist-only": "tsdown --config-loader unrun"
|
|
50
49
|
}
|
package/src/constants.ts
DELETED
package/src/factory.ts
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
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
DELETED
package/src/validation.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
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
|
-
}
|