forked from Badgerati/Pode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb-rest-openapi-shared.ps1
92 lines (70 loc) · 3.63 KB
/
web-rest-openapi-shared.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
$path = Split-Path -Parent -Path (Split-Path -Parent -Path $MyInvocation.MyCommand.Path)
Import-Module "$($path)/src/Pode.psm1" -Force -ErrorAction Stop
Start-PodeServer {
Add-PodeEndpoint -Address localhost -Port 8080 -Protocol Http -Name 'user'
Add-PodeEndpoint -Address localhost -Port 8081 -Protocol Http -Name 'admin'
New-PodeLoggingMethod -Terminal | Enable-PodeErrorLogging
Enable-PodeOpenApi -Title 'OpenAPI Example' -RouteFilter '/api/*' -RestrictRoutes
Enable-PodeOpenApiViewer -Type Swagger -DarkMode
Enable-PodeOpenApiViewer -Type ReDoc
New-PodeAuthScheme -Basic | Add-PodeAuth -Name 'Validate' -Sessionless -ScriptBlock {
param($username, $password)
# here you'd check a real user storage, this is just for example
if ($username -eq 'morty' -and $password -eq 'pickle') {
return @{
User = @{
ID ='M0R7Y302'
Name = 'Morty'
Type = 'Human'
}
}
}
return @{ Message = 'Invalid details supplied' }
}
Add-PodeOAComponentResponse -Name 'OK' -Description 'A user object' -ContentSchemas @{
'application/json' = (New-PodeOAObjectProperty -Properties @(
(New-PodeOAStringProperty -Name 'Name'),
(New-PodeOAIntProperty -Name 'UserId')
))
}
New-PodeOAIntProperty -Name 'userId' -Required |
ConvertTo-PodeOAParameter -In Path |
Add-PodeOAComponentParameter -Name 'UserId'
Add-PodeAuthMiddleware -Name AuthMiddleware -Authentication Validate -Route '/api/*'
Add-PodeRoute -Method Get -Path "/api/resources" -EndpointName 'user' -ScriptBlock {
Write-PodeJsonResponse -Value @{ Name = 'Rick'; UserId = 123 }
} -PassThru |
Set-PodeOARouteInfo -Summary 'A cool summary' -Tags 'Resources' -PassThru |
Add-PodeOAResponse -StatusCode 200 -Reference 'OK'
Add-PodeRoute -Method Post -Path "/api/resources" -ScriptBlock {
Write-PodeJsonResponse -Value @{ Name = 'Rick'; UserId = 123 }
} -PassThru |
Set-PodeOARouteInfo -Summary 'A cool summary' -Tags 'Resources' -PassThru |
Add-PodeOAResponse -StatusCode 200 -Reference 'OK'
Add-PodeRoute -Method Get -Path '/api/users/:userId' -ScriptBlock {
Write-PodeJsonResponse -Value @{ Name = 'Rick'; UserId = $WebEvent.Parameters['userId'] }
} -PassThru |
Set-PodeOARouteInfo -Summary 'A cool summary' -Tags 'Users' -PassThru |
Set-PodeOARequest -Parameters @(
(ConvertTo-PodeOAParameter -Reference 'UserId')
) -PassThru |
Add-PodeOAResponse -StatusCode 200 -Reference 'OK'
Add-PodeRoute -Method Get -Path '/api/users' -ScriptBlock {
Write-PodeJsonResponse -Value @{ Name = 'Rick'; UserId = $WebEvent.Query['userId'] }
} -PassThru |
Set-PodeOARouteInfo -Summary 'A cool summary' -Tags 'Users' -PassThru |
Set-PodeOARequest -Parameters @(
(New-PodeOAIntProperty -Name 'userId' -Required | ConvertTo-PodeOAParameter -In Query)
) -PassThru |
Add-PodeOAResponse -StatusCode 200 -Reference 'OK'
Add-PodeRoute -Method Post -Path '/api/users' -ScriptBlock {
Write-PodeJsonResponse -Value @{ Name = 'Rick'; UserId = $WebEvent.Data.userId }
} -PassThru |
Set-PodeOARouteInfo -Summary 'A cool summary' -Tags 'Users' -PassThru |
Set-PodeOARequest -RequestBody (
New-PodeOARequestBody -Required -ContentSchemas @{
'application/json' = (New-PodeOAIntProperty -Name 'userId' -Object)
}
) -PassThru |
Add-PodeOAResponse -StatusCode 200 -Reference 'OK'
}