@prismicio/react 2.0.0-beta.2 → 2.0.0-beta.6

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,5 +1,4 @@
1
- import * as prismic from "@prismicio/client";
2
- import * as prismicT from "@prismicio/types";
1
+ import type * as prismicT from "@prismicio/types";
3
2
 
4
3
  import {
5
4
  ClientHookReturnType,
@@ -8,23 +7,24 @@ import {
8
7
  useStatefulPrismicClientMethod,
9
8
  } from "./useStatefulPrismicClientMethod";
10
9
 
11
- const proto = prismic.Client.prototype;
12
-
13
10
  /**
14
11
  * A hook that queries content from the Prismic repository.
15
12
  *
16
13
  * @remarks
17
14
  * An additional `@prismicio/client` instance can be provided at `params.client`.
15
+ *
18
16
  * @typeParam TDocument - Type of Prismic documents returned
17
+ *
19
18
  * @param params - Parameters to filter, sort, and paginate results
20
19
  *
21
20
  * @returns The composable payload {@link ClientHookReturnType}
21
+ *
22
22
  * @see Underlying `@prismicio/client` method {@link proto.get}
23
23
  */
24
24
  export const usePrismicDocuments = <TDocument extends prismicT.PrismicDocument>(
25
25
  ...args: [params?: ClientMethodParameters<"get">[0] & HookOnlyParameters]
26
26
  ): ClientHookReturnType<prismicT.Query<TDocument>> =>
27
- useStatefulPrismicClientMethod(proto.get, args);
27
+ useStatefulPrismicClientMethod("get", args);
28
28
 
29
29
  /**
30
30
  * A hook that queries content from the Prismic repository and returns only the
@@ -32,10 +32,13 @@ export const usePrismicDocuments = <TDocument extends prismicT.PrismicDocument>(
32
32
  *
33
33
  * @remarks
34
34
  * An additional `@prismicio/client` instance can be provided at `params.client`.
35
+ *
35
36
  * @typeParam TDocument - Type of the Prismic document returned
37
+ *
36
38
  * @param params - Parameters to filter, sort, and paginate results
37
39
  *
38
40
  * @returns The composable payload {@link ClientHookReturnType}
41
+ *
39
42
  * @see Underlying `@prismicio/client` method {@link proto.getFirst}
40
43
  */
41
44
  export const useFirstPrismicDocument = <
@@ -43,7 +46,7 @@ export const useFirstPrismicDocument = <
43
46
  >(
44
47
  ...args: [params?: ClientMethodParameters<"getFirst">[0] & HookOnlyParameters]
45
48
  ): ClientHookReturnType<TDocument> =>
46
- useStatefulPrismicClientMethod(proto.getFirst, args);
49
+ useStatefulPrismicClientMethod("getFirst", args);
47
50
 
48
51
  /**
49
52
  * A hook that queries content from the Prismic repository and returns all
@@ -51,29 +54,38 @@ export const useFirstPrismicDocument = <
51
54
  *
52
55
  * @remarks
53
56
  * An additional `@prismicio/client` instance can be provided at `params.client`.
57
+ *
54
58
  * @typeParam TDocument - Type of Prismic documents returned
59
+ *
55
60
  * @param params - Parameters to filter and sort results
56
61
  *
57
62
  * @returns The composable payload {@link ClientHookReturnType}
63
+ *
58
64
  * @see Underlying `@prismicio/client` method {@link proto.getAll}
59
65
  */
60
- export const useAllPrismicDocuments = <
66
+ export const useAllPrismicDocumentsDangerously = <
61
67
  TDocument extends prismicT.PrismicDocument,
62
68
  >(
63
- ...args: [params?: ClientMethodParameters<"getAll">[0] & HookOnlyParameters]
69
+ ...args: [
70
+ params?: ClientMethodParameters<"dangerouslyGetAll">[0] &
71
+ HookOnlyParameters,
72
+ ]
64
73
  ): ClientHookReturnType<TDocument[]> =>
65
- useStatefulPrismicClientMethod(proto.getAll, args);
74
+ useStatefulPrismicClientMethod("dangerouslyGetAll", args);
66
75
 
67
76
  /**
68
77
  * A hook that queries a document from the Prismic repository with a specific ID.
69
78
  *
70
79
  * @remarks
71
80
  * An additional `@prismicio/client` instance can be provided at `params.client`.
81
+ *
72
82
  * @typeParam TDocument - Type of the Prismic document returned
83
+ *
73
84
  * @param id - ID of the document
74
85
  * @param params - Parameters to filter, sort, and paginate results
75
86
  *
76
87
  * @returns The composable payload {@link ClientHookReturnType}
88
+ *
77
89
  * @see Underlying `@prismicio/client` method {@link proto.getByID}
78
90
  */
79
91
  export const usePrismicDocumentByID = <
@@ -84,18 +96,21 @@ export const usePrismicDocumentByID = <
84
96
  params?: ClientMethodParameters<"getByID">[1] & HookOnlyParameters,
85
97
  ]
