Skip to content
This repository was archived by the owner on Dec 18, 2023. It is now read-only.

Commit 4cc300b

Browse files
committed
initial working draft
0 parents  commit 4cc300b

21 files changed

+19440
-0
lines changed

.editorconfig

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
charset = utf-8
7+
trim_trailing_whitespace = true
8+
insert_final_newline = true
9+
end_of_line = lf
10+
max_line_length = null

.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
node_modules
2+
.env
3+
dist
4+
demo/uploads
5+
build
6+
.DS_Store
7+
package-lock.json

README.md

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Payload Redirects Plugin
2+
3+
[![NPM](https://img.shields.io/npm/v/@payloadcms/plugin-redirects)](https://www.npmjs.com/package/@payloadcms/plugin-redirects)
4+
5+
A plugin for [Payload CMS](https://github.com/payloadcms/payload) to easily manage your redirects.
6+
7+
Core features:
8+
9+
- Adds a `redirects` collection to your config that:
10+
- includes a `from` and `to` fields
11+
- allows `to` to be a document reference
12+
13+
## Installation
14+
15+
```bash
16+
yarn add @payloadcms/plugin-redirects
17+
# OR
18+
npm i @payloadcms/plugin-redirects
19+
```
20+
21+
## Basic Usage
22+
23+
In the `plugins` array of your [Payload config](https://payloadcms.com/docs/configuration/overview), call the plugin with [options](#options):
24+
25+
```js
26+
import { buildConfig } from "payload/config";
27+
import redirects from "@payloadcms/plugin-redirects";
28+
29+
const config = buildConfig({
30+
collections: [
31+
{
32+
slug: "pages",
33+
fields: [],
34+
},
35+
],
36+
plugins: [
37+
redirects({
38+
collections: ["pages"],
39+
}),
40+
],
41+
});
42+
43+
export default config;
44+
```
45+
46+
### Options
47+
48+
- `collections` : string[] | optional
49+
50+
An array of collections slugs to populate in the `to` field of each redirect.
51+
52+
- `overrides` : object | optional
53+
54+
A partial collection config that allows you to override anything on the `redirects` collection.
55+
56+
## TypeScript
57+
58+
All types can be directly imported:
59+
60+
```js
61+
import { PluginConfig } from "@payloadcms/plugin-redirects/types";
62+
```
63+
64+
## Development
65+
66+
To actively develop or debug this plugin you can either work directly within the demo directory of this repo, or link your own project.
67+
68+
1. #### Internal Demo
69+
70+
This repo includes a fully working, self-seeding instance of Payload that installs the plugin directly from the source code. This is the easiest way to get started. To spin up this demo, follow these steps:
71+
72+
1. First clone the repo
73+
1. Then, `cd YOUR_PLUGIN_REPO && yarn && cd demo && yarn && yarn dev`
74+
1. Now open `http://localhost:3000/admin` in your browser
75+
1. Enter username `[email protected]` and password `test`
76+
77+
That's it! Changes made in `./src` will be reflected in your demo. Keep in mind that the demo database is automatically seeded on every startup, any changes you make to the data get destroyed each time you reboot the app.
78+
79+
1. #### Linked Project
80+
81+
You can alternatively link your own project to the source code:
82+
83+
1. First clone the repo
84+
1. Then, `cd YOUR_PLUGIN_REPO && yarn && cd demo && cp env.example .env && yarn && yarn dev`
85+
1. Now `cd` back into your own project and run, `yarn link @payloadcms/plugin-redirects`
86+
1. If this plugin using React in any way, continue to the next step. Otherwise skip to step 7.
87+
1. From your own project, `cd node_modules/react && yarn link && cd ../react-dom && yarn link && cd ../../`
88+
1. Then, `cd YOUR_PLUGIN_REPO && yarn link react react-dom`
89+
90+
All set! You can now boot up your own project as normal, and your local copy of the plugin source code will be used. Keep in mind that changes to the source code require a rebuild, `yarn build`.
91+
92+
## Screenshots

demo/.env.example

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
MONGODB_URI=mongodb://localhost/payload-plugin-redirects
2+
PAYLOAD_SECRET=kjnsaldkjfnasdljkfghbnseanljnuadlrigjandrg

demo/nodemon.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"ext": "ts",
3+
"exec": "ts-node src/server.ts"
4+
}

demo/package.json

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "payload-starter-typescript",
3+
"description": "Blank template - no collections",
4+
"version": "1.0.0",
5+
"main": "dist/server.js",
6+
"license": "MIT",
7+
"scripts": {
8+
"dev": "cross-env PAYLOAD_SEED=true PAYLOAD_DROP_DATABASE=true PAYLOAD_CONFIG_PATH=src/payload.config.ts nodemon",
9+
"build:payload": "cross-env PAYLOAD_CONFIG_PATH=src/payload.config.ts payload build",
10+
"build:server": "tsc",
11+
"build": "yarn build:payload && yarn build:server",
12+
"serve": "cross-env PAYLOAD_CONFIG_PATH=dist/payload.config.js NODE_ENV=production node dist/server.js",
13+
"generate:types": "cross-env PAYLOAD_CONFIG_PATH=src/payload.config.ts payload generate:types"
14+
},
15+
"dependencies": {
16+
"dotenv": "^8.2.0",
17+
"express": "^4.17.1",
18+
"payload": "^1.1.26"
19+
},
20+
"devDependencies": {
21+
"@types/express": "^4.17.9",
22+
"cross-env": "^7.0.3",
23+
"nodemon": "^2.0.6",
24+
"ts-node": "^9.1.1",
25+
"typescript": "^4.1.3"
26+
}
27+
}

demo/src/collections/Pages.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { CollectionConfig } from 'payload/types';
2+
3+
const Pages: CollectionConfig = {
4+
slug: 'pages',
5+
admin: {
6+
useAsTitle: 'title',
7+
},
8+
fields: [
9+
{
10+
name: 'title',
11+
type: 'text',
12+
required: true
13+
},
14+
{
15+
name: 'excerpt',
16+
type: 'text',
17+
},
18+
{
19+
name: 'slug',
20+
type: 'text',
21+
required: true,
22+
admin: {
23+
position: 'sidebar',
24+
}
25+
},
26+
],
27+
}
28+
29+
export default Pages;

demo/src/collections/Users.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { CollectionConfig } from 'payload/types';
2+
3+
const Users: CollectionConfig = {
4+
slug: 'users',
5+
auth: true,
6+
admin: {
7+
useAsTitle: 'email',
8+
},
9+
access: {
10+
read: () => true,
11+
},
12+
fields: [
13+
// Email added by default
14+
// Add more fields as needed
15+
],
16+
};
17+
18+
export default Users;

demo/src/payload.config.ts

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { buildConfig } from 'payload/config';
2+
import path from 'path';
3+
// import redirects from '../../dist';
4+
import redirects from '../../src'
5+
import Users from './collections/Users'
6+
import Pages from './collections/Pages'
7+
8+
export default buildConfig({
9+
serverURL: 'http://localhost:3000',
10+
admin: {
11+
user: Users.slug,
12+
webpack: (config) => {
13+
const newConfig = {
14+
...config,
15+
resolve: {
16+
...config.resolve,
17+
alias: {
18+
...config.resolve.alias,
19+
react: path.join(__dirname, "../node_modules/react"),
20+
"react-dom": path.join(__dirname, "../node_modules/react-dom"),
21+
"payload": path.join(__dirname, "../node_modules/payload"),
22+
},
23+
},
24+
};
25+
26+
return newConfig;
27+
},
28+
},
29+
collections: [
30+
Users,
31+
Pages
32+
],
33+
localization: {
34+
locales: [
35+
'en',
36+
'es'
37+
],
38+
defaultLocale: 'en',
39+
fallback: true,
40+
},
41+
plugins: [
42+
redirects({
43+
collections: ['pages'],
44+
}),
45+
],
46+
typescript: {
47+
outputFile: path.resolve(__dirname, 'payload-types.ts')
48+
},
49+
});

demo/src/seed/index.ts

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { Payload } from 'payload';
2+
3+
export const seed = async (payload: Payload) => {
4+
payload.logger.info('Seeding data...');
5+
6+
await payload.create({
7+
collection: 'users',
8+
data: {
9+
10+
password: 'test',
11+
}
12+
});
13+
14+
const { id: homePageID } = await payload.create({
15+
collection: 'pages',
16+
data: {
17+
title: 'Home Page',
18+
slug: 'home',
19+
excerpt: 'This is the home page',
20+
},
21+
})
22+
23+
await payload.create({
24+
collection: 'redirects',
25+
data: {
26+
from: 'https://payloadcms.com/old',
27+
to: {
28+
type: 'reference',
29+
reference: {
30+
relationTo: 'pages',
31+
value: homePageID
32+
}
33+
}
34+
},
35+
})
36+
37+
await payload.create({
38+
collection: 'redirects',
39+
data: {
40+
from: 'https://payloadcms.com/bad',
41+
to: {
42+
type: 'custom',
43+
url: 'https://payloadcms.com/good'
44+
}
45+
},
46+
})
47+
}

demo/src/server.ts

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import express from 'express';
2+
import payload from 'payload';
3+
import { seed } from './seed';
4+
5+
require('dotenv').config();
6+
const app = express();
7+
8+
// Redirect root to Admin panel
9+
app.get('/', (_, res) => {
10+
res.redirect('/admin');
11+
});
12+
13+
// Initialize Payload
14+
const start = async () => {
15+
await payload.initAsync({
16+
secret: process.env.PAYLOAD_SECRET,
17+
mongoURL: process.env.MONGODB_URI,
18+
express: app,
19+
onInit: () => {
20+
payload.logger.info(`Payload Admin URL: ${payload.getAdminURL()}`)
21+
},
22+
})
23+
24+
if (process.env.PAYLOAD_SEED === 'true') {
25+
await seed(payload);
26+
}
27+
28+
app.listen(3000);
29+
}
30+
31+
start();

demo/tsconfig.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
"lib": [
5+
"dom",
6+
"dom.iterable",
7+
"esnext"
8+
],
9+
"strict": false,
10+
"esModuleInterop": true,
11+
"skipLibCheck": true,
12+
"outDir": "./dist",
13+
"rootDir": "../",
14+
"jsx": "react",
15+
},
16+
"ts-node": {
17+
"transpileOnly": true
18+
}
19+
}

0 commit comments

Comments
 (0)