@quitecustard/payload 0.3.0 → 0.3.2

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 CHANGED
@@ -59,6 +59,8 @@ objects in place, so two collections must not receive one.
59
59
  - `owner-field` — a relationship auto-stamped to the creating user, refused on update by field-level
60
60
  access rather than by `admin.readOnly`, which Payload never consults on a write
61
61
  - `seo-tab` — over `@payloadcms/plugin-seo`'s field factories
62
+ - `link-url` — `validateLinkField`: the link protocol allowlist in the one-argument shape a
63
+ field's `validate` takes
62
64
  - `social-url` — `normaliseSocialUrl` (bare host → `https://…`) plus `validateSocialUrl`
63
65
  - `colour-picker-field`, `colour-options-field`, `phone-field`, `height-field`, `size-field`,
64
66
  `spacing-select-field`, `layout-fields`, `media-content-fields`
@@ -13,7 +13,7 @@ type SelectOption = {
13
13
  * linkField({ relationTo: ['pages', 'posts'], styleOptions: [{ label: 'Primary', value: 'primary' }] })
14
14
  * ```
15
15
  */
16
- export declare const linkField: ({ relationTo, dropdown, staticOptions, phoneComponent, anchorComponent, styleOptions, disableLabel, localizedLabel, overrides, maxDepth, }: {
16
+ export declare const linkField: ({ relationTo, dropdown, staticOptions, phoneComponent, anchorComponent, anchorDescription, styleOptions, disableLabel, localizedLabel, overrides, maxDepth, }: {
17
17
  /** Collections the "internal link" type may point to. */
18
18
  relationTo: CollectionSlug[];
19
19
  /** Collection slug for a "dropdown" link type (e.g. a menus collection). Omit to disable it. */
@@ -28,6 +28,8 @@ export declare const linkField: ({ relationTo, dropdown, staticOptions, phoneCom
28
28
  * disable it.
29
29
  */
30
30
  anchorComponent?: string;
31
+ /** Help text under the anchor picker, e.g. where its markers come from. */
32
+ anchorDescription?: string;
31
33
  /** Style select options (e.g. primary/secondary/outline). Omit to skip the style field. */
32
34
  styleOptions?: SelectOption[];
33
35
  /** Whether the field also carries a "Label" text input alongside the link type. */
@@ -14,7 +14,7 @@ const withRowWidth = (field) => ({ ...field, admin: { ...field.admin, width: '50
14
14
  * linkField({ relationTo: ['pages', 'posts'], styleOptions: [{ label: 'Primary', value: 'primary' }] })
15
15
  * ```
16
16
  */
17
- export const linkField = ({ relationTo, dropdown, staticOptions, phoneComponent, anchorComponent, styleOptions, disableLabel = false, localizedLabel = false, overrides = {}, maxDepth = 1, }) => {
17
+ export const linkField = ({ relationTo, dropdown, staticOptions, phoneComponent, anchorComponent, anchorDescription, styleOptions, disableLabel = false, localizedLabel = false, overrides = {}, maxDepth = 1, }) => {
18
18
  const typeOptions = [
19
19
  { label: 'Internal link', value: 'reference' },
20
20
  { label: 'Custom URL', value: 'custom' },
@@ -106,6 +106,7 @@ export const linkField = ({ relationTo, dropdown, staticOptions, phoneComponent,
106
106
  type: 'text',
107
107
  required: true,
108
108
  admin: {
109
+ ...(anchorDescription ? { description: anchorDescription } : {}),
109
110
  condition: (_, siblingData) => siblingData?.type === 'anchor',
110
111
  components: { Field: anchorComponent },
111
112
  },
@@ -0,0 +1,14 @@
1
+ /**
2
+ * The link protocol allowlist as a text field's `validate`: root-relative paths, in-page anchors,
3
+ * http(s), mailto: and tel:, never javascript:/data:. Empty values pass, so pair it with
4
+ * `required` where the field must be filled.
5
+ *
6
+ * ```ts
7
+ * { name: 'website', type: 'text', validate: validateLinkField }
8
+ * ```
9
+ *
10
+ * Use this, not `validateLinkUrl` from `@quitecustard/core/url/link-protocol`, as the `validate`
11
+ * itself. Payload calls `validate(value, options)`, and `validateLinkUrl`'s second parameter is a
12
+ * protocol allowlist - the options object would land in that slot.
13
+ */
14
+ export declare const validateLinkField: (value: string | null | undefined) => true | string;
@@ -0,0 +1,15 @@
1
+ import { validateLinkUrl } from '@quitecustard/core/url/link-protocol';
2
+ /**
3
+ * The link protocol allowlist as a text field's `validate`: root-relative paths, in-page anchors,
4
+ * http(s), mailto: and tel:, never javascript:/data:. Empty values pass, so pair it with
5
+ * `required` where the field must be filled.
6
+ *
7
+ * ```ts
8
+ * { name: 'website', type: 'text', validate: validateLinkField }
9
+ * ```
10
+ *
11
+ * Use this, not `validateLinkUrl` from `@quitecustard/core/url/link-protocol`, as the `validate`
12
+ * itself. Payload calls `validate(value, options)`, and `validateLinkUrl`'s second parameter is a
13
+ * protocol allowlist - the options object would land in that slot.
14
+ */
15
+ export const validateLinkField = (value) => validateLinkUrl(value);
@@ -1,4 +1,5 @@
1
- import type { Field, FieldAccess, RelationshipField } from 'payload';
1
+ import type { Field, FieldAccess, RelationshipField, TypedUser } from 'payload';
2
+ type GetUserId = (user: TypedUser) => unknown;
2
3
  /**
3
4
  * A relationship field to the auth collection, auto-stamped to the creating user: forced on create,
4
5
  * and refused on update by field-level access, so ownership can't be reassigned through the admin
@@ -11,9 +12,16 @@ import type { Field, FieldAccess, RelationshipField } from 'payload';
11
12
  * ```ts
12
13
  * ownerField({ relationTo: 'users' }, { readAccess: staffFieldLevel })
13
14
  * ```
15
+ *
16
+ * `getUserId` resolves the id to stamp from the caller, defaulting to `user.id`. A project with
17
+ * more than one auth collection passes its own, returning null for a caller that is not an
18
+ * account `relationTo` can point at - an API-key client, say - so that caller's id is never
19
+ * written into a relationship to a different collection.
14
20
  */
15
- export declare const ownerField: ({ relationTo }: {
21
+ export declare const ownerField: ({ relationTo, getUserId, }: {
16
22
  relationTo: RelationshipField["relationTo"];
23
+ getUserId?: GetUserId;
17
24
  }, { readAccess, ...overrides }?: Partial<Omit<RelationshipField, "type" | "relationTo">> & {
18
25
  readAccess?: FieldAccess;
19
26
  }) => Field;
27
+ export {};
@@ -1,10 +1,16 @@
1
- const makeSetOwner = () => {
1
+ const defaultUserId = (user) => user.id;
2
+ const makeSetOwner = (getUserId) => {
2
3
  const setOwner = ({ req, operation, value }) => {
3
- // `?? value` rather than `?? null`: a Local API create (a seed, a migration, an import) carries
4
- // no user, and discarding an explicitly supplied owner there would leave the row unowned.
5
- if (operation === 'create')
6
- return req?.user?.id ?? value ?? null;
7
- return value;
4
+ if (operation !== 'create')
5
+ return value;
6
+ const user = req?.user;
7
+ // No caller at all: a Local API create (a seed, a migration, an import) carries no user, and
8
+ // discarding an explicitly supplied owner there would leave the row unowned.
9
+ if (!user)
10
+ return value ?? null;
11
+ // A caller who is not an owner-able account (`getUserId` answers null) stamps nobody. The
12
+ // supplied value is NOT a fallback here, or that caller could name any owner it liked.
13
+ return getUserId(user) ?? null;
8
14
  };
9
15
  return setOwner;
10
16
  };
@@ -20,8 +26,13 @@ const makeSetOwner = () => {
20
26
  * ```ts
21
27
  * ownerField({ relationTo: 'users' }, { readAccess: staffFieldLevel })
22
28
  * ```
29
+ *
30
+ * `getUserId` resolves the id to stamp from the caller, defaulting to `user.id`. A project with
31
+ * more than one auth collection passes its own, returning null for a caller that is not an
32
+ * account `relationTo` can point at - an API-key client, say - so that caller's id is never
33
+ * written into a relationship to a different collection.
23
34
  */
24
- export const ownerField = ({ relationTo }, { readAccess, ...overrides } = {}) => {
35
+ export const ownerField = ({ relationTo, getUserId = defaultUserId, }, { readAccess, ...overrides } = {}) => {
25
36
  const { admin, hooks, access, ...rest } = overrides;
26
37
  return {
27
38
  name: 'owner',
@@ -42,7 +53,7 @@ export const ownerField = ({ relationTo }, { readAccess, ...overrides } = {}) =>
42
53
  ...admin,
43
54
  },
44
55
  hooks: {
45
- beforeChange: [makeSetOwner()],
56
+ beforeChange: [makeSetOwner(getUserId)],
46
57
  ...hooks,
47
58
  },
48
59
  ...rest,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quitecustard/payload",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "description": "Payload v3 helpers: relation narrowing, transactions, autosave-vs-unpublish detection, sync locks, unique slugs and usernames",
5
5
  "keywords": [
6
6
  "payload",
@@ -38,7 +38,7 @@
38
38
  }
39
39
  },
40
40
  "dependencies": {
41
- "@quitecustard/core": "^0.3.0"
41
+ "@quitecustard/core": "^0.4.0"
42
42
  },
43
43
  "peerDependencies": {
44
44
  "@payloadcms/drizzle": "^3.0.0",