@ecopages/core 0.9.0 → 0.10.1
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 +3 -3
- package/src/adapters/node/create-app.d.ts +4 -4
- package/src/adapters/shared/http/api-request-pipeline.js +8 -6
- package/src/adapters/shared/http/define-api-handler.d.ts +8 -8
- package/src/adapters/shared/http/file-route-middleware-pipeline.d.ts +10 -11
- package/src/adapters/shared/http/file-route-middleware-pipeline.js +5 -6
- package/src/adapters/shared/http/fs-server-response-matcher.js +12 -4
- package/src/adapters/shared/http/run-middleware-chain.d.ts +29 -2
- package/src/adapters/shared/http/run-middleware-chain.js +108 -10
- package/src/adapters/shared/runtime/render-context.js +2 -21
- package/src/build/runtime/build-request-policy.js +4 -1
- package/src/build.d.ts +1 -0
- package/src/build.js +1 -0
- package/src/declarations.d.ts +2 -0
- package/src/eco/eco.browser.js +6 -26
- package/src/eco/eco.js +5 -16
- package/src/eco/page-data-hooks.d.ts +27 -0
- package/src/eco/page-data-hooks.js +64 -0
- package/src/eco/page-layout-normalization.d.ts +2 -2
- package/src/index.d.ts +1 -0
- package/src/index.js +1 -0
- package/src/integration.d.ts +1 -1
- package/src/integration.js +1 -1
- package/src/middleware/create-middleware.d.ts +41 -0
- package/src/middleware/create-middleware.js +72 -0
- package/src/plugins/eco-component-meta-plugin.d.ts +33 -0
- package/src/plugins/eco-component-meta-plugin.js +170 -23
- package/src/route-renderer/error-pages/error-page-renderer.js +29 -1
- package/src/route-renderer/explicit-view-render-preparation.d.ts +18 -0
- package/src/route-renderer/explicit-view-render-preparation.js +53 -7
- package/src/route-renderer/orchestration/document-shell/layout-shell-props.service.d.ts +15 -5
- package/src/route-renderer/orchestration/document-shell/layout-shell-props.service.js +27 -15
- package/src/route-renderer/orchestration/integration-renderer.d.ts +3 -1
- package/src/route-renderer/orchestration/integration-renderer.js +10 -6
- package/src/route-renderer/orchestration/route-pipeline/route-prepared-options.builder.js +0 -3
- package/src/route-renderer/orchestration/route-pipeline/route-render-orchestrator.d.ts +4 -0
- package/src/route-renderer/orchestration/route-pipeline/route-render-orchestrator.js +6 -1
- package/src/route-renderer/page-loading/page-module-loader.d.ts +27 -7
- package/src/route-renderer/page-loading/page-module-loader.js +46 -15
- package/src/route-renderer/page-loading/resolved-page-dependencies.js +14 -9
- package/src/static-site-generator/static-site-generator.d.ts +0 -1
- package/src/static-site-generator/static-site-generator.js +6 -6
- package/src/types/contracts.d.ts +196 -140
- package/src/types/eco-api-types.d.ts +76 -46
- package/src/types/locals-brand.d.ts +9 -0
- package/src/types/locals-brand.js +9 -0
- package/src/types/middleware-types.d.ts +143 -0
- package/src/types/middleware-types.js +1 -0
- package/src/types/public-types.d.ts +3 -2
- package/src/route-renderer/orchestration/route-pipeline/route-prepared-options.utils.d.ts +0 -4
- package/src/route-renderer/orchestration/route-pipeline/route-prepared-options.utils.js +0 -30
- package/src/utils/locals-utils.d.ts +0 -15
- package/src/utils/locals-utils.js +0 -34
|
@@ -2,11 +2,10 @@
|
|
|
2
2
|
* Type definitions for the eco namespace API
|
|
3
3
|
* @module
|
|
4
4
|
*/
|
|
5
|
-
import type { DependencyLazyTrigger, EcoComponent, EcoComponentDependencies, EcoHtmlComponent, EcoPageLayouts, EcoPagesElement, EcoPageComponent,
|
|
6
|
-
import type {
|
|
5
|
+
import type { DependencyLazyTrigger, EcoComponent, EcoComponentDependencies, EcoHtmlComponent, EcoPageLayouts, EcoPagesElement, EcoPageComponent, GetMetadata, GetPaths, GetProps, GetRequestProps, HtmlTemplateProps, LayoutBuildContext, LayoutProps, LayoutRequestContext, PagePropsFor, PageDependenciesInput, DynamicPageDependenciesInput, StaticCacheStrategy } from './contracts.js';
|
|
6
|
+
import type { ListLocals, PageMiddlewareEntry } from './middleware-types.js';
|
|
7
7
|
import type { ComponentIdentity } from './component-identity.js';
|
|
8
|
-
export type { EcoPageComponent, FileOwnedDependencyContribution, GetPageDependencies, GetPageDependenciesContext, PageDependenciesInput, PageDependenciesResult, PagePropsFor,
|
|
9
|
-
type RequiresKeys = keyof RequestLocals;
|
|
8
|
+
export type { EcoPageComponent, FileOwnedDependencyContribution, GetPageDependencies, GetPageDependenciesContext, PageDependenciesInput, PageDependenciesResult, PagePropsFor, } from './contracts.js';
|
|
10
9
|
/**
|
|
11
10
|
* Extracts the props type from one eco component.
|
|
12
11
|
*/
|
|
@@ -52,9 +51,6 @@ export type EcoEmbedProps<TComponent extends EcoComponent> = {
|
|
|
52
51
|
props: EmbedPropsOf<TComponent>;
|
|
53
52
|
children?: unknown;
|
|
54
53
|
};
|
|
55
|
-
type WithRequiredLocals<K extends keyof RequestLocals> = Omit<RequestLocals, K> & {
|
|
56
|
-
[P in K]-?: Exclude<RequestLocals[P], null | undefined>;
|
|
57
|
-
};
|
|
58
54
|
/**
|
|
59
55
|
* Lazy trigger options map directly to scripts-injector attributes.
|
|
60
56
|
* Only one trigger type can be active at a time.
|
|
@@ -88,12 +84,6 @@ export interface PageOptionsBase<T, E = EcoPagesElement> {
|
|
|
88
84
|
/** Bound by the component identity attribution transform. */
|
|
89
85
|
identity?: ComponentIdentity;
|
|
90
86
|
integration?: string;
|
|
91
|
-
/**
|
|
92
|
-
* Declares browser dependencies for the page, or resolves them per render after
|
|
93
|
-
* `props` are available.
|
|
94
|
-
*/
|
|
95
|
-
dependencies?: PageDependenciesInput<T>;
|
|
96
|
-
layout?: EcoPageLayouts<E>;
|
|
97
87
|
/**
|
|
98
88
|
* Lists the Params a dynamic route (e.g. `[slug].tsx`) is known for at build time, as `[{ params }]`.
|
|
99
89
|
*
|
|
@@ -102,13 +92,6 @@ export interface PageOptionsBase<T, E = EcoPagesElement> {
|
|
|
102
92
|
* and write HTML for it, `'dynamic'` Pages plan a Page Browser Graph for it.
|
|
103
93
|
*/
|
|
104
94
|
paths?: GetPaths;
|
|
105
|
-
/**
|
|
106
|
-
* Returns the page's props object. Props are passed to both render() and metadata().
|
|
107
|
-
*
|
|
108
|
-
* @remarks
|
|
109
|
-
* Receives Params from `paths` at build time and from the request at run time.
|
|
110
|
-
*/
|
|
111
|
-
props?: GetProps<T>;
|
|
112
95
|
/**
|
|
113
96
|
* Generate page metadata (title, description, etc.).
|
|
114
97
|
*
|
|
@@ -118,55 +101,108 @@ export interface PageOptionsBase<T, E = EcoPagesElement> {
|
|
|
118
101
|
*/
|
|
119
102
|
metadata?: GetMetadata<T>;
|
|
120
103
|
/**
|
|
121
|
-
*
|
|
104
|
+
* Renders the Page from what `props` returned, with the route's Params and Query.
|
|
122
105
|
*
|
|
123
|
-
*
|
|
106
|
+
* @remarks
|
|
107
|
+
* It receives no `locals`: a Dynamic Page reads them in `props` and returns what `render` needs.
|
|
124
108
|
*/
|
|
125
|
-
|
|
126
|
-
render: (props: PagePropsFor<T> & Partial<RequestPageContext>) => E | Promise<E>;
|
|
109
|
+
render: (props: PagePropsFor<T>) => E | Promise<E>;
|
|
127
110
|
}
|
|
128
111
|
/**
|
|
129
|
-
* Page
|
|
112
|
+
* Options of a Static or revalidating Page. No request exists when its `props` hook runs, so the hook receives the
|
|
113
|
+
* build context and the Page takes no middleware.
|
|
130
114
|
*/
|
|
131
|
-
interface
|
|
115
|
+
export interface StaticPageOptions<T, E = EcoPagesElement> extends PageOptionsBase<T, E> {
|
|
132
116
|
/**
|
|
133
117
|
* Cache configuration for ISR (Incremental Static Regeneration).
|
|
134
118
|
* - `'static'`: Reuse until the next deploy (default); HTML is revalidated with ETag
|
|
135
|
-
* - `'dynamic'`: No caching, render on every request
|
|
136
119
|
* - `{ revalidate: number, tags?: string[] }`: Cache with time-based revalidation
|
|
137
120
|
*/
|
|
138
|
-
cache?:
|
|
121
|
+
cache?: StaticCacheStrategy;
|
|
139
122
|
middleware?: undefined;
|
|
123
|
+
/**
|
|
124
|
+
* The layout tiers that wrap the Page, outer to inner.
|
|
125
|
+
*
|
|
126
|
+
* @remarks
|
|
127
|
+
* An entry's `props` factory receives the Params and no `locals`: no request exists when a Static or revalidating
|
|
128
|
+
* Page renders.
|
|
129
|
+
*/
|
|
130
|
+
layout?: EcoPageLayouts<E, LayoutBuildContext>;
|
|
131
|
+
/**
|
|
132
|
+
* Returns the page's props object. Props are passed to both render() and metadata().
|
|
133
|
+
*
|
|
134
|
+
* @remarks
|
|
135
|
+
* Receives each Params entry of `paths` at build, and the Params of the page again when a revalidating Page
|
|
136
|
+
* regenerates.
|
|
137
|
+
*/
|
|
138
|
+
props?: GetProps<T>;
|
|
139
|
+
/**
|
|
140
|
+
* Declares browser dependencies for the page, or resolves them per render after `props` are available.
|
|
141
|
+
*/
|
|
142
|
+
dependencies?: PageDependenciesInput<T>;
|
|
140
143
|
}
|
|
141
144
|
/**
|
|
142
|
-
*
|
|
145
|
+
* Options of a Dynamic Page, rendered on every request.
|
|
146
|
+
*
|
|
147
|
+
* @typeParam TLocals - The Locals its `props` hook reads. Without a `middleware` list it has no keys.
|
|
143
148
|
*/
|
|
144
|
-
interface
|
|
149
|
+
export interface DynamicPageOptions<T, E = EcoPagesElement, TLocals = {}> extends PageOptionsBase<T, E> {
|
|
145
150
|
/**
|
|
146
|
-
*
|
|
147
|
-
* Middleware runs on every request, so caching would bypass middleware effects.
|
|
151
|
+
* No caching: render on every request. Middleware requires it, since caching would bypass middleware effects.
|
|
148
152
|
*/
|
|
149
153
|
cache: 'dynamic';
|
|
154
|
+
/**
|
|
155
|
+
* The layout tiers that wrap the Page, outer to inner.
|
|
156
|
+
*
|
|
157
|
+
* @remarks
|
|
158
|
+
* An entry's `props` factory runs on the server for each request and reads the same `locals` as the `props`
|
|
159
|
+
* hook. What it returns reaches the browser for hydration.
|
|
160
|
+
*/
|
|
161
|
+
layout?: EcoPageLayouts<E, LayoutRequestContext<TLocals>>;
|
|
150
162
|
/**
|
|
151
163
|
* Request-time middleware for file-based routes.
|
|
152
164
|
* Runs before rendering and can short-circuit by returning a Response.
|
|
153
165
|
*/
|
|
154
|
-
middleware
|
|
166
|
+
middleware?: readonly PageMiddlewareEntry[];
|
|
167
|
+
/**
|
|
168
|
+
* The Page's request handler: it receives the request, its Params and Query, and the Locals its middleware
|
|
169
|
+
* provided, and returns the props `render` and `metadata` receive.
|
|
170
|
+
*
|
|
171
|
+
* @remarks
|
|
172
|
+
* It runs on every request and never at build. For a hydrated Page what it returns reaches the browser, so it
|
|
173
|
+
* returns only what the browser may see.
|
|
174
|
+
*/
|
|
175
|
+
props?: GetRequestProps<T, TLocals>;
|
|
176
|
+
/**
|
|
177
|
+
* Declares browser dependencies for the page, or resolves them from Params and Query.
|
|
178
|
+
*
|
|
179
|
+
* @remarks
|
|
180
|
+
* The resolver receives no `props`: the build plans the Page once per `paths` entry without running `props`.
|
|
181
|
+
*/
|
|
182
|
+
dependencies?: DynamicPageDependenciesInput;
|
|
155
183
|
}
|
|
184
|
+
/**
|
|
185
|
+
* Page options with a `middleware` list whose locals type `locals` in `props` and in the layout entries.
|
|
186
|
+
*
|
|
187
|
+
* @remarks
|
|
188
|
+
* `locals` is exactly what the list provides, a later entry overriding an earlier one.
|
|
189
|
+
*/
|
|
190
|
+
export type PageOptionsWithTypedLocals<T, E, M extends readonly PageMiddlewareEntry[]> = Omit<DynamicPageOptions<T, E, ListLocals<M>>, 'middleware'> & {
|
|
191
|
+
middleware: M;
|
|
192
|
+
};
|
|
156
193
|
/**
|
|
157
194
|
* Options for creating a page with eco.page()
|
|
158
195
|
*
|
|
159
196
|
* Define paths, props, and metadata inline. When using
|
|
160
197
|
* `middleware`, `cache` must be set to `'dynamic'` because middleware
|
|
161
198
|
* runs on every request and caching would bypass middleware effects.
|
|
199
|
+
* A Dynamic Page's `props` receives the request; a Static or revalidating Page's `props` receives only the build
|
|
200
|
+
* context.
|
|
162
201
|
*
|
|
163
202
|
* @template T - The props type for the page
|
|
164
203
|
* @template E - The element/return type (defaults to EcoPagesElement for Kita, use ReactNode for React)
|
|
165
204
|
*/
|
|
166
|
-
export type PageOptions<T, E = EcoPagesElement> =
|
|
167
|
-
export type PagePropsForWithLocals<T, K extends keyof RequestLocals | never = never> = PagePropsFor<T> & (K extends never ? Partial<RequestPageContext> : Omit<Partial<RequestPageContext>, 'locals'> & {
|
|
168
|
-
locals: WithRequiredLocals<Extract<K, keyof RequestLocals>>;
|
|
169
|
-
});
|
|
205
|
+
export type PageOptions<T, E = EcoPagesElement> = StaticPageOptions<T, E> | DynamicPageOptions<T, E>;
|
|
170
206
|
/**
|
|
171
207
|
* The eco namespace interface
|
|
172
208
|
*/
|
|
@@ -198,16 +234,10 @@ export interface Eco {
|
|
|
198
234
|
/**
|
|
199
235
|
* Create a page component with type-safe props from `props`.
|
|
200
236
|
* Returns an EcoPageComponent with attached paths, props, and metadata.
|
|
201
|
-
*
|
|
202
|
-
* If `requires` is provided, the `render` callback receives `locals` as required and non-null for the required keys.
|
|
203
237
|
*/
|
|
204
238
|
page: {
|
|
205
|
-
<T = {}, E = EcoPagesElement>(options:
|
|
206
|
-
|
|
207
|
-
}): EcoPageComponent<T>;
|
|
208
|
-
<T = {}, E = EcoPagesElement, const K extends RequiresKeys = never>(options: Omit<PageOptions<T, E>, 'render' | 'requires'> & {
|
|
209
|
-
requires: PageRequires<K>;
|
|
210
|
-
render: (props: PagePropsForWithLocals<T, K>) => E | Promise<E>;
|
|
211
|
-
}): EcoPageComponent<T>;
|
|
239
|
+
<T = {}, E = EcoPagesElement, const M extends readonly PageMiddlewareEntry[] = readonly PageMiddlewareEntry[]>(options: PageOptionsWithTypedLocals<T, E, M>): EcoPageComponent<T>;
|
|
240
|
+
<T = {}, E = EcoPagesElement>(options: StaticPageOptions<T, E>): EcoPageComponent<T>;
|
|
241
|
+
<T = {}, E = EcoPagesElement>(options: DynamicPageOptions<T, E>): EcoPageComponent<T>;
|
|
212
242
|
};
|
|
213
243
|
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The key of the brand a made middleware carries, typed with the locals it leaves available.
|
|
3
|
+
*
|
|
4
|
+
* @remarks
|
|
5
|
+
* Only `createMiddleware` and `composeMiddleware` import it, and the package does not export it, so no other code
|
|
6
|
+
* can build an object with the brand: a literal `{}` cannot pose as a middleware that proves a local. A spread copy
|
|
7
|
+
* keeps the brand in its type but not the stages, so `flattenMiddleware` rejects it when the list is defined.
|
|
8
|
+
*/
|
|
9
|
+
export declare const localsBrand: unique symbol;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The key of the brand a made middleware carries, typed with the locals it leaves available.
|
|
3
|
+
*
|
|
4
|
+
* @remarks
|
|
5
|
+
* Only `createMiddleware` and `composeMiddleware` import it, and the package does not export it, so no other code
|
|
6
|
+
* can build an object with the brand: a literal `{}` cannot pose as a middleware that proves a local. A spread copy
|
|
7
|
+
* keeps the brand in its type but not the stages, so `flattenMiddleware` rejects it when the list is defined.
|
|
8
|
+
*/
|
|
9
|
+
export const localsBrand = Symbol('ecopages.middlewareLocals');
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import type { localsBrand } from './locals-brand.js';
|
|
2
|
+
import type { FileRouteMiddleware, FileRouteMiddlewareContext, Middleware } from './contracts.js';
|
|
3
|
+
declare const providedBrand: unique symbol;
|
|
4
|
+
declare const unknownLocals: unique symbol;
|
|
5
|
+
/**
|
|
6
|
+
* What `next(locals)` resolves to: the response of the later stages, labelled with the locals it provided.
|
|
7
|
+
*
|
|
8
|
+
* @remarks
|
|
9
|
+
* The label exists only in the types. It is `Omit<Response, 'clone'>` and not `Response` on purpose: when a
|
|
10
|
+
* stage returns both `next({ ... })` and a bare `Response`, TypeScript merges return types by subtype, and a
|
|
11
|
+
* `Response` subtype would be absorbed into plain `Response`, losing what was provided. Without `clone` it is
|
|
12
|
+
* no subtype, so both survive. The label is invariant in `TLocals` (a function from and to it), so
|
|
13
|
+
* `Provided<{ session }>` is not a subtype of `Provided<{}>`; otherwise a stage that returns `next({ session })`
|
|
14
|
+
* on one path and `next()` on another would be reduced to `{}` and lose `session`. At runtime it is a normal
|
|
15
|
+
* `Response`, and a helper that takes one needs a `Response`: mutate its headers and return it, do not cast.
|
|
16
|
+
*/
|
|
17
|
+
export type Provided<TLocals extends object> = Omit<Response, 'clone'> & {
|
|
18
|
+
readonly [providedBrand]: (locals: TLocals) => TLocals;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* The `next` a stage calls to continue. Pass the locals the stage provides to later stages and to the handler.
|
|
22
|
+
*
|
|
23
|
+
* @remarks
|
|
24
|
+
* Two overloads instead of one optional parameter: with `locals?: TLocals`, `next<{ session: Session }>()` and
|
|
25
|
+
* `next(cond ? { session } : undefined)` would both type-check while providing nothing at runtime.
|
|
26
|
+
*/
|
|
27
|
+
export interface ProvideNext {
|
|
28
|
+
(): Promise<Provided<{}>>;
|
|
29
|
+
<TLocals extends object>(locals: TLocals): Promise<Provided<TLocals>>;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* What a stage returns: the result of `next(...)`, or a `Response` that stops the request.
|
|
33
|
+
*
|
|
34
|
+
* @remarks
|
|
35
|
+
* `any` accepts every provided shape; the shape each stage provides is read back from its own return type.
|
|
36
|
+
*/
|
|
37
|
+
export type StageResult = Provided<any> | Response;
|
|
38
|
+
/**
|
|
39
|
+
* The context a stage receives. `locals` holds what the stages before it provided.
|
|
40
|
+
*/
|
|
41
|
+
export type MiddlewareContext<TLocals extends object = {}> = Omit<FileRouteMiddlewareContext, 'locals'> & {
|
|
42
|
+
readonly locals: Readonly<TLocals>;
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* One stage: a function of the context and `next`.
|
|
46
|
+
*
|
|
47
|
+
* @typeParam TIn - The locals provided by the stages before it
|
|
48
|
+
* @typeParam TResult - What it returns, inferred from the body
|
|
49
|
+
*/
|
|
50
|
+
export type MiddlewareStage<TIn extends object = {}, TResult extends StageResult | Promise<StageResult> = StageResult | Promise<StageResult>> = (context: MiddlewareContext<TIn>, next: ProvideNext) => TResult;
|
|
51
|
+
/**
|
|
52
|
+
* A middleware made by `createMiddleware` or `composeMiddleware`.
|
|
53
|
+
*
|
|
54
|
+
* @typeParam TLocals - Everything available after it runs: what it provides, and what the stages composed into
|
|
55
|
+
* it provide.
|
|
56
|
+
*
|
|
57
|
+
* @remarks
|
|
58
|
+
* The brand is required and keyed by a symbol the package does not export, so only `createMiddleware` and
|
|
59
|
+
* `composeMiddleware` make one. It is invariant in `TLocals`: a middleware cannot be upcast to one that proves
|
|
60
|
+
* fewer keys, such as `EcoMiddleware` (`{}`), and then listed where the fold would believe it provides nothing.
|
|
61
|
+
* Its runtime value is an identity function that nothing calls.
|
|
62
|
+
*/
|
|
63
|
+
export interface EcoMiddleware<TLocals extends object = {}> {
|
|
64
|
+
readonly [localsBrand]: (locals: TLocals) => TLocals;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* An {@link EcoMiddleware} of any provided locals, for lists and constraints that accept every shape.
|
|
68
|
+
*
|
|
69
|
+
* @remarks
|
|
70
|
+
* `any` widens the check; each middleware's own locals are read back through {@link ListLocals}.
|
|
71
|
+
*/
|
|
72
|
+
export type AnyEcoMiddleware = EcoMiddleware<any>;
|
|
73
|
+
/**
|
|
74
|
+
* An entry of a `middleware` list: a made middleware, or an existing plain `(ctx, next)` function, which
|
|
75
|
+
* provides no locals.
|
|
76
|
+
*/
|
|
77
|
+
export type MiddlewareEntry = AnyEcoMiddleware | Middleware | FileRouteMiddleware;
|
|
78
|
+
/**
|
|
79
|
+
* An entry of an API route or group list. A plain function is typed with the context an API pipeline supplies, so
|
|
80
|
+
* an inline `(ctx, next) => ...` infers its parameters.
|
|
81
|
+
*/
|
|
82
|
+
export type ApiMiddlewareEntry = AnyEcoMiddleware | Middleware<Request, unknown>;
|
|
83
|
+
/**
|
|
84
|
+
* An entry of a page's `middleware` list. A plain function is typed with the context a page pipeline supplies,
|
|
85
|
+
* which carries no `query` or schema results.
|
|
86
|
+
*/
|
|
87
|
+
export type PageMiddlewareEntry = AnyEcoMiddleware | FileRouteMiddleware;
|
|
88
|
+
/**
|
|
89
|
+
* The form a stage takes once running: the pipeline supplies the context and a `next` that merges what a stage
|
|
90
|
+
* provides into `ctx.locals`.
|
|
91
|
+
*/
|
|
92
|
+
export type RuntimeMiddlewareStage = (context: never, next: never) => unknown;
|
|
93
|
+
/** What a stage returns with the locals it provided, or `{}` when it only returns a `Response`. */
|
|
94
|
+
export type ProvidedBy<TResult> = [Extract<TResult, Provided<any>>] extends [never] ? {} : ProvidedOf<Extract<TResult, Provided<any>>>;
|
|
95
|
+
type ProvidedOf<TResult> = TResult extends Provided<infer TLocals> ? TLocals : never;
|
|
96
|
+
type Simplify<T> = {
|
|
97
|
+
[K in keyof T]: T[K];
|
|
98
|
+
} & {};
|
|
99
|
+
/**
|
|
100
|
+
* Later keys override earlier ones, as the runtime merge does. A union on either side stays a union, so a
|
|
101
|
+
* stage that provides different keys on different paths keeps every earlier local.
|
|
102
|
+
*/
|
|
103
|
+
export type MergeLocals<TBefore, TAfter> = TBefore extends unknown ? TAfter extends unknown ? Simplify<Omit<TBefore, keyof TAfter> & TAfter> : never : never;
|
|
104
|
+
/**
|
|
105
|
+
* What a middleware of unknown locals provides, such as one typed {@link AnyEcoMiddleware}: it may overwrite any
|
|
106
|
+
* key, so it marks the fold instead of naming keys.
|
|
107
|
+
*/
|
|
108
|
+
type UnknownLocals = {
|
|
109
|
+
readonly [unknownLocals]: true;
|
|
110
|
+
};
|
|
111
|
+
/**
|
|
112
|
+
* What a made middleware leaves available after it runs; nothing for a plain function, and
|
|
113
|
+
* {@link UnknownLocals} for a middleware whose locals are `any` or an index signature.
|
|
114
|
+
*/
|
|
115
|
+
export type TotalOf<TEntry> = TEntry extends EcoMiddleware<infer TLocals> ? 0 extends 1 & TLocals ? UnknownLocals : string extends keyof TLocals ? UnknownLocals : TLocals : {};
|
|
116
|
+
/** The locals before an entry of unknown provision: every key stays, typed `unknown`, so a direct read fails. */
|
|
117
|
+
type Unproven<TAcc> = TAcc extends unknown ? {
|
|
118
|
+
[K in keyof TAcc]: unknown;
|
|
119
|
+
} : never;
|
|
120
|
+
type Step<TAcc, TTotal> = [Extract<TTotal, UnknownLocals>] extends [never] ? MergeLocals<TAcc, TTotal> : Unproven<TAcc>;
|
|
121
|
+
/**
|
|
122
|
+
* The locals after a whole list runs, in order, starting from `TAcc`, the locals a group already provided.
|
|
123
|
+
*
|
|
124
|
+
* @remarks
|
|
125
|
+
* A list whose rest is not a fixed tuple, such as a spread array, may run its elements any number of times or
|
|
126
|
+
* not at all. Its elements can overwrite a key from before, so every key they may provide takes the union of
|
|
127
|
+
* the earlier type and theirs, and a key only they provide may be missing. An entry of unknown locals, such as
|
|
128
|
+
* one typed `AnyEcoMiddleware`, in the fixed part or the rest, may overwrite every key, so each key before it
|
|
129
|
+
* becomes `unknown`: nothing before it is proven, and only a later entry proves a key again.
|
|
130
|
+
*/
|
|
131
|
+
export type ListLocals<TList extends readonly unknown[], TAcc = {}> = TList extends readonly [
|
|
132
|
+
infer THead,
|
|
133
|
+
...infer TTail
|
|
134
|
+
] ? ListLocals<TTail, Step<TAcc, TotalOf<THead>>> : TList extends readonly [] ? Simplify<TAcc> : RestLocals<TAcc, TotalOf<TList[number]>>;
|
|
135
|
+
type RestLocals<TAcc, TElement> = [Extract<TElement, UnknownLocals>] extends [never] ? MaybeOverwritten<TAcc, TElement> : Simplify<Unproven<TAcc>>;
|
|
136
|
+
type MaybeOverwritten<TAcc, TElement> = TElement extends unknown ? Simplify<Omit<TAcc, keyof TElement> & {
|
|
137
|
+
[K in keyof TElement]: K extends keyof TAcc ? TAcc[K] | TElement[K] : TElement[K] | undefined;
|
|
138
|
+
}> : never;
|
|
139
|
+
/** A middleware, or an inline stage typed from the locals before it: one input of `composeMiddleware`. */
|
|
140
|
+
export type MiddlewareStageInput<TIn extends object> = AnyEcoMiddleware | MiddlewareStage<TIn>;
|
|
141
|
+
/** The locals after one more stage or middleware. */
|
|
142
|
+
export type MiddlewareLocalsAfter<TIn extends object, TStage> = TStage extends EcoMiddleware<infer TLocals> ? MergeLocals<TIn, TLocals> : TStage extends (...args: never) => infer TResult ? MergeLocals<TIn, ProvidedBy<Awaited<TResult>>> : TIn;
|
|
143
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
export type { ApiHandler, ApiHandlerContext, BrowserForeignChildHost, EcoComponent, EcoComponentConfig, EcoComponentDependencies, DependencyAttributes, EcoChildren, EcoFunctionComponent, EcoPageComponent, EcoPageFile, EcoPagesAppConfig, EcoPagesElement, EcoPageLayoutEntry, EcoPageLayoutEntrySpec, EcoPageLayoutSpec, EcoPageLayouts, EcoHtmlComponent, EcoLayoutComponent,
|
|
1
|
+
export type { ApiHandler, ApiHandlerContext, ApiMiddlewareContext, BrowserForeignChildHost, EcoComponent, EcoComponentConfig, EcoComponentDependencies, DependencyAttributes, DynamicEcoPageComponent, DynamicPageDependenciesInput, EcoChildren, EcoFunctionComponent, EcoPageComponent, EcoPageFile, EcoPagesAppConfig, EcoPagesElement, EcoPageLayoutEntry, EcoPageLayoutEntrySpec, EcoPageLayoutSpec, EcoPageLayouts, EcoHtmlComponent, EcoLayoutComponent, ErrorHandler, ErrorPageTemplateProps, EcopagesRouteInfo, EcopagesSocket, EcopagesWebSocketHandler, FileRouteMiddleware, FileRouteMiddlewareContext, FileOwnedDependencyContribution, GetDynamicPageDependencies, GetDynamicPageDependenciesContext, GetPageDependencies, GetPageDependenciesContext, GetMetadata, GetMetadataContext, GetPaths, GetProps, GetRequestProps, HtmlTemplateProps, LayoutProps, LayoutBuildContext, LayoutPropsContext, LayoutRequestContext, Middleware, MiddlewareList, WithMiddlewareLocals, MiddlewareNext, OutgoingWebSocketMessage, IncomingWebSocketMessage, PageHeadProps, PageMetadataProps, PageBuildContext, PageParams, PageQuery, PageRequestContext, PageDependenciesInput, PageDependenciesResult, PageProps, PagePropsFor, PageRobotsMetadata, RenderContext, RenderOptions, RouteGroupBuilder, RouteHandlerContext, RouteMethod, RouteOptions, RouteSchema, SitemapConfig, StaticCacheStrategy, StaticEcoPageComponent, StandardSchema, StandardSchemaFailureResult, StandardSchemaIssue, StandardSchemaResult, StandardSchemaSuccessResult, StandardSchemaV1, StaticPageContext, PagePath, WebSocketCloseInfo, } from './contracts.js';
|
|
2
2
|
export type { ViewReference } from './view-reference.js';
|
|
3
3
|
export type { CacheConfig, CacheStrategy } from './cache-types.js';
|
|
4
|
-
export type { CallableComponentOf, ChildrenOf, ComponentOptions, Eco, EcoEmbedProps, EmbedPropsOf, HtmlOptions, LayoutOptions, LazyTrigger, PageOptions, PageOptionsBase,
|
|
4
|
+
export type { CallableComponentOf, ChildrenOf, ComponentOptions, Eco, EcoEmbedProps, EmbedPropsOf, HtmlOptions, LayoutOptions, LazyTrigger, DynamicPageOptions, PageOptions, PageOptionsBase, StaticPageOptions, PropsOf, ResultOf, } from './eco-api-types.js';
|
|
5
|
+
export type { AnyEcoMiddleware, EcoMiddleware, ListLocals, MergeLocals, MiddlewareContext, MiddlewareEntry, MiddlewareStage, ProvideNext, Provided, StageResult, } from './middleware-types.js';
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { LocalsAccessError } from '../../../errors/locals-access-error.js';
|
|
2
|
-
/**
|
|
3
|
-
* Proxy that throws when dynamic route locals are accessed on non-dynamic pages.
|
|
4
|
-
*/
|
|
5
|
-
export function createPageLocalsProxy(filePath) {
|
|
6
|
-
const errorMessage = `[ecopages] Request locals are only available during request-time rendering with cache: 'dynamic'. Page: ${filePath}. If you meant to use locals here, set cache: 'dynamic' and provide locals from route middleware/handlers.`;
|
|
7
|
-
return new Proxy({}, {
|
|
8
|
-
get: () => {
|
|
9
|
-
throw new LocalsAccessError(errorMessage);
|
|
10
|
-
},
|
|
11
|
-
set: () => {
|
|
12
|
-
throw new LocalsAccessError(errorMessage);
|
|
13
|
-
},
|
|
14
|
-
has: () => {
|
|
15
|
-
throw new LocalsAccessError(errorMessage);
|
|
16
|
-
},
|
|
17
|
-
ownKeys: () => {
|
|
18
|
-
throw new LocalsAccessError(errorMessage);
|
|
19
|
-
},
|
|
20
|
-
deleteProperty: () => {
|
|
21
|
-
throw new LocalsAccessError(errorMessage);
|
|
22
|
-
},
|
|
23
|
-
defineProperty: () => {
|
|
24
|
-
throw new LocalsAccessError(errorMessage);
|
|
25
|
-
},
|
|
26
|
-
getOwnPropertyDescriptor: () => {
|
|
27
|
-
throw new LocalsAccessError(errorMessage);
|
|
28
|
-
},
|
|
29
|
-
});
|
|
30
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import type { ApiHandlerContext } from '../types/contracts.js';
|
|
2
|
-
/**
|
|
3
|
-
* Creates a require function for validating and retrieving request locals.
|
|
4
|
-
* Supports both single key access and multiple keys with type safety.
|
|
5
|
-
*
|
|
6
|
-
* @param getLocals - Function that returns the current locals object
|
|
7
|
-
* @returns A require function that throws the onMissing response if keys are not found
|
|
8
|
-
*
|
|
9
|
-
* @example
|
|
10
|
-
* ```ts
|
|
11
|
-
* const require = createRequire(() => context.locals);
|
|
12
|
-
* const userId = require('userId', () => new Response('Unauthorized', { status: 401 }));
|
|
13
|
-
* ```
|
|
14
|
-
*/
|
|
15
|
-
export declare function createRequire(getLocals: () => Record<string, unknown>): ApiHandlerContext['require'];
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Creates a require function for validating and retrieving request locals.
|
|
3
|
-
* Supports both single key access and multiple keys with type safety.
|
|
4
|
-
*
|
|
5
|
-
* @param getLocals - Function that returns the current locals object
|
|
6
|
-
* @returns A require function that throws the onMissing response if keys are not found
|
|
7
|
-
*
|
|
8
|
-
* @example
|
|
9
|
-
* ```ts
|
|
10
|
-
* const require = createRequire(() => context.locals);
|
|
11
|
-
* const userId = require('userId', () => new Response('Unauthorized', { status: 401 }));
|
|
12
|
-
* ```
|
|
13
|
-
*/
|
|
14
|
-
export function createRequire(getLocals) {
|
|
15
|
-
return (keyOrKeys, onMissing) => {
|
|
16
|
-
const locals = getLocals();
|
|
17
|
-
if (Array.isArray(keyOrKeys)) {
|
|
18
|
-
const result = {};
|
|
19
|
-
for (const key of keyOrKeys) {
|
|
20
|
-
const value = locals[key];
|
|
21
|
-
if (value === undefined || value === null) {
|
|
22
|
-
throw onMissing();
|
|
23
|
-
}
|
|
24
|
-
result[key] = value;
|
|
25
|
-
}
|
|
26
|
-
return result;
|
|
27
|
-
}
|
|
28
|
-
const value = locals[keyOrKeys];
|
|
29
|
-
if (value === undefined || value === null) {
|
|
30
|
-
throw onMissing();
|
|
31
|
-
}
|
|
32
|
-
return value;
|
|
33
|
-
};
|
|
34
|
-
}
|