86
98
  ): ClientHookReturnType<TDocument> =>
87
- useStatefulPrismicClientMethod(proto.getByID, args);
99
+ useStatefulPrismicClientMethod("getByID", args);
88
100
 
89
101
  /**
90
102
  * A hook that queries documents from the Prismic repository with specific IDs.
91
103
  *
92
104
  * @remarks
93
105
  * An additional `@prismicio/client` instance can be provided at `params.client`.
106
+ *
94
107
  * @typeParam TDocument - Type of Prismic documents returned
108
+ *
95
109
  * @param ids - A list of document IDs
96
110
  * @param params - Parameters to filter, sort, and paginate results
97
111
  *
98
112
  * @returns The composable payload {@link ClientHookReturnType}
113
+ *
99
114
  * @see Underlying `@prismicio/client` method {@link proto.getByIDs}
100
115
  */
101
116
  export const usePrismicDocumentsByIDs = <
@@ -106,18 +121,21 @@ export const usePrismicDocumentsByIDs = <
106
121
  params?: ClientMethodParameters<"getByIDs">[1] & HookOnlyParameters,
107
122
  ]
108
123
  ): ClientHookReturnType<prismicT.Query<TDocument>> =>
109
- useStatefulPrismicClientMethod(proto.getByIDs, args);
124
+ useStatefulPrismicClientMethod("getByIDs", args);
110
125
 
111
126
  /**
112
127
  * A hook that queries all documents from the Prismic repository with specific IDs.
113
128
  *
114
129
  * @remarks
115
130
  * An additional `@prismicio/client` instance can be provided at `params.client`.
131
+ *
116
132
  * @typeParam TDocument - Type of Prismic documents returned
133
+ *
117
134
  * @param ids - A list of document IDs
118
135
  * @param params - Parameters to filter and sort results
119
136
  *
120
137
  * @returns The composable payload {@link ClientHookReturnType}
138
+ *
121
139
  * @see Underlying `@prismicio/client` method {@link proto.getAllByIDs}
122
140
  */
123
141
  export const useAllPrismicDocumentsByIDs = <
@@ -128,7 +146,7 @@ export const useAllPrismicDocumentsByIDs = <
128
146
  params?: ClientMethodParameters<"getAllByIDs">[1] & HookOnlyParameters,
129
147
  ]
130
148
  ): ClientHookReturnType<TDocument[]> =>
131
- useStatefulPrismicClientMethod(proto.getAllByIDs, args);
149
+ useStatefulPrismicClientMethod("getAllByIDs", args);
132
150
 
133
151
  /**
134
152
  * A hook that queries a document from the Prismic repository with a specific
@@ -136,12 +154,15 @@ export const useAllPrismicDocumentsByIDs = <
136
154
  *
137
155
  * @remarks
138
156
  * An additional `@prismicio/client` instance can be provided at `params.client`.
157
+ *
139
158
  * @typeParam TDocument - Type of the Prismic document returned
159
+ *
140
160
  * @param documentType - The API ID of the document's Custom Type
141
161
  * @param uid - UID of the document
142
162
  * @param params - Parameters to filter, sort, and paginate results
143
163
  *
144
164
  * @returns The composable payload {@link ClientHookReturnType}
165
+ *
145
166
  * @see Underlying `@prismicio/client` method {@link proto.getByUID}
146
167
  */
