mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-17 04:46:05 +00:00
init: nuxt adapter
This commit is contained in:
15
examples/nuxt/server/middleware/bknd.ts
Normal file
15
examples/nuxt/server/middleware/bknd.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { serve } from "bknd/adapter/nuxt";
|
||||
import config from "../../bknd.config";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const pathname = event.path
|
||||
const request = toWebRequest(event);
|
||||
|
||||
if (pathname.startsWith("/api") || pathname !== "/") {
|
||||
const res = await serve(config, process.env)(request);
|
||||
|
||||
if (res && res.status !== 404) {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
});
|
||||
31
examples/nuxt/server/routes/todos.post.ts
Normal file
31
examples/nuxt/server/routes/todos.post.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
export default defineEventHandler(async (event) => {
|
||||
const body = await readBody(event);
|
||||
const { data, action } = body;
|
||||
|
||||
const api = await getApi({});
|
||||
|
||||
switch (action) {
|
||||
case 'get': {
|
||||
const limit = 5;
|
||||
const todos = await api.data.readMany("todos", { limit, sort: "-id" });
|
||||
return { total: todos.body.meta.total, todos, limit };
|
||||
}
|
||||
|
||||
case 'create': {
|
||||
return await api.data.createOne("todos", { title: data.title });
|
||||
}
|
||||
|
||||
case 'delete': {
|
||||
return await api.data.deleteOne("todos", data.id);
|
||||
}
|
||||
|
||||
case 'toggle': {
|
||||
return await api.data.updateOne("todos", data.id, { done: !data.done });
|
||||
}
|
||||
|
||||
default: {
|
||||
return { path: action };
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
21
examples/nuxt/server/utils/bknd.ts
Normal file
21
examples/nuxt/server/utils/bknd.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { type NuxtBkndConfig, getApp as getNuxtApp } from "bknd/adapter/nuxt";
|
||||
import bkndConfig from "../../bknd.config";
|
||||
|
||||
export async function getApp<Env = NodeJS.ProcessEnv>(
|
||||
config: NuxtBkndConfig<Env>,
|
||||
args: Env = process.env as Env,
|
||||
) {
|
||||
return await getNuxtApp(config, args);
|
||||
}
|
||||
|
||||
export async function getApi({ headers, verify }: { verify?: boolean; headers?: Headers }) {
|
||||
const app = await getApp(bkndConfig, process.env);
|
||||
|
||||
if (verify) {
|
||||
const api = app.getApi({ headers });
|
||||
await api.verifyAuth();
|
||||
return api;
|
||||
}
|
||||
|
||||
return app.getApi();
|
||||
}
|
||||
Reference in New Issue
Block a user