mevdschee / php-crud-api

Single file PHP script that adds a REST API to a SQL database

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

"Request Tranformer" middleware

vascanera opened this issue · comments

Hi Maurits & all,
Would it be possible (what would be the effort level?) to implement a "request transformer" middleware so that we could have a function (executed first among the middlewares) that receives the global Request object as the input, transforms it as desired, and then return a modified (transformed) request object that the API will use from here on for the current process?

This would solve the case of masking (hiding) implementation details of a url route on the client side without resorting to other frameworks / libraries (the last piece of the puzzle for building really nice custom API routes using just this awesome lib).

Example:
On the client, when I click a submit button, I don't want the url to show sensitive data, like real user ids or real column names as they are stored in the database.
I would like to access: api/users/f3xh4hc33a instead of api.php/records/users/3
The API would need to be aware of this request and use if for subsequent operations (and not use the global Request object) from this point on until finishing the current process.
Such function could look something like:

function transformRequest ($originalRequest) { 
     $transformedRequest = $originalRequest;
     // ...;
     // ... split request into parts; 
     // ... transform request parts as required;
     $transformedRequest.url = '/records/users/3'; 
     $transformedRequest.method = 'POST'; 
     // ...;

     return $transformedRequest;
}

Hi @vascanera, My understanding is that what you want is exactly what the customization.beforeHandler is designed to facilitate, see:

$result = call_user_func($beforeHandler, $operation, $tableName, $request, $environment);

Kind regards, Maurits

You are absolutely right, that is exactly what I need. How in the world did I miss that part from the docs?...
Thank you!