147
168
  export const usePrismicDocumentByUID = <
@@ -153,7 +174,7 @@ export const usePrismicDocumentByUID = <
153
174
  params?: ClientMethodParameters<"getByUID">[2] & HookOnlyParameters,
154
175
  ]
155
176
  ): ClientHookReturnType<TDocument> =>
156
- useStatefulPrismicClientMethod(proto.getByUID, args);
177
+ useStatefulPrismicClientMethod("getByUID", args);
157
178
 
158
179
  /**
159
180
  * A hook that queries documents from the Prismic repository with specific UIDs
@@ -161,12 +182,15 @@ export const usePrismicDocumentByUID = <
161
182
  *
162
183
  * @remarks
163
184
  * An additional `@prismicio/client` instance can be provided at `params.client`.
185
+ *
164
186
  * @typeParam TDocument - Type of the Prismic document returned
187
+ *
165
188
  * @param documentType - The API ID of the document's Custom Type
166
189
  * @param uids - A list of document UIDs.
167
190
  * @param params - Parameters to filter, sort, and paginate results
168
191
  *
169
192
  * @returns The composable payload {@link ClientHookReturnType}
193
+ *
170
194
  * @see Underlying `@prismicio/client` method {@link proto.getByUID}
171
195
  */
172
196
  export const usePrismicDocumentsByUIDs = <
@@ -178,7 +202,7 @@ export const usePrismicDocumentsByUIDs = <
178
202
  params?: ClientMethodParameters<"getByUIDs">[2] & HookOnlyParameters,
179
203
  ]
180
204
  ): ClientHookReturnType<prismicT.Query<TDocument>> =>
181
- useStatefulPrismicClientMethod(proto.getByUIDs, args);
205
+ useStatefulPrismicClientMethod("getByUIDs", args);
182
206
 
183
207
  /**
184
208
  * A hook that queries all documents from the Prismic repository with specific
@@ -186,12 +210,15 @@ export const usePrismicDocumentsByUIDs = <
186
210
  *
187
211
  * @remarks
188
212
  * An additional `@prismicio/client` instance can be provided at `params.client`.
213
+ *
189
214
  * @typeParam TDocument - Type of the Prismic document returned
215
+ *
190
216
  * @param documentType - The API ID of the document's Custom Type
191
217
  * @param uids - A list of document UIDs.
192
218
  * @param params - Parameters to filter, sort, and paginate results
193
219
  *
194
220
  * @returns The composable payload {@link ClientHookReturnType}
221
+ *
195
222
  * @see Underlying `@prismicio/client` method {@link proto.getByUID}
196
223
  */
197
224
  export const useAllPrismicDocumentsByUIDs = <
@@ -203,7 +230,7 @@ export const useAllPrismicDocumentsByUIDs = <
203
230
  params?: ClientMethodParameters<"getByUIDs">[2] & HookOnlyParameters,
204
231
  ]
205
232
  ): ClientHookReturnType<TDocument[]> =>
206
- useStatefulPrismicClientMethod(proto.getAllByUIDs, args);
233
+ useStatefulPrismicClientMethod("getAllByUIDs", args);
207
234
 
208
235
  /**
209
236
  * A hook that queries a singleton document from the Prismic repository for a
@@ -211,11 +238,14 @@ export const useAllPrismicDocumentsByUIDs = <
211
238
  *
212
239
  * @remarks
213
240
  * An additional `@prismicio/client` instance can be provided at `params.client`.
241
+ *
214
242
  * @typeParam TDocument - Type of the Prismic document returned
243
+ *
215
244
  * @param documentType - The API ID of the singleton Custom Type
216
245
  * @param params - Parameters to filter, sort, and paginate results
217
246
  *
218
247
  * @returns The composable payload {@link ClientHookReturnType}
248
+ *
219
249
  * @see Underlying `@prismicio/client` method {@link proto.getSingle}
220
250
  */
