@@ -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 `unstable_mapSliceZone ()` helper.
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 `unstable_mapSliceZone ()` helper.
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
- * `unstable_mapSliceZone ()` helper.
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
- * `unstable_mapSliceZone ()` helper.
90
+ * `mapSliceZone ()` helper.
88
91
*
89
92
* @typeParam TSlice - The Slice passed as a prop.
90
- * @typeParam TContext - Arbitrary data passed to `unstable_mapSliceZone ()` and
91
- * made available to all Slice mappers.
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,21 +119,18 @@ type SliceMapperArgs<
116
119
> ;
117
120
118
121
/**
119
- * Arbitrary data passed to `unstable_mapSliceZone ()` and made available to
120
- * all Slice mappers.
122
+ * Arbitrary data passed to `mapSliceZone ()` and made available to all Slice
123
+ * mappers.
121
124
*/
122
125
context : TContext ;
123
126
} ;
124
127
125
128
/**
126
129
* A record of mappers.
127
130
*/
128
- export type Mappers <
129
- TSlice extends SliceLike = SliceLike ,
130
- TContext = unknown ,
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,60 +143,67 @@ 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 Mapper <
146
+ export type SliceMapper <
147
147
TSlice extends SliceLike = SliceLike ,
148
148
TMappedSlice extends Record < string , unknown > | undefined | void =
149
149
| Record < string , unknown >
150
150
| undefined
151
151
| void ,
152
152
TContext = unknown ,
153
153
> = (
154
- args : MapperArgs < TSlice , TContext > ,
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 ResolveLazyMapperModule < TMapper extends Mapper | LazyModule < Mapper > > =
169
- TMapper extends LazyModule < Mapper >
170
- ? Awaited < ReturnType < TMapper > > extends {
171
- default : unknown ;
172
- }
173
- ? Awaited < ReturnType < TMapper > > [ "default" ]
174
- : Awaited < ReturnType < TMapper > >
175
- : TMapper ;
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
- TSliceLike extends SliceLike ,
182
- TMappers extends Mappers ,
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 TMappers
185
- ? SliceLikeRestV2 < TSliceLike [ "slice_type" ] > &
186
- MappedSliceLike &
187
- Awaited <
188
- ReturnType <
189
- ResolveLazyMapperModule < TMappers [ TSliceLike [ "slice_type" ] ] >
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 TMappers
195
- ? SliceLikeGraphQL < TSliceLike [ "type" ] > &
196
- MappedSliceLike &
197
- Awaited <
198
- ReturnType < ResolveLazyMapperModule < TMappers [ TSliceLike [ "type" ] ] > >
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 unstable_mapSliceZone (page.data.slices, {
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 unstable_mapSliceZone <
227
+ export function mapSliceZone <
224
228
TSliceLike extends SliceLike ,
225
- TMappers extends Mappers ,
229
+ TSliceMappers extends SliceMappers < TSliceLike , TContext > ,
226
230
TContext = unknown ,
227
231
> (
228
232
sliceZone : SliceZoneLike < TSliceLike > ,
229
- mappers : TMappers ,
233
+ mappers : TSliceMappers ,
230
234
context ?: TContext ,
231
- ) : Promise < MapSliceLike < TSliceLike , TMappers > [ ] > {
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 ( mapperArgs ) ;
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`
0 commit comments