Skip to content

Commit acfbd4e

Browse files
committed
chore: up
1 parent 61f32f2 commit acfbd4e

File tree

10 files changed

+118
-106
lines changed

10 files changed

+118
-106
lines changed

_nuxthub/composables/config.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

_nuxthub/composables/hub.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export function useHub() {
2+
const config = useState<HubConfig['public']>('hub_config')
3+
4+
return {
5+
config
6+
}
7+
}

_nuxthub/plugins/config.server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export default defineNuxtPlugin(() => {
22
const event = useRequestEvent()
33

4-
useState('hub_config', () => event?.context.config?.public || {})
4+
useState('hub_config', () => event?.context.hub?.config?.public || {})
55
})

_nuxthub/server/api/auth/[...provider].get.ts renamed to _nuxthub/server/api/auth/providers/[...provider].get.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ export default eventHandler(async event => {
1212
throw createError({ statusCode: 400, message: 'Could not resolve this provider.' })
1313
}
1414

15+
// TODO: handle redirect with ?redirect query param (must start with /)
1516
return oauth[handlerName as OAuthHandler]({
1617
config: oauthConfig as any,
1718
async onSuccess(event, { user }) {
19+
// TODO: handle user creation in database with provider
1820
await setUserSession(event, { user })
19-
return sendRedirect(event, '/todos')
21+
return sendRedirect(event, config.oauth.redirect)
2022
}
2123
})(event)
2224
})
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
export default eventHandler(async (event) => {
2-
event.context.config = await _fetchConfig()
2+
const config = await _fetchConfig(event)
3+
4+
event.context.hub = {
5+
config
6+
}
37
})

_nuxthub/server/utils/config.ts

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,60 @@
1+
import type { H3Event } from 'h3'
12
import type { Storage } from 'unstorage'
23
import { defu } from 'defu'
34
import { createStorage } from 'unstorage'
45
import httpDriver from 'unstorage/drivers/http'
56
import cloudflareKVBindingDriver from 'unstorage/drivers/cloudflare-kv-binding'
67
import { joinURL } from 'ufo'
78

8-
const defaults: Config = {
9-
oauth: {
10-
github: {
11-
clientId: process.env.NUXT_OAUTH_GITHUB_CLIENT_ID,
12-
clientSecret: process.env.NUXT_OAUTH_GITHUB_CLIENT_SECRET
13-
}
14-
},
15-
public: {
16-
features: {
17-
// users features
18-
}
19-
}
20-
}
21-
229
let _configKV: Storage
2310