221
251
  export const useSinglePrismicDocument = <
@@ -226,18 +256,21 @@ export const useSinglePrismicDocument = <
226
256
  params?: ClientMethodParameters<"getSingle">[1] & HookOnlyParameters,
227
257
  ]
228
258
  ): ClientHookReturnType<TDocument> =>
229
- useStatefulPrismicClientMethod(proto.getSingle, args);
259
+ useStatefulPrismicClientMethod("getSingle", args);
230
260
 
231
261
  /**
232
262
  * A hook that queries documents from the Prismic repository for a specific Custom Type.
233
263
  *
234
264
  * @remarks
235
265
  * An additional `@prismicio/client` instance can be provided at `params.client`.
266
+ *
236
267
  * @typeParam TDocument - Type of Prismic documents returned
268
+ *
237
269
  * @param documentType - The API ID of the Custom Type
238
270
  * @param params - Parameters to filter, sort, and paginate results
239
271
  *
240
272
  * @returns The composable payload {@link ClientHookReturnType}
273
+ *
241
274
  * @see Underlying `@prismicio/client` method {@link proto.getByType}
242
275
  */
243
276
  export const usePrismicDocumentsByType = <
@@ -248,7 +281,7 @@ export const usePrismicDocumentsByType = <
248
281
  params?: ClientMethodParameters<"getByType">[1] & HookOnlyParameters,
249
282
  ]
250
283
  ): ClientHookReturnType<prismicT.Query<TDocument>> =>
251
- useStatefulPrismicClientMethod(proto.getByType, args);
284
+ useStatefulPrismicClientMethod("getByType", args);
252
285
 
253
286
  /**
254
287
  * A hook that queries all documents from the Prismic repository for a specific
@@ -256,11 +289,14 @@ export const usePrismicDocumentsByType = <
256
289
  *
257
290
  * @remarks
258
291
  * An additional `@prismicio/client` instance can be provided at `params.client`.
292
+ *
259
293
  * @typeParam TDocument - Type of Prismic documents returned
294
+ *
260
295
  * @param documentType - The API ID of the Custom Type
261
296
  * @param params - Parameters to filter and sort results
262
297
  *
263
298
  * @returns The composable payload {@link ClientHookReturnType}
299
+ *
264
300
  * @see Underlying `@prismicio/client` method {@link proto.getAllByType}
265
301
  */
266
302
  export const useAllPrismicDocumentsByType = <
@@ -271,18 +307,21 @@ export const useAllPrismicDocumentsByType = <
271
307
  params?: ClientMethodParameters<"getAllByType">[1] & HookOnlyParameters,
272
308
  ]
273
309
  ): ClientHookReturnType<TDocument[]> =>
274
- useStatefulPrismicClientMethod(proto.getAllByType, args);
310
+ useStatefulPrismicClientMethod("getAllByType", args);
275
311
 
276
312
  /**
277
313
  * A hook that queries documents from the Prismic repository with a specific tag.
278
314
  *
279
315
  * @remarks
280
316
  * An additional `@prismicio/client` instance can be provided at `params.client`.
317
+ *
281
318
  * @typeParam TDocument - Type of Prismic documents returned
319
+ *
282
320
  * @param tag - The tag that must be included on a document
283
321
  * @param params - Parameters to filter, sort, and paginate results
284
322
  *
285
323
  * @returns The composable payload {@link ClientHookReturnType}
324
+ *
286
325
  * @see Underlying `@prismicio/client` method {@link proto.getByTag}
287
326
  */
288
327
  export const usePrismicDocumentsByTag = <
@@ -293,18 +332,21 @@ export const usePrismicDocumentsByTag = <
293
332
  params?: ClientMethodParameters<"getByTag">[1] & HookOnlyParameters,
294
333
  ]
295
334
  ): ClientHookReturnType<prismicT.Query<TDocument>> =>
296
- useStatefulPrismicClientMethod(proto.getByTag, args);
335
+ useStatefulPrismicClientMethod("getByTag", args);
297
336
 
