From 66c8254722a701e46e1f755547277c327e1cb0aa Mon Sep 17 00:00:00 2001 From: Andrew Kushnir Date: Thu, 14 Nov 2024 12:06:30 -0800 Subject: [PATCH] refactor(core): add `REQUEST`, `RESPONSE_INIT` and `REQUEST_CONTEXT` tokens This commit introduces the `REQUEST`, `RESPONSE_INIT` and `REQUEST_CONTEXT` tokens, which will replace similar ones from https://github.com/angular/angular-cli/blob/28503186230b5e22b84499641d56c9c981fdab1d/packages/angular/ssr/tokens/src/tokens.ts, so those tokens would be imported in application code via `@angular/core` package. --- adev/src/assets/BUILD.bazel | 1 - goldens/public-api/core/index.api.md | 9 +++ packages/BUILD.bazel | 1 - .../core/src/application/platform_tokens.ts | 70 +++++++++++++++++++ packages/core/src/core.ts | 1 + .../bundling/defer/bundle.golden_symbols.json | 3 + packages/ssr/BUILD.bazel | 10 --- 7 files changed, 83 insertions(+), 12 deletions(-) create mode 100644 packages/core/src/application/platform_tokens.ts diff --git a/adev/src/assets/BUILD.bazel b/adev/src/assets/BUILD.bazel index ddda62187e960..99599093c2875 100644 --- a/adev/src/assets/BUILD.bazel +++ b/adev/src/assets/BUILD.bazel @@ -71,7 +71,6 @@ copy_to_directory( "//packages/service-worker:service-worker_docs", "//packages/ssr:ssr_docs", "//packages/ssr:ssr_node_docs", - "//packages/ssr:ssr_tokens_docs", "//packages/upgrade:upgrade_docs", "//packages/upgrade/static:upgrade_static_docs", "//packages/upgrade/static/testing:upgrade_static_testing_docs", diff --git a/goldens/public-api/core/index.api.md b/goldens/public-api/core/index.api.md index 057fe9a5c8f06..12f0955403786 100644 --- a/goldens/public-api/core/index.api.md +++ b/goldens/public-api/core/index.api.md @@ -1562,6 +1562,12 @@ export interface RendererType2 { styles: string[]; } +// @public +export const REQUEST: InjectionToken; + +// @public +export const REQUEST_CONTEXT: InjectionToken; + // @public export function resolveForwardRef(type: T): T; @@ -1618,6 +1624,9 @@ export enum ResourceStatus { Resolved = 4 } +// @public +export const RESPONSE_INIT: InjectionToken; + // @public export function runInInjectionContext(injector: Injector, fn: () => ReturnT): ReturnT; diff --git a/packages/BUILD.bazel b/packages/BUILD.bazel index ef895da0a5e98..8fa6bc9be5dbb 100644 --- a/packages/BUILD.bazel +++ b/packages/BUILD.bazel @@ -101,7 +101,6 @@ generate_api_manifest( "//packages/service-worker:service-worker_docs_extraction", "//packages/ssr:ssr_docs_extraction", "//packages/ssr:ssr_node_docs_extraction", - "//packages/ssr:ssr_tokens_docs_extraction", "//packages/upgrade:upgrade_docs_extraction", "//packages/upgrade/static:upgrade_static_docs_extraction", "//packages/upgrade/static/testing:upgrade_static_testing_docs_extraction", diff --git a/packages/core/src/application/platform_tokens.ts b/packages/core/src/application/platform_tokens.ts new file mode 100644 index 0000000000000..268cb162fc662 --- /dev/null +++ b/packages/core/src/application/platform_tokens.ts @@ -0,0 +1,70 @@ +/** + * @license + * Copyright Google LLC All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://angular.dev/license + */ + +import {InjectionToken} from '../di/injection_token'; + +/** + * Injection token representing the current HTTP request object. + * + * Use this token to access the current request when handling server-side + * rendering (SSR). + * + * @remarks + * This token may be `null` in the following scenarios: + * + * * During the build processes. + * * When the application is rendered in the browser (client-side rendering). + * * When performing static site generation (SSG). + * * During route extraction in development (at the time of the request). + * + * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Request | `Request` on MDN} + * + * @developerPreview + */ +export const REQUEST = new InjectionToken('REQUEST', { + providedIn: 'platform', + factory: () => null, +}); + +/** + * Injection token for response initialization options. + * + * Use this token to provide response options for configuring or initializing + * HTTP responses in server-side rendering or API endpoints. + * + * @remarks + * This token may be `null` in the following scenarios: + * + * * During the build processes. + * * When the application is rendered in the browser (client-side rendering). + * * When performing static site generation (SSG). + * * During route extraction in development (at the time of the request). + * + * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Response/Response | `ResponseInit` on MDN} + * + * @developerPreview + */ +export const RESPONSE_INIT = new InjectionToken('RESPONSE_INIT', { + providedIn: 'platform', + factory: () => null, +}); + +/** + * Injection token for additional request context. + * + * Use this token to pass custom metadata or context related to the current request in server-side rendering. + * + * @remarks + * This token is only available during server-side rendering and will be `null` in other contexts. + * + * @developerPreview + */ +export const REQUEST_CONTEXT = new InjectionToken('REQUEST_CONTEXT', { + providedIn: 'platform', + factory: () => null, +}); diff --git a/packages/core/src/core.ts b/packages/core/src/core.ts index abc81954782fe..e801bc009c347 100644 --- a/packages/core/src/core.ts +++ b/packages/core/src/core.ts @@ -115,6 +115,7 @@ export { export {ApplicationConfig, mergeApplicationConfig} from './application/application_config'; export {makeStateKey, StateKey, TransferState} from './transfer_state'; export {booleanAttribute, numberAttribute} from './util/coercion'; +export {REQUEST, REQUEST_CONTEXT, RESPONSE_INIT} from './application/platform_tokens'; import {global} from './util/global'; if (typeof ngDevMode !== 'undefined' && ngDevMode) { diff --git a/packages/core/test/bundling/defer/bundle.golden_symbols.json b/packages/core/test/bundling/defer/bundle.golden_symbols.json index bbe9084d1277a..64242874b193c 100644 --- a/packages/core/test/bundling/defer/bundle.golden_symbols.json +++ b/packages/core/test/bundling/defer/bundle.golden_symbols.json @@ -1886,6 +1886,9 @@ { "name": "init_platform_ref" }, + { + "name": "init_platform_tokens" + }, { "name": "init_profiler" }, diff --git a/packages/ssr/BUILD.bazel b/packages/ssr/BUILD.bazel index d5d788a1b9dde..8d4f1c3ac0690 100644 --- a/packages/ssr/BUILD.bazel +++ b/packages/ssr/BUILD.bazel @@ -22,13 +22,3 @@ generate_api_docs( entry_point = "@npm//:node_modules/@angular/ssr/node/index.d.ts", module_name = "@angular/ssr/node", ) - -generate_api_docs( - name = "ssr_tokens_docs", - srcs = [ - "//packages:common_files_and_deps_for_docs", - "@npm//@angular/ssr", - ], - entry_point = "@npm//:node_modules/@angular/ssr/tokens/index.d.ts", - module_name = "@angular/ssr/tokens", -)