@prismicio/client 7.3.0 → 7.4.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/createClient.cjs +10 -2
- package/dist/createClient.cjs.map +1 -1
- package/dist/createClient.js +10 -2
- package/dist/createClient.js.map +1 -1
- package/dist/errors/PreviewTokenExpired.cjs +7 -0
- package/dist/errors/PreviewTokenExpired.cjs.map +1 -0
- package/dist/errors/PreviewTokenExpired.d.ts +8 -0
- package/dist/errors/PreviewTokenExpired.js +7 -0
- package/dist/errors/PreviewTokenExpired.js.map +1 -0
- package/dist/errors/RepositoryNotFoundError.cjs +7 -0
- package/dist/errors/RepositoryNotFoundError.cjs.map +1 -0
- package/dist/errors/RepositoryNotFoundError.d.ts +3 -0
- package/dist/errors/RepositoryNotFoundError.js +7 -0
- package/dist/errors/RepositoryNotFoundError.js.map +1 -0
- package/dist/helpers/{unstable_mapSliceZone.cjs → mapSliceZone.cjs} +7 -4
- package/dist/helpers/mapSliceZone.cjs.map +1 -0
- package/dist/helpers/{unstable_mapSliceZone.d.ts → mapSliceZone.d.ts} +17 -23
- package/dist/helpers/{unstable_mapSliceZone.js → mapSliceZone.js} +7 -4
- package/dist/helpers/mapSliceZone.js.map +1 -0
- package/dist/index.cjs +8 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +9 -1
- package/dist/index.js +7 -1
- package/dist/index.js.map +1 -1
- package/dist/package.json.cjs +1 -1
- package/dist/package.json.js +1 -1
- package/package.json +1 -1
- package/src/createClient.ts +20 -6
- package/src/errors/PreviewTokenExpired.ts +12 -0
- package/src/errors/RepositoryNotFoundError.ts +5 -0
- package/src/helpers/{unstable_mapSliceZone.ts → mapSliceZone.ts} +70 -57
- package/src/index.ts +10 -1
- package/dist/helpers/unstable_mapSliceZone.cjs.map +0 -1
- package/dist/helpers/unstable_mapSliceZone.js.map +0 -1
|
@@ -11,6 +11,9 @@ type LazyModule<T> = () => Promise<T | { default: T }>;
|
|
|
11
11
|
*/
|
|
12
12
|
type MaybeLazyModule<T> = T | LazyModule<T>;
|
|
13
13
|
|
|
14
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
15
|
+
type AnyFunction = (...args: any[]) => any;
|
|
16
|
+
|
|
14
17
|
/**
|
|
15
18
|
* Returns the type of a `SliceLike` type.
|
|
16
19
|
*
|
|
@@ -24,7 +27,7 @@ type ExtractSliceType<TSlice extends SliceLike> = TSlice extends Slice
|
|
|
24
27
|
|
|
25
28
|
/**
|
|
26
29
|
* The minimum required properties to represent a Prismic Slice from the Prismic
|
|
27
|
-
* Rest API V2 for the `
|
|
30
|
+
* Rest API V2 for the `mapSliceZone()` helper.
|
|
28
31
|
*
|
|
29
32
|
* @typeParam SliceType - Type name of the Slice.
|
|
30
33
|
*/
|
|
@@ -35,7 +38,7 @@ type SliceLikeRestV2<TSliceType extends string = string> = Pick<
|
|
|
35
38
|
|
|
36
39
|
/**
|
|
37
40
|
* The minimum required properties to represent a Prismic Slice from the Prismic
|
|
38
|
-
* GraphQL API for the `
|
|
41
|
+
* GraphQL API for the `mapSliceZone()` helper.
|
|
39
42
|
*
|
|
40
43
|
* @typeParam SliceType - Type name of the Slice.
|
|
41
44
|
*/
|
|
@@ -45,7 +48,7 @@ type SliceLikeGraphQL<TSliceType extends string = string> = {
|
|
|
45
48
|
|
|
46
49
|
/**
|
|
47
50
|
* The minimum required properties to represent a Prismic Slice for the
|
|
48
|
-
* `
|
|
51
|
+
* `mapSliceZone()` helper.
|
|
49
52
|
*
|
|
50
53
|
* If using Prismic's Rest API V2, use the `Slice` export from
|
|
51
54
|
* `@prismicio/client` for a full interface.
|
|
@@ -84,11 +87,11 @@ type MappedSliceLike = {
|
|
|
84
87
|
|
|
85
88
|
/**
|
|
86
89
|
* Arguments for a function mapping content from a Prismic Slice using the
|
|
87
|
-
* `
|
|
90
|
+
* `mapSliceZone()` helper.
|
|
88
91
|
*
|
|
89
92
|
* @typeParam TSlice - The Slice passed as a prop.
|
|
90
|
-
* @typeParam TContext - Arbitrary data passed to `
|
|
91
|
-
*
|
|
93
|
+
* @typeParam TContext - Arbitrary data passed to `mapSliceZone()` and made
|
|
94
|
+
* available to all Slice mappers.
|
|
92
95
|
*/
|
|
93
96
|
type SliceMapperArgs<
|
|
94
97
|
TSlice extends SliceLike = SliceLike,
|
|
@@ -116,8 +119,8 @@ type SliceMapperArgs<
|
|
|
116
119
|
>;
|
|
117
120
|
|
|
118
121
|
/**
|
|
119
|
-
* Arbitrary data passed to `
|
|
120
|
-
*
|
|
122
|
+
* Arbitrary data passed to `mapSliceZone()` and made available to all Slice
|
|
123
|
+
* mappers.
|
|
121
124
|
*/
|
|
122
125
|
context: TContext;
|
|
123
126
|
};
|
|
@@ -125,12 +128,9 @@ type SliceMapperArgs<
|
|
|
125
128
|
/**
|
|
126
129
|
* A record of mappers.
|
|
127
130
|
*/
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
> = {
|
|
132
|
-
[P in ExtractSliceType<TSlice>]: MaybeLazyModule<
|
|
133
|
-
Mapper<
|
|
131
|
+
type SliceMappers<TSlice extends SliceLike = SliceLike, TContext = unknown> = {
|
|
132
|
+
[P in ExtractSliceType<TSlice>]?: MaybeLazyModule<
|
|
133
|
+
SliceMapper<
|
|
134
134
|
Extract<TSlice, SliceLike<P>>,
|
|
135
135
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
136
136
|
any,
|
|
@@ -143,7 +143,7 @@ export type Mappers<
|
|
|
143
143
|
* A function that maps a Slice and its metadata to a modified version. The
|
|
144
144
|
* return value will replace the Slice in the Slice Zone.
|
|
145
145
|
*/
|
|
146
|
-
export type
|
|
146
|
+
export type SliceMapper<
|
|
147
147
|
TSlice extends SliceLike = SliceLike,
|
|
148
148
|
TMappedSlice extends Record<string, unknown> | undefined | void =
|
|
149
149
|
| Record<string, unknown>
|
|
@@ -151,52 +151,59 @@ export type Mapper<
|
|
|
151
151
|
| void,
|
|
152
152
|
TContext = unknown,
|
|
153
153
|
> = (
|
|
154
|
-
args:
|
|
154
|
+
args: SliceMapperArgs<TSlice, TContext>,
|
|
155
155
|
) => TMappedSlice | Promise<TMappedSlice>;
|
|
156
156
|
|
|
157
|
-
/**
|
|
158
|
-
* Arguments provided to a mapper function.
|
|
159
|
-
*/
|
|
160
|
-
export type MapperArgs<
|
|
161
|
-
TSlice extends SliceLike = SliceLike,
|
|
162
|
-
TContext = unknown,
|
|
163
|
-
> = SliceMapperArgs<TSlice, TContext>;
|
|
164
|
-
|
|
165
157
|
/**
|
|
166
158
|
* Unwraps a lazily loaded mapper module.
|
|
167
159
|
*/
|
|
168
|
-
type
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
160
|
+
type ResolveLazySliceMapperModule<
|
|
161
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
162
|
+
TSliceMapper extends SliceMapper<any, any> | LazyModule<SliceMapper>,
|
|
163
|
+
> = TSliceMapper extends LazyModule<SliceMapper>
|
|
164
|
+
? Awaited<ReturnType<TSliceMapper>> extends {
|
|
165
|
+
default: unknown;
|
|
166
|
+
}
|
|
167
|
+
? Awaited<ReturnType<TSliceMapper>>["default"]
|
|
168
|
+
: Awaited<ReturnType<TSliceMapper>>
|
|
169
|
+
: TSliceMapper;
|
|
176
170
|
|
|
177
171
|
/**
|
|
178
172
|
* Transforms a Slice into its mapped version.
|
|
179
173
|
*/
|
|
180
174
|
type MapSliceLike<
|
|
181
|
-
|
|
182
|
-
|
|
175
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
176
|
+
TSliceLike extends SliceLike<any>,
|
|
177
|
+
TSliceMappers extends SliceMappers<
|
|
178
|
+
SliceLike<ExtractSliceType<TSliceLike>>,
|
|
179
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
180
|
+
any
|
|
181
|
+
>,
|
|
183
182
|
> = TSliceLike extends Slice
|
|
184
|
-
? TSliceLike["slice_type"] extends keyof
|
|
185
|
-
?
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
183
|
+
? TSliceLike["slice_type"] extends keyof TSliceMappers
|
|
184
|
+
? TSliceMappers[TSliceLike["slice_type"]] extends AnyFunction
|
|
185
|
+
? SliceLikeRestV2<TSliceLike["slice_type"]> &
|
|
186
|
+
MappedSliceLike &
|
|
187
|
+
Awaited<
|
|
188
|
+
ReturnType<
|
|
189
|
+
ResolveLazySliceMapperModule<
|
|
190
|
+
TSliceMappers[TSliceLike["slice_type"]]
|
|
191
|
+
>
|
|
192
|
+
>
|
|
190
193
|
>
|
|
191
|
-
|
|
194
|
+
: TSliceLike
|
|
192
195
|
: TSliceLike
|
|
193
196
|
: TSliceLike extends SliceLikeGraphQL
|
|
194
|
-
? TSliceLike["type"] extends keyof
|
|
195
|
-
?
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
197
|
+
? TSliceLike["type"] extends keyof TSliceMappers
|
|
198
|
+
? TSliceMappers[TSliceLike["type"]] extends AnyFunction
|
|
199
|
+
? SliceLikeGraphQL<TSliceLike["type"]> &
|
|
200
|
+
MappedSliceLike &
|
|
201
|
+
Awaited<
|
|
202
|
+
ReturnType<
|
|
203
|
+
ResolveLazySliceMapperModule<TSliceMappers[TSliceLike["type"]]>
|
|
204
|
+
>
|
|
205
|
+
>
|
|
206
|
+
: TSliceLike
|
|
200
207
|
: TSliceLike
|
|
201
208
|
: never;
|
|
202
209
|
|
|
@@ -210,31 +217,34 @@ type MapSliceLike<
|
|
|
210
217
|
* @example
|
|
211
218
|
*
|
|
212
219
|
* ```typescript
|
|
213
|
-
* const mappedSliceZone = await
|
|
220
|
+
* const mappedSliceZone = await mapSliceZone(page.data.slices, {
|
|
214
221
|
* code_block: ({ slice }) => ({
|
|
215
222
|
* codeHTML: await highlight(slice.primary.code),
|
|
216
223
|
* }),
|
|
217
224
|
* });
|
|
218
225
|
* ```
|
|
219
|
-
*
|
|
220
|
-
* @experimental Names and implementations may change in the future.
|
|
221
|
-
* `unstable_mapSliceZone()` does not follow SemVer.
|
|
222
226
|
*/
|
|
223
|
-
export function
|
|
227
|
+
export function mapSliceZone<
|
|
224
228
|
TSliceLike extends SliceLike,
|
|
225
|
-
|
|
229
|
+
TSliceMappers extends SliceMappers<TSliceLike, TContext>,
|
|
226
230
|
TContext = unknown,
|
|
227
231
|
>(
|
|
228
232
|
sliceZone: SliceZoneLike<TSliceLike>,
|
|
229
|
-
mappers:
|
|
233
|
+
mappers: TSliceMappers,
|
|
230
234
|
context?: TContext,
|
|
231
|
-
): Promise<
|
|
235
|
+
): Promise<
|
|
236
|
+
MapSliceLike<
|
|
237
|
+
TSliceLike,
|
|
238
|
+
// @ts-expect-error - I don't know how to fix this type
|
|
239
|
+
TSliceMappers
|
|
240
|
+
>[]
|
|
241
|
+
> {
|
|
232
242
|
return Promise.all(
|
|
233
243
|
sliceZone.map(async (slice, index, slices) => {
|
|
234
244
|
const isRestSliceType = "slice_type" in slice;
|
|
235
245
|
const sliceType = isRestSliceType ? slice.slice_type : slice.type;
|
|
236
246
|
|
|
237
|
-
const mapper = mappers[sliceType];
|
|
247
|
+
const mapper = mappers[sliceType as keyof typeof mappers];
|
|
238
248
|
|
|
239
249
|
if (!mapper) {
|
|
240
250
|
return slice;
|
|
@@ -244,7 +254,10 @@ export function unstable_mapSliceZone<
|
|
|
244
254
|
|
|
245
255
|
// `result` may be a mapper function OR a module
|
|
246
256
|
// containing a mapper function.
|
|
247
|
-
let result = await mapper(
|
|
257
|
+
let result = await mapper(
|
|
258
|
+
// @ts-expect-error - I don't know how to fix this type
|
|
259
|
+
mapperArgs,
|
|
260
|
+
);
|
|
248
261
|
|
|
249
262
|
// `result` is a module containing a mapper function,
|
|
250
263
|
// we need to dig out the mapper function. `result`
|
package/src/index.ts
CHANGED
|
@@ -7,6 +7,7 @@ import type {
|
|
|
7
7
|
HTMLRichTextFunctionSerializer,
|
|
8
8
|
HTMLRichTextMapSerializer,
|
|
9
9
|
} from "./helpers/asHTML";
|
|
10
|
+
import { mapSliceZone } from "./helpers/mapSliceZone";
|
|
10
11
|
|
|
11
12
|
import { filter } from "./filter";
|
|
12
13
|
|
|
@@ -69,14 +70,20 @@ export { asHTML } from "./helpers/asHTML";
|
|
|
69
70
|
export { asImageSrc } from "./helpers/asImageSrc";
|
|
70
71
|
export { asImageWidthSrcSet } from "./helpers/asImageWidthSrcSet";
|
|
71
72
|
export { asImagePixelDensitySrcSet } from "./helpers/asImagePixelDensitySrcSet";
|
|
72
|
-
export { unstable_mapSliceZone } from "./helpers/unstable_mapSliceZone";
|
|
73
73
|
export * as isFilled from "./helpers/isFilled";
|
|
74
74
|
|
|
75
|
+
/**
|
|
76
|
+
* @deprecated Renamed to `mapSliceZone`
|
|
77
|
+
*/
|
|
78
|
+
const unstable_mapSliceZone = mapSliceZone;
|
|
79
|
+
export { mapSliceZone, unstable_mapSliceZone };
|
|
80
|
+
|
|
75
81
|
// Conversion helper.
|
|
76
82
|
export { documentToLinkField } from "./helpers/documentToLinkField";
|
|
77
83
|
|
|
78
84
|
export type { LinkResolverFunction } from "./helpers/asLink";
|
|
79
85
|
export type { AsLinkAttrsConfig } from "./helpers/asLinkAttrs";
|
|
86
|
+
export type { SliceMapper } from "./helpers/mapSliceZone";
|
|
80
87
|
|
|
81
88
|
/**
|
|
82
89
|
* @deprecated Renamed to `HTMLRichTextMapSerializer`
|
|
@@ -103,7 +110,9 @@ export { ForbiddenError } from "./errors/ForbiddenError";
|
|
|
103
110
|
export { NotFoundError } from "./errors/NotFoundError";
|
|
104
111
|
export { RefNotFoundError } from "./errors/RefNotFoundError";
|
|
105
112
|
export { RefExpiredError } from "./errors/RefExpiredError";
|
|
113
|
+
export { PreviewTokenExpiredError } from "./errors/PreviewTokenExpired";
|
|
106
114
|
export { ParsingError } from "./errors/ParsingError";
|
|
115
|
+
export { RepositoryNotFoundError } from "./errors/RepositoryNotFoundError";
|
|
107
116
|
|
|
108
117
|
//=============================================================================
|
|
109
118
|
// Types - Types representing Prismic content, models, and API payloads.
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"unstable_mapSliceZone.cjs","sources":["../../../src/helpers/unstable_mapSliceZone.ts"],"sourcesContent":["import { Slice } from \"../types/value/slice\";\n\n/**\n * Convert a value to a lazyily loaded module. This is useful when using\n * functions like `() => import(\"...\")`.\n */\ntype LazyModule<T> = () => Promise<T | { default: T }>;\n\n/**\n * Mark a type as potentially lazy-loaded via a module.\n */\ntype MaybeLazyModule<T> = T | LazyModule<T>;\n\n/**\n * Returns the type of a `SliceLike` type.\n *\n * @typeParam Slice - The Slice from which the type will be extracted.\n */\ntype ExtractSliceType<TSlice extends SliceLike> = TSlice extends Slice\n\t? TSlice[\"slice_type\"]\n\t: TSlice extends SliceLikeGraphQL\n\t? TSlice[\"type\"]\n\t: never;\n\n/**\n * The minimum required properties to represent a Prismic Slice from the Prismic\n * Rest API V2 for the `unstable_mapSliceZone()` helper.\n *\n * @typeParam SliceType - Type name of the Slice.\n */\ntype SliceLikeRestV2<TSliceType extends string = string> = Pick<\n\tSlice<TSliceType>,\n\t\"id\" | \"slice_type\"\n>;\n\n/**\n * The minimum required properties to represent a Prismic Slice from the Prismic\n * GraphQL API for the `unstable_mapSliceZone()` helper.\n *\n * @typeParam SliceType - Type name of the Slice.\n */\ntype SliceLikeGraphQL<TSliceType extends string = string> = {\n\ttype: Slice<TSliceType>[\"slice_type\"];\n};\n\n/**\n * The minimum required properties to represent a Prismic Slice for the\n * `unstable_mapSliceZone()` helper.\n *\n * If using Prismic's Rest API V2, use the `Slice` export from\n * `@prismicio/client` for a full interface.\n *\n * @typeParam SliceType - Type name of the Slice.\n */\ntype SliceLike<TSliceType extends string = string> =\n\t| SliceLikeRestV2<TSliceType>\n\t| SliceLikeGraphQL<TSliceType>;\n\n/**\n * A looser version of the `SliceZone` type from `@prismicio/client` using\n * `SliceLike`.\n *\n * If using Prismic's Rest API V2, use the `SliceZone` export from\n * `@prismicio/client` for the full type.\n *\n * @typeParam TSlice - The type(s) of a Slice in the Slice Zone.\n */\ntype SliceZoneLike<TSlice extends SliceLike = SliceLike> = readonly TSlice[];\n\n/**\n * A set of properties that identify a Slice as having been mapped. Consumers of\n * the mapped Slice Zone can use these properties to detect and specially handle\n * mapped Slices.\n */\ntype MappedSliceLike = {\n\t/**\n\t * If `true`, this Slice has been modified from its original value using a\n\t * mapper.\n\t *\n\t * @internal\n\t */\n\t__mapped: true;\n};\n\n/**\n * Arguments for a function mapping content from a Prismic Slice using the\n * `unstable_mapSliceZone()` helper.\n *\n * @typeParam TSlice - The Slice passed as a prop.\n * @typeParam TContext - Arbitrary data passed to `unstable_mapSliceZone()` and\n * made available to all Slice mappers.\n */\ntype SliceMapperArgs<\n\tTSlice extends SliceLike = SliceLike,\n\tTContext = unknown,\n> = {\n\t/**\n\t * Slice data.\n\t */\n\tslice: TSlice;\n\n\t/**\n\t * The index of the Slice in the Slice Zone.\n\t */\n\tindex: number;\n\n\t/**\n\t * All Slices from the Slice Zone to which the Slice belongs.\n\t */\n\t// TODO: We have to keep this list of Slices general due to circular\n\t// reference limtiations. If we had another generic to determine the full\n\t// union of Slice types, it would include TSlice. This causes TypeScript to\n\t// throw a compilation error.\n\tslices: SliceZoneLike<\n\t\tTSlice extends SliceLikeGraphQL ? SliceLikeGraphQL : SliceLikeRestV2\n\t>;\n\n\t/**\n\t * Arbitrary data passed to `unstable_mapSliceZone()` and made available to\n\t * all Slice mappers.\n\t */\n\tcontext: TContext;\n};\n\n/**\n * A record of mappers.\n */\nexport type Mappers<\n\tTSlice extends SliceLike = SliceLike,\n\tTContext = unknown,\n> = {\n\t[P in ExtractSliceType<TSlice>]: MaybeLazyModule<\n\t\tMapper<\n\t\t\tExtract<TSlice, SliceLike<P>>,\n\t\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\t\t\tany,\n\t\t\tTContext\n\t\t>\n\t>;\n};\n\n/**\n * A function that maps a Slice and its metadata to a modified version. The\n * return value will replace the Slice in the Slice Zone.\n */\nexport type Mapper<\n\tTSlice extends SliceLike = SliceLike,\n\tTMappedSlice extends Record<string, unknown> | undefined | void =\n\t\t| Record<string, unknown>\n\t\t| undefined\n\t\t| void,\n\tTContext = unknown,\n> = (\n\targs: MapperArgs<TSlice, TContext>,\n) => TMappedSlice | Promise<TMappedSlice>;\n\n/**\n * Arguments provided to a mapper function.\n */\nexport type MapperArgs<\n\tTSlice extends SliceLike = SliceLike,\n\tTContext = unknown,\n> = SliceMapperArgs<TSlice, TContext>;\n\n/**\n * Unwraps a lazily loaded mapper module.\n */\ntype ResolveLazyMapperModule<TMapper extends Mapper | LazyModule<Mapper>> =\n\tTMapper extends LazyModule<Mapper>\n\t\t? Awaited<ReturnType<TMapper>> extends {\n\t\t\t\tdefault: unknown;\n\t\t }\n\t\t\t? Awaited<ReturnType<TMapper>>[\"default\"]\n\t\t\t: Awaited<ReturnType<TMapper>>\n\t\t: TMapper;\n\n/**\n * Transforms a Slice into its mapped version.\n */\ntype MapSliceLike<\n\tTSliceLike extends SliceLike,\n\tTMappers extends Mappers,\n> = TSliceLike extends Slice\n\t? TSliceLike[\"slice_type\"] extends keyof TMappers\n\t\t? SliceLikeRestV2<TSliceLike[\"slice_type\"]> &\n\t\t\t\tMappedSliceLike &\n\t\t\t\tAwaited<\n\t\t\t\t\tReturnType<\n\t\t\t\t\t\tResolveLazyMapperModule<TMappers[TSliceLike[\"slice_type\"]]>\n\t\t\t\t\t>\n\t\t\t\t>\n\t\t: TSliceLike\n\t: TSliceLike extends SliceLikeGraphQL\n\t? TSliceLike[\"type\"] extends keyof TMappers\n\t\t? SliceLikeGraphQL<TSliceLike[\"type\"]> &\n\t\t\t\tMappedSliceLike &\n\t\t\t\tAwaited<\n\t\t\t\t\tReturnType<ResolveLazyMapperModule<TMappers[TSliceLike[\"type\"]]>>\n\t\t\t\t>\n\t\t: TSliceLike\n\t: never;\n\n/**\n * Transforms a Slice Zone using a set of mapping functions, one for each type\n * of Slice. Mapping functions can be async.\n *\n * Whenever possible, use this function on the server to minimize client-side\n * processing.\n *\n * @example\n *\n * ```typescript\n * const mappedSliceZone = await unstable_mapSliceZone(page.data.slices, {\n * \tcode_block: ({ slice }) => ({\n * \t\tcodeHTML: await highlight(slice.primary.code),\n * \t}),\n * });\n * ```\n *\n * @experimental Names and implementations may change in the future.\n * `unstable_mapSliceZone()` does not follow SemVer.\n */\nexport function unstable_mapSliceZone<\n\tTSliceLike extends SliceLike,\n\tTMappers extends Mappers,\n\tTContext = unknown,\n>(\n\tsliceZone: SliceZoneLike<TSliceLike>,\n\tmappers: TMappers,\n\tcontext?: TContext,\n): Promise<MapSliceLike<TSliceLike, TMappers>[]> {\n\treturn Promise.all(\n\t\tsliceZone.map(async (slice, index, slices) => {\n\t\t\tconst isRestSliceType = \"slice_type\" in slice;\n\t\t\tconst sliceType = isRestSliceType ? slice.slice_type : slice.type;\n\n\t\t\tconst mapper = mappers[sliceType];\n\n\t\t\tif (!mapper) {\n\t\t\t\treturn slice;\n\t\t\t}\n\n\t\t\tconst mapperArgs = { slice, slices, index, context };\n\n\t\t\t// `result` may be a mapper function OR a module\n\t\t\t// containing a mapper function.\n\t\t\tlet result = await mapper(mapperArgs);\n\n\t\t\t// `result` is a module containing a mapper function,\n\t\t\t// we need to dig out the mapper function. `result`\n\t\t\t// will be reassigned with the mapper function's value.\n\t\t\tif (\n\t\t\t\t// `mapper.length < 1` ensures the given\n\t\t\t\t// function is something of the form:\n\t\t\t\t// `() => import(...)`\n\t\t\t\tmapper.length < 1 &&\n\t\t\t\t(typeof result === \"function\" ||\n\t\t\t\t\t(typeof result === \"object\" && \"default\" in result))\n\t\t\t) {\n\t\t\t\tresult = \"default\" in result ? result.default : result;\n\t\t\t\tresult = await result(mapperArgs);\n\t\t\t}\n\n\t\t\tif (isRestSliceType) {\n\t\t\t\treturn {\n\t\t\t\t\t__mapped: true,\n\t\t\t\t\tid: slice.id,\n\t\t\t\t\tslice_type: sliceType,\n\t\t\t\t\t...result,\n\t\t\t\t};\n\t\t\t} else {\n\t\t\t\treturn {\n\t\t\t\t\t__mapped: true,\n\t\t\t\t\ttype: sliceType,\n\t\t\t\t\t...result,\n\t\t\t\t};\n\t\t\t}\n\t\t}),\n\t);\n}\n"],"names":[],"mappings":";;AA8NgB,SAAA,sBAKf,WACA,SACA,SAAkB;AAElB,SAAO,QAAQ,IACd,UAAU,IAAI,OAAO,OAAO,OAAO,WAAU;AAC5C,UAAM,kBAAkB,gBAAgB;AACxC,UAAM,YAAY,kBAAkB,MAAM,aAAa,MAAM;AAEvD,UAAA,SAAS,QAAQ,SAAS;AAEhC,QAAI,CAAC,QAAQ;AACL,aAAA;AAAA,IACP;AAED,UAAM,aAAa,EAAE,OAAO,QAAQ,OAAO,QAAO;AAI9C,QAAA,SAAS,MAAM,OAAO,UAAU;AAKpC;AAAA;AAAA;AAAA;AAAA,MAIC,OAAO,SAAS,MACf,OAAO,WAAW,cACjB,OAAO,WAAW,YAAY,aAAa;AAAA,MAC5C;AACQ,eAAA,aAAa,SAAS,OAAO,UAAU;AACvC,eAAA,MAAM,OAAO,UAAU;AAAA,IAChC;AAED,QAAI,iBAAiB;AACb,aAAA;AAAA,QACN,UAAU;AAAA,QACV,IAAI,MAAM;AAAA,QACV,YAAY;AAAA,QACZ,GAAG;AAAA,MAAA;AAAA,WAEE;AACC,aAAA;AAAA,QACN,UAAU;AAAA,QACV,MAAM;AAAA,QACN,GAAG;AAAA,MAAA;AAAA,IAEJ;AAAA,EACD,CAAA,CAAC;AAEJ;;"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"unstable_mapSliceZone.js","sources":["../../../src/helpers/unstable_mapSliceZone.ts"],"sourcesContent":["import { Slice } from \"../types/value/slice\";\n\n/**\n * Convert a value to a lazyily loaded module. This is useful when using\n * functions like `() => import(\"...\")`.\n */\ntype LazyModule<T> = () => Promise<T | { default: T }>;\n\n/**\n * Mark a type as potentially lazy-loaded via a module.\n */\ntype MaybeLazyModule<T> = T | LazyModule<T>;\n\n/**\n * Returns the type of a `SliceLike` type.\n *\n * @typeParam Slice - The Slice from which the type will be extracted.\n */\ntype ExtractSliceType<TSlice extends SliceLike> = TSlice extends Slice\n\t? TSlice[\"slice_type\"]\n\t: TSlice extends SliceLikeGraphQL\n\t? TSlice[\"type\"]\n\t: never;\n\n/**\n * The minimum required properties to represent a Prismic Slice from the Prismic\n * Rest API V2 for the `unstable_mapSliceZone()` helper.\n *\n * @typeParam SliceType - Type name of the Slice.\n */\ntype SliceLikeRestV2<TSliceType extends string = string> = Pick<\n\tSlice<TSliceType>,\n\t\"id\" | \"slice_type\"\n>;\n\n/**\n * The minimum required properties to represent a Prismic Slice from the Prismic\n * GraphQL API for the `unstable_mapSliceZone()` helper.\n *\n * @typeParam SliceType - Type name of the Slice.\n */\ntype SliceLikeGraphQL<TSliceType extends string = string> = {\n\ttype: Slice<TSliceType>[\"slice_type\"];\n};\n\n/**\n * The minimum required properties to represent a Prismic Slice for the\n * `unstable_mapSliceZone()` helper.\n *\n * If using Prismic's Rest API V2, use the `Slice` export from\n * `@prismicio/client` for a full interface.\n *\n * @typeParam SliceType - Type name of the Slice.\n */\ntype SliceLike<TSliceType extends string = string> =\n\t| SliceLikeRestV2<TSliceType>\n\t| SliceLikeGraphQL<TSliceType>;\n\n/**\n * A looser version of the `SliceZone` type from `@prismicio/client` using\n * `SliceLike`.\n *\n * If using Prismic's Rest API V2, use the `SliceZone` export from\n * `@prismicio/client` for the full type.\n *\n * @typeParam TSlice - The type(s) of a Slice in the Slice Zone.\n */\ntype SliceZoneLike<TSlice extends SliceLike = SliceLike> = readonly TSlice[];\n\n/**\n * A set of properties that identify a Slice as having been mapped. Consumers of\n * the mapped Slice Zone can use these properties to detect and specially handle\n * mapped Slices.\n */\ntype MappedSliceLike = {\n\t/**\n\t * If `true`, this Slice has been modified from its original value using a\n\t * mapper.\n\t *\n\t * @internal\n\t */\n\t__mapped: true;\n};\n\n/**\n * Arguments for a function mapping content from a Prismic Slice using the\n * `unstable_mapSliceZone()` helper.\n *\n * @typeParam TSlice - The Slice passed as a prop.\n * @typeParam TContext - Arbitrary data passed to `unstable_mapSliceZone()` and\n * made available to all Slice mappers.\n */\ntype SliceMapperArgs<\n\tTSlice extends SliceLike = SliceLike,\n\tTContext = unknown,\n> = {\n\t/**\n\t * Slice data.\n\t */\n\tslice: TSlice;\n\n\t/**\n\t * The index of the Slice in the Slice Zone.\n\t */\n\tindex: number;\n\n\t/**\n\t * All Slices from the Slice Zone to which the Slice belongs.\n\t */\n\t// TODO: We have to keep this list of Slices general due to circular\n\t// reference limtiations. If we had another generic to determine the full\n\t// union of Slice types, it would include TSlice. This causes TypeScript to\n\t// throw a compilation error.\n\tslices: SliceZoneLike<\n\t\tTSlice extends SliceLikeGraphQL ? SliceLikeGraphQL : SliceLikeRestV2\n\t>;\n\n\t/**\n\t * Arbitrary data passed to `unstable_mapSliceZone()` and made available to\n\t * all Slice mappers.\n\t */\n\tcontext: TContext;\n};\n\n/**\n * A record of mappers.\n */\nexport type Mappers<\n\tTSlice extends SliceLike = SliceLike,\n\tTContext = unknown,\n> = {\n\t[P in ExtractSliceType<TSlice>]: MaybeLazyModule<\n\t\tMapper<\n\t\t\tExtract<TSlice, SliceLike<P>>,\n\t\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\t\t\tany,\n\t\t\tTContext\n\t\t>\n\t>;\n};\n\n/**\n * A function that maps a Slice and its metadata to a modified version. The\n * return value will replace the Slice in the Slice Zone.\n */\nexport type Mapper<\n\tTSlice extends SliceLike = SliceLike,\n\tTMappedSlice extends Record<string, unknown> | undefined | void =\n\t\t| Record<string, unknown>\n\t\t| undefined\n\t\t| void,\n\tTContext = unknown,\n> = (\n\targs: MapperArgs<TSlice, TContext>,\n) => TMappedSlice | Promise<TMappedSlice>;\n\n/**\n * Arguments provided to a mapper function.\n */\nexport type MapperArgs<\n\tTSlice extends SliceLike = SliceLike,\n\tTContext = unknown,\n> = SliceMapperArgs<TSlice, TContext>;\n\n/**\n * Unwraps a lazily loaded mapper module.\n */\ntype ResolveLazyMapperModule<TMapper extends Mapper | LazyModule<Mapper>> =\n\tTMapper extends LazyModule<Mapper>\n\t\t? Awaited<ReturnType<TMapper>> extends {\n\t\t\t\tdefault: unknown;\n\t\t }\n\t\t\t? Awaited<ReturnType<TMapper>>[\"default\"]\n\t\t\t: Awaited<ReturnType<TMapper>>\n\t\t: TMapper;\n\n/**\n * Transforms a Slice into its mapped version.\n */\ntype MapSliceLike<\n\tTSliceLike extends SliceLike,\n\tTMappers extends Mappers,\n> = TSliceLike extends Slice\n\t? TSliceLike[\"slice_type\"] extends keyof TMappers\n\t\t? SliceLikeRestV2<TSliceLike[\"slice_type\"]> &\n\t\t\t\tMappedSliceLike &\n\t\t\t\tAwaited<\n\t\t\t\t\tReturnType<\n\t\t\t\t\t\tResolveLazyMapperModule<TMappers[TSliceLike[\"slice_type\"]]>\n\t\t\t\t\t>\n\t\t\t\t>\n\t\t: TSliceLike\n\t: TSliceLike extends SliceLikeGraphQL\n\t? TSliceLike[\"type\"] extends keyof TMappers\n\t\t? SliceLikeGraphQL<TSliceLike[\"type\"]> &\n\t\t\t\tMappedSliceLike &\n\t\t\t\tAwaited<\n\t\t\t\t\tReturnType<ResolveLazyMapperModule<TMappers[TSliceLike[\"type\"]]>>\n\t\t\t\t>\n\t\t: TSliceLike\n\t: never;\n\n/**\n * Transforms a Slice Zone using a set of mapping functions, one for each type\n * of Slice. Mapping functions can be async.\n *\n * Whenever possible, use this function on the server to minimize client-side\n * processing.\n *\n * @example\n *\n * ```typescript\n * const mappedSliceZone = await unstable_mapSliceZone(page.data.slices, {\n * \tcode_block: ({ slice }) => ({\n * \t\tcodeHTML: await highlight(slice.primary.code),\n * \t}),\n * });\n * ```\n *\n * @experimental Names and implementations may change in the future.\n * `unstable_mapSliceZone()` does not follow SemVer.\n */\nexport function unstable_mapSliceZone<\n\tTSliceLike extends SliceLike,\n\tTMappers extends Mappers,\n\tTContext = unknown,\n>(\n\tsliceZone: SliceZoneLike<TSliceLike>,\n\tmappers: TMappers,\n\tcontext?: TContext,\n): Promise<MapSliceLike<TSliceLike, TMappers>[]> {\n\treturn Promise.all(\n\t\tsliceZone.map(async (slice, index, slices) => {\n\t\t\tconst isRestSliceType = \"slice_type\" in slice;\n\t\t\tconst sliceType = isRestSliceType ? slice.slice_type : slice.type;\n\n\t\t\tconst mapper = mappers[sliceType];\n\n\t\t\tif (!mapper) {\n\t\t\t\treturn slice;\n\t\t\t}\n\n\t\t\tconst mapperArgs = { slice, slices, index, context };\n\n\t\t\t// `result` may be a mapper function OR a module\n\t\t\t// containing a mapper function.\n\t\t\tlet result = await mapper(mapperArgs);\n\n\t\t\t// `result` is a module containing a mapper function,\n\t\t\t// we need to dig out the mapper function. `result`\n\t\t\t// will be reassigned with the mapper function's value.\n\t\t\tif (\n\t\t\t\t// `mapper.length < 1` ensures the given\n\t\t\t\t// function is something of the form:\n\t\t\t\t// `() => import(...)`\n\t\t\t\tmapper.length < 1 &&\n\t\t\t\t(typeof result === \"function\" ||\n\t\t\t\t\t(typeof result === \"object\" && \"default\" in result))\n\t\t\t) {\n\t\t\t\tresult = \"default\" in result ? result.default : result;\n\t\t\t\tresult = await result(mapperArgs);\n\t\t\t}\n\n\t\t\tif (isRestSliceType) {\n\t\t\t\treturn {\n\t\t\t\t\t__mapped: true,\n\t\t\t\t\tid: slice.id,\n\t\t\t\t\tslice_type: sliceType,\n\t\t\t\t\t...result,\n\t\t\t\t};\n\t\t\t} else {\n\t\t\t\treturn {\n\t\t\t\t\t__mapped: true,\n\t\t\t\t\ttype: sliceType,\n\t\t\t\t\t...result,\n\t\t\t\t};\n\t\t\t}\n\t\t}),\n\t);\n}\n"],"names":[],"mappings":"AA8NgB,SAAA,sBAKf,WACA,SACA,SAAkB;AAElB,SAAO,QAAQ,IACd,UAAU,IAAI,OAAO,OAAO,OAAO,WAAU;AAC5C,UAAM,kBAAkB,gBAAgB;AACxC,UAAM,YAAY,kBAAkB,MAAM,aAAa,MAAM;AAEvD,UAAA,SAAS,QAAQ,SAAS;AAEhC,QAAI,CAAC,QAAQ;AACL,aAAA;AAAA,IACP;AAED,UAAM,aAAa,EAAE,OAAO,QAAQ,OAAO,QAAO;AAI9C,QAAA,SAAS,MAAM,OAAO,UAAU;AAKpC;AAAA;AAAA;AAAA;AAAA,MAIC,OAAO,SAAS,MACf,OAAO,WAAW,cACjB,OAAO,WAAW,YAAY,aAAa;AAAA,MAC5C;AACQ,eAAA,aAAa,SAAS,OAAO,UAAU;AACvC,eAAA,MAAM,OAAO,UAAU;AAAA,IAChC;AAED,QAAI,iBAAiB;AACb,aAAA;AAAA,QACN,UAAU;AAAA,QACV,IAAI,MAAM;AAAA,QACV,YAAY;AAAA,QACZ,GAAG;AAAA,MAAA;AAAA,WAEE;AACC,aAAA;AAAA,QACN,UAAU;AAAA,QACV,MAAM;AAAA,QACN,GAAG;AAAA,MAAA;AAAA,IAEJ;AAAA,EACD,CAAA,CAAC;AAEJ;"}
|