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

🔨 REFACTOR: update plugin-flydrive #195

Merged
merged 2 commits into from
Jun 26, 2024
Merged
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
5 changes: 5 additions & 0 deletions .changeset/metal-pugs-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@oberoncms/plugin-flydrive": patch
---

update flydrive handlers
5 changes: 5 additions & 0 deletions .changeset/tasty-owls-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@oberoncms/plugin-pgsql": patch
---

fix tsx prebuild
47 changes: 20 additions & 27 deletions apps/documentation/src/pages/docs/plugins/storage/flydrive.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ argument, the driver should be an instance of the flydrive any flydrive driver
Make sure to follow the [flydrive
documentation](https://flydrive.dev/docs/introduction) instructions.
</Callout>
```typescript export const {(flyDrivePlugin, initFlyDriveRouter)} = getFlyDrivePlugin(driver)

```typescript
// create the driver
import { getFlyDrivePlugin } from "@oberoncms/plugin-flydrive/plugin"
const flyDrivePlugin = getFlyDrivePlugin(driver) // <- pass a flydrive driver instance
```

## Example using S3 driver
Expand All @@ -41,13 +45,10 @@ create a flydrive adapter, and generate your plugin!
import "server-only"

import { initAdapter } from "@oberoncms/core/adapter"


import { S3Driver } from "flydrive/drivers/s3" // <- import the driver you want to use
import { getFlyDrivePlugin } from "@oberoncms/plugin-flydrive/plugin" // <- import the plugin
const s3Driver = new S3Driver({
const s3Driver = new S3Driver({ // <- create the s3 driver
credentials: {
accessKeyId: process.env.ACCESS_KEY_ID,
secretAccessKey: process.env.SECRET_ACCESS_KEY,
Expand All @@ -59,26 +60,18 @@ const s3Driver = new S3Driver({
generateURL() // mandatory
},
})

// generate the plugin & api, and export the router to use it in the api
export const { flyDrivePlugin, initFlyDriveRouter } = getFlyDrivePlugin(s3Driver)

// add the plugin to the oberon adapter
export const adapter = initAdapter([
flyDrivePlugin, // <- add the plugin
// other plugins
])
export const { adapter, handler } = initOberon({
config,
plugins: [
// other plugins || order doesn't matter
getFlyDrivePlugin(s3Driver), // <- generate and add the plugin
],
})
```

## Prepare API

```typescript
// api/flydrive/route.ts
import { initFlyDriveRouter } from "./adapter" // <where you placed your adapter.ts>
export const { GET, POST } = initFlyDriveRouter()
```

## Editor configuration

```typescript
Expand Down Expand Up @@ -146,13 +139,13 @@ import { VercelBlobDriver } from "@oberoncms/plugin-flydrive/vercel"

const vercelBlobDriver = new VercelBlobDriver() // <- create the driver, it will consume token from the environment

export const { flyDrivePlugin, initFlyDriveRouter } =
getFlyDrivePlugin(vercelBlobDriver)

export const adapter = initAdapter([
flyDrivePlugin,
// other plugins
])
export const { adapter, handler } = initOberon({
config,
plugins: [
// other plugins || order doesn't matter
getFlyDrivePlugin(vercelBlobDriver),
],
})
```

for API and Editor configuration, follow the same steps as shown in the S3
Expand Down
4 changes: 2 additions & 2 deletions packages/plugins/flydrive/src/internal/disk-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { getImageSize } from "./get-image-size"

export function initRouteHandler(disk: Disk): {
POST: (req: NextRequest) => Promise<Response>
GET: (req: NextRequest) => Response
GET: (req: NextRequest) => Promise<Response>
} {
const POST: (req: NextRequest) => Promise<Response> = async (req) => {
const image = (await req.formData()).get("image") as File | null
Expand Down Expand Up @@ -38,7 +38,7 @@ export function initRouteHandler(disk: Disk): {
})
}

const GET: (req: NextRequest) => Response = () => {
const GET: (req: NextRequest) => Promise<Response> = async () => {
// could be used to get presigned URLs
return new Response("GET request handled", { status: 200 })
}
Expand Down
35 changes: 13 additions & 22 deletions packages/plugins/flydrive/src/internal/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,19 @@ import type { DriverContract } from "flydrive/types"
import { name, version } from "../../package.json" with { type: "json" }
import { initRouteHandler } from "./disk-handlers"

interface plugin {
flyDrivePlugin: OberonPlugin
initFlyDriveRouter: () => ReturnType<typeof initRouteHandler>
}

export const getFlyDrivePlugin = (dickDriver: DriverContract): plugin => {
const driver = new Disk(dickDriver)
export const getFlyDrivePlugin = (diskDriver: DriverContract): OberonPlugin => {
const driver = new Disk(diskDriver)

return {
flyDrivePlugin: (adapter) => ({
name,
version,
adapter: {
deleteImage: async (key) => {
await Promise.allSettled([
//
driver.delete(key),
adapter.deleteImage(key),
])
},
return (adapter) => ({
name,
version,
handlers: {
flydrive: () => initRouteHandler(driver),
},
adapter: {
deleteImage: async (key) => {
await Promise.allSettled([driver.delete(key), adapter.deleteImage(key)])
},
}),
initFlyDriveRouter: () => initRouteHandler(driver),
}
},
})
}
4 changes: 2 additions & 2 deletions packages/plugins/pgsql/src/db/client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { drizzle, NodePgDatabase } from "drizzle-orm/node-postgres"
import { Pool } from "pg"

import pg from "pg"
const { Pool } = pg
import * as schema from "./schema"

export type DatabaseClient = NodePgDatabase<typeof schema>
Expand Down