rimecms 1.1.2 → 1.1.4

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.
Files changed (73) hide show
  1. package/dist/adapter-sqlite/relations.server.d.ts +4 -4
  2. package/dist/core/adapter.d.ts +4 -4
  3. package/dist/core/fields/types.d.ts +34 -3
  4. package/dist/core/fields/types.test-d.d.ts +1 -0
  5. package/dist/core/fields/types.test-d.js +37 -0
  6. package/dist/core/i18n/en/fields.d.ts +1 -1
  7. package/dist/core/i18n/en/fields.js +1 -1
  8. package/dist/core/i18n/fr/fields.d.ts +1 -1
  9. package/dist/core/i18n/fr/fields.js +1 -1
  10. package/dist/core/pipeline/hooks/set-default-values.server.d.ts +1 -1
  11. package/dist/core/pipeline/hooks/set-default-values.server.js +25 -30
  12. package/dist/core/pipeline/persist/relations/diff.server.d.ts +4 -4
  13. package/dist/core/pipeline/persist/relations/extract.server.js +27 -45
  14. package/dist/core/prototype/area/api.server.d.ts +7 -1
  15. package/dist/core/prototype/area/api.server.js +3 -0
  16. package/dist/core/prototype/area/operations/update.d.ts +2 -1
  17. package/dist/core/prototype/collection/api.server.d.ts +16 -3
  18. package/dist/core/prototype/collection/api.server.js +6 -0
  19. package/dist/core/prototype/collection/operations/create.d.ts +2 -1
  20. package/dist/core/prototype/collection/operations/delete.js +6 -4
  21. package/dist/core/prototype/collection/operations/duplicate.js +2 -4
  22. package/dist/core/prototype/collection/operations/update-by-id.d.ts +2 -1
  23. package/dist/core/prototype/collection/thumbnail/hooks/set-document-thumbnail.server.js +9 -15
  24. package/dist/core/prototype/collection/upload/disk/delete.server.js +1 -1
  25. package/dist/core/prototype/shared/title/hooks/set-document-title.server.js +2 -2
  26. package/dist/core/prototype/shared/versions/hooks/handle-new-version.server.js +17 -1
  27. package/dist/fields/blocks/component/focus/BlocksFocus.svelte +21 -4
  28. package/dist/fields/blocks/component/focus/Inspector.svelte +12 -28
  29. package/dist/fields/blocks/component/focus/LayersList.svelte +4 -1
  30. package/dist/fields/blocks/component/focus/Renders.svelte +44 -28
  31. package/dist/fields/blocks/component/focus/Renders.svelte.d.ts +3 -0
  32. package/dist/fields/blocks/component/focus/StageAdd.svelte +42 -0
  33. package/dist/fields/blocks/component/focus/StageAdd.svelte.d.ts +6 -0
  34. package/dist/fields/blocks/component/focus/focus.svelte.d.ts +7 -13
  35. package/dist/fields/blocks/component/focus/focus.svelte.js +38 -33
  36. package/dist/fields/blocks/index.d.ts +26 -5
  37. package/dist/fields/blocks/index.js +8 -2
  38. package/dist/fields/relation/component/Relation.svelte +16 -3
  39. package/dist/fields/relation/component/Relation.svelte.d.ts +2 -1
  40. package/dist/fields/relation/component/RelationInline.svelte +6 -9
  41. package/dist/fields/relation/fetch.d.ts +12 -0
  42. package/dist/fields/relation/fetch.js +27 -0
  43. package/dist/fields/relation/index.d.ts +16 -7
  44. package/dist/fields/relation/index.js +20 -4
  45. package/dist/fields/relation/module.server.js +15 -18
  46. package/dist/fields/relation/populate.js +4 -21
  47. package/dist/fields/relation/relation.d.ts +38 -0
  48. package/dist/fields/relation/relation.js +55 -0
  49. package/dist/fields/relation/relation.test-d.d.ts +1 -0
  50. package/dist/fields/relation/relation.test-d.js +10 -0
  51. package/dist/fields/relation/value.d.ts +2 -2
  52. package/dist/fields/rich-text/component/Cell.svelte +2 -2
  53. package/dist/fields/rich-text/json.d.ts +4 -9
  54. package/dist/fields/rich-text/json.js +8 -6
  55. package/dist/fields/types.d.ts +4 -1
  56. package/dist/live/Consumer.svelte +3 -3
  57. package/dist/live/Consumer.svelte.d.ts +2 -2
  58. package/dist/panel/components/sections/collection/grid/grid-item/GridItem.svelte +7 -1
  59. package/dist/panel/components/ui/button/button.svelte +2 -3
  60. package/dist/panel/context/documentForm.svelte.js +2 -2
  61. package/dist/panel/style/index.css +4 -3
  62. package/dist/panel/style/palette.css +3 -3
  63. package/dist/panel/style/scheme.css +1 -0
  64. package/dist/public.d.ts +3 -3
  65. package/dist/public.js +3 -3
  66. package/dist/types.d.ts +1 -1
  67. package/dist/util/types.d.ts +1 -1
  68. package/dist/util/types.js +1 -1
  69. package/package.json +1 -1
  70. package/dist/fields/blocks/component/focus/StagePlaceholder.svelte +0 -86
  71. package/dist/fields/blocks/component/focus/StagePlaceholder.svelte.d.ts +0 -8
  72. package/dist/fields/relation/util.d.ts +0 -57
  73. package/dist/fields/relation/util.js +0 -86
