Skip to content

Commit

Permalink
Merge pull request #5 from NomadicDaddy/main
Browse files Browse the repository at this point in the history
Add debug extension and update script references in error pages and l…
  • Loading branch information
NomadicDaddy authored Oct 17, 2024
2 parents 84b1425 + 4eb2cf9 commit 1d2a049
Show file tree
Hide file tree
Showing 11 changed files with 6,061 additions and 67 deletions.
15 changes: 9 additions & 6 deletions .build.ps1
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
'# required powershell modules'
Install-Module -Name Pode -Verbose # MIT
Install-Module -Name PSSQLite -Verbose # MIT
Install-Module -Name PSSQLite -MinimumVersion 1.1.0 -MaximumVersion 1.99.99 -Verbose # MIT
Install-Module -Name Pode -MinimumVersion 2.11.0 -MaximumVersion 2.99.99 -Verbose # MIT

'# development powershell modules'
Install-Module -Name Pester -Verbose # Apache 2.0
Install-Module -Name PSScriptAnalyzer -Verbose # MIT
Install-Module -Name Pester -MinimumVersion 5.6.1 -MaximumVersion 5.99.99 -Verbose # Apache 2.0
Install-Module -Name PSScriptAnalyzer -MinimumVersion 1.23.0 -MaximumVersion 1.99.99 -Verbose # MIT

'# required npm packages'
npm install htmx.org@next htmx-ext-client-side-templates htmx-ext-debug htmx-ext-json-enc mustache
npm install tailwindcss @tailwindcss/forms @tailwindcss/typography @tailwindcss/aspect-ratio

'# development npm packages'
npm install -D eslint globals @eslint/js prettier prettier-plugin-tailwindcss prettier-plugin-sql
npm install -D eslint globals htmx-ext-debug @eslint/js prettier prettier-plugin-tailwindcss prettier-plugin-sql

'# update packages'
npm update
Expand All @@ -23,9 +23,12 @@ npm run test
npm run css

'# copy distribution files'
Copy-Item -Path './node_modules/htmx.org/dist/htmx.min.js' -Destination './public/js/htmx.min.js' -Force -Verbose
Copy-Item -Path './node_modules/htmx-ext-client-side-templates/client-side-templates.js' -Destination './public/js/client-side-templates.js' -Force -Verbose
Copy-Item -Path './node_modules/htmx-ext-debug/debug.js' -Destination './public/js/debug.js' -Force -Verbose
Copy-Item -Path './node_modules/htmx-ext-json-enc/json-enc.js' -Destination './public/js/json-enc.js' -Force -Verbose
Copy-Item -Path './node_modules/htmx.org/dist/htmx.js' -Destination './public/js/htmx.js' -Force -Verbose
Copy-Item -Path './node_modules/htmx.org/dist/htmx.min.js' -Destination './public/js/htmx.min.js' -Force -Verbose
Copy-Item -Path './node_modules/mustache/mustache.js' -Destination './public/js/mustache.js' -Force -Verbose
Copy-Item -Path './node_modules/mustache/mustache.min.js' -Destination './public/js/mustache.min.js' -Force -Verbose

