This repository has been archived by the owner on Dec 8, 2021. It is now read-only.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Middleware Support in Polaris
This PR contains the code to enable middleware in Polaris - with it comes BREAKING CHANGES
What is Middleware?
Middleware is a concept that's been around for quite some time. In web frameworks, middleware is code that is able to manipulate the request that comes in before it hits your route logic. For Polaris, middleware gets run AFTER a request comes in, but BEFORE your route script block is run - hence, MIDDLEware.
I'll go into an example but first I need to talk about an important BREAKING CHANGE.
Breaking Changes
There are 2 major breaking changes.
param($request, $response)
line at the top of the script. These parameters are made global in the scope of the script and will exist without this param line. This means that YOU MUST REMOVE THIS LINE WITH THESE CHANGES. Why? Because the global params are $Request and $Response. If you haveparam($request, $response)
, that will take priority over the global params and what you will have instead is two null variables. REMOVE THAT LINE. It's much easier if you do. 😄$request.QueryParameters
has been renamed to$request.Query
because "Parameters" is a very long word.Ok. On to that example. Did you remove those param lines yet? Do it. Seriously.
Example
A very important middleware for Polaris is a JSON body parser. This will take the stream of the HTTP body (which is a string representation of the JSON body) and convert it into a PowerShell object to allow us to interact with the body data. Let's write a middleware that does this.
Like our Route scripts, the middleware is also just a PowerShell script:
Now that $Request is made available globally, we can check if the raw body string exists. If it does, we can parse that string and store it in the $Request.Body.
Then, we're able to use this body in our Route scripts like so:
When you do an
Invoke-RestMethod
you get the expected result:It's really simple to get started. You can add any number of middlewares - they will be run in the order that they are added to Polaris.
But of course, since a JSON Body parser will be used in a lot of situations, we have one built in. All you have to do is flip the switch.
When you're ready to start your server, add this flag and it will turn on that exact middleware code above:
or use the helper cmdlet:
Use-JsonBodyParserMiddleware
Misc
This PR also includes helper cmdlets to:
Remove-PolarisRouteMiddleware
Remove-PolarisWebRoute
Get-PolarisWebRoute
Get-PolarisRouteMiddleware
That's all for now. We're really excited to see what you come up with - using middleware.
Tyler