diff --git a/packages/plugin-redirects/demo/.env.example b/packages/plugin-redirects/demo/.env.example deleted file mode 100644 index 8c2caced5..000000000 --- a/packages/plugin-redirects/demo/.env.example +++ /dev/null @@ -1,2 +0,0 @@ -MONGODB_URI=mongodb://localhost/payload-plugin-redirects -PAYLOAD_SECRET=kjnsaldkjfnasdljkfghbnseanljnuadlrigjandrg diff --git a/packages/plugin-redirects/demo/nodemon.json b/packages/plugin-redirects/demo/nodemon.json deleted file mode 100644 index ed1a1850d..000000000 --- a/packages/plugin-redirects/demo/nodemon.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "ext": "ts", - "exec": "ts-node src/server.ts" -} diff --git a/packages/plugin-redirects/demo/package.json b/packages/plugin-redirects/demo/package.json deleted file mode 100644 index 2849036cc..000000000 --- a/packages/plugin-redirects/demo/package.json +++ /dev/null @@ -1,28 +0,0 @@ -{ - "name": "payload-starter-typescript", - "description": "Blank template - no collections", - "version": "1.0.0", - "main": "dist/server.js", - "license": "MIT", - "scripts": { - "dev": "cross-env PAYLOAD_SEED=true PAYLOAD_DROP_DATABASE=true PAYLOAD_CONFIG_PATH=src/payload.config.ts nodemon", - "build:payload": "cross-env PAYLOAD_CONFIG_PATH=src/payload.config.ts payload build", - "build:server": "tsc", - "build": "yarn build:payload && yarn build:server", - "serve": "cross-env PAYLOAD_CONFIG_PATH=dist/payload.config.js NODE_ENV=production ts-node server.js", - "generate:types": "cross-env PAYLOAD_CONFIG_PATH=src/payload.config.ts payload generate:types" - }, - "dependencies": { - "@swc/core": "^1.3.32", - "dotenv": "^8.2.0", - "express": "^4.17.1", - "payload": "^1.8.2" - }, - "devDependencies": { - "@types/express": "^4.17.9", - "cross-env": "^7.0.3", - "nodemon": "^2.0.6", - "ts-node": "^10.9.1", - "typescript": "^4.1.3" - } -} diff --git a/packages/plugin-redirects/demo/src/collections/Pages.ts b/packages/plugin-redirects/demo/src/collections/Pages.ts deleted file mode 100644 index 75a06e324..000000000 --- a/packages/plugin-redirects/demo/src/collections/Pages.ts +++ /dev/null @@ -1,29 +0,0 @@ -import type { CollectionConfig } from 'payload/types' - -const Pages: CollectionConfig = { - slug: 'pages', - admin: { - useAsTitle: 'title', - }, - fields: [ - { - name: 'title', - type: 'text', - required: true, - }, - { - name: 'excerpt', - type: 'text', - }, - { - name: 'slug', - type: 'text', - required: true, - admin: { - position: 'sidebar', - }, - }, - ], -} - -export default Pages diff --git a/packages/plugin-redirects/demo/src/payload.config.ts b/packages/plugin-redirects/demo/src/payload.config.ts deleted file mode 100644 index 417be050f..000000000 --- a/packages/plugin-redirects/demo/src/payload.config.ts +++ /dev/null @@ -1,45 +0,0 @@ -import path from 'path' -import { buildConfig } from 'payload/config' - -import redirects from '../../src' -import Pages from './collections/Pages' -// to build this demo you must import the plugin from dist -// import redirects from '../../dist'; -import Users from './collections/Users' - -export default buildConfig({ - serverURL: 'http://localhost:3000', - admin: { - user: Users.slug, - webpack: config => { - const newConfig = { - ...config, - resolve: { - ...config.resolve, - alias: { - ...config.resolve.alias, - react: path.join(__dirname, '../node_modules/react'), - 'react-dom': path.join(__dirname, '../node_modules/react-dom'), - payload: path.join(__dirname, '../node_modules/payload'), - }, - }, - } - - return newConfig - }, - }, - collections: [Users, Pages], - localization: { - locales: ['en', 'es'], - defaultLocale: 'en', - fallback: true, - }, - plugins: [ - redirects({ - collections: ['pages'], - }), - ], - typescript: { - outputFile: path.resolve(__dirname, 'payload-types.ts'), - }, -}) diff --git a/packages/plugin-redirects/demo/src/seed/index.ts b/packages/plugin-redirects/demo/src/seed/index.ts deleted file mode 100644 index edb4df70e..000000000 --- a/packages/plugin-redirects/demo/src/seed/index.ts +++ /dev/null @@ -1,47 +0,0 @@ -import type { Payload } from 'payload' - -export const seed = async (payload: Payload): Promise => { - payload.logger.info('Seeding data...') - - await payload.create({ - collection: 'users', - data: { - email: 'dev@payloadcms.com', - password: 'test', - }, - }) - - const { id: homePageID } = await payload.create({ - collection: 'pages', - data: { - title: 'Home Page', - slug: 'home', - excerpt: 'This is the home page', - }, - }) - - await payload.create({ - collection: 'redirects', - data: { - from: 'https://payloadcms.com/old', - to: { - type: 'reference', - reference: { - relationTo: 'pages', - value: homePageID, - }, - }, - }, - }) - - await payload.create({ - collection: 'redirects', - data: { - from: 'https://payloadcms.com/bad', - to: { - type: 'custom', - url: 'https://payloadcms.com/good', - }, - }, - }) -} diff --git a/packages/plugin-redirects/demo/src/server.ts b/packages/plugin-redirects/demo/src/server.ts deleted file mode 100644 index 6adb869e2..000000000 --- a/packages/plugin-redirects/demo/src/server.ts +++ /dev/null @@ -1,34 +0,0 @@ -import dotenv from 'dotenv' -dotenv.config() - -import express from 'express' -import payload from 'payload' - -import { seed } from './seed' - -const app = express() - -// Redirect root to Admin panel -app.get('/', (_, res) => { - res.redirect('/admin') -}) - -// Initialize Payload -const start = async (): Promise => { - await payload.init({ - secret: process.env.PAYLOAD_SECRET, - mongoURL: process.env.MONGODB_URI, - express: app, - onInit: () => { - payload.logger.info(`Payload Admin URL: ${payload.getAdminURL()}`) - }, - }) - - if (process.env.PAYLOAD_SEED === 'true') { - await seed(payload) - } - - app.listen(3000) -} - -start() diff --git a/packages/plugin-redirects/demo/tsconfig.json b/packages/plugin-redirects/demo/tsconfig.json deleted file mode 100644 index 7ccbfab78..000000000 --- a/packages/plugin-redirects/demo/tsconfig.json +++ /dev/null @@ -1,29 +0,0 @@ -{ - "compilerOptions": { - "target": "es2015", - "module": "commonjs", - "lib": [ - "dom", - "dom.iterable", - "esnext" - ], - "allowJs": true, - "strict": false, - "esModuleInterop": true, - "skipLibCheck": true, - "outDir": "./dist", - "rootDir": "../", - "jsx": "react" - }, - "include": [ - "src" - ], - "exclude": [ - "node_modules", - "dist", - "build", - ], - "ts-node": { - "swc": true - } -} diff --git a/test/plugin-redirects/collections/Pages.ts b/test/plugin-redirects/collections/Pages.ts new file mode 100644 index 000000000..d70712777 --- /dev/null +++ b/test/plugin-redirects/collections/Pages.ts @@ -0,0 +1,30 @@ +import type { CollectionConfig } from 'payload/dist/collections/config/types' + +import { pagesSlug } from '../shared' + +export const Pages: CollectionConfig = { + slug: pagesSlug, + labels: { + singular: 'Page', + plural: 'Pages', + }, + admin: { + useAsTitle: 'title', + }, + versions: { + drafts: true, + }, + fields: [ + { + name: 'title', + label: 'Title', + type: 'text', + required: true, + }, + { + name: 'excerpt', + label: 'Excerpt', + type: 'text', + }, + ], +} diff --git a/packages/plugin-redirects/demo/src/collections/Users.ts b/test/plugin-redirects/collections/Users.ts similarity index 61% rename from packages/plugin-redirects/demo/src/collections/Users.ts rename to test/plugin-redirects/collections/Users.ts index 3a635d139..b080611bd 100644 --- a/packages/plugin-redirects/demo/src/collections/Users.ts +++ b/test/plugin-redirects/collections/Users.ts @@ -1,6 +1,6 @@ -import type { CollectionConfig } from 'payload/types' +import type { CollectionConfig } from 'payload/dist/collections/config/types' -const Users: CollectionConfig = { +export const Users: CollectionConfig = { slug: 'users', auth: true, admin: { @@ -14,5 +14,3 @@ const Users: CollectionConfig = { // Add more fields as needed ], } - -export default Users diff --git a/test/plugin-redirects/config.ts b/test/plugin-redirects/config.ts new file mode 100644 index 000000000..5c3144670 --- /dev/null +++ b/test/plugin-redirects/config.ts @@ -0,0 +1,31 @@ +import redirectsPlugin from '../../packages/plugin-redirects/src' +import { buildConfigWithDefaults } from '../buildConfigWithDefaults' +import { devUser } from '../credentials' +import { Pages } from './collections/Pages' +import { Users } from './collections/Users' +import { seed } from './seed' + +export default buildConfigWithDefaults({ + collections: [Users, Pages], + localization: { + defaultLocale: 'en', + fallback: true, + locales: ['en', 'es', 'de'], + }, + onInit: async (payload) => { + await payload.create({ + collection: 'users', + data: { + email: devUser.email, + password: devUser.password, + }, + }) + + await seed(payload) + }, + plugins: [ + redirectsPlugin({ + collections: ['pages'], + }), + ], +}) diff --git a/test/plugin-redirects/int.spec.ts b/test/plugin-redirects/int.spec.ts new file mode 100644 index 000000000..de42b9062 --- /dev/null +++ b/test/plugin-redirects/int.spec.ts @@ -0,0 +1,65 @@ +import payload from '../../packages/payload/src' +import { initPayloadTest } from '../helpers/configHelpers' +import { pagesSlug } from './shared' + +describe('Redirects Plugin', () => { + let page: Page + + beforeAll(async () => { + await initPayloadTest({ __dirname, init: { local: true } }) + + page = await payload.create({ + collection: 'pages', + data: { + title: 'Test', + }, + }) + }) + + it('should add a redirects collection', async () => { + const redirect = await payload.find({ + collection: 'redirects', + depth: 0, + limit: 1, + }) + + expect(redirect).toBeTruthy() + }) + + it('should add a redirect with to internal page', async () => { + const redirect = await payload.create({ + collection: 'redirects', + data: { + from: '/test', + to: { + type: 'reference', + reference: { + relationTo: pagesSlug, + value: page.id, + }, + }, + }, + }) + + expect(redirect).toBeTruthy() + expect(redirect.from).toBe('/test') + expect(redirect.to.reference.value).toMatchObject(page) + }) + + it('should add a redirect with to custom url', async () => { + const redirect = await payload.create({ + collection: 'redirects', + data: { + from: '/test2', + to: { + type: 'custom', + url: '/test', + }, + }, + }) + + expect(redirect).toBeTruthy() + expect(redirect.from).toBe('/test2') + expect(redirect.to.url).toBe('/test') + }) +}) diff --git a/test/plugin-redirects/payload-types.ts b/test/plugin-redirects/payload-types.ts new file mode 100644 index 000000000..9c4350ab2 --- /dev/null +++ b/test/plugin-redirects/payload-types.ts @@ -0,0 +1,83 @@ +/* tslint:disable */ +/* eslint-disable */ +/** + * This file was automatically generated by Payload. + * DO NOT MODIFY IT BY HAND. Instead, modify your source Payload config, + * and re-run `payload generate:types` to regenerate this file. + */ + +export interface Config { + collections: { + users: User + pages: Page + redirects: Redirect + 'payload-preferences': PayloadPreference + 'payload-migrations': PayloadMigration + } + globals: {} +} +export interface User { + id: string + updatedAt: string + createdAt: string + email: string + resetPasswordToken?: string | null + resetPasswordExpiration?: string | null + salt?: string | null + hash?: string | null + loginAttempts?: number | null + lockUntil?: string | null + password: string | null +} +export interface Page { + id: string + title: string + excerpt?: string | null + updatedAt: string + createdAt: string + _status?: ('draft' | 'published') | null +} +export interface Redirect { + id: string + from: string + to?: { + type?: ('reference' | 'custom') | null + reference?: { + relationTo: 'pages' + value: string | Page + } | null + url?: string | null + } + updatedAt: string + createdAt: string +} +export interface PayloadPreference { + id: string + user: { + relationTo: 'users' + value: string | User + } + key?: string | null + value?: + | { + [k: string]: unknown + } + | unknown[] + | string + | number + | boolean + | null + updatedAt: string + createdAt: string +} +export interface PayloadMigration { + id: string + name?: string | null + batch?: number | null + updatedAt: string + createdAt: string +} + +declare module 'payload' { + export interface GeneratedTypes extends Config {} +} diff --git a/test/plugin-redirects/seed/index.ts b/test/plugin-redirects/seed/index.ts new file mode 100644 index 000000000..51ea8f0fc --- /dev/null +++ b/test/plugin-redirects/seed/index.ts @@ -0,0 +1,23 @@ +import type { Payload } from '../../../packages/payload/src' +import type { PayloadRequest } from '../../../packages/payload/types' + +export const seed = async (payload: Payload): Promise => { + payload.logger.info('Seeding data...') + const req = {} as PayloadRequest + + try { + await payload.create({ + collection: 'users', + data: { + email: 'demo@payloadcms.com', + password: 'demo', + }, + req, + }) + + return true + } catch (err) { + console.error(err) + return false + } +} diff --git a/test/plugin-redirects/shared.ts b/test/plugin-redirects/shared.ts new file mode 100644 index 000000000..748a8a017 --- /dev/null +++ b/test/plugin-redirects/shared.ts @@ -0,0 +1 @@ +export const pagesSlug = 'pages' diff --git a/test/plugin-search/collections/Posts.ts b/test/plugin-search/collections/Posts.ts index eb9f3d3ad..d5491f05f 100644 --- a/test/plugin-search/collections/Posts.ts +++ b/test/plugin-search/collections/Posts.ts @@ -1,6 +1,6 @@ import type { CollectionConfig } from '../../../packages/payload/src/collections/config/types' -import { postsSlug } from '../../_community/collections/Posts' +import { postsSlug } from '../shared' export const Posts: CollectionConfig = { slug: postsSlug, diff --git a/test/plugin-search/shared.ts b/test/plugin-search/shared.ts index 2d302ec98..3b4805dfc 100644 --- a/test/plugin-search/shared.ts +++ b/test/plugin-search/shared.ts @@ -1,3 +1,3 @@ export const pagesSlug = 'pages' -export const productsSlug = 'posts' +export const postsSlug = 'posts'