-
Notifications
You must be signed in to change notification settings - Fork 12k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(@angular/ssr): add
createRequestHandler
and `createNodeRequest…
…Handler `utilities Introduced the `createRequestHandler` and `createNodeRequestHandler` utilities to expose middleware functions from the `server.ts` entry point for use with Vite. This provides flexibility in integrating different server frameworks, including Express, Hono, and Fastify, with Angular SSR. Examples: **Express** ```ts export default createNodeRequestHandler(app); ``` **Nest.js** ```ts const app = await NestFactory.create(AppModule); export default createNodeRequestHandler(app); ``` **Hono** ```ts const app = new Hono(); export default createRequestHandler(app.fetch); ``` **Fastify** ```ts export default createNodeRequestHandler(async (req, res) => { await app.ready(); app.server.emit('request', req, res); }); ```
- Loading branch information
1 parent
bbc2901
commit 92209dd
Showing
12 changed files
with
326 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/** | ||
* @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 type { IncomingMessage, ServerResponse } from 'node:http'; | ||
|
||
/** | ||
* Represents a middleware function for handling HTTP requests in a Node.js environment. | ||
* | ||
* @param req - The incoming HTTP request object. | ||
* @param res - The outgoing HTTP response object. | ||
* @param next - A callback function that signals the completion of the middleware or forwards the error if provided. | ||
* | ||
* @returns A Promise that resolves to void or simply void. The handler can be asynchronous. | ||
*/ | ||
type RequestHandlerFunction = ( | ||
req: IncomingMessage, | ||
res: ServerResponse, | ||
next: (err?: unknown) => void, | ||
) => Promise<void> | void; | ||
|
||
/** | ||
* Attaches metadata to the handler function to mark it as a special handler for Node.js environments. | ||
* | ||
* @typeParam T - The type of the handler function. | ||
* @param handler - The handler function to be defined and annotated. | ||
* @returns The same handler function passed as an argument, with metadata attached. | ||
* | ||
* @example | ||
* Usage in an Express application: | ||
* ```ts | ||
* const app = express(); | ||
* export default createNodeRequestHandler(app); | ||
* ``` | ||
* | ||
* @example | ||
* Usage in a Hono application: | ||
* ```ts | ||
* const app = new Hono(); | ||
* export default createNodeRequestHandler(async (req, res, next) => { | ||
* try { | ||
* const webRes = await app.fetch(createWebRequestFromNodeRequest(req)); | ||
* if (webRes) { | ||
* await writeResponseToNodeResponse(webRes, res); | ||
* } else { | ||
* next(); | ||
* } | ||
* } catch (error) { | ||
* next(error); | ||
* } | ||
* })); | ||
* ``` | ||
* | ||
* @example | ||
* Usage in a Fastify application: | ||
* ```ts | ||
* const app = Fastify(); | ||
* export default createNodeRequestHandler(async (req, res) => { | ||
* await app.ready(); | ||
* app.server.emit('request', req, res); | ||
* res.send('Hello from Fastify with Node Next Handler!'); | ||
* })); | ||
* ``` | ||
* @developerPreview | ||
*/ | ||
export function createNodeRequestHandler<T extends RequestHandlerFunction>(handler: T): T { | ||
(handler as T & { __ng_node_request_handler__?: boolean })['__ng_node_request_handler__'] = true; | ||
|
||
return handler; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* @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 | ||
*/ | ||
|
||
/** | ||
* Function for handling HTTP requests in a web environment. | ||
* | ||
* @param request - The incoming HTTP request object. | ||
* @returns A Promise resolving to a `Response` object, `null`, or directly a `Response`, | ||
* supporting both synchronous and asynchronous handling. | ||
*/ | ||
type RequestHandlerFunction = (request: Request) => Promise<Response | null> | null | Response; | ||
|
||
/** | ||
* Annotates a request handler function with metadata, marking it as a special | ||
* handler. | ||
* | ||
* @param handler - The request handler function to be annotated. | ||
* @returns The same handler function passed in, with metadata attached. | ||
* | ||
* @example | ||
* Example usage in a Hono application: | ||
* ```ts | ||
* const app = new Hono(); | ||
* export default createRequestHandler(app.fetch); | ||
* ``` | ||
* | ||
* @example | ||
* Example usage in a H3 application: | ||
* ```ts | ||
* const app = createApp(); | ||
* const handler = toWebHandler(app); | ||
* export default createRequestHandler(handler); | ||
* ``` | ||
* @developerPreview | ||
*/ | ||
export function createRequestHandler(handler: RequestHandlerFunction): RequestHandlerFunction { | ||
(handler as RequestHandlerFunction & { __ng_request_handler__?: boolean })[ | ||
'__ng_request_handler__' | ||
] = true; | ||
|
||
return handler; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.