@prismicio/vue 4.0.3 → 4.2.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.
@@ -1,22 +1,22 @@
1
- import { PrismicPlugin } from "./types";
2
- /**
3
- * Accesses `@prismicio/vue` plugin interface.
4
- *
5
- * @example
6
- *
7
- * ```javascript
8
- * // With the composition API
9
- * import { usePrismic } from "@prismicio/vue";
10
- *
11
- * export default {
12
- * setup() {
13
- * const prismic = usePrismic();
14
- *
15
- * return {};
16
- * },
17
- * };
18
- * ```
19
- *
20
- * @returns The interface {@link PrismicPlugin}
21
- */
22
- export declare const usePrismic: () => PrismicPlugin;
1
+ import { PrismicPlugin } from "./types";
2
+ /**
3
+ * Accesses `@prismicio/vue` plugin interface.
4
+ *
5
+ * @example
6
+ *
7
+ * ```javascript
8
+ * // With the composition API
9
+ * import { usePrismic } from "@prismicio/vue";
10
+ *
11
+ * export default {
12
+ * setup() {
13
+ * const prismic = usePrismic();
14
+ *
15
+ * return {};
16
+ * },
17
+ * };
18
+ * ```
19
+ *
20
+ * @returns The interface {@link PrismicPlugin}
21
+ */
22
+ export declare const usePrismic: () => PrismicPlugin;
@@ -1,64 +1,64 @@
1
- import { Client, ForbiddenError, ParsingError, PrismicError } from "@prismicio/client";
2
- import { Ref } from "vue";
3
- import { PrismicClientComposableState, VueUseParameters } from "./types";
4
- type UnwrapPromise<T> = T extends Promise<infer U> ? U : T;
5
- type ClientMethodLike = (...args: any[]) => Promise<any> | any;
6
- type ClientMethods = typeof Client.prototype;
7
- type ClientError = PrismicError<unknown> | ParsingError | ForbiddenError;
8
- /**
9
- * @internal
10
- */
11
- export type ClientMethodParameters<TMethodName extends keyof ClientMethods> = ClientMethods[TMethodName] extends ClientMethodLike ? VueUseParameters<Parameters<ClientMethods[TMethodName]>> : never;
12
- /**
13
- * @internal
14
- */
15
- export type ClientMethodReturnType<TMethodName extends keyof ClientMethods> = ClientMethods[TMethodName] extends ClientMethodLike ? ReturnType<ClientMethods[TMethodName]> : never;
16
- /**
17
- * @internal
18
- */
19
- export type ComposableOnlyParameters = {
20
- client?: Ref<Client> | Client;
21
- };
22
- /**
23
- * The return type of a `@prismicio/client` Vue composable.
24
- *
25
- * @typeParam TData - The expected format of the `data` property of the returned
26
- * object
27
- */
28
- export type ClientComposableReturnType<TData = unknown> = {
29
- /**
30
- * The current state of the composable's client method call.
31
- */
32
- state: Ref<PrismicClientComposableState>;
33
- /**
34
- * Data returned by the client.
35
- */
36
- data: Ref<TData | null>;
37
- /**
38
- * Error returned by the composable's client method call if in an errror
39
- * state.
40
- */
41
- error: Ref<ClientError | Error | null>;
42
- /**
43
- * Perform the composable's client method call again.
44
- */
45
- refresh: () => Promise<void>;
46
- };
47
- /**
48
- * A low level Vue composable that uses provided method name on plugin or
49
- * provided client with given arguments. The composable has its own internal
50
- * state manager to report async status, such as pending or error statuses.
51
- *
52
- * @typeParam TClientMethodName - A method name from `@prismicio/client`
53
- * @typeParam TClientMethodArguments - The method expected arguments
54
- * @typeParam TClientMethodReturnType - The method expected return type
55
- *
56
- * @param method - The `@prismicio/client` method name to use
57
- * @param args - The arguments to use with requested method
58
- *
59
- * @returns The composable payload {@link ClientComposableReturnType}
60
- *
61
- * @internal
62
- */
63
- export declare const useStatefulPrismicClientMethod: <TClientMethodName extends keyof Client<any>, TClientMethodArguments extends ClientMethodParameters<TClientMethodName>, TClientMethodReturnType extends UnwrapPromise<ClientMethodReturnType<TClientMethodName>>>(methodName: TClientMethodName, args: TClientMethodArguments) => ClientComposableReturnType<TClientMethodReturnType>;
64
- export {};
1
+ import { Client, ForbiddenError, ParsingError, PrismicError } from "@prismicio/client";
2
+ import { Ref } from "vue";
3
+ import { PrismicClientComposableState, VueUseParameters } from "./types";
4
+ declare type UnwrapPromise<T> = T extends Promise<infer U> ? U : T;
5
+ declare type ClientMethodLike = (...args: any[]) => Promise<any> | any;
6
+ declare type ClientMethods = typeof Client.prototype;
7
+ declare type ClientError = PrismicError<unknown> | ParsingError | ForbiddenError;
8
+ /**
9
+ * @internal
10
+ */
11
+ export declare type ClientMethodParameters<TMethodName extends keyof ClientMethods> = ClientMethods[TMethodName] extends ClientMethodLike ? VueUseParameters<Parameters<ClientMethods[TMethodName]>> : never;
12
+ /**
13
+ * @internal
14
+ */
15
+ export declare type ClientMethodReturnType<TMethodName extends keyof ClientMethods> = ClientMethods[TMethodName] extends ClientMethodLike ? ReturnType<ClientMethods[TMethodName]> : never;
16
+ /**
17
+ * @internal
18
+ */
19
+ export declare type ComposableOnlyParameters = {
20
+ client?: Ref<Client> | Client;
21
+ };
22
+ /**
23
+ * The return type of a `@prismicio/client` Vue composable.
24
+ *
25
+ * @typeParam TData - The expected format of the `data` property of the returned
26
+ * object
27
+ */
28
+ export declare type ClientComposableReturnType<TData = unknown> = {
29
+ /**
30
+ * The current state of the composable's client method call.
31
+ */
32
+ state: Ref<PrismicClientComposableState>;
33
+ /**
34
+ * Data returned by the client.
35
+ */
36
+ data: Ref<TData | null>;
37
+ /**
38
+ * Error returned by the composable's client method call if in an errror
39
+ * state.
40
+ */
41
+ error: Ref<ClientError | Error | null>;
42
+ /**
43
+ * Perform the composable's client method call again.
44
+ */
45
+ refresh: () => Promise<void>;
46
+ };
47
+ /**
48
+ * A low level Vue composable that uses provided method name on plugin or
49
+ * provided client with given arguments. The composable has its own internal
50
+ * state manager to report async status, such as pending or error statuses.
51
+ *
52
+ * @typeParam TClientMethodName - A method name from `@prismicio/client`
53
+ * @typeParam TClientMethodArguments - The method expected arguments
54
+ * @typeParam TClientMethodReturnType - The method expected return type
55
+ *
56
+ * @param method - The `@prismicio/client` method name to use
57
+ * @param args - The arguments to use with requested method
58
+ *
59
+ * @returns The composable payload {@link ClientComposableReturnType}
60
+ *
61
+ * @internal
62
+ */
63
+ export declare const useStatefulPrismicClientMethod: <TClientMethodName extends keyof Client<any>, TClientMethodArguments extends ClientMethodParameters<TClientMethodName>, TClientMethodReturnType extends UnwrapPromise<ClientMethodReturnType<TClientMethodName>>>(methodName: TClientMethodName, args: TClientMethodArguments) => ClientComposableReturnType<TClientMethodReturnType>;
64
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prismicio/vue",
3
- "version": "4.0.3",
3
+ "version": "4.2.0",
4
4
  "description": "Vue plugin, components, and composables to fetch and present Prismic content",
