Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IfExists parameter for Routes to define if a second Route should Error, Overwrite or be Skipped #1042

Merged
merged 2 commits into from
Dec 16, 2022

Conversation

Badgerati
Copy link
Owner

Description of the Change

Adds a new -IfExists parameter to Add-PodeRoute, Add-PodeStaticRoute, and Add-PodeSignalRoute - including the relevant Group functions like Add-PodeRouteGroup. This parameter takes the values Default, Error, Overwrite, and Skip. (the default behaviour is Error, as it currently is)

The same parameter has also been added onto Use-PodeRoutes, meaning you can apply Skip to all Routes on mass:

Use-PodeRoutes -IfExists Skip

Furthermore, there's also a new Set-PodeRouteIfExistsPreference function which let's you overwrite the global default behaviour of Error:

Set-PodeRouteIfExistsPreference -Value Overwrite

In the case of the options:

  • Default: will use the IfExists value from higher up the hierarchy (see below) - if none defined, Error is the final default
  • Error: if the Route already exists, throw an error
  • Overwrite: if the Route already exists, delete the existing Route and recreate with the new definition
  • Skip: if the Route already exists, skip over the Route and do nothing

The default value that is used is defined by the following hierarchy:

  • IfExists value of the current Route (like Add-PodeRoute)
  • IfExists value of the Route Group (like Add-PodeRouteGroup)
  • IfExists value from Use-PodeRoute
  • IfExists value from Set-PodeRouteIfExistsPreference
  • Throw an Error

This also adds 3 new Test functions:

  • Test-PodeRoute
  • Test-PodeStaticRoute
  • Test-PodeSignalRoute

Related Issue

Resolves #964

Examples

To Skip over creating a Route that already exists:

Start-PodeServer -Thread 2 -Verbose {
    Add-PodeEndpoint -Address * -Port 8090 -Protocol Http

    Add-PodeRoute -Method Get -Path '/' -ScriptBlock {
        Write-PodeJsonResponse -Value @{ Result = 1 }
    }

    Add-PodeRoute -Method Get -Path '/' -IfExists Skip -ScriptBlock {
        Write-PodeJsonResponse -Value @{ Result = 2 }
    }
}

Running the above, and calling http://localhost:8090/ will return the result of 1 not 2 😄. If you swap Skip to Overwrite and re-run, you'll get the result of 2 instead!

@Badgerati Badgerati added this to the 2.8.0 milestone Dec 10, 2022
@Badgerati Badgerati self-assigned this Dec 10, 2022
@Badgerati Badgerati merged commit ea42c85 into develop Dec 16, 2022
@Badgerati Badgerati deleted the Issue-964 branch December 16, 2022 19:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Invoking "Use-PodeRoutes" as schedule
1 participant