-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit bd7e1e4
Showing
8 changed files
with
235 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[Tt]emp/ | ||
ps_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) [2019] [Matthew Kelly (Badgerati)] | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Pode YAML | ||
|
||
This is an extension module for the [Pode](https://github.com/Badgerati/Pode) web server (v1.3.0+). It allows you to parse and send YAML requests and responses. | ||
|
||
## Install | ||
|
||
> Note: this module has a dependency on the [powershell-yaml](https://www.powershellgallery.com/packages/powershell-yaml/0.4.1) module | ||
You can either install this module globally: | ||
|
||
```powershell | ||
Install-Module -Name powershell-yaml | ||
Install-Module -Name Pode.Yaml | ||
``` | ||
|
||
or you can let Pode install it for you locally, by adding the following into your `package.json`: | ||
|
||
```json | ||
"modules": { | ||
"powershell-yaml": "latest", | ||
"pode.yaml": "latest" | ||
} | ||
``` | ||
|
||
## Usage | ||
|
||
### Body Parser | ||
|
||
You'll need to first import the module, and then enable it as a body-parser within your server's scriptblock: | ||
|
||
```powershell | ||
Import-PodeModule -Name Pode.Yaml -Now | ||
Enable-PodeYamlBodyParser | ||
``` | ||
|
||
### Response | ||
|
||
You can respond back with YAML in a Route as follows: | ||
|
||
```powershell | ||
Add-PodeRoute -Method Get -Path '/' -ScriptBlock { | ||
Write-PodeYamlResponse -Value @{ Name = 'example' } | ||
} | ||
``` | ||
|
||
### Content Type | ||
|
||
The default content type expected on the request, and used on the response is `application/x-yaml`. You can change this by specifying the `-ContentType` parameter on the above `PodeYaml` functions. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"name": "pode-yaml-example", | ||
"version": "1.0.0", | ||
"scripts": { | ||
"start": "./rest-api-yaml.ps1" | ||
}, | ||
"modules": { | ||
"Pode.Yaml": "1.0.0" | ||
}, | ||
"author": "Matthew Kelly (Badgerati)", | ||
"license": "MIT" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<# | ||
This example can be started as follows: | ||
> pode install | ||
> pode start | ||
#> | ||
Start-PodeServer -Threads 2 { | ||
|
||
# listen on localhost:8090 | ||
Add-PodeEndpoint -Address * -Port 8090 -Protocol Http | ||
|
||
# import the YAML module | ||
Import-PodeModule -Name Pode.Yaml -Now | ||
|
||
# enable YAML requests | ||
Enable-PodeYamlBodyParser | ||
|
||
Add-PodeRoute -Method Post -Path '/' -ScriptBlock { | ||
param($e) | ||
Write-PodeYamlResponse -Value @{ Name = $e.Data.name; Numbers = @(1,2,3) } | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# | ||
# Module manifest for module 'Pode.Yaml' | ||
# | ||
# Generated by: Matthew Kelly (Badgerati) | ||
# | ||
# Generated on: 22/12/2019 | ||
# | ||
|
||
@{ | ||
# Script module or binary module file associated with this manifest. | ||
RootModule = 'Pode.Yaml.psm1' | ||
|
||
# Version number of this module. | ||
ModuleVersion = '1.0.0' | ||
|
||
# ID used to uniquely identify this module | ||
GUID = 'a7530101-dab2-4bcf-8a3a-af607ca90d67' | ||
|
||
# Author of this module | ||
Author = 'Matthew Kelly (Badgerati)' | ||
|
||
# Copyright statement for this module | ||
Copyright = 'Copyright (c) 2019 Matthew Kelly (Badgerati), licensed under the MIT License.' | ||
|
||
# Description of the functionality provided by this module | ||
Description = 'A YAML Requesta dn Response extension module for Pode' | ||
|
||
# Minimum version of the Windows PowerShell engine required by this module | ||
PowerShellVersion = '5.0' | ||
|
||
# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell. | ||
PrivateData = @{ | ||
PSData = @{ | ||
|
||
# Tags applied to this module. These help with module discovery in online galleries. | ||
Tags = @('powershell', 'powershell-core', 'windows', 'unix', 'linux', 'PSEdition_Core', | ||
'PSEdition_Desktop', 'cross-platform', 'yaml', 'pode') | ||
|
||
# A URL to the license for this module. | ||
LicenseUri = 'https://raw.githubusercontent.com/Badgerati/Pode.Yaml/master/LICENSE.txt' | ||
|
||
# A URL to the main website for this project. | ||
ProjectUri = 'https://github.com/Badgerati/Pode.Yaml' | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# ensure the powershell-yaml module is imported | ||
if ($null -eq (Get-Module -Name 'powershell-yaml' -ErrorAction Ignore)) { | ||
Import-PodeModule -Name 'powershell-yaml' -Now | ||
} | ||
|
||
# root path to module | ||
$root = Split-Path -Parent -Path $MyInvocation.MyCommand.Path | ||
|
||
# get existing functions from memory for later comparison | ||
$sysfuncs = Get-ChildItem Function: | ||
|
||
# load public functions | ||
Get-ChildItem "$($root)/Public/*.ps1" | Resolve-Path | ForEach-Object { . $_ } | ||
|
||
# get functions from memory and compare to existing to find new functions added | ||
$funcs = Get-ChildItem Function: | Where-Object { $sysfuncs -notcontains $_ } | ||
|
||
# export the module's public functions | ||
Export-ModuleMember -Function ($funcs.Name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
function Enable-PodeYamlBodyParser | ||
{ | ||
[CmdletBinding()] | ||
param( | ||
[Parameter()] | ||
[ValidatePattern('^\w+\/[\w\.\+-]+$')] | ||
[ValidateNotNullOrEmpty()] | ||
[string] | ||
$ContentType = 'application/x-yaml' | ||
) | ||
|
||
Get-PodeYamlBodyParser | Add-PodeBodyParser -ContentType $ContentType | ||
} | ||
|
||
function Get-PodeYamlBodyParser | ||
{ | ||
[CmdletBinding()] | ||
param() | ||
|
||
return { | ||
param($body) | ||
return ($body | ConvertFrom-Yaml) | ||
} | ||
} | ||
|
||
function Write-PodeYamlResponse | ||
{ | ||
[CmdletBinding(DefaultParameterSetName='Value')] | ||
param ( | ||
[Parameter(Mandatory=$true, ParameterSetName='Value')] | ||
$Value, | ||
|
||
[Parameter(Mandatory=$true, ParameterSetName='File')] | ||
[string] | ||
$Path, | ||
|
||
[Parameter()] | ||
[ValidatePattern('^\w+\/[\w\.\+-]+$')] | ||
[ValidateNotNullOrEmpty()] | ||
[string] | ||
$ContentType = 'application/x-yaml', | ||
|
||
[Parameter()] | ||
[int] | ||
$StatusCode = 200 | ||
) | ||
|
||
switch ($PSCmdlet.ParameterSetName.ToLowerInvariant()) { | ||
'file' { | ||
if (Test-PodePath $Path) { | ||
$Value = Get-PodeFileContent -Path $Path | ||
} | ||
} | ||
|
||
'value' { | ||
if ($Value -isnot [string]) { | ||
$Value = ($Value | ConvertTo-Yaml) | ||
} | ||
} | ||
} | ||
|
||
Write-PodeTextResponse -Value $Value -ContentType $ContentType -StatusCode $StatusCode | ||
} |