@payloadcms/plugin-redirects 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -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 `dev@payloadcms.com` 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
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Simple object check.
3
+ * @param item
4
+ * @returns {boolean}
5
+ */
6
+ export declare function isObject(item: unknown): boolean;
7
+ /**
8
+ * Deep merge two objects.
9
+ * @param target
10
+ * @param ...sources
11
+ */
12
+ export default function deepMerge<T, R>(target: T, source: R): T;
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ // @ts-nocheck
3
+ var __assign = (this && this.__assign) || function () {
4
+ __assign = Object.assign || function(t) {
5
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
6
+ s = arguments[i];
7
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
8
+ t[p] = s[p];
9
+ }
10
+ return t;
11
+ };
12
+ return __assign.apply(this, arguments);
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.isObject = void 0;
16
+ /**
17
+ * Simple object check.
18
+ * @param item
19
+ * @returns {boolean}
20
+ */
21
+ function isObject(item) {
22
+ return (item && typeof item === 'object' && !Array.isArray(item));
23
+ }
24
+ exports.isObject = isObject;
25
+ /**
26
+ * Deep merge two objects.
27
+ * @param target
28
+ * @param ...sources
29
+ */
30
+ function deepMerge(target, source) {
31
+ var output = __assign({}, target);
32
+ if (isObject(target) && isObject(source)) {
33
+ Object.keys(source).forEach(function (key) {
34
+ var _a, _b;
35
+ if (isObject(source[key])) {
36
+ if (!(key in target)) {
37
+ Object.assign(output, (_a = {}, _a[key] = source[key], _a));
38
+ }
39
+ else {
40
+ output[key] = deepMerge(target[key], source[key]);
41
+ }
42
+ }
43
+ else {
44
+ Object.assign(output, (_b = {}, _b[key] = source[key], _b));
45
+ }
46
+ });
47
+ }
48
+ return output;
49
+ }
50
+ exports.default = deepMerge;
51
+ //# sourceMappingURL=deepMerge.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deepMerge.js","sourceRoot":"","sources":["../src/deepMerge.ts"],"names":[],"mappings":";AAAA,cAAc;;;;;;;;;;;;;;AAEd;;;;GAIG;AACH,SAAgB,QAAQ,CAAC,IAAa;IACpC,OAAO,CAAC,IAAI,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC;AACpE,CAAC;AAFD,4BAEC;AAED;;;;GAIG;AACH,SAAwB,SAAS,CAAO,MAAS,EAAE,MAAS;IAC1D,IAAM,MAAM,gBAAQ,MAAM,CAAE,CAAC;IAC7B,IAAI,QAAQ,CAAC,MAAM,CAAC,IAAI,QAAQ,CAAC,MAAM,CAAC,EAAE;QACxC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,UAAC,GAAG;;YAC9B,IAAI,QAAQ,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,EAAE;gBACzB,IAAI,CAAC,CAAC,GAAG,IAAI,MAAM,CAAC,EAAE;oBACpB,MAAM,CAAC,MAAM,CAAC,MAAM,YAAI,GAAC,GAAG,IAAG,MAAM,CAAC,GAAG,CAAC,MAAG,CAAC;iBAC/C;qBAAM;oBACL,MAAM,CAAC,GAAG,CAAC,GAAG,SAAS,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC;iBACnD;aACF;iBAAM;gBACL,MAAM,CAAC,MAAM,CAAC,MAAM,YAAI,GAAC,GAAG,IAAG,MAAM,CAAC,GAAG,CAAC,MAAG,CAAC;aAC/C;QACH,CAAC,CAAC,CAAC;KACJ;IAED,OAAO,MAAM,CAAC;AAChB,CAAC;AAjBD,4BAiBC"}
@@ -0,0 +1,4 @@
1
+ import { Config } from 'payload/config';
2
+ import { PluginConfig } from './types';
3
+ declare const redirects: (pluginConfig: PluginConfig) => (incomingConfig: Config) => Config;
4
+ export default redirects;
package/dist/index.js ADDED
@@ -0,0 +1,97 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
14
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
15
+ if (ar || !(i in from)) {
16
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
17
+ ar[i] = from[i];
18
+ }
19
+ }
20
+ return to.concat(ar || Array.prototype.slice.call(from));
21
+ };
22
+ var __importDefault = (this && this.__importDefault) || function (mod) {
23
+ return (mod && mod.__esModule) ? mod : { "default": mod };
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ var deepMerge_1 = __importDefault(require("./deepMerge"));
27
+ var redirects = function (pluginConfig) { return function (incomingConfig) { return (__assign(__assign({}, incomingConfig), { collections: __spreadArray(__spreadArray([], (incomingConfig === null || incomingConfig === void 0 ? void 0 : incomingConfig.collections) || [], true), [
28
+ (0, deepMerge_1.default)({
29
+ slug: 'redirects',
30
+ admin: {
31
+ defaultColumns: [
32
+ 'from',
33
+ 'to.type',
34
+ 'createdAt',
35
+ ],
36
+ },
37
+ access: {
38
+ read: function () { return true; },
39
+ },
40
+ fields: [
41
+ {
42
+ name: 'from',
43
+ label: 'From URL',
44
+ index: true,
45
+ required: true,
46
+ type: 'text',
47
+ },
48
+ {
49
+ name: 'to',
50
+ label: false,
51
+ type: 'group',
52
+ fields: [
53
+ {
54
+ name: 'type',
55
+ label: 'To URL Type',
56
+ type: 'radio',
57
+ options: [
58
+ {
59
+ label: 'Internal link',
60
+ value: 'reference',
61
+ },
62
+ {
63
+ label: 'Custom URL',
64
+ value: 'custom',
65
+ },
66
+ ],
67
+ defaultValue: 'reference',
68
+ admin: {
69
+ layout: 'horizontal',
70
+ },
71
+ },
72
+ {
73
+ name: 'reference',
74
+ label: 'Document to redirect to',
75
+ type: 'relationship',
76
+ relationTo: (pluginConfig === null || pluginConfig === void 0 ? void 0 : pluginConfig.collections) || [],
77
+ required: true,
78
+ admin: {
79
+ condition: function (_, siblingData) { return (siblingData === null || siblingData === void 0 ? void 0 : siblingData.type) === 'reference'; },
80
+ },
81
+ },
82
+ {
83
+ name: 'url',
84
+ label: 'Custom URL',
85
+ type: 'text',
86
+ required: true,
87
+ admin: {
88
+ condition: function (_, siblingData) { return (siblingData === null || siblingData === void 0 ? void 0 : siblingData.type) === 'custom'; },
89
+ },
90
+ },
91
+ ],
92
+ },
93
+ ],
94
+ }, (pluginConfig === null || pluginConfig === void 0 ? void 0 : pluginConfig.overrides) || {}),
95
+ ], false) })); }; };
96
+ exports.default = redirects;
97
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],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,EAAE,WAAW,IAAK,OAAA,CAAA,WAAW,aAAX,WAAW,uBAAX,WAAW,CAAE,IAAI,MAAK,WAAW,EAAjC,CAAiC;6BACjE;yBACF;wBACD;4BACE,IAAI,EAAE,KAAK;4BACX,KAAK,EAAE,YAAY;4BACnB,IAAI,EAAE,MAAM;4BACZ,QAAQ,EAAE,IAAI;4BACd,KAAK,EAAE;gCACL,SAAS,EAAE,UAAC,CAAC,EAAE,WAAW,IAAK,OAAA,CAAA,WAAW,aAAX,WAAW,uBAAX,WAAW,CAAE,IAAI,MAAK,QAAQ,EAA9B,CAA8B;6BAC9D;yBACF;qBACF;iBACF;aACF;SACF,EAAE,CAAA,YAAY,aAAZ,YAAY,uBAAZ,YAAY,CAAE,SAAS,KAAI,EAAE,CAAC;iBAEnC,EAxEoF,CAwEpF,EAxEgD,CAwEhD,CAAC;AAEH,kBAAe,SAAS,CAAC"}
@@ -0,0 +1,5 @@
1
+ import { CollectionConfig } from "payload/types";
2
+ export type PluginConfig = {
3
+ overrides?: Partial<CollectionConfig>;
4
+ collections?: string[];
5
+ };
package/dist/types.js ADDED
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@payloadcms/plugin-redirects",
3
+ "version": "0.0.1",
4
+ "homepage:": "https://payloadcms.com",
5
+ "repository": "git@github.com:payloadcms/plugin-redirects.git",
6
+ "description": "Redirects plugin for Payload CMS",
7
+ "main": "dist/index.js",
8
+ "types": "dist/index.d.ts",
9
+ "scripts": {
10
+ "build": "tsc",
11
+ "test": "echo \"Error: no test specified\" && exit 1"
12
+ },
13
+ "keywords": [
14
+ "payload",
15
+ "cms",
16
+ "plugin",
17
+ "typescript",
18
+ "react",
19
+ "redirects",
20
+ "nextjs"
21
+ ],
22
+ "author": "dev@payloadcms.com",
23
+ "license": "MIT",
24
+ "peerDependencies": {
25
+ "payload": "^0.18.5 || ^1.0.0",
26
+ "react": "^16.8.0 || ^17.0.0 || ^18.0.0"
27
+ },
28
+ "devDependencies": {
29
+ "payload": "^1.0.9",
30
+ "react": "^18.0.0",
31
+ "typescript": "^4.5.5"
32
+ },
33
+ "files": [
34
+ "dist",
35
+ "types.js",
36
+ "types.d.ts"
37
+ ]
38
+ }
package/types.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './dist/types';
package/types.js ADDED
@@ -0,0 +1 @@
1
+ module.exports = require('./dist/types');