@ecopages/core 0.2.0-alpha.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/CHANGELOG.md +89 -0
- package/LICENSE +21 -0
- package/README.md +32 -0
- package/package.json +279 -0
- package/src/adapters/abstract/application-adapter.d.ts +168 -0
- package/src/adapters/abstract/application-adapter.js +109 -0
- package/src/adapters/abstract/application-adapter.ts +337 -0
- package/src/adapters/abstract/router-adapter.d.ts +26 -0
- package/src/adapters/abstract/router-adapter.js +5 -0
- package/src/adapters/abstract/router-adapter.ts +30 -0
- package/src/adapters/abstract/server-adapter.d.ts +69 -0
- package/src/adapters/abstract/server-adapter.js +15 -0
- package/src/adapters/abstract/server-adapter.ts +79 -0
- package/src/adapters/bun/client-bridge.d.ts +34 -0
- package/src/adapters/bun/client-bridge.js +48 -0
- package/src/adapters/bun/client-bridge.ts +62 -0
- package/src/adapters/bun/create-app.d.ts +60 -0
- package/src/adapters/bun/create-app.js +117 -0
- package/src/adapters/bun/create-app.ts +189 -0
- package/src/adapters/bun/define-api-handler.d.ts +61 -0
- package/src/adapters/bun/define-api-handler.js +15 -0
- package/src/adapters/bun/define-api-handler.ts +114 -0
- package/src/adapters/bun/hmr-manager.d.ts +84 -0
- package/src/adapters/bun/hmr-manager.js +227 -0
- package/src/adapters/bun/hmr-manager.ts +281 -0
- package/src/adapters/bun/index.d.ts +3 -0
- package/src/adapters/bun/index.js +8 -0
- package/src/adapters/bun/index.ts +3 -0
- package/src/adapters/bun/server-adapter.d.ts +155 -0
- package/src/adapters/bun/server-adapter.js +368 -0
- package/src/adapters/bun/server-adapter.ts +492 -0
- package/src/adapters/bun/server-lifecycle.d.ts +52 -0
- package/src/adapters/bun/server-lifecycle.js +120 -0
- package/src/adapters/bun/server-lifecycle.ts +154 -0
- package/src/adapters/index.d.ts +6 -0
- package/src/adapters/index.js +14 -0
- package/src/adapters/index.ts +6 -0
- package/src/adapters/node/create-app.d.ts +21 -0
- package/src/adapters/node/create-app.js +143 -0
- package/src/adapters/node/create-app.ts +179 -0
- package/src/adapters/node/index.d.ts +4 -0
- package/src/adapters/node/index.js +8 -0
- package/src/adapters/node/index.ts +9 -0
- package/src/adapters/node/node-client-bridge.d.ts +26 -0
- package/src/adapters/node/node-client-bridge.js +66 -0
- package/src/adapters/node/node-client-bridge.ts +79 -0
- package/src/adapters/node/node-hmr-manager.d.ts +62 -0
- package/src/adapters/node/node-hmr-manager.js +221 -0
- package/src/adapters/node/node-hmr-manager.ts +271 -0
- package/src/adapters/node/server-adapter.d.ts +190 -0
- package/src/adapters/node/server-adapter.js +420 -0
- package/src/adapters/node/server-adapter.ts +561 -0
- package/src/adapters/node/static-content-server.d.ts +24 -0
- package/src/adapters/node/static-content-server.js +166 -0
- package/src/adapters/node/static-content-server.ts +203 -0
- package/src/adapters/shared/api-response.d.ts +52 -0
- package/src/adapters/shared/api-response.js +96 -0
- package/src/adapters/shared/api-response.ts +104 -0
- package/src/adapters/shared/application-adapter.d.ts +18 -0
- package/src/adapters/shared/application-adapter.js +90 -0
- package/src/adapters/shared/application-adapter.ts +199 -0
- package/src/adapters/shared/explicit-static-route-matcher.d.ts +38 -0
- package/src/adapters/shared/explicit-static-route-matcher.js +100 -0
- package/src/adapters/shared/explicit-static-route-matcher.ts +134 -0
- package/src/adapters/shared/file-route-middleware-pipeline.d.ts +65 -0
- package/src/adapters/shared/file-route-middleware-pipeline.js +98 -0
- package/src/adapters/shared/file-route-middleware-pipeline.ts +123 -0
- package/src/adapters/shared/fs-server-response-factory.d.ts +19 -0
- package/src/adapters/shared/fs-server-response-factory.js +97 -0
- package/src/adapters/shared/fs-server-response-factory.ts +118 -0
- package/src/adapters/shared/fs-server-response-matcher.d.ts +71 -0
- package/src/adapters/shared/fs-server-response-matcher.js +155 -0
- package/src/adapters/shared/fs-server-response-matcher.ts +198 -0
- package/src/adapters/shared/render-context.d.ts +14 -0
- package/src/adapters/shared/render-context.js +69 -0
- package/src/adapters/shared/render-context.ts +105 -0
- package/src/adapters/shared/server-adapter.d.ts +87 -0
- package/src/adapters/shared/server-adapter.js +353 -0
- package/src/adapters/shared/server-adapter.ts +442 -0
- package/src/adapters/shared/server-route-handler.d.ts +89 -0
- package/src/adapters/shared/server-route-handler.js +120 -0
- package/src/adapters/shared/server-route-handler.ts +166 -0
- package/src/adapters/shared/server-static-builder.d.ts +38 -0
- package/src/adapters/shared/server-static-builder.js +46 -0
- package/src/adapters/shared/server-static-builder.ts +82 -0
- package/src/build/build-adapter.d.ts +74 -0
- package/src/build/build-adapter.js +54 -0
- package/src/build/build-adapter.ts +132 -0
- package/src/build/build-types.d.ts +57 -0
- package/src/build/build-types.js +0 -0
- package/src/build/build-types.ts +83 -0
- package/src/build/esbuild-build-adapter.d.ts +69 -0
- package/src/build/esbuild-build-adapter.js +390 -0
- package/src/build/esbuild-build-adapter.ts +510 -0
- package/src/config/config-builder.d.ts +227 -0
- package/src/config/config-builder.js +392 -0
- package/src/config/config-builder.ts +474 -0
- package/src/constants.d.ts +32 -0
- package/src/constants.js +21 -0
- package/src/constants.ts +39 -0
- package/src/create-app.d.ts +17 -0
- package/src/create-app.js +66 -0
- package/src/create-app.ts +87 -0
- package/src/declarations.d.ts +26 -0
- package/src/define-api-handler.d.ts +25 -0
- package/src/define-api-handler.js +15 -0
- package/src/define-api-handler.ts +66 -0
- package/src/dev/sc-server.d.ts +30 -0
- package/src/dev/sc-server.js +111 -0
- package/src/dev/sc-server.ts +143 -0
- package/src/eco/README.md +636 -0
- package/src/eco/component-render-context.d.ts +105 -0
- package/src/eco/component-render-context.js +77 -0
- package/src/eco/component-render-context.ts +202 -0
- package/src/eco/eco.d.ts +9 -0
- package/src/eco/eco.js +110 -0
- package/src/eco/eco.ts +221 -0
- package/src/eco/eco.types.d.ts +170 -0
- package/src/eco/eco.types.js +0 -0
- package/src/eco/eco.types.ts +202 -0
- package/src/eco/eco.utils.d.ts +40 -0
- package/src/eco/eco.utils.js +40 -0
- package/src/eco/eco.utils.ts +89 -0
- package/src/eco/global-injector-map.d.ts +16 -0
- package/src/eco/global-injector-map.js +80 -0
- package/src/eco/global-injector-map.ts +112 -0
- package/src/eco/lazy-injector-map.d.ts +8 -0
- package/src/eco/lazy-injector-map.js +70 -0
- package/src/eco/lazy-injector-map.ts +120 -0
- package/src/eco/module-dependencies.d.ts +18 -0
- package/src/eco/module-dependencies.js +49 -0
- package/src/eco/module-dependencies.ts +75 -0
- package/src/env.d.ts +20 -0
- package/src/errors/http-error.d.ts +31 -0
- package/src/errors/http-error.js +50 -0
- package/src/errors/http-error.ts +72 -0
- package/src/errors/index.d.ts +2 -0
- package/src/errors/index.js +4 -0
- package/src/errors/index.ts +2 -0
- package/src/errors/locals-access-error.d.ts +4 -0
- package/src/errors/locals-access-error.js +9 -0
- package/src/errors/locals-access-error.ts +7 -0
- package/src/global/app-logger.d.ts +2 -0
- package/src/global/app-logger.js +6 -0
- package/src/global/app-logger.ts +4 -0
- package/src/hmr/client/__screenshots__/hmr-runtime.test.browser.ts/HMR-Runtime-HMR-Server-Integration-should-have-HMR-script-injected-in-page-1.png +0 -0
- package/src/hmr/client/__screenshots__/hmr-runtime.test.browser.ts/HMR-Runtime-HMR-Server-Integration-should-load-fixture-app-page-1.png +0 -0
- package/src/hmr/client/__screenshots__/hmr-runtime.test.browser.ts/HMR-Runtime-WebSocket-Connection-should-connect-to-correct-HMR-endpoint-1.png +0 -0
- package/src/hmr/client/hmr-runtime.d.ts +10 -0
- package/src/hmr/client/hmr-runtime.js +86 -0
- package/src/hmr/client/hmr-runtime.ts +121 -0
- package/src/hmr/hmr-strategy.d.ts +159 -0
- package/src/hmr/hmr-strategy.js +29 -0
- package/src/hmr/hmr-strategy.ts +172 -0
- package/src/hmr/hmr.test.e2e.d.ts +1 -0
- package/src/hmr/hmr.test.e2e.js +50 -0
- package/src/hmr/hmr.test.e2e.ts +75 -0
- package/src/hmr/strategies/default-hmr-strategy.d.ts +43 -0
- package/src/hmr/strategies/default-hmr-strategy.js +34 -0
- package/src/hmr/strategies/default-hmr-strategy.ts +60 -0
- package/src/hmr/strategies/js-hmr-strategy.d.ts +136 -0
- package/src/hmr/strategies/js-hmr-strategy.js +179 -0
- package/src/hmr/strategies/js-hmr-strategy.ts +308 -0
- package/src/index.browser.d.ts +3 -0
- package/src/index.browser.js +4 -0
- package/src/index.browser.ts +3 -0
- package/src/index.d.ts +5 -0
- package/src/index.js +10 -0
- package/src/index.ts +5 -0
- package/src/integrations/ghtml/ghtml-renderer.d.ts +15 -0
- package/src/integrations/ghtml/ghtml-renderer.js +60 -0
- package/src/integrations/ghtml/ghtml-renderer.ts +93 -0
- package/src/integrations/ghtml/ghtml.plugin.d.ts +20 -0
- package/src/integrations/ghtml/ghtml.plugin.js +21 -0
- package/src/integrations/ghtml/ghtml.plugin.ts +32 -0
- package/src/internal-types.d.ts +200 -0
- package/src/internal-types.js +0 -0
- package/src/internal-types.ts +212 -0
- package/src/plugins/alias-resolver-plugin.d.ts +2 -0
- package/src/plugins/alias-resolver-plugin.js +39 -0
- package/src/plugins/alias-resolver-plugin.ts +45 -0
- package/src/plugins/eco-component-meta-plugin.d.ts +95 -0
- package/src/plugins/eco-component-meta-plugin.js +157 -0
- package/src/plugins/eco-component-meta-plugin.ts +474 -0
- package/src/plugins/integration-plugin.d.ts +102 -0
- package/src/plugins/integration-plugin.js +100 -0
- package/src/plugins/integration-plugin.ts +184 -0
- package/src/plugins/processor.d.ts +82 -0
- package/src/plugins/processor.js +122 -0
- package/src/plugins/processor.ts +220 -0
- package/src/public-types.d.ts +1094 -0
- package/src/public-types.js +0 -0
- package/src/public-types.ts +1255 -0
- package/src/route-renderer/GRAPH.md +387 -0
- package/src/route-renderer/README.md +135 -0
- package/src/route-renderer/component-graph-executor.d.ts +32 -0
- package/src/route-renderer/component-graph-executor.js +31 -0
- package/src/route-renderer/component-graph-executor.ts +84 -0
- package/src/route-renderer/component-graph.d.ts +42 -0
- package/src/route-renderer/component-graph.js +72 -0
- package/src/route-renderer/component-graph.ts +159 -0
- package/src/route-renderer/component-marker.d.ts +52 -0
- package/src/route-renderer/component-marker.js +46 -0
- package/src/route-renderer/component-marker.ts +117 -0
- package/src/route-renderer/dependency-resolver.d.ts +24 -0
- package/src/route-renderer/dependency-resolver.js +428 -0
- package/src/route-renderer/dependency-resolver.ts +596 -0
- package/src/route-renderer/html-post-processing.service.d.ts +40 -0
- package/src/route-renderer/html-post-processing.service.js +86 -0
- package/src/route-renderer/html-post-processing.service.ts +103 -0
- package/src/route-renderer/integration-renderer.d.ts +339 -0
- package/src/route-renderer/integration-renderer.js +526 -0
- package/src/route-renderer/integration-renderer.ts +696 -0
- package/src/route-renderer/marker-graph-resolver.d.ts +76 -0
- package/src/route-renderer/marker-graph-resolver.js +93 -0
- package/src/route-renderer/marker-graph-resolver.ts +153 -0
- package/src/route-renderer/page-module-loader.d.ts +61 -0
- package/src/route-renderer/page-module-loader.js +102 -0
- package/src/route-renderer/page-module-loader.ts +153 -0
- package/src/route-renderer/render-execution.service.d.ts +69 -0
- package/src/route-renderer/render-execution.service.js +91 -0
- package/src/route-renderer/render-execution.service.ts +158 -0
- package/src/route-renderer/render-preparation.service.d.ts +112 -0
- package/src/route-renderer/render-preparation.service.js +243 -0
- package/src/route-renderer/render-preparation.service.ts +358 -0
- package/src/route-renderer/route-renderer.d.ts +26 -0
- package/src/route-renderer/route-renderer.js +68 -0
- package/src/route-renderer/route-renderer.ts +80 -0
- package/src/router/fs-router-scanner.d.ts +41 -0
- package/src/router/fs-router-scanner.js +155 -0
- package/src/router/fs-router-scanner.ts +217 -0
- package/src/router/fs-router.d.ts +26 -0
- package/src/router/fs-router.js +100 -0
- package/src/router/fs-router.ts +122 -0
- package/src/services/asset-processing-service/asset-processing.service.d.ts +41 -0
- package/src/services/asset-processing-service/asset-processing.service.js +250 -0
- package/src/services/asset-processing-service/asset-processing.service.ts +306 -0
- package/src/services/asset-processing-service/asset.factory.d.ts +17 -0
- package/src/services/asset-processing-service/asset.factory.js +82 -0
- package/src/services/asset-processing-service/asset.factory.ts +105 -0
- package/src/services/asset-processing-service/assets.types.d.ts +88 -0
- package/src/services/asset-processing-service/assets.types.js +0 -0
- package/src/services/asset-processing-service/assets.types.ts +112 -0
- package/src/services/asset-processing-service/index.d.ts +3 -0
- package/src/services/asset-processing-service/index.js +3 -0
- package/src/services/asset-processing-service/index.ts +3 -0
- package/src/services/asset-processing-service/processor.interface.d.ts +22 -0
- package/src/services/asset-processing-service/processor.interface.js +6 -0
- package/src/services/asset-processing-service/processor.interface.ts +27 -0
- package/src/services/asset-processing-service/processor.registry.d.ts +8 -0
- package/src/services/asset-processing-service/processor.registry.js +15 -0
- package/src/services/asset-processing-service/processor.registry.ts +18 -0
- package/src/services/asset-processing-service/processors/base/base-processor.d.ts +24 -0
- package/src/services/asset-processing-service/processors/base/base-processor.js +59 -0
- package/src/services/asset-processing-service/processors/base/base-processor.ts +76 -0
- package/src/services/asset-processing-service/processors/base/base-script-processor.d.ts +16 -0
- package/src/services/asset-processing-service/processors/base/base-script-processor.js +80 -0
- package/src/services/asset-processing-service/processors/base/base-script-processor.ts +105 -0
- package/src/services/asset-processing-service/processors/index.d.ts +5 -0
- package/src/services/asset-processing-service/processors/index.js +5 -0
- package/src/services/asset-processing-service/processors/index.ts +5 -0
- package/src/services/asset-processing-service/processors/script/content-script.processor.d.ts +5 -0
- package/src/services/asset-processing-service/processors/script/content-script.processor.js +57 -0
- package/src/services/asset-processing-service/processors/script/content-script.processor.ts +66 -0
- package/src/services/asset-processing-service/processors/script/file-script.processor.d.ts +8 -0
- package/src/services/asset-processing-service/processors/script/file-script.processor.js +76 -0
- package/src/services/asset-processing-service/processors/script/file-script.processor.ts +88 -0
- package/src/services/asset-processing-service/processors/script/node-module-script.processor.d.ts +7 -0
- package/src/services/asset-processing-service/processors/script/node-module-script.processor.js +74 -0
- package/src/services/asset-processing-service/processors/script/node-module-script.processor.ts +84 -0
- package/src/services/asset-processing-service/processors/stylesheet/content-stylesheet.processor.d.ts +5 -0
- package/src/services/asset-processing-service/processors/stylesheet/content-stylesheet.processor.js +25 -0
- package/src/services/asset-processing-service/processors/stylesheet/content-stylesheet.processor.ts +27 -0
- package/src/services/asset-processing-service/processors/stylesheet/file-stylesheet.processor.d.ts +9 -0
- package/src/services/asset-processing-service/processors/stylesheet/file-stylesheet.processor.js +63 -0
- package/src/services/asset-processing-service/processors/stylesheet/file-stylesheet.processor.ts +77 -0
- package/src/services/cache/cache.types.d.ts +107 -0
- package/src/services/cache/cache.types.js +0 -0
- package/src/services/cache/cache.types.ts +126 -0
- package/src/services/cache/index.d.ts +7 -0
- package/src/services/cache/index.js +7 -0
- package/src/services/cache/index.ts +18 -0
- package/src/services/cache/memory-cache-store.d.ts +42 -0
- package/src/services/cache/memory-cache-store.js +98 -0
- package/src/services/cache/memory-cache-store.ts +130 -0
- package/src/services/cache/page-cache-service.d.ts +70 -0
- package/src/services/cache/page-cache-service.js +152 -0
- package/src/services/cache/page-cache-service.ts +202 -0
- package/src/services/html-transformer.service.d.ts +50 -0
- package/src/services/html-transformer.service.js +163 -0
- package/src/services/html-transformer.service.ts +217 -0
- package/src/services/page-module-import.service.d.ts +37 -0
- package/src/services/page-module-import.service.js +88 -0
- package/src/services/page-module-import.service.ts +129 -0
- package/src/services/page-request-cache-coordinator.service.d.ts +75 -0
- package/src/services/page-request-cache-coordinator.service.js +107 -0
- package/src/services/page-request-cache-coordinator.service.ts +128 -0
- package/src/services/schema-validation-service.d.ts +122 -0
- package/src/services/schema-validation-service.js +101 -0
- package/src/services/schema-validation-service.ts +204 -0
- package/src/services/validation/standard-schema.types.d.ts +65 -0
- package/src/services/validation/standard-schema.types.js +0 -0
- package/src/services/validation/standard-schema.types.ts +68 -0
- package/src/static-site-generator/static-site-generator.d.ts +57 -0
- package/src/static-site-generator/static-site-generator.js +272 -0
- package/src/static-site-generator/static-site-generator.ts +359 -0
- package/src/utils/css.d.ts +1 -0
- package/src/utils/css.js +7 -0
- package/src/utils/css.ts +5 -0
- package/src/utils/deep-merge.d.ts +14 -0
- package/src/utils/deep-merge.js +32 -0
- package/src/utils/deep-merge.ts +47 -0
- package/src/utils/hash.d.ts +1 -0
- package/src/utils/hash.js +7 -0
- package/src/utils/hash.ts +5 -0
- package/src/utils/html.d.ts +1 -0
- package/src/utils/html.js +4 -0
- package/src/utils/html.ts +1 -0
- package/src/utils/invariant.d.ts +5 -0
- package/src/utils/invariant.js +11 -0
- package/src/utils/invariant.ts +15 -0
- package/src/utils/locals-utils.d.ts +15 -0
- package/src/utils/locals-utils.js +24 -0
- package/src/utils/locals-utils.ts +37 -0
- package/src/utils/parse-cli-args.d.ts +24 -0
- package/src/utils/parse-cli-args.js +47 -0
- package/src/utils/parse-cli-args.ts +83 -0
- package/src/utils/path-utils.module.d.ts +5 -0
- package/src/utils/path-utils.module.js +14 -0
- package/src/utils/path-utils.module.ts +14 -0
- package/src/utils/runtime.d.ts +11 -0
- package/src/utils/runtime.js +40 -0
- package/src/utils/runtime.ts +44 -0
- package/src/utils/server-utils.module.d.ts +19 -0
- package/src/utils/server-utils.module.js +56 -0
- package/src/utils/server-utils.module.ts +67 -0
- package/src/watchers/project-watcher.d.ts +120 -0
- package/src/watchers/project-watcher.js +238 -0
- package/src/watchers/project-watcher.test-helpers.d.ts +4 -0
- package/src/watchers/project-watcher.test-helpers.js +51 -0
- package/src/watchers/project-watcher.test-helpers.ts +40 -0
- package/src/watchers/project-watcher.ts +306 -0
|
@@ -0,0 +1,1255 @@
|
|
|
1
|
+
import type { Readable } from 'node:stream';
|
|
2
|
+
import type { ApiResponseBuilder } from './adapters/shared/api-response.js';
|
|
3
|
+
import type { EcoBuildPlugin } from './build/build-types.ts';
|
|
4
|
+
import type { EcoPageComponent } from './eco/eco.types.ts';
|
|
5
|
+
import type { EcoPagesAppConfig } from './internal-types.ts';
|
|
6
|
+
import type { HmrStrategy } from './hmr/hmr-strategy.ts';
|
|
7
|
+
import type { ProcessedAsset } from './services/asset-processing-service/assets.types.ts';
|
|
8
|
+
import type { CacheStats, CacheStrategy } from './services/cache/cache.types.ts';
|
|
9
|
+
import type { InteractionEventsString as ScriptsInjectorInteractionEventsString } from '@ecopages/scripts-injector/types';
|
|
10
|
+
|
|
11
|
+
export type { EcoPageComponent } from './eco/eco.types.ts';
|
|
12
|
+
|
|
13
|
+
import type {
|
|
14
|
+
StandardSchema,
|
|
15
|
+
StandardSchemaResult,
|
|
16
|
+
StandardSchemaSuccessResult,
|
|
17
|
+
StandardSchemaFailureResult,
|
|
18
|
+
StandardSchemaIssue,
|
|
19
|
+
InferOutput,
|
|
20
|
+
} from './services/validation/standard-schema.types.ts';
|
|
21
|
+
|
|
22
|
+
export type {
|
|
23
|
+
StandardSchema,
|
|
24
|
+
StandardSchemaResult,
|
|
25
|
+
StandardSchemaSuccessResult,
|
|
26
|
+
StandardSchemaFailureResult,
|
|
27
|
+
StandardSchemaIssue,
|
|
28
|
+
InferOutput,
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export type InteractionEventsString = ScriptsInjectorInteractionEventsString;
|
|
32
|
+
|
|
33
|
+
export type DependencyLazyTrigger =
|
|
34
|
+
| { 'on:idle': true }
|
|
35
|
+
| { 'on:interaction': InteractionEventsString }
|
|
36
|
+
| { 'on:visible': true | string };
|
|
37
|
+
|
|
38
|
+
export type DependencyAttributes = Record<string, string>;
|
|
39
|
+
|
|
40
|
+
export type EcoComponentStylesheetEntry = {
|
|
41
|
+
src?: string;
|
|
42
|
+
content?: string;
|
|
43
|
+
attributes?: DependencyAttributes;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export type EcoComponentScriptEntry = {
|
|
47
|
+
src?: string;
|
|
48
|
+
content?: string;
|
|
49
|
+
attributes?: DependencyAttributes;
|
|
50
|
+
lazy?: DependencyLazyTrigger;
|
|
51
|
+
ssr?: boolean;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export type ResolvedLazyScriptGroup = {
|
|
55
|
+
lazy: DependencyLazyTrigger;
|
|
56
|
+
scripts: string;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export type LazyTriggerRule =
|
|
60
|
+
| { 'on:idle': { scripts: string[] } }
|
|
61
|
+
| { 'on:interaction': { value: string; scripts: string[] } }
|
|
62
|
+
| { 'on:visible': { value?: string; scripts: string[] } };
|
|
63
|
+
|
|
64
|
+
export type ResolvedLazyTrigger = {
|
|
65
|
+
triggerId: string;
|
|
66
|
+
rules: LazyTriggerRule[];
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Narrow interface for cache invalidation in API handlers.
|
|
71
|
+
* Exposes only the methods needed for programmatic cache control.
|
|
72
|
+
*/
|
|
73
|
+
export interface CacheInvalidator {
|
|
74
|
+
/**
|
|
75
|
+
* Invalidate all cached entries matching any of the provided tags.
|
|
76
|
+
* @param tags - Array of tags to invalidate
|
|
77
|
+
* @returns Number of entries invalidated
|
|
78
|
+
*/
|
|
79
|
+
invalidateByTags(tags: string[]): Promise<number>;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Invalidate cached entries by exact path.
|
|
83
|
+
* @param paths - Array of URL paths to invalidate
|
|
84
|
+
* @returns Number of entries invalidated
|
|
85
|
+
*/
|
|
86
|
+
invalidateByPaths(paths: string[]): Promise<number>;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Clear all cached entries.
|
|
90
|
+
*/
|
|
91
|
+
clear(): Promise<void>;
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Get cache statistics for debugging.
|
|
95
|
+
*/
|
|
96
|
+
stats(): Promise<CacheStats>;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Context interface for HMR strategies.
|
|
101
|
+
* Provides access to watched files, specifier mappings, and build configuration.
|
|
102
|
+
*/
|
|
103
|
+
export interface DefaultHmrContext {
|
|
104
|
+
/**
|
|
105
|
+
* Map of registered entrypoints to their output URLs.
|
|
106
|
+
*/
|
|
107
|
+
getWatchedFiles(): Map<string, string>;
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Map of bare specifiers to vendor URLs for import resolution.
|
|
111
|
+
*/
|
|
112
|
+
getSpecifierMap(): Map<string, string>;
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Directory where HMR bundles are written.
|
|
116
|
+
*/
|
|
117
|
+
getDistDir(): string;
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Build plugins to use during bundling.
|
|
121
|
+
*/
|
|
122
|
+
getPlugins(): EcoBuildPlugin[];
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Absolute path to the source directory.
|
|
126
|
+
*/
|
|
127
|
+
getSrcDir(): string;
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Absolute path to the layouts directory.
|
|
131
|
+
* Used to detect layout file changes that require full page reloads.
|
|
132
|
+
*/
|
|
133
|
+
getLayoutsDir(): string;
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Absolute path to the pages directory.
|
|
137
|
+
* Used by plugins to identify page files for transformation.
|
|
138
|
+
*/
|
|
139
|
+
getPagesDir(): string;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Represents an event broadcast to connected clients via the ClientBridge.
|
|
144
|
+
*/
|
|
145
|
+
export type ClientBridgeEvent = {
|
|
146
|
+
/**
|
|
147
|
+
* Event type: 'reload' triggers full refresh, 'update' for JS modules, 'css-update' for stylesheets, 'layout-update' for layout changes
|
|
148
|
+
*/
|
|
149
|
+
type: 'reload' | 'error' | 'update' | 'css-update' | 'layout-update';
|
|
150
|
+
/**
|
|
151
|
+
* Path to the changed file
|
|
152
|
+
*/
|
|
153
|
+
path?: string;
|
|
154
|
+
/**
|
|
155
|
+
* Optional message for error or debug info
|
|
156
|
+
*/
|
|
157
|
+
message?: string;
|
|
158
|
+
/**
|
|
159
|
+
* Timestamp for cache busting
|
|
160
|
+
*/
|
|
161
|
+
timestamp?: number;
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Adapter-agnostic interface for broadcasting development events to connected clients.
|
|
166
|
+
* Implemented by both the Bun and Node client bridges.
|
|
167
|
+
*/
|
|
168
|
+
export interface IClientBridge {
|
|
169
|
+
broadcast(event: ClientBridgeEvent): void;
|
|
170
|
+
reload(): void;
|
|
171
|
+
cssUpdate(path: string): void;
|
|
172
|
+
update(path: string): void;
|
|
173
|
+
error(message: string): void;
|
|
174
|
+
subscriberCount: number;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Interface for the HMR Manager.
|
|
179
|
+
* Used by integration plugins to register entrypoints and strategies.
|
|
180
|
+
*/
|
|
181
|
+
export interface IHmrManager {
|
|
182
|
+
/**
|
|
183
|
+
* Registers a client entrypoint to be built and watched.
|
|
184
|
+
*/
|
|
185
|
+
registerEntrypoint(entrypointPath: string): Promise<string>;
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Registers mappings from bare specifiers to vendor URLs.
|
|
189
|
+
*/
|
|
190
|
+
registerSpecifierMap(map: Record<string, string>): void;
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Registers a custom HMR strategy.
|
|
194
|
+
*/
|
|
195
|
+
registerStrategy(strategy: HmrStrategy): void;
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Sets the build plugins to use during bundling.
|
|
199
|
+
*/
|
|
200
|
+
setPlugins(plugins: EcoBuildPlugin[]): void;
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Enables or disables HMR.
|
|
204
|
+
*/
|
|
205
|
+
setEnabled(enabled: boolean): void;
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Returns whether HMR is enabled.
|
|
209
|
+
*/
|
|
210
|
+
isEnabled(): boolean;
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Broadcasts an HMR event to connected clients.
|
|
214
|
+
*/
|
|
215
|
+
broadcast(event: ClientBridgeEvent): void;
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Gets the output URL for a registered entrypoint.
|
|
219
|
+
*/
|
|
220
|
+
getOutputUrl(entrypointPath: string): string | undefined;
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Gets the map of watched files.
|
|
224
|
+
*/
|
|
225
|
+
getWatchedFiles(): Map<string, string>;
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Gets the specifier map.
|
|
229
|
+
*/
|
|
230
|
+
getSpecifierMap(): Map<string, string>;
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Gets the HMR dist directory.
|
|
234
|
+
*/
|
|
235
|
+
getDistDir(): string;
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Gets the build plugins.
|
|
239
|
+
*/
|
|
240
|
+
getPlugins(): EcoBuildPlugin[];
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Gets the default HMR context.
|
|
244
|
+
*/
|
|
245
|
+
getDefaultContext(): DefaultHmrContext;
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Handles a file change event.
|
|
249
|
+
*/
|
|
250
|
+
handleFileChange(path: string): Promise<void>;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Represents the dependencies for an EcoComponent.
|
|
255
|
+
*/
|
|
256
|
+
export type EcoComponentDependencies = {
|
|
257
|
+
stylesheets?: Array<string | EcoComponentStylesheetEntry>;
|
|
258
|
+
scripts?: Array<string | EcoComponentScriptEntry>;
|
|
259
|
+
/**
|
|
260
|
+
* Browser module declarations resolved from node_modules.
|
|
261
|
+
*
|
|
262
|
+
* Supports grammar entries such as `react-aria-components{Table,Select}`
|
|
263
|
+
* to express explicit module imports for client bundles.
|
|
264
|
+
*/
|
|
265
|
+
modules?: string[];
|
|
266
|
+
components?: EcoComponent[];
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
export type EcoPagesElement = string | Promise<string>;
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Represents the input configuration for EcoPages.
|
|
273
|
+
*/
|
|
274
|
+
export type EcoPagesConfig = Omit<
|
|
275
|
+
Partial<EcoPagesAppConfig>,
|
|
276
|
+
'baseUrl' | 'derivedPaths' | 'templatesExt' | 'integrationsDependencies'
|
|
277
|
+
> &
|
|
278
|
+
Pick<EcoPagesAppConfig, 'baseUrl' | 'rootDir'>;
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Internal metadata injected by eco-component-meta-plugin.
|
|
282
|
+
* Contains component file path and integration info for dependency resolution.
|
|
283
|
+
* @internal
|
|
284
|
+
*/
|
|
285
|
+
export interface EcoInjectedMeta {
|
|
286
|
+
/** Hashed identifier for client-side use (doesn't expose file paths) */
|
|
287
|
+
id: string;
|
|
288
|
+
/** Full file path of the component (use path.dirname() to get directory) */
|
|
289
|
+
file: string;
|
|
290
|
+
/** The integration identifier (e.g., 'react', 'kitajs', 'lit', 'ghtml', 'mdx') */
|
|
291
|
+
integration: string;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
export type EcoComponentConfig = {
|
|
295
|
+
/** @internal Injected by eco-component-meta-plugin */
|
|
296
|
+
__eco?: EcoInjectedMeta;
|
|
297
|
+
/**
|
|
298
|
+
* Explicit integration override for this component.
|
|
299
|
+
* When provided, this takes precedence over auto-detected integration metadata.
|
|
300
|
+
*/
|
|
301
|
+
integration?: string;
|
|
302
|
+
/**
|
|
303
|
+
* The layout component to wrap this page during rendering.
|
|
304
|
+
*
|
|
305
|
+
* The layout receives the page content as `children` and is responsible for
|
|
306
|
+
* providing the page structure (header, footer, navigation, etc.).
|
|
307
|
+
*
|
|
308
|
+
* For React pages with client-side routing, the layout also handles the router context.
|
|
309
|
+
* Use `EcoRouter` and `PageContent` from `@ecopages/react-router` in layouts that
|
|
310
|
+
* need SPA navigation support.
|
|
311
|
+
*
|
|
312
|
+
* @example
|
|
313
|
+
* ```tsx
|
|
314
|
+
* // Simple layout (no routing)
|
|
315
|
+
* const Layout = ({ children }) => <main>{children}</main>;
|
|
316
|
+
*
|
|
317
|
+
* // Layout with SPA routing
|
|
318
|
+
* const Layout = ({ children }) => (
|
|
319
|
+
* <EcoRouter page={...} pageProps={...}>
|
|
320
|
+
* <Header />
|
|
321
|
+
* <PageContent />
|
|
322
|
+
* </EcoRouter>
|
|
323
|
+
* );
|
|
324
|
+
*
|
|
325
|
+
* // Page using the layout
|
|
326
|
+
* const MyPage = () => <h1>Hello</h1>;
|
|
327
|
+
* MyPage.config = { layout: Layout };
|
|
328
|
+
* ```
|
|
329
|
+
*/
|
|
330
|
+
layout?: EcoComponent;
|
|
331
|
+
dependencies?: EcoComponentDependencies;
|
|
332
|
+
/**
|
|
333
|
+
* Internal: Resolved lazy scripts grouped by trigger.
|
|
334
|
+
* Set by the renderer, used by eco.component() for multi-trigger auto-wrapping.
|
|
335
|
+
* @internal
|
|
336
|
+
*/
|
|
337
|
+
_resolvedLazyScripts?: ResolvedLazyScriptGroup[];
|
|
338
|
+
/**
|
|
339
|
+
* Internal: Resolved lazy triggers for the global injector map.
|
|
340
|
+
* Set by the renderer in the default full orchestration flow.
|
|
341
|
+
* @internal
|
|
342
|
+
*/
|
|
343
|
+
_resolvedLazyTriggers?: ResolvedLazyTrigger[];
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* The base structure for any EcoPages component.
|
|
348
|
+
*/
|
|
349
|
+
export type EcoComponentBase = {
|
|
350
|
+
/**
|
|
351
|
+
* The configuration options for the EcoComponent.
|
|
352
|
+
*/
|
|
353
|
+
config?: EcoComponentConfig;
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* Static paths for dynamic routes (consolidated eco.page API).
|
|
357
|
+
* @internal Used by the renderer to retrieve static paths from the page component.
|
|
358
|
+
*/
|
|
359
|
+
staticPaths?: GetStaticPaths;
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* Static props fetcher (consolidated eco.page API).
|
|
363
|
+
* @internal Used by the renderer to retrieve static props from the page component.
|
|
364
|
+
*/
|
|
365
|
+
staticProps?: GetStaticProps<any>;
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* Metadata generator (consolidated eco.page API).
|
|
369
|
+
* @internal Used by the renderer to retrieve metadata from the page component.
|
|
370
|
+
*/
|
|
371
|
+
metadata?: GetMetadata<any>;
|
|
372
|
+
};
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* Checks if a type is `any`.
|
|
376
|
+
*/
|
|
377
|
+
export type IsAny<T> = 0 extends 1 & T ? true : false;
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* A function component type that is framework-agnostic.
|
|
381
|
+
* Uses a broader signature to support both direct calls and HOC wrappers.
|
|
382
|
+
*/
|
|
383
|
+
export type EcoFunctionComponent<P, R> = {
|
|
384
|
+
(props: P, ...args: any[]): R;
|
|
385
|
+
} & EcoComponentBase;
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* Represents an EcoComponent.
|
|
389
|
+
*
|
|
390
|
+
* It can be defined by passing the props type as the first generic,
|
|
391
|
+
* or by passing the component type itself to infer the signature.
|
|
392
|
+
*
|
|
393
|
+
* @template T - The type of the props object or the component function itself.
|
|
394
|
+
* @template C - The type of the rendered element.
|
|
395
|
+
*
|
|
396
|
+
* @example
|
|
397
|
+
* //1. Simplest usage
|
|
398
|
+
* export const MyComponent: EcoComponent<{prop1: string}> = ({prop1}) => {
|
|
399
|
+
* return <div>...</div>;
|
|
400
|
+
* };
|
|
401
|
+
*
|
|
402
|
+
* @example
|
|
403
|
+
* // 2. Using with HOCs like MobX observer (passing the props type)
|
|
404
|
+
* export const MyObservedComponent: EcoComponent<object> = observer(function MyObservedComponent() {
|
|
405
|
+
* return <div>...</div>;
|
|
406
|
+
* });
|
|
407
|
+
*
|
|
408
|
+
* @example
|
|
409
|
+
* // 3. Passing the full function signature (Perfect for Generic Components)
|
|
410
|
+
* export const Select: EcoComponent<<T extends object>(props: SelectProps<T>) => JSX.Element> = <T extends object>({
|
|
411
|
+
* label,
|
|
412
|
+
* items,
|
|
413
|
+
* }: SelectProps<T>) => {
|
|
414
|
+
* return <select>...</select>;
|
|
415
|
+
* };
|
|
416
|
+
*/
|
|
417
|
+
/**
|
|
418
|
+
* A function component type that is framework-agnostic.
|
|
419
|
+
* Uses a broader signature to support both direct calls and HOC wrappers.
|
|
420
|
+
*/
|
|
421
|
+
export type EcoComponent<P = any, R = any> =
|
|
422
|
+
IsAny<P> extends true
|
|
423
|
+
? EcoFunctionComponent<any, any> | EcoComponentBase
|
|
424
|
+
: P extends (props: infer Props, ...args: any[]) => infer Return
|
|
425
|
+
? EcoFunctionComponent<Props, Return>
|
|
426
|
+
: EcoFunctionComponent<P, R>;
|
|
427
|
+
|
|
428
|
+
/**
|
|
429
|
+
* Represents a page in EcoPages.
|
|
430
|
+
*/
|
|
431
|
+
export type PageProps<T = unknown> = T & StaticPageContext & { locals?: RequestLocals };
|
|
432
|
+
|
|
433
|
+
/**
|
|
434
|
+
* Represents the metadata for a page.
|
|
435
|
+
*/
|
|
436
|
+
export interface PageMetadataProps {
|
|
437
|
+
title: string;
|
|
438
|
+
description: string;
|
|
439
|
+
image?: string;
|
|
440
|
+
url?: string;
|
|
441
|
+
keywords?: string[];
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/**
|
|
445
|
+
* Represents the props for the head of a page.
|
|
446
|
+
*/
|
|
447
|
+
export interface PageHeadProps<T = EcoPagesElement> {
|
|
448
|
+
metadata: PageMetadataProps;
|
|
449
|
+
dependencies?: EcoComponentDependencies;
|
|
450
|
+
children?: T;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
/**
|
|
454
|
+
* Represents the props for the HTML template of a page.
|
|
455
|
+
*/
|
|
456
|
+
export interface HtmlTemplateProps extends PageHeadProps {
|
|
457
|
+
children: EcoPagesElement;
|
|
458
|
+
language?: string;
|
|
459
|
+
headContent?: EcoPagesElement;
|
|
460
|
+
pageProps: Record<string, unknown>;
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
/**
|
|
464
|
+
* Represents the props for the error 404 template.
|
|
465
|
+
*/
|
|
466
|
+
export interface Error404TemplateProps extends Omit<HtmlTemplateProps, 'children'> {
|
|
467
|
+
message: string;
|
|
468
|
+
stack?: string;
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
/**
|
|
472
|
+
* Represents the parameters for a page.
|
|
473
|
+
* The keys are strings, and the values can be either a string or an array of strings.
|
|
474
|
+
*/
|
|
475
|
+
export type PageParams = Record<string, string | string[]>;
|
|
476
|
+
|
|
477
|
+
/**
|
|
478
|
+
* Represents a query object for a page.
|
|
479
|
+
* The keys are strings and the values can be either a string or an array of strings.
|
|
480
|
+
*/
|
|
481
|
+
export type PageQuery = Record<string, string | string[]>;
|
|
482
|
+
|
|
483
|
+
/**
|
|
484
|
+
* Request-scoped data that is only available during request-time rendering.
|
|
485
|
+
*
|
|
486
|
+
* Apps should augment this interface via module augmentation:
|
|
487
|
+
*
|
|
488
|
+
* declare module '@ecopages/core' {
|
|
489
|
+
* interface RequestLocals { session?: Session | null }
|
|
490
|
+
* }
|
|
491
|
+
*/
|
|
492
|
+
export interface RequestLocals {}
|
|
493
|
+
|
|
494
|
+
/**
|
|
495
|
+
* Represents the context object for a static page.
|
|
496
|
+
*/
|
|
497
|
+
export type StaticPageContext = {
|
|
498
|
+
params?: PageParams;
|
|
499
|
+
query?: PageQuery;
|
|
500
|
+
};
|
|
501
|
+
|
|
502
|
+
/**
|
|
503
|
+
* Request-time page context.
|
|
504
|
+
*
|
|
505
|
+
* This is only populated during SSR. Static generation must not access locals.
|
|
506
|
+
*/
|
|
507
|
+
export type RequestPageContext = {
|
|
508
|
+
locals: RequestLocals;
|
|
509
|
+
};
|
|
510
|
+
|
|
511
|
+
/**
|
|
512
|
+
* Adds optional `locals` to a props type.
|
|
513
|
+
* Primarily used for layouts that may receive request-scoped data from middleware.
|
|
514
|
+
*
|
|
515
|
+
* @template P - The base props type
|
|
516
|
+
*
|
|
517
|
+
* @example
|
|
518
|
+
* ```ts
|
|
519
|
+
* type MyLayoutProps = WithLocals<{ children: ReactNode }>;
|
|
520
|
+
* // { children: ReactNode; locals?: RequestLocals }
|
|
521
|
+
* ```
|
|
522
|
+
*/
|
|
523
|
+
export type WithLocals<P> = P & { locals?: RequestLocals };
|
|
524
|
+
|
|
525
|
+
/**
|
|
526
|
+
* Represents the params for a static path.
|
|
527
|
+
*/
|
|
528
|
+
export type StaticPath = { params: PageParams };
|
|
529
|
+
|
|
530
|
+
/**
|
|
531
|
+
* The function that returns the static paths for a page.
|
|
532
|
+
*/
|
|
533
|
+
export type GetStaticPaths = (context: { appConfig: EcoPagesAppConfig; runtimeOrigin: string }) => Promise<{
|
|
534
|
+
paths: StaticPath[];
|
|
535
|
+
}>;
|
|
536
|
+
|
|
537
|
+
/**
|
|
538
|
+
* The context object for the getMetadata function.
|
|
539
|
+
*/
|
|
540
|
+
export type GetMetadataContext<T = Record<string, unknown>> = Required<StaticPageContext> & {
|
|
541
|
+
props: T;
|
|
542
|
+
appConfig: EcoPagesAppConfig;
|
|
543
|
+
};
|
|
544
|
+
|
|
545
|
+
/**
|
|
546
|
+
* The function that returns the metadata for a page.
|
|
547
|
+
*/
|
|
548
|
+
export type GetMetadata<T = Record<string, unknown>> = (
|
|
549
|
+
context: GetMetadataContext<T>,
|
|
550
|
+
) => PageMetadataProps | Promise<PageMetadataProps>;
|
|
551
|
+
|
|
552
|
+
/**
|
|
553
|
+
* The function that returns the static props for a page.
|
|
554
|
+
*/
|
|
555
|
+
export type GetStaticProps<T> = (context: {
|
|
556
|
+
pathname: StaticPath;
|
|
557
|
+
appConfig: EcoPagesAppConfig;
|
|
558
|
+
runtimeOrigin: string;
|
|
559
|
+
}) => Promise<{
|
|
560
|
+
props: T;
|
|
561
|
+
}>;
|
|
562
|
+
|
|
563
|
+
/**
|
|
564
|
+
* Represents a page file in EcoPages.
|
|
565
|
+
* @template T - The type of the page props.
|
|
566
|
+
*/
|
|
567
|
+
export type EcoPageFile<T = unknown> = T & {
|
|
568
|
+
default: EcoComponent<any, any>;
|
|
569
|
+
getStaticPaths?: GetStaticPaths;
|
|
570
|
+
getStaticProps?: GetStaticProps<Record<string, unknown>>;
|
|
571
|
+
getMetadata?: GetMetadata;
|
|
572
|
+
cache?: CacheStrategy;
|
|
573
|
+
};
|
|
574
|
+
|
|
575
|
+
/**
|
|
576
|
+
* Represents a CSS processor.
|
|
577
|
+
*/
|
|
578
|
+
export interface CssProcessor {
|
|
579
|
+
/**
|
|
580
|
+
* Processes a CSS file at the specified path.
|
|
581
|
+
* @param path - The path to the CSS file.
|
|
582
|
+
* @returns A promise that resolves to the processed CSS as a string.
|
|
583
|
+
*/
|
|
584
|
+
processPath: (path: string, options?: any) => Promise<string>;
|
|
585
|
+
|
|
586
|
+
/**
|
|
587
|
+
* Processes a CSS string or buffer.
|
|
588
|
+
* @param contents - The CSS contents as a string or buffer.
|
|
589
|
+
* @returns A promise that resolves to the processed CSS as a string.
|
|
590
|
+
*/
|
|
591
|
+
processStringOrBuffer: (contents: string | Buffer, options?: any) => Promise<string>;
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
/**
|
|
595
|
+
* The options for the route renderer.
|
|
596
|
+
*/
|
|
597
|
+
export type RouteRendererOptions = {
|
|
598
|
+
file: string;
|
|
599
|
+
params?: PageParams;
|
|
600
|
+
query?: PageQuery;
|
|
601
|
+
locals?: RequestLocals;
|
|
602
|
+
};
|
|
603
|
+
|
|
604
|
+
/**
|
|
605
|
+
* The body of the route renderer.
|
|
606
|
+
*/
|
|
607
|
+
export type RouteRendererBody = BodyInit | Readable;
|
|
608
|
+
|
|
609
|
+
/**
|
|
610
|
+
* Result of rendering a route, including body and optional cache configuration.
|
|
611
|
+
*/
|
|
612
|
+
export type RouteRenderResult = {
|
|
613
|
+
body: RouteRendererBody;
|
|
614
|
+
/** Cache strategy from page component's eco.page({ cache }) option */
|
|
615
|
+
cacheStrategy?: CacheStrategy;
|
|
616
|
+
};
|
|
617
|
+
|
|
618
|
+
/**
|
|
619
|
+
* Represents the dependencies required for an integration plugin.
|
|
620
|
+
* It combines the base integration plugin dependencies with specific integration plugin dependencies.
|
|
621
|
+
*/
|
|
622
|
+
export type IntegrationPluginDependencies = BaseIntegrationPluginDependencies & SpecificIntegrationPluginDependencies;
|
|
623
|
+
|
|
624
|
+
type BaseIntegrationPluginDependencies = {
|
|
625
|
+
inline?: boolean;
|
|
626
|
+
};
|
|
627
|
+
|
|
628
|
+
/**
|
|
629
|
+
* Represents the dependencies required for a specific integration plugin.
|
|
630
|
+
* It can be one of the following types:
|
|
631
|
+
* {@link ScriptImportIntegrationPluginDependencies}
|
|
632
|
+
* {@link ScriptContentIntegrationPluginDependencies}
|
|
633
|
+
* {@link StylesheetImportIntegrationPluginDependencies}
|
|
634
|
+
* {@link StylesheetContentIntegrationPluginDependencies}
|
|
635
|
+
*/
|
|
636
|
+
type SpecificIntegrationPluginDependencies =
|
|
637
|
+
| ScriptImportIntegrationPluginDependencies
|
|
638
|
+
| ScriptContentIntegrationPluginDependencies
|
|
639
|
+
| StylesheetImportIntegrationPluginDependencies
|
|
640
|
+
| StylesheetContentIntegrationPluginDependencies;
|
|
641
|
+
|
|
642
|
+
/**
|
|
643
|
+
* Script dependencies for an integration plugin with an import path.
|
|
644
|
+
*/
|
|
645
|
+
type ScriptImportIntegrationPluginDependencies = {
|
|
646
|
+
kind: 'script';
|
|
647
|
+
importPath: string;
|
|
648
|
+
position?: 'head' | 'body';
|
|
649
|
+
/** @default true */
|
|
650
|
+
minify?: boolean;
|
|
651
|
+
};
|
|
652
|
+
|
|
653
|
+
/**
|
|
654
|
+
* Script dependencies for an integration plugin with content.
|
|
655
|
+
*/
|
|
656
|
+
type ScriptContentIntegrationPluginDependencies = {
|
|
657
|
+
kind: 'script';
|
|
658
|
+
content: string;
|
|
659
|
+
position?: 'head' | 'body';
|
|
660
|
+
/** @default true */
|
|
661
|
+
minify?: boolean;
|
|
662
|
+
};
|
|
663
|
+
|
|
664
|
+
/**
|
|
665
|
+
* Stylesheet dependencies for an integration plugin with an import path.
|
|
666
|
+
*/
|
|
667
|
+
type StylesheetImportIntegrationPluginDependencies = {
|
|
668
|
+
kind: 'stylesheet';
|
|
669
|
+
importPath: string;
|
|
670
|
+
};
|
|
671
|
+
|
|
672
|
+
/**
|
|
673
|
+
* Stylesheet dependencies for an integration plugin with content.
|
|
674
|
+
*/
|
|
675
|
+
type StylesheetContentIntegrationPluginDependencies = {
|
|
676
|
+
kind: 'stylesheet';
|
|
677
|
+
content: string;
|
|
678
|
+
};
|
|
679
|
+
|
|
680
|
+
/**
|
|
681
|
+
* The options for the integration renderer.
|
|
682
|
+
*/
|
|
683
|
+
export type IntegrationRendererRenderOptions<C = EcoPagesElement> = RouteRendererOptions & {
|
|
684
|
+
props?: Record<string, unknown>;
|
|
685
|
+
metadata: PageMetadataProps;
|
|
686
|
+
HtmlTemplate: EcoComponent<HtmlTemplateProps, C>;
|
|
687
|
+
Page: EcoComponent<PageProps, C>;
|
|
688
|
+
Layout?: EcoComponent;
|
|
689
|
+
dependencies?: EcoComponentDependencies;
|
|
690
|
+
resolvedDependencies: ProcessedAsset[];
|
|
691
|
+
componentRender?: ComponentRenderResult;
|
|
692
|
+
pageProps?: Record<string, unknown>;
|
|
693
|
+
cacheStrategy?: CacheStrategy;
|
|
694
|
+
pageLocals?: RequestLocals;
|
|
695
|
+
};
|
|
696
|
+
|
|
697
|
+
export interface ComponentRenderInput {
|
|
698
|
+
component: EcoComponent;
|
|
699
|
+
props: Record<string, unknown>;
|
|
700
|
+
children?: string;
|
|
701
|
+
integrationContext?: unknown;
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
export interface ComponentRenderResult {
|
|
705
|
+
html: string;
|
|
706
|
+
canAttachAttributes: boolean;
|
|
707
|
+
rootTag?: string;
|
|
708
|
+
integrationName: string;
|
|
709
|
+
rootAttributes?: Record<string, string>;
|
|
710
|
+
assets?: ProcessedAsset[];
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
/**
|
|
714
|
+
* Represents a deep required type for a given object
|
|
715
|
+
*/
|
|
716
|
+
export type DeepRequired<T> = Required<{
|
|
717
|
+
[K in keyof T]: T[K] extends Required<T[K]> ? T[K] : DeepRequired<T[K]>;
|
|
718
|
+
}>;
|
|
719
|
+
|
|
720
|
+
/**
|
|
721
|
+
* The Prettify helper is a utility type that takes an object type and makes the hover overlay more readable.
|
|
722
|
+
*/
|
|
723
|
+
export type Prettify<T> = {
|
|
724
|
+
[K in keyof T]: T[K];
|
|
725
|
+
} & {};
|
|
726
|
+
|
|
727
|
+
/**
|
|
728
|
+
* Services available to API handlers.
|
|
729
|
+
*/
|
|
730
|
+
export interface ApiHandlerServices {
|
|
731
|
+
/**
|
|
732
|
+
* Cache invalidation service.
|
|
733
|
+
* Null when caching is disabled.
|
|
734
|
+
*/
|
|
735
|
+
cache: CacheInvalidator | null;
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
/**
|
|
739
|
+
* Options for rendering a view.
|
|
740
|
+
*/
|
|
741
|
+
export interface RenderOptions {
|
|
742
|
+
status?: number;
|
|
743
|
+
headers?: HeadersInit;
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
/**
|
|
747
|
+
* Options for JSON/HTML response helpers.
|
|
748
|
+
*/
|
|
749
|
+
export interface ResponseOptions {
|
|
750
|
+
status?: number;
|
|
751
|
+
headers?: HeadersInit;
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
/**
|
|
755
|
+
* Context for rendering views in route handlers.
|
|
756
|
+
* Provides methods to render eco.page views and return formatted responses.
|
|
757
|
+
*/
|
|
758
|
+
export interface RenderContext {
|
|
759
|
+
/**
|
|
760
|
+
* Render an eco.page view with full layout and includes.
|
|
761
|
+
* @param view - The eco.page component to render
|
|
762
|
+
* @param props - Props to pass to the view
|
|
763
|
+
* @param options - Optional status code and headers
|
|
764
|
+
*/
|
|
765
|
+
render<P = Record<string, unknown>>(view: EcoComponent<P>, props: P, options?: RenderOptions): Promise<Response>;
|
|
766
|
+
|
|
767
|
+
/**
|
|
768
|
+
* Render an eco.page view without layout (for partials/fragments).
|
|
769
|
+
* @param view - The eco.page component to render
|
|
770
|
+
* @param props - Props to pass to the view
|
|
771
|
+
* @param options - Optional status code and headers
|
|
772
|
+
*/
|
|
773
|
+
renderPartial<P = Record<string, unknown>>(
|
|
774
|
+
view: EcoComponent<P>,
|
|
775
|
+
props: P,
|
|
776
|
+
options?: RenderOptions,
|
|
777
|
+
): Promise<Response>;
|
|
778
|
+
|
|
779
|
+
/**
|
|
780
|
+
* Return a JSON response.
|
|
781
|
+
* @param data - Data to serialize as JSON
|
|
782
|
+
* @param options - Optional status code and headers
|
|
783
|
+
*/
|
|
784
|
+
json(data: unknown, options?: ResponseOptions): Response;
|
|
785
|
+
|
|
786
|
+
/**
|
|
787
|
+
* Return an HTML response.
|
|
788
|
+
* @param content - HTML string content
|
|
789
|
+
* @param options - Optional status code and headers
|
|
790
|
+
*/
|
|
791
|
+
html(content: string, options?: ResponseOptions): Response;
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
/**
|
|
795
|
+
* Context provided to the API handler.
|
|
796
|
+
*/
|
|
797
|
+
export interface ApiHandlerContext<TRequest extends Request = Request, TServer = any> extends RenderContext {
|
|
798
|
+
request: TRequest;
|
|
799
|
+
params: Record<string, string>;
|
|
800
|
+
response: ApiResponseBuilder;
|
|
801
|
+
server: TServer;
|
|
802
|
+
/**
|
|
803
|
+
* Request-scoped data store.
|
|
804
|
+
* Only valid during request-time handling (SSR/API). Must not be used for static generation.
|
|
805
|
+
*/
|
|
806
|
+
locals: RequestLocals;
|
|
807
|
+
/**
|
|
808
|
+
* Require one or more locals keys. If missing, executes `onMissing` to produce a terminating Response.
|
|
809
|
+
*/
|
|
810
|
+
require: {
|
|
811
|
+
<K extends keyof RequestLocals>(key: K, onMissing: () => Response): Exclude<RequestLocals[K], null | undefined>;
|
|
812
|
+
<K extends keyof RequestLocals>(
|
|
813
|
+
keys: readonly K[],
|
|
814
|
+
onMissing: () => Response,
|
|
815
|
+
): { [P in K]-?: Exclude<RequestLocals[P], null | undefined> };
|
|
816
|
+
};
|
|
817
|
+
/**
|
|
818
|
+
* Services available to the API handler.
|
|
819
|
+
*/
|
|
820
|
+
services: ApiHandlerServices;
|
|
821
|
+
/**
|
|
822
|
+
* Parsed and optionally validated request body.
|
|
823
|
+
*
|
|
824
|
+
* - Without schema: Contains the parsed JSON body as `unknown`
|
|
825
|
+
* - With schema: Contains the validated and type-safe body data
|
|
826
|
+
*
|
|
827
|
+
* For raw access to the request body stream, use `ctx.request.body`.
|
|
828
|
+
*
|
|
829
|
+
* @example Without validation
|
|
830
|
+
* ```typescript
|
|
831
|
+
* app.post('/posts', async (ctx) => {
|
|
832
|
+
* const data = ctx.body; // [unknown] - parsed JSON
|
|
833
|
+
* return ctx.json({ received: data });
|
|
834
|
+
* });
|
|
835
|
+
* ```
|
|
836
|
+
*
|
|
837
|
+
* @example With validation
|
|
838
|
+
* ```typescript
|
|
839
|
+
* import { z } from 'zod';
|
|
840
|
+
*
|
|
841
|
+
* app.post('/posts', async (ctx) => {
|
|
842
|
+
* const { title, author } = ctx.body; // [type-safe]
|
|
843
|
+
* return ctx.json({ title, author });
|
|
844
|
+
* }, {
|
|
845
|
+
* schema: {
|
|
846
|
+
* body: z.object({ title: z.string(), author: z.string() })
|
|
847
|
+
* }
|
|
848
|
+
* });
|
|
849
|
+
* ```
|
|
850
|
+
*
|
|
851
|
+
* Validation runs before the handler executes. Invalid requests receive a 400 response.
|
|
852
|
+
*/
|
|
853
|
+
body?: unknown;
|
|
854
|
+
/**
|
|
855
|
+
* Parsed and optionally validated query parameters.
|
|
856
|
+
*
|
|
857
|
+
* - Without schema: Contains the parsed query params as a string record
|
|
858
|
+
* - With schema: Contains the validated and type-safe query data
|
|
859
|
+
*
|
|
860
|
+
* For raw access to query parameters, use `ctx.request.url` and parse manually.
|
|
861
|
+
*
|
|
862
|
+
* @example Pagination with type coercion
|
|
863
|
+
* ```typescript
|
|
864
|
+
* import { z } from 'zod';
|
|
865
|
+
*
|
|
866
|
+
* app.get('/posts', async (ctx) => {
|
|
867
|
+
* const { page, limit, sortBy } = ctx.query;
|
|
868
|
+
* // page: number, limit: number, sortBy: 'date' | 'title' | 'views'
|
|
869
|
+
* return ctx.json({ page, limit, sortBy });
|
|
870
|
+
* }, {
|
|
871
|
+
* schema: {
|
|
872
|
+
* query: z.object({
|
|
873
|
+
* page: z.coerce.number().min(1).default(1),
|
|
874
|
+
* limit: z.coerce.number().min(1).max(100).default(20),
|
|
875
|
+
* sortBy: z.enum(['date', 'title', 'views']).default('date')
|
|
876
|
+
* })
|
|
877
|
+
* }
|
|
878
|
+
* });
|
|
879
|
+
* ```
|
|
880
|
+
*
|
|
881
|
+
* @example Search with filters
|
|
882
|
+
* ```typescript
|
|
883
|
+
* app.get('/search', async (ctx) => {
|
|
884
|
+
* const { q, category } = ctx.query;
|
|
885
|
+
* return ctx.json({ results: await search(q, category) });
|
|
886
|
+
* }, {
|
|
887
|
+
* schema: {
|
|
888
|
+
* query: z.object({
|
|
889
|
+
* q: z.string().min(2).max(100),
|
|
890
|
+
* category: z.enum(['posts', 'users', 'comments']).optional()
|
|
891
|
+
* })
|
|
892
|
+
* }
|
|
893
|
+
* });
|
|
894
|
+
* ```
|
|
895
|
+
*/
|
|
896
|
+
query?: unknown;
|
|
897
|
+
/**
|
|
898
|
+
* Parsed and optionally validated request headers.
|
|
899
|
+
*
|
|
900
|
+
* - Without schema: Contains the parsed headers as a string record
|
|
901
|
+
* - With schema: Contains the validated and type-safe header data
|
|
902
|
+
*
|
|
903
|
+
* For raw access to headers, use `ctx.request.headers`.
|
|
904
|
+
*
|
|
905
|
+
* @example API key authentication
|
|
906
|
+
* ```typescript
|
|
907
|
+
* import { z } from 'zod';
|
|
908
|
+
*
|
|
909
|
+
* app.post('/api/webhooks', async (ctx) => {
|
|
910
|
+
* const apiKey = ctx.headers['x-api-key'];
|
|
911
|
+
* // apiKey is guaranteed to be a valid UUID
|
|
912
|
+
* return ctx.json({ received: true });
|
|
913
|
+
* }, {
|
|
914
|
+
* schema: {
|
|
915
|
+
* headers: z.object({
|
|
916
|
+
* 'x-api-key': z.string().uuid()
|
|
917
|
+
* })
|
|
918
|
+
* }
|
|
919
|
+
* });
|
|
920
|
+
* ```
|
|
921
|
+
*
|
|
922
|
+
* @example Webhook signature validation
|
|
923
|
+
* ```typescript
|
|
924
|
+
* app.post('/webhooks/stripe', async (ctx) => {
|
|
925
|
+
* const signature = ctx.headers['stripe-signature'];
|
|
926
|
+
* // signature is guaranteed to exist
|
|
927
|
+
* const isValid = verifyStripeSignature(ctx.body, signature);
|
|
928
|
+
* return ctx.json({ verified: isValid });
|
|
929
|
+
* }, {
|
|
930
|
+
* schema: {
|
|
931
|
+
* headers: z.object({
|
|
932
|
+
* 'stripe-signature': z.string().min(1)
|
|
933
|
+
* })
|
|
934
|
+
* }
|
|
935
|
+
* });
|
|
936
|
+
* ```
|
|
937
|
+
*
|
|
938
|
+
* @example Content negotiation
|
|
939
|
+
* ```typescript
|
|
940
|
+
* app.post('/api/data', async (ctx) => {
|
|
941
|
+
* const { accept } = ctx.headers;
|
|
942
|
+
* if (accept === 'application/xml') {
|
|
943
|
+
* return ctx.html(toXml(data), { headers: { 'Content-Type': 'application/xml' } });
|
|
944
|
+
* }
|
|
945
|
+
* return ctx.json(data);
|
|
946
|
+
* }, {
|
|
947
|
+
* schema: {
|
|
948
|
+
* headers: z.object({
|
|
949
|
+
* 'content-type': z.literal('application/json'),
|
|
950
|
+
* accept: z.enum(['application/json', 'application/xml']).optional()
|
|
951
|
+
* })
|
|
952
|
+
* }
|
|
953
|
+
* });
|
|
954
|
+
* ```
|
|
955
|
+
*/
|
|
956
|
+
headers?: unknown;
|
|
957
|
+
}
|
|
958
|
+
|
|
959
|
+
/**
|
|
960
|
+
* Next function for middleware chain.
|
|
961
|
+
* Call to continue to the next middleware or final handler.
|
|
962
|
+
*/
|
|
963
|
+
export type MiddlewareNext = () => Promise<Response>;
|
|
964
|
+
|
|
965
|
+
/**
|
|
966
|
+
* Middleware function signature.
|
|
967
|
+
* Receives the request context and a next function to continue the chain.
|
|
968
|
+
* Can short-circuit by returning a Response directly without calling next().
|
|
969
|
+
*
|
|
970
|
+
* @typeParam TRequest - Request type
|
|
971
|
+
* @typeParam TServer - Server type
|
|
972
|
+
* @typeParam TContext - Extended context type, defaults to base ApiHandlerContext
|
|
973
|
+
*
|
|
974
|
+
* @example Basic middleware (no context extension)
|
|
975
|
+
* ```typescript
|
|
976
|
+
* const loggingMiddleware: Middleware = async (ctx, next) => {
|
|
977
|
+
* console.log(`${ctx.request.method} ${ctx.request.url}`);
|
|
978
|
+
* return next();
|
|
979
|
+
* };
|
|
980
|
+
* ```
|
|
981
|
+
*
|
|
982
|
+
* @example Middleware with extended context (use EcoMiddleware helper)
|
|
983
|
+
* ```typescript
|
|
984
|
+
* type AuthContext = ApiHandlerContext<BunRequest<string>, Server> & { user: User };
|
|
985
|
+
*
|
|
986
|
+
* const authMiddleware: EcoMiddleware<AuthContext> = async (ctx, next) => {
|
|
987
|
+
* const user = await authenticate(ctx.request);
|
|
988
|
+
* if (!user) return ctx.response.status(401).json({ error: 'Unauthorized' });
|
|
989
|
+
* ctx.user = user;
|
|
990
|
+
* return next();
|
|
991
|
+
* };
|
|
992
|
+
* ```
|
|
993
|
+
*/
|
|
994
|
+
export type Middleware<
|
|
995
|
+
TRequest extends Request = Request,
|
|
996
|
+
TServer = any,
|
|
997
|
+
TContext extends ApiHandlerContext<TRequest, TServer> = ApiHandlerContext<TRequest, TServer>,
|
|
998
|
+
> = (context: TContext, next: MiddlewareNext) => Promise<Response> | Response;
|
|
999
|
+
|
|
1000
|
+
/**
|
|
1001
|
+
* Helper type for defining middleware with extended context.
|
|
1002
|
+
* Automatically infers TRequest and TServer from the provided context type.
|
|
1003
|
+
*
|
|
1004
|
+
* @typeParam TContext - The extended context type that includes TRequest and TServer
|
|
1005
|
+
*
|
|
1006
|
+
* @example
|
|
1007
|
+
* ```typescript
|
|
1008
|
+
* type AuthContext = ApiHandlerContext<BunRequest<string>, Server> & {
|
|
1009
|
+
* session: { user: User };
|
|
1010
|
+
* };
|
|
1011
|
+
*
|
|
1012
|
+
* const authMiddleware: EcoMiddleware<AuthContext> = async (ctx, next) => {
|
|
1013
|
+
* const session = await authenticate(ctx.request);
|
|
1014
|
+
* if (!session) return Response.redirect('/login');
|
|
1015
|
+
* ctx.session = session;
|
|
1016
|
+
* return next();
|
|
1017
|
+
* };
|
|
1018
|
+
* ```
|
|
1019
|
+
*/
|
|
1020
|
+
export type EcoMiddleware<TContext extends ApiHandlerContext<any, any>> =
|
|
1021
|
+
TContext extends ApiHandlerContext<infer TRequest, infer TServer> ? Middleware<TRequest, TServer, TContext> : never;
|
|
1022
|
+
|
|
1023
|
+
/**
|
|
1024
|
+
* Represents an API handler in EcoPages.
|
|
1025
|
+
* Defines the path, method, handler function, optional middleware, and validation schemas.
|
|
1026
|
+
*
|
|
1027
|
+
* @example Basic handler
|
|
1028
|
+
* ```typescript
|
|
1029
|
+
* app.get('/users/:id', async (ctx) => {
|
|
1030
|
+
* return ctx.json({ id: ctx.params.id });
|
|
1031
|
+
* });
|
|
1032
|
+
* ```
|
|
1033
|
+
*
|
|
1034
|
+
* @example With validation
|
|
1035
|
+
* ```typescript
|
|
1036
|
+
* import { z } from 'zod';
|
|
1037
|
+
*
|
|
1038
|
+
* app.post('/posts', async (ctx) => {
|
|
1039
|
+
* const { title, content } = ctx.body;
|
|
1040
|
+
* return ctx.json({ success: true, title, content });
|
|
1041
|
+
* }, {
|
|
1042
|
+
* schema: {
|
|
1043
|
+
* body: z.object({
|
|
1044
|
+
* title: z.string().min(3),
|
|
1045
|
+
* content: z.string()
|
|
1046
|
+
* })
|
|
1047
|
+
* }
|
|
1048
|
+
* });
|
|
1049
|
+
* ```
|
|
1050
|
+
*
|
|
1051
|
+
* @example With middleware
|
|
1052
|
+
* ```typescript
|
|
1053
|
+
* const authMiddleware = async (ctx, next) => {
|
|
1054
|
+
* if (!ctx.request.headers.get('authorization')) {
|
|
1055
|
+
* return ctx.response.status(401).json({ error: 'Unauthorized' });
|
|
1056
|
+
* }
|
|
1057
|
+
* return next();
|
|
1058
|
+
* };
|
|
1059
|
+
*
|
|
1060
|
+
* app.get('/protected', async (ctx) => {
|
|
1061
|
+
* return ctx.json({ message: 'Secret data' });
|
|
1062
|
+
* }, {
|
|
1063
|
+
* middleware: [authMiddleware]
|
|
1064
|
+
* });
|
|
1065
|
+
* ```
|
|
1066
|
+
*
|
|
1067
|
+
* @example Multiple validations
|
|
1068
|
+
* ```typescript
|
|
1069
|
+
* import { z } from 'zod';
|
|
1070
|
+
*
|
|
1071
|
+
* app.post('/api/search', async (ctx) => {
|
|
1072
|
+
* const { q, page } = ctx.query;
|
|
1073
|
+
* const filters = ctx.body;
|
|
1074
|
+
* return ctx.json({ query: q, page, filters });
|
|
1075
|
+
* }, {
|
|
1076
|
+
* schema: {
|
|
1077
|
+
* query: z.object({ q: z.string(), page: z.string() }),
|
|
1078
|
+
* body: z.object({ category: z.string().optional() })
|
|
1079
|
+
* }
|
|
1080
|
+
* });
|
|
1081
|
+
* ```
|
|
1082
|
+
*/
|
|
1083
|
+
export interface ApiHandler<TPath extends string = string, TRequest extends Request = Request, TServer = any> {
|
|
1084
|
+
path: TPath;
|
|
1085
|
+
method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'OPTIONS' | 'HEAD';
|
|
1086
|
+
handler: (context: ApiHandlerContext<TRequest, TServer>) => Promise<Response> | Response;
|
|
1087
|
+
/** Optional middleware chain executed before the handler */
|
|
1088
|
+
middleware?: Middleware<TRequest, TServer, any>[];
|
|
1089
|
+
/** Optional validation schemas for request body, query parameters, and headers */
|
|
1090
|
+
schema?: {
|
|
1091
|
+
body?: StandardSchema;
|
|
1092
|
+
query?: StandardSchema;
|
|
1093
|
+
headers?: StandardSchema;
|
|
1094
|
+
params?: StandardSchema;
|
|
1095
|
+
};
|
|
1096
|
+
}
|
|
1097
|
+
|
|
1098
|
+
/**
|
|
1099
|
+
* Global error handler for centralized error handling across all routes.
|
|
1100
|
+
* Receives the error and full request context, returns a Response.
|
|
1101
|
+
* Falls back to default handling if the handler itself throws.
|
|
1102
|
+
*/
|
|
1103
|
+
export type ErrorHandler<TRequest extends Request = Request, TServer = any> = (
|
|
1104
|
+
error: unknown,
|
|
1105
|
+
context: ApiHandlerContext<TRequest, TServer>,
|
|
1106
|
+
) => Promise<Response> | Response;
|
|
1107
|
+
|
|
1108
|
+
/**
|
|
1109
|
+
* Options for route handlers.
|
|
1110
|
+
*/
|
|
1111
|
+
export interface RouteOptions<
|
|
1112
|
+
TRequest extends Request = Request,
|
|
1113
|
+
TServer = any,
|
|
1114
|
+
TContext extends ApiHandlerContext<TRequest, TServer> = ApiHandlerContext<TRequest, TServer>,
|
|
1115
|
+
> {
|
|
1116
|
+
middleware?: Middleware<TRequest, TServer, TContext>[];
|
|
1117
|
+
schema?: RouteSchema;
|
|
1118
|
+
}
|
|
1119
|
+
|
|
1120
|
+
export interface RouteSchema {
|
|
1121
|
+
body?: StandardSchema;
|
|
1122
|
+
query?: StandardSchema;
|
|
1123
|
+
headers?: StandardSchema;
|
|
1124
|
+
params?: StandardSchema;
|
|
1125
|
+
}
|
|
1126
|
+
|
|
1127
|
+
/**
|
|
1128
|
+
* Helper type to extract inferred types from a schema, with fallback to unknown.
|
|
1129
|
+
*/
|
|
1130
|
+
export type InferSchemaOutput<T> = T extends StandardSchema ? InferOutput<T> : unknown;
|
|
1131
|
+
|
|
1132
|
+
/**
|
|
1133
|
+
* Context with typed body/query/headers based on the provided schema.
|
|
1134
|
+
*/
|
|
1135
|
+
export type TypedApiHandlerContext<
|
|
1136
|
+
TSchema extends RouteSchema,
|
|
1137
|
+
TRequest extends Request = Request,
|
|
1138
|
+
TServer = any,
|
|
1139
|
+
> = Omit<ApiHandlerContext<TRequest, TServer>, 'body' | 'query' | 'headers'> & {
|
|
1140
|
+
body: InferSchemaOutput<TSchema['body']>;
|
|
1141
|
+
query: InferSchemaOutput<TSchema['query']>;
|
|
1142
|
+
headers: InferSchemaOutput<TSchema['headers']>;
|
|
1143
|
+
params: InferSchemaOutput<TSchema['params']>;
|
|
1144
|
+
};
|
|
1145
|
+
|
|
1146
|
+
/**
|
|
1147
|
+
* Options for the group method.
|
|
1148
|
+
*
|
|
1149
|
+
* @typeParam TContext - Extended context type that middleware provides to handlers
|
|
1150
|
+
*
|
|
1151
|
+
* @example Group with auth middleware extending context
|
|
1152
|
+
* ```typescript
|
|
1153
|
+
* type AuthContext = ApiHandlerContext<BunRequest<string>, Server> & { user: User };
|
|
1154
|
+
*
|
|
1155
|
+
* app.group<AuthContext>('/api', (r) => {
|
|
1156
|
+
* r.get('/profile', (ctx) => {
|
|
1157
|
+
* // ctx.user is properly typed!
|
|
1158
|
+
* return ctx.json({ name: ctx.user.name });
|
|
1159
|
+
* });
|
|
1160
|
+
* }, { middleware: [authMiddleware] });
|
|
1161
|
+
* ```
|
|
1162
|
+
*/
|
|
1163
|
+
export interface GroupOptions<
|
|
1164
|
+
TRequest extends Request = Request,
|
|
1165
|
+
TServer = any,
|
|
1166
|
+
TContext extends ApiHandlerContext<TRequest, TServer> = ApiHandlerContext<TRequest, TServer>,
|
|
1167
|
+
> {
|
|
1168
|
+
middleware?: Middleware<TRequest, TServer, TContext>[];
|
|
1169
|
+
}
|
|
1170
|
+
|
|
1171
|
+
/**
|
|
1172
|
+
* Context type that combines schema-typed fields with an extended base context.
|
|
1173
|
+
* Used by RouteGroupBuilder to merge schema validation types with middleware-extended context.
|
|
1174
|
+
* Note: Path parameter typing (e.g., :id -> params.id) comes from the TContext.request type.
|
|
1175
|
+
*/
|
|
1176
|
+
export type TypedGroupHandlerContext<TSchema extends RouteSchema, TContext extends ApiHandlerContext<any, any>> = Omit<
|
|
1177
|
+
TContext,
|
|
1178
|
+
'body' | 'query' | 'headers' | 'params'
|
|
1179
|
+
> & {
|
|
1180
|
+
body: InferSchemaOutput<TSchema['body']>;
|
|
1181
|
+
query: InferSchemaOutput<TSchema['query']>;
|
|
1182
|
+
headers: InferSchemaOutput<TSchema['headers']>;
|
|
1183
|
+
params: InferSchemaOutput<TSchema['params']>;
|
|
1184
|
+
};
|
|
1185
|
+
|
|
1186
|
+
/**
|
|
1187
|
+
* Builder interface for defining routes within a group.
|
|
1188
|
+
* Provides chainable methods for registering routes with shared prefix and middleware.
|
|
1189
|
+
*
|
|
1190
|
+
* @typeParam TRequest - The request type
|
|
1191
|
+
* @typeParam TServer - The server type
|
|
1192
|
+
* @typeParam TContext - Extended context type from group middleware
|
|
1193
|
+
*/
|
|
1194
|
+
export interface RouteGroupBuilder<
|
|
1195
|
+
TRequest extends Request = Request,
|
|
1196
|
+
TServer = any,
|
|
1197
|
+
TContext extends ApiHandlerContext<TRequest, TServer> = ApiHandlerContext<TRequest, TServer>,
|
|
1198
|
+
> {
|
|
1199
|
+
get<P extends string, TSchema extends RouteSchema = RouteSchema>(
|
|
1200
|
+
path: P,
|
|
1201
|
+
handler: (context: TypedGroupHandlerContext<TSchema, TContext>) => Promise<Response> | Response,
|
|
1202
|
+
options?: RouteOptions<TRequest, TServer, TContext> & { schema?: TSchema },
|
|
1203
|
+
): RouteGroupBuilder<TRequest, TServer, TContext>;
|
|
1204
|
+
|
|
1205
|
+
post<P extends string, TSchema extends RouteSchema = RouteSchema>(
|
|
1206
|
+
path: P,
|
|
1207
|
+
handler: (context: TypedGroupHandlerContext<TSchema, TContext>) => Promise<Response> | Response,
|
|
1208
|
+
options?: RouteOptions<TRequest, TServer, TContext> & { schema?: TSchema },
|
|
1209
|
+
): RouteGroupBuilder<TRequest, TServer, TContext>;
|
|
1210
|
+
|
|
1211
|
+
put<P extends string, TSchema extends RouteSchema = RouteSchema>(
|
|
1212
|
+
path: P,
|
|
1213
|
+
handler: (context: TypedGroupHandlerContext<TSchema, TContext>) => Promise<Response> | Response,
|
|
1214
|
+
options?: RouteOptions<TRequest, TServer, TContext> & { schema?: TSchema },
|
|
1215
|
+
): RouteGroupBuilder<TRequest, TServer, TContext>;
|
|
1216
|
+
|
|
1217
|
+
delete<P extends string, TSchema extends RouteSchema = RouteSchema>(
|
|
1218
|
+
path: P,
|
|
1219
|
+
handler: (context: TypedGroupHandlerContext<TSchema, TContext>) => Promise<Response> | Response,
|
|
1220
|
+
options?: RouteOptions<TRequest, TServer, TContext> & { schema?: TSchema },
|
|
1221
|
+
): RouteGroupBuilder<TRequest, TServer, TContext>;
|
|
1222
|
+
|
|
1223
|
+
patch<P extends string, TSchema extends RouteSchema = RouteSchema>(
|
|
1224
|
+
path: P,
|
|
1225
|
+
handler: (context: TypedGroupHandlerContext<TSchema, TContext>) => Promise<Response> | Response,
|
|
1226
|
+
options?: RouteOptions<TRequest, TServer, TContext> & { schema?: TSchema },
|
|
1227
|
+
): RouteGroupBuilder<TRequest, TServer, TContext>;
|
|
1228
|
+
|
|
1229
|
+
options<P extends string, TSchema extends RouteSchema = RouteSchema>(
|
|
1230
|
+
path: P,
|
|
1231
|
+
handler: (context: TypedGroupHandlerContext<TSchema, TContext>) => Promise<Response> | Response,
|
|
1232
|
+
options?: RouteOptions<TRequest, TServer, TContext> & { schema?: TSchema },
|
|
1233
|
+
): RouteGroupBuilder<TRequest, TServer, TContext>;
|
|
1234
|
+
|
|
1235
|
+
head<P extends string, TSchema extends RouteSchema = RouteSchema>(
|
|
1236
|
+
path: P,
|
|
1237
|
+
handler: (context: TypedGroupHandlerContext<TSchema, TContext>) => Promise<Response> | Response,
|
|
1238
|
+
options?: RouteOptions<TRequest, TServer, TContext> & { schema?: TSchema },
|
|
1239
|
+
): RouteGroupBuilder<TRequest, TServer, TContext>;
|
|
1240
|
+
}
|
|
1241
|
+
|
|
1242
|
+
/**
|
|
1243
|
+
* A function that dynamically imports a view module.
|
|
1244
|
+
* Used by app.static() to enable HMR in development.
|
|
1245
|
+
*/
|
|
1246
|
+
export type ViewLoader<P = any> = () => Promise<{ default: EcoPageComponent<P> }>;
|
|
1247
|
+
|
|
1248
|
+
/**
|
|
1249
|
+
* Represents a static route registered via app.static().
|
|
1250
|
+
* Uses a loader function to enable HMR in development mode.
|
|
1251
|
+
*/
|
|
1252
|
+
export interface StaticRoute<P = any> {
|
|
1253
|
+
path: string;
|
|
1254
|
+
loader: ViewLoader<P>;
|
|
1255
|
+
}
|