298
337
  /**
299
338
  * A hook that queries all documents from the Prismic repository with a specific tag.
300
339
  *
301
340
  * @remarks
302
341
  * An additional `@prismicio/client` instance can be provided at `params.client`.
342
+ *
303
343
  * @typeParam TDocument - Type of Prismic documents returned
344
+ *
304
345
  * @param tag - The tag that must be included on a document
305
346
  * @param params - Parameters to filter and sort results
306
347
  *
307
348
  * @returns The composable payload {@link ClientHookReturnType}
349
+ *
308
350
  * @see Underlying `@prismicio/client` method {@link proto.getAllByTag}
309
351
  */
310
352
  export const useAllPrismicDocumentsByTag = <
@@ -315,7 +357,7 @@ export const useAllPrismicDocumentsByTag = <
315
357
  params?: ClientMethodParameters<"getAllByTag">[1] & HookOnlyParameters,
316
358
  ]
317
359
  ): ClientHookReturnType<TDocument[]> =>
318
- useStatefulPrismicClientMethod(proto.getAllByTag, args);
360
+ useStatefulPrismicClientMethod("getAllByTag", args);
319
361
 
320
362
  /**
321
363
  * A hook that queries documents from the Prismic repository with specific tags.
@@ -323,11 +365,14 @@ export const useAllPrismicDocumentsByTag = <
323
365
  *
324
366
  * @remarks
325
367
  * An additional `@prismicio/client` instance can be provided at `params.client`.
368
+ *
326
369
  * @typeParam TDocument - Type of Prismic documents returned
370
+ *
327
371
  * @param tags - A list of tags that must be included on a document
328
372
  * @param params - Parameters to filter, sort, and paginate results
329
373
  *
330
374
  * @returns The composable payload {@link ClientHookReturnType}
375
+ *
331
376
  * @see Underlying `@prismicio/client` method {@link proto.getByTags}
332
377
  */
333
378
  export const usePrismicDocumentsBySomeTags = <
@@ -338,7 +383,7 @@ export const usePrismicDocumentsBySomeTags = <
338
383
  params?: ClientMethodParameters<"getBySomeTags">[1] & HookOnlyParameters,
339
384
  ]
340
385
  ): ClientHookReturnType<prismicT.Query<TDocument>> =>
341
- useStatefulPrismicClientMethod(proto.getBySomeTags, args);
386
+ useStatefulPrismicClientMethod("getBySomeTags", args);
342
387
 
343
388
  /**
344
389
  * A hook that queries all documents from the Prismic repository with specific
@@ -346,11 +391,14 @@ export const usePrismicDocumentsBySomeTags = <
346
391
  *
347
392
  * @remarks
348
393
  * An additional `@prismicio/client` instance can be provided at `params.client`.
394
+ *
349
395
  * @typeParam TDocument - Type of Prismic documents returned
396
+ *
350
397
  * @param tags - A list of tags that must be included on a document
351
398
  * @param params - Parameters to filter and sort results
352
399
  *
353
400
  * @returns The composable payload {@link ClientHookReturnType}
401
+ *
354
402
  * @see Underlying `@prismicio/client` method {@link proto.getAllByTags}
355
403
  */
356
404
  export const useAllPrismicDocumentsBySomeTags = <
@@ -361,7 +409,7 @@ export const useAllPrismicDocumentsBySomeTags = <
361
409
  params?: ClientMethodParameters<"getAllBySomeTags">[1] & HookOnlyParameters,
362
410
  ]
363
411
  ): ClientHookReturnType<TDocument[]> =>
364
- useStatefulPrismicClientMethod(proto.getAllBySomeTags, args);
412
+ useStatefulPrismicClientMethod("getAllBySomeTags", args);
365
413
 
366
414
  /**
367
415
  * A hook that queries documents from the Prismic repository with specific tags.
@@ -369,11 +417,14 @@ export const useAllPrismicDocumentsBySomeTags = <
369
417
  *
370
418
  * @remarks
371
419
  * An additional `@prismicio/client` instance can be provided at `params.client`.
420
+ *
372
421
  * @typeParam TDocument - Type of Prismic documents returned
422
+ *
373
423
  * @param tags - A list of tags that must be included on a document
374
424
  * @param params - Parameters to filter, sort, and paginate results
375
425
  *
376
426
  * @returns The composable payload {@link ClientHookReturnType}
427
+ *
377
428
  * @see Underlying `@prismicio/client` method {@link proto.getByTags}
378
429
  */