2411
export function useConfigKV () {
25-
if (!_configKV) {
26-
if (import.meta.dev && process.env.NUXT_HUB_URL) {
27-
// Use https://unstorage.unjs.io/drivers/http
28-
_configKV = createStorage({
29-
driver: httpDriver({
30-
base: joinURL(process.env.NUXT_HUB_URL, '/api/_hub/config/'),
31-
headers: {
32-
Authorization: `Bearer ${process.env.NUXT_HUB_SECRET_KEY}`
33-
}
34-
})
12+
if (_configKV) {
13+
return _configKV
14+
}
15+
if (import.meta.dev && process.env.NUXT_HUB_URL) {
16+
// Use https://unstorage.unjs.io/drivers/http
17+
_configKV = createStorage({
18+
driver: httpDriver({
19+
base: joinURL(process.env.NUXT_HUB_URL, '/api/_hub/config/'),
20+
headers: {
21+
Authorization: `Bearer ${process.env.NUXT_HUB_SECRET_KEY}`
22+
}
3523
})
36-
} else {
37-
const binding = process.env.CONFIG || globalThis.__env__?.CONFIG || globalThis.CONFIG
38-
if (binding) {
39-
_configKV = createStorage({
40-
driver: cloudflareKVBindingDriver({
41-
binding
42-
})
43-
})
44-
} else {
45-
throw createError('Missing Cloudflare binding CONFIG')
46-
}
47-
}
24+
})
25+
return _configKV
26+
}
27+
// @ts-ignore
28+
const binding = process.env.CONFIG || globalThis.__env__?.CONFIG || globalThis.CONFIG
29+
if (!binding) {
30+
throw createError('Missing Cloudflare binding CONFIG')
4831
}
32+
_configKV = createStorage({
33+
driver: cloudflareKVBindingDriver({
34+
binding
35+
})
36+
})
4937

5038
return _configKV
5139
}
5240

53-
let _config: Config
41+
let _config: HubConfig
5442

55-
export async function _fetchConfig() {
56-
let configValue = await useConfigKV().getItem<Config>('config')
57-
configValue = z.custom<Config>().parse(configValue)
58-
_config = defu(configValue, defaults)
43+
export async function _fetchConfig(event: H3Event) {
44+
const runtimeConfig = useRuntimeConfig(event)
45+
let configValue = await useConfigKV().getItem<HubConfig>('config')
46+
configValue = z.custom<HubConfig>().parse(configValue)
47+
_config = defu(configValue, {
48+
oauth: {
49+
redirect: '/',
50+
...runtimeConfig.oauth
51+
},
52+
public: {
53+
features: {
54+
// users features
55+
}
56+
}
57+
} as HubConfig)
5958

6059
return _config
6160
}
@@ -68,12 +67,12 @@ export function getConfig() {
6867
return _config
6968
}
7069

71-
export async function setConfig(config: Config) {
70+
export async function setConfig(config: HubConfig) {
7271
if (!_config) {
7372
throw createError('Please run _fetchConfig() in order to use setConfig()')
7473
}
7574

76-
let configValue = z.custom<Config>().parse(config)
75+
let configValue = z.custom<HubConfig>().parse(config)
7776
configValue = defu(config, _config)
7877
await useConfigKV().setItem('config', configValue)
7978
_config = configValue

_nuxthub/server/utils/database.ts

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,39 @@ let _db: DrizzleD1Database | BetterSQLite3Database | SqliteRemoteDatabase
1414
let _client: D1Database
1515

1616
export function useDatabase () {
17-
if (!_db) {
18-
if (import.meta.dev && process.env.NUXT_HUB_URL) {
19-
_db = drizzleHTTP(async (sql, params, method) => {
20-
// https://orm.drizzle.team/docs/get-started-sqlite#http-proxy
21-
try {
22-
const rows = await ofetch('/api/_hub/database/query', {
23-
baseURL: process.env.NUXT_HUB_URL,
24-
method: 'POST',
25-
body: { sql, params, method },
26-
headers: {
27-
Authorization: `Bearer ${process.env.NUXT_HUB_SECRET_KEY}`
28-
}
29-
})
30-
if (method === 'run') return rows
31-
return { rows }
32-
} catch (err: any) {
33-
if (['begin', 'commit'].includes(sql)) {
34-
return { rows: [] }
17+
if (_db) {
18+
return _db
19+
}
20+
if (import.meta.dev && process.env.NUXT_HUB_URL) {
21+
_db = drizzleHTTP(async (sql, params, method) => {
22+
// https://orm.drizzle.team/docs/get-started-sqlite#http-proxy
23+
try {
24+
const rows = await ofetch('/api/_hub/database/query', {
25+
baseURL: process.env.NUXT_HUB_URL,
26+
method: 'POST',
27+
body: { sql, params, method },
28+
headers: {
29+
Authorization: `Bearer ${process.env.NUXT_HUB_SECRET_KEY}`
3530
}
36-
console.error('Error from remote database:', err.data.message, '\n', { sql, params, method })
31+
})
32+
if (method === 'run') return rows
33+
return { rows }
34+
} catch (err: any) {
35+
if (['begin', 'commit'].includes(sql)) {
3736
return { rows: [] }
3837
}
39-
})
40-
} else {
41-
const binding = process.env.DB || globalThis.__env__?.DB || globalThis.DB
42-
if (binding) {
43-
_client = binding as D1Database
44-
_db = drizzleD1(_client)
45-
} else {
46-
throw createError('Missing Cloudflare D1 binding DB')
38+
console.error('Error from remote database:', err.data.message, '\n', { sql, params, method })
39+
return { rows: [] }
4740
}
48-
}
41+
})
42+
return _db
43+
}
44+
const binding = process.env.DB || globalThis.__env__?.DB || globalThis.DB
45+
if (binding) {
46+
_client = binding as D1Database
47+
_db = drizzleD1(_client)
48+
} else {
49+
throw createError('Missing Cloudflare D1 binding DB')
4950
}
5051
return _db
5152
}

_nuxthub/server/utils/kv.ts

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,30 @@ import { joinURL } from 'ufo'
77
let _kv: Storage
88

99
export function useKV () {
10-
if (!_kv) {
11-
if (import.meta.dev && process.env.NUXT_HUB_URL) {
12-
// Use https://unstorage.unjs.io/drivers/http
13-
_kv = createStorage({
14-
driver: httpDriver({
15-
base: joinURL(process.env.NUXT_HUB_URL, '/api/_hub/kv/'),
16-
headers: {
17-
Authorization: `Bearer ${process.env.NUXT_HUB_SECRET_KEY}`
18-
}
19-
})
10+
if (_kv) {
11+
return _kv
12+
}
13+
if (import.meta.dev && process.env.NUXT_HUB_URL) {
14+
// Use https://unstorage.unjs.io/drivers/http
15+
_kv = createStorage({
16+
driver: httpDriver({
17+
base: joinURL(process.env.NUXT_HUB_URL, '/api/_hub/kv/'),
18+
headers: {
19+
Authorization: `Bearer ${process.env.NUXT_HUB_SECRET_KEY}`
20+
}
2021
})
21-
} else {
22-
const binding = process.env.KV || globalThis.__env__?.KV || globalThis.KV
23-
if (binding) {
24-
_kv = createStorage({
25-
driver: cloudflareKVBindingDriver({
26-
binding
27-
})
28-
})
29-
} else {
30-
throw createError('Missing Cloudflare binding KV')
31-
}
32-
}
22+
})
23+
return _kv
24+
}
25+
const binding = process.env.KV || globalThis.__env__?.KV || globalThis.KV
26+
if (binding) {
27+
_kv = createStorage({
28+
driver: cloudflareKVBindingDriver({
29+
binding
30+
})
31+
})
32+
} else {
33+
throw createError('Missing Cloudflare binding KV')
3334
}
34-
3535
return _kv
3636
}

_nuxthub/types/config.d.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
declare interface Config {
2-
oauth?: {
1+
declare interface HubConfig {
2+
oauth: {
3+
redirect: string
34
[key: string]: {
45
clientId?: string
56
clientSecret?: string
67
}
78
},
8-
public?: {
9-
features?: object
10-
}
9+
/**
10+
* Configuration exposed to the Vue part
11+
*/
12+
public: {}
1113
}

pages/index.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const { loggedIn } = useUserSession()
2222
</h3>
2323
<UButton
2424
v-if="!loggedIn"
25-
to="/api/auth/github"
25+
to="/api/auth/providers/github"
2626
icon="i-simple-icons-github"
2727
label="Login with GitHub"
2828
color="black"

0 commit comments

Comments
 (0)