Merge commit '547fec74b9dcbf32887aad4fdd6348685c654ef2' into feature_filters

# Conflicts:
#	sources/MVCFramework.Filters.CORS.pas
#	sources/MVCFramework.pas
#	unittests/general/Several/LiveServerTestU.pas
This commit is contained in:
Daniele Teti 2024-02-05 16:30:02 +01:00
parent d1b14eb24a
commit 8b0d370a85
3 changed files with 32 additions and 18 deletions

View File

@ -1,4 +1,4 @@
// ***************************************************************************
// ***************************************************************************
//
// Delphi MVC Framework
//
@ -170,7 +170,12 @@ type
/// <remarks>Using events gives you a lot of flexibility to add functionality to normal htmx responses.</remarks>
/// <param name="Names">A collection of the names of the javscript events to be triggered</param>
/// <param name="After">The timing of the event</param>
function HXTriggerClientEvents(Names: TArray<string>; After: TClientEventType = etReceived): TMVCWebResponse;
function HXTriggerClientEvents(Names: TArray<string>; After: TClientEventType = etReceived): TMVCWebResponse; overload;
/// <summary>Allows you to trigger a collection of client side events.</summary>
/// <remarks>Using events gives you a lot of flexibility to add functionality to normal htmx responses.</remarks>
/// <param name="EventsDescriptors">A JSON object with events descriptors (https://htmx.org/headers/hx-trigger/)</param>
function HXTriggerClientEvents(EventsDescriptors: TJSONObject; After: TClientEventType = etReceived): TMVCWebResponse; overload;
/// <summary>Allows you to trigger a client side event with parameters.</summary>
/// <remarks>Using events gives you a lot of flexibility to add functionality to normal htmx responses.</remarks>
@ -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<string>; After: TClientEventType): TMVCWebResponse;
var
Value: string;

View File

@ -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);

View File

@ -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