@prismicio/client 7.0.1 → 7.1.1
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/buildQueryURL.cjs.map +1 -1
- package/dist/buildQueryURL.d.ts +1 -0
- package/dist/buildQueryURL.js.map +1 -1
- package/dist/createClient.cjs +8 -1
- package/dist/createClient.cjs.map +1 -1
- package/dist/createClient.d.ts +8 -0
- package/dist/createClient.js +8 -1
- package/dist/createClient.js.map +1 -1
- package/dist/filter.cjs.map +1 -1
- package/dist/filter.js.map +1 -1
- package/dist/getGraphQLEndpoint.cjs.map +1 -1
- package/dist/getGraphQLEndpoint.js.map +1 -1
- package/dist/getRepositoryEndpoint.cjs.map +1 -1
- package/dist/getRepositoryEndpoint.js.map +1 -1
- package/dist/getRepositoryName.cjs.map +1 -1
- package/dist/getRepositoryName.d.ts +1 -0
- package/dist/getRepositoryName.js.map +1 -1
- package/dist/getToolbarSrc.cjs.map +1 -1
- package/dist/getToolbarSrc.js.map +1 -1
- package/dist/helpers/asHTML.cjs.map +1 -1
- package/dist/helpers/asHTML.d.ts +0 -6
- package/dist/helpers/asHTML.js.map +1 -1
- package/dist/helpers/asImagePixelDensitySrcSet.cjs.map +1 -1
- package/dist/helpers/asImagePixelDensitySrcSet.d.ts +2 -1
- package/dist/helpers/asImagePixelDensitySrcSet.js.map +1 -1
- package/dist/helpers/asImageSrc.cjs.map +1 -1
- package/dist/helpers/asImageSrc.d.ts +2 -1
- package/dist/helpers/asImageSrc.js.map +1 -1
- package/dist/helpers/asImageWidthSrcSet.cjs.map +1 -1
- package/dist/helpers/asImageWidthSrcSet.d.ts +2 -1
- package/dist/helpers/asImageWidthSrcSet.js.map +1 -1
- package/dist/helpers/asLink.cjs.map +1 -1
- package/dist/helpers/asLink.d.ts +3 -4
- package/dist/helpers/asLink.js.map +1 -1
- package/dist/helpers/asLinkAttrs.cjs.map +1 -1
- package/dist/helpers/asLinkAttrs.d.ts +3 -1
- package/dist/helpers/asLinkAttrs.js.map +1 -1
- package/dist/helpers/asText.cjs.map +1 -1
- package/dist/helpers/asText.d.ts +0 -4
- package/dist/helpers/asText.js.map +1 -1
- package/dist/helpers/isFilled.d.ts +13 -10
- package/dist/helpers/unstable_mapSliceZone.cjs +39 -0
- package/dist/helpers/unstable_mapSliceZone.cjs.map +1 -0
- package/dist/helpers/unstable_mapSliceZone.d.ts +142 -0
- package/dist/helpers/unstable_mapSliceZone.js +39 -0
- package/dist/helpers/unstable_mapSliceZone.js.map +1 -0
- package/dist/index.cjs +2 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/isRepositoryEndpoint.cjs.map +1 -1
- package/dist/isRepositoryEndpoint.js.map +1 -1
- package/dist/lib/devMsg.cjs.map +1 -1
- package/dist/lib/devMsg.js.map +1 -1
- package/dist/lib/serializerHelpers.cjs.map +1 -1
- package/dist/lib/serializerHelpers.js.map +1 -1
- package/dist/package.json.cjs +1 -1
- package/dist/package.json.js +1 -1
- package/dist/types/api/repository.d.ts +6 -1
- package/dist/types/model/customType.d.ts +6 -0
- package/dist/types/value/document.d.ts +3 -2
- package/package.json +20 -20
- package/src/buildQueryURL.ts +1 -0
- package/src/createClient.ts +9 -1
- package/src/getRepositoryName.ts +1 -0
- package/src/helpers/asHTML.ts +0 -6
- package/src/helpers/asImagePixelDensitySrcSet.ts +1 -0
- package/src/helpers/asImageSrc.ts +1 -0
- package/src/helpers/asImageWidthSrcSet.ts +1 -0
- package/src/helpers/asLink.ts +3 -4
- package/src/helpers/asLinkAttrs.ts +2 -0
- package/src/helpers/asText.ts +0 -4
- package/src/helpers/unstable_mapSliceZone.ts +280 -0
- package/src/index.ts +1 -0
- package/src/types/api/repository.ts +6 -2
- package/src/types/model/customType.ts +7 -0
- package/src/types/value/document.ts +3 -2
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
import { Slice } from "../types/value/slice";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Convert a value to a lazyily loaded module. This is useful when using
|
|
5
|
+
* functions like `() => import("...")`.
|
|
6
|
+
*/
|
|
7
|
+
type LazyModule<T> = () => Promise<T | { default: T }>;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Mark a type as potentially lazy-loaded via a module.
|
|
11
|
+
*/
|
|
12
|
+
type MaybeLazyModule<T> = T | LazyModule<T>;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Returns the type of a `SliceLike` type.
|
|
16
|
+
*
|
|
17
|
+
* @typeParam Slice - The Slice from which the type will be extracted.
|
|
18
|
+
*/
|
|
19
|
+
type ExtractSliceType<TSlice extends SliceLike> = TSlice extends Slice
|
|
20
|
+
? TSlice["slice_type"]
|
|
21
|
+
: TSlice extends SliceLikeGraphQL
|
|
22
|
+
? TSlice["type"]
|
|
23
|
+
: never;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The minimum required properties to represent a Prismic Slice from the Prismic
|
|
27
|
+
* Rest API V2 for the `unstable_mapSliceZone()` helper.
|
|
28
|
+
*
|
|
29
|
+
* @typeParam SliceType - Type name of the Slice.
|
|
30
|
+
*/
|
|
31
|
+
type SliceLikeRestV2<TSliceType extends string = string> = Pick<
|
|
32
|
+
Slice<TSliceType>,
|
|
33
|
+
"id" | "slice_type"
|
|
34
|
+
>;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* The minimum required properties to represent a Prismic Slice from the Prismic
|
|
38
|
+
* GraphQL API for the `unstable_mapSliceZone()` helper.
|
|
39
|
+
*
|
|
40
|
+
* @typeParam SliceType - Type name of the Slice.
|
|
41
|
+
*/
|
|
42
|
+
type SliceLikeGraphQL<TSliceType extends string = string> = {
|
|
43
|
+
type: Slice<TSliceType>["slice_type"];
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The minimum required properties to represent a Prismic Slice for the
|
|
48
|
+
* `unstable_mapSliceZone()` helper.
|
|
49
|
+
*
|
|
50
|
+
* If using Prismic's Rest API V2, use the `Slice` export from
|
|
51
|
+
* `@prismicio/client` for a full interface.
|
|
52
|
+
*
|
|
53
|
+
* @typeParam SliceType - Type name of the Slice.
|
|
54
|
+
*/
|
|
55
|
+
type SliceLike<TSliceType extends string = string> =
|
|
56
|
+
| SliceLikeRestV2<TSliceType>
|
|
57
|
+
| SliceLikeGraphQL<TSliceType>;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* A looser version of the `SliceZone` type from `@prismicio/client` using
|
|
61
|
+
* `SliceLike`.
|
|
62
|
+
*
|
|
63
|
+
* If using Prismic's Rest API V2, use the `SliceZone` export from
|
|
64
|
+
* `@prismicio/client` for the full type.
|
|
65
|
+
*
|
|
66
|
+
* @typeParam TSlice - The type(s) of a Slice in the Slice Zone.
|
|
67
|
+
*/
|
|
68
|
+
type SliceZoneLike<TSlice extends SliceLike = SliceLike> = readonly TSlice[];
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* A set of properties that identify a Slice as having been mapped. Consumers of
|
|
72
|
+
* the mapped Slice Zone can use these properties to detect and specially handle
|
|
73
|
+
* mapped Slices.
|
|
74
|
+
*/
|
|
75
|
+
type MappedSliceLike = {
|
|
76
|
+
/**
|
|
77
|
+
* If `true`, this Slice has been modified from its original value using a
|
|
78
|
+
* mapper.
|
|
79
|
+
*
|
|
80
|
+
* @internal
|
|
81
|
+
*/
|
|
82
|
+
__mapped: true;
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Arguments for a function mapping content from a Prismic Slice using the
|
|
87
|
+
* `unstable_mapSliceZone()` helper.
|
|
88
|
+
*
|
|
89
|
+
* @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.
|
|
92
|
+
*/
|
|
93
|
+
type SliceMapperArgs<
|
|
94
|
+
TSlice extends SliceLike = SliceLike,
|
|
95
|
+
TContext = unknown,
|
|
96
|
+
> = {
|
|
97
|
+
/**
|
|
98
|
+
* Slice data.
|
|
99
|
+
*/
|
|
100
|
+
slice: TSlice;
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* The index of the Slice in the Slice Zone.
|
|
104
|
+
*/
|
|
105
|
+
index: number;
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* All Slices from the Slice Zone to which the Slice belongs.
|
|
109
|
+
*/
|
|
110
|
+
// TODO: We have to keep this list of Slices general due to circular
|
|
111
|
+
// reference limtiations. If we had another generic to determine the full
|
|
112
|
+
// union of Slice types, it would include TSlice. This causes TypeScript to
|
|
113
|
+
// throw a compilation error.
|
|
114
|
+
slices: SliceZoneLike<
|
|
115
|
+
TSlice extends SliceLikeGraphQL ? SliceLikeGraphQL : SliceLikeRestV2
|
|
116
|
+
>;
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Arbitrary data passed to `unstable_mapSliceZone()` and made available to
|
|
120
|
+
* all Slice mappers.
|
|
121
|
+
*/
|
|
122
|
+
context: TContext;
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* A record of mappers.
|
|
127
|
+
*/
|
|
128
|
+
export type Mappers<
|
|
129
|
+
TSlice extends SliceLike = SliceLike,
|
|
130
|
+
TContext = unknown,
|
|
131
|
+
> = {
|
|
132
|
+
[P in ExtractSliceType<TSlice>]: MaybeLazyModule<
|
|
133
|
+
Mapper<
|
|
134
|
+
Extract<TSlice, SliceLike<P>>,
|
|
135
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
136
|
+
any,
|
|
137
|
+
TContext
|
|
138
|
+
>
|
|
139
|
+
>;
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* A function that maps a Slice and its metadata to a modified version. The
|
|
144
|
+
* return value will replace the Slice in the Slice Zone.
|
|
145
|
+
*/
|
|
146
|
+
export type Mapper<
|
|
147
|
+
TSlice extends SliceLike = SliceLike,
|
|
148
|
+
TMappedSlice extends Record<string, unknown> | undefined | void =
|
|
149
|
+
| Record<string, unknown>
|
|
150
|
+
| undefined
|
|
151
|
+
| void,
|
|
152
|
+
TContext = unknown,
|
|
153
|
+
> = (
|
|
154
|
+
args: MapperArgs<TSlice, TContext>,
|
|
155
|
+
) => TMappedSlice | Promise<TMappedSlice>;
|
|
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
|
+
/**
|
|
166
|
+
* Unwraps a lazily loaded mapper module.
|
|
167
|
+
*/
|
|
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;
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Transforms a Slice into its mapped version.
|
|
179
|
+
*/
|
|
180
|
+
type MapSliceLike<
|
|
181
|
+
TSliceLike extends SliceLike,
|
|
182
|
+
TMappers extends Mappers,
|
|
183
|
+
> = 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"]]>
|
|
190
|
+
>
|
|
191
|
+
>
|
|
192
|
+
: TSliceLike
|
|
193
|
+
: TSliceLike extends SliceLikeGraphQL
|
|
194
|
+
? TSliceLike["type"] extends keyof TMappers
|
|
195
|
+
? SliceLikeGraphQL<TSliceLike["type"]> &
|
|
196
|
+
MappedSliceLike &
|
|
197
|
+
Awaited<
|
|
198
|
+
ReturnType<ResolveLazyMapperModule<TMappers[TSliceLike["type"]]>>
|
|
199
|
+
>
|
|
200
|
+
: TSliceLike
|
|
201
|
+
: never;
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Transforms a Slice Zone using a set of mapping functions, one for each type
|
|
205
|
+
* of Slice. Mapping functions can be async.
|
|
206
|
+
*
|
|
207
|
+
* Whenever possible, use this function on the server to minimize client-side
|
|
208
|
+
* processing.
|
|
209
|
+
*
|
|
210
|
+
* @example
|
|
211
|
+
*
|
|
212
|
+
* ```typescript
|
|
213
|
+
* const mappedSliceZone = await unstable_mapSliceZone(page.data.slices, {
|
|
214
|
+
* code_block: ({ slice }) => ({
|
|
215
|
+
* codeHTML: await highlight(slice.primary.code),
|
|
216
|
+
* }),
|
|
217
|
+
* });
|
|
218
|
+
* ```
|
|
219
|
+
*
|
|
220
|
+
* @experimental Names and implementations may change in the future.
|
|
221
|
+
* `unstable_mapSliceZone()` does not follow SemVer.
|
|
222
|
+
*/
|
|
223
|
+
export function unstable_mapSliceZone<
|
|
224
|
+
TSliceLike extends SliceLike,
|
|
225
|
+
TMappers extends Mappers,
|
|
226
|
+
TContext = unknown,
|
|
227
|
+
>(
|
|
228
|
+
sliceZone: SliceZoneLike<TSliceLike>,
|
|
229
|
+
mappers: TMappers,
|
|
230
|
+
context?: TContext,
|
|
231
|
+
): Promise<MapSliceLike<TSliceLike, TMappers>[]> {
|
|
232
|
+
return Promise.all(
|
|
233
|
+
sliceZone.map(async (slice, index, slices) => {
|
|
234
|
+
const isRestSliceType = "slice_type" in slice;
|
|
235
|
+
const sliceType = isRestSliceType ? slice.slice_type : slice.type;
|
|
236
|
+
|
|
237
|
+
const mapper = mappers[sliceType];
|
|
238
|
+
|
|
239
|
+
if (!mapper) {
|
|
240
|
+
return slice;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const mapperArgs = { slice, slices, index, context };
|
|
244
|
+
|
|
245
|
+
// `result` may be a mapper function OR a module
|
|
246
|
+
// containing a mapper function.
|
|
247
|
+
let result = await mapper(mapperArgs);
|
|
248
|
+
|
|
249
|
+
// `result` is a module containing a mapper function,
|
|
250
|
+
// we need to dig out the mapper function. `result`
|
|
251
|
+
// will be reassigned with the mapper function's value.
|
|
252
|
+
if (
|
|
253
|
+
// `mapper.length < 1` ensures the given
|
|
254
|
+
// function is something of the form:
|
|
255
|
+
// `() => import(...)`
|
|
256
|
+
mapper.length < 1 &&
|
|
257
|
+
(typeof result === "function" ||
|
|
258
|
+
(typeof result === "object" && "default" in result))
|
|
259
|
+
) {
|
|
260
|
+
result = "default" in result ? result.default : result;
|
|
261
|
+
result = await result(mapperArgs);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
if (isRestSliceType) {
|
|
265
|
+
return {
|
|
266
|
+
__mapped: true,
|
|
267
|
+
id: slice.id,
|
|
268
|
+
slice_type: sliceType,
|
|
269
|
+
...result,
|
|
270
|
+
};
|
|
271
|
+
} else {
|
|
272
|
+
return {
|
|
273
|
+
__mapped: true,
|
|
274
|
+
type: sliceType,
|
|
275
|
+
...result,
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
}),
|
|
279
|
+
);
|
|
280
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -68,6 +68,7 @@ export { asHTML } from "./helpers/asHTML";
|
|
|
68
68
|
export { asImageSrc } from "./helpers/asImageSrc";
|
|
69
69
|
export { asImageWidthSrcSet } from "./helpers/asImageWidthSrcSet";
|
|
70
70
|
export { asImagePixelDensitySrcSet } from "./helpers/asImagePixelDensitySrcSet";
|
|
71
|
+
export { unstable_mapSliceZone } from "./helpers/unstable_mapSliceZone";
|
|
71
72
|
export * as isFilled from "./helpers/isFilled";
|
|
72
73
|
|
|
73
74
|
// Conversion helper.
|
|
@@ -34,7 +34,9 @@ export interface Repository {
|
|
|
34
34
|
types: Record<string, string>;
|
|
35
35
|
|
|
36
36
|
/**
|
|
37
|
-
*
|
|
37
|
+
* @deprecated Tags are only available in the forms object.
|
|
38
|
+
*
|
|
39
|
+
* @see More details in the blog post "A change to how the Prismic API handles tags": {@link https://prismic.io/blog/a-change-to-how-the-prismic-api-handles-tags}
|
|
38
40
|
*/
|
|
39
41
|
tags: string[];
|
|
40
42
|
|
|
@@ -50,7 +52,9 @@ export interface Repository {
|
|
|
50
52
|
*/
|
|
51
53
|
oauth_initiate: string;
|
|
52
54
|
|
|
53
|
-
|
|
55
|
+
/**
|
|
56
|
+
* The token used for the OAuth process for the repository.
|
|
57
|
+
*/
|
|
54
58
|
oauth_token: string;
|
|
55
59
|
|
|
56
60
|
/**
|
|
@@ -21,6 +21,13 @@ export interface CustomTypeModel<
|
|
|
21
21
|
// TODO: Revert to `label?: string | null` if `label` can be partial in: https://github.com/prismicio/prismic-types-internal/blob/HEAD/src/customtypes/CustomType.ts#L39
|
|
22
22
|
label: string | null | undefined;
|
|
23
23
|
|
|
24
|
+
/**
|
|
25
|
+
* The format of the custom type model.
|
|
26
|
+
*
|
|
27
|
+
* Fallback to "custom" if undefined.
|
|
28
|
+
*/
|
|
29
|
+
format?: "page" | "custom";
|
|
30
|
+
|
|
24
31
|
/**
|
|
25
32
|
* Determines if more than one document for the custom type can be created.
|
|
26
33
|
*/
|
|
@@ -2,6 +2,7 @@ import { AnyRegularField } from "./types";
|
|
|
2
2
|
|
|
3
3
|
import { GroupField } from "./group";
|
|
4
4
|
import { SliceZone } from "./sliceZone";
|
|
5
|
+
import { TimestampField } from "./timestamp";
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* Document metadata for a translation of a Prismic document.
|
|
@@ -46,11 +47,11 @@ export interface PrismicDocumentHeader<TypeEnum = string, LangEnum = string> {
|
|
|
46
47
|
/**
|
|
47
48
|
* The timestamp at which the document was first published.
|
|
48
49
|
*/
|
|
49
|
-
first_publication_date:
|
|
50
|
+
first_publication_date: TimestampField<"filled">;
|
|
50
51
|
/**
|
|
51
52
|
* The timestamp at which the document was last published.
|
|
52
53
|
*/
|
|
53
|
-
last_publication_date:
|
|
54
|
+
last_publication_date: TimestampField<"filled">;
|
|
54
55
|
/**
|
|
55
56
|
* Slugs associated with document.
|
|
56
57
|
*
|