5
5
  "keywords": [
6
6
  "typescript",
@@ -57,38 +57,38 @@
57
57
  "test": "npm run lint && npm run types && npm run unit && npm run build && npm run size"
58
58
  },
59
59
  "dependencies": {
60
- "@prismicio/client": "^7.2.0",
60
+ "@prismicio/client": "^7.4.0",
61
61
  "isomorphic-unfetch": "^3.1.0",
62
- "vue-router": "^4.2.4"
62
+ "vue-router": "^4.3.0"
63
63
  },
64
64
  "devDependencies": {
65
65
  "@prismicio/mock": "^0.3.1",
66
66
  "@size-limit/preset-small-lib": "^8.2.6",
67
- "@trivago/prettier-plugin-sort-imports": "^4.2.0",
68
- "@types/jsdom-global": "^3.0.4",
69
- "@typescript-eslint/eslint-plugin": "^5.61.0",
70
- "@typescript-eslint/parser": "^5.61.0",
71
- "@vitejs/plugin-vue": "^4.3.4",
72
- "@vitest/coverage-v8": "^0.34.3",
73
- "@vue/compiler-sfc": "^3.3.4",
67
+ "@trivago/prettier-plugin-sort-imports": "^4.3.0",
68
+ "@types/jsdom-global": "^3.0.7",
69
+ "@typescript-eslint/eslint-plugin": "^5.62.0",
70
+ "@typescript-eslint/parser": "^5.62.0",
71
+ "@vitejs/plugin-vue": "^4.6.2",
72
+ "@vitest/coverage-v8": "^0.34.6",
73
+ "@vue/compiler-sfc": "^3.4.21",
74
74
  "@vue/eslint-config-typescript": "^11.0.3",
75
- "@vue/test-utils": "^2.4.1",
76
- "eslint": "^8.48.0",
77
- "eslint-config-prettier": "^8.8.0",
75
+ "@vue/test-utils": "^2.4.5",
76
+ "eslint": "^8.57.0",
77
+ "eslint-config-prettier": "^8.10.0",
78
78
  "eslint-plugin-prettier": "^4.2.1",
79
79
  "eslint-plugin-tsdoc": "^0.2.17",
80
- "eslint-plugin-vue": "^9.15.1",
81
- "jsdom": "^22.1.0",
80
+ "eslint-plugin-vue": "^9.24.0",
81
+ "jsdom": "^24.0.0",
82
82
  "jsdom-global": "^3.0.2",
83
83
  "prettier": "^2.8.8",
84
84
  "prettier-plugin-jsdoc": "^0.4.2",
85
85
  "size-limit": "^8.2.6",
86
86
  "standard-version": "^9.5.0",
87
- "typescript": "^5.2.2",
88
- "vite": "^4.4.9",
89
- "vite-plugin-sdk": "^0.1.1",
90
- "vitest": "^0.34.3",
91
- "vue": "^3.3.4"
87
+ "typescript": "^5.4.3",
88
+ "vite": "^4.5.3",
89
+ "vite-plugin-sdk": "^0.1.2",
90
+ "vitest": "^0.34.6",
91
+ "vue": "^3.4.21"
92
92
  },
