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

fix: nuxt module - add storage for dashboard templates and update dashboard handler #3

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"bumpp": "bumpp package.json",
"release": "pnpm build && pnpm publish --no-git-checks --access public",
"release:beta": "pnpm release --tag beta --access public",
"release:alpha": "pnpm release --tag alpha --access public"
"release:alpha": "pnpm release --tag alpha --access public",
"test:types": "tsc --noEmit --skipLibCheck"
},
"dependencies": {
"bumpp": "^9.8.1",
Expand All @@ -59,6 +60,7 @@
"jiti": "^2.4.1",
"nitropack": "^2.10.4",
"nuxt": "^3.14.1592",
"pathe": "^1.1.2",
"typescript": "5.6.3",
"unbuild": "^2.0.0"
},
Expand Down
32 changes: 19 additions & 13 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 13 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@ import type { Nitro } from 'nitropack'
import type { Nuxt } from 'nuxt/schema'
import { fileURLToPath } from 'node:url'

declare module 'nitropack' {
interface NitroOptions {
// kutu?: {
// /**
// * @default By true, /_api/_analytics/dashboard
// */
// ui?: boolean
// }
export function nitroModule(nitro: Nitro) {
const templateDir = fileURLToPath(new URL('./runtime/templates', import.meta.url))
// Configure storage for themes
nitro.options.storage = nitro.options.storage || {}
nitro.options.storage['kutu:templates'] = {
driver: 'fs',
base: templateDir,
}
}

export function nitroModule(nitro: Nitro) {
// Add plugin to inject bindings to dev server
// Add templates directory to bundled storage for production
nitro.options.bundledStorage = nitro.options.bundledStorage || []
nitro.options.bundledStorage.push('kutu/templates')

// Add plugin
nitro.options.plugins = nitro.options.plugins || []
nitro.options.plugins.push(
fileURLToPath(new URL('runtime/plugin', import.meta.url)),
fileURLToPath(new URL('./runtime/plugin', import.meta.url)),
)
}

Expand Down
12 changes: 4 additions & 8 deletions src/runtime/handlers/dashboard.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import { readFileSync } from 'node:fs'
import { resolve } from 'node:path'
import { fileURLToPath } from 'node:url'

export function createDashboardHandler() {
const __dirname = fileURLToPath(new URL('.', import.meta.url))
const template = readFileSync(resolve(__dirname, '../templates/dashboard.html'), 'utf-8')
import { useStorage } from 'nitropack/runtime'

export async function createDashboardHandler() {
const theme: string | null = await useStorage().getItem('kutu:templates:dashboard.html')
return async () => {
return template
return theme
}
}
2 changes: 1 addition & 1 deletion src/runtime/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ export default <NitroAppPlugin> function (nitroApp) {

// Add filtered logs to route handlers
nitroApp.router.add('/api/_analytics/logs', eventHandler(createAnalyticsHandler(logBuffer, requestCounts)))
nitroApp.router.add('/api/_analytics/dashboard', eventHandler(createDashboardHandler()))
nitroApp.router.add('/api/_analytics/dashboard', eventHandler(createDashboardHandler))
}
Loading