Skip to content

Commit

Permalink
refactor(core): add REQUEST, RESPONSE_INIT and REQUEST_CONTEXT
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
AndrewKushnir committed Nov 14, 2024
1 parent 64cfe18 commit 88dbe7b
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
9 changes: 9 additions & 0 deletions goldens/public-api/core/index.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1562,6 +1562,12 @@ export interface RendererType2 {
styles: string[];
}

// @public
export const REQUEST: InjectionToken<Request | null>;

// @public
export const REQUEST_CONTEXT: InjectionToken<unknown>;

// @public
export function resolveForwardRef<T>(type: T): T;

Expand Down Expand Up @@ -1618,6 +1624,9 @@ export enum ResourceStatus {
Resolved = 4
}

// @public
export const RESPONSE_INIT: InjectionToken<ResponseInit | null>;

// @public
export function runInInjectionContext<ReturnT>(injector: Injector, fn: () => ReturnT): ReturnT;

Expand Down
70 changes: 70 additions & 0 deletions packages/core/src/application/platform_tokens.ts
Original file line number Diff line number Diff line change
@@ -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 | null>('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<ResponseInit | null>('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<unknown>('REQUEST_CONTEXT', {
providedIn: 'platform',
factory: () => null,
});
1 change: 1 addition & 0 deletions packages/core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 88dbe7b

Please # to comment.