Compare commits
82 Commits
v3.0.0-alp
...
v3.0.0-bet
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d8783eaad4 | ||
|
|
cddb08de1a | ||
|
|
03a110a750 | ||
|
|
6accc705be | ||
|
|
312dca003b | ||
|
|
94af06466b | ||
|
|
7cf2686097 | ||
|
|
f14883aa11 | ||
|
|
9df8de2386 | ||
|
|
8b2cf4705e | ||
|
|
364e9832ac | ||
|
|
2deeb61f17 | ||
|
|
14498e8a9c | ||
|
|
eb78022387 | ||
|
|
3677a59a78 | ||
|
|
7c1c840a59 | ||
|
|
a73eaf5d37 | ||
|
|
68989a58a8 | ||
|
|
9841731ae7 | ||
|
|
ba7ac5d439 | ||
|
|
7c60772b26 | ||
|
|
1141a5d3af | ||
|
|
6f74fd1f98 | ||
|
|
75873bfcfa | ||
|
|
1faf621f17 | ||
|
|
1d1c73dfcc | ||
|
|
d057ce0a85 | ||
|
|
0ce26d2c08 | ||
|
|
abf285d713 | ||
|
|
c2ee8e3999 | ||
|
|
167ba0c68f | ||
|
|
9ad1cbe920 | ||
|
|
313ea52e3d | ||
|
|
3acfb7a83f | ||
|
|
783dae2bbb | ||
|
|
59681b211b | ||
|
|
98438175cf | ||
|
|
af40302e5f | ||
|
|
ec0e0ae449 | ||
|
|
607ff17033 | ||
|
|
e73e610669 | ||
|
|
30fddde066 | ||
|
|
a56d2842fb | ||
|
|
35f59a47cc | ||
|
|
817d57bd12 | ||
|
|
5826048e7b | ||
|
|
1a975b31cf | ||
|
|
73298a80f0 | ||
|
|
a5d14ef4c1 | ||
|
|
d0c79b65f8 | ||
|
|
2fc50b1a1f | ||
|
|
0ddeedb0b3 | ||
|
|
09c2fb10f3 | ||
|
|
5bfff5b7ba | ||
|
|
702088375c | ||
|
|
ea507fbcc4 | ||
|
|
0ff1e6632b | ||
|
|
996ee47f96 | ||
|
|
3e9bd5bb62 | ||
|
|
ee7221c986 | ||
|
|
318c126ae3 | ||
|
|
2154aea89f | ||
|
|
4d3ad1af35 | ||
|
|
75cab7688f | ||
|
|
5084d6dd97 | ||
|
|
518f80cbb6 | ||
|
|
c9399efa65 | ||
|
|
dd9133659c | ||
|
|
d3016b7eb5 | ||
|
|
69e884f5b7 | ||
|
|
6d122905f4 | ||
|
|
e6e016ac2d | ||
|
|
3e7925e33f | ||
|
|
7a2ccba63c | ||
|
|
be2134eb69 | ||
|
|
95e422b0e1 | ||
|
|
53d9c4ca95 | ||
|
|
30948ab545 | ||
|
|
906df6b401 | ||
|
|
b9c585bab5 | ||
|
|
12203140ad | ||
|
|
0704152e38 |
76
.github/workflows/main.yml
vendored
76
.github/workflows/main.yml
vendored
@@ -4,7 +4,7 @@ on:
|
||||
pull_request:
|
||||
types: [opened, reopened, synchronize]
|
||||
push:
|
||||
branches: ['main', 'alpha']
|
||||
branches: ['main', 'beta']
|
||||
|
||||
concurrency:
|
||||
group: ${{ github.workflow }}-${{ github.ref }}
|
||||
@@ -39,7 +39,7 @@ jobs:
|
||||
echo "needs_build: ${{ steps.filter.outputs.needs_build }}"
|
||||
echo "templates: ${{ steps.filter.outputs.templates }}"
|
||||
|
||||
core-build:
|
||||
build:
|
||||
needs: changes
|
||||
if: ${{ needs.changes.outputs.needs_build == 'true' }}
|
||||
runs-on: ubuntu-latest
|
||||
@@ -65,66 +65,29 @@ jobs:
|
||||
run: |
|
||||
echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV
|
||||
|
||||
- uses: actions/cache@v4
|
||||
name: Setup pnpm cache
|
||||
- name: Setup pnpm cache
|
||||
uses: actions/cache@v4
|
||||
timeout-minutes: 720
|
||||
with:
|
||||
path: ${{ env.STORE_PATH }}
|
||||
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
|
||||
key: pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
|
||||
restore-keys: |
|
||||
${{ runner.os }}-pnpm-store-
|
||||
${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
|
||||
pnpm-store-
|
||||
pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
|
||||
|
||||
- run: pnpm install
|
||||
- run: pnpm run build:core
|
||||
- run: pnpm run build:all
|
||||
|
||||
- name: Cache build
|
||||
uses: actions/cache@v4
|
||||
timeout-minutes: 10
|
||||
with:
|
||||
path: ./*
|
||||
key: ${{ github.sha }}-${{ github.run_number }}
|
||||
|
||||
plugins-build:
|
||||
needs: changes
|
||||
if: ${{ needs.changes.outputs.needs_build == 'true' }}
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 25
|
||||
|
||||
- name: Use Node.js 18
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: 18
|
||||
|
||||
- name: Install pnpm
|
||||
uses: pnpm/action-setup@v3
|
||||
with:
|
||||
version: 8
|
||||
run_install: false
|
||||
|
||||
- name: Get pnpm store directory
|
||||
shell: bash
|
||||
run: |
|
||||
echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV
|
||||
|
||||
- uses: actions/cache@v4
|
||||
name: Setup pnpm cache
|
||||
with:
|
||||
path: ${{ env.STORE_PATH }}
|
||||
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
|
||||
restore-keys: |
|
||||
${{ runner.os }}-pnpm-store-
|
||||
${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
|
||||
|
||||
- run: pnpm install
|
||||
- run: pnpm run build:plugins
|
||||
|
||||
tests-unit:
|
||||
runs-on: ubuntu-latest
|
||||
needs: core-build
|
||||
if: false # Disable until tests are updated for 3.0
|
||||
needs: build
|
||||
|
||||
steps:
|
||||
- name: Use Node.js 18
|
||||
@@ -140,6 +103,7 @@ jobs:
|
||||
|
||||
- name: Restore build
|
||||
uses: actions/cache@v4
|
||||
timeout-minutes: 10
|
||||
with:
|
||||
path: ./*
|
||||
key: ${{ github.sha }}-${{ github.run_number }}
|
||||
@@ -151,7 +115,7 @@ jobs:
|
||||
|
||||
tests-int:
|
||||
runs-on: ubuntu-latest
|
||||
needs: core-build
|
||||
needs: build
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
@@ -184,6 +148,7 @@ jobs:
|
||||
|
||||
- name: Restore build
|
||||
uses: actions/cache@v4
|
||||
timeout-minutes: 10
|
||||
with:
|
||||
path: ./*
|
||||
key: ${{ github.sha }}-${{ github.run_number }}
|
||||
@@ -242,7 +207,7 @@ jobs:
|
||||
|
||||
tests-e2e:
|
||||
runs-on: ubuntu-latest
|
||||
needs: core-build
|
||||
needs: build
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
@@ -255,8 +220,11 @@ jobs:
|
||||
- email
|
||||
- field-error-states
|
||||
- fields-relationship
|
||||
# - fields
|
||||
- fields/lexical
|
||||
- fields
|
||||
- fields__collections__Blocks
|
||||
- fields__collections__Array
|
||||
- fields__collections__Relationship
|
||||
- fields__collections__Lexical
|
||||
- live-preview
|
||||
- localization
|
||||
- plugin-form-builder
|
||||
@@ -279,6 +247,7 @@ jobs:
|
||||
|
||||
- name: Restore build
|
||||
uses: actions/cache@v4
|
||||
timeout-minutes: 10
|
||||
with:
|
||||
path: ./*
|
||||
key: ${{ github.sha }}-${{ github.run_number }}
|
||||
@@ -299,7 +268,7 @@ jobs:
|
||||
tests-type-generation:
|
||||
if: false # This should be replaced with gen on a real Payload project
|
||||
runs-on: ubuntu-latest
|
||||
needs: core-build
|
||||
needs: build
|
||||
|
||||
steps:
|
||||
- name: Use Node.js 18
|
||||
@@ -315,6 +284,7 @@ jobs:
|
||||
|
||||
- name: Restore build
|
||||
uses: actions/cache@v4
|
||||
timeout-minutes: 10
|
||||
with:
|
||||
path: ./*
|
||||
key: ${{ github.sha }}-${{ github.run_number }}
|
||||
|
||||
@@ -10,3 +10,5 @@
|
||||
**/temp
|
||||
**/docs/**
|
||||
tsconfig.json
|
||||
packages/payload/*.js
|
||||
packages/payload/*.d.ts
|
||||
|
||||
@@ -3,7 +3,7 @@ import type { Metadata } from 'next'
|
||||
|
||||
import config from '@payload-config'
|
||||
/* DO NOT MODIFY IT BECAUSE IT COULD BE REWRITTEN AT ANY TIME. */
|
||||
import { NotFoundPage, generatePageMetadata } from '@payloadcms/next/views/NotFound/index.js'
|
||||
import { NotFoundPage, generatePageMetadata } from '@payloadcms/next/views'
|
||||
|
||||
type Args = {
|
||||
params: {
|
||||
@@ -14,8 +14,8 @@ type Args = {
|
||||
}
|
||||
}
|
||||
|
||||
export const generateMetadata = ({ params }: Args): Promise<Metadata> =>
|
||||
generatePageMetadata({ config, params })
|
||||
export const generateMetadata = ({ params, searchParams }: Args): Promise<Metadata> =>
|
||||
generatePageMetadata({ config, params, searchParams })
|
||||
|
||||
const NotFound = ({ params, searchParams }: Args) => NotFoundPage({ config, params, searchParams })
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ import type { Metadata } from 'next'
|
||||
|
||||
import config from '@payload-config'
|
||||
/* DO NOT MODIFY IT BECAUSE IT COULD BE REWRITTEN AT ANY TIME. */
|
||||
import { RootPage, generatePageMetadata } from '@payloadcms/next/views/Root/index.js'
|
||||
import { RootPage, generatePageMetadata } from '@payloadcms/next/views'
|
||||
|
||||
type Args = {
|
||||
params: {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/* THIS FILE WAS GENERATED AUTOMATICALLY BY PAYLOAD. */
|
||||
/* DO NOT MODIFY it because it could be re-written at any time. */
|
||||
import config from '@payload-config'
|
||||
import { REST_DELETE, REST_GET, REST_PATCH, REST_POST } from '@payloadcms/next/routes/index.js'
|
||||
import { REST_DELETE, REST_GET, REST_PATCH, REST_POST } from '@payloadcms/next/routes'
|
||||
|
||||
export const GET = REST_GET(config)
|
||||
export const POST = REST_POST(config)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/* THIS FILE WAS GENERATED AUTOMATICALLY BY PAYLOAD. */
|
||||
/* DO NOT MODIFY it because it could be re-written at any time. */
|
||||
import config from '@payload-config'
|
||||
import { GRAPHQL_PLAYGROUND_GET } from '@payloadcms/next/routes/index.js'
|
||||
import { GRAPHQL_PLAYGROUND_GET } from '@payloadcms/next/routes'
|
||||
|
||||
export const GET = GRAPHQL_PLAYGROUND_GET(config)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/* THIS FILE WAS GENERATED AUTOMATICALLY BY PAYLOAD. */
|
||||
/* DO NOT MODIFY it because it could be re-written at any time. */
|
||||
import config from '@payload-config'
|
||||
import { GRAPHQL_POST } from '@payloadcms/next/routes/index.js'
|
||||
import { GRAPHQL_POST } from '@payloadcms/next/routes'
|
||||
|
||||
export const POST = GRAPHQL_POST(config)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/* THIS FILE WAS GENERATED AUTOMATICALLY BY PAYLOAD. */
|
||||
import configPromise from '@payload-config'
|
||||
import { RootLayout } from '@payloadcms/next/layouts/Root/index.js'
|
||||
import { RootLayout } from '@payloadcms/next/layouts'
|
||||
/* DO NOT MODIFY IT BECAUSE IT COULD BE REWRITTEN AT ANY TIME. */
|
||||
import React from 'react'
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "payload-monorepo",
|
||||
"version": "3.0.0-alpha.59",
|
||||
"version": "3.0.0-beta.1",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"workspaces:": [
|
||||
|
||||
@@ -10,6 +10,6 @@
|
||||
}
|
||||
},
|
||||
"module": {
|
||||
"type": "commonjs"
|
||||
"type": "es6"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,2 +1,4 @@
|
||||
#!/usr/bin/env node
|
||||
require('../dist/index.js')
|
||||
|
||||
import { main } from '../dist/index.js'
|
||||
main()
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "create-payload-app",
|
||||
"version": "3.0.0-alpha.54",
|
||||
"version": "3.0.0-beta.1",
|
||||
"license": "MIT",
|
||||
"type": "module",
|
||||
"homepage": "https://payloadcms.com",
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
import { Main } from './main.js'
|
||||
import { error } from './utils/log.js'
|
||||
|
||||
async function main(): Promise<void> {
|
||||
await new Main().init()
|
||||
export async function main(): Promise<void> {
|
||||
try {
|
||||
await new Main().init()
|
||||
} catch (e) {
|
||||
if (e instanceof Error) {
|
||||
error(e.message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
main().catch((e) => error(`An error has occurred: ${e instanceof Error ? e.message : e}`))
|
||||
|
||||
@@ -4,6 +4,7 @@ import * as p from '@clack/prompts'
|
||||
import { parse, stringify } from 'comment-json'
|
||||
import execa from 'execa'
|
||||
import fs from 'fs'
|
||||
import fse from 'fs-extra'
|
||||
import globby from 'globby'
|
||||
import path from 'path'
|
||||
import { promisify } from 'util'
|
||||
@@ -31,6 +32,8 @@ type InitNextArgs = Pick<CliArgs, '--debug'> & {
|
||||
useDistFiles?: boolean
|
||||
}
|
||||
|
||||
type NextConfigType = 'cjs' | 'esm'
|
||||
|
||||
type InitNextResult =
|
||||
| {
|
||||
isSrcDir: boolean
|
||||
@@ -45,11 +48,22 @@ export async function initNext(args: InitNextArgs): Promise<InitNextResult> {
|
||||
|
||||
const nextAppDetails = args.nextAppDetails || (await getNextAppDetails(projectDir))
|
||||
|
||||
const { hasTopLevelLayout, isSrcDir, nextAppDir } =
|
||||
nextAppDetails || (await getNextAppDetails(projectDir))
|
||||
if (!nextAppDetails.nextAppDir) {
|
||||
warning(`Could not find app directory in ${projectDir}, creating...`)
|
||||
const createdAppDir = path.resolve(projectDir, nextAppDetails.isSrcDir ? 'src/app' : 'app')
|
||||
fse.mkdirSync(createdAppDir, { recursive: true })
|
||||
nextAppDetails.nextAppDir = createdAppDir
|
||||
}
|
||||
|
||||
if (!nextAppDir) {
|
||||
return { isSrcDir, reason: `Could not find app directory in ${projectDir}`, success: false }
|
||||
const { hasTopLevelLayout, isSrcDir, nextAppDir, nextConfigType } = nextAppDetails
|
||||
|
||||
if (!nextConfigType) {
|
||||
return {
|
||||
isSrcDir,
|
||||
nextAppDir,
|
||||
reason: `Could not determine Next Config type in ${projectDir}. Possibly try renaming next.config.js to next.config.cjs or next.config.mjs.`,
|
||||
success: false,
|
||||
}
|
||||
}
|
||||
|
||||
if (hasTopLevelLayout) {
|
||||
@@ -69,6 +83,7 @@ export async function initNext(args: InitNextArgs): Promise<InitNextResult> {
|
||||
const configurationResult = installAndConfigurePayload({
|
||||
...args,
|
||||
nextAppDetails,
|
||||
nextConfigType,
|
||||
useDistFiles: true, // Requires running 'pnpm pack-template-files' in cpa
|
||||
})
|
||||
|
||||
@@ -96,6 +111,13 @@ export async function initNext(args: InitNextArgs): Promise<InitNextResult> {
|
||||
|
||||
async function addPayloadConfigToTsConfig(projectDir: string, isSrcDir: boolean) {
|
||||
const tsConfigPath = path.resolve(projectDir, 'tsconfig.json')
|
||||
|
||||
// Check if tsconfig.json exists
|
||||
if (!fs.existsSync(tsConfigPath)) {
|
||||
warning(`Could not find tsconfig.json to add @payload-config path.`)
|
||||
return
|
||||
}
|
||||
|
||||
const userTsConfigContent = await readFile(tsConfigPath, {
|
||||
encoding: 'utf8',
|
||||
})
|
||||
@@ -119,13 +141,18 @@ async function addPayloadConfigToTsConfig(projectDir: string, isSrcDir: boolean)
|
||||
}
|
||||
|
||||
function installAndConfigurePayload(
|
||||
args: InitNextArgs & { nextAppDetails: NextAppDetails; useDistFiles?: boolean },
|
||||
args: InitNextArgs & {
|
||||
nextAppDetails: NextAppDetails
|
||||
nextConfigType: NextConfigType
|
||||
useDistFiles?: boolean
|
||||
},
|
||||
):
|
||||
| { payloadConfigPath: string; success: true }
|
||||
| { payloadConfigPath?: string; reason: string; success: false } {
|
||||
const {
|
||||
'--debug': debug,
|
||||
nextAppDetails: { isSrcDir, nextAppDir, nextConfigPath } = {},
|
||||
nextConfigType,
|
||||
projectDir,
|
||||
useDistFiles,
|
||||
} = args
|
||||
@@ -172,6 +199,7 @@ function installAndConfigurePayload(
|
||||
logDebug(`nextAppDir: ${nextAppDir}`)
|
||||
logDebug(`projectDir: ${projectDir}`)
|
||||
logDebug(`nextConfigPath: ${nextConfigPath}`)
|
||||
logDebug(`payloadConfigPath: ${path.resolve(projectDir, 'payload.config.ts')}`)
|
||||
|
||||
logDebug(
|
||||
`isSrcDir: ${isSrcDir}. source: ${templateSrcDir}. dest: ${path.dirname(nextConfigPath)}`,
|
||||
@@ -181,7 +209,7 @@ function installAndConfigurePayload(
|
||||
copyRecursiveSync(templateSrcDir, path.dirname(nextConfigPath), debug)
|
||||
|
||||
// Wrap next.config.js with withPayload
|
||||
wrapNextConfig({ nextConfigPath })
|
||||
wrapNextConfig({ nextConfigPath, nextConfigType })
|
||||
|
||||
return {
|
||||
payloadConfigPath: path.resolve(nextAppDir, '../payload.config.ts'),
|
||||
@@ -191,10 +219,10 @@ function installAndConfigurePayload(
|
||||
|
||||
async function installDeps(projectDir: string, packageManager: PackageManager, dbType: DbType) {
|
||||
const packagesToInstall = ['payload', '@payloadcms/next', '@payloadcms/richtext-lexical'].map(
|
||||
(pkg) => `${pkg}@alpha`,
|
||||
(pkg) => `${pkg}@beta`,
|
||||
)
|
||||
|
||||
packagesToInstall.push(`@payloadcms/db-${dbType}@alpha`)
|
||||
packagesToInstall.push(`@payloadcms/db-${dbType}@beta`)
|
||||
|
||||
let exitCode = 0
|
||||
switch (packageManager) {
|
||||
@@ -226,6 +254,7 @@ type NextAppDetails = {
|
||||
isSrcDir: boolean
|
||||
nextAppDir?: string
|
||||
nextConfigPath?: string
|
||||
nextConfigType?: NextConfigType
|
||||
}
|
||||
|
||||
export async function getNextAppDetails(projectDir: string): Promise<NextAppDetails> {
|
||||
@@ -246,6 +275,7 @@ export async function getNextAppDetails(projectDir: string): Promise<NextAppDeta
|
||||
await globby(['**/app'], {
|
||||
absolute: true,
|
||||
cwd: projectDir,
|
||||
ignore: ['**/node_modules/**'],
|
||||
onlyDirectories: true,
|
||||
})
|
||||
)?.[0]
|
||||
@@ -254,9 +284,31 @@ export async function getNextAppDetails(projectDir: string): Promise<NextAppDeta
|
||||
nextAppDir = undefined
|
||||
}
|
||||
|
||||
const configType = await getProjectType(projectDir, nextConfigPath)
|
||||
|
||||
const hasTopLevelLayout = nextAppDir
|
||||
? fs.existsSync(path.resolve(nextAppDir, 'layout.tsx'))
|
||||
: false
|
||||
|
||||
return { hasTopLevelLayout, isSrcDir, nextAppDir, nextConfigPath }
|
||||
return { hasTopLevelLayout, isSrcDir, nextAppDir, nextConfigPath, nextConfigType: configType }
|
||||
}
|
||||
|
||||
async function getProjectType(projectDir: string, nextConfigPath: string): Promise<'cjs' | 'esm'> {
|
||||
if (nextConfigPath.endsWith('.mjs')) {
|
||||
return 'esm'
|
||||
}
|
||||
if (nextConfigPath.endsWith('.cjs')) {
|
||||
return 'cjs'
|
||||
}
|
||||
|
||||
const packageObj = await fse.readJson(path.resolve(projectDir, 'package.json'))
|
||||
const packageJsonType = packageObj.type
|
||||
if (packageJsonType === 'module') {
|
||||
return 'esm'
|
||||
}
|
||||
if (packageJsonType === 'commonjs') {
|
||||
return 'cjs'
|
||||
}
|
||||
|
||||
return 'cjs'
|
||||
}
|
||||
|
||||
@@ -1,61 +1,133 @@
|
||||
import { parseAndModifyConfigContent, withPayloadImportStatement } from './wrap-next-config.js'
|
||||
import { parseAndModifyConfigContent, withPayloadStatement } from './wrap-next-config.js'
|
||||
import * as p from '@clack/prompts'
|
||||
|
||||
const defaultNextConfig = `/** @type {import('next').NextConfig} */
|
||||
const esmConfigs = {
|
||||
defaultNextConfig: `/** @type {import('next').NextConfig} */
|
||||
const nextConfig = {};
|
||||
|
||||
export default nextConfig;
|
||||
`
|
||||
|
||||
const nextConfigWithFunc = `const nextConfig = {
|
||||
// Your Next.js config here
|
||||
}
|
||||
|
||||
export default someFunc(nextConfig)
|
||||
`
|
||||
const nextConfigWithFuncMultiline = `const nextConfig = {
|
||||
// Your Next.js config here
|
||||
}
|
||||
|
||||
`,
|
||||
nextConfigWithFunc: `const nextConfig = {};
|
||||
export default someFunc(nextConfig);
|
||||
`,
|
||||
nextConfigWithFuncMultiline: `const nextConfig = {};;
|
||||
export default someFunc(
|
||||
nextConfig
|
||||
)
|
||||
`
|
||||
|
||||
const nextConfigExportNamedDefault = `const nextConfig = {
|
||||
// Your Next.js config here
|
||||
);
|
||||
`,
|
||||
nextConfigExportNamedDefault: `const nextConfig = {};
|
||||
const wrapped = someFunc(asdf);
|
||||
export { wrapped as default };
|
||||
`,
|
||||
}
|
||||
|
||||
const cjsConfigs = {
|
||||
defaultNextConfig: `
|
||||
/** @type {import('next').NextConfig} */
|
||||
const nextConfig = {};
|
||||
module.exports = nextConfig;
|
||||
`,
|
||||
anonConfig: `module.exports = {};`,
|
||||
nextConfigWithFunc: `const nextConfig = {};
|
||||
module.exports = someFunc(nextConfig);
|
||||
`,
|
||||
nextConfigWithFuncMultiline: `const nextConfig = {};
|
||||
module.exports = someFunc(
|
||||
nextConfig
|
||||
);
|
||||
`,
|
||||
nextConfigExportNamedDefault: `const nextConfig = {};
|
||||
const wrapped = someFunc(asdf);
|
||||
module.exports = wrapped;
|
||||
`,
|
||||
}
|
||||
const wrapped = someFunc(asdf)
|
||||
export { wrapped as default }
|
||||
`
|
||||
|
||||
describe('parseAndInsertWithPayload', () => {
|
||||
it('should parse the default next config', () => {
|
||||
const { modifiedConfigContent } = parseAndModifyConfigContent(defaultNextConfig)
|
||||
expect(modifiedConfigContent).toContain(withPayloadImportStatement)
|
||||
expect(modifiedConfigContent).toContain('withPayload(nextConfig)')
|
||||
})
|
||||
it('should parse the config with a function', () => {
|
||||
const { modifiedConfigContent } = parseAndModifyConfigContent(nextConfigWithFunc)
|
||||
expect(modifiedConfigContent).toContain('withPayload(someFunc(nextConfig))')
|
||||
describe('esm', () => {
|
||||
const configType = 'esm'
|
||||
const importStatement = withPayloadStatement[configType]
|
||||
it('should parse the default next config', () => {
|
||||
const { modifiedConfigContent } = parseAndModifyConfigContent(
|
||||
esmConfigs.defaultNextConfig,
|
||||
configType,
|
||||
)
|
||||
expect(modifiedConfigContent).toContain(importStatement)
|
||||
expect(modifiedConfigContent).toContain('withPayload(nextConfig)')
|
||||
})
|
||||
it('should parse the config with a function', () => {
|
||||
const { modifiedConfigContent } = parseAndModifyConfigContent(
|
||||
esmConfigs.nextConfigWithFunc,
|
||||
configType,
|
||||
)
|
||||
expect(modifiedConfigContent).toContain('withPayload(someFunc(nextConfig))')
|
||||
})
|
||||
|
||||
it('should parse the config with a function on a new line', () => {
|
||||
const { modifiedConfigContent } = parseAndModifyConfigContent(
|
||||
esmConfigs.nextConfigWithFuncMultiline,
|
||||
configType,
|
||||
)
|
||||
expect(modifiedConfigContent).toContain(importStatement)
|
||||
expect(modifiedConfigContent).toMatch(/withPayload\(someFunc\(\n nextConfig\n\)\)/)
|
||||
})
|
||||
|
||||
// Unsupported: export { wrapped as default }
|
||||
it('should give warning with a named export as default', () => {
|
||||
const warnLogSpy = jest.spyOn(p.log, 'warn').mockImplementation(() => {})
|
||||
|
||||
const { modifiedConfigContent, success } = parseAndModifyConfigContent(
|
||||
esmConfigs.nextConfigExportNamedDefault,
|
||||
configType,
|
||||
)
|
||||
expect(modifiedConfigContent).toContain(importStatement)
|
||||
expect(success).toBe(false)
|
||||
|
||||
expect(warnLogSpy).toHaveBeenCalledWith(
|
||||
expect.stringContaining('Could not automatically wrap'),
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
it('should parse the config with a function on a new line', () => {
|
||||
const { modifiedConfigContent } = parseAndModifyConfigContent(nextConfigWithFuncMultiline)
|
||||
expect(modifiedConfigContent).toContain(withPayloadImportStatement)
|
||||
expect(modifiedConfigContent).toMatch(/withPayload\(someFunc\(\n nextConfig\n\)\)/)
|
||||
})
|
||||
|
||||
// Unsupported: export { wrapped as default }
|
||||
it('should give warning with a named export as default', () => {
|
||||
const warnLogSpy = jest.spyOn(p.log, 'warn').mockImplementation(() => {})
|
||||
|
||||
const { modifiedConfigContent, success } = parseAndModifyConfigContent(
|
||||
nextConfigExportNamedDefault,
|
||||
)
|
||||
expect(modifiedConfigContent).toContain(withPayloadImportStatement)
|
||||
expect(success).toBe(false)
|
||||
|
||||
expect(warnLogSpy).toHaveBeenCalledWith(expect.stringContaining('Could not automatically wrap'))
|
||||
describe('cjs', () => {
|
||||
const configType = 'cjs'
|
||||
const requireStatement = withPayloadStatement[configType]
|
||||
it('should parse the default next config', () => {
|
||||
const { modifiedConfigContent } = parseAndModifyConfigContent(
|
||||
cjsConfigs.defaultNextConfig,
|
||||
configType,
|
||||
)
|
||||
expect(modifiedConfigContent).toContain(requireStatement)
|
||||
expect(modifiedConfigContent).toContain('withPayload(nextConfig)')
|
||||
})
|
||||
it('should parse anonymous default config', () => {
|
||||
const { modifiedConfigContent } = parseAndModifyConfigContent(
|
||||
cjsConfigs.anonConfig,
|
||||
configType,
|
||||
)
|
||||
expect(modifiedConfigContent).toContain(requireStatement)
|
||||
expect(modifiedConfigContent).toContain('withPayload({})')
|
||||
})
|
||||
it('should parse the config with a function', () => {
|
||||
const { modifiedConfigContent } = parseAndModifyConfigContent(
|
||||
cjsConfigs.nextConfigWithFunc,
|
||||
configType,
|
||||
)
|
||||
expect(modifiedConfigContent).toContain('withPayload(someFunc(nextConfig))')
|
||||
})
|
||||
it('should parse the config with a function on a new line', () => {
|
||||
const { modifiedConfigContent } = parseAndModifyConfigContent(
|
||||
cjsConfigs.nextConfigWithFuncMultiline,
|
||||
configType,
|
||||
)
|
||||
expect(modifiedConfigContent).toContain(requireStatement)
|
||||
expect(modifiedConfigContent).toMatch(/withPayload\(someFunc\(\n nextConfig\n\)\)/)
|
||||
})
|
||||
it('should parse the config with a named export as default', () => {
|
||||
const { modifiedConfigContent } = parseAndModifyConfigContent(
|
||||
cjsConfigs.nextConfigExportNamedDefault,
|
||||
configType,
|
||||
)
|
||||
expect(modifiedConfigContent).toContain(requireStatement)
|
||||
expect(modifiedConfigContent).toContain('withPayload(wrapped)')
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -5,12 +5,23 @@ import fs from 'fs'
|
||||
import { warning } from '../utils/log.js'
|
||||
import { log } from '../utils/log.js'
|
||||
|
||||
export const withPayloadImportStatement = `import { withPayload } from '@payloadcms/next'\n`
|
||||
export const withPayloadStatement = {
|
||||
cjs: `const { withPayload } = require('@payloadcms/next/withPayload')\n`,
|
||||
esm: `import { withPayload } from '@payloadcms/next/withPayload'\n`,
|
||||
}
|
||||
|
||||
export const wrapNextConfig = (args: { nextConfigPath: string }) => {
|
||||
const { nextConfigPath } = args
|
||||
type NextConfigType = 'cjs' | 'esm'
|
||||
|
||||
export const wrapNextConfig = (args: {
|
||||
nextConfigPath: string
|
||||
nextConfigType: NextConfigType
|
||||
}) => {
|
||||
const { nextConfigPath, nextConfigType: configType } = args
|
||||
const configContent = fs.readFileSync(nextConfigPath, 'utf8')
|
||||
const { modifiedConfigContent: newConfig, success } = parseAndModifyConfigContent(configContent)
|
||||
const { modifiedConfigContent: newConfig, success } = parseAndModifyConfigContent(
|
||||
configContent,
|
||||
configType,
|
||||
)
|
||||
|
||||
if (!success) {
|
||||
return
|
||||
@@ -22,72 +33,110 @@ export const wrapNextConfig = (args: { nextConfigPath: string }) => {
|
||||
/**
|
||||
* Parses config content with AST and wraps it with withPayload function
|
||||
*/
|
||||
export function parseAndModifyConfigContent(content: string): {
|
||||
modifiedConfigContent: string
|
||||
success: boolean
|
||||
} {
|
||||
content = withPayloadImportStatement + content
|
||||
export function parseAndModifyConfigContent(
|
||||
content: string,
|
||||
configType: NextConfigType,
|
||||
): { modifiedConfigContent: string; success: boolean } {
|
||||
content = withPayloadStatement[configType] + content
|
||||
const ast = parseModule(content, { loc: true })
|
||||
const exportDefaultDeclaration = ast.body.find((p) => p.type === 'ExportDefaultDeclaration') as
|
||||
| Directive
|
||||
| undefined
|
||||
|
||||
const exportNamedDeclaration = ast.body.find((p) => p.type === 'ExportNamedDeclaration') as
|
||||
| ExportNamedDeclaration
|
||||
| undefined
|
||||
if (configType === 'esm') {
|
||||
const exportDefaultDeclaration = ast.body.find((p) => p.type === 'ExportDefaultDeclaration') as
|
||||
| Directive
|
||||
| undefined
|
||||
|
||||
if (!exportDefaultDeclaration && !exportNamedDeclaration) {
|
||||
throw new Error('Could not find ExportDefaultDeclaration in next.config.js')
|
||||
}
|
||||
const exportNamedDeclaration = ast.body.find((p) => p.type === 'ExportNamedDeclaration') as
|
||||
| ExportNamedDeclaration
|
||||
| undefined
|
||||
|
||||
if (exportDefaultDeclaration && exportDefaultDeclaration.declaration?.loc) {
|
||||
const modifiedConfigContent = insertBeforeAndAfter(
|
||||
content,
|
||||
exportDefaultDeclaration.declaration.loc,
|
||||
)
|
||||
return { modifiedConfigContent, success: true }
|
||||
} else if (exportNamedDeclaration) {
|
||||
const exportSpecifier = exportNamedDeclaration.specifiers.find(
|
||||
(s) =>
|
||||
s.type === 'ExportSpecifier' &&
|
||||
s.exported?.name === 'default' &&
|
||||
s.local?.type === 'Identifier' &&
|
||||
s.local?.name,
|
||||
)
|
||||
if (!exportDefaultDeclaration && !exportNamedDeclaration) {
|
||||
throw new Error('Could not find ExportDefaultDeclaration in next.config.js')
|
||||
}
|
||||
|
||||
if (exportSpecifier) {
|
||||
warning('Could not automatically wrap next.config.js with withPayload.')
|
||||
warning('Automatic wrapping of named exports as default not supported yet.')
|
||||
if (exportDefaultDeclaration && exportDefaultDeclaration.declaration?.loc) {
|
||||
const modifiedConfigContent = insertBeforeAndAfter(
|
||||
content,
|
||||
exportDefaultDeclaration.declaration.loc,
|
||||
configType,
|
||||
)
|
||||
return { modifiedConfigContent, success: true }
|
||||
} else if (exportNamedDeclaration) {
|
||||
const exportSpecifier = exportNamedDeclaration.specifiers.find(
|
||||
(s) =>
|
||||
s.type === 'ExportSpecifier' &&
|
||||
s.exported?.name === 'default' &&
|
||||
s.local?.type === 'Identifier' &&
|
||||
s.local?.name,
|
||||
)
|
||||
|
||||
warnUserWrapNotSuccessful()
|
||||
return {
|
||||
modifiedConfigContent: content,
|
||||
success: false,
|
||||
if (exportSpecifier) {
|
||||
warning('Could not automatically wrap next.config.js with withPayload.')
|
||||
warning('Automatic wrapping of named exports as default not supported yet.')
|
||||
|
||||
warnUserWrapNotSuccessful(configType)
|
||||
return {
|
||||
modifiedConfigContent: content,
|
||||
success: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
warning('Could not automatically wrap Next config with withPayload.')
|
||||
warnUserWrapNotSuccessful(configType)
|
||||
return {
|
||||
modifiedConfigContent: content,
|
||||
success: false,
|
||||
}
|
||||
} else if (configType === 'cjs') {
|
||||
// Find `module.exports = X`
|
||||
const moduleExports = ast.body.find(
|
||||
(p) =>
|
||||
p.type === 'ExpressionStatement' &&
|
||||
p.expression?.type === 'AssignmentExpression' &&
|
||||
p.expression.left?.type === 'MemberExpression' &&
|
||||
p.expression.left.object?.type === 'Identifier' &&
|
||||
p.expression.left.object.name === 'module' &&
|
||||
p.expression.left.property?.type === 'Identifier' &&
|
||||
p.expression.left.property.name === 'exports',
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
) as any
|
||||
|
||||
if (moduleExports && moduleExports.expression.right?.loc) {
|
||||
const modifiedConfigContent = insertBeforeAndAfter(
|
||||
content,
|
||||
moduleExports.expression.right.loc,
|
||||
configType,
|
||||
)
|
||||
return { modifiedConfigContent, success: true }
|
||||
}
|
||||
|
||||
return {
|
||||
modifiedConfigContent: content,
|
||||
success: false,
|
||||
}
|
||||
}
|
||||
|
||||
warning('Could not automatically wrap next.config.js with withPayload.')
|
||||
warnUserWrapNotSuccessful()
|
||||
warning('Could not automatically wrap Next config with withPayload.')
|
||||
warnUserWrapNotSuccessful(configType)
|
||||
return {
|
||||
modifiedConfigContent: content,
|
||||
success: false,
|
||||
}
|
||||
}
|
||||
|
||||
function warnUserWrapNotSuccessful() {
|
||||
function warnUserWrapNotSuccessful(configType: NextConfigType) {
|
||||
// Output directions for user to update next.config.js
|
||||
const withPayloadMessage = `
|
||||
|
||||
${chalk.bold(`Please manually wrap your existing next.config.js with the withPayload function. Here is an example:`)}
|
||||
|
||||
import withPayload from '@payloadcms/next/withPayload'
|
||||
${withPayloadStatement[configType]}
|
||||
|
||||
const nextConfig = {
|
||||
// Your Next.js config here
|
||||
}
|
||||
|
||||
export default withPayload(nextConfig)
|
||||
${configType === 'esm' ? 'export default withPayload(nextConfig)' : 'module.exports = withPayload(nextConfig)'}
|
||||
|
||||
`
|
||||
|
||||
@@ -125,7 +174,7 @@ type Loc = {
|
||||
start: { column: number; line: number }
|
||||
}
|
||||
|
||||
function insertBeforeAndAfter(content: string, loc: Loc) {
|
||||
function insertBeforeAndAfter(content: string, loc: Loc, configType: NextConfigType) {
|
||||
const { end, start } = loc
|
||||
const lines = content.split('\n')
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@payloadcms/db-mongodb",
|
||||
"version": "3.0.0-alpha.59",
|
||||
"version": "3.0.0-beta.1",
|
||||
"description": "The officially supported MongoDB database adapter for Payload",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
||||
@@ -1,18 +1,20 @@
|
||||
import { sanitizeConfig } from 'payload/config'
|
||||
import { SanitizedConfig, sanitizeConfig } from 'payload/config'
|
||||
import { Config } from 'payload/config'
|
||||
import { getLocalizedSortProperty } from './getLocalizedSortProperty.js'
|
||||
|
||||
const config = {
|
||||
const config = sanitizeConfig({
|
||||
localization: {
|
||||
locales: ['en', 'es'],
|
||||
defaultLocale: 'en',
|
||||
fallback: true,
|
||||
},
|
||||
} as Config
|
||||
} as Config) as SanitizedConfig
|
||||
|
||||
describe('get localized sort property', () => {
|
||||
it('passes through a non-localized sort property', () => {
|
||||
const result = getLocalizedSortProperty({
|
||||
segments: ['title'],
|
||||
config: sanitizeConfig(config),
|
||||
config,
|
||||
fields: [
|
||||
{
|
||||
name: 'title',
|
||||
@@ -28,7 +30,7 @@ describe('get localized sort property', () => {
|
||||
it('properly localizes an un-localized sort property', () => {
|
||||
const result = getLocalizedSortProperty({
|
||||
segments: ['title'],
|
||||
config: sanitizeConfig(config),
|
||||
config,
|
||||
fields: [
|
||||
{
|
||||
name: 'title',
|
||||
@@ -45,7 +47,7 @@ describe('get localized sort property', () => {
|
||||
it('keeps specifically asked-for localized sort properties', () => {
|
||||
const result = getLocalizedSortProperty({
|
||||
segments: ['title', 'es'],
|
||||
config: sanitizeConfig(config),
|
||||
config,
|
||||
fields: [
|
||||
{
|
||||
name: 'title',
|
||||
@@ -62,7 +64,7 @@ describe('get localized sort property', () => {
|
||||
it('properly localizes nested sort properties', () => {
|
||||
const result = getLocalizedSortProperty({
|
||||
segments: ['group', 'title'],
|
||||
config: sanitizeConfig(config),
|
||||
config,
|
||||
fields: [
|
||||
{
|
||||
name: 'group',
|
||||
@@ -85,7 +87,7 @@ describe('get localized sort property', () => {
|
||||
it('keeps requested locale with nested sort properties', () => {
|
||||
const result = getLocalizedSortProperty({
|
||||
segments: ['group', 'title', 'es'],
|
||||
config: sanitizeConfig(config),
|
||||
config,
|
||||
fields: [
|
||||
{
|
||||
name: 'group',
|
||||
@@ -108,7 +110,7 @@ describe('get localized sort property', () => {
|
||||
it('properly localizes field within row', () => {
|
||||
const result = getLocalizedSortProperty({
|
||||
segments: ['title'],
|
||||
config: sanitizeConfig(config),
|
||||
config,
|
||||
fields: [
|
||||
{
|
||||
type: 'row',
|
||||
@@ -130,7 +132,7 @@ describe('get localized sort property', () => {
|
||||
it('properly localizes field within named tab', () => {
|
||||
const result = getLocalizedSortProperty({
|
||||
segments: ['tab', 'title'],
|
||||
config: sanitizeConfig(config),
|
||||
config,
|
||||
fields: [
|
||||
{
|
||||
type: 'tabs',
|
||||
@@ -157,7 +159,7 @@ describe('get localized sort property', () => {
|
||||
it('properly localizes field within unnamed tab', () => {
|
||||
const result = getLocalizedSortProperty({
|
||||
segments: ['title'],
|
||||
config: sanitizeConfig(config),
|
||||
config,
|
||||
fields: [
|
||||
{
|
||||
type: 'tabs',
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@payloadcms/db-postgres",
|
||||
"version": "3.0.0-alpha.59",
|
||||
"version": "3.0.0-beta.1",
|
||||
"description": "The officially supported Postgres database adapter for Payload",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@payloadcms/graphql",
|
||||
"version": "3.0.0-alpha.59",
|
||||
"version": "3.0.0-beta.1",
|
||||
"main": "./src/index.ts",
|
||||
"types": "./src/index.d.ts",
|
||||
"type": "module",
|
||||
|
||||
15
packages/next/.swcrc-cjs
Normal file
15
packages/next/.swcrc-cjs
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/swcrc",
|
||||
"sourceMaps": true,
|
||||
"jsc": {
|
||||
"target": "esnext",
|
||||
"parser": {
|
||||
"syntax": "typescript",
|
||||
"tsx": true,
|
||||
"dts": true
|
||||
}
|
||||
},
|
||||
"module": {
|
||||
"type": "commonjs"
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@payloadcms/next",
|
||||
"version": "3.0.0-alpha.59",
|
||||
"version": "3.0.0-beta.1",
|
||||
"main": "./src/index.js",
|
||||
"types": "./src/index.js",
|
||||
"type": "module",
|
||||
@@ -11,7 +11,8 @@
|
||||
"directory": "packages/next"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "pnpm copyfiles && pnpm build:swc && pnpm build:types && pnpm build:webpack && rm dist/prod/index.js",
|
||||
"build:cjs": "swc ./src/withPayload.js -o ./dist/cjs/withPayload.cjs --config-file .swcrc-cjs",
|
||||
"build": "pnpm copyfiles && pnpm build:swc && pnpm build:cjs && pnpm build:types && pnpm build:webpack && rm dist/prod/index.js",
|
||||
"build:swc": "swc ./src -d ./dist --config-file .swcrc",
|
||||
"build:types": "tsc --emitDeclarationOnly --outDir dist",
|
||||
"build:webpack": "webpack --config webpack.config.js",
|
||||
@@ -27,6 +28,10 @@
|
||||
"require": "./src/index.js",
|
||||
"types": "./src/index.js"
|
||||
},
|
||||
"./withPayload": {
|
||||
"import": "./src/withPayload.js",
|
||||
"require": "./src/withPayload.js"
|
||||
},
|
||||
"./*": {
|
||||
"import": "./src/exports/*.ts",
|
||||
"require": "./src/exports/*.ts",
|
||||
@@ -84,9 +89,9 @@
|
||||
"require": "./dist/prod/styles.css",
|
||||
"default": "./dist/prod/styles.css"
|
||||
},
|
||||
".": {
|
||||
"import": "./dist/index.js",
|
||||
"require": "./dist/index.js"
|
||||
"./withPayload": {
|
||||
"import": "./dist/withPayload.js",
|
||||
"require": "./dist/cjs/withPayload.cjs"
|
||||
},
|
||||
"./*": {
|
||||
"import": "./dist/exports/*.js",
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
export { getNextI18n } from '../utilities/getNextI18n.js'
|
||||
export { getNextRequestI18n } from '../utilities/getNextRequestI18n.js'
|
||||
export { getPayloadHMR } from '../utilities/getPayloadHMR.js'
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
import type { AcceptedLanguages } from '@payloadcms/translations'
|
||||
import type { SanitizedConfig } from 'payload/types'
|
||||
|
||||
import { translations } from '@payloadcms/translations/client'
|
||||
import { rtlLanguages } from '@payloadcms/translations'
|
||||
import { initI18n } from '@payloadcms/translations'
|
||||
import { RootProvider } from '@payloadcms/ui/providers/Root'
|
||||
import '@payloadcms/ui/scss/app.scss'
|
||||
import { buildComponentMap } from '@payloadcms/ui/utilities/buildComponentMap'
|
||||
import { headers as getHeaders, cookies as nextCookies } from 'next/headers.js'
|
||||
import { parseCookies } from 'payload/auth'
|
||||
import { createClientConfig } from 'payload/config'
|
||||
import { deepMerge } from 'payload/utilities'
|
||||
import React from 'react'
|
||||
import 'react-toastify/dist/ReactToastify.css'
|
||||
|
||||
@@ -20,8 +21,6 @@ export const metadata = {
|
||||
title: 'Next.js',
|
||||
}
|
||||
|
||||
const rtlLanguages = ['ar', 'fa', 'ha', 'ku', 'ur', 'ps', 'dv', 'ks', 'khw', 'he', 'yi']
|
||||
|
||||
export const RootLayout = async ({
|
||||
children,
|
||||
config: configPromise,
|
||||
@@ -30,32 +29,42 @@ export const RootLayout = async ({
|
||||
config: Promise<SanitizedConfig>
|
||||
}) => {
|
||||
const config = await configPromise
|
||||
const clientConfig = await createClientConfig(config)
|
||||
|
||||
const headers = getHeaders()
|
||||
const cookies = parseCookies(headers)
|
||||
|
||||
const lang =
|
||||
getRequestLanguage({
|
||||
config,
|
||||
cookies,
|
||||
headers,
|
||||
}) ?? clientConfig.i18n.fallbackLanguage
|
||||
const languageCode = getRequestLanguage({
|
||||
config,
|
||||
cookies,
|
||||
headers,
|
||||
})
|
||||
|
||||
const dir = rtlLanguages.includes(lang) ? 'RTL' : 'LTR'
|
||||
const i18n = await initI18n({ config: config.i18n, context: 'client', language: languageCode })
|
||||
const clientConfig = await createClientConfig({ config, t: i18n.t })
|
||||
|
||||
const mergedTranslations = deepMerge(translations, clientConfig.i18n.translations)
|
||||
const dir = (rtlLanguages as unknown as AcceptedLanguages[]).includes(languageCode)
|
||||
? 'RTL'
|
||||
: 'LTR'
|
||||
|
||||
const languageOptions = Object.entries(translations || {}).map(([language, translations]) => ({
|
||||
label: translations.general.thisLanguage,
|
||||
value: language,
|
||||
}))
|
||||
const languageOptions = Object.entries(config.i18n.supportedLanguages || {}).reduce(
|
||||
(acc, [language, languageConfig]) => {
|
||||
if (Object.keys(config.i18n.supportedLanguages).includes(language)) {
|
||||
acc.push({
|
||||
label: languageConfig.translations.general.thisLanguage,
|
||||
value: language,
|
||||
})
|
||||
}
|
||||
|
||||
return acc
|
||||
},
|
||||
[],
|
||||
)
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/require-await
|
||||
async function switchLanguageServerAction(lang: string): Promise<void> {
|
||||
'use server'
|
||||
nextCookies().set({
|
||||
name: `${config.cookiePrefix || 'payload'}-lng'`,
|
||||
name: `${config.cookiePrefix || 'payload'}-lng`,
|
||||
path: '/',
|
||||
value: lang,
|
||||
})
|
||||
@@ -66,20 +75,22 @@ export const RootLayout = async ({
|
||||
DefaultListView,
|
||||
children,
|
||||
config,
|
||||
i18n,
|
||||
})
|
||||
|
||||
return (
|
||||
<html dir={dir} lang={lang}>
|
||||
<html dir={dir} lang={languageCode}>
|
||||
<body>
|
||||
<RootProvider
|
||||
componentMap={componentMap}
|
||||
config={clientConfig}
|
||||
dateFNSKey={i18n.dateFNSKey}
|
||||
fallbackLang={clientConfig.i18n.fallbackLanguage}
|
||||
lang={lang}
|
||||
languageCode={languageCode}
|
||||
languageOptions={languageOptions}
|
||||
// eslint-disable-next-line react/jsx-no-bind
|
||||
switchLanguageServerAction={switchLanguageServerAction}
|
||||
translations={mergedTranslations[lang]}
|
||||
translations={i18n.translations}
|
||||
>
|
||||
{wrappedChildren}
|
||||
</RootProvider>
|
||||
|
||||
@@ -1,11 +1,5 @@
|
||||
import type { BuildFormStateArgs } from '@payloadcms/ui/forms/buildStateFromSchema'
|
||||
import type {
|
||||
DocumentPreferences,
|
||||
Field,
|
||||
PayloadRequest,
|
||||
SanitizedConfig,
|
||||
TypeWithID,
|
||||
} from 'payload/types'
|
||||
import type { DocumentPreferences, Field, PayloadRequest, TypeWithID } from 'payload/types'
|
||||
|
||||
import { buildStateFromSchema } from '@payloadcms/ui/forms/buildStateFromSchema'
|
||||
import { reduceFieldsToValues } from '@payloadcms/ui/utilities/reduceFieldsToValues'
|
||||
@@ -22,12 +16,12 @@ if (!cached) {
|
||||
cached = global._payload_fieldSchemaMap = null
|
||||
}
|
||||
|
||||
export const getFieldSchemaMap = (config: SanitizedConfig): FieldSchemaMap => {
|
||||
export const getFieldSchemaMap = (req: PayloadRequest): FieldSchemaMap => {
|
||||
if (cached && process.env.NODE_ENV !== 'development') {
|
||||
return cached
|
||||
}
|
||||
|
||||
cached = buildFieldSchemaMap(config)
|
||||
cached = buildFieldSchemaMap(req)
|
||||
|
||||
return cached
|
||||
}
|
||||
@@ -65,7 +59,7 @@ export const buildFormState = async ({ req }: { req: PayloadRequest }) => {
|
||||
})
|
||||
}
|
||||
|
||||
const fieldSchemaMap = getFieldSchemaMap(req.payload.config)
|
||||
const fieldSchemaMap = getFieldSchemaMap(req)
|
||||
|
||||
const id = collectionSlug ? reqData.id : undefined
|
||||
const schemaPathSegments = schemaPath.split('.')
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
import type { SanitizedConfig } from 'payload/types'
|
||||
import type { PayloadRequest } from 'payload/types'
|
||||
|
||||
import type { FieldSchemaMap } from './types.js'
|
||||
|
||||
import { traverseFields } from './traverseFields.js'
|
||||
|
||||
export const buildFieldSchemaMap = (config: SanitizedConfig): FieldSchemaMap => {
|
||||
export const buildFieldSchemaMap = ({
|
||||
i18n,
|
||||
payload: { config },
|
||||
}: PayloadRequest): FieldSchemaMap => {
|
||||
const result: FieldSchemaMap = new Map()
|
||||
|
||||
const validRelationships = config.collections.map((c) => c.slug) || []
|
||||
@@ -13,6 +16,7 @@ export const buildFieldSchemaMap = (config: SanitizedConfig): FieldSchemaMap =>
|
||||
traverseFields({
|
||||
config,
|
||||
fields: collection.fields,
|
||||
i18n,
|
||||
schemaMap: result,
|
||||
schemaPath: collection.slug,
|
||||
validRelationships,
|
||||
@@ -23,6 +27,7 @@ export const buildFieldSchemaMap = (config: SanitizedConfig): FieldSchemaMap =>
|
||||
traverseFields({
|
||||
config,
|
||||
fields: global.fields,
|
||||
i18n,
|
||||
schemaMap: result,
|
||||
schemaPath: global.slug,
|
||||
validRelationships,
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import type { I18n } from '@payloadcms/translations'
|
||||
import type { Field, SanitizedConfig } from 'payload/types'
|
||||
|
||||
import { tabHasName } from 'payload/types'
|
||||
@@ -7,6 +8,7 @@ import type { FieldSchemaMap } from './types.js'
|
||||
type Args = {
|
||||
config: SanitizedConfig
|
||||
fields: Field[]
|
||||
i18n: I18n
|
||||
schemaMap: FieldSchemaMap
|
||||
schemaPath: string
|
||||
validRelationships: string[]
|
||||
@@ -15,6 +17,7 @@ type Args = {
|
||||
export const traverseFields = ({
|
||||
config,
|
||||
fields,
|
||||
i18n,
|
||||
schemaMap,
|
||||
schemaPath,
|
||||
validRelationships,
|
||||
@@ -28,6 +31,7 @@ export const traverseFields = ({
|
||||
traverseFields({
|
||||
config,
|
||||
fields: field.fields,
|
||||
i18n,
|
||||
schemaMap,
|
||||
schemaPath: `${schemaPath}.${field.name}`,
|
||||
validRelationships,
|
||||
@@ -39,6 +43,7 @@ export const traverseFields = ({
|
||||
traverseFields({
|
||||
config,
|
||||
fields: field.fields,
|
||||
i18n,
|
||||
schemaMap,
|
||||
schemaPath,
|
||||
validRelationships,
|
||||
@@ -54,6 +59,7 @@ export const traverseFields = ({
|
||||
traverseFields({
|
||||
config,
|
||||
fields: block.fields,
|
||||
i18n,
|
||||
schemaMap,
|
||||
schemaPath: blockSchemaPath,
|
||||
validRelationships,
|
||||
@@ -65,6 +71,7 @@ export const traverseFields = ({
|
||||
if (typeof field.editor.generateSchemaMap === 'function') {
|
||||
field.editor.generateSchemaMap({
|
||||
config,
|
||||
i18n,
|
||||
schemaMap,
|
||||
schemaPath: `${schemaPath}.${field.name}`,
|
||||
})
|
||||
@@ -83,6 +90,7 @@ export const traverseFields = ({
|
||||
traverseFields({
|
||||
config,
|
||||
fields: tab.fields,
|
||||
i18n,
|
||||
schemaMap,
|
||||
schemaPath: tabSchemaPath,
|
||||
validRelationships,
|
||||
|
||||
@@ -6,7 +6,6 @@ import type {
|
||||
} from 'payload/types'
|
||||
|
||||
import { initI18n } from '@payloadcms/translations'
|
||||
import { translations } from '@payloadcms/translations/api'
|
||||
import { executeAuthStrategies } from 'payload/auth'
|
||||
import { parseCookies } from 'payload/auth'
|
||||
import { getDataLoader } from 'payload/utilities'
|
||||
@@ -72,11 +71,10 @@ export const createPayloadRequest = async ({
|
||||
headers: request.headers,
|
||||
})
|
||||
|
||||
const i18n = initI18n({
|
||||
const i18n = await initI18n({
|
||||
config: config.i18n,
|
||||
context: 'api',
|
||||
language,
|
||||
translations,
|
||||
})
|
||||
|
||||
const customRequest: CustomPayloadRequest = {
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
import type { I18n } from '@payloadcms/translations'
|
||||
import type { SanitizedConfig } from 'payload/types'
|
||||
|
||||
import { initI18n } from '@payloadcms/translations'
|
||||
import { translations } from '@payloadcms/translations/client'
|
||||
import { cookies, headers } from 'next/headers.js'
|
||||
|
||||
import { getRequestLanguage } from './getRequestLanguage.js'
|
||||
|
||||
export const getNextI18n = ({
|
||||
config,
|
||||
language,
|
||||
}: {
|
||||
config: SanitizedConfig
|
||||
language?: string
|
||||
}): I18n =>
|
||||
initI18n({
|
||||
config: config.i18n,
|
||||
context: 'client',
|
||||
language: language || getRequestLanguage({ config, cookies: cookies(), headers: headers() }),
|
||||
translations,
|
||||
})
|
||||
19
packages/next/src/utilities/getNextRequestI18n.ts
Normal file
19
packages/next/src/utilities/getNextRequestI18n.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import type { I18n } from '@payloadcms/translations'
|
||||
import type { SanitizedConfig } from 'payload/types'
|
||||
|
||||
import { initI18n } from '@payloadcms/translations'
|
||||
import { cookies, headers } from 'next/headers.js'
|
||||
|
||||
import { getRequestLanguage } from './getRequestLanguage.js'
|
||||
|
||||
/**
|
||||
* In the context of NextJS, this function initializes the i18n object for the current request.
|
||||
*
|
||||
* It must be called on the server side, and within the lifecycle of a request since it relies on the request headers and cookies.
|
||||
*/
|
||||
export const getNextRequestI18n = async ({ config }: { config: SanitizedConfig }): Promise<I18n> =>
|
||||
initI18n({
|
||||
config: config.i18n,
|
||||
context: 'client',
|
||||
language: getRequestLanguage({ config, cookies: cookies(), headers: headers() }),
|
||||
})
|
||||
@@ -1,12 +1,13 @@
|
||||
import type { AcceptedLanguages } from '@payloadcms/translations'
|
||||
import type { ReadonlyRequestCookies } from 'next/dist/server/web/spec-extension/adapters/request-cookies.js'
|
||||
import type { SanitizedConfig } from 'payload/config'
|
||||
|
||||
import { matchLanguage } from '@payloadcms/translations'
|
||||
import { extractHeaderLanguage } from '@payloadcms/translations'
|
||||
|
||||
type GetRequestLanguageArgs = {
|
||||
config: SanitizedConfig
|
||||
cookies: Map<string, string> | ReadonlyRequestCookies
|
||||
defaultLanguage?: string
|
||||
defaultLanguage?: AcceptedLanguages
|
||||
headers: Request['headers']
|
||||
}
|
||||
|
||||
@@ -15,14 +16,23 @@ export const getRequestLanguage = ({
|
||||
cookies,
|
||||
defaultLanguage = 'en',
|
||||
headers,
|
||||
}: GetRequestLanguageArgs): string => {
|
||||
const acceptLanguage = headers.get('Accept-Language')
|
||||
const cookieLanguage = cookies.get(`${config.cookiePrefix || 'payload'}-lng'`)
|
||||
}: GetRequestLanguageArgs): AcceptedLanguages => {
|
||||
const langCookie = cookies.get(`${config.cookiePrefix || 'payload'}-lng`)
|
||||
const languageFromCookie = typeof langCookie === 'string' ? langCookie : langCookie?.value
|
||||
const languageFromHeader = headers.get('Accept-Language')
|
||||
? extractHeaderLanguage(headers.get('Accept-Language'))
|
||||
: undefined
|
||||
const fallbackLang = config?.i18n?.fallbackLanguage || defaultLanguage
|
||||
|
||||
const reqLanguage =
|
||||
(typeof cookieLanguage === 'string' ? cookieLanguage : cookieLanguage?.value) ||
|
||||
acceptLanguage ||
|
||||
defaultLanguage
|
||||
const supportedLanguageKeys = Object.keys(config?.i18n?.supportedLanguages || {})
|
||||
|
||||
return matchLanguage(reqLanguage)
|
||||
if (languageFromCookie && supportedLanguageKeys.includes(languageFromCookie)) {
|
||||
return languageFromCookie as AcceptedLanguages
|
||||
}
|
||||
|
||||
if (languageFromHeader && supportedLanguageKeys.includes(languageFromHeader)) {
|
||||
return languageFromHeader
|
||||
}
|
||||
|
||||
return supportedLanguageKeys.includes(fallbackLang) ? (fallbackLang as AcceptedLanguages) : 'en'
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@ import type {
|
||||
} from 'payload/types'
|
||||
|
||||
import { initI18n } from '@payloadcms/translations'
|
||||
import { translations } from '@payloadcms/translations/client'
|
||||
import { findLocaleFromCode } from '@payloadcms/ui/utilities/findLocaleFromCode'
|
||||
import { headers as getHeaders } from 'next/headers.js'
|
||||
import { notFound, redirect } from 'next/navigation.js'
|
||||
@@ -45,14 +44,13 @@ export const initPage = async ({
|
||||
const cookies = parseCookies(headers)
|
||||
const language = getRequestLanguage({ config: payload.config, cookies, headers })
|
||||
|
||||
const i18n = initI18n({
|
||||
const i18n = await initI18n({
|
||||
config: payload.config.i18n,
|
||||
context: 'client',
|
||||
language,
|
||||
translations,
|
||||
})
|
||||
|
||||
const req = createLocalReq(
|
||||
const req = await createLocalReq(
|
||||
{
|
||||
fallbackLocale: null,
|
||||
locale: locale.code,
|
||||
|
||||
@@ -12,10 +12,11 @@ import './index.scss'
|
||||
|
||||
export { generateCreateFirstUserMetadata } from './meta.js'
|
||||
|
||||
export const CreateFirstUser: React.FC<AdminViewProps> = async ({ initPageResult }) => {
|
||||
export const CreateFirstUserView: React.FC<AdminViewProps> = async ({ initPageResult }) => {
|
||||
const {
|
||||
req,
|
||||
req: {
|
||||
i18n,
|
||||
payload: {
|
||||
config,
|
||||
config: {
|
||||
@@ -51,6 +52,7 @@ export const CreateFirstUser: React.FC<AdminViewProps> = async ({ initPageResult
|
||||
const createFirstUserFieldMap = mapFields({
|
||||
config,
|
||||
fieldSchema: fields,
|
||||
i18n,
|
||||
parentPath: userSlug,
|
||||
})
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ import type { SanitizedCollectionConfig, SanitizedGlobalConfig } from 'payload/t
|
||||
|
||||
import type { GenerateViewMetadata } from '../Root/index.js'
|
||||
|
||||
import { getNextI18n } from '../../utilities/getNextI18n.js'
|
||||
import { getNextRequestI18n } from '../../utilities/getNextRequestI18n.js'
|
||||
import { generateMetadata as apiMeta } from '../API/meta.js'
|
||||
import { generateMetadata as editMeta } from '../Edit/meta.js'
|
||||
import { generateMetadata as livePreviewMeta } from '../LivePreview/meta.js'
|
||||
@@ -89,7 +89,7 @@ export const getMetaBySegment: GenerateEditViewMetadata = async ({
|
||||
}
|
||||
}
|
||||
|
||||
const i18n = await getNextI18n({
|
||||
const i18n = await getNextRequestI18n({
|
||||
config,
|
||||
})
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ import type {
|
||||
import { APIView as DefaultAPIView } from '../API/index.js'
|
||||
import { EditView as DefaultEditView } from '../Edit/index.js'
|
||||
import { LivePreviewView as DefaultLivePreviewView } from '../LivePreview/index.js'
|
||||
import { Unauthorized } from '../Unauthorized/index.js'
|
||||
import { UnauthorizedView } from '../Unauthorized/index.js'
|
||||
import { VersionView as DefaultVersionView } from '../Version/index.js'
|
||||
import { VersionsView as DefaultVersionsView } from '../Versions/index.js'
|
||||
import { getCustomViewByKey } from './getCustomViewByKey.js'
|
||||
@@ -77,7 +77,7 @@ export const getViewsFromConfig = ({
|
||||
CustomView = getCustomViewByKey(views, 'Default')
|
||||
DefaultView = DefaultEditView
|
||||
} else {
|
||||
ErrorView = Unauthorized
|
||||
ErrorView = UnauthorizedView
|
||||
}
|
||||
break
|
||||
}
|
||||
@@ -87,7 +87,7 @@ export const getViewsFromConfig = ({
|
||||
CustomView = getCustomViewByKey(views, 'Default')
|
||||
DefaultView = DefaultEditView
|
||||
} else {
|
||||
ErrorView = Unauthorized
|
||||
ErrorView = UnauthorizedView
|
||||
}
|
||||
break
|
||||
}
|
||||
@@ -118,7 +118,7 @@ export const getViewsFromConfig = ({
|
||||
CustomView = getCustomViewByKey(views, 'Versions')
|
||||
DefaultView = DefaultVersionsView
|
||||
} else {
|
||||
ErrorView = Unauthorized
|
||||
ErrorView = UnauthorizedView
|
||||
}
|
||||
break
|
||||
}
|
||||
@@ -150,7 +150,7 @@ export const getViewsFromConfig = ({
|
||||
CustomView = getCustomViewByKey(views, 'Version')
|
||||
DefaultView = DefaultVersionView
|
||||
} else {
|
||||
ErrorView = Unauthorized
|
||||
ErrorView = UnauthorizedView
|
||||
}
|
||||
} else {
|
||||
const baseRoute = [adminRoute, collectionEntity, collectionSlug, segment3]
|
||||
@@ -191,7 +191,7 @@ export const getViewsFromConfig = ({
|
||||
CustomView = getCustomViewByKey(views, 'Default')
|
||||
DefaultView = DefaultEditView
|
||||
} else {
|
||||
ErrorView = Unauthorized
|
||||
ErrorView = UnauthorizedView
|
||||
}
|
||||
break
|
||||
}
|
||||
@@ -219,7 +219,7 @@ export const getViewsFromConfig = ({
|
||||
CustomView = getCustomViewByKey(views, 'Versions')
|
||||
DefaultView = DefaultVersionsView
|
||||
} else {
|
||||
ErrorView = Unauthorized
|
||||
ErrorView = UnauthorizedView
|
||||
}
|
||||
break
|
||||
}
|
||||
@@ -229,7 +229,7 @@ export const getViewsFromConfig = ({
|
||||
CustomView = getCustomViewByKey(views, 'Default')
|
||||
DefaultView = DefaultEditView
|
||||
} else {
|
||||
ErrorView = Unauthorized
|
||||
ErrorView = UnauthorizedView
|
||||
}
|
||||
break
|
||||
}
|
||||
@@ -244,7 +244,7 @@ export const getViewsFromConfig = ({
|
||||
CustomView = getCustomViewByKey(views, 'Version')
|
||||
DefaultView = DefaultVersionView
|
||||
} else {
|
||||
ErrorView = Unauthorized
|
||||
ErrorView = UnauthorizedView
|
||||
}
|
||||
} else {
|
||||
const baseRoute = [adminRoute, 'globals', globalSlug].filter(Boolean).join('/')
|
||||
|
||||
@@ -15,6 +15,7 @@ import React from 'react'
|
||||
|
||||
import type { GenerateEditViewMetadata } from './getMetaBySegment.js'
|
||||
|
||||
import { NotFoundView } from '../NotFound/index.js'
|
||||
import { getMetaBySegment } from './getMetaBySegment.js'
|
||||
import { getViewsFromConfig } from './getViewsFromConfig.js'
|
||||
|
||||
@@ -106,12 +107,8 @@ export const Document: React.FC<AdminViewProps> = async ({
|
||||
ErrorView = collectionViews?.ErrorView
|
||||
}
|
||||
|
||||
if (!CustomView && !DefaultView && !ViewOverride) {
|
||||
if (ErrorView) {
|
||||
return <ErrorView initPageResult={initPageResult} searchParams={searchParams} />
|
||||
}
|
||||
|
||||
notFound()
|
||||
if (!CustomView && !DefaultView && !ViewOverride && !ErrorView) {
|
||||
ErrorView = NotFoundView
|
||||
}
|
||||
}
|
||||
|
||||
@@ -143,12 +140,8 @@ export const Document: React.FC<AdminViewProps> = async ({
|
||||
DefaultView = globalViews?.DefaultView
|
||||
ErrorView = globalViews?.ErrorView
|
||||
|
||||
if (!CustomView && !DefaultView && !ViewOverride) {
|
||||
if (ErrorView) {
|
||||
return <ErrorView initPageResult={initPageResult} searchParams={searchParams} />
|
||||
}
|
||||
|
||||
notFound()
|
||||
if (!CustomView && !DefaultView && !ViewOverride && !ErrorView) {
|
||||
ErrorView = NotFoundView
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -219,11 +212,15 @@ export const Document: React.FC<AdminViewProps> = async ({
|
||||
uploadEdits: undefined,
|
||||
}}
|
||||
>
|
||||
<RenderCustomComponent
|
||||
CustomComponent={ViewOverride || CustomView}
|
||||
DefaultComponent={DefaultView}
|
||||
componentProps={viewComponentProps}
|
||||
/>
|
||||
{ErrorView ? (
|
||||
<ErrorView initPageResult={initPageResult} searchParams={searchParams} />
|
||||
) : (
|
||||
<RenderCustomComponent
|
||||
CustomComponent={ViewOverride || CustomView}
|
||||
DefaultComponent={DefaultView}
|
||||
componentProps={viewComponentProps}
|
||||
/>
|
||||
)}
|
||||
</FormQueryParamsProvider>
|
||||
</EditDepthProvider>
|
||||
</DocumentInfoProvider>
|
||||
|
||||
@@ -13,7 +13,7 @@ export { generateForgotPasswordMetadata } from './meta.js'
|
||||
const Link = (LinkImport.default || LinkImport) as unknown as typeof LinkImport.default
|
||||
export const forgotPasswordBaseClass = 'forgot-password'
|
||||
|
||||
export const ForgotPassword: React.FC<AdminViewProps> = ({ initPageResult }) => {
|
||||
export const ForgotPasswordView: React.FC<AdminViewProps> = ({ initPageResult }) => {
|
||||
const {
|
||||
req: {
|
||||
i18n,
|
||||
|
||||
@@ -13,7 +13,7 @@ import React, { Fragment } from 'react'
|
||||
|
||||
import type { DefaultListViewProps, ListPreferences } from './Default/types.js'
|
||||
|
||||
import { Unauthorized } from '../Unauthorized/index.js'
|
||||
import { UnauthorizedView } from '../Unauthorized/index.js'
|
||||
import { DefaultListView } from './Default/index.js'
|
||||
|
||||
export { generateListMetadata } from './meta.js'
|
||||
@@ -35,7 +35,7 @@ export const ListView: React.FC<AdminViewProps> = async ({ initPageResult, searc
|
||||
const collectionSlug = collectionConfig?.slug
|
||||
|
||||
if (!permissions?.collections?.[collectionSlug]?.read?.permission) {
|
||||
return <Unauthorized initPageResult={initPageResult} searchParams={searchParams} />
|
||||
return <UnauthorizedView initPageResult={initPageResult} searchParams={searchParams} />
|
||||
}
|
||||
|
||||
let listPreferences: ListPreferences
|
||||
@@ -117,7 +117,10 @@ export const ListView: React.FC<AdminViewProps> = async ({ initPageResult, searc
|
||||
<Fragment>
|
||||
<HydrateClientUser permissions={permissions} user={user} />
|
||||
<ListInfoProvider
|
||||
collectionConfig={createClientCollectionConfig(collectionConfig)}
|
||||
collectionConfig={createClientCollectionConfig({
|
||||
collection: collectionConfig,
|
||||
t: initPageResult.req.i18n.t,
|
||||
})}
|
||||
collectionSlug={collectionSlug}
|
||||
hasCreatePermission={permissions?.collections?.[collectionSlug]?.create?.permission}
|
||||
newDocumentURL={`${admin}/collections/${collectionSlug}/create`}
|
||||
|
||||
@@ -11,7 +11,7 @@ export { generateLoginMetadata } from './meta.js'
|
||||
|
||||
export const loginBaseClass = 'login'
|
||||
|
||||
export const Login: React.FC<AdminViewProps> = ({ initPageResult, searchParams }) => {
|
||||
export const LoginView: React.FC<AdminViewProps> = ({ initPageResult, searchParams }) => {
|
||||
const { req } = initPageResult
|
||||
|
||||
const {
|
||||
|
||||
@@ -10,7 +10,7 @@ const baseClass = 'logout'
|
||||
|
||||
export { generateLogoutMetadata } from './meta.js'
|
||||
|
||||
export const Logout: React.FC<
|
||||
export const LogoutView: React.FC<
|
||||
AdminViewProps & {
|
||||
inactivity?: boolean
|
||||
}
|
||||
@@ -39,5 +39,5 @@ export const Logout: React.FC<
|
||||
}
|
||||
|
||||
export const LogoutInactivity: React.FC<AdminViewProps> = (props) => {
|
||||
return <Logout inactivity {...props} />
|
||||
return <LogoutView inactivity {...props} />
|
||||
}
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import type { I18n } from '@payloadcms/translations'
|
||||
import type { Metadata } from 'next'
|
||||
import type { SanitizedConfig } from 'payload/types'
|
||||
import type { AdminViewComponent, SanitizedConfig } from 'payload/types'
|
||||
|
||||
import { getNextI18n } from '@payloadcms/next/utilities'
|
||||
import { HydrateClientUser } from '@payloadcms/ui/elements/HydrateClientUser'
|
||||
import { DefaultTemplate } from '@payloadcms/ui/templates/Default'
|
||||
import React, { Fragment } from 'react'
|
||||
|
||||
import { getNextRequestI18n } from '../../utilities/getNextRequestI18n.js'
|
||||
import { initPage } from '../../utilities/initPage.js'
|
||||
import { NotFoundClient } from './index.client.js'
|
||||
|
||||
@@ -19,7 +19,7 @@ export const generatePageMetadata = async ({
|
||||
}): Promise<Metadata> => {
|
||||
const config = await configPromise
|
||||
|
||||
const i18n = getNextI18n({
|
||||
const i18n = await getNextRequestI18n({
|
||||
config,
|
||||
})
|
||||
|
||||
@@ -65,3 +65,7 @@ export const NotFoundPage = async ({
|
||||
</Fragment>
|
||||
)
|
||||
}
|
||||
|
||||
export const NotFoundView: AdminViewComponent = () => {
|
||||
return <NotFoundClient marginTop="large" />
|
||||
}
|
||||
|
||||
@@ -4,15 +4,15 @@ import type { AdminViewComponent } from 'payload/types'
|
||||
import type { initPage } from '../../utilities/initPage.js'
|
||||
|
||||
import { Account } from '../Account/index.js'
|
||||
import { CreateFirstUser } from '../CreateFirstUser/index.js'
|
||||
import { CreateFirstUserView } from '../CreateFirstUser/index.js'
|
||||
import { Dashboard } from '../Dashboard/index.js'
|
||||
import { Document as DocumentView } from '../Document/index.js'
|
||||
import { ForgotPassword, forgotPasswordBaseClass } from '../ForgotPassword/index.js'
|
||||
import { ForgotPasswordView, forgotPasswordBaseClass } from '../ForgotPassword/index.js'
|
||||
import { ListView } from '../List/index.js'
|
||||
import { Login, loginBaseClass } from '../Login/index.js'
|
||||
import { Logout, LogoutInactivity } from '../Logout/index.js'
|
||||
import { LoginView, loginBaseClass } from '../Login/index.js'
|
||||
import { LogoutInactivity, LogoutView } from '../Logout/index.js'
|
||||
import { ResetPassword, resetPasswordBaseClass } from '../ResetPassword/index.js'
|
||||
import { Unauthorized } from '../Unauthorized/index.js'
|
||||
import { UnauthorizedView } from '../Unauthorized/index.js'
|
||||
import { Verify, verifyBaseClass } from '../Verify/index.js'
|
||||
import { getCustomViewByRoute } from './getCustomViewByRoute.js'
|
||||
|
||||
@@ -24,12 +24,12 @@ const baseClasses = {
|
||||
}
|
||||
|
||||
const oneSegmentViews = {
|
||||
'create-first-user': CreateFirstUser,
|
||||
forgot: ForgotPassword,
|
||||
login: Login,
|
||||
logout: Logout,
|
||||
'create-first-user': CreateFirstUserView,
|
||||
forgot: ForgotPasswordView,
|
||||
login: LoginView,
|
||||
logout: LogoutView,
|
||||
'logout-inactivity': LogoutInactivity,
|
||||
unauthorized: Unauthorized,
|
||||
unauthorized: UnauthorizedView,
|
||||
}
|
||||
|
||||
export const getViewFromConfig = ({
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import type { Metadata } from 'next'
|
||||
import type { SanitizedConfig } from 'payload/types'
|
||||
|
||||
import { getNextI18n } from '../../utilities/getNextI18n.js'
|
||||
import { getNextRequestI18n } from '../../utilities/getNextRequestI18n.js'
|
||||
import { generateAccountMetadata } from '../Account/index.js'
|
||||
import { generateCreateFirstUserMetadata } from '../CreateFirstUser/index.js'
|
||||
import { generateDashboardMetadata } from '../Dashboard/index.js'
|
||||
@@ -49,7 +49,7 @@ export const generatePageMetadata = async ({ config: configPromise, params }: Ar
|
||||
const isGlobal = segmentOne === 'globals'
|
||||
const isCollection = segmentOne === 'collections'
|
||||
|
||||
const i18n = getNextI18n({
|
||||
const i18n = await getNextRequestI18n({
|
||||
config,
|
||||
})
|
||||
|
||||
|
||||
36
packages/next/src/views/Unauthorized/index.scss
Normal file
36
packages/next/src/views/Unauthorized/index.scss
Normal file
@@ -0,0 +1,36 @@
|
||||
@import '../../scss/styles.scss';
|
||||
|
||||
.unauthorized {
|
||||
margin-top: var(--base);
|
||||
|
||||
& > * {
|
||||
&:first-child {
|
||||
margin-top: 0;
|
||||
}
|
||||
&:last-child {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
}
|
||||
|
||||
&__button {
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
&--margin-top-large {
|
||||
margin-top: calc(var(--base) * 2);
|
||||
}
|
||||
|
||||
@include large-break {
|
||||
&--margin-top-large {
|
||||
margin-top: var(--base);
|
||||
}
|
||||
}
|
||||
|
||||
@include small-break {
|
||||
margin-top: calc(var(--base) / 2);
|
||||
|
||||
&--margin-top-large {
|
||||
margin-top: calc(var(--base) / 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,11 +5,15 @@ import { Gutter } from '@payloadcms/ui/elements/Gutter'
|
||||
import LinkImport from 'next/link.js'
|
||||
import React from 'react'
|
||||
|
||||
import './index.scss'
|
||||
|
||||
const Link = (LinkImport.default || LinkImport) as unknown as typeof LinkImport.default
|
||||
|
||||
export { generateUnauthorizedMetadata } from './meta.js'
|
||||
|
||||
export const Unauthorized: AdminViewComponent = ({ initPageResult }) => {
|
||||
const baseClass = 'unauthorized'
|
||||
|
||||
export const UnauthorizedView: AdminViewComponent = ({ initPageResult }) => {
|
||||
const {
|
||||
req: {
|
||||
i18n,
|
||||
@@ -22,11 +26,10 @@ export const Unauthorized: AdminViewComponent = ({ initPageResult }) => {
|
||||
} = initPageResult
|
||||
|
||||
return (
|
||||
<Gutter className="unauthorized">
|
||||
<Gutter className={baseClass}>
|
||||
<h2>{i18n.t('error:unauthorized')}</h2>
|
||||
<p>{i18n.t('error:notAllowedToAccessPage')}</p>
|
||||
<br />
|
||||
<Button Link={Link} el="link" to={logoutRoute}>
|
||||
<Button Link={Link} className={`${baseClass}__button`} el="link" to={logoutRoute}>
|
||||
{i18n.t('authentication:logOut')}
|
||||
</Button>
|
||||
</Gutter>
|
||||
|
||||
@@ -85,7 +85,9 @@ export const SetStepNav: React.FC<{
|
||||
url: `${adminRoute}/collections/${collectionSlug}/${id}/versions`,
|
||||
},
|
||||
{
|
||||
label: doc?.createdAt ? formatDate(doc.createdAt, dateFormat, i18n?.language) : '',
|
||||
label: doc?.createdAt
|
||||
? formatDate({ date: doc.createdAt, i18n, pattern: dateFormat })
|
||||
: '',
|
||||
},
|
||||
]
|
||||
}
|
||||
@@ -101,7 +103,9 @@ export const SetStepNav: React.FC<{
|
||||
url: `${adminRoute}/globals/${globalConfig.slug}/versions`,
|
||||
},
|
||||
{
|
||||
label: doc?.createdAt ? formatDate(doc.createdAt, dateFormat, i18n?.language) : '',
|
||||
label: doc?.createdAt
|
||||
? formatDate({ date: doc.createdAt, i18n, pattern: dateFormat })
|
||||
: '',
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
@@ -61,7 +61,7 @@ export const DefaultVersionView: React.FC<DefaultVersionsViewProps> = ({
|
||||
} = config
|
||||
|
||||
const formattedCreatedAt = doc?.createdAt
|
||||
? formatDate(doc.createdAt, dateFormat, i18n.language)
|
||||
? formatDate({ date: doc.createdAt, i18n, pattern: dateFormat })
|
||||
: ''
|
||||
|
||||
const originalDocFetchURL = `${serverURL}${apiRoute}/${globalSlug ? 'globals/' : ''}${
|
||||
|
||||
@@ -88,7 +88,7 @@ export const SelectComparison: React.FC<Props> = (props) => {
|
||||
setOptions((existingOptions) => [
|
||||
...existingOptions,
|
||||
...data.docs.map((doc) => ({
|
||||
label: formatDate(doc.updatedAt, dateFormat, i18n.language),
|
||||
label: formatDate({ date: doc.updatedAt, i18n, pattern: dateFormat }),
|
||||
value: doc.id,
|
||||
})),
|
||||
])
|
||||
|
||||
@@ -22,7 +22,7 @@ export const generateMetadata: GenerateEditViewMetadata = async ({
|
||||
const doc: any = {} // TODO: figure this out
|
||||
|
||||
const formattedCreatedAt = doc?.createdAt
|
||||
? formatDate(doc.createdAt, config?.admin?.dateFormat, i18n?.language)
|
||||
? formatDate({ date: doc.createdAt, i18n, pattern: config?.admin?.dateFormat })
|
||||
: ''
|
||||
|
||||
if (collectionConfig) {
|
||||
|
||||
@@ -38,7 +38,8 @@ export const CreatedAtCell: React.FC<CreatedAtCellProps> = ({
|
||||
|
||||
return (
|
||||
<Link href={to}>
|
||||
{cellData && formatDate(cellData as Date | number | string, dateFormat, i18n.language)}
|
||||
{cellData &&
|
||||
formatDate({ date: cellData as Date | number | string, i18n, pattern: dateFormat })}
|
||||
</Link>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -101,7 +101,7 @@ export const VersionsView: EditViewComponent = async (props) => {
|
||||
return (
|
||||
<React.Fragment>
|
||||
<SetStepNav
|
||||
collectionSlug={collectionConfig?.slug || globalConfig?.slug}
|
||||
collectionSlug={collectionConfig?.slug}
|
||||
globalSlug={globalConfig?.slug}
|
||||
id={id}
|
||||
pluralLabel={collectionConfig?.labels?.plural || globalConfig?.label}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
export { RootLayout } from './layouts/Root/index.js'
|
||||
export { Dashboard as DashboardPage } from './views/Dashboard/index.js'
|
||||
export { Login } from './views/Login/index.js'
|
||||
export { LoginView } from './views/Login/index.js'
|
||||
export { RootPage } from './views/Root/index.js'
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
/** @type {import('next').NextConfig} */
|
||||
/**
|
||||
* @param {import('next').NextConfig} nextConfig
|
||||
*
|
||||
* @returns {import('next').NextConfig}
|
||||
* */
|
||||
const withPayload = (nextConfig = {}) => {
|
||||
return {
|
||||
...nextConfig,
|
||||
|
||||
13
packages/next/tsconfig.cjs.json
Normal file
13
packages/next/tsconfig.cjs.json
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"extends": "../../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"module": "CommonJS",
|
||||
"composite": true, // Required for references to work
|
||||
"noEmit": false /* Do not emit outputs. */,
|
||||
"emitDeclarationOnly": true,
|
||||
"outDir": "./dist/cjs" /* Specify an output folder for all emitted files. */,
|
||||
"rootDir": "./src" /* Specify the root folder within your source files. */,
|
||||
"sourceMap": true
|
||||
},
|
||||
"include": ["src/withPayload.js" /* Include the withPayload.js file in the build */]
|
||||
}
|
||||
5
packages/payload/.gitignore
vendored
5
packages/payload/.gitignore
vendored
@@ -20,3 +20,8 @@
|
||||
/versions.js
|
||||
/operations.js
|
||||
/operations.d.ts
|
||||
/node.js
|
||||
/node.d.ts
|
||||
/uploads.js
|
||||
/uploads.d.ts
|
||||
/i18n
|
||||
|
||||
5
packages/payload/node.d.ts
vendored
5
packages/payload/node.d.ts
vendored
@@ -1,5 +0,0 @@
|
||||
export {
|
||||
importConfig,
|
||||
importWithoutClientFiles,
|
||||
} from './dist/utilities/importWithoutClientFiles.js'
|
||||
//# sourceMappingURL=node.d.ts.map
|
||||
@@ -1,6 +0,0 @@
|
||||
export {
|
||||
importConfig,
|
||||
importWithoutClientFiles,
|
||||
} from './dist/utilities/importWithoutClientFiles.js'
|
||||
|
||||
//# sourceMappingURL=node.js.map
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "payload",
|
||||
"version": "3.0.0-alpha.59",
|
||||
"version": "3.0.0-beta.1",
|
||||
"description": "Node, React and MongoDB Headless CMS and Application Framework",
|
||||
"license": "MIT",
|
||||
"main": "./src/index.ts",
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import type { I18n } from '@payloadcms/translations'
|
||||
import type { JSONSchema4 } from 'json-schema'
|
||||
|
||||
import type { SanitizedConfig } from '../config/types.js'
|
||||
@@ -28,10 +29,12 @@ type RichTextAdapterBase<
|
||||
}) => Promise<void> | null
|
||||
generateComponentMap: (args: {
|
||||
config: SanitizedConfig
|
||||
i18n: I18n
|
||||
schemaPath: string
|
||||
}) => Map<string, React.ReactNode>
|
||||
generateSchemaMap?: (args: {
|
||||
config: SanitizedConfig
|
||||
i18n: I18n
|
||||
schemaMap: Map<string, Field[]>
|
||||
schemaPath: string
|
||||
}) => Map<string, Field[]>
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import type React from 'react'
|
||||
|
||||
export type DescriptionFunction = () => string
|
||||
import type { LabelFunction } from '../../config/types.js'
|
||||
|
||||
export type DescriptionFunction = LabelFunction
|
||||
|
||||
export type DescriptionComponent = React.ComponentType<FieldDescriptionProps>
|
||||
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import type { LabelFunction } from '../../config/types.js'
|
||||
|
||||
export type LabelProps = {
|
||||
CustomLabel?: React.ReactNode
|
||||
as?: 'label' | 'span'
|
||||
htmlFor?: string
|
||||
label?: Record<string, string> | false | string
|
||||
label?: LabelFunction | Record<string, string> | false | string
|
||||
required?: boolean
|
||||
unstyled?: boolean
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { Translations } from '@payloadcms/translations'
|
||||
import type { SupportedLanguages } from '@payloadcms/translations'
|
||||
|
||||
import type { Permissions } from '../../auth/index.js'
|
||||
import type { SanitizedCollectionConfig } from '../../collections/config/types.js'
|
||||
@@ -43,7 +43,7 @@ export type InitPageResult = {
|
||||
locale: Locale
|
||||
permissions: Permissions
|
||||
req: PayloadRequest
|
||||
translations: Translations
|
||||
translations: SupportedLanguages
|
||||
visibleEntities: VisibleEntities
|
||||
}
|
||||
|
||||
|
||||
@@ -2,15 +2,12 @@ import crypto from 'crypto'
|
||||
|
||||
import type { Field, FieldHook } from '../../fields/config/types.js'
|
||||
|
||||
import { extractTranslations } from '../../translations/extractTranslations.js'
|
||||
|
||||
const labels = extractTranslations(['authentication:enableAPIKey', 'authentication:apiKey'])
|
||||
|
||||
const encryptKey: FieldHook = ({ req, value }) =>
|
||||
value ? req.payload.encrypt(value as string) : null
|
||||
const decryptKey: FieldHook = ({ req, value }) =>
|
||||
value ? req.payload.decrypt(value as string) : undefined
|
||||
|
||||
// eslint-disable-next-line no-restricted-exports
|
||||
export default [
|
||||
{
|
||||
name: 'enableAPIKey',
|
||||
@@ -21,7 +18,7 @@ export default [
|
||||
},
|
||||
},
|
||||
defaultValue: false,
|
||||
label: labels['authentication:enableAPIKey'],
|
||||
label: ({ t }) => t('authentication:enableAPIKey'),
|
||||
},
|
||||
{
|
||||
name: 'apiKey',
|
||||
@@ -35,7 +32,7 @@ export default [
|
||||
afterRead: [decryptKey],
|
||||
beforeChange: [encryptKey],
|
||||
},
|
||||
label: labels['authentication:apiKey'],
|
||||
label: ({ t }) => t('authentication:apiKey'),
|
||||
},
|
||||
{
|
||||
name: 'apiKeyIndex',
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
import type { Field } from '../../fields/config/types.js'
|
||||
|
||||
import { email } from '../../fields/validations.js'
|
||||
import { extractTranslations } from '../../translations/extractTranslations.js'
|
||||
|
||||
const labels = extractTranslations(['general:email'])
|
||||
|
||||
const baseAuthFields: Field[] = [
|
||||
{
|
||||
@@ -14,7 +11,7 @@ const baseAuthFields: Field[] = [
|
||||
Field: () => null,
|
||||
},
|
||||
},
|
||||
label: labels['general:email'],
|
||||
label: ({ t }) => t('general:email'),
|
||||
required: true,
|
||||
unique: true,
|
||||
validate: email,
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
import type { Field, FieldHook } from '../../fields/config/types.js'
|
||||
|
||||
import { extractTranslations } from '../../translations/extractTranslations.js'
|
||||
|
||||
const labels = extractTranslations(['authentication:verified'])
|
||||
|
||||
const autoRemoveVerificationToken: FieldHook = ({ data, operation, originalDoc, value }) => {
|
||||
// If a user manually sets `_verified` to true,
|
||||
// and it was `false`, set _verificationToken to `null`.
|
||||
@@ -33,7 +29,7 @@ export default [
|
||||
Field: () => null,
|
||||
},
|
||||
},
|
||||
label: labels['authentication:verified'],
|
||||
label: ({ t }) => t('authentication:verified'),
|
||||
},
|
||||
{
|
||||
name: '_verificationToken',
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
import type { CollectionConfig } from '../collections/config/types.js'
|
||||
|
||||
import { extractTranslations } from '../translations/extractTranslations.js'
|
||||
|
||||
const labels = extractTranslations(['general:user', 'general:users'])
|
||||
|
||||
export const defaultUserCollection: CollectionConfig = {
|
||||
slug: 'users',
|
||||
admin: {
|
||||
@@ -14,7 +10,7 @@ export const defaultUserCollection: CollectionConfig = {
|
||||
},
|
||||
fields: [],
|
||||
labels: {
|
||||
plural: labels['general:users'],
|
||||
singular: labels['general:user'],
|
||||
plural: ({ t }) => t('general:users'),
|
||||
singular: ({ t }) => t('general:user'),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ import { getAccessResults } from '../getAccessResults.js'
|
||||
|
||||
export type AuthArgs = {
|
||||
headers: Request['headers']
|
||||
req: Omit<PayloadRequest, 'user'>
|
||||
req?: Omit<PayloadRequest, 'user'>
|
||||
}
|
||||
|
||||
export type AuthResult = {
|
||||
@@ -19,7 +19,7 @@ export type AuthResult = {
|
||||
user: User | null
|
||||
}
|
||||
|
||||
export const auth = async (args: AuthArgs): Promise<AuthResult> => {
|
||||
export const auth = async (args: Required<AuthArgs>): Promise<AuthResult> => {
|
||||
const { headers } = args
|
||||
const req = args.req as PayloadRequest
|
||||
const { payload } = req
|
||||
|
||||
@@ -10,6 +10,6 @@ export const auth = async (payload: Payload, options: AuthArgs): Promise<AuthRes
|
||||
|
||||
return await authOperation({
|
||||
headers,
|
||||
req: createLocalReq({ req: options.req as PayloadRequest }, payload),
|
||||
req: await createLocalReq({ req: options.req as PayloadRequest }, payload),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -78,7 +78,6 @@ export const resolve: ResolveFn = async (specifier, context, nextResolve) => {
|
||||
// and keep going
|
||||
let nextResult: ResolveResult
|
||||
|
||||
// First, try to
|
||||
if (!isTS) {
|
||||
try {
|
||||
nextResult = await nextResolve(specifier, context, nextResolve)
|
||||
|
||||
@@ -23,14 +23,22 @@ export type ClientCollectionConfig = Omit<
|
||||
fields: ClientFieldConfig[]
|
||||
}
|
||||
|
||||
import type { TFunction } from '@payloadcms/translations'
|
||||
|
||||
import type { ClientFieldConfig } from '../../fields/config/client.js'
|
||||
import type { SanitizedCollectionConfig } from './types.js'
|
||||
|
||||
import { createClientFieldConfigs } from '../../fields/config/client.js'
|
||||
|
||||
export const createClientCollectionConfig = (collection: SanitizedCollectionConfig) => {
|
||||
export const createClientCollectionConfig = ({
|
||||
collection,
|
||||
t,
|
||||
}: {
|
||||
collection: SanitizedCollectionConfig
|
||||
t: TFunction
|
||||
}) => {
|
||||
const sanitized = { ...collection }
|
||||
sanitized.fields = createClientFieldConfigs(sanitized.fields)
|
||||
sanitized.fields = createClientFieldConfigs({ fields: sanitized.fields, t })
|
||||
|
||||
const serverOnlyCollectionProperties: Partial<ServerOnlyCollectionProperties>[] = [
|
||||
'hooks',
|
||||
@@ -60,6 +68,14 @@ export const createClientCollectionConfig = (collection: SanitizedCollectionConf
|
||||
delete sanitized.auth.verify
|
||||
}
|
||||
|
||||
if (sanitized.labels) {
|
||||
Object.entries(sanitized.labels).forEach(([labelType, collectionLabel]) => {
|
||||
if (typeof collectionLabel === 'function') {
|
||||
sanitized.labels[labelType] = collectionLabel({ t })
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if ('admin' in sanitized) {
|
||||
sanitized.admin = { ...sanitized.admin }
|
||||
|
||||
@@ -85,7 +101,11 @@ export const createClientCollectionConfig = (collection: SanitizedCollectionConf
|
||||
return sanitized
|
||||
}
|
||||
|
||||
export const createClientCollectionConfigs = (
|
||||
collections: SanitizedCollectionConfig[],
|
||||
): ClientCollectionConfig[] =>
|
||||
collections.map((collection) => createClientCollectionConfig(collection))
|
||||
export const createClientCollectionConfigs = ({
|
||||
collections,
|
||||
t,
|
||||
}: {
|
||||
collections: SanitizedCollectionConfig[]
|
||||
t: TFunction
|
||||
}): ClientCollectionConfig[] =>
|
||||
collections.map((collection) => createClientCollectionConfig({ collection, t }))
|
||||
|
||||
@@ -11,15 +11,12 @@ import TimestampsRequired from '../../errors/TimestampsRequired.js'
|
||||
import { sanitizeFields } from '../../fields/config/sanitize.js'
|
||||
import { fieldAffectsData } from '../../fields/config/types.js'
|
||||
import mergeBaseFields from '../../fields/mergeBaseFields.js'
|
||||
import { extractTranslations } from '../../translations/extractTranslations.js'
|
||||
import { getBaseUploadFields } from '../../uploads/getBaseFields.js'
|
||||
import { formatLabels } from '../../utilities/formatLabels.js'
|
||||
import { isPlainObject } from '../../utilities/isPlainObject.js'
|
||||
import baseVersionFields from '../../versions/baseFields.js'
|
||||
import { authDefaults, defaults } from './defaults.js'
|
||||
|
||||
const translations = extractTranslations(['general:createdAt', 'general:updatedAt'])
|
||||
|
||||
const sanitizeCollection = (
|
||||
config: Config,
|
||||
collection: CollectionConfig,
|
||||
@@ -51,7 +48,7 @@ const sanitizeCollection = (
|
||||
disableBulkEdit: true,
|
||||
hidden: true,
|
||||
},
|
||||
label: translations['general:updatedAt'],
|
||||
label: ({ t }) => t('general:updatedAt'),
|
||||
})
|
||||
}
|
||||
if (!hasCreatedAt) {
|
||||
@@ -64,7 +61,7 @@ const sanitizeCollection = (
|
||||
// The default sort for list view is createdAt. Thus, enabling indexing by default, is a major performance improvement, especially for large or a large amount of collections.
|
||||
type: 'date',
|
||||
index: true,
|
||||
label: translations['general:createdAt'],
|
||||
label: ({ t }) => t('general:createdAt'),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -140,10 +140,10 @@ const collectionSchema = joi.object().keys({
|
||||
labels: joi.object({
|
||||
plural: joi
|
||||
.alternatives()
|
||||
.try(joi.string(), joi.object().pattern(joi.string(), [joi.string()])),
|
||||
.try(joi.func(), joi.string(), joi.object().pattern(joi.string(), [joi.string()])),
|
||||
singular: joi
|
||||
.alternatives()
|
||||
.try(joi.string(), joi.object().pattern(joi.string(), [joi.string()])),
|
||||
.try(joi.func(), joi.string(), joi.object().pattern(joi.string(), [joi.string()])),
|
||||
}),
|
||||
timestamps: joi.boolean(),
|
||||
typescript: joi.object().keys({
|
||||
|
||||
@@ -14,6 +14,7 @@ import type {
|
||||
Endpoint,
|
||||
EntityDescription,
|
||||
GeneratePreviewURL,
|
||||
LabelFunction,
|
||||
LivePreviewConfig,
|
||||
} from '../../config/types.js'
|
||||
import type { Field } from '../../fields/config/types.js'
|
||||
@@ -360,8 +361,8 @@ export type CollectionConfig = {
|
||||
* Label configuration
|
||||
*/
|
||||
labels?: {
|
||||
plural?: Record<string, string> | string
|
||||
singular?: Record<string, string> | string
|
||||
plural?: LabelFunction | Record<string, string> | string
|
||||
singular?: LabelFunction | Record<string, string> | string
|
||||
}
|
||||
slug: string
|
||||
/**
|
||||
|
||||
@@ -34,6 +34,7 @@ export type Options<TSlug extends keyof GeneratedTypes['collections']> = {
|
||||
user?: Document
|
||||
}
|
||||
|
||||
// eslint-disable-next-line no-restricted-exports
|
||||
export default async function createLocal<TSlug extends keyof GeneratedTypes['collections']>(
|
||||
payload: Payload,
|
||||
options: Options<TSlug>,
|
||||
@@ -58,7 +59,7 @@ export default async function createLocal<TSlug extends keyof GeneratedTypes['co
|
||||
)
|
||||
}
|
||||
|
||||
const req = createLocalReq(options, payload)
|
||||
const req = await createLocalReq(options, payload)
|
||||
req.file = file ?? (await getFileByPath(filePath))
|
||||
|
||||
return createOperation<TSlug>({
|
||||
|
||||
@@ -50,7 +50,7 @@ export async function duplicate<TSlug extends keyof GeneratedTypes['collections'
|
||||
)
|
||||
}
|
||||
|
||||
const req = createLocalReq(options, payload)
|
||||
const req = await createLocalReq(options, payload)
|
||||
|
||||
return duplicateOperation<TSlug>({
|
||||
id,
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import type { TFunction } from '@payloadcms/translations'
|
||||
|
||||
import type { ClientCollectionConfig } from '../collections/config/client.js'
|
||||
import type { SanitizedCollectionConfig } from '../collections/config/types.js'
|
||||
import type { ClientGlobalConfig } from '../globals/config/client.js'
|
||||
@@ -41,10 +43,13 @@ export type ClientConfig = Omit<
|
||||
globals: ClientGlobalConfig[]
|
||||
}
|
||||
|
||||
export const createClientConfig = async (
|
||||
configPromise: Promise<SanitizedConfig> | SanitizedConfig,
|
||||
): Promise<ClientConfig> => {
|
||||
const config = await configPromise
|
||||
export const createClientConfig = async ({
|
||||
config,
|
||||
t,
|
||||
}: {
|
||||
config: SanitizedConfig
|
||||
t: TFunction
|
||||
}): Promise<ClientConfig> => {
|
||||
const clientConfig: ClientConfig = { ...config }
|
||||
|
||||
const serverOnlyConfigProperties: Partial<ServerOnlyRootProperties>[] = [
|
||||
@@ -95,11 +100,15 @@ export const createClientConfig = async (
|
||||
}
|
||||
}
|
||||
|
||||
clientConfig.collections = createClientCollectionConfigs(
|
||||
clientConfig.collections as SanitizedCollectionConfig[],
|
||||
)
|
||||
clientConfig.collections = createClientCollectionConfigs({
|
||||
collections: clientConfig.collections as SanitizedCollectionConfig[],
|
||||
t,
|
||||
})
|
||||
|
||||
clientConfig.globals = createClientGlobalConfigs(clientConfig.globals as SanitizedGlobalConfig[])
|
||||
clientConfig.globals = createClientGlobalConfigs({
|
||||
globals: clientConfig.globals as SanitizedGlobalConfig[],
|
||||
t,
|
||||
})
|
||||
|
||||
return clientConfig
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { translations } from '@payloadcms/translations/api'
|
||||
import { en } from '@payloadcms/translations/languages/en'
|
||||
import merge from 'deepmerge'
|
||||
|
||||
import type {
|
||||
@@ -87,8 +87,10 @@ export const sanitizeConfig = (incomingConfig: Config): SanitizedConfig => {
|
||||
|
||||
config.i18n = {
|
||||
fallbackLanguage: 'en',
|
||||
supportedLanguages: Object.keys(translations),
|
||||
translations,
|
||||
supportedLanguages: {
|
||||
en,
|
||||
},
|
||||
translations: {},
|
||||
...(incomingConfig?.i18n ?? {}),
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { I18nOptions } from '@payloadcms/translations'
|
||||
import type { I18nOptions, TFunction } from '@payloadcms/translations'
|
||||
import type { Options as ExpressFileUploadOptions } from 'express-fileupload'
|
||||
import type GraphQL from 'graphql'
|
||||
import type { Transporter } from 'nodemailer'
|
||||
@@ -363,6 +363,8 @@ export type LocalizationConfig = Prettify<
|
||||
LocalizationConfigWithLabels | LocalizationConfigWithNoLabels
|
||||
>
|
||||
|
||||
export type LabelFunction = ({ t }: { t: TFunction }) => string
|
||||
|
||||
export type SharpDependency = (
|
||||
input?:
|
||||
| ArrayBuffer
|
||||
@@ -671,11 +673,12 @@ export type Config = {
|
||||
|
||||
export type SanitizedConfig = Omit<
|
||||
DeepRequired<Config>,
|
||||
'collections' | 'endpoint' | 'globals' | 'localization'
|
||||
'collections' | 'endpoint' | 'globals' | 'i18n' | 'localization'
|
||||
> & {
|
||||
collections: SanitizedCollectionConfig[]
|
||||
endpoints: Endpoint[]
|
||||
globals: SanitizedGlobalConfig[]
|
||||
i18n: Required<I18nOptions>
|
||||
localization: SanitizedLocalizationConfig | false
|
||||
paths: {
|
||||
config: string
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { TFunction } from '@payloadcms/translations'
|
||||
|
||||
import { translations } from '@payloadcms/translations/api'
|
||||
import { en } from '@payloadcms/translations/languages/en'
|
||||
import httpStatus from 'http-status'
|
||||
|
||||
import APIError from './APIError.js'
|
||||
@@ -8,7 +8,7 @@ import APIError from './APIError.js'
|
||||
class AuthenticationError extends APIError {
|
||||
constructor(t?: TFunction) {
|
||||
super(
|
||||
t ? t('error:emailOrPasswordIncorrect') : translations.en.error.emailOrPasswordIncorrect,
|
||||
t ? t('error:emailOrPasswordIncorrect') : en.translations.error.emailOrPasswordIncorrect,
|
||||
httpStatus.UNAUTHORIZED,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { TFunction } from '@payloadcms/translations'
|
||||
|
||||
import { translations } from '@payloadcms/translations/api'
|
||||
import { en } from '@payloadcms/translations/languages/en'
|
||||
import httpStatus from 'http-status'
|
||||
|
||||
import APIError from './APIError.js'
|
||||
@@ -8,7 +8,7 @@ import APIError from './APIError.js'
|
||||
class ErrorDeletingFile extends APIError {
|
||||
constructor(t?: TFunction) {
|
||||
super(
|
||||
t ? t('error:deletingFile') : translations.en.error.deletingFile,
|
||||
t ? t('error:deletingFile') : en.translations.error.deletingFile,
|
||||
httpStatus.INTERNAL_SERVER_ERROR,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { TFunction } from '@payloadcms/translations'
|
||||
|
||||
import { translations } from '@payloadcms/translations/api'
|
||||
import { en } from '@payloadcms/translations/languages/en'
|
||||
import httpStatus from 'http-status'
|
||||
|
||||
import APIError from './APIError.js'
|
||||
@@ -8,7 +8,7 @@ import APIError from './APIError.js'
|
||||
class FileUploadError extends APIError {
|
||||
constructor(t?: TFunction) {
|
||||
super(
|
||||
t ? t('error:problemUploadingFile') : translations.en.error.problemUploadingFile,
|
||||
t ? t('error:problemUploadingFile') : en.translations.error.problemUploadingFile,
|
||||
httpStatus.BAD_REQUEST,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { TFunction } from '@payloadcms/translations'
|
||||
|
||||
import { translations } from '@payloadcms/translations/api'
|
||||
import { en } from '@payloadcms/translations/languages/en'
|
||||
import httpStatus from 'http-status'
|
||||
|
||||
import APIError from './APIError.js'
|
||||
@@ -8,7 +8,7 @@ import APIError from './APIError.js'
|
||||
class Forbidden extends APIError {
|
||||
constructor(t?: TFunction) {
|
||||
super(
|
||||
t ? t('error:notAllowedToPerformAction') : translations.en.error.notAllowedToPerformAction,
|
||||
t ? t('error:notAllowedToPerformAction') : en.translations.error.notAllowedToPerformAction,
|
||||
httpStatus.FORBIDDEN,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
import type { TFunction } from '@payloadcms/translations'
|
||||
|
||||
import { translations } from '@payloadcms/translations/api'
|
||||
import { en } from '@payloadcms/translations/languages/en'
|
||||
import httpStatus from 'http-status'
|
||||
|
||||
import APIError from './APIError.js'
|
||||
|
||||
class LockedAuth extends APIError {
|
||||
constructor(t?: TFunction) {
|
||||
super(t ? t('error:userLocked') : translations.en.error.userLocked, httpStatus.UNAUTHORIZED)
|
||||
super(t ? t('error:userLocked') : en.translations.error.userLocked, httpStatus.UNAUTHORIZED)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { TFunction } from '@payloadcms/translations'
|
||||
|
||||
import { translations } from '@payloadcms/translations/api'
|
||||
import { en } from '@payloadcms/translations/languages/en'
|
||||
import httpStatus from 'http-status'
|
||||
|
||||
import APIError from './APIError.js'
|
||||
@@ -8,7 +8,7 @@ import APIError from './APIError.js'
|
||||
class MissingFile extends APIError {
|
||||
constructor(t?: TFunction) {
|
||||
super(
|
||||
t ? t('error:noFilesUploaded') : translations.en.error.noFilesUploaded,
|
||||
t ? t('error:noFilesUploaded') : en.translations.error.noFilesUploaded,
|
||||
httpStatus.BAD_REQUEST,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
import type { TFunction } from '@payloadcms/translations'
|
||||
|
||||
import { translations } from '@payloadcms/translations/api'
|
||||
import { en } from '@payloadcms/translations/languages/en'
|
||||
import httpStatus from 'http-status'
|
||||
|
||||
import APIError from './APIError.js'
|
||||
|
||||
class NotFound extends APIError {
|
||||
constructor(t?: TFunction) {
|
||||
super(t ? t('general:notFound') : translations.en.general.notFound, httpStatus.NOT_FOUND)
|
||||
super(t ? t('general:notFound') : en.translations.general.notFound, httpStatus.NOT_FOUND)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
import type { TFunction } from '@payloadcms/translations'
|
||||
|
||||
import { translations } from '@payloadcms/translations/api'
|
||||
import { en } from '@payloadcms/translations/languages/en'
|
||||
import httpStatus from 'http-status'
|
||||
|
||||
import APIError from './APIError.js'
|
||||
|
||||
class UnauthorizedError extends APIError {
|
||||
constructor(t?: TFunction) {
|
||||
super(t ? t('error:unauthorized') : translations.en.error.unauthorized, httpStatus.UNAUTHORIZED)
|
||||
super(t ? t('error:unauthorized') : en.translations.error.unauthorized, httpStatus.UNAUTHORIZED)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { TFunction } from '@payloadcms/translations'
|
||||
|
||||
import { translations } from '@payloadcms/translations/api'
|
||||
import { en } from '@payloadcms/translations/languages/en'
|
||||
import httpStatus from 'http-status'
|
||||
|
||||
import APIError from './APIError.js'
|
||||
@@ -10,8 +10,8 @@ class ValidationError extends APIError<{ field: string; message: string }[]> {
|
||||
const message = t
|
||||
? t('error:followingFieldsInvalid', { count: results.length })
|
||||
: results.length === 1
|
||||
? translations.en.error.followingFieldsInvalid_one
|
||||
: translations.en.error.followingFieldsInvalid_other
|
||||
? en.translations.error.followingFieldsInvalid_one
|
||||
: en.translations.error.followingFieldsInvalid_other
|
||||
|
||||
super(`${message} ${results.map((f) => f.field).join(', ')}`, httpStatus.BAD_REQUEST, results)
|
||||
}
|
||||
|
||||
1
packages/payload/src/exports/i18n/ar.ts
Normal file
1
packages/payload/src/exports/i18n/ar.ts
Normal file
@@ -0,0 +1 @@
|
||||
export { ar } from '@payloadcms/translations/languages/ar'
|
||||
1
packages/payload/src/exports/i18n/az.ts
Normal file
1
packages/payload/src/exports/i18n/az.ts
Normal file
@@ -0,0 +1 @@
|
||||
export { az } from '@payloadcms/translations/languages/az'
|
||||
1
packages/payload/src/exports/i18n/bg.ts
Normal file
1
packages/payload/src/exports/i18n/bg.ts
Normal file
@@ -0,0 +1 @@
|
||||
export { bg } from '@payloadcms/translations/languages/bg'
|
||||
1
packages/payload/src/exports/i18n/cs.ts
Normal file
1
packages/payload/src/exports/i18n/cs.ts
Normal file
@@ -0,0 +1 @@
|
||||
export { cs } from '@payloadcms/translations/languages/cs'
|
||||
1
packages/payload/src/exports/i18n/de.ts
Normal file
1
packages/payload/src/exports/i18n/de.ts
Normal file
@@ -0,0 +1 @@
|
||||
export { de } from '@payloadcms/translations/languages/de'
|
||||
1
packages/payload/src/exports/i18n/en.ts
Normal file
1
packages/payload/src/exports/i18n/en.ts
Normal file
@@ -0,0 +1 @@
|
||||
export { en } from '@payloadcms/translations/languages/en'
|
||||
1
packages/payload/src/exports/i18n/es.ts
Normal file
1
packages/payload/src/exports/i18n/es.ts
Normal file
@@ -0,0 +1 @@
|
||||
export { es } from '@payloadcms/translations/languages/es'
|
||||
1
packages/payload/src/exports/i18n/fa.ts
Normal file
1
packages/payload/src/exports/i18n/fa.ts
Normal file
@@ -0,0 +1 @@
|
||||
export { fa } from '@payloadcms/translations/languages/fa'
|
||||
1
packages/payload/src/exports/i18n/fr.ts
Normal file
1
packages/payload/src/exports/i18n/fr.ts
Normal file
@@ -0,0 +1 @@
|
||||
export { fr } from '@payloadcms/translations/languages/fr'
|
||||
1
packages/payload/src/exports/i18n/hr.ts
Normal file
1
packages/payload/src/exports/i18n/hr.ts
Normal file
@@ -0,0 +1 @@
|
||||
export { hr } from '@payloadcms/translations/languages/hr'
|
||||
1
packages/payload/src/exports/i18n/hu.ts
Normal file
1
packages/payload/src/exports/i18n/hu.ts
Normal file
@@ -0,0 +1 @@
|
||||
export { hu } from '@payloadcms/translations/languages/hu'
|
||||
1
packages/payload/src/exports/i18n/it.ts
Normal file
1
packages/payload/src/exports/i18n/it.ts
Normal file
@@ -0,0 +1 @@
|
||||
export { it } from '@payloadcms/translations/languages/it'
|
||||
1
packages/payload/src/exports/i18n/ja.ts
Normal file
1
packages/payload/src/exports/i18n/ja.ts
Normal file
@@ -0,0 +1 @@
|
||||
export { ja } from '@payloadcms/translations/languages/ja'
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user