@easy-cms/plugin-redirects 0.24.0 → 0.25.0

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/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Plugin } from '@easy-cms/core';
1
+ import { TypedPlugin } from '@easy-cms/core';
2
2
 
3
3
  type Doc$1 = Record<string, unknown>;
4
4
  interface UrlArgs {
@@ -38,7 +38,51 @@ interface RedirectsPluginOptions {
38
38
  * A `redirects` collection under Settings in the admin, `resolveRedirect()` for your app's
39
39
  * middleware, and automatic redirects when a page's address changes.
40
40
  */
41
- declare function redirectsPlugin(options?: RedirectsPluginOptions): Plugin;
41
+ /** `to_<slug>`: the field of a redirect that points to a document of collection `S`. */
42
+ type TargetField<S extends string> = `to_${Underscored<S>}`;
43
+ type Underscored<S extends string> = S extends `${infer A}-${infer B}` ? `${A}_${Underscored<B>}` : S;
44
+ /** The redirects collection, as the inferred document types see it. */
45
+ type RedirectsCollection<R extends string, S extends string> = {
46
+ readonly slug: R;
47
+ readonly fields: readonly [
48
+ {
49
+ readonly name: 'from';
50
+ readonly type: 'text';
51
+ readonly required: true;
52
+ },
53
+ {
54
+ readonly name: 'to';
55
+ readonly type: 'text';
56
+ },
57
+ {
58
+ readonly name: 'locale';
59
+ readonly type: 'select';
60
+ readonly options: readonly string[];
61
+ },
62
+ {
63
+ readonly name: 'type';
64
+ readonly type: 'select';
65
+ readonly options: readonly ['301', '302', '307', '308'];
66
+ readonly required: true;
67
+ readonly defaultValue: '301';
68
+ },
69
+ ...{
70
+ [K in S]: {
71
+ readonly name: TargetField<K>;
72
+ readonly type: 'relationship';
73
+ readonly to: K;
74
+ };
75
+ }[S][]
76
+ ];
77
+ };
78
+ /** What `redirectsPlugin(options)` adds: the redirects collection. */
79
+ type RedirectsPluginTypes<R extends string, S extends string> = {
80
+ readonly collections: readonly [RedirectsCollection<R, S>];
81
+ };
82
+ declare function redirectsPlugin<const S extends string = never, const R extends string = 'redirects'>(options?: RedirectsPluginOptions & {
83
+ readonly collections?: readonly S[];
84
+ readonly slug?: R;
85
+ }): TypedPlugin<RedirectsPluginTypes<R, S>>;
42
86
 
43
87
  type Doc = Record<string, unknown>;
44
88
  /** The part of the Local API the helpers use; a structural type. */
@@ -81,4 +125,4 @@ declare function resolveRedirect(cms: RedirectsCMS, input: string | URL): Promis
81
125
  declare const STATUSES: readonly [301, 302, 307, 308];
82
126
  type RedirectStatus = (typeof STATUSES)[number];
83
127
 
84
- export { type Redirect, type RedirectStatus, type RedirectsPluginOptions, STATUSES, type UrlArgs, clearRedirects, normalizePath, redirectsPlugin, resolveRedirect };
128
+ export { type Redirect, type RedirectStatus, type RedirectsCollection, type RedirectsPluginOptions, type RedirectsPluginTypes, STATUSES, type UrlArgs, clearRedirects, normalizePath, redirectsPlugin, resolveRedirect };
package/dist/index.js CHANGED
@@ -1,3 +1,8 @@
1
+ // src/plugin.ts
2
+ import {
3
+ definePlugin
4
+ } from "@easy-cms/core";
5
+
1
6
  // src/shared.ts
2
7
  var REDIRECTS_SOURCE = /* @__PURE__ */ Symbol.for("easy-cms.plugin-redirects.source");
3
8
  var STATUSES = [301, 302, 307, 308];
@@ -95,7 +100,7 @@ async function target(cms, source, doc) {
95
100
 
96
101
  // src/plugin.ts
97
102
  function redirectsPlugin(options = {}) {
98
- return (config) => {
103
+ return definePlugin((config) => {
99
104
  const slug = options.slug ?? "redirects";
100
105
  const collections = options.collections ?? [];
101
106
  const missing = collections.filter((s) => !config.collections?.some((c) => c.slug === s));
@@ -273,7 +278,7 @@ function redirectsPlugin(options = {}) {
273
278
  collections: [...(config.collections ?? []).map(withAuto), redirects2],
274
279
  endpoints: [...config.endpoints ?? [], endpoint]
275
280
  };
276
- };
281
+ });
277
282
  }
278
283
  export {
279
284
  STATUSES,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@easy-cms/plugin-redirects",
3
- "version": "0.24.0",
3
+ "version": "0.25.0",
4
4
  "description": "Redirects for Easy CMS: 301s managed in the admin, automatic redirects when a page's address changes",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -16,11 +16,11 @@
16
16
  "dist"
17
17
  ],
18
18
  "peerDependencies": {
19
- "@easy-cms/core": "^0.24.0"
19
+ "@easy-cms/core": "^0.25.0"
20
20
  },
21
21
  "devDependencies": {
22
- "@easy-cms/core": "^0.24.0",
23
- "@easy-cms/db-sqlite": "^0.24.0"
22
+ "@easy-cms/core": "^0.25.0",
23
+ "@easy-cms/db-sqlite": "^0.25.0"
24
24
  },
25
25
  "engines": {
26
26
  "node": ">=22.12"