@@ -1,8 +1,8 @@
1
- import type { WithRelationPopulated } from '../core/fields/types.js';
1
+ import type { WithRelationResolved } from '../core/fields/types.js';
2
2
  import type { Snippet } from 'svelte';
3
3
  declare function $$render<T>(): {
4
4
  props: {
5
- child: Snippet<[doc: WithRelationPopulated<T>]>;
5
+ child: Snippet<[doc: WithRelationResolved<T>]>;
6
6
  data: {
7
7
  doc: T;
8
8
  };
@@ -4,6 +4,7 @@
4
4
  import { PARAMS } from '../../../../../../core/constants.js';
5
5
  import { UPLOAD_PATH } from '../../../../../../core/prototype/collection/upload/constant.js';
6
6
  import { isUploadConfig } from '../../../../../../core/prototype/collection/upload/util/config';
7
+ import { getMimeTypeFromExtension } from '../../../../../../core/prototype/collection/upload/util/mime';
7
8
  import type { GenericDoc } from '../../../../../../core/prototype/types';
8
9
  import { panelPath } from '../../../../../../core/routes/util';
9
10
  import { getLocaleContext } from '../../../../../context/locale.svelte.js';
@@ -29,7 +30,12 @@
29
30
  const isUploadCollection = $derived(isUploadConfig(config));
30
31
  const hasDraft = $derived(!!(config.versions && config.versions.draft));
31
32
 
32
- const mimeType = $derived(typeof doc.mimeType === 'string' ? doc.mimeType : '');
33
+ // A file's own type, else the one its thumbnail's extension names: a page shows its image.
34
+ const mimeType = $derived.by(() => {
35
+ if (typeof doc.mimeType === 'string') return doc.mimeType;
36
+ const url = typeof doc._thumbnail === 'string' ? doc._thumbnail : '';
37
+ return getMimeTypeFromExtension(url.split('.').pop() || '') || '';
38
+ });
33
39
  const isImage = $derived(!!doc._thumbnail && mimeType.startsWith('image/'));
34
40
  // A photo fills its frame; a png, a gif or an svg may be a logo, so it shows whole.
35
41
  const contain = $derived(/^image\/(png|gif|svg)/.test(mimeType));
@@ -103,7 +103,7 @@
103
103
  --rz-button-ghost-fg: var(--rz-fg);
104
104
 
105
105
  /* Secondary variant */
106
- --rz-button-secondary-bg: var(--rz-bg-raised);
106
+ --rz-button-secondary-bg: oklch(from var(--rz-bg-inverse) l c h / 0.032);
107
107
  --rz-button-secondary-fg: var(--rz-fg);
108
108
 
109
109
  /* Link variant */
@@ -129,8 +129,7 @@
129
129
  font-variation-settings: 'wght' 500;
130
130
  font-weight: 500;
131
131
  transition-property:
132
- box-shadow, color, background-color, border-color, text-decoration-color, fill, stroke,
133
- filter;
132
+ box-shadow, color, background-color, border-color, text-decoration-color, fill, stroke, filter;
134
133
  transition-duration: 0.25s;
135
134
  gap: var(--rz-size-2);
136
135
  fill: currentColor;
@@ -7,7 +7,7 @@ import { getFieldAtPath } from '../../core/fields/util.js';
7
7
  import { buildConfigMap } from '../../core/pipeline/config-map/index.js';
8
8
  import { AUTO_SAVE_DELAY_MS, VERSIONS_STATUS } from '../../core/prototype/shared/versions/constant.js';
9
9
  import { apiUrl, panelUrl } from '../../core/routes/util.js';
10
- import { isJSONContent, richTextJSONToText } from '../../fields/rich-text/json.js';
10
+ import { isJSONContent, RichText } from '../../fields/rich-text/json.js';
11
11
  import { isObjectLiteral, omit } from '../../util/object.js';
12
12
  import { normalizeFieldPath } from '../../util/string.js';
13
13
  import { randomId } from '../../util/random.js';
@@ -82,7 +82,7 @@ function createDocumentFormState({ initial, config, readOnly, key, onNestedDocum
82
82
  function computeTitleFromValue(value) {
83
83
  // Handle rich text value
84
84
  if (isJSONContent(value)) {
85
- return richTextJSONToText(value);
85
+ return RichText.toText(value);
86
86
  }
87
87
  if (typeof value === 'string') {
88
88
  return value;
@@ -520,9 +520,9 @@ video {
520
520
  --rz-gray-10: oklch(0.625 var(--rz-gray-chroma) var(--rz-gray-hue));
521
521
  --rz-gray-11: oklch(0.69 var(--rz-gray-chroma) var(--rz-gray-hue));
522
522
  --rz-gray-12: oklch(0.755 var(--rz-gray-chroma) var(--rz-gray-hue));
523
- --rz-gray-13: oklch(0.815 var(--rz-gray-chroma) var(--rz-gray-hue));
524
- --rz-gray-14: oklch(0.865 var(--rz-gray-chroma) var(--rz-gray-hue));
525
- --rz-gray-15: oklch(0.905 var(--rz-gray-chroma) var(--rz-gray-hue));
523
+ --rz-gray-13: oklch(0.845 var(--rz-gray-chroma) var(--rz-gray-hue));
524
+ --rz-gray-14: oklch(0.885 var(--rz-gray-chroma) var(--rz-gray-hue));
525
+ --rz-gray-15: oklch(0.915 var(--rz-gray-chroma) var(--rz-gray-hue));
526
526
  --rz-gray-16: oklch(0.94 var(--rz-gray-chroma) var(--rz-gray-hue));
527
527
  --rz-gray-17: oklch(0.968 var(--rz-gray-chroma) var(--rz-gray-hue));
528
528
  --rz-gray-18: oklch(0.986 var(--rz-gray-chroma) var(--rz-gray-hue));
@@ -547,6 +547,7 @@ video {
547
547
  */
548
548
  :root {
549
549
  color-scheme: light dark;
550
+ scrollbar-color: var(--rz-border-strong) transparent; /* thumb track */
550
551
 
551
552
  /* levels */
552
553
  --rz-bg-base: var(--rz-gray-15);
@@ -18,9 +18,9 @@
18
18
  --rz-gray-10: oklch(0.625 var(--rz-gray-chroma) var(--rz-gray-hue));
19
19
  --rz-gray-11: oklch(0.69 var(--rz-gray-chroma) var(--rz-gray-hue));
20
20
  --rz-gray-12: oklch(0.755 var(--rz-gray-chroma) var(--rz-gray-hue));
21
- --rz-gray-13: oklch(0.815 var(--rz-gray-chroma) var(--rz-gray-hue));
22
- --rz-gray-14: oklch(0.865 var(--rz-gray-chroma) var(--rz-gray-hue));
23
- --rz-gray-15: oklch(0.905 var(--rz-gray-chroma) var(--rz-gray-hue));
21
+ --rz-gray-13: oklch(0.845 var(--rz-gray-chroma) var(--rz-gray-hue));
22
+ --rz-gray-14: oklch(0.885 var(--rz-gray-chroma) var(--rz-gray-hue));
23
+ --rz-gray-15: oklch(0.915 var(--rz-gray-chroma) var(--rz-gray-hue));
24
24
  --rz-gray-16: oklch(0.94 var(--rz-gray-chroma) var(--rz-gray-hue));
25
25
  --rz-gray-17: oklch(0.968 var(--rz-gray-chroma) var(--rz-gray-hue));
26
26
  --rz-gray-18: oklch(0.986 var(--rz-gray-chroma) var(--rz-gray-hue));
@@ -6,6 +6,7 @@
6
6
  */
7
7
  :root {
8
8
  color-scheme: light dark;
9
+ scrollbar-color: var(--rz-border-strong) transparent; /* thumb track */
9
10
 
10
11
  /* levels */
11
12
  --rz-bg-base: var(--rz-gray-15);
package/dist/public.d.ts CHANGED
@@ -1,13 +1,13 @@
1
1
  import { getI18nContext, setI18nContext } from './core/i18n/context.js';
2
2
  import { createI18n } from './core/i18n/index.js';
3
3
  import { openSse } from './core/plugins/sse/index.js';
4
- import { isRelationPopulated, isRelationResolved, isRelationUnresolved, resolveRelation } from './fields/relation/util.js';
4
+ import { Relation } from './fields/relation/relation.js';
5
5
  import RenderRichText from './fields/rich-text/core/render-rich-text.svelte';
6
- import { richTextJSONToText } from './fields/rich-text/json.js';
6
+ import { RichText } from './fields/rich-text/json.js';
7
7
  import LiveConsumer from './live/Consumer.svelte';
8
8
  import LiveEdit from './live/LiveEdit.svelte';
9
9
  import LiveProvider from './live/Provider.svelte';
10
10
  export type { SSEEvent, SSEHandler } from './core/plugins/sse/index.js';
11
11
  export type { RichTextNodeRenderer, RichTextNodeRendererProps } from './fields/rich-text/core/types.js';
12
12
  export type { JSONContent } from '@tiptap/core';
13
- export { createI18n, getI18nContext, isRelationPopulated, isRelationResolved, isRelationUnresolved, LiveConsumer, LiveEdit, LiveProvider, openSse, RenderRichText, resolveRelation, richTextJSONToText, setI18nContext };
13
+ export { createI18n, getI18nContext, LiveConsumer, LiveEdit, LiveProvider, openSse, Relation, RenderRichText, RichText, setI18nContext };
package/dist/public.js CHANGED
@@ -3,10 +3,10 @@
3
3
  import { getI18nContext, setI18nContext } from './core/i18n/context.js';
4
4
  import { createI18n } from './core/i18n/index.js';
5
5
  import { openSse } from './core/plugins/sse/index.js';
6
- import { isRelationPopulated, isRelationResolved, isRelationUnresolved, resolveRelation } from './fields/relation/util.js';
6
+ import { Relation } from './fields/relation/relation.js';
7
7
  import RenderRichText from './fields/rich-text/core/render-rich-text.svelte';
8
- import { richTextJSONToText } from './fields/rich-text/json.js';
8
+ import { RichText } from './fields/rich-text/json.js';
9
9
  import LiveConsumer from './live/Consumer.svelte';
10
10
  import LiveEdit from './live/LiveEdit.svelte';
11
11
  import LiveProvider from './live/Provider.svelte';
12
- export { createI18n, getI18nContext, isRelationPopulated, isRelationResolved, isRelationUnresolved, LiveConsumer, LiveEdit, LiveProvider, openSse, RenderRichText, resolveRelation, richTextJSONToText, setI18nContext };
12
+ export { createI18n, getI18nContext, LiveConsumer, LiveEdit, LiveProvider, openSse, Relation, RenderRichText, RichText, setI18nContext };
package/dist/types.d.ts CHANGED
@@ -10,7 +10,7 @@ export type { CollectionProps, FieldPanelTableConfig, FormErrors, Navigation, Ro
10
10
  export type { DocumentFormContext } from './panel/context/documentForm.svelte.js';
11
11
  export type { JsonFile, UploadDoc } from './core/prototype/collection/upload/types.js';
12
12
  export type { Link } from './fields/link/types.js';
13
- export type { WithRelationPopulated } from './core/fields/types.js';
13
+ export type { WithRelationInput, WithRelationResolved } from './core/fields/types.js';
14
14
  export type { RouteHandlers } from './core/routes/handler.server.js';
15
15
  export type { Plugin } from './core/plugins/index.js';
16
16
  export type { SSEAccess, SSEConfig } from './core/plugins/sse/types.js';
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * Generic type utilities. Nothing here names a rime type — that is the rule that keeps this
3
- * file in util/. The two that did (WithRelationPopulated, WithoutBuilders) moved to
3
+ * file in util/. The ones that do (WithRelationResolved, WithoutBuilders) are in
4
4
  * core/fields/types.ts.
5
5
  */
6
6
  export type OmitPreservingDiscrimination<T, K extends keyof T> = T extends any ? Omit<T, K> : never;
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * Generic type utilities. Nothing here names a rime type — that is the rule that keeps this
3
- * file in util/. The two that did (WithRelationPopulated, WithoutBuilders) moved to
3
+ * file in util/. The ones that do (WithRelationResolved, WithoutBuilders) are in
4
4
  * core/fields/types.ts.
5
5
  */
6
6
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rimecms",
3
- "version": "1.1.2",
3
+ "version": "1.1.4",
4
4
  "homepage": "https://github.com/bienbiendev/rime",
5
5
  "packageManager": "bun@1.3.0",
6
6
  "scripts": {
@@ -1,86 +0,0 @@
1
- <script lang="ts">
2
- import { t__ } from '../../../../core/i18n/index.js';
3
- import Kbd from '../../../../panel/components/ui/kbd/Kbd.svelte';
4
- import { getBlocksFocusContext } from './focus.svelte.js';
5
-
6
- /** `compact` inside a block's render, where the list may be a narrow column. */
7
- type Props = { list: string; compact?: boolean };
8
- const { list, compact = false }: Props = $props();
9
-
10
- const focus = getBlocksFocusContext()!;
11
-
12
- /** "Type $1 to add a block", cut around the key. */
13
- const KEY = '\u0000';
14
- const [before, after] = $derived(t__('fields.type_to_add_block', KEY).split(KEY));
15
-
16
- /** Adds at the end of the list: the list selected, then the picker, as `/` opens. */
17
- function add(event: MouseEvent) {
18
- event.stopPropagation();
19
- focus.selectList(list);
20
- focus.pick();
21
- }
22
- </script>
23
-
24
- <!-- Under the blocks: the key that adds one, and a click that does the same. -->
25
- <button
26
- type="button"
27
- class="rz-stage-placeholder"
28
- class:rz-stage-placeholder--compact={compact}
29
- onclick={add}
30
- >
31
- {before}<Kbd keys="/" />{after}
32
- </button>
33
-
34
- <style>/* A level: its fill, a hairline, and its shadow when it has one. raised has none, float does. */
35
-
36
- /* A well: an input, a track, a dropzone. Its edge shows on hover. */
37
-
38
- /* The hover tint, laid over the element's own fill. */
39
-
40
- /* The picked item of a segmented control. */
41
-
42
- /* The primary button's face: black on light, white on dark. */
43
-
44
- /* Keyboard focus on a button, a tab, a link. */
45
-
46
- /* A focused input: an accent edge and a soft halo. */
47
-
48
- /* An input with an error. */
49
- /* A row of its own, across every column of the list. */
50
- .rz-stage-placeholder {
51
- display: block;
52
- grid-column: 1 / -1;
53
- width: 100%;
54
- padding: var(--rz-size-1) var(--rz-size-1-5);
55
- border-radius: var(--rz-radius-lg);
56
- color: var(--rz-fg-subtle);
57
- font-size: var(--rz-text-xl);
58
- text-align: left;
59
- cursor: text;
60
- transition: color 0.15s;
61
-
62
- &:hover {
63
- color: var(--rz-fg-muted);
64
- }
65
- &:focus-visible {
66
- outline: 2px solid var(--rz-accent-border);
67
- outline-offset: 2px;
68
- }
69
-
70
- :global(.rz-kbd) {
71
- margin-inline: var(--rz-size-0-5);
72
- color: var(--rz-fg-muted);
73
- font-size: var(--rz-text-sm);
74
- vertical-align: text-bottom;
75
- }
76
- }
77
-
78
- .rz-stage-placeholder--compact {
79
- padding: var(--rz-size-2) var(--rz-size-3);
80
- font-size: var(--rz-text-sm);
81
-
82
- :global(.rz-kbd) {
83
- font-size: var(--rz-text-xs);
84
- }
85
- }
86
- </style>
@@ -1,8 +0,0 @@
1
- /** `compact` inside a block's render, where the list may be a narrow column. */
2
- type Props = {
3
- list: string;
4
- compact?: boolean;
5
- };
6
- declare const StagePlaceholder: import("svelte").Component<Props, {}, "">;
7
- type StagePlaceholder = ReturnType<typeof StagePlaceholder>;
8
- export default StagePlaceholder;
@@ -1,57 +0,0 @@
1
- import type { RelationRef, RelationValue } from '../types.js';
2
- import type { Relation } from './index.js';
3
- /**
4
- * Checks if a relation field's value is populated with full documents.
5
- *
6
- * A relation is populated when it is a non-empty array of documents: objects that are not
7
- * relation references (`id`, `relationTo`, `documentId`). Anything else is not populated.
8
- *
9
- * @example
10
- * isRelationPopulated([{ id: '7674…', title: 'Home' }]); // true
11
- * isRelationPopulated('7674e91b-598a-4a72-a5cd-9594736a34dd'); // false
12
- * isRelationPopulated([{ id: '7674…', relationTo: 'articles', documentId: 'b674…' }]); // false
13
- * isRelationPopulated([]); // false
14
- * isRelationPopulated(undefined); // false
15
- * isRelationPopulated(null); // false
16
- */
17
- export declare const isRelationPopulated: <T>(value: RelationValue<T> | null | undefined) => value is T[];
18
- /**
19
- * Checks if a relation value is resolved (contains the actual referenced document).
20
- *
21
- * @example
22
- * // Returns true for a resolved relation
23
- * isRelationResolved({ title: 'Home Page', _prototype: 'collection', _type: 'pages' });
24
- */
25
- export declare const isRelationResolved: <T>(value: any) => value is T;
26
- /**
27
- * Checks if a relation value is a reference (contains only the relationTo and documentId).
28
- *
29
- * @example
30
- * // Returns true for a relation reference
31
- * isRelationRef({ relationTo: 'pages', documentId: '123' });
32
- */
33
- export declare const isRelationRef: (value: unknown) => value is RelationRef;
34
- /**
35
- * Checks if a relation value is unresolved (contains only reference information).
36
- *
37
- * @example
38
- * // Returns true for an unresolved relation
39
- * isRelationUnresolved({ relationTo: 'pages', documentId: '123' });
40
- */
41
- export declare const isRelationUnresolved: (value: any) => value is Omit<Relation, "path" | "position" | "ownerId">;
42
- /**
43
- * Resolves a relation reference to the actual document it points to.
44
- *
45
- * @example
46
- * // Resolves a relation reference to the actual document
47
- * const doc = await resolveRelationRef({ relationTo: 'pages', documentId: '123' });
48
- */
49
- export declare function resolveRelationRef<T>(item: T | RelationRef | string): Promise<T>;
50
- /**
51
- * Resolves a relation field value to the actual documents it points to.
52
- *
53
- * @example
54
- * // Resolves a relation value to the actual documents
55
- * const docs = await resolveRelation([{ relationTo: 'pages', documentId: '123' }]);
56
- */
57
- export declare function resolveRelation<T>(value: RelationValue<T> | null | undefined): Promise<T[] | null | undefined>;
@@ -1,86 +0,0 @@
1
- import { hasProps, isObjectLiteral } from '../../util/object.js';
2
- /**
3
- * Checks if a relation field's value is populated with full documents.
4
- *
5
- * A relation is populated when it is a non-empty array of documents: objects that are not
6
- * relation references (`id`, `relationTo`, `documentId`). Anything else is not populated.
7
- *
8
- * @example
9
- * isRelationPopulated([{ id: '7674…', title: 'Home' }]); // true
10
- * isRelationPopulated('7674e91b-598a-4a72-a5cd-9594736a34dd'); // false
11
- * isRelationPopulated([{ id: '7674…', relationTo: 'articles', documentId: 'b674…' }]); // false
12
- * isRelationPopulated([]); // false
13
- * isRelationPopulated(undefined); // false
14
- * isRelationPopulated(null); // false
15
- */
16
- export const isRelationPopulated = (value) => {
17
- if (!Array.isArray(value) || value.length === 0)
18
- return false;
19
- return value.every((v) => isObjectLiteral(v) && !hasProps(['id', 'relationTo', 'documentId'], v));
20
- };
21
- /**
22
- * Checks if a relation value is resolved (contains the actual referenced document).
23
- *
24
- * @example
25
- * // Returns true for a resolved relation
26
- * isRelationResolved({ title: 'Home Page', _prototype: 'collection', _type: 'pages' });
27
- */
28
- export const isRelationResolved = (value) => {
29
- return value && isObjectLiteral(value) && hasProps(['title', '_prototype', '_type'], value);
30
- };
31
- /**
32
- * Checks if a relation value is a reference (contains only the relationTo and documentId).
33
- *
34
- * @example
35
- * // Returns true for a relation reference
36
- * isRelationRef({ relationTo: 'pages', documentId: '123' });
37
- */
38
- export const isRelationRef = (value) => typeof value === 'object' &&
39
- value !== null &&
40
- typeof value.relationTo === 'string' &&
41
- typeof value.documentId === 'string';
42
- /**
43
- * Checks if a relation value is unresolved (contains only reference information).
44
- *
45
- * @example
46
- * // Returns true for an unresolved relation
47
- * isRelationUnresolved({ relationTo: 'pages', documentId: '123' });
48
- */
49
- export const isRelationUnresolved = (value) => {
50
- return value && isObjectLiteral(value) && hasProps(['relationTo', 'documentId'], value);
51
- };
52
- /**
53
- * Resolves a relation reference to the actual document it points to.
54
- *
55
- * @example
56
- * // Resolves a relation reference to the actual document
57
- * const doc = await resolveRelationRef({ relationTo: 'pages', documentId: '123' });
58
- */
59
- export async function resolveRelationRef(item) {
60
- if (isRelationRef(item)) {
61
- return fetch(`api/${item.relationTo}/${item.documentId}`)
62
- .then((r) => r.json())
63
- .then((r) => r.doc);
64
- }
65
- if (typeof item === 'string') {
66
- throw new Error(`Cannot resolve relation from a bare id ("${item}") — missing "relationTo". ` +
67
- `resolveRelationRef only works on populated docs or { relationTo, documentId } objects.`);
68
- }
69
- if (isRelationResolved(item)) {
70
- return item;
71
- }
72
- throw new Error(`Unrecognized relation shape: ${JSON.stringify(item)}`);
73
- }
74
- /**
75
- * Resolves a relation field value to the actual documents it points to.
76
- *
77
- * @example
78
- * // Resolves a relation value to the actual documents
79
- * const docs = await resolveRelation([{ relationTo: 'pages', documentId: '123' }]);
80
- */
81
- export async function resolveRelation(value) {
82
- if (value === null || value === undefined)
83
- return value;
84
- const items = Array.isArray(value) ? value : [value];
85
- return Promise.all(items.map((item) => resolveRelationRef(item)));
86
- }