93
93
  "peerDependencies": {
94
94
  "vue": "^3.0.0"
@@ -41,10 +41,10 @@ type ExtractSliceType<TSlice extends SliceLike> = TSlice extends SliceLikeRestV2
41
41
  *
42
42
  * @typeParam TSliceType - Type name of the Slice.
43
43
  */
44
- export type SliceLikeRestV2<TSliceType extends string = string> = {
45
- slice_type: Slice<TSliceType>["slice_type"];
46
- id?: string;
47
- };
44
+ export type SliceLikeRestV2<TSliceType extends string = string> = Pick<
45
+ Slice<TSliceType>,
46
+ "id" | "slice_type"
47
+ >;
48
48
 
49
49
  /**
50
50
  * The minimum required properties to represent a Prismic Slice from the Prismic
@@ -60,14 +60,23 @@ export type SliceLikeGraphQL<TSliceType extends string = string> = {
60
60
  * The minimum required properties to represent a Prismic Slice for the
61
61
  * `<SliceZone />` component.
62
62
  *
63
- * If using Prismic's REST API, use the `Slice` export from `@prismicio/client`
64
- * for a full interface.
63
+ * If using Prismic's Rest API V2, use the `Slice` export from
64
+ * `@prismicio/client` for a full interface.
65
65
  *
66
66
  * @typeParam TSliceType - Type name of the Slice
67
67
  */
68
- export type SliceLike<TSliceType extends string = string> =
68
+ export type SliceLike<TSliceType extends string = string> = (
69
69
  | SliceLikeRestV2<TSliceType>
70
- | SliceLikeGraphQL<TSliceType>;
70
+ | SliceLikeGraphQL<TSliceType>
71
+ ) & {
72
+ /**
73
+ * If `true`, this Slice has been modified from its original value using a
74
+ * mapper and `@prismicio/client`'s `mapSliceZone()`.
75
+ *
76
+ * @internal
77
+ */
78
+ __mapped?: true;
79
+ };
71
80
 
72
81
  /**
73
82
  * A looser version of the `SliceZone` type from `@prismicio/client` using
@@ -78,8 +87,9 @@ export type SliceLike<TSliceType extends string = string> =
78
87
  *
79
88
  * @typeParam TSlice - The type(s) of slices in the Slice Zone
80
89
  */
81
- export type SliceZoneLike<TSlice extends SliceLike = SliceLike> =
82
- readonly TSlice[];
90
+ export type SliceZoneLike<
91
+ TSlice extends SliceLike = SliceLike & Record<string, unknown>,
92
+ > = readonly TSlice[];
83
93
 
84
94
  /**
85
95
  * Vue props for a component rendering content from a Prismic Slice using the
@@ -90,8 +100,7 @@ export type SliceZoneLike<TSlice extends SliceLike = SliceLike> =
90
100
  * available to all Slice components
91
101
  */
92
102
  export type SliceComponentProps<
93
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
94
- TSlice extends SliceLike = any,
103
+ TSlice extends SliceLike = SliceLike,
95
104
  TContext = unknown,
96
105
  > = {
97
106
  /**
@@ -111,7 +120,9 @@ export type SliceComponentProps<
111
120
  // reference limtiations. If we had another generic to determine the full
112
121
  // union of Slice types, it would include TSlice. This causes TypeScript to
113
122
  // throw a compilation error.
114
- slices: SliceZoneLike<SliceLike>;
123
+ slices: SliceZoneLike<
124
+ TSlice extends SliceLikeGraphQL ? SliceLikeGraphQL : SliceLikeRestV2
125
+ >;
115
126
 
116
127
  /**
117
128
  * Arbitrary data passed to `<SliceZone />` and made available to all Slice
@@ -246,19 +257,29 @@ export const TODOSliceComponent = __PRODUCTION__
246
257
  ? ((() => null) as FunctionalComponent<SliceComponentProps>)
247
258
  : /*#__PURE__*/ (defineComponent({
248
259
  name: "TODOSliceComponent",
249
- props: getSliceComponentProps(),
250
- setup(props) {
260
+ props: [],
261
+ inheritAttrs: false,
262
+ setup(_props, { attrs }) {
251
263
  const type = computed(() =>
252
- "slice_type" in props.slice
253
- ? props.slice.slice_type
254
- : props.slice.type,
264
+ attrs.slice && typeof attrs.slice === "object"
265
+ ? "slice_type" in attrs.slice
266
+ ? (attrs.slice as Record<string, unknown>).slice_type
267
+ : "type" in attrs.slice
268
+ ? (attrs.slice as Record<string, unknown>).type
269
+ : null
270
+ : null,
255
271
  );
256
272
 
257
273
  watchEffect(() => {
258
- console.warn(
259
- `[SliceZone] Could not find a component for Slice type "${type.value}"`,
260
- props.slice,
261
- );
274
+ type.value
275
+ ? console.warn(
276
+ `[SliceZone] Could not find a component for Slice type "${type.value}"`,
277
+ attrs.slice,
278
+ )
279
+ : console.warn(
280
+ "[SliceZone] Could not find a component for mapped Slice",
281
+ attrs,
282
+ );
262
283
  });
263
284
 
264
285
  return () => {
@@ -266,9 +287,13 @@ export const TODOSliceComponent = __PRODUCTION__
266
287
  "section",
267
288
  {
268
289
  "data-slice-zone-todo-component": "",
269
- "data-slice-type": type.value,
290
+ "data-slice-type": type.value ? type.value : null,
270
291
  },
271
- [`Could not find a component for Slice type "${type.value}"`],
292
+ [
293
+ type.value
294
+ ? `Could not find a component for Slice type "${type.value}"`
295
+ : "Could not find a component for mapped Slice",
296
+ ],
272
297
  );
273
298
  };
274
299
  },
@@ -426,6 +451,7 @@ export type SliceZoneProps<TContext = unknown> = {
426
451
  *
427
452
  * @returns The Vue component to render for a Slice.
428
453
  */
454
+ // TODO: Remove in v5 when the `resolver` prop is removed.
429
455
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
430
456
  resolver?: SliceZoneResolver<any, TContext>;
431
457
 
@@ -499,11 +525,21 @@ export const SliceZoneImpl = /*#__PURE__*/ defineComponent({
499
525
  return () => null;
500
526
  }
501
527
 
528
+ // TODO: Remove in v3 when the `resolver` prop is removed.
529
+ if (!__PRODUCTION__) {
530
+ if (props.resolver) {
531
+ console.warn(
532
+ "The `resolver` prop is deprecated. Please replace it with a components map using the `components` prop.",
533
+ );
534
+ }
535
+ }
536
+
502
537
  const { options } = usePrismic();
503
538
 
504
539
  const renderedSlices = computed(() => {
505
540
  return props.slices.map((slice, index) => {
506
- const type = "slice_type" in slice ? slice.slice_type : slice.type;
541
+ const type =
542
+ "slice_type" in slice ? (slice.slice_type as string) : slice.type;
507
543
 
508
544
  let component =
509
545
  props.components && type in props.components
@@ -512,7 +548,7 @@ export const SliceZoneImpl = /*#__PURE__*/ defineComponent({
512
548
  options.components?.sliceZoneDefaultComponent ||
513
549
  TODOSliceComponent;
514
550
 
515
- // TODO: Remove `resolver` in v3 in favor of `components`.
551
+ // TODO: Remove `resolver` in v5 in favor of `components`.
516
552
  if (props.resolver) {
517
553
  const resolvedComponent = props.resolver({
518
554
  slice,
@@ -526,17 +562,23 @@ export const SliceZoneImpl = /*#__PURE__*/ defineComponent({
526
562
  }
527
563
 
528
564
  const key =
529
- "id" in slice && slice.id
565
+ "id" in slice && typeof slice.id === "string"
530
566
  ? slice.id
531
567
  : `${index}-${JSON.stringify(slice)}`;
532
568
 
533
- const p = {
534
- key,
535
- slice,
536
- index,
537
- context: props.context,
538
- slices: props.slices,
539
- };
569
+ let p;
570
+ if (slice.__mapped) {
571
+ const { __mapped, ...mappedProps } = slice;
572
+ p = { key, ...mappedProps };
573
+ } else {
574
+ p = {
575
+ key,
576
+ slice,
577
+ index,
578
+ context: props.context,
579
+ slices: props.slices,
580
+ };
581
+ }
540
582
 
541
583
  return h(simplyResolveComponent(component as ConcreteComponent), p);
542
584
  });
@@ -14,6 +14,7 @@ import {
14
14
  createClient,
15
15
  documentToLinkField,
16
16
  filter,
17
+ isFilled,
17
18
  } from "@prismicio/client";
18
19
  import { App } from "vue";
19
20
 
@@ -120,6 +121,7 @@ export const createPrismic = (options: PrismicPluginOptions): PrismicPlugin => {
120
121
  asImageSrc,
121
122
  asImageWidthSrcSet,
122
123
  asImagePixelDensitySrcSet,
124
+ isFilled,
123
125
  documentToLinkField,
124
126
  };
125
127
 
package/src/types.ts CHANGED
@@ -15,6 +15,7 @@ import type {
15
15
  cookie,
16
16
  documentToLinkField,
17
17
  filter,
18
+ isFilled,
18
19
  } from "@prismicio/client";
19
20
  import type { App, ConcreteComponent, DefineComponent, Raw, Ref } from "vue";
20
21
 
@@ -378,6 +379,12 @@ export type PrismicPluginHelpers = {
378
379
  */
379
380
  asImagePixelDensitySrcSet: typeof asImagePixelDensitySrcSet;
380
381
 
382
+ /**
383
+ * Helpers to determine if a field is filled. This is `@prismicio/client`
384
+ * {@link isFilled} object.
385
+ */
386
+ isFilled: typeof isFilled;
387
+
381
388
  /**
382
389
  * Converts a document into a link field. This is `@prismicio/client`
383
390
  * {@link documentToLinkField} function.