Skip to content

Commit

Permalink
Move all handlers to single api handler endpoint under cms (#194)
Browse files Browse the repository at this point in the history
  • Loading branch information
4leite authored Jun 26, 2024
1 parent 7f48d1e commit 9bb8cb7
Show file tree
Hide file tree
Showing 23 changed files with 348 additions and 251 deletions.
11 changes: 11 additions & 0 deletions .changeset/five-penguins-rhyme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
"@oberoncms/plugin-uploadthing": minor
"create-oberon-app": minor
"@oberoncms/plugin-flydrive": minor
"@oberoncms/core": minor
"@tohuhono/dev": minor
"@oberon/playground": minor
"@oberon/recipe-nextjs": minor
---

Move all handlers to single api handler endpoint under cms
3 changes: 0 additions & 3 deletions apps/playground/app/(oberon)/api/auth/[...nextauth]/route.tsx

This file was deleted.

4 changes: 0 additions & 4 deletions apps/playground/app/(oberon)/api/uploadthing/route.ts

This file was deleted.

3 changes: 3 additions & 0 deletions apps/playground/app/(oberon)/cms/api/[...path]/route.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { handler } from "@/oberon/adapter"

export const { GET, POST, PUT, PATCH, DELETE } = handler
2 changes: 1 addition & 1 deletion apps/playground/oberon/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { plugin as uploadthingPlugin } from "@oberoncms/plugin-uploadthing/plugi
import { plugin as sendPlugin } from "./send"
import { config } from "./config"

export const { adapter, handlers } = initOberon({
export const { adapter, handler } = initOberon({
config,
plugins: [
developmentPlugin,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ ${pluginImports}
import { config } from "./config"
export const { adapter, handlers } = initOberon({
export const { adapter, handler } = initOberon({
config,
plugins: [
${pluginAliasNames.join(", ")}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,25 @@ import {
type TransformResult,
type OberonPage,
type PageData,
type OberonPlugin,
type OberonHandler,
type OberonPluginAdapter,
type PluginVersion,
} from "../lib/dtd"
import {
applyTransforms,
getComponentTransformVersions,
getTransforms,
} from "./transforms"
import { exportTailwindClasses } from "./export-tailwind-clases"
import { initPlugins } from "./init-plugins"

export function initOberon({
export function initAdapter({
config,
plugins,
versions,
pluginAdapter: adapter,
}: {
config: OberonConfig
plugins: OberonPlugin[]
}): {
handlers: Record<string, OberonHandler>
adapter: OberonAdapter
} {
const { versions, handlers, adapter } = initPlugins(plugins)

pluginAdapter: OberonPluginAdapter
versions: PluginVersion[]
}): OberonAdapter {
const can: OberonAdapter["can"] = async (action, permission = "read") => {
// Check unauthenticated first so we can do it outside of request context
if (adapter.hasPermission({ action, permission })) {
Expand Down Expand Up @@ -233,130 +229,127 @@ export function initOberon({
})

return {
handlers,
adapter: {
prebuild: async () => {
await adapter.prebuild()
await exportTailwindClasses(adapter)
},
/*
* Auth
*/
can,
signIn: adapter.signIn,
signOut: adapter.signOut,
/*
* Site actions
*/
getConfig: async () => {
await will("site", "read")
return await getConfigCached()
},
migrateData: async () => {
const user = await whoWill("site", "write")
return migrate(user)
},

/*
* Page actions
*/
getAllPaths: async function () {
await will("pages", "read")
return getAllPathsCached()
},
getAllPages: async function () {
await will("pages", "read")
return getAllPagesCached()
},
getPageData: async function (key) {
await will("pages", "read")
return getPageDataCached(key)
},
// TODO return value
addPage: async function (data: unknown) {
const user = await whoWill("pages", "write")
const { key } = AddPageSchema.parse(data)
await adapter.addPage({
key,
data: INITIAL_DATA,
updatedAt: new Date(),
updatedBy: user.email,
})
revalidatePath(key)
revalidateTag("oberon-pages")
},
// TODO return value
deletePage: async function (data: unknown) {
await will("pages", "write")
const { key } = DeletePageSchema.parse(data)
await adapter.deletePage(key)
revalidatePath(key)
revalidateTag("oberon-pages")
},
publishPageData: async function (data: unknown) {
const user = await whoWill("pages", "write")
const { key, data: pageData } = PublishPageSchema.parse(data)
await updatePageData({
key,
data: pageData as PageData,
updatedBy: user.email,
})
return { message: `Successfully published ${key}` }
},

/*
* Image actions
*/
getAllImages: async function () {
await will("images", "read")
return getAllImagesCached()
},
addImage: async function (data: unknown) {
await will("images", "write")

const image = AddImageSchema.parse(data)
await adapter.addImage(image)
revalidateTag("oberon-images")
return adapter.getAllImages()
},
// TODO uploadthing
deleteImage: async function (data) {
await will("images", "write")
revalidateTag("oberon-images")
return adapter.deleteImage(data)
},

/*
* User actions
*/
getAllUsers: async function () {
await will("users", "read")
return getAllUsersCached()
},
addUser: async function (data: unknown) {
await will("users", "write")
const { email, role } = AddUserSchema.parse(data)
const { id } = await adapter.addUser({
email,
role,
})
revalidateTag("oberon-users")
return { id, email, role }
},
deleteUser: async function (data: unknown) {
await will("users", "write")
const { id } = DeleteUserSchema.parse(data)
await adapter.deleteUser(id)
revalidateTag("oberon-users")
return { id }
},
changeRole: async function (data: unknown) {
await will("users", "write")
const { role, id } = ChangeRoleSchema.parse(data)
await adapter.changeRole({ role, id })
revalidateTag("oberon-users")
return { role, id }
},
prebuild: async () => {
await adapter.prebuild()
await exportTailwindClasses(adapter)
},
/*
* Auth
*/
can,
signIn: adapter.signIn,
signOut: adapter.signOut,
/*
* Site actions
*/
getConfig: async () => {
await will("site", "read")
return await getConfigCached()
},
migrateData: async () => {
const user = await whoWill("site", "write")
return migrate(user)
},

/*
* Page actions
*/
getAllPaths: async function () {
await will("pages", "read")
return getAllPathsCached()
},
getAllPages: async function () {
await will("pages", "read")
return getAllPagesCached()
},
getPageData: async function (key) {
await will("pages", "read")
return getPageDataCached(key)
},
// TODO return value
addPage: async function (data: unknown) {
const user = await whoWill("pages", "write")
const { key } = AddPageSchema.parse(data)
await adapter.addPage({
key,
data: INITIAL_DATA,
updatedAt: new Date(),
updatedBy: user.email,
})
revalidatePath(key)
revalidateTag("oberon-pages")
},
// TODO return value
deletePage: async function (data: unknown) {
await will("pages", "write")
const { key } = DeletePageSchema.parse(data)
await adapter.deletePage(key)
revalidatePath(key)
revalidateTag("oberon-pages")
},
publishPageData: async function (data: unknown) {
const user = await whoWill("pages", "write")
const { key, data: pageData } = PublishPageSchema.parse(data)
await updatePageData({
key,
data: pageData as PageData,
updatedBy: user.email,
})
return { message: `Successfully published ${key}` }
},

/*
* Image actions
*/
getAllImages: async function () {
await will("images", "read")
return getAllImagesCached()
},
addImage: async function (data: unknown) {
await will("images", "write")

const image = AddImageSchema.parse(data)
await adapter.addImage(image)
revalidateTag("oberon-images")
return adapter.getAllImages()
},
// TODO uploadthing
deleteImage: async function (data) {
await will("images", "write")
revalidateTag("oberon-images")
return adapter.deleteImage(data)
},

/*
* User actions
*/
getAllUsers: async function () {
await will("users", "read")
return getAllUsersCached()
},
addUser: async function (data: unknown) {
await will("users", "write")
const { email, role } = AddUserSchema.parse(data)
const { id } = await adapter.addUser({
email,
role,
})
revalidateTag("oberon-users")
return { id, email, role }
},
deleteUser: async function (data: unknown) {
await will("users", "write")
const { id } = DeleteUserSchema.parse(data)
await adapter.deleteUser(id)
revalidateTag("oberon-users")
return { id }
},
changeRole: async function (data: unknown) {
await will("users", "write")
const { role, id } = ChangeRoleSchema.parse(data)
await adapter.changeRole({ role, id })
revalidateTag("oberon-users")
return { role, id }
},
}
}
Loading

0 comments on commit 9bb8cb7

Please sign in to comment.