This repository has no description
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485import fs from 'fs'import path from 'path'import { sqliteD1Adapter } from '@payloadcms/db-d1-sqlite'import { lexicalEditor } from '@payloadcms/richtext-lexical'import { buildConfig } from 'payload'import { fileURLToPath } from 'url'import { CloudflareContext, getCloudflareContext } from '@opennextjs/cloudflare'import { GetPlatformProxyOptions } from 'wrangler'import { r2Storage } from '@payloadcms/storage-r2'
import { Users } from './collections/Users'import { Media } from './collections/Media'import { Tags } from './collections/Tags'import { Pages } from './collections/Pages'import { Projects } from './collections/Projects'import { Posts } from './collections/Posts'
const filename = fileURLToPath(import.meta.url)const dirname = path.dirname(filename)const realpath = (value: string) => (fs.existsSync(value) ? fs.realpathSync(value) : undefined)
const isCLI = process.argv.some((value) => realpath(value)?.endsWith(path.join('payload', 'bin.js')))const isProduction = process.env.NODE_ENV === 'production'
const createLog = (level: string, fn: typeof console.log) => (objOrMsg: object | string, msg?: string) => { if (typeof objOrMsg === 'string') { fn(JSON.stringify({ level, msg: objOrMsg })) } else { fn(JSON.stringify({ level, ...objOrMsg, msg: msg ?? (objOrMsg as { msg?: string }).msg })) } }
const cloudflareLogger = { level: process.env.PAYLOAD_LOG_LEVEL || 'info', trace: createLog('trace', console.debug), debug: createLog('debug', console.debug), info: createLog('info', console.log), warn: createLog('warn', console.warn), error: createLog('error', console.error), fatal: createLog('fatal', console.error), silent: () => {},} as any // Use PayloadLogger type when it's exported
const cloudflare = isCLI || !isProduction ? await getCloudflareContextFromWrangler() : await getCloudflareContext({ async: true })
export default buildConfig({ admin: { user: Users.slug, importMap: { baseDir: path.resolve(dirname), }, }, collections: [Users, Media, Tags, Pages, Projects, Posts], editor: lexicalEditor(), secret: process.env.PAYLOAD_SECRET || '', typescript: { outputFile: path.resolve(dirname, 'payload-types.ts'), }, db: sqliteD1Adapter({ binding: cloudflare.env.D1, }), logger: isProduction ? cloudflareLogger : undefined, plugins: [ r2Storage({ bucket: cloudflare.env.R2, collections: { media: true }, }), ],})
// Adapted from https://github.com/opennextjs/opennextjs-cloudflare/blob/d00b3a13e42e65aad76fba41774815726422cc39/packages/cloudflare/src/api/cloudflare-context.ts#L328C36-L328C46function getCloudflareContextFromWrangler(): Promise<CloudflareContext> { return import(/* webpackIgnore: true */ `${'__wrangler'.replaceAll('_', '')}`).then( ({ getPlatformProxy }) => getPlatformProxy({ environment: process.env.CLOUDFLARE_ENV, remoteBindings: isProduction, } satisfies GetPlatformProxyOptions), )}