'# initialize database'
Expand Down
37 changes: 21 additions & 16 deletions api/crud/get.ps1
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
{

$isHTMX = $($WebEvent.Request.Headers.'HX-Request')
$isJsonEnc = $($WebEvent.Request.Headers.'Content-Type') -eq 'application/json'

$db = (Get-PodeConfig).Podex.DBFile

Write-FormattedLog -tag 'api' -log "CRUD API: $($WebEvent.Method.ToUpper()) $($WebEvent.Path) `$isHTMX:$($isHTMX) Q: $($WebEvent.Query | ConvertTo-Json -Compress)"
Write-FormattedLog -tag 'api' -log "Items API: $($WebEvent.Method.ToUpper()) $($WebEvent.Path) `$isHTMX:$($isHTMX) Q: $($WebEvent.Query | ConvertTo-Json -Compress)"

try {
$tagFilter = $WebEvent.Query['tagFilter']
Expand Down Expand Up @@ -43,9 +45,7 @@

# Write-FormattedLog -tag 'database' -log "db: $($db); sqlx: $($sqlx); tagFilter: $tagFilter; search: $search; params: $($params | ConvertTo-Json -Compress)"
$rs = (Invoke-SqliteQuery -DataSource $db -Query $sqlx -SqlParameters $params -As PSObject)

# Write-FormattedLog -tag 'database' -log "db: $($db); countSqlx: $($countSqlx); tagFilter: $tagFilter; search: $search; params: $($params | ConvertTo-Json -Compress)"
$totalItems = (Invoke-SqliteQuery -DataSource $db -Query $countSqlx -SqlParameters $params -As PSObject).count
$totalItems = $rs.Count

$tags = (Invoke-SqliteQuery -DataSource $db -Query "select [tag], case when [tag] = '$($tagFilter)' then 'selected' else '' end as [selected] from [tag] order by [tag] asc ;" -As PSObject)

Expand All @@ -54,7 +54,6 @@
$totalPages = [Math]::Ceiling($totalItems / $pageSize)
$hasPreviousPage = $page -gt 1
$hasNextPage = $page -lt $totalPages

$pages = @()
for ($i = 1; $i -le $totalPages; $i++) {
$pages += @{
Expand All @@ -64,7 +63,7 @@
}

$response = @{
features = $rs
rows = $rs
tags = $tags
startIndex = $startIndex
endIndex = $endIndex
Expand All @@ -77,23 +76,29 @@
currentPage = $page
}

# Write-FormattedLog -tag 'debug' -log ($response | ConvertTo-Json -Depth 5 -Compress) -save $true
Write-FormattedLog -tag 'debug' -log ($response | ConvertTo-Json -Depth 5 -Compress)
if ((Get-PodeConfig).Podex.Debug) {
New-Item -Name "$($WebEvent.Method).json" -Path $PSScriptRoot -ItemType File -Value ($response | ConvertTo-Json -Depth 5) -Force
}

if ($isHTMX) {
Write-FormattedLog -tag 'debug' -log "CRUDs found: $($rs.Count)"
$response | ConvertTo-Json -Depth 5 | Write-PodeJsonResponse -StatusCode 200
} else {
if ($response.features) {
Write-FormattedLog -tag 'debug' -log "CRUDs found: $($rs.Count)"
if ($response.rows) {
Write-FormattedLog -tag 'debug' -log "Items found: $($totalItems)"
if ($isJsonEnc) {
$response | ConvertTo-Json -Depth 5 | Write-PodeJsonResponse -StatusCode 200
} else {
Write-FormattedLog -tag 'debug' -log "No features found"
Write-PodeJsonResponse -StatusCode 204 -Value @{ message = "No features found" }
$response | Write-PodeHtmlResponse -StatusCode 200
}
} else {
Write-FormattedLog -tag 'debug' -log "No items found"
if ($isJsonEnc) {
Write-PodeJsonResponse -StatusCode 204 -Value @{ message = "No items found" }
} else {
Write-PodeHtmlResponse -StatusCode 204 -Value @{ message = "No items found" }
}
}

} catch {
Write-FormattedLog -tag 'error' -log "Error retrieving features: $_"
Write-FormattedLog -tag 'error' -log "Error retrieving items: $($_.Exception.Message)"
Write-PodeJsonResponse -StatusCode 500 -Value @{ message = "Internal server error" }
}

Expand Down
5 changes: 3 additions & 2 deletions errors/404.html.pode
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
<title>404 Not Found</title>
<link rel="stylesheet" type="text/css" href="/public/css/output.css" />
<link rel="icon" type="image/svg+xml" href="/public/images/favicon.svg" />
<script src="/public/js/htmx.min.js"></script>
<script src="/public/js/htmx.js"></script>
<script src="/public/js/debug.js"></script>
<script src="/public/js/client-side-templates.js"></script>
<script src="/public/js/json-enc.js"></script>
<script src="/public/js/mustache.min.js"></script>
<script src="/public/js/mustache.js"></script>
</head>
<body class="bg-gray-100 text-gray-800 dark:bg-gray-900 dark:text-gray-200" hx-boost="true">
$(Use-PodePartialView -Path 'partials/header' -Data @{ PageName = '404 Not Found' } ;)
Expand Down
5 changes: 3 additions & 2 deletions errors/default.html.pode
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
<title>$($data.status.code ;) Error</title>
<link rel="stylesheet" type="text/css" href="/public/css/output.css" />
<link rel="icon" type="image/svg+xml" href="/public/images/favicon.svg" />
<script src="/public/js/htmx.min.js"></script>
<script src="/public/js/htmx.js"></script>
<script src="/public/js/debug.js"></script>
<script src="/public/js/client-side-templates.js"></script>
<script src="/public/js/json-enc.js"></script>
<script src="/public/js/mustache.min.js"></script>
<script src="/public/js/mustache.js"></script>
</head>
<body class="bg-gray-100 text-gray-800 dark:bg-gray-900 dark:text-gray-200" hx-boost="true">
$(Use-PodePartialView -Path 'partials/header' -Data @{ PageName = '$($data.status.code ;) Error' } ;)
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"scripts": {
"format": "prettier --write .",
"lint": "eslint .",
"css": "tailwindcss -c ./tailwind.config.mjs -i ./public/css/tailwind.css -o ./public/css/output.css --minify",
"css": "tailwindcss -c ./tailwind.config.mjs -i ./public/css/tailwind.css -o ./public/css/output.css #--minify",
"analyze": "pwsh -NoLogo -NoProfile -ExecutionPolicy Bypass -Command \"Import-Module PSScriptAnalyzer ; Invoke-ScriptAnalyzer -Path ./\"",
"test": "pwsh -NoLogo -NoProfile -ExecutionPolicy Bypass -Command \"Import-Module Pester ; Invoke-Pester -Path ./tests/*.ps1\"",
"build": "pwsh -NoLogo -NoProfile -ExecutionPolicy Bypass \"./.build.ps1\"",
Expand All @@ -27,16 +27,16 @@
"@tailwindcss/forms": "^0.5.9",
"@tailwindcss/typography": "^0.5.15",
"htmx-ext-client-side-templates": "^2.0.0",
"htmx-ext-debug": "^2.0.0",
"htmx-ext-json-enc": "^2.0.1",
"htmx.org": "^2.0.3",
"mustache": "^4.2.0",
"tailwindcss": "^3.4.13"
"tailwindcss": "^3.4.14"
},
"devDependencies": {
"@eslint/js": "^9.12.0",
"eslint": "^9.12.0",
"globals": "^15.10.0",
"globals": "^15.11.0",
"htmx-ext-debug": "^2.0.0",
"prettier": "^3.3.3",
"prettier-plugin-sql": "^0.18.1",
"prettier-plugin-tailwindcss": "^0.6.8"
Expand Down
11 changes: 11 additions & 0 deletions public/js/debug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
htmx.defineExtension('debug', {
onEvent: function(name, evt) {
if (console.debug) {
console.debug(name, evt)
} else if (console) {
console.log('DEBUG:', name, evt)
} else {
throw new Error('NO CONSOLE SUPPORTED')
}
}
})
Loading

0 comments on commit 1d2a049

Please sign in to comment.