@rpcbase/router 0.39.0 → 0.41.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.
package/dist/index.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  export * from 'react-router';
2
- export * from './applyRouteLoaders';
3
2
  export * from './loadRoute';
4
3
  export * from './useApplyMeta';
5
4
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAA;AAC5B,cAAc,qBAAqB,CAAA;AACnC,cAAc,aAAa,CAAA;AAC3B,cAAc,gBAAgB,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAA;AAC5B,cAAc,aAAa,CAAA;AAC3B,cAAc,gBAAgB,CAAA"}
package/dist/index.js CHANGED
@@ -1,118 +1,6 @@
1
- import { createPath, matchRoutes, parsePath, useLocation } from "react-router";
1
+ import { useLocation } from "react-router";
2
2
  export * from "react-router";
3
3
  import { lazy, useEffect } from "react";
4
- function createLocation(current, to, state = null, key) {
5
- const location = {
6
- pathname: current,
7
- search: "",
8
- hash: "",
9
- ...typeof to === "string" ? parsePath(to) : to,
10
- state,
11
- // TODO: This could be cleaned up. push/replace should probably just take
12
- // full Locations now and avoid the need to run through this flow at all
13
- // But that's a pretty big refactor to the current test suite so going to
14
- // keep as is for the time being and just let any incoming keys take precedence
15
- key: to && to.key || key
16
- };
17
- return location;
18
- }
19
- function getShortCircuitMatches(routes) {
20
- const route = routes.length === 1 ? routes[0] : routes.find((r) => r.index || !r.path || r.path === "/") || {
21
- id: "__shim-error-route__"
22
- };
23
- return {
24
- matches: [
25
- {
26
- params: {},
27
- pathname: "",
28
- pathnameBase: "",
29
- route
30
- }
31
- ],
32
- route
33
- };
34
- }
35
- async function applyRouteLoaders(req, dataRoutes) {
36
- const baseUrl = `${req.protocol}://${req.get("host")}`;
37
- const url = new URL(req.originalUrl, baseUrl);
38
- const method = req.method;
39
- const location = createLocation("", createPath(url), null, "default");
40
- const baseContext = {
41
- basename: "",
42
- location,
43
- loaderHeaders: {},
44
- actionHeaders: {}
45
- };
46
- const matches = matchRoutes(dataRoutes, location) || [];
47
- if (!matches) {
48
- const error = {
49
- status: 404,
50
- message: `No route matches URL: ${req.originalUrl}`
51
- };
52
- const { matches: notFoundMatches, route } = getShortCircuitMatches(dataRoutes);
53
- return {
54
- ...baseContext,
55
- matches: notFoundMatches,
56
- loaderData: {},
57
- actionData: null,
58
- errors: { [route.id]: error },
59
- statusCode: 404
60
- };
61
- }
62
- if (method !== "GET") {
63
- return {
64
- ...baseContext,
65
- matches,
66
- loaderData: {},
67
- actionData: null,
68
- errors: null,
69
- statusCode: 200
70
- };
71
- }
72
- const loaderPromisesResults = await Promise.allSettled(
73
- matches.map(async (match) => {
74
- const { route, params } = match;
75
- if (!route.loader) return null;
76
- try {
77
- return {
78
- id: route.id,
79
- data: await route.loader({
80
- params,
81
- ctx: { req }
82
- })
83
- };
84
- } catch (error) {
85
- throw { id: route.id, reason: error };
86
- }
87
- })
88
- );
89
- const loaderData = {};
90
- let errors = null;
91
- for (const result of loaderPromisesResults) {
92
- if (result.status === "fulfilled") {
93
- if (result.value) {
94
- loaderData[result.value.id] = result.value.data;
95
- }
96
- } else if (result.status === "rejected") {
97
- const id = result.reason?.id;
98
- if (!id) {
99
- throw new Error(`missing route ID in error: ${result.reason}`);
100
- }
101
- if (!errors) {
102
- errors = {};
103
- }
104
- errors[id] = result.reason;
105
- }
106
- }
107
- return {
108
- ...baseContext,
109
- matches,
110
- loaderData,
111
- actionData: null,
112
- errors,
113
- statusCode: Object.keys(errors || {}).length > 0 ? 500 : 200
114
- };
115
- }
116
4
  const loadRoute = (importPromise) => {
117
5
  const Component = lazy(async () => {
118
6
  const module = await importPromise;
@@ -125,28 +13,14 @@ const loadRoute = (importPromise) => {
125
13
  };
126
14
  return { Component, loader };
127
15
  };
128
- const useApplyMeta = () => {
16
+ const useApplyMeta = ({
17
+ defaultTitle = "",
18
+ defaultMeta = [],
19
+ pagesMeta = {}
20
+ } = {}) => {
129
21
  const location = useLocation();
130
22
  useEffect(() => {
131
23
  const loadMeta = async () => {
132
- let defaultTitle = "";
133
- let defaultMeta = [];
134
- let pagesMeta = {};
135
- try {
136
- const importPath = "@/static/meta";
137
- const module = await import(importPath);
138
- defaultTitle = module.defaultTitle || defaultTitle;
139
- defaultMeta = module.defaultMeta || defaultMeta;
140
- pagesMeta = module.pagesMeta || pagesMeta;
141
- } catch (error) {
142
- if (globalThis.__rb_env__.MODE !== "production") {
143
- console.warn(
144
- "Failed to load meta data from '@/static/meta'.",
145
- error
146
- );
147
- }
148
- return;
149
- }
150
24
  let pageMeta = pagesMeta[location.pathname];
151
25
  if (!pageMeta) {
152
26
  pageMeta = { title: defaultTitle, meta: defaultMeta };
@@ -175,10 +49,9 @@ const useApplyMeta = () => {
175
49
  document.head.appendChild(canonicalLink);
176
50
  };
177
51
  loadMeta();
178
- }, [location.pathname]);
52
+ }, [location.pathname, defaultTitle, defaultMeta, pagesMeta]);
179
53
  };
180
54
  export {
181
- applyRouteLoaders,
182
55
  loadRoute,
183
56
  useApplyMeta
184
57
  };
@@ -1,2 +1,13 @@
1
- export declare const useApplyMeta: () => void;
1
+ type MetaAttributeValue = string | number | boolean | undefined;
2
+ type MetaEntry = Record<string, MetaAttributeValue>;
3
+ type PageMeta = {
4
+ title: string;
5
+ meta: MetaEntry[];
6
+ };
7
+ export declare const useApplyMeta: ({ defaultTitle, defaultMeta, pagesMeta }?: {
8
+ defaultTitle?: string;
9
+ defaultMeta?: MetaEntry[];
10
+ pagesMeta?: Record<string, PageMeta>;
11
+ }) => void;
12
+ export {};
2
13
  //# sourceMappingURL=useApplyMeta.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"useApplyMeta.d.ts","sourceRoot":"","sources":["../src/useApplyMeta.tsx"],"names":[],"mappings":"AAkBA,eAAO,MAAM,YAAY,YAoExB,CAAA"}
1
+ {"version":3,"file":"useApplyMeta.d.ts","sourceRoot":"","sources":["../src/useApplyMeta.tsx"],"names":[],"mappings":"AAIA,KAAK,kBAAkB,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,SAAS,CAAA;AAC/D,KAAK,SAAS,GAAG,MAAM,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAAA;AAEnD,KAAK,QAAQ,GAAG;IACd,KAAK,EAAE,MAAM,CAAA;IACb,IAAI,EAAE,SAAS,EAAE,CAAA;CAClB,CAAA;AAGD,eAAO,MAAM,YAAY,GAAI,2CAI1B;IACD,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,WAAW,CAAC,EAAE,SAAS,EAAE,CAAA;IACzB,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAA;CAChC,SAiDL,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rpcbase/router",
3
- "version": "0.39.0",
3
+ "version": "0.41.0",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist"
@@ -1,4 +0,0 @@
1
- import { Request } from 'express';
2
- import { StaticHandlerContext } from './index';
3
- export declare function applyRouteLoaders(req: Request, dataRoutes: any[]): Promise<StaticHandlerContext>;
4
- //# sourceMappingURL=applyRouteLoaders.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"applyRouteLoaders.d.ts","sourceRoot":"","sources":["../src/applyRouteLoaders.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,OAAO,EAAC,MAAM,SAAS,CAAA;AAE/B,OAAO,EACL,oBAAoB,EAMrB,MAAM,SAAS,CAAA;AAsDhB,wBAAsB,iBAAiB,CACrC,GAAG,EAAE,OAAO,EACZ,UAAU,EAAE,GAAG,EAAE,GAChB,OAAO,CAAC,oBAAoB,CAAC,CAkG/B"}