vasp-cli 0.3.1 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/vasp +80 -31
- package/package.json +2 -2
- package/starters/minimal.vasp +1 -1
- package/starters/recipe.vasp +70 -0
- package/starters/todo-auth-ssr.vasp +33 -20
- package/starters/todo.vasp +15 -8
- package/templates/shared/.gitignore.hbs +1 -0
- package/templates/shared/README.md.hbs +53 -0
- package/templates/shared/auth/client/Login.vue.hbs +1 -1
- package/templates/shared/auth/client/Register.vue.hbs +1 -1
- package/templates/shared/auth/server/index.hbs +4 -8
- package/templates/shared/auth/server/middleware.hbs +33 -15
- package/templates/shared/auth/server/plugin.hbs +7 -0
- package/templates/shared/auth/server/providers/github.hbs +1 -1
- package/templates/shared/auth/server/providers/google.hbs +1 -1
- package/templates/shared/auth/server/providers/usernameAndPassword.hbs +3 -6
- package/templates/shared/bunfig.toml.hbs +3 -0
- package/templates/shared/drizzle/schema.hbs +39 -9
- package/templates/shared/jobs/_job.hbs +12 -2
- package/templates/shared/package.json.hbs +14 -4
- package/templates/shared/server/db/client.hbs +19 -1
- package/templates/shared/server/db/seed.hbs +16 -0
- package/templates/shared/server/index.hbs +48 -0
- package/templates/shared/server/middleware/errorHandler.hbs +75 -0
- package/templates/shared/server/middleware/logger.hbs +74 -0
- package/templates/{templates/shared → shared}/server/middleware/rateLimit.hbs +2 -2
- package/templates/shared/server/routes/_vasp.hbs +37 -0
- package/templates/shared/server/routes/actions/_action.hbs +5 -1
- package/templates/shared/server/routes/api/_api.hbs +24 -0
- package/templates/shared/server/routes/crud/_crud.hbs +103 -11
- package/templates/shared/server/routes/queries/_query.hbs +5 -1
- package/templates/shared/server/routes/realtime/_channel.hbs +58 -10
- package/templates/shared/shared/types.hbs +58 -0
- package/templates/shared/shared/validation.hbs +20 -0
- package/templates/shared/tests/actions/_action.test.js.hbs +7 -0
- package/templates/shared/tests/actions/_action.test.ts.hbs +7 -0
- package/templates/shared/tests/auth/login.test.js.hbs +7 -0
- package/templates/shared/tests/auth/login.test.ts.hbs +7 -0
- package/templates/shared/tests/crud/_entity.test.js.hbs +7 -0
- package/templates/shared/tests/crud/_entity.test.ts.hbs +7 -0
- package/templates/shared/tests/queries/_query.test.js.hbs +7 -0
- package/templates/shared/tests/queries/_query.test.ts.hbs +7 -0
- package/templates/shared/tests/setup.js.hbs +5 -0
- package/templates/shared/tests/setup.ts.hbs +5 -0
- package/templates/shared/tests/vitest.config.js.hbs +8 -0
- package/templates/shared/tests/vitest.config.ts.hbs +8 -0
- package/templates/shared/tsconfig.json.hbs +2 -1
- package/templates/spa/js/src/App.vue.hbs +9 -1
- package/templates/spa/js/src/components/VaspErrorBoundary.vue.hbs +33 -0
- package/templates/spa/js/src/components/VaspNotifications.vue.hbs +60 -0
- package/templates/spa/js/src/vasp/auth.js.hbs +31 -15
- package/templates/spa/js/src/vasp/client/actions.js.hbs +7 -1
- package/templates/spa/js/src/vasp/client/crud.js.hbs +94 -5
- package/templates/spa/js/src/vasp/useVaspNotifications.js.hbs +35 -0
- package/templates/spa/js/vite.config.js.hbs +1 -0
- package/templates/spa/ts/src/App.vue.hbs +9 -1
- package/templates/spa/ts/src/components/VaspErrorBoundary.vue.hbs +33 -0
- package/templates/spa/ts/src/components/VaspNotifications.vue.hbs +60 -0
- package/templates/spa/ts/src/vasp/auth.ts.hbs +31 -15
- package/templates/spa/ts/src/vasp/client/actions.ts.hbs +7 -1
- package/templates/spa/ts/src/vasp/client/crud.ts.hbs +96 -10
- package/templates/spa/ts/src/vasp/client/types.ts.hbs +14 -28
- package/templates/spa/ts/src/vasp/useVaspNotifications.ts.hbs +41 -0
- package/templates/spa/ts/vite.config.ts.hbs +1 -0
- package/templates/ssr/js/error.vue.hbs +23 -0
- package/templates/ssr/js/nuxt.config.js.hbs +1 -0
- package/templates/ssr/js/plugins/vasp.client.js.hbs +11 -1
- package/templates/ssr/ts/error.vue.hbs +26 -0
- package/templates/ssr/ts/nuxt.config.ts.hbs +1 -0
- package/templates/ssr/ts/plugins/vasp.client.ts.hbs +11 -1
- package/templates/starters/minimal.vasp +15 -0
- package/templates/starters/recipe.vasp +70 -0
- package/templates/starters/todo-auth-ssr.vasp +65 -0
- package/templates/starters/todo.vasp +42 -0
- package/templates/templates/shared/.gitignore.hbs +0 -8
- package/templates/templates/shared/auth/client/Login.vue.hbs +0 -46
- package/templates/templates/shared/auth/client/Register.vue.hbs +0 -42
- package/templates/templates/shared/auth/server/index.hbs +0 -51
- package/templates/templates/shared/auth/server/middleware.hbs +0 -33
- package/templates/templates/shared/auth/server/providers/github.hbs +0 -48
- package/templates/templates/shared/auth/server/providers/google.hbs +0 -53
- package/templates/templates/shared/auth/server/providers/usernameAndPassword.hbs +0 -69
- package/templates/templates/shared/bunfig.toml.hbs +0 -2
- package/templates/templates/shared/drizzle/schema.hbs +0 -48
- package/templates/templates/shared/jobs/_job.hbs +0 -34
- package/templates/templates/shared/jobs/boss.hbs +0 -15
- package/templates/templates/shared/package.json.hbs +0 -35
- package/templates/templates/shared/server/db/client.hbs +0 -12
- package/templates/templates/shared/server/index.hbs +0 -60
- package/templates/templates/shared/server/routes/actions/_action.hbs +0 -20
- package/templates/templates/shared/server/routes/crud/_crud.hbs +0 -86
- package/templates/templates/shared/server/routes/jobs/_schedule.hbs +0 -12
- package/templates/templates/shared/server/routes/queries/_query.hbs +0 -20
- package/templates/templates/shared/server/routes/realtime/_channel.hbs +0 -78
- package/templates/templates/shared/server/routes/realtime/index.hbs +0 -9
- package/templates/templates/shared/tsconfig.json.hbs +0 -21
- package/templates/templates/spa/js/index.html.hbs +0 -12
- package/templates/templates/spa/js/src/App.vue.hbs +0 -3
- package/templates/templates/spa/js/src/main.js.hbs +0 -9
- package/templates/templates/spa/js/src/router/index.js.hbs +0 -41
- package/templates/templates/spa/js/src/vasp/auth.js.hbs +0 -45
- package/templates/templates/spa/js/src/vasp/client/actions.js.hbs +0 -15
- package/templates/templates/spa/js/src/vasp/client/crud.js.hbs +0 -30
- package/templates/templates/spa/js/src/vasp/client/index.js.hbs +0 -16
- package/templates/templates/spa/js/src/vasp/client/queries.js.hbs +0 -15
- package/templates/templates/spa/js/src/vasp/client/realtime.js.hbs +0 -51
- package/templates/templates/spa/js/src/vasp/plugin.js.hbs +0 -11
- package/templates/templates/spa/js/vite.config.js.hbs +0 -26
- package/templates/templates/spa/ts/index.html.hbs +0 -12
- package/templates/templates/spa/ts/src/App.vue.hbs +0 -3
- package/templates/templates/spa/ts/src/main.ts.hbs +0 -9
- package/templates/templates/spa/ts/src/router/index.ts.hbs +0 -41
- package/templates/templates/spa/ts/src/vasp/auth.ts.hbs +0 -53
- package/templates/templates/spa/ts/src/vasp/client/actions.ts.hbs +0 -19
- package/templates/templates/spa/ts/src/vasp/client/crud.ts.hbs +0 -37
- package/templates/templates/spa/ts/src/vasp/client/index.ts.hbs +0 -17
- package/templates/templates/spa/ts/src/vasp/client/queries.ts.hbs +0 -19
- package/templates/templates/spa/ts/src/vasp/client/realtime.ts.hbs +0 -56
- package/templates/templates/spa/ts/src/vasp/client/types.ts.hbs +0 -33
- package/templates/templates/spa/ts/src/vasp/plugin.ts.hbs +0 -12
- package/templates/templates/spa/ts/vite.config.ts.hbs +0 -26
- package/templates/templates/ssr/js/_page.vue.hbs +0 -10
- package/templates/templates/ssr/js/app.vue.hbs +0 -3
- package/templates/templates/ssr/js/composables/useAuth.js.hbs +0 -52
- package/templates/templates/ssr/js/composables/useVasp.js.hbs +0 -6
- package/templates/templates/ssr/js/middleware/auth.js.hbs +0 -8
- package/templates/templates/ssr/js/nuxt.config.js.hbs +0 -15
- package/templates/templates/ssr/js/plugins/vasp.client.js.hbs +0 -27
- package/templates/templates/ssr/js/plugins/vasp.server.js.hbs +0 -33
- package/templates/templates/ssr/ts/_page.vue.hbs +0 -10
- package/templates/templates/ssr/ts/app.vue.hbs +0 -3
- package/templates/templates/ssr/ts/composables/useAuth.ts.hbs +0 -56
- package/templates/templates/ssr/ts/composables/useVasp.ts.hbs +0 -10
- package/templates/templates/ssr/ts/middleware/auth.ts.hbs +0 -8
- package/templates/templates/ssr/ts/nuxt.config.ts.hbs +0 -19
- package/templates/templates/ssr/ts/plugins/vasp.client.ts.hbs +0 -27
- package/templates/templates/ssr/ts/plugins/vasp.server.ts.hbs +0 -33
- /package/templates/{templates/shared/.env.example.hbs → shared/.env.hbs} +0 -0
- /package/templates/{templates/shared → shared}/drizzle/drizzle.config.hbs +0 -0
- /package/templates/{templates/shared → shared}/server/middleware/csrf.hbs +0 -0
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { Elysia } from 'elysia'
|
|
2
|
-
import { jwt } from '@elysiajs/jwt'
|
|
3
|
-
import { cookie } from '@elysiajs/cookie'
|
|
4
|
-
import { db } from '../db/client.{{ext}}'
|
|
5
|
-
import { users } from '../../drizzle/schema.{{ext}}'
|
|
6
|
-
import { eq } from 'drizzle-orm'
|
|
7
|
-
{{#if (includes authMethods "usernameAndPassword")}}
|
|
8
|
-
import { usernameAndPasswordRoutes } from './providers/usernameAndPassword.{{ext}}'
|
|
9
|
-
{{/if}}
|
|
10
|
-
{{#if (includes authMethods "google")}}
|
|
11
|
-
import { googleRoutes } from './providers/google.{{ext}}'
|
|
12
|
-
{{/if}}
|
|
13
|
-
{{#if (includes authMethods "github")}}
|
|
14
|
-
import { githubRoutes } from './providers/github.{{ext}}'
|
|
15
|
-
{{/if}}
|
|
16
|
-
|
|
17
|
-
const JWT_SECRET = process.env.JWT_SECRET || 'change-me-in-production'
|
|
18
|
-
|
|
19
|
-
export const authPlugin = new Elysia({ name: 'auth-plugin' })
|
|
20
|
-
.use(jwt({ name: 'jwt', secret: JWT_SECRET }))
|
|
21
|
-
.use(cookie())
|
|
22
|
-
|
|
23
|
-
export const authRoutes = new Elysia({ prefix: '/auth' })
|
|
24
|
-
.use(authPlugin)
|
|
25
|
-
{{#if (includes authMethods "usernameAndPassword")}}
|
|
26
|
-
.use(usernameAndPasswordRoutes)
|
|
27
|
-
{{/if}}
|
|
28
|
-
{{#if (includes authMethods "google")}}
|
|
29
|
-
.use(googleRoutes)
|
|
30
|
-
{{/if}}
|
|
31
|
-
{{#if (includes authMethods "github")}}
|
|
32
|
-
.use(githubRoutes)
|
|
33
|
-
{{/if}}
|
|
34
|
-
.get('/me', async ({ jwt, cookie: { token }, set }) => {
|
|
35
|
-
const payload = await jwt.verify(token?.value ?? '')
|
|
36
|
-
if (!payload || typeof payload.userId !== 'number') {
|
|
37
|
-
set.status = 401
|
|
38
|
-
return { error: 'Unauthorized' }
|
|
39
|
-
}
|
|
40
|
-
const [user] = await db.select().from(users).where(eq(users.id, payload.userId)).limit(1)
|
|
41
|
-
if (!user) {
|
|
42
|
-
set.status = 401
|
|
43
|
-
return { error: 'User not found' }
|
|
44
|
-
}
|
|
45
|
-
const { passwordHash: _ph, ...safeUser } = user
|
|
46
|
-
return safeUser
|
|
47
|
-
})
|
|
48
|
-
.post('/logout', ({ cookie: { token }, set }) => {
|
|
49
|
-
token?.remove()
|
|
50
|
-
return { ok: true }
|
|
51
|
-
})
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { Elysia } from 'elysia'
|
|
2
|
-
import { jwt } from '@elysiajs/jwt'
|
|
3
|
-
import { cookie } from '@elysiajs/cookie'
|
|
4
|
-
import { db } from '../db/client.{{ext}}'
|
|
5
|
-
import { users } from '../../drizzle/schema.{{ext}}'
|
|
6
|
-
import { eq } from 'drizzle-orm'
|
|
7
|
-
|
|
8
|
-
const JWT_SECRET = process.env.JWT_SECRET || 'change-me-in-production'
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* requireAuth — Elysia plugin that verifies the JWT cookie and injects `user` into the context.
|
|
12
|
-
* Use on any route that requires authentication.
|
|
13
|
-
*
|
|
14
|
-
* @example
|
|
15
|
-
* new Elysia().use(requireAuth).get('/protected', ({ user }) => user)
|
|
16
|
-
*/
|
|
17
|
-
export const requireAuth = new Elysia({ name: 'require-auth' })
|
|
18
|
-
.use(jwt({ name: 'jwt', secret: JWT_SECRET }))
|
|
19
|
-
.use(cookie())
|
|
20
|
-
.derive(async ({ jwt, cookie: { token }, set }) => {
|
|
21
|
-
const payload = await jwt.verify(token?.value ?? '')
|
|
22
|
-
if (!payload || typeof payload.userId !== 'number') {
|
|
23
|
-
set.status = 401
|
|
24
|
-
throw new Error('Unauthorized')
|
|
25
|
-
}
|
|
26
|
-
const [user] = await db.select().from(users).where(eq(users.id, payload.userId)).limit(1)
|
|
27
|
-
if (!user) {
|
|
28
|
-
set.status = 401
|
|
29
|
-
throw new Error('User not found')
|
|
30
|
-
}
|
|
31
|
-
const { passwordHash: _ph, ...safeUser } = user
|
|
32
|
-
return { user: safeUser }
|
|
33
|
-
})
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import { Elysia } from 'elysia'
|
|
2
|
-
import { db } from '../../db/client.{{ext}}'
|
|
3
|
-
import { users } from '../../../drizzle/schema.{{ext}}'
|
|
4
|
-
import { eq } from 'drizzle-orm'
|
|
5
|
-
import { authPlugin } from '../index.{{ext}}'
|
|
6
|
-
|
|
7
|
-
const GITHUB_CLIENT_ID = process.env.GITHUB_CLIENT_ID || ''
|
|
8
|
-
const GITHUB_CLIENT_SECRET = process.env.GITHUB_CLIENT_SECRET || ''
|
|
9
|
-
const REDIRECT_URI = process.env.GITHUB_REDIRECT_URI || 'http://localhost:{{backendPort}}/auth/github/callback'
|
|
10
|
-
|
|
11
|
-
export const githubRoutes = new Elysia()
|
|
12
|
-
.use(authPlugin)
|
|
13
|
-
.get('/github', ({ set }) => {
|
|
14
|
-
const params = new URLSearchParams({
|
|
15
|
-
client_id: GITHUB_CLIENT_ID,
|
|
16
|
-
redirect_uri: REDIRECT_URI,
|
|
17
|
-
scope: 'read:user user:email',
|
|
18
|
-
})
|
|
19
|
-
set.redirect = `https://github.com/login/oauth/authorize?${params}`
|
|
20
|
-
})
|
|
21
|
-
.get('/github/callback', async ({ query, jwt, cookie: { token }, set }) => {
|
|
22
|
-
const { code } = query
|
|
23
|
-
if (!code) { set.status = 400; return { error: 'Missing code' } }
|
|
24
|
-
|
|
25
|
-
const tokenRes = await fetch('https://github.com/login/oauth/access_token', {
|
|
26
|
-
method: 'POST',
|
|
27
|
-
headers: { 'Content-Type': 'application/json', Accept: 'application/json' },
|
|
28
|
-
body: JSON.stringify({ client_id: GITHUB_CLIENT_ID, client_secret: GITHUB_CLIENT_SECRET, code }),
|
|
29
|
-
})
|
|
30
|
-
const { access_token } = await tokenRes.json()
|
|
31
|
-
|
|
32
|
-
const userRes = await fetch('https://api.github.com/user', {
|
|
33
|
-
headers: { Authorization: `Bearer ${access_token}`, Accept: 'application/json' },
|
|
34
|
-
})
|
|
35
|
-
const ghUser = await userRes.json()
|
|
36
|
-
const githubId = String(ghUser.id)
|
|
37
|
-
const email = ghUser.email || `${ghUser.login}@github.local`
|
|
38
|
-
|
|
39
|
-
let [user] = await db.select().from(users).where(eq(users.githubId, githubId)).limit(1)
|
|
40
|
-
if (!user) {
|
|
41
|
-
;[user] = await db.insert(users).values({ username: ghUser.login, email, githubId }).returning()
|
|
42
|
-
}
|
|
43
|
-
if (!user) { set.status = 500; return { error: 'Failed to create user' } }
|
|
44
|
-
|
|
45
|
-
const tokenValue = await jwt.sign({ userId: user.id })
|
|
46
|
-
token.set({ value: tokenValue, httpOnly: true, sameSite: 'lax', path: '/' })
|
|
47
|
-
set.redirect = '/'
|
|
48
|
-
})
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import { Elysia } from 'elysia'
|
|
2
|
-
import { db } from '../../db/client.{{ext}}'
|
|
3
|
-
import { users } from '../../../drizzle/schema.{{ext}}'
|
|
4
|
-
import { eq } from 'drizzle-orm'
|
|
5
|
-
import { authPlugin } from '../index.{{ext}}'
|
|
6
|
-
|
|
7
|
-
const GOOGLE_CLIENT_ID = process.env.GOOGLE_CLIENT_ID || ''
|
|
8
|
-
const GOOGLE_CLIENT_SECRET = process.env.GOOGLE_CLIENT_SECRET || ''
|
|
9
|
-
const REDIRECT_URI = process.env.GOOGLE_REDIRECT_URI || 'http://localhost:{{backendPort}}/auth/google/callback'
|
|
10
|
-
|
|
11
|
-
export const googleRoutes = new Elysia()
|
|
12
|
-
.use(authPlugin)
|
|
13
|
-
.get('/google', ({ set }) => {
|
|
14
|
-
const params = new URLSearchParams({
|
|
15
|
-
client_id: GOOGLE_CLIENT_ID,
|
|
16
|
-
redirect_uri: REDIRECT_URI,
|
|
17
|
-
response_type: 'code',
|
|
18
|
-
scope: 'openid email profile',
|
|
19
|
-
})
|
|
20
|
-
set.redirect = `https://accounts.google.com/o/oauth2/v2/auth?${params}`
|
|
21
|
-
})
|
|
22
|
-
.get('/google/callback', async ({ query, jwt, cookie: { token }, set }) => {
|
|
23
|
-
const { code } = query
|
|
24
|
-
if (!code) { set.status = 400; return { error: 'Missing code' } }
|
|
25
|
-
|
|
26
|
-
// Exchange code for tokens
|
|
27
|
-
const tokenRes = await fetch('https://oauth2.googleapis.com/token', {
|
|
28
|
-
method: 'POST',
|
|
29
|
-
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
30
|
-
body: new URLSearchParams({
|
|
31
|
-
code,
|
|
32
|
-
client_id: GOOGLE_CLIENT_ID,
|
|
33
|
-
client_secret: GOOGLE_CLIENT_SECRET,
|
|
34
|
-
redirect_uri: REDIRECT_URI,
|
|
35
|
-
grant_type: 'authorization_code',
|
|
36
|
-
}),
|
|
37
|
-
})
|
|
38
|
-
const tokenData = await tokenRes.json()
|
|
39
|
-
const idToken = tokenData.id_token
|
|
40
|
-
const payload = JSON.parse(Buffer.from(idToken.split('.')[1], 'base64').toString())
|
|
41
|
-
const googleId = payload.sub
|
|
42
|
-
const email = payload.email
|
|
43
|
-
|
|
44
|
-
let [user] = await db.select().from(users).where(eq(users.googleId, googleId)).limit(1)
|
|
45
|
-
if (!user) {
|
|
46
|
-
;[user] = await db.insert(users).values({ username: email, email, googleId }).returning()
|
|
47
|
-
}
|
|
48
|
-
if (!user) { set.status = 500; return { error: 'Failed to create user' } }
|
|
49
|
-
|
|
50
|
-
const tokenValue = await jwt.sign({ userId: user.id })
|
|
51
|
-
token.set({ value: tokenValue, httpOnly: true, sameSite: 'lax', path: '/' })
|
|
52
|
-
set.redirect = '/'
|
|
53
|
-
})
|
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
import { Elysia, t } from 'elysia'
|
|
2
|
-
import { db } from '../../db/client.{{ext}}'
|
|
3
|
-
import { users } from '../../../drizzle/schema.{{ext}}'
|
|
4
|
-
import { eq } from 'drizzle-orm'
|
|
5
|
-
import { authPlugin } from '../index.{{ext}}'
|
|
6
|
-
|
|
7
|
-
async function hashPassword(password{{#if isTypeScript}}: string{{/if}}) {
|
|
8
|
-
const encoder = new TextEncoder()
|
|
9
|
-
const data = encoder.encode(password)
|
|
10
|
-
const hash = await crypto.subtle.digest('SHA-256', data)
|
|
11
|
-
return Buffer.from(hash).toString('hex')
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
async function verifyPassword(password{{#if isTypeScript}}: string{{/if}}, hash{{#if isTypeScript}}: string{{/if}}) {
|
|
15
|
-
return (await hashPassword(password)) === hash
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export const usernameAndPasswordRoutes = new Elysia()
|
|
19
|
-
.use(authPlugin)
|
|
20
|
-
.post(
|
|
21
|
-
'/register',
|
|
22
|
-
async ({ body, jwt, cookie: { token }, set }) => {
|
|
23
|
-
const existing = await db.select().from(users).where(eq(users.username, body.username)).limit(1)
|
|
24
|
-
if (existing.length > 0) {
|
|
25
|
-
set.status = 400
|
|
26
|
-
return { error: 'Username already taken' }
|
|
27
|
-
}
|
|
28
|
-
const passwordHash = await hashPassword(body.password)
|
|
29
|
-
const [user] = await db
|
|
30
|
-
.insert(users)
|
|
31
|
-
.values({ username: body.username, email: body.email ?? null, passwordHash })
|
|
32
|
-
.returning()
|
|
33
|
-
if (!user) {
|
|
34
|
-
set.status = 500
|
|
35
|
-
return { error: 'Failed to create user' }
|
|
36
|
-
}
|
|
37
|
-
const tokenValue = await jwt.sign({ userId: user.id })
|
|
38
|
-
token.set({ value: tokenValue, httpOnly: true, sameSite: 'lax', path: '/' })
|
|
39
|
-
const { passwordHash: _ph, ...safeUser } = user
|
|
40
|
-
return safeUser
|
|
41
|
-
},
|
|
42
|
-
{
|
|
43
|
-
body: t.Object({
|
|
44
|
-
username: t.String({ minLength: 3 }),
|
|
45
|
-
password: t.String({ minLength: 8 }),
|
|
46
|
-
email: t.Optional(t.String({ format: 'email' })),
|
|
47
|
-
}),
|
|
48
|
-
},
|
|
49
|
-
)
|
|
50
|
-
.post(
|
|
51
|
-
'/login',
|
|
52
|
-
async ({ body, jwt, cookie: { token }, set }) => {
|
|
53
|
-
const [user] = await db.select().from(users).where(eq(users.username, body.username)).limit(1)
|
|
54
|
-
if (!user || !user.passwordHash || !(await verifyPassword(body.password, user.passwordHash))) {
|
|
55
|
-
set.status = 401
|
|
56
|
-
return { error: 'Invalid username or password' }
|
|
57
|
-
}
|
|
58
|
-
const tokenValue = await jwt.sign({ userId: user.id })
|
|
59
|
-
token.set({ value: tokenValue, httpOnly: true, sameSite: 'lax', path: '/' })
|
|
60
|
-
const { passwordHash: _ph, ...safeUser } = user
|
|
61
|
-
return safeUser
|
|
62
|
-
},
|
|
63
|
-
{
|
|
64
|
-
body: t.Object({
|
|
65
|
-
username: t.String(),
|
|
66
|
-
password: t.String(),
|
|
67
|
-
}),
|
|
68
|
-
},
|
|
69
|
-
)
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import { pgTable, serial, text, integer, boolean, timestamp, doublePrecision } from 'drizzle-orm/pg-core'
|
|
2
|
-
{{#if isTypeScript}}
|
|
3
|
-
import type { InferSelectModel, InferInsertModel } from 'drizzle-orm'
|
|
4
|
-
{{/if}}
|
|
5
|
-
|
|
6
|
-
{{#if hasAuth}}
|
|
7
|
-
// Users table — generated by Vasp auth system
|
|
8
|
-
export const users = pgTable('users', {
|
|
9
|
-
id: serial('id').primaryKey(),
|
|
10
|
-
username: text('username').notNull().unique(),
|
|
11
|
-
email: text('email').unique(),
|
|
12
|
-
passwordHash: text('password_hash'),
|
|
13
|
-
googleId: text('google_id').unique(),
|
|
14
|
-
githubId: text('github_id').unique(),
|
|
15
|
-
createdAt: timestamp('created_at').defaultNow().notNull(),
|
|
16
|
-
updatedAt: timestamp('updated_at').defaultNow().notNull(),
|
|
17
|
-
})
|
|
18
|
-
{{#if isTypeScript}}
|
|
19
|
-
export type User = InferSelectModel<typeof users>
|
|
20
|
-
export type NewUser = InferInsertModel<typeof users>
|
|
21
|
-
{{/if}}
|
|
22
|
-
|
|
23
|
-
{{/if}}
|
|
24
|
-
{{#each crudsWithFields}}
|
|
25
|
-
{{#if hasEntity}}
|
|
26
|
-
// {{entity}} table — generated from entity block
|
|
27
|
-
export const {{camelCase entity}}s = pgTable('{{camelCase entity}}s', {
|
|
28
|
-
{{#each fields}}
|
|
29
|
-
{{camelCase name}}: {{{drizzleColumn name type modifiers}}},
|
|
30
|
-
{{/each}}
|
|
31
|
-
createdAt: timestamp('created_at').defaultNow().notNull(),
|
|
32
|
-
updatedAt: timestamp('updated_at').defaultNow().notNull(),
|
|
33
|
-
})
|
|
34
|
-
{{else}}
|
|
35
|
-
// {{entity}} table — no entity block found, add your columns below
|
|
36
|
-
export const {{camelCase entity}}s = pgTable('{{camelCase entity}}s', {
|
|
37
|
-
id: serial('id').primaryKey(),
|
|
38
|
-
// TODO: Add your {{entity}} columns here
|
|
39
|
-
createdAt: timestamp('created_at').defaultNow().notNull(),
|
|
40
|
-
updatedAt: timestamp('updated_at').defaultNow().notNull(),
|
|
41
|
-
})
|
|
42
|
-
{{/if}}
|
|
43
|
-
{{#if ../isTypeScript}}
|
|
44
|
-
export type {{pascalCase entity}} = InferSelectModel<typeof {{camelCase entity}}s>
|
|
45
|
-
export type New{{pascalCase entity}} = InferInsertModel<typeof {{camelCase entity}}s>
|
|
46
|
-
{{/if}}
|
|
47
|
-
|
|
48
|
-
{{/each}}
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { getBoss } from './boss.{{ext}}'
|
|
2
|
-
import { {{namedExport}} } from '{{importPath fnSource ext}}'
|
|
3
|
-
|
|
4
|
-
const JOB_NAME = '{{camelCase name}}'
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Register the '{{name}}' job worker with PgBoss.
|
|
8
|
-
* Called once on server startup.
|
|
9
|
-
*/
|
|
10
|
-
export async function register{{pascalCase name}}Worker() {
|
|
11
|
-
const boss = await getBoss()
|
|
12
|
-
await boss.work(JOB_NAME, async (job) => {
|
|
13
|
-
await {{namedExport}}(job.data)
|
|
14
|
-
})
|
|
15
|
-
{{#if hasSchedule}}
|
|
16
|
-
|
|
17
|
-
// Register cron schedule: {{schedule}}
|
|
18
|
-
await boss.schedule(JOB_NAME, '{{schedule}}', {})
|
|
19
|
-
{{/if}}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Schedule a '{{name}}' job on demand.
|
|
24
|
-
* @param {unknown} data - Data to pass to the job handler
|
|
25
|
-
*/
|
|
26
|
-
export async function schedule{{pascalCase name}}(data) {
|
|
27
|
-
const boss = await getBoss()
|
|
28
|
-
return boss.send(JOB_NAME, data, {
|
|
29
|
-
retryLimit: Number(process.env.JOB_RETRY_LIMIT) || 3,
|
|
30
|
-
retryDelay: Number(process.env.JOB_RETRY_DELAY) || 60,
|
|
31
|
-
retryBackoff: true,
|
|
32
|
-
expireInMinutes: Number(process.env.JOB_EXPIRE_MINUTES) || 15,
|
|
33
|
-
})
|
|
34
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import PgBoss from 'pg-boss'
|
|
2
|
-
|
|
3
|
-
const connectionString = process.env.DATABASE_URL
|
|
4
|
-
if (!connectionString) throw new Error('DATABASE_URL is required for PgBoss job queue')
|
|
5
|
-
|
|
6
|
-
// Singleton PgBoss instance shared across all job workers
|
|
7
|
-
let boss = null
|
|
8
|
-
|
|
9
|
-
export async function getBoss() {
|
|
10
|
-
if (!boss) {
|
|
11
|
-
boss = new PgBoss(connectionString)
|
|
12
|
-
await boss.start()
|
|
13
|
-
}
|
|
14
|
-
return boss
|
|
15
|
-
}
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "{{kebabCase appName}}",
|
|
3
|
-
"version": "0.1.0",
|
|
4
|
-
"private": true,
|
|
5
|
-
"type": "module",
|
|
6
|
-
"scripts": {
|
|
7
|
-
"dev": "vasp start",
|
|
8
|
-
"build": "vasp build",
|
|
9
|
-
"dev:server": "bun --hot server/index.{{ext}}",
|
|
10
|
-
"dev:client": "{{#if isSpa}}vite{{else}}nuxt dev{{/if}}",
|
|
11
|
-
"db:generate": "bunx drizzle-kit generate",
|
|
12
|
-
"db:migrate": "bunx drizzle-kit migrate",
|
|
13
|
-
"db:studio": "bunx drizzle-kit studio"
|
|
14
|
-
},
|
|
15
|
-
"dependencies": {
|
|
16
|
-
"@vasp-framework/runtime": "^0.1.0",
|
|
17
|
-
"elysia": "^1.1.0",
|
|
18
|
-
"@elysiajs/cors": "^1.1.0",
|
|
19
|
-
"@elysiajs/static": "^1.1.0",
|
|
20
|
-
"drizzle-orm": "^0.36.0",
|
|
21
|
-
"postgres": "^3.4.0",
|
|
22
|
-
"ofetch": "^1.3.4",
|
|
23
|
-
"vue": "^3.5.0"{{#if isSpa}},
|
|
24
|
-
"vue-router": "^4.4.0"{{else}},
|
|
25
|
-
"nuxt": "^4.0.0"{{/if}}{{#if hasJobs}},
|
|
26
|
-
"pg-boss": "^10.0.0"{{/if}}
|
|
27
|
-
},
|
|
28
|
-
"devDependencies": {
|
|
29
|
-
"drizzle-kit": "^0.28.0"{{#if isSpa}},
|
|
30
|
-
"@vitejs/plugin-vue": "^5.2.0",
|
|
31
|
-
"vite": "^6.0.0"{{/if}}{{#if isTypeScript}},
|
|
32
|
-
"typescript": "^5.6.0",
|
|
33
|
-
"vue-tsc": "^2.0.0"{{/if}}
|
|
34
|
-
}
|
|
35
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { drizzle } from 'drizzle-orm/postgres-js'
|
|
2
|
-
import postgres from 'postgres'
|
|
3
|
-
import * as schema from '../../drizzle/schema.{{ext}}'
|
|
4
|
-
|
|
5
|
-
const connectionString = process.env.DATABASE_URL
|
|
6
|
-
|
|
7
|
-
if (!connectionString) {
|
|
8
|
-
throw new Error('DATABASE_URL environment variable is required')
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
const client = postgres(connectionString)
|
|
12
|
-
export const db = drizzle(client, { schema })
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import { Elysia } from 'elysia'
|
|
2
|
-
import { cors } from '@elysiajs/cors'
|
|
3
|
-
import { staticPlugin } from '@elysiajs/static'
|
|
4
|
-
import { db } from './db/client.{{ext}}'
|
|
5
|
-
import { rateLimit } from './middleware/rateLimit.{{ext}}'
|
|
6
|
-
{{#if isSsr}}
|
|
7
|
-
import { csrfProtection } from './middleware/csrf.{{ext}}'
|
|
8
|
-
{{/if}}
|
|
9
|
-
{{#if hasAuth}}
|
|
10
|
-
import { authRoutes } from './auth/index.{{ext}}'
|
|
11
|
-
{{/if}}
|
|
12
|
-
{{#each queries}}
|
|
13
|
-
import { {{camelCase name}}Route } from './routes/queries/{{camelCase name}}.{{../ext}}'
|
|
14
|
-
{{/each}}
|
|
15
|
-
{{#each actions}}
|
|
16
|
-
import { {{camelCase name}}Route } from './routes/actions/{{camelCase name}}.{{../ext}}'
|
|
17
|
-
{{/each}}
|
|
18
|
-
{{#each cruds}}
|
|
19
|
-
import { {{camelCase entity}}CrudRoutes } from './routes/crud/{{camelCase entity}}.{{../ext}}'
|
|
20
|
-
{{/each}}
|
|
21
|
-
{{#if hasRealtime}}
|
|
22
|
-
import { realtimeRoutes } from './routes/realtime/index.{{ext}}'
|
|
23
|
-
{{/if}}
|
|
24
|
-
{{#each jobs}}
|
|
25
|
-
import { {{camelCase name}}ScheduleRoute } from './routes/jobs/{{camelCase name}}Schedule.{{../ext}}'
|
|
26
|
-
{{/each}}
|
|
27
|
-
|
|
28
|
-
const PORT = Number(process.env.PORT) || {{backendPort}}
|
|
29
|
-
|
|
30
|
-
const app = new Elysia()
|
|
31
|
-
.use(cors({
|
|
32
|
-
origin: process.env.CORS_ORIGIN || 'http://localhost:{{frontendPort}}',
|
|
33
|
-
credentials: true,
|
|
34
|
-
}))
|
|
35
|
-
.use(rateLimit())
|
|
36
|
-
{{#if isSsr}}
|
|
37
|
-
.use(csrfProtection())
|
|
38
|
-
{{/if}}
|
|
39
|
-
.get('/api/health', () => ({ status: 'ok', version: '{{vaspVersion}}' }))
|
|
40
|
-
{{#if hasAuth}}
|
|
41
|
-
.use(authRoutes)
|
|
42
|
-
{{/if}}
|
|
43
|
-
{{#each queries}}
|
|
44
|
-
.use({{camelCase name}}Route)
|
|
45
|
-
{{/each}}
|
|
46
|
-
{{#each actions}}
|
|
47
|
-
.use({{camelCase name}}Route)
|
|
48
|
-
{{/each}}
|
|
49
|
-
{{#each cruds}}
|
|
50
|
-
.use({{camelCase entity}}CrudRoutes)
|
|
51
|
-
{{/each}}
|
|
52
|
-
{{#if hasRealtime}}
|
|
53
|
-
.use(realtimeRoutes)
|
|
54
|
-
{{/if}}
|
|
55
|
-
{{#each jobs}}
|
|
56
|
-
.use({{camelCase name}}ScheduleRoute)
|
|
57
|
-
{{/each}}
|
|
58
|
-
.listen(PORT)
|
|
59
|
-
|
|
60
|
-
console.log(`🚀 Vasp backend running at http://localhost:${PORT}`)
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { Elysia } from 'elysia'
|
|
2
|
-
import { db } from '../../db/client.{{ext}}'
|
|
3
|
-
{{#if requiresAuth}}
|
|
4
|
-
import { requireAuth } from '../../auth/middleware.{{ext}}'
|
|
5
|
-
{{/if}}
|
|
6
|
-
import { {{namedExport}} } from '{{importPath fnSource ext}}'
|
|
7
|
-
|
|
8
|
-
export const {{camelCase name}}Route = new Elysia()
|
|
9
|
-
{{#if requiresAuth}}
|
|
10
|
-
.use(requireAuth)
|
|
11
|
-
.post('/api/actions/{{camelCase name}}', async ({ body, user }) => {
|
|
12
|
-
const result = await {{namedExport}}({ db, user, args: body })
|
|
13
|
-
return result
|
|
14
|
-
})
|
|
15
|
-
{{else}}
|
|
16
|
-
.post('/api/actions/{{camelCase name}}', async ({ body }) => {
|
|
17
|
-
const result = await {{namedExport}}({ db, args: body })
|
|
18
|
-
return result
|
|
19
|
-
})
|
|
20
|
-
{{/if}}
|
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
import { Elysia, t } from 'elysia'
|
|
2
|
-
import { db } from '../../db/client.{{ext}}'
|
|
3
|
-
import { eq, sql, asc, desc, and, ilike } from 'drizzle-orm'
|
|
4
|
-
import { {{camelCase entity}}s } from '../../../drizzle/schema.{{ext}}'
|
|
5
|
-
{{#if hasAuth}}
|
|
6
|
-
import { requireAuth } from '../../auth/middleware.{{ext}}'
|
|
7
|
-
{{/if}}
|
|
8
|
-
{{#if hasRealtime}}
|
|
9
|
-
import { publish{{pascalCase entity}} } from '../realtime/{{camelCase realtimeName}}.{{ext}}'
|
|
10
|
-
{{/if}}
|
|
11
|
-
|
|
12
|
-
export const {{camelCase entity}}CrudRoutes = new Elysia({ prefix: '/api/crud/{{camelCase entity}}' })
|
|
13
|
-
{{#if hasAuth}}
|
|
14
|
-
.use(requireAuth)
|
|
15
|
-
{{/if}}
|
|
16
|
-
{{#if (includes operations "list")}}
|
|
17
|
-
.get('/', async ({ query }) => {
|
|
18
|
-
const limit = Math.min(Math.max(Number(query.limit) || 20, 1), 100)
|
|
19
|
-
const offset = Math.max(Number(query.offset) || 0, 0)
|
|
20
|
-
|
|
21
|
-
const table = {{camelCase entity}}s
|
|
22
|
-
|
|
23
|
-
// Multi-column sorting: orderBy=col1,col2 & dir=asc,desc
|
|
24
|
-
const orderByFields = (query.orderBy ?? 'id').split(',')
|
|
25
|
-
const directions = (query.dir ?? 'asc').split(',')
|
|
26
|
-
const orderClauses = orderByFields.map((field, i) => {
|
|
27
|
-
const col = table[field.trim()] ?? table.id
|
|
28
|
-
const dirFn = (directions[i] ?? directions[0] ?? 'asc').trim() === 'desc' ? desc : asc
|
|
29
|
-
return dirFn(col)
|
|
30
|
-
})
|
|
31
|
-
|
|
32
|
-
// Build WHERE conditions from filter.* query params
|
|
33
|
-
const conditions = []
|
|
34
|
-
for (const [key, value] of Object.entries(query)) {
|
|
35
|
-
if (!key.startsWith('filter.')) continue
|
|
36
|
-
const field = key.slice(7)
|
|
37
|
-
if (!table[field]) continue
|
|
38
|
-
conditions.push(eq(table[field], value))
|
|
39
|
-
}
|
|
40
|
-
const where = conditions.length > 0 ? and(...conditions) : undefined
|
|
41
|
-
|
|
42
|
-
const baseQuery = db.select().from(table)
|
|
43
|
-
const countQuery = db.select({ count: sql`count(*)::int` }).from(table)
|
|
44
|
-
|
|
45
|
-
const [data, countResult] = await Promise.all([
|
|
46
|
-
(where ? baseQuery.where(where) : baseQuery).orderBy(...orderClauses).limit(limit).offset(offset),
|
|
47
|
-
where ? countQuery.where(where) : countQuery,
|
|
48
|
-
])
|
|
49
|
-
|
|
50
|
-
return { data, total: countResult[0]?.count ?? 0, limit, offset }
|
|
51
|
-
})
|
|
52
|
-
{{/if}}
|
|
53
|
-
{{#if (includes operations "create")}}
|
|
54
|
-
.post('/', async ({ body }) => {
|
|
55
|
-
const [created] = await db.insert({{camelCase entity}}s).values(body).returning()
|
|
56
|
-
{{#if hasRealtime}}
|
|
57
|
-
publish{{pascalCase entity}}('created', created)
|
|
58
|
-
{{/if}}
|
|
59
|
-
return created
|
|
60
|
-
})
|
|
61
|
-
{{/if}}
|
|
62
|
-
.get('/:id', async ({ params: { id }, set }) => {
|
|
63
|
-
const [item] = await db.select().from({{camelCase entity}}s).where(eq({{camelCase entity}}s.id, Number(id))).limit(1)
|
|
64
|
-
if (!item) { set.status = 404; return { error: 'Not found' } }
|
|
65
|
-
return item
|
|
66
|
-
})
|
|
67
|
-
{{#if (includes operations "update")}}
|
|
68
|
-
.put('/:id', async ({ params: { id }, body, set }) => {
|
|
69
|
-
const [updated] = await db.update({{camelCase entity}}s).set(body).where(eq({{camelCase entity}}s.id, Number(id))).returning()
|
|
70
|
-
if (!updated) { set.status = 404; return { error: 'Not found' } }
|
|
71
|
-
{{#if hasRealtime}}
|
|
72
|
-
publish{{pascalCase entity}}('updated', updated)
|
|
73
|
-
{{/if}}
|
|
74
|
-
return updated
|
|
75
|
-
})
|
|
76
|
-
{{/if}}
|
|
77
|
-
{{#if (includes operations "delete")}}
|
|
78
|
-
.delete('/:id', async ({ params: { id }, set }) => {
|
|
79
|
-
const [deleted] = await db.delete({{camelCase entity}}s).where(eq({{camelCase entity}}s.id, Number(id))).returning()
|
|
80
|
-
if (!deleted) { set.status = 404; return { error: 'Not found' } }
|
|
81
|
-
{{#if hasRealtime}}
|
|
82
|
-
publish{{pascalCase entity}}('deleted', deleted)
|
|
83
|
-
{{/if}}
|
|
84
|
-
return { ok: true }
|
|
85
|
-
})
|
|
86
|
-
{{/if}}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { Elysia, t } from 'elysia'
|
|
2
|
-
import { schedule{{pascalCase name}} } from '../../../jobs/{{camelCase name}}.{{ext}}'
|
|
3
|
-
|
|
4
|
-
export const {{camelCase name}}ScheduleRoute = new Elysia()
|
|
5
|
-
.post(
|
|
6
|
-
'/api/jobs/{{camelCase name}}/schedule',
|
|
7
|
-
async ({ body }) => {
|
|
8
|
-
const id = await schedule{{pascalCase name}}(body)
|
|
9
|
-
return { jobId: id }
|
|
10
|
-
},
|
|
11
|
-
{ body: t.Unknown() },
|
|
12
|
-
)
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { Elysia } from 'elysia'
|
|
2
|
-
import { db } from '../../db/client.{{ext}}'
|
|
3
|
-
{{#if requiresAuth}}
|
|
4
|
-
import { requireAuth } from '../../auth/middleware.{{ext}}'
|
|
5
|
-
{{/if}}
|
|
6
|
-
import { {{namedExport}} } from '{{importPath fnSource ext}}'
|
|
7
|
-
|
|
8
|
-
export const {{camelCase name}}Route = new Elysia()
|
|
9
|
-
{{#if requiresAuth}}
|
|
10
|
-
.use(requireAuth)
|
|
11
|
-
.get('/api/queries/{{camelCase name}}', async ({ query, user }) => {
|
|
12
|
-
const result = await {{namedExport}}({ db, user, args: query })
|
|
13
|
-
return result
|
|
14
|
-
})
|
|
15
|
-
{{else}}
|
|
16
|
-
.get('/api/queries/{{camelCase name}}', async ({ query }) => {
|
|
17
|
-
const result = await {{namedExport}}({ db, args: query })
|
|
18
|
-
return result
|
|
19
|
-
})
|
|
20
|
-
{{/if}}
|