379
430
  export const usePrismicDocumentsByEveryTag = <
@@ -384,7 +435,7 @@ export const usePrismicDocumentsByEveryTag = <
384
435
  params?: ClientMethodParameters<"getByEveryTag">[1] & HookOnlyParameters,
385
436
  ]
386
437
  ): ClientHookReturnType<prismicT.Query<TDocument>> =>
387
- useStatefulPrismicClientMethod(proto.getByEveryTag, args);
438
+ useStatefulPrismicClientMethod("getByEveryTag", args);
388
439
 
389
440
  /**
390
441
  * A hook that queries all documents from the Prismic repository with specific
@@ -392,11 +443,14 @@ export const usePrismicDocumentsByEveryTag = <
392
443
  *
393
444
  * @remarks
394
445
  * An additional `@prismicio/client` instance can be provided at `params.client`.
446
+ *
395
447
  * @typeParam TDocument - Type of Prismic documents returned
448
+ *
396
449
  * @param tags - A list of tags that must be included on a document
397
450
  * @param params - Parameters to filter and sort results
398
451
  *
399
452
  * @returns The composable payload {@link ClientHookReturnType}
453
+ *
400
454
  * @see Underlying `@prismicio/client` method {@link proto.getAllByTags}
401
455
  */
402
456
  export const useAllPrismicDocumentsByEveryTag = <
@@ -407,4 +461,4 @@ export const useAllPrismicDocumentsByEveryTag = <
407
461
  params?: ClientMethodParameters<"getAllByEveryTag">[1] & HookOnlyParameters,
408
462
  ]
409
463
  ): ClientHookReturnType<TDocument[]> =>
410
- useStatefulPrismicClientMethod(proto.getAllByEveryTag, args);
464
+ useStatefulPrismicClientMethod("getAllByEveryTag", args);
package/src/index.ts CHANGED
@@ -25,13 +25,17 @@ export type {
25
25
  SliceZoneComponents,
26
26
  SliceZoneLike,
27
27
  SliceZoneProps,
28
+ SliceZoneResolver,
28
29
  } from "./SliceZone";
29
30
 
30
31
  export { PrismicToolbar } from "./PrismicToolbar";
31
32
  export type { PrismicToolbarProps } from "./PrismicToolbar";
32
33
 
34
+ export { usePrismicPreviewResolver } from "./usePrismicPreviewResolver";
35
+ export type { UsePrismicPreviewResolverArgs } from "./usePrismicPreviewResolver";
36
+
33
37
  export {
34
- useAllPrismicDocuments,
38
+ useAllPrismicDocumentsDangerously,
35
39
  useAllPrismicDocumentsByEveryTag,
36
40
  useAllPrismicDocumentsByIDs,
37
41
  useAllPrismicDocumentsBySomeTags,
@@ -49,7 +53,7 @@ export {
49
53
  usePrismicDocumentsByType,
50
54
  usePrismicDocumentsByUIDs,
51
55
  useSinglePrismicDocument,
52
- } from "./hooks";
56
+ } from "./clientHooks";
53
57
 
