@ecopages/core 0.9.0 → 0.10.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/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
|
@@ -4,7 +4,7 @@ import { requireAppRuntime } from '../utils/app-runtime.js';
|
|
|
4
4
|
import { resolveBuildParallelism } from '../build/build-parallelism.js';
|
|
5
5
|
import { fileSystem } from '@ecopages/file-system';
|
|
6
6
|
import { CLIENT_OUTPUT_DIR } from '../utils/resolve-entry-file.js';
|
|
7
|
-
import { prepareExplicitStaticRender, resolveExplicitViewRenderer, } from '../route-renderer/explicit-view-render-preparation.js';
|
|
7
|
+
import { assertViewRendersWithoutRequest, prepareExplicitStaticRender, resolveExplicitViewRenderer, } from '../route-renderer/explicit-view-render-preparation.js';
|
|
8
8
|
import { ensureIntegrationRuntimeReady, requireBrowserBuildGlobalScriptResolution, resolveIntegrationDependencies, setupAppRuntimeIntegrations, teardownAppRuntimeIntegrations, } from '../build/app-build-manifest-runtime.js';
|
|
9
9
|
import { emitProductionServerModules, shouldBuildPagesUnifiedGraph } from '../build/cache/pages-unified-graph-build.js';
|
|
10
10
|
import { getImportedViewPaths } from '../build/server/imported-view-plugin.js';
|
|
@@ -15,6 +15,7 @@ import { toEcopagesRouteInfo } from '../utils/ecopages-route-info.js';
|
|
|
15
15
|
import { buildSegmentPath } from '../router/server/segment-path-matcher.js';
|
|
16
16
|
import { normalizePathname } from '../utils/path-pattern.js';
|
|
17
17
|
import { ErrorPageRenderer } from '../route-renderer/error-pages/error-page-renderer.js';
|
|
18
|
+
import { isDynamicPage } from '../eco/page-data-hooks.js';
|
|
18
19
|
import { SemanticErrorPageExporter, SEMANTIC_ERROR_PAGE_PATHNAMES, } from '../route-renderer/error-pages/semantic-error-page-exporter.js';
|
|
19
20
|
function createStaticGenerationPageError(route, error, label = 'static page') {
|
|
20
21
|
const cause = error instanceof Error ? error : new Error(String(error));
|
|
@@ -81,10 +82,7 @@ export class StaticSiteGenerator {
|
|
|
81
82
|
}
|
|
82
83
|
async shouldSkipStaticPageFile(filePath, routeRendererFactory) {
|
|
83
84
|
const pageModule = await routeRendererFactory.getPageRenderer(filePath).loadPageModule(filePath);
|
|
84
|
-
return pageModule.default
|
|
85
|
-
}
|
|
86
|
-
shouldSkipStaticView(_routePath, view) {
|
|
87
|
-
return view.cache === 'dynamic';
|
|
85
|
+
return isDynamicPage(pageModule.default);
|
|
88
86
|
}
|
|
89
87
|
/**
|
|
90
88
|
* Writes the robots.txt file declared by the app config.
|
|
@@ -352,6 +350,7 @@ export class StaticSiteGenerator {
|
|
|
352
350
|
...dynamicPageEntries.map((route) => ({
|
|
353
351
|
routeFile: route.templateRoute.filePath,
|
|
354
352
|
params: route.params,
|
|
353
|
+
skipPropsHook: true,
|
|
355
354
|
})),
|
|
356
355
|
...dynamicRouteTemplates,
|
|
357
356
|
...importedViews,
|
|
@@ -546,7 +545,8 @@ export class StaticSiteGenerator {
|
|
|
546
545
|
for (const route of input.staticRoutes) {
|
|
547
546
|
try {
|
|
548
547
|
const view = (await route.loader()).default;
|
|
549
|
-
|
|
548
|
+
assertViewRendersWithoutRequest(route.path, view);
|
|
549
|
+
if (isDynamicPage(view)) {
|
|
550
550
|
input.skipped.push(route.path);
|
|
551
551
|
continue;
|
|
552
552
|
}
|
package/src/types/contracts.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Readable } from 'node:stream';
|
|
2
2
|
import type { ComponentIdentity } from './component-identity.js';
|
|
3
|
+
import type { ApiMiddlewareEntry, ListLocals, PageMiddlewareEntry } from './middleware-types.js';
|
|
3
4
|
import type { ViewReference } from './view-reference.js';
|
|
4
5
|
import type { ApiResponseBuilder } from './api-response.js';
|
|
5
6
|
import type { AssetDefinition, ProcessedAsset } from './asset-types.js';
|
|
@@ -18,8 +19,6 @@ export type HmrStrategyContract = {
|
|
|
18
19
|
ownsDevTransformEntrypoint(sourcePath: string, variant?: string): boolean;
|
|
19
20
|
process(filePath: string): Promise<HmrAction>;
|
|
20
21
|
};
|
|
21
|
-
type RequiresKeys = keyof RequestLocals;
|
|
22
|
-
export type PageRequires<K extends RequiresKeys = RequiresKeys> = K | readonly K[];
|
|
23
22
|
export type GetPageDependenciesContext<T = Record<string, unknown>> = {
|
|
24
23
|
props: PagePropsFor<T>;
|
|
25
24
|
params?: PageParams;
|
|
@@ -33,6 +32,16 @@ export type PageDependenciesResult = FileOwnedDependencyContribution & {
|
|
|
33
32
|
};
|
|
34
33
|
export type GetPageDependencies<T = Record<string, unknown>> = (context: GetPageDependenciesContext<T>) => PageDependenciesResult | undefined | Promise<PageDependenciesResult | undefined>;
|
|
35
34
|
export type PageDependenciesInput<T = Record<string, unknown>> = EcoComponentDependencies | GetPageDependencies<T>;
|
|
35
|
+
/**
|
|
36
|
+
* What a Dynamic Page's dependency resolver receives.
|
|
37
|
+
*
|
|
38
|
+
* @remarks
|
|
39
|
+
* It has no `props`: the build plans a Dynamic Page without running its `props` hook, and the Page Browser Graph
|
|
40
|
+
* is cached by Params and Query alone, so request data must not decide its dependencies.
|
|
41
|
+
*/
|
|
42
|
+
export type GetDynamicPageDependenciesContext = Omit<GetPageDependenciesContext, 'props'>;
|
|
43
|
+
export type GetDynamicPageDependencies = (context: GetDynamicPageDependenciesContext) => PageDependenciesResult | undefined | Promise<PageDependenciesResult | undefined>;
|
|
44
|
+
export type DynamicPageDependenciesInput = EcoComponentDependencies | GetDynamicPageDependencies;
|
|
36
45
|
/**
|
|
37
46
|
* Runtime-agnostic incoming WebSocket frame.
|
|
38
47
|
*
|
|
@@ -73,7 +82,7 @@ interface WebSocketContextFactoryInput<TParams extends Record<string, string> =
|
|
|
73
82
|
readonly kind: string;
|
|
74
83
|
readonly params: TParams;
|
|
75
84
|
readonly search: Readonly<PageQuery>;
|
|
76
|
-
readonly locals?:
|
|
85
|
+
readonly locals?: Record<string, unknown>;
|
|
77
86
|
}
|
|
78
87
|
/**
|
|
79
88
|
* Runtime-agnostic WebSocket socket view exposed to handlers.
|
|
@@ -542,9 +551,7 @@ export interface BrowserForeignChildHost {
|
|
|
542
551
|
/**
|
|
543
552
|
* Represents a page in EcoPages.
|
|
544
553
|
*/
|
|
545
|
-
export type PageProps<T = unknown> = T & StaticPageContext
|
|
546
|
-
locals?: RequestLocals;
|
|
547
|
-
};
|
|
554
|
+
export type PageProps<T = unknown> = T & StaticPageContext;
|
|
548
555
|
/**
|
|
549
556
|
* Page-level robots directives for the document head and sitemap filtering.
|
|
550
557
|
*
|
|
@@ -622,7 +629,7 @@ export interface PageHeadProps<T = EcoPagesElement> {
|
|
|
622
629
|
/**
|
|
623
630
|
* Represents the props for a route layout.
|
|
624
631
|
*/
|
|
625
|
-
export interface LayoutProps<T = EcoPagesElement>
|
|
632
|
+
export interface LayoutProps<T = EcoPagesElement> {
|
|
626
633
|
children: T;
|
|
627
634
|
}
|
|
628
635
|
/**
|
|
@@ -644,26 +651,49 @@ export interface HtmlTemplateProps<T = EcoPagesElement> extends PageHeadProps<T>
|
|
|
644
651
|
pageModuleUrl?: string;
|
|
645
652
|
}
|
|
646
653
|
/**
|
|
647
|
-
*
|
|
654
|
+
* A layout factory context whose `params`, `query` and `locals` are all optional, and whose `locals` has no keys.
|
|
655
|
+
*
|
|
656
|
+
* @remarks
|
|
657
|
+
* A shared layout entry whose factory takes it is accepted on any Page. An entry that reads locals annotates the
|
|
658
|
+
* ones it reads on its factory parameter instead, so only a Page whose middleware provides them accepts it. A
|
|
659
|
+
* factory written in `eco.page({ layout })` is typed with {@link LayoutRequestContext} or {@link LayoutBuildContext},
|
|
660
|
+
* from the Page's Cache Strategy and middleware list.
|
|
648
661
|
*/
|
|
649
662
|
export type LayoutPropsContext = {
|
|
650
663
|
params?: PageParams;
|
|
651
664
|
query?: PageQuery;
|
|
652
|
-
locals?:
|
|
665
|
+
locals?: {};
|
|
666
|
+
};
|
|
667
|
+
/**
|
|
668
|
+
* What a layout entry's `props` factory receives on a Static or revalidating Page: no request exists, so it has no
|
|
669
|
+
* `locals`.
|
|
670
|
+
*/
|
|
671
|
+
export type LayoutBuildContext = Omit<LayoutPropsContext, 'locals'>;
|
|
672
|
+
/**
|
|
673
|
+
* What a layout entry's `props` factory receives on a Dynamic Page.
|
|
674
|
+
*
|
|
675
|
+
* @typeParam TLocals - The Locals the Page's middleware provided, typed from its `middleware` list.
|
|
676
|
+
*/
|
|
677
|
+
export type LayoutRequestContext<TLocals = {}> = LayoutBuildContext & {
|
|
678
|
+
readonly locals: Readonly<TLocals>;
|
|
653
679
|
};
|
|
654
680
|
/**
|
|
655
681
|
* One normalized layout tier in a page layout stack (outer→inner), as stored on page config.
|
|
656
682
|
*/
|
|
657
683
|
export type EcoPageLayoutEntry = {
|
|
658
684
|
component: EcoComponent;
|
|
659
|
-
props?: (context:
|
|
685
|
+
props?: (context: LayoutRequestContext) => Record<string, unknown>;
|
|
660
686
|
};
|
|
661
687
|
/**
|
|
662
688
|
* A layout tier with a props factory, as written in `eco.page({ layout })`.
|
|
689
|
+
*
|
|
690
|
+
* @typeParam TContext - What the factory receives. `eco.page()` sets it from the Page, so a factory written inline
|
|
691
|
+
* reads the Page's typed `locals`; an entry shared by many Pages annotates the `locals` it reads on the factory
|
|
692
|
+
* parameter, and a Page whose middleware does not provide them does not compile.
|
|
663
693
|
*/
|
|
664
|
-
export type EcoPageLayoutEntrySpec<E = EcoPagesElement> = {
|
|
694
|
+
export type EcoPageLayoutEntrySpec<E = EcoPagesElement, TContext = LayoutPropsContext> = {
|
|
665
695
|
component: EcoLayoutComponent<E>;
|
|
666
|
-
props?: (context:
|
|
696
|
+
props?: (context: TContext) => Record<string, unknown>;
|
|
667
697
|
};
|
|
668
698
|
/**
|
|
669
699
|
* Layout declaration accepted by `eco.page()`: an `eco.layout()` result, a plain function typed with
|
|
@@ -674,7 +704,7 @@ export type EcoPageLayoutEntrySpec<E = EcoPagesElement> = {
|
|
|
674
704
|
* still assignable, so the type cannot prove that a layout renders `children`; layout composition checks that
|
|
675
705
|
* when it renders the page.
|
|
676
706
|
*/
|
|
677
|
-
export type EcoPageLayoutSpec<E = EcoPagesElement> = EcoLayoutComponent<E> | EcoPageLayoutEntrySpec<E>;
|
|
707
|
+
export type EcoPageLayoutSpec<E = EcoPagesElement, TContext = LayoutPropsContext> = EcoLayoutComponent<E> | EcoPageLayoutEntrySpec<E, TContext>;
|
|
678
708
|
/**
|
|
679
709
|
* One or more layout tiers for `eco.page({ layout })`.
|
|
680
710
|
*
|
|
@@ -682,7 +712,7 @@ export type EcoPageLayoutSpec<E = EcoPagesElement> = EcoLayoutComponent<E> | Eco
|
|
|
682
712
|
* When an array, order is **outer → inner** (outermost layout wraps all inner tiers).
|
|
683
713
|
* Normalized at factory time to `config.layouts` and `config.layoutEntries`.
|
|
684
714
|
*/
|
|
685
|
-
export type EcoPageLayouts<E = EcoPagesElement> = EcoPageLayoutSpec<E> | EcoPageLayoutSpec<E>[];
|
|
715
|
+
export type EcoPageLayouts<E = EcoPagesElement, TContext = LayoutPropsContext> = EcoPageLayoutSpec<E, TContext> | EcoPageLayoutSpec<E, TContext>[];
|
|
686
716
|
/**
|
|
687
717
|
* A layout component: an `eco.layout()` result or a plain function that takes {@link LayoutProps}.
|
|
688
718
|
*/
|
|
@@ -697,8 +727,8 @@ export type EcoHtmlComponent<T = EcoPagesElement> = EcoComponent<HtmlTemplatePro
|
|
|
697
727
|
* @remarks
|
|
698
728
|
* `status` is the HTTP status being rendered. `message` is the public `HttpError`
|
|
699
729
|
* text for 4xx page-pipeline failures; unmatched URLs omit it. `stack` is only
|
|
700
|
-
* passed for 5xx pages in development. Semantic error pages
|
|
701
|
-
*
|
|
730
|
+
* passed for 5xx pages in development. Semantic error pages render outside the page middleware pipeline, so their
|
|
731
|
+
* layout factories receive empty `locals`.
|
|
702
732
|
*/
|
|
703
733
|
export interface ErrorPageTemplateProps extends Omit<HtmlTemplateProps, 'children'> {
|
|
704
734
|
status: number;
|
|
@@ -722,17 +752,6 @@ export type PageParams = Record<string, string | string[]>;
|
|
|
722
752
|
* `z.union([z.string(), z.array(z.string())])`.
|
|
723
753
|
*/
|
|
724
754
|
export type PageQuery = Record<string, string | string[]>;
|
|
725
|
-
/**
|
|
726
|
-
* Request-scoped data that is only available during request-time rendering.
|
|
727
|
-
*
|
|
728
|
-
* Apps should augment this interface via module augmentation:
|
|
729
|
-
*
|
|
730
|
-
* declare module '@ecopages/core' {
|
|
731
|
-
* interface RequestLocals { session?: Session | null }
|
|
732
|
-
* }
|
|
733
|
-
*/
|
|
734
|
-
export interface RequestLocals {
|
|
735
|
-
}
|
|
736
755
|
/**
|
|
737
756
|
* Represents the context object for a static page.
|
|
738
757
|
*/
|
|
@@ -740,29 +759,6 @@ export type StaticPageContext = {
|
|
|
740
759
|
params?: PageParams;
|
|
741
760
|
query?: PageQuery;
|
|
742
761
|
};
|
|
743
|
-
/**
|
|
744
|
-
* Request-time page context.
|
|
745
|
-
*
|
|
746
|
-
* This is only populated during SSR. Static generation must not access locals.
|
|
747
|
-
*/
|
|
748
|
-
export type RequestPageContext = {
|
|
749
|
-
locals: RequestLocals;
|
|
750
|
-
};
|
|
751
|
-
/**
|
|
752
|
-
* Adds optional `locals` to a props type.
|
|
753
|
-
* Primarily used for layouts that may receive request-scoped data from middleware.
|
|
754
|
-
*
|
|
755
|
-
* @template P - The base props type
|
|
756
|
-
*
|
|
757
|
-
* @example
|
|
758
|
-
* ```ts
|
|
759
|
-
* type MyLayoutProps = WithLocals<{ children: ReactNode }>;
|
|
760
|
-
* // { children: ReactNode; locals?: RequestLocals }
|
|
761
|
-
* ```
|
|
762
|
-
*/
|
|
763
|
-
export type WithLocals<P> = P & {
|
|
764
|
-
locals?: RequestLocals;
|
|
765
|
-
};
|
|
766
762
|
/**
|
|
767
763
|
* One page that `paths` lists at build.
|
|
768
764
|
*/
|
|
@@ -813,15 +809,43 @@ export type PagePropsFor<T> = T extends GetProps<infer P> ? P & {
|
|
|
813
809
|
params?: PageParams;
|
|
814
810
|
query?: PageQuery;
|
|
815
811
|
};
|
|
816
|
-
|
|
812
|
+
type PageRenderComponent<T> = EcoComponent<PagePropsFor<T>>;
|
|
813
|
+
type PageStaticsBase<T> = {
|
|
817
814
|
paths?: GetPaths;
|
|
818
|
-
props?: GetProps<T>;
|
|
819
815
|
metadata?: GetMetadata<T>;
|
|
816
|
+
middleware?: readonly PageMiddlewareEntry[];
|
|
817
|
+
};
|
|
818
|
+
/**
|
|
819
|
+
* A Cache Strategy that stores the rendered HTML, so no request exists when the `props` hook runs.
|
|
820
|
+
*/
|
|
821
|
+
export type StaticCacheStrategy = Exclude<CacheStrategy, 'dynamic'>;
|
|
822
|
+
/**
|
|
823
|
+
* What `eco.page()` keeps on a Static or revalidating Page: its `props` hook receives the build context.
|
|
824
|
+
*/
|
|
825
|
+
export type StaticPageStatics<T> = PageStaticsBase<T> & {
|
|
826
|
+
cache?: StaticCacheStrategy;
|
|
827
|
+
props?: GetProps<T>;
|
|
820
828
|
resolveDependencies?: GetPageDependencies<T>;
|
|
821
|
-
cache?: CacheStrategy;
|
|
822
|
-
requires?: PageRequires;
|
|
823
|
-
middleware?: FileRouteMiddleware[];
|
|
824
829
|
};
|
|
830
|
+
/**
|
|
831
|
+
* What `eco.page()` keeps on a Dynamic Page: its `props` hook is the request handler and receives the request
|
|
832
|
+
* context.
|
|
833
|
+
*/
|
|
834
|
+
export type DynamicPageStatics<T> = PageStaticsBase<T> & {
|
|
835
|
+
cache: 'dynamic';
|
|
836
|
+
props?: GetRequestProps<T>;
|
|
837
|
+
resolveDependencies?: GetDynamicPageDependencies;
|
|
838
|
+
};
|
|
839
|
+
export type StaticEcoPageComponent<T> = PageRenderComponent<T> & StaticPageStatics<T>;
|
|
840
|
+
export type DynamicEcoPageComponent<T> = PageRenderComponent<T> & DynamicPageStatics<T>;
|
|
841
|
+
/**
|
|
842
|
+
* A Page as `eco.page()` returns it.
|
|
843
|
+
*
|
|
844
|
+
* @remarks
|
|
845
|
+
* A union on `cache`, so a caller branches on it before calling the `props` or dependency hook, which receive
|
|
846
|
+
* different contexts.
|
|
847
|
+
*/
|
|
848
|
+
export type EcoPageComponent<T> = StaticEcoPageComponent<T> | DynamicEcoPageComponent<T>;
|
|
825
849
|
/**
|
|
826
850
|
* The function that lists the Params a page is known for at build time.
|
|
827
851
|
*
|
|
@@ -844,24 +868,65 @@ export type GetMetadataContext<T = Record<string, unknown>> = Required<StaticPag
|
|
|
844
868
|
*/
|
|
845
869
|
export type GetMetadata<T = Record<string, unknown>> = (context: GetMetadataContext<T>) => PageMetadataProps | Promise<PageMetadataProps>;
|
|
846
870
|
/**
|
|
847
|
-
*
|
|
848
|
-
*
|
|
849
|
-
* @remarks
|
|
850
|
-
* Returns the props object directly. `paths` lists the Params known at build; `props` receives Params,
|
|
851
|
-
* from `paths` at build and from the request at run time.
|
|
871
|
+
* What the `props` hook of a Static or revalidating Page receives. No request exists when it runs: at build, and
|
|
872
|
+
* when a revalidating Page regenerates.
|
|
852
873
|
*/
|
|
853
|
-
export type
|
|
874
|
+
export type PageBuildContext = {
|
|
854
875
|
params: PageParams;
|
|
855
876
|
appConfig: EcoPagesAppConfig;
|
|
856
877
|
runtimeOrigin: string;
|
|
857
|
-
}
|
|
878
|
+
};
|
|
879
|
+
/**
|
|
880
|
+
* What the `props` hook of a Dynamic Page receives on each request.
|
|
881
|
+
*
|
|
882
|
+
* @typeParam TLocals - The Locals the Page's middleware provided, typed from its `middleware` list.
|
|
883
|
+
*/
|
|
884
|
+
export type PageRequestContext<TLocals = {}> = PageBuildContext & {
|
|
885
|
+
request: Request;
|
|
886
|
+
query: PageQuery;
|
|
887
|
+
readonly locals: Readonly<TLocals>;
|
|
888
|
+
};
|
|
889
|
+
/**
|
|
890
|
+
* The `props` hook of a Static or revalidating Page.
|
|
891
|
+
*
|
|
892
|
+
* @remarks
|
|
893
|
+
* Returns the props object directly. `paths` lists the Params known at build, and the hook receives each of them.
|
|
894
|
+
*/
|
|
895
|
+
export type GetProps<T> = ((context: PageBuildContext) => T | Promise<T>) & ((context: PageBuildContext) => WithoutRouteProps<T> | Promise<WithoutRouteProps<T>>);
|
|
896
|
+
/**
|
|
897
|
+
* Keys a Page's `props` hook may not return.
|
|
898
|
+
*
|
|
899
|
+
* @remarks
|
|
900
|
+
* The route sets them on what `render` receives, after the props, so a prop of the same name would never reach
|
|
901
|
+
* `render` while `metadata` read it.
|
|
902
|
+
*/
|
|
903
|
+
export type NoRouteProps = {
|
|
904
|
+
params?: never;
|
|
905
|
+
query?: never;
|
|
906
|
+
};
|
|
907
|
+
/**
|
|
908
|
+
* `T` with every object member barred from {@link NoRouteProps} keys.
|
|
909
|
+
*
|
|
910
|
+
* @remarks
|
|
911
|
+
* The props hook types pair it with a plain `T | Promise<T>` signature that infers `T`: inferring through this
|
|
912
|
+
* conditional would take one branch of a union return, and `null` or `undefined` returns pass unchanged. The
|
|
913
|
+
* conditional stays deferred for a type parameter, so a hook that returns a caller's generic `T` does not type-check
|
|
914
|
+
* against it; such a wrapper passes a concrete props type.
|
|
915
|
+
*/
|
|
916
|
+
type WithoutRouteProps<T> = T extends object ? T & NoRouteProps : T;
|
|
917
|
+
/**
|
|
918
|
+
* The `props` hook of a Dynamic Page: the Page's request handler, run on every request and never by the build.
|
|
919
|
+
*/
|
|
920
|
+
export type GetRequestProps<T, TLocals = {}> = ((context: PageRequestContext<TLocals>) => T | Promise<T>) & ((context: PageRequestContext<TLocals>) => WithoutRouteProps<T> | Promise<WithoutRouteProps<T>>);
|
|
858
921
|
/**
|
|
859
922
|
* Represents a page file in EcoPages.
|
|
923
|
+
*
|
|
924
|
+
* @remarks
|
|
925
|
+
* The Page declares its Cache Strategy on its default export; a module that still exports `cache` fails to load.
|
|
860
926
|
* @template T - The type of the page props.
|
|
861
927
|
*/
|
|
862
928
|
export type EcoPageFile<T = unknown> = T & {
|
|
863
929
|
default: EcoComponent<any, any>;
|
|
864
|
-
cache?: CacheStrategy;
|
|
865
930
|
};
|
|
866
931
|
/**
|
|
867
932
|
* The options for the route renderer.
|
|
@@ -870,7 +935,15 @@ export type RouteRendererOptions = {
|
|
|
870
935
|
file: string;
|
|
871
936
|
params?: PageParams;
|
|
872
937
|
query?: PageQuery;
|
|
873
|
-
locals
|
|
938
|
+
/** The request's locals store, as the Page's middleware left it. */
|
|
939
|
+
locals?: Record<string, unknown>;
|
|
940
|
+
/**
|
|
941
|
+
* The incoming request, set only for a request-time render of a Dynamic Page.
|
|
942
|
+
*
|
|
943
|
+
* @remarks
|
|
944
|
+
* A Dynamic Page's `props` hook receives it with `query` and `locals`, and cannot run without it.
|
|
945
|
+
*/
|
|
946
|
+
request?: Request;
|
|
874
947
|
/**
|
|
875
948
|
* Preloaded page module from request-time inspection.
|
|
876
949
|
*
|
|
@@ -948,7 +1021,6 @@ export type IntegrationRendererRenderOptions<C = EcoPagesElement> = RouteRendere
|
|
|
948
1021
|
pagePackage?: PagePackageResult;
|
|
949
1022
|
pageProps?: Record<string, unknown>;
|
|
950
1023
|
cacheStrategy?: CacheStrategy;
|
|
951
|
-
pageLocals?: RequestLocals;
|
|
952
1024
|
};
|
|
953
1025
|
export type InternalIntegrationRendererRenderOptions<C = EcoPagesElement> = IntegrationRendererRenderOptions<C> & {
|
|
954
1026
|
sourceDependencyPaths?: readonly string[];
|
|
@@ -1154,19 +1226,9 @@ export interface ApiHandlerContext<TRequest extends Request = Request, TServer =
|
|
|
1154
1226
|
response: ApiResponseBuilder;
|
|
1155
1227
|
server: TServer;
|
|
1156
1228
|
/**
|
|
1157
|
-
*
|
|
1158
|
-
* Only valid during request-time handling (SSR/API). Must not be used for static generation.
|
|
1229
|
+
* The locals the route's middleware provided, typed from its `middleware` list. Without one it has no keys.
|
|
1159
1230
|
*/
|
|
1160
|
-
locals:
|
|
1161
|
-
/**
|
|
1162
|
-
* Require one or more locals keys. If missing, executes `onMissing` to produce a terminating Response.
|
|
1163
|
-
*/
|
|
1164
|
-
require: {
|
|
1165
|
-
<K extends keyof RequestLocals>(key: K, onMissing: () => Response): Exclude<RequestLocals[K], null | undefined>;
|
|
1166
|
-
<K extends keyof RequestLocals>(keys: readonly K[], onMissing: () => Response): {
|
|
1167
|
-
[P in K]-?: Exclude<RequestLocals[P], null | undefined>;
|
|
1168
|
-
};
|
|
1169
|
-
};
|
|
1231
|
+
locals: {};
|
|
1170
1232
|
/**
|
|
1171
1233
|
* Services available to the API handler.
|
|
1172
1234
|
*/
|
|
@@ -1300,6 +1362,20 @@ export interface ApiHandlerContext<TRequest extends Request = Request, TServer =
|
|
|
1300
1362
|
*/
|
|
1301
1363
|
headers?: unknown;
|
|
1302
1364
|
}
|
|
1365
|
+
/**
|
|
1366
|
+
* The context a plain API middleware function receives.
|
|
1367
|
+
*/
|
|
1368
|
+
export interface ApiMiddlewareContext<TRequest extends Request = Request, TServer = any> extends Omit<ApiHandlerContext<TRequest, TServer>, 'locals'> {
|
|
1369
|
+
/**
|
|
1370
|
+
* The request's locals store.
|
|
1371
|
+
*
|
|
1372
|
+
* @remarks
|
|
1373
|
+
* A plain function may write a new key here and the later stages and the handler see it at runtime, but no type
|
|
1374
|
+
* carries what it writes: they read only what a `createMiddleware` stage provides. Overwriting or deleting a key
|
|
1375
|
+
* a stage provided with `next` makes this function's `next` throw, since readers are typed for that value.
|
|
1376
|
+
*/
|
|
1377
|
+
locals: Record<string, unknown>;
|
|
1378
|
+
}
|
|
1303
1379
|
/**
|
|
1304
1380
|
* Context available to file-route page middleware.
|
|
1305
1381
|
*
|
|
@@ -1307,68 +1383,37 @@ export interface ApiHandlerContext<TRequest extends Request = Request, TServer =
|
|
|
1307
1383
|
* response helpers, but final document rendering stays owned by the page route
|
|
1308
1384
|
* execution path. The query string is not parsed for page middleware; read it from `request.url`.
|
|
1309
1385
|
*/
|
|
1310
|
-
export interface FileRouteMiddlewareContext<TRequest extends Request = Request, TServer = any> extends Omit<
|
|
1386
|
+
export interface FileRouteMiddlewareContext<TRequest extends Request = Request, TServer = any> extends Omit<ApiMiddlewareContext<TRequest, TServer>, 'render' | 'renderPartial' | 'query'> {
|
|
1311
1387
|
}
|
|
1312
1388
|
/**
|
|
1313
|
-
*
|
|
1314
|
-
*
|
|
1389
|
+
* The `next` of a plain middleware function. Call it to continue to the next middleware or the handler.
|
|
1390
|
+
*
|
|
1391
|
+
* @remarks
|
|
1392
|
+
* A plain function provides no locals. To provide typed locals to later stages and the handler, make the
|
|
1393
|
+
* middleware with `createMiddleware`.
|
|
1315
1394
|
*/
|
|
1316
1395
|
export type MiddlewareNext = () => Promise<Response>;
|
|
1317
1396
|
/**
|
|
1318
|
-
*
|
|
1397
|
+
* A plain middleware function for API routes.
|
|
1319
1398
|
* Receives the request context and a next function to continue the chain.
|
|
1320
1399
|
* Can short-circuit by returning a Response directly without calling next().
|
|
1321
1400
|
*
|
|
1322
1401
|
* @typeParam TRequest - Request type
|
|
1323
1402
|
* @typeParam TServer - Server type
|
|
1324
|
-
* @typeParam TContext - Extended context type, defaults to base ApiHandlerContext
|
|
1325
1403
|
*
|
|
1326
|
-
* @example
|
|
1404
|
+
* @example
|
|
1327
1405
|
* ```typescript
|
|
1328
1406
|
* const loggingMiddleware: Middleware = async (ctx, next) => {
|
|
1329
1407
|
* console.log(`${ctx.request.method} ${ctx.request.url}`);
|
|
1330
1408
|
* return next();
|
|
1331
1409
|
* };
|
|
1332
1410
|
* ```
|
|
1333
|
-
*
|
|
1334
|
-
* @example Middleware with extended context (use EcoMiddleware helper)
|
|
1335
|
-
* ```typescript
|
|
1336
|
-
* type AuthContext = ApiHandlerContext<Request, unknown> & { user: User };
|
|
1337
|
-
*
|
|
1338
|
-
* const authMiddleware: EcoMiddleware<AuthContext> = async (ctx, next) => {
|
|
1339
|
-
* const user = await authenticate(ctx.request);
|
|
1340
|
-
* if (!user) return ctx.response.status(401).json({ error: 'Unauthorized' });
|
|
1341
|
-
* ctx.user = user;
|
|
1342
|
-
* return next();
|
|
1343
|
-
* };
|
|
1344
|
-
* ```
|
|
1345
1411
|
*/
|
|
1346
|
-
export type Middleware<TRequest extends Request = Request, TServer = any
|
|
1412
|
+
export type Middleware<TRequest extends Request = Request, TServer = any> = (context: ApiMiddlewareContext<TRequest, TServer>, next: MiddlewareNext) => Promise<Response> | Response;
|
|
1347
1413
|
/**
|
|
1348
|
-
*
|
|
1414
|
+
* A plain middleware function for file-based page routes.
|
|
1349
1415
|
*/
|
|
1350
|
-
export type FileRouteMiddleware<TRequest extends Request = Request, TServer = any
|
|
1351
|
-
/**
|
|
1352
|
-
* Helper type for defining middleware with extended context.
|
|
1353
|
-
* Automatically infers TRequest and TServer from the provided context type.
|
|
1354
|
-
*
|
|
1355
|
-
* @typeParam TContext - The extended context type that includes TRequest and TServer
|
|
1356
|
-
*
|
|
1357
|
-
* @example
|
|
1358
|
-
* ```typescript
|
|
1359
|
-
* type AuthContext = ApiHandlerContext<Request, unknown> & {
|
|
1360
|
-
* session: { user: User };
|
|
1361
|
-
* };
|
|
1362
|
-
*
|
|
1363
|
-
* const authMiddleware: EcoMiddleware<AuthContext> = async (ctx, next) => {
|
|
1364
|
-
* const session = await authenticate(ctx.request);
|
|
1365
|
-
* if (!session) return Response.redirect('/login');
|
|
1366
|
-
* ctx.session = session;
|
|
1367
|
-
* return next();
|
|
1368
|
-
* };
|
|
1369
|
-
* ```
|
|
1370
|
-
*/
|
|
1371
|
-
export type EcoMiddleware<TContext extends ApiHandlerContext<any, any>> = TContext extends ApiHandlerContext<infer TRequest, infer TServer> ? Middleware<TRequest, TServer, TContext> : never;
|
|
1416
|
+
export type FileRouteMiddleware<TRequest extends Request = Request, TServer = any> = (context: FileRouteMiddlewareContext<TRequest, TServer>, next: MiddlewareNext) => Promise<Response> | Response;
|
|
1372
1417
|
/**
|
|
1373
1418
|
* The request methods an `ApiHandler` answers, in the order an endpoint's `Allow` header lists them.
|
|
1374
1419
|
*/
|
|
@@ -1443,7 +1488,7 @@ export interface ApiHandler<TPath extends string = string, TRequest extends Requ
|
|
|
1443
1488
|
method: ApiHandlerMethod;
|
|
1444
1489
|
handler: (context: ApiHandlerContext<TRequest, TServer>) => Promise<Response> | Response;
|
|
1445
1490
|
/** Optional middleware chain executed before the handler */
|
|
1446
|
-
middleware?: MiddlewareList
|
|
1491
|
+
middleware?: MiddlewareList;
|
|
1447
1492
|
/** Optional validation schemas for request body, query parameters, and headers */
|
|
1448
1493
|
schema?: {
|
|
1449
1494
|
body?: StandardSchema;
|
|
@@ -1464,34 +1509,45 @@ export type ErrorHandler<TRequest extends Request = Request, TServer = any> = (e
|
|
|
1464
1509
|
* @typeParam TMiddleware - The route's middleware; inferred as a tuple so the handler sees what each one adds
|
|
1465
1510
|
* @typeParam TSchema - The route's schema; each field it declares types the matching context field
|
|
1466
1511
|
*/
|
|
1467
|
-
export interface RouteOptions<
|
|
1512
|
+
export interface RouteOptions<TSchema extends RouteSchema = RouteSchema, TMiddleware extends MiddlewareList = MiddlewareList> {
|
|
1468
1513
|
middleware?: TMiddleware;
|
|
1469
1514
|
schema?: TSchema;
|
|
1470
1515
|
}
|
|
1471
1516
|
/**
|
|
1472
|
-
* A middleware list
|
|
1517
|
+
* A middleware list: middleware made by `createMiddleware` or `composeMiddleware`, and plain functions.
|
|
1473
1518
|
*
|
|
1474
1519
|
* @remarks
|
|
1475
|
-
*
|
|
1476
|
-
*
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
export type MiddlewareList<TRequest extends Request = Request, TServer = any> = readonly {
|
|
1483
|
-
middleware(context: ApiHandlerContext<TRequest, TServer>, next: MiddlewareNext): Promise<Response> | Response;
|
|
1484
|
-
}['middleware'][];
|
|
1485
|
-
/**
|
|
1486
|
-
* The context a list of middleware adds: the intersection of the context each one declares.
|
|
1520
|
+
* Declare it as a literal list or a `const` tuple, so each entry's locals are read in order. A list typed as a
|
|
1521
|
+
* plain array provides no locals. Put `middleware` before the handler so its locals type the handler.
|
|
1522
|
+
*/
|
|
1523
|
+
export type MiddlewareList = readonly ApiMiddlewareEntry[];
|
|
1524
|
+
declare const noMiddlewareList: unique symbol;
|
|
1525
|
+
/**
|
|
1526
|
+
* The list a route, group or handler infers when it declares no `middleware`.
|
|
1487
1527
|
*
|
|
1488
1528
|
* @remarks
|
|
1489
|
-
*
|
|
1490
|
-
*
|
|
1529
|
+
* It must stay a supertype of an inline list, so the `(ctx, next) => ...` entries of one take their parameter
|
|
1530
|
+
* types from it, and it must differ from `MiddlewareList`, so a list annotated `MiddlewareList` by hand folds as a
|
|
1531
|
+
* plain array of unknown locals instead of keeping the group's keys. The optional brand gives both: every list is
|
|
1532
|
+
* assignable to it, and only this type is identical to it.
|
|
1491
1533
|
*/
|
|
1492
|
-
export type
|
|
1493
|
-
|
|
1494
|
-
|
|
1534
|
+
export type NoMiddlewareList = MiddlewareList & {
|
|
1535
|
+
readonly [noMiddlewareList]?: true;
|
|
1536
|
+
};
|
|
1537
|
+
/**
|
|
1538
|
+
* A context with the locals a middleware list provides, as seen by a handler or a group.
|
|
1539
|
+
*
|
|
1540
|
+
* @remarks
|
|
1541
|
+
* Later entries override earlier ones, and a route continues the locals of its group: the route's list folds onto
|
|
1542
|
+
* them, so a route entry of unknown locals leaves no group key proven either. A route that declares no list
|
|
1543
|
+
* infers {@link NoMiddlewareList} and keeps its group's locals.
|
|
1544
|
+
*/
|
|
1545
|
+
export type WithMiddlewareLocals<TContext extends {
|
|
1546
|
+
readonly locals: object;
|
|
1547
|
+
}, TList extends readonly unknown[]> = Omit<TContext, 'locals'> & {
|
|
1548
|
+
readonly locals: IsNoList<TList> extends true ? TContext['locals'] : ListLocals<TList, TContext['locals']>;
|
|
1549
|
+
};
|
|
1550
|
+
type IsNoList<TList> = (<T>() => T extends TList ? 1 : 2) extends <T>() => T extends NoMiddlewareList ? 1 : 2 ? true : false;
|
|
1495
1551
|
export interface RouteSchema {
|
|
1496
1552
|
body?: StandardSchema;
|
|
1497
1553
|
query?: StandardSchema;
|
|
@@ -1528,7 +1584,7 @@ type RouteSchemaField = keyof RouteSchema;
|
|
|
1528
1584
|
* `TRouteContext` has no inference site; passing it explicitly types a context that the route's
|
|
1529
1585
|
* middleware list does not declare.
|
|
1530
1586
|
*/
|
|
1531
|
-
export type RouteMethod<TReturn, TRequest extends Request = Request, TServer = any, TContext extends ApiHandlerContext<TRequest, TServer> = ApiHandlerContext<TRequest, TServer>> = <TSchema extends RouteSchema = RouteSchema, TRouteContext extends TContext = TContext, const TMiddleware extends MiddlewareList
|
|
1587
|
+
export type RouteMethod<TReturn, TRequest extends Request = Request, TServer = any, TContext extends ApiHandlerContext<TRequest, TServer> = ApiHandlerContext<TRequest, TServer>> = <TSchema extends RouteSchema = RouteSchema, TRouteContext extends TContext = TContext, const TMiddleware extends MiddlewareList = NoMiddlewareList>(path: string, handler: (context: RouteHandlerContext<TSchema, WithMiddlewareLocals<TRouteContext, TMiddleware>>) => Promise<Response> | Response, options?: RouteOptions<TSchema, TMiddleware>) => TReturn;
|
|
1532
1588
|
/**
|
|
1533
1589
|
* Builder for defining routes within a group, one method per HTTP method.
|
|
1534
1590
|
* Provides chainable methods for registering routes with shared prefix and middleware.
|