Review main Abandoned

65708[WIP][POC][FEATURE] Add TypeScript middleware implementation for AJAX requests

Benjamin Franzke CR 0 V 0 Normal +84 −14 updated 05/20/25, 12:17

Commit message

The added middleware is a single-pass middleware (as in PHP PSR-15 middlewares)
but uses callbacks instead of objects (as in classic node.js express middlewares).

Background: As the TypeScript `types` directive allows to declare the type of a
callback method, we can use callbacks to model the middleware pipeline, so we
do not need objects + interfaces as in PSR-15 (where objects where actually only
used to ensure type-safety via interfaces).

A middleware is implemented as follows:

  import AjaxRequest = require('TYPO3/CMS/Core/Ajax/AjaxRequest')
  import type {AjaxMiddleware, RequestFetcher} from 'TYPO3/CMS/Core/Ajax/AjaxRequestTypes'

  let myMiddleware: AjaxMiddleware = (request: Request, next: RequestFetcher) => {
    // apply preprocessing to request
    const requestClone = request.clone();

    return next(request).then((response: Response): Response => {
      // apply post processing to response
      const contentType = response.headers.get('content-type');
      // do something, use clone if accessing body,
      // or following middleware can't acces body.
      const clonedReponse = response.clone()
      return clonedReponse.json().then(function(body) {
        // do something with the body data
        console.log('request received', body)

        // pass on original response to the outer middleware
        return resolve(response);
      });
    }
  }

  new AjaxRequest('/endpoint').addMiddleware(myMiddleware).get()

TODO:

 * RST
 * Add unit tests to AjaxRequestTest.ts
 * Add interface for "globally" registered middlewares
   for *all* AJAX requests? In current state of this POC
   the user (in this case AjaxDataHandler) of the AjaxRequest
   module defines which middlewares are to be executed
   => "local" middlewares currently
 * Decide whether to name the method .addMiddleware() or
   whether we should add .withMiddleware() and clone the
   object
 * Decide for a type name and general term for the "next"/"handler"
   callback.
   RequestFetcher is leaning towards the RequestHandler term
   used in PSR-15 for the opposite thing. Express uses the
   simple term "next", which is short and easy to write,
   but may be a bit undescriptive for developers that are
   not used to node.js express server side middlewares.
 * Intended as replacement for
   https://review.typo3.org/c/Packages/TYPO3.CMS/+/65612/2
   to demonstrate middlewares as an alternative approach.
   => But new FEATURE, so not applicable for 10.4

Review

  • Code review CR 0
  • Verified V 0

Files 8

Review log 79

  1. Benjamin Franzke uploaded patch set 1 09/12/20, 20:48

  2. TYPO3com voted on patch set 1 V -1 09/12/20, 21:04 Completed build in 16m 16s on Sat, 12 Sep, 09:04 PM Test Summary: 27 of 73694 failed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-5567

  3. Benjamin Franzke uploaded patch set 2 09/12/20, 21:52

  4. TYPO3com voted on patch set 2 V +1 09/12/20, 22:12 Completed build in 20m 01s on Sat, 12 Sep, 10:12 PM Test Summary: 73208 passed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-5568

  5. Oliver Hader commented on patch set 2 09/13/20, 20:32

    Thx, I'll give it a try with ext:sudo_mode the next few days... * sudo_modo probably then would have to check PHP PageRender/AssetRender whether AjaxDataHandler is present to be loaded as RequireJS module - in case only ContextMenuActions is required as module, that might be tricky // as an alternative the RequireJS state could be analyzed as well (specified or defined) * middleware probably require some sort of scalar identifier to make use of them with more than two middleware levels - basically in terms of "insert before/after"
  6. Benni Mack wrote 11/29/21, 10:04

    Change destination moved from master to main
  7. Benni Mack updated the commit message in patch set 3 11/29/21, 13:22

  8. Benjamin Franzke abandoned the change 05/20/25, 12:14

  9. Benjamin Franzke commented on patch set 3 05/20/25, 12:17