54
58
  export type {
55
59
  JSXMapSerializer,
@@ -1,4 +1,4 @@
1
- import * as prismic from "@prismicio/client";
1
+ import type * as prismic from "@prismicio/client";
2
2
 
3
3
  import { invariant } from "./lib/invariant";
4
4
 
@@ -0,0 +1,88 @@
1
+ import type * as prismic from "@prismicio/client";
2
+
3
+ import * as React from "react";
4
+
5
+ import {
6
+ ClientHookReturnType,
7
+ useStatefulPrismicClientMethod,
8
+ } from "./useStatefulPrismicClientMethod";
9
+
10
+ export type UsePrismicPreviewResolverArgs = {
11
+ /**
12
+ * An optional `@prismicio/client` instance to override the Client provided to
13
+ * `<PrismicProvider>`
14
+ */
15
+ client?: prismic.Client;
16
+
17
+ /**
18
+ * A function that maps a Prismic document to a URL within your app.
19
+ */
20
+ linkResolver?: Parameters<
21
+ prismic.Client["resolvePreviewURL"]
22
+ >[0]["linkResolver"];
23
+
24
+ /**
25
+ * A fallback URL if the Link Resolver does not return a value.
26
+ */
27
+ defaultURL?: Parameters<prismic.Client["resolvePreviewURL"]>[0]["defaultURL"];
28
+
29
+ /**
30
+ * The preview token (also known as a ref) that will be used to query preview
31
+ * content from the Prismic repository.
32
+ */
33
+ previewToken?: Parameters<
34
+ prismic.Client["resolvePreviewURL"]
35
+ >[0]["previewToken"];
36
+
37
+ /**
38
+ * The previewed document that will be used to determine the destination URL.
39
+ */
40
+ documentID?: Parameters<prismic.Client["resolvePreviewURL"]>[0]["documentID"];
41
+
42
+ /**
43
+ * A function to automatically navigate to the resolved URL. If a function is
44
+ * not provded, `usePreviewResolver` will not navigate to the URL.
45
+ *
46
+ * @param url - The resolved preview URL.
47
+ */
48
+ navigate?: (url: string) => unknown;
49
+ };
50
+
51
+ /**
52
+ * Resolve a preview session's URL. The resolved URL can be used to redirect to
53
+ * the previewed document.
54
+ *
55
+ * If a `navigate` function is provided, the hook will automatically navigate to
56
+ * the previewed document's URL.
57
+ *
58
+ * @param args - Arguments to configure how a URL is resolved.
59
+ *
60
+ * @returns A tuple containing the resolved URL and the hook's state.
61
+ */
62
+ export const usePrismicPreviewResolver = (
63
+ args: UsePrismicPreviewResolverArgs = {},
64
+ ): ClientHookReturnType<string> => {
65
+ const result = useStatefulPrismicClientMethod(
66
+ "resolvePreviewURL",
67
+ [
68
+ {
69
+ linkResolver: args.linkResolver,
70
+ defaultURL: args.defaultURL || "/",
71
+ previewToken: args.previewToken,
72
+ documentID: args.documentID,
73
+ },
74
+ ],
75
+ args.client,
76
+ );
77
+
78
+ const [resolvedURL] = result;
79
+ const { navigate } = args;
80
+
81
+ React.useEffect(() => {
82
+ if (resolvedURL && navigate) {
83
+ navigate(resolvedURL);
84
+ }
85
+ }, [resolvedURL, navigate]);
86
+
87
+ return result;
88
+ };
@@ -1,5 +1,6 @@
1
+ import type * as prismic from "@prismicio/client";
2
+
1
3
  import * as React from "react";
2
- import * as prismic from "@prismicio/client";
3
4
 
4
5
  import { PrismicClientHookState } from "./types";
5
6
  import { usePrismicClient } from "./usePrismicClient";
@@ -46,12 +47,25 @@ type UnwrapPromise<T> = T extends Promise<infer U> ? U : T;
46
47
 
47
48
  type ClientPrototype = typeof prismic.Client.prototype;
48
49
 
49
- export type ClientMethodParameters<MethodName extends keyof ClientPrototype> =
50
+ type ClientMethod<MethodName extends keyof ClientPrototype> =
50
51
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
51
52
  ClientPrototype[MethodName] extends (...args: any[]) => any
52
- ? Parameters<ClientPrototype[MethodName]>
53
+ ? ClientPrototype[MethodName]
53
54
  : never;
54
55
 
56
+ type ClientMethodName = keyof {
57
+ [P in keyof prismic.Client as prismic.Client[P] extends (
58
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
59
+ ...args: any[]
60
+ ) => // eslint-disable-next-line @typescript-eslint/no-explicit-any
61
+ Promise<any>
62
+ ? P
63
+ : never]: unknown;
64
+ };
65
+
66
+ export type ClientMethodParameters<MethodName extends keyof ClientPrototype> =
67
+ Parameters<ClientMethod<MethodName>>;
68
+
55
69
  export type HookOnlyParameters = {
56
70
  client?: prismic.Client;
57
71
  skip?: boolean;
@@ -111,45 +125,49 @@ export type ClientHookReturnType<TData = unknown> = [
111
125
  * `@prismicio/client` instance. The created hook has its own internal state
112
126
  * manager to report async status, such as pending or error statuses.
113
127
  *
114
- * @param method - The `@prismicio/client` method to which hook arguments will
115
- * be forwarded.
128
+ * @param methodName - The `@prismicio/client` method to which hook arguments
129
+ * will be forwarded.
116
130
  *
117
131
  * @returns A new React hook configured for the provided method.
132
+ *
118
133
  * @internal
119
134
  */
120
135
  export const useStatefulPrismicClientMethod = <
121
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
122
- TMethod extends (...args: any[]) => Promise<any>,
123
- TArgs extends Parameters<TMethod>,
124
- TData extends UnwrapPromise<ReturnType<TMethod>>,
136
+ TMethodName extends ClientMethodName,
137
+ TArgs extends Parameters<ClientMethod<TMethodName>>,
138
+ TData extends UnwrapPromise<ReturnType<ClientMethod<TMethodName>>>,
125
139
  >(
126
- method: TMethod,
140
+ methodName: TMethodName,
127
141
  args: TArgs,
142
+ explicitClient?: prismic.Client,
128
143
  ): ClientHookReturnType<TData> => {
129
144
  const lastArg = args[args.length - 1];
130
145
  const {
131
- client: explicitClient,
146
+ client: lastArgExplicitClient,
132
147
  skip,
133
148
  ...params
134
149
  } = isParams(lastArg) ? lastArg : ({} as HookOnlyParameters);
135
150
  const argsWithoutParams = isParams(lastArg) ? args.slice(0, -1) : args;
136
151
 
137
- const client = usePrismicClient(explicitClient);
152
+ const client = usePrismicClient(explicitClient || lastArgExplicitClient);
138
153
 
139
154
  const [state, dispatch] = React.useReducer<
140
- React.Reducer<
141
- StateMachineState<UnwrapPromise<ReturnType<TMethod>>>,
142
- StateMachineAction<UnwrapPromise<ReturnType<TMethod>>>
143
- >
155
+ React.Reducer<StateMachineState<TData>, StateMachineAction<TData>>
144
156
  >(reducer, initialState);
145
157
 
146
158
  React.useEffect(
147
159
  () => {
148
160
  if (state.state === "idle" && !skip) {
149
161
  dispatch(["start"]);
150
- method
151
- .call(client, ...argsWithoutParams, params)
152
- .then((result) => dispatch(["succeed", result]))
162
+
163
+ client[methodName]
164
+ .call(
165
+ client,
166
+ // @ts-expect-error - Merging method arg types is too complex
167
+ ...argsWithoutParams,
168
+ params,
169
+ )
170
+ .then((result) => dispatch(["succeed", result as TData]))
153
171
  .catch((error) => dispatch(["fail", error]));
154
172
  }
155
173
  },
@@ -159,7 +177,7 @@ export const useStatefulPrismicClientMethod = <
159
177
  client,
160
178
  state.state,
161
179
  skip,
162
- method,
180
+ methodName,
163
181
  // eslint-disable-next-line react-hooks/exhaustive-deps
164
182
  ...args.slice(0, -1),
165
183
  // eslint-disable-next-line react-hooks/exhaustive-deps
@@ -168,7 +186,13 @@ export const useStatefulPrismicClientMethod = <
168
186
  );
169
187
 
170
188
  return React.useMemo(
171
- () => [state.data, { state: state.state, error: state.error }],
189
+ () => [
190
+ state.data,
191
+ {
192
+ state: state.state,
193
+ error: state.error,
194
+ },
195
+ ],
172
196
  [state],
173
197
  );
174
198
  };