From 8b0d370a859834870013a1da39c94fbd7a686ab5 Mon Sep 17 00:00:00 2001 From: Daniele Teti Date: Mon, 5 Feb 2024 16:30:02 +0100 Subject: [PATCH] Merge commit '547fec74b9dcbf32887aad4fdd6348685c654ef2' into feature_filters # Conflicts: # sources/MVCFramework.Filters.CORS.pas # sources/MVCFramework.pas # unittests/general/Several/LiveServerTestU.pas --- .../htmx => contrib}/MVCFramework.HTMX.pas | 37 ++++++++++++++----- .../MVCFramework.Middleware.StaticFiles.pas | 4 +- sources/MVCFramework.pas | 9 ++--- 3 files changed, 32 insertions(+), 18 deletions(-) rename {samples/htmx => contrib}/MVCFramework.HTMX.pas (93%) diff --git a/samples/htmx/MVCFramework.HTMX.pas b/contrib/MVCFramework.HTMX.pas similarity index 93% rename from samples/htmx/MVCFramework.HTMX.pas rename to contrib/MVCFramework.HTMX.pas index 3c90ce37..e191fc37 100644 --- a/samples/htmx/MVCFramework.HTMX.pas +++ b/contrib/MVCFramework.HTMX.pas @@ -1,4 +1,4 @@ -// *************************************************************************** +// *************************************************************************** // // Delphi MVC Framework // @@ -170,7 +170,12 @@ type /// Using events gives you a lot of flexibility to add functionality to normal htmx responses. /// A collection of the names of the javscript events to be triggered /// The timing of the event - function HXTriggerClientEvents(Names: TArray; After: TClientEventType = etReceived): TMVCWebResponse; + function HXTriggerClientEvents(Names: TArray; After: TClientEventType = etReceived): TMVCWebResponse; overload; + + /// Allows you to trigger a collection of client side events. + /// Using events gives you a lot of flexibility to add functionality to normal htmx responses. + /// A JSON object with events descriptors (https://htmx.org/headers/hx-trigger/) + function HXTriggerClientEvents(EventsDescriptors: TJSONObject; After: TClientEventType = etReceived): TMVCWebResponse; overload; /// Allows you to trigger a client side event with parameters. /// Using events gives you a lot of flexibility to add functionality to normal htmx responses. @@ -392,19 +397,19 @@ end; function THTMXResponseHelper.HXTriggerClientEvent(Name: string; Params: TValue; After: TClientEventType): TMVCWebResponse; var - ser: TMVCJsonDataObjectsSerializer; - Data: TJsonObject; + lSer: TMVCJsonDataObjectsSerializer; + lData: TJsonObject; begin if not Params.IsEmpty then begin - Data := TJsonObject.Create; - ser := TMVCJsonDataObjectsSerializer.Create; + lData := TJsonObject.Create; + lSer := TMVCJsonDataObjectsSerializer.Create; try - ser.TValueToJSONObjectProperty(Data, Name, Params, stdefault, [], []); - SetCustomHeader(ClientEventTypes[After], Data.ToJSON); + lSer.TValueToJSONObjectProperty(lData, Name, Params, stdefault, [], []); + SetCustomHeader(ClientEventTypes[After], lData.ToJSON); finally - ser.Free; - Data.Free; + lSer.Free; + lData.Free; end; end else @@ -413,6 +418,18 @@ begin Result := Self; end; +function THTMXResponseHelper.HXTriggerClientEvents(EventsDescriptors: TJSONObject; + After: TClientEventType): TMVCWebResponse; +begin + if EventsDescriptors = nil then + begin + Exit(Self); + end; + + SetCustomHeader(ClientEventTypes[After], EventsDescriptors.ToJSON(true)); + Result := Self; +end; + function THTMXResponseHelper.HXTriggerClientEvents(Names: TArray; After: TClientEventType): TMVCWebResponse; var Value: string; diff --git a/sources/MVCFramework.Middleware.StaticFiles.pas b/sources/MVCFramework.Middleware.StaticFiles.pas index 14a83014..96cd5422 100644 --- a/sources/MVCFramework.Middleware.StaticFiles.pas +++ b/sources/MVCFramework.Middleware.StaticFiles.pas @@ -1,4 +1,4 @@ -// *************************************************************************** +// *************************************************************************** // // Delphi MVC Framework // @@ -345,7 +345,7 @@ begin end; TMVCStaticContents.SendFile(AFileName, lContentType, AContext); Result := True; - Log(TLogLevel.levDebug, AContext.Request.HTTPMethodAsString + ':' + + LogI(AContext.Request.HTTPMethodAsString + ':' + AContext.Request.PathInfo + ' [' + AContext.Request.ClientIp + '] -> ' + ClassName + ' - ' + IntToStr(AContext.Response.StatusCode) + ' ' + AContext.Response.ReasonString); diff --git a/sources/MVCFramework.pas b/sources/MVCFramework.pas index 8293dbf1..78d2cd11 100644 --- a/sources/MVCFramework.pas +++ b/sources/MVCFramework.pas @@ -2456,15 +2456,15 @@ begin case RouterLogState of rlsRouteFound: begin - Log(TLogLevel.levNormal, Context.Request.HTTPMethodAsString + ':' + + LogI(Context.Request.HTTPMethodAsString + ':' + Context.Request.PathInfo + ' [' + Context.Request.ClientIp + '] -> ' + Sender.GetQualifiedActionName + ' - ' + IntToStr(Context.Response.StatusCode) + ' ' + Context.Response.ReasonString); end; rlsRouteNotFound: begin - Log(TLogLevel.levNormal, Context.Request.HTTPMethodAsString + ':' + - Context.Request.PathInfo + ' [' + Context.Request.ClientIp + '] -> {NOT FOUND} - ' + + LogW(Context.Request.HTTPMethodAsString + ':' + + Context.Request.PathInfo + ' [' + Context.Request.ClientIp + '] -> {ROUTE NOT FOUND} - ' + IntToStr(Context.Response.StatusCode) + ' ' + Context.Response.ReasonString); end; else @@ -2789,9 +2789,6 @@ begin lContext.Response.StatusCode := http_status.NotFound; lContext.Response.ReasonString := 'Not Found'; fOnRouterLog(lRouter, rlsRouteNotFound, lContext); - raise EMVCException.Create(lContext.Response.ReasonString, - lContext.Request.HTTPMethodAsString + ' ' + lContext.Request.PathInfo, 0, - http_status.NotFound); end else begin