@richie-router/server 0.1.2 → 0.1.3
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.cjs +17 -12
- package/dist/esm/index.mjs +17 -12
- package/dist/types/index.d.ts +18 -19
- package/package.json +2 -2
package/dist/cjs/index.cjs
CHANGED
|
@@ -45,10 +45,10 @@ __export(exports_src, {
|
|
|
45
45
|
});
|
|
46
46
|
module.exports = __toCommonJS(exports_src);
|
|
47
47
|
var import_core = require("@richie-router/core");
|
|
48
|
-
function defineHeadTags(routeManifest,
|
|
48
|
+
function defineHeadTags(routeManifest, routerSchema, definitions) {
|
|
49
49
|
return {
|
|
50
50
|
routeManifest,
|
|
51
|
-
|
|
51
|
+
routerSchema,
|
|
52
52
|
definitions
|
|
53
53
|
};
|
|
54
54
|
}
|
|
@@ -112,11 +112,11 @@ function buildMatches(routeManifest, location) {
|
|
|
112
112
|
};
|
|
113
113
|
});
|
|
114
114
|
}
|
|
115
|
-
async function executeHeadTag(request, headTags,
|
|
116
|
-
const definition = headTags.definitions[
|
|
117
|
-
const schemaEntry = headTags.
|
|
115
|
+
async function executeHeadTag(request, headTags, routeId, params, rawSearch) {
|
|
116
|
+
const definition = headTags.definitions[routeId];
|
|
117
|
+
const schemaEntry = headTags.routerSchema[routeId];
|
|
118
118
|
if (!definition) {
|
|
119
|
-
throw new Error(`Unknown head
|
|
119
|
+
throw new Error(`Unknown server head route "${routeId}".`);
|
|
120
120
|
}
|
|
121
121
|
const search = schemaEntry?.searchSchema ? schemaEntry.searchSchema.parse(rawSearch) : rawSearch;
|
|
122
122
|
const head = await definition.head({
|
|
@@ -132,11 +132,10 @@ async function executeHeadTag(request, headTags, headTagName, params, rawSearch)
|
|
|
132
132
|
async function resolveMatchedHead(request, headTags, matches) {
|
|
133
133
|
const resolvedHeadByRoute = new Map;
|
|
134
134
|
for (const match of matches) {
|
|
135
|
-
|
|
136
|
-
if (typeof headOption !== "string") {
|
|
135
|
+
if (!match.route.serverHead) {
|
|
137
136
|
continue;
|
|
138
137
|
}
|
|
139
|
-
const result = await executeHeadTag(request, headTags,
|
|
138
|
+
const result = await executeHeadTag(request, headTags, match.route.fullPath, match.params, match.search);
|
|
140
139
|
resolvedHeadByRoute.set(match.route.fullPath, result.head);
|
|
141
140
|
}
|
|
142
141
|
return import_core.resolveHeadConfig(matches, resolvedHeadByRoute);
|
|
@@ -144,17 +143,23 @@ async function resolveMatchedHead(request, headTags, matches) {
|
|
|
144
143
|
async function handleHeadTagRequest(request, options) {
|
|
145
144
|
const url = new URL(request.url);
|
|
146
145
|
const headBasePath = options.headBasePath ?? "/head-api";
|
|
147
|
-
if (
|
|
146
|
+
if (url.pathname !== headBasePath) {
|
|
148
147
|
return {
|
|
149
148
|
matched: false,
|
|
150
149
|
response: new Response("Not Found", { status: 404 })
|
|
151
150
|
};
|
|
152
151
|
}
|
|
153
|
-
const
|
|
152
|
+
const routeId = url.searchParams.get("routeId");
|
|
153
|
+
if (!routeId) {
|
|
154
|
+
return {
|
|
155
|
+
matched: true,
|
|
156
|
+
response: jsonResponse({ message: "Missing routeId" }, { status: 400 })
|
|
157
|
+
};
|
|
158
|
+
}
|
|
154
159
|
const params = JSON.parse(url.searchParams.get("params") ?? "{}");
|
|
155
160
|
const search = JSON.parse(url.searchParams.get("search") ?? "{}");
|
|
156
161
|
try {
|
|
157
|
-
const result = await executeHeadTag(request, options.headTags,
|
|
162
|
+
const result = await executeHeadTag(request, options.headTags, routeId, params, search);
|
|
158
163
|
return {
|
|
159
164
|
matched: true,
|
|
160
165
|
response: jsonResponse(result)
|
package/dist/esm/index.mjs
CHANGED
|
@@ -10,10 +10,10 @@ import {
|
|
|
10
10
|
resolveHeadConfig,
|
|
11
11
|
serializeHeadConfig
|
|
12
12
|
} from "@richie-router/core";
|
|
13
|
-
function defineHeadTags(routeManifest,
|
|
13
|
+
function defineHeadTags(routeManifest, routerSchema, definitions) {
|
|
14
14
|
return {
|
|
15
15
|
routeManifest,
|
|
16
|
-
|
|
16
|
+
routerSchema,
|
|
17
17
|
definitions
|
|
18
18
|
};
|
|
19
19
|
}
|
|
@@ -77,11 +77,11 @@ function buildMatches(routeManifest, location) {
|
|
|
77
77
|
};
|
|
78
78
|
});
|
|
79
79
|
}
|
|
80
|
-
async function executeHeadTag(request, headTags,
|
|
81
|
-
const definition = headTags.definitions[
|
|
82
|
-
const schemaEntry = headTags.
|
|
80
|
+
async function executeHeadTag(request, headTags, routeId, params, rawSearch) {
|
|
81
|
+
const definition = headTags.definitions[routeId];
|
|
82
|
+
const schemaEntry = headTags.routerSchema[routeId];
|
|
83
83
|
if (!definition) {
|
|
84
|
-
throw new Error(`Unknown head
|
|
84
|
+
throw new Error(`Unknown server head route "${routeId}".`);
|
|
85
85
|
}
|
|
86
86
|
const search = schemaEntry?.searchSchema ? schemaEntry.searchSchema.parse(rawSearch) : rawSearch;
|
|
87
87
|
const head = await definition.head({
|
|
@@ -97,11 +97,10 @@ async function executeHeadTag(request, headTags, headTagName, params, rawSearch)
|
|
|
97
97
|
async function resolveMatchedHead(request, headTags, matches) {
|
|
98
98
|
const resolvedHeadByRoute = new Map;
|
|
99
99
|
for (const match of matches) {
|
|
100
|
-
|
|
101
|
-
if (typeof headOption !== "string") {
|
|
100
|
+
if (!match.route.serverHead) {
|
|
102
101
|
continue;
|
|
103
102
|
}
|
|
104
|
-
const result = await executeHeadTag(request, headTags,
|
|
103
|
+
const result = await executeHeadTag(request, headTags, match.route.fullPath, match.params, match.search);
|
|
105
104
|
resolvedHeadByRoute.set(match.route.fullPath, result.head);
|
|
106
105
|
}
|
|
107
106
|
return resolveHeadConfig(matches, resolvedHeadByRoute);
|
|
@@ -109,17 +108,23 @@ async function resolveMatchedHead(request, headTags, matches) {
|
|
|
109
108
|
async function handleHeadTagRequest(request, options) {
|
|
110
109
|
const url = new URL(request.url);
|
|
111
110
|
const headBasePath = options.headBasePath ?? "/head-api";
|
|
112
|
-
if (
|
|
111
|
+
if (url.pathname !== headBasePath) {
|
|
113
112
|
return {
|
|
114
113
|
matched: false,
|
|
115
114
|
response: new Response("Not Found", { status: 404 })
|
|
116
115
|
};
|
|
117
116
|
}
|
|
118
|
-
const
|
|
117
|
+
const routeId = url.searchParams.get("routeId");
|
|
118
|
+
if (!routeId) {
|
|
119
|
+
return {
|
|
120
|
+
matched: true,
|
|
121
|
+
response: jsonResponse({ message: "Missing routeId" }, { status: 400 })
|
|
122
|
+
};
|
|
123
|
+
}
|
|
119
124
|
const params = JSON.parse(url.searchParams.get("params") ?? "{}");
|
|
120
125
|
const search = JSON.parse(url.searchParams.get("search") ?? "{}");
|
|
121
126
|
try {
|
|
122
|
-
const result = await executeHeadTag(request, options.headTags,
|
|
127
|
+
const result = await executeHeadTag(request, options.headTags, routeId, params, search);
|
|
123
128
|
return {
|
|
124
129
|
matched: true,
|
|
125
130
|
response: jsonResponse(result)
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,23 +1,22 @@
|
|
|
1
|
-
import { type
|
|
2
|
-
export interface HeadTagContext<TSearch> {
|
|
1
|
+
import { type ResolveAllParamsForRouteId, type RouteIdsWithServerHead, type RouterSchemaShape, type InferRouterSearchSchema, type AnyRoute, type HeadConfig } from '@richie-router/core';
|
|
2
|
+
export interface HeadTagContext<TParams extends Record<string, string>, TSearch> {
|
|
3
3
|
request: Request;
|
|
4
|
-
params:
|
|
4
|
+
params: TParams;
|
|
5
5
|
search: TSearch;
|
|
6
6
|
}
|
|
7
|
-
export interface HeadTagDefinition<TSearch> {
|
|
7
|
+
export interface HeadTagDefinition<TParams extends Record<string, string>, TSearch> {
|
|
8
8
|
staleTime?: number;
|
|
9
|
-
head: (ctx: HeadTagContext<TSearch>) => Promise<HeadConfig> | HeadConfig;
|
|
9
|
+
head: (ctx: HeadTagContext<TParams, TSearch>) => Promise<HeadConfig> | HeadConfig;
|
|
10
10
|
}
|
|
11
|
-
export
|
|
12
|
-
[
|
|
13
|
-
}
|
|
11
|
+
export type HeadTagDefinitions<TRouterSchema extends RouterSchemaShape> = {
|
|
12
|
+
[TRouteId in RouteIdsWithServerHead<TRouterSchema>]: HeadTagDefinition<ResolveAllParamsForRouteId<TRouteId>, InferRouterSearchSchema<TRouterSchema, TRouteId>>;
|
|
13
|
+
};
|
|
14
|
+
export interface DefinedHeadTags<TRouteManifest extends AnyRoute, TRouterSchema extends RouterSchemaShape> {
|
|
14
15
|
routeManifest: TRouteManifest;
|
|
15
|
-
|
|
16
|
-
definitions:
|
|
16
|
+
routerSchema: TRouterSchema;
|
|
17
|
+
definitions: HeadTagDefinitions<TRouterSchema>;
|
|
17
18
|
}
|
|
18
|
-
export declare function defineHeadTags<TRouteManifest extends AnyRoute,
|
|
19
|
-
[THeadTagName in keyof THeadTagSchema]: HeadTagDefinition<InferHeadTagSearchSchema<THeadTagSchema, THeadTagName>>;
|
|
20
|
-
}>>(routeManifest: TRouteManifest, headTagSchema: THeadTagSchema, definitions: TDefinitions): DefinedHeadTags<TRouteManifest, THeadTagSchema, TDefinitions>;
|
|
19
|
+
export declare function defineHeadTags<TRouteManifest extends AnyRoute, TRouterSchema extends RouterSchemaShape>(routeManifest: TRouteManifest, routerSchema: TRouterSchema, definitions: HeadTagDefinitions<TRouterSchema>): DefinedHeadTags<TRouteManifest, TRouterSchema>;
|
|
21
20
|
export interface HtmlOptions {
|
|
22
21
|
template: string | ((ctx: {
|
|
23
22
|
request: Request;
|
|
@@ -25,20 +24,20 @@ export interface HtmlOptions {
|
|
|
25
24
|
head: HeadConfig;
|
|
26
25
|
}) => string | Promise<string>);
|
|
27
26
|
}
|
|
28
|
-
export interface HandleRequestOptions<TRouteManifest extends AnyRoute,
|
|
27
|
+
export interface HandleRequestOptions<TRouteManifest extends AnyRoute, TRouterSchema extends RouterSchemaShape> {
|
|
29
28
|
routeManifest: TRouteManifest;
|
|
30
|
-
headTags: DefinedHeadTags<TRouteManifest,
|
|
29
|
+
headTags: DefinedHeadTags<TRouteManifest, TRouterSchema>;
|
|
31
30
|
html: HtmlOptions;
|
|
32
31
|
headBasePath?: string;
|
|
33
32
|
routeBasePath?: string;
|
|
34
33
|
}
|
|
35
|
-
export interface HandleHeadTagRequestOptions<TRouteManifest extends AnyRoute,
|
|
36
|
-
headTags: DefinedHeadTags<TRouteManifest,
|
|
34
|
+
export interface HandleHeadTagRequestOptions<TRouteManifest extends AnyRoute, TRouterSchema extends RouterSchemaShape> {
|
|
35
|
+
headTags: DefinedHeadTags<TRouteManifest, TRouterSchema>;
|
|
37
36
|
headBasePath?: string;
|
|
38
37
|
}
|
|
39
38
|
export interface HandleRequestResult {
|
|
40
39
|
matched: boolean;
|
|
41
40
|
response: Response;
|
|
42
41
|
}
|
|
43
|
-
export declare function handleHeadTagRequest<TRouteManifest extends AnyRoute,
|
|
44
|
-
export declare function handleRequest<TRouteManifest extends AnyRoute,
|
|
42
|
+
export declare function handleHeadTagRequest<TRouteManifest extends AnyRoute, TRouterSchema extends RouterSchemaShape>(request: Request, options: HandleHeadTagRequestOptions<TRouteManifest, TRouterSchema>): Promise<HandleRequestResult>;
|
|
43
|
+
export declare function handleRequest<TRouteManifest extends AnyRoute, TRouterSchema extends RouterSchemaShape>(request: Request, options: HandleRequestOptions<TRouteManifest, TRouterSchema>): Promise<HandleRequestResult>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@richie-router/server",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "Server helpers for Richie Router head tags and document handling",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"exports": {
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
}
|
|
14
14
|
},
|
|
15
15
|
"dependencies": {
|
|
16
|
-
"@richie-router/core": "^0.1.
|
|
16
|
+
"@richie-router/core": "^0.1.2"
|
|
17
17
|
},
|
|
18
18
|
"author": "Richie <oss@ricsam.dev>",
|
|
19
19
|
"homepage": "https://docs.ricsam.dev/richie-router",
|