@prismicio/client 7.10.0-alpha.0 → 7.10.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/BaseClient.cjs +108 -0
- package/dist/BaseClient.cjs.map +1 -0
- package/dist/BaseClient.d.ts +152 -0
- package/dist/BaseClient.js +108 -0
- package/dist/BaseClient.js.map +1 -0
- package/dist/Client.cjs +1217 -0
- package/dist/Client.cjs.map +1 -0
- package/dist/Client.d.ts +924 -0
- package/dist/Client.js +1217 -0
- package/dist/Client.js.map +1 -0
- package/dist/Migration.cjs +318 -0
- package/dist/Migration.cjs.map +1 -0
- package/dist/Migration.d.ts +213 -0
- package/dist/Migration.js +318 -0
- package/dist/Migration.js.map +1 -0
- package/dist/WriteClient.cjs +515 -0
- package/dist/WriteClient.cjs.map +1 -0
- package/dist/WriteClient.d.ts +314 -0
- package/dist/WriteClient.js +515 -0
- package/dist/WriteClient.js.map +1 -0
- package/dist/createClient.cjs +2 -1215
- package/dist/createClient.cjs.map +1 -1
- package/dist/createClient.d.ts +2 -1003
- package/dist/createClient.js +1 -1214
- package/dist/createClient.js.map +1 -1
- package/dist/createMigration.cjs +6 -0
- package/dist/createMigration.cjs.map +1 -0
- package/dist/createMigration.d.ts +24 -0
- package/dist/createMigration.js +6 -0
- package/dist/createMigration.js.map +1 -0
- package/dist/createWriteClient.cjs +6 -0
- package/dist/createWriteClient.cjs.map +1 -0
- package/dist/createWriteClient.d.ts +32 -0
- package/dist/createWriteClient.js +6 -0
- package/dist/createWriteClient.js.map +1 -0
- package/dist/getRepositoryName.cjs +7 -2
- package/dist/getRepositoryName.cjs.map +1 -1
- package/dist/getRepositoryName.d.ts +3 -6
- package/dist/getRepositoryName.js +7 -2
- package/dist/getRepositoryName.js.map +1 -1
- package/dist/index.cjs +14 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +17 -2
- package/dist/index.js +14 -1
- package/dist/index.js.map +1 -1
- package/dist/lib/isMigrationValue.cjs +24 -0
- package/dist/lib/isMigrationValue.cjs.map +1 -0
- package/dist/lib/isMigrationValue.d.ts +62 -0
- package/dist/lib/isMigrationValue.js +24 -0
- package/dist/lib/isMigrationValue.js.map +1 -0
- package/dist/lib/isValue.cjs +53 -0
- package/dist/lib/isValue.cjs.map +1 -0
- package/dist/lib/isValue.d.ts +72 -0
- package/dist/lib/isValue.js +53 -0
- package/dist/lib/isValue.js.map +1 -0
- package/dist/lib/pLimit.cjs +49 -0
- package/dist/lib/pLimit.cjs.map +1 -0
- package/dist/lib/pLimit.d.ts +22 -0
- package/dist/lib/pLimit.js +49 -0
- package/dist/lib/pLimit.js.map +1 -0
- package/dist/lib/resolveMigrationDocumentData.cjs +123 -0
- package/dist/lib/resolveMigrationDocumentData.cjs.map +1 -0
- package/dist/lib/resolveMigrationDocumentData.d.ts +52 -0
- package/dist/lib/resolveMigrationDocumentData.js +123 -0
- package/dist/lib/resolveMigrationDocumentData.js.map +1 -0
- package/dist/lib/validateAssetMetadata.cjs +28 -0
- package/dist/lib/validateAssetMetadata.cjs.map +1 -0
- package/dist/lib/validateAssetMetadata.d.ts +10 -0
- package/dist/lib/validateAssetMetadata.js +28 -0
- package/dist/lib/validateAssetMetadata.js.map +1 -0
- package/dist/package.json.cjs +1 -1
- package/dist/package.json.js +1 -1
- package/dist/types/api/asset/asset.d.ts +177 -0
- package/dist/types/api/asset/tag.d.ts +53 -0
- package/dist/types/api/migration/document.d.ts +60 -0
- package/dist/types/migration/Asset.cjs +33 -0
- package/dist/types/migration/Asset.cjs.map +1 -0
- package/dist/types/migration/Asset.d.ts +109 -0
- package/dist/types/migration/Asset.js +33 -0
- package/dist/types/migration/Asset.js.map +1 -0
- package/dist/types/migration/ContentRelationship.d.ts +17 -0
- package/dist/types/migration/Document.cjs +47 -0
- package/dist/types/migration/Document.cjs.map +1 -0
- package/dist/types/migration/Document.d.ts +94 -0
- package/dist/types/migration/Document.js +47 -0
- package/dist/types/migration/Document.js.map +1 -0
- package/dist/types/model/link.cjs.map +1 -1
- package/dist/types/model/link.d.ts +0 -1
- package/dist/types/model/link.js.map +1 -1
- package/dist/types/model/linkToMedia.d.ts +0 -1
- package/dist/types/value/contentRelationship.d.ts +0 -1
- package/dist/types/value/link.cjs.map +1 -1
- package/dist/types/value/link.d.ts +0 -2
- package/dist/types/value/link.js.map +1 -1
- package/dist/types/value/linkToMedia.d.ts +0 -1
- package/package.json +2 -2
- package/src/BaseClient.ts +330 -0
- package/src/Client.ts +1787 -0
- package/src/Migration.ts +531 -0
- package/src/WriteClient.ts +916 -0
- package/src/createClient.ts +3 -1931
- package/src/createMigration.ts +31 -0
- package/src/createWriteClient.ts +43 -0
- package/src/getRepositoryName.ts +19 -14
- package/src/index.ts +32 -4
- package/src/lib/isMigrationValue.ts +126 -0
- package/src/lib/isValue.ts +196 -0
- package/src/lib/pLimit.ts +106 -0
- package/src/lib/resolveMigrationDocumentData.ts +233 -0
- package/src/lib/validateAssetMetadata.ts +82 -0
- package/src/types/api/asset/asset.ts +211 -0
- package/src/types/api/asset/tag.ts +59 -0
- package/src/types/api/migration/document.ts +84 -0
- package/src/types/migration/Asset.ts +156 -0
- package/src/types/migration/ContentRelationship.ts +36 -0
- package/src/types/migration/Document.ts +157 -0
- package/src/types/model/link.ts +0 -1
- package/src/types/model/linkToMedia.ts +0 -1
- package/src/types/value/contentRelationship.ts +0 -1
- package/src/types/value/link.ts +0 -2
- package/src/types/value/linkToMedia.ts +0 -1
package/src/Client.ts
ADDED
|
@@ -0,0 +1,1787 @@
|
|
|
1
|
+
import { appendFilters } from "./lib/appendFilters"
|
|
2
|
+
import { castThunk } from "./lib/castThunk"
|
|
3
|
+
import { devMsg } from "./lib/devMsg"
|
|
4
|
+
import { everyTagFilter } from "./lib/everyTagFilter"
|
|
5
|
+
import { findMasterRef } from "./lib/findMasterRef"
|
|
6
|
+
import { findRefByID } from "./lib/findRefByID"
|
|
7
|
+
import { findRefByLabel } from "./lib/findRefByLabel"
|
|
8
|
+
import { getPreviewCookie } from "./lib/getPreviewCookie"
|
|
9
|
+
import { minifyGraphQLQuery } from "./lib/minifyGraphQLQuery"
|
|
10
|
+
import { someTagsFilter } from "./lib/someTagsFilter"
|
|
11
|
+
import { typeFilter } from "./lib/typeFilter"
|
|
12
|
+
|
|
13
|
+
import type { Query } from "./types/api/query"
|
|
14
|
+
import type { Ref } from "./types/api/ref"
|
|
15
|
+
import type { Form, Repository } from "./types/api/repository"
|
|
16
|
+
import type { PrismicDocument } from "./types/value/document"
|
|
17
|
+
|
|
18
|
+
import { ForbiddenError } from "./errors/ForbiddenError"
|
|
19
|
+
import { NotFoundError } from "./errors/NotFoundError"
|
|
20
|
+
import { ParsingError } from "./errors/ParsingError"
|
|
21
|
+
import { PreviewTokenExpiredError } from "./errors/PreviewTokenExpired"
|
|
22
|
+
import { PrismicError } from "./errors/PrismicError"
|
|
23
|
+
import { RefExpiredError } from "./errors/RefExpiredError"
|
|
24
|
+
import { RefNotFoundError } from "./errors/RefNotFoundError"
|
|
25
|
+
import { RepositoryNotFoundError } from "./errors/RepositoryNotFoundError"
|
|
26
|
+
|
|
27
|
+
import type { LinkResolverFunction } from "./helpers/asLink"
|
|
28
|
+
import { asLink } from "./helpers/asLink"
|
|
29
|
+
|
|
30
|
+
import {
|
|
31
|
+
type AbortSignalLike,
|
|
32
|
+
BaseClient,
|
|
33
|
+
type BaseClientConfig,
|
|
34
|
+
type FetchParams,
|
|
35
|
+
type HttpRequestLike,
|
|
36
|
+
} from "./BaseClient"
|
|
37
|
+
import type { BuildQueryURLArgs } from "./buildQueryURL"
|
|
38
|
+
import { buildQueryURL } from "./buildQueryURL"
|
|
39
|
+
import { filter } from "./filter"
|
|
40
|
+
import { getRepositoryEndpoint } from "./getRepositoryEndpoint"
|
|
41
|
+
import { getRepositoryName } from "./getRepositoryName"
|
|
42
|
+
import { isRepositoryEndpoint } from "./isRepositoryEndpoint"
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* The largest page size allowed by the Prismic REST API V2. This value is used
|
|
46
|
+
* to minimize the number of requests required to query content.
|
|
47
|
+
*/
|
|
48
|
+
const MAX_PAGE_SIZE = 100
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* The number of milliseconds in which repository metadata is considered valid.
|
|
52
|
+
* A ref can be invalidated quickly depending on how frequently content is
|
|
53
|
+
* updated in the Prismic repository. As such, repository's metadata can only be
|
|
54
|
+
* considered valid for a short amount of time.
|
|
55
|
+
*/
|
|
56
|
+
export const REPOSITORY_CACHE_TTL = 5000
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* The number of milliseconds in which a multi-page `getAll` (e.g. `getAll`,
|
|
60
|
+
* `getAllByType`, `getAllByTag`) will wait between individual page requests.
|
|
61
|
+
*
|
|
62
|
+
* This is done to ensure API performance is sustainable and reduces the chance
|
|
63
|
+
* of a failed API request due to overloading.
|
|
64
|
+
*/
|
|
65
|
+
export const GET_ALL_QUERY_DELAY = 500
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* The default number of milliseconds to wait before retrying a rate-limited
|
|
69
|
+
* `fetch()` request (429 response code). The default value is only used if the
|
|
70
|
+
* response does not include a `retry-after` header.
|
|
71
|
+
*
|
|
72
|
+
* The API allows up to 200 requests per second.
|
|
73
|
+
*/
|
|
74
|
+
const DEFUALT_RETRY_AFTER_MS = 1000
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Extracts one or more Prismic document types that match a given Prismic
|
|
78
|
+
* document type. If no matches are found, no extraction is performed and the
|
|
79
|
+
* union of all provided Prismic document types are returned.
|
|
80
|
+
*
|
|
81
|
+
* @typeParam TDocuments - Prismic document types from which to extract.
|
|
82
|
+
* @typeParam TDocumentType - Type(s) to match `TDocuments` against.
|
|
83
|
+
*/
|
|
84
|
+
type ExtractDocumentType<
|
|
85
|
+
TDocuments extends PrismicDocument,
|
|
86
|
+
TDocumentType extends TDocuments["type"],
|
|
87
|
+
> =
|
|
88
|
+
Extract<TDocuments, { type: TDocumentType }> extends never
|
|
89
|
+
? TDocuments
|
|
90
|
+
: Extract<TDocuments, { type: TDocumentType }>
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Modes for client ref management.
|
|
94
|
+
*/
|
|
95
|
+
enum RefStateMode {
|
|
96
|
+
/**
|
|
97
|
+
* Use the repository's master ref.
|
|
98
|
+
*/
|
|
99
|
+
Master = "Master",
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Use a given Release identified by its ID.
|
|
103
|
+
*/
|
|
104
|
+
ReleaseID = "ReleaseID",
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Use a given Release identified by its label.
|
|
108
|
+
*/
|
|
109
|
+
ReleaseLabel = "ReleaseLabel",
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Use a given ref.
|
|
113
|
+
*/
|
|
114
|
+
Manual = "Manual",
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* An object containing stateful information about a client's ref strategy.
|
|
119
|
+
*/
|
|
120
|
+
type RefState = {
|
|
121
|
+
/**
|
|
122
|
+
* Determines if automatic preview support is enabled.
|
|
123
|
+
*/
|
|
124
|
+
autoPreviewsEnabled: boolean
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* An optional HTTP server request object used during previews if automatic
|
|
128
|
+
* previews are enabled.
|
|
129
|
+
*/
|
|
130
|
+
httpRequest?: HttpRequestLike
|
|
131
|
+
} & (
|
|
132
|
+
| {
|
|
133
|
+
mode: RefStateMode.Master
|
|
134
|
+
}
|
|
135
|
+
| {
|
|
136
|
+
mode: RefStateMode.ReleaseID
|
|
137
|
+
releaseID: string
|
|
138
|
+
}
|
|
139
|
+
| {
|
|
140
|
+
mode: RefStateMode.ReleaseLabel
|
|
141
|
+
releaseLabel: string
|
|
142
|
+
}
|
|
143
|
+
| {
|
|
144
|
+
mode: RefStateMode.Manual
|
|
145
|
+
ref: RefStringOrThunk
|
|
146
|
+
}
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* A ref or a function that returns a ref. If a static ref is known, one can be
|
|
151
|
+
* given. If the ref must be fetched on-demand, a function can be provided. This
|
|
152
|
+
* function can optionally be asynchronous.
|
|
153
|
+
*/
|
|
154
|
+
type RefStringOrThunk =
|
|
155
|
+
| string
|
|
156
|
+
| (() => string | undefined | Promise<string | undefined>)
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Configuration for clients that determine how content is queried.
|
|
160
|
+
*/
|
|
161
|
+
export type ClientConfig = {
|
|
162
|
+
/**
|
|
163
|
+
* The full Rest API V2 endpoint for the repository. This is only helpful if
|
|
164
|
+
* you're using Prismic behind a proxy which we do not recommend.
|
|
165
|
+
*
|
|
166
|
+
* @defaultValue `getRepositoryEndpoint(repositoryNameOrEndpoint)`
|
|
167
|
+
*/
|
|
168
|
+
documentAPIEndpoint?: string
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* The secure token for accessing the Prismic repository. This is only
|
|
172
|
+
* required if the repository is set to private.
|
|
173
|
+
*/
|
|
174
|
+
accessToken?: string
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* A string representing a version of the Prismic repository's content. This
|
|
178
|
+
* may point to the latest version (called the "master ref"), or a preview
|
|
179
|
+
* with draft content.
|
|
180
|
+
*/
|
|
181
|
+
ref?: RefStringOrThunk
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* A list of route resolver objects that define how a document's `url`
|
|
185
|
+
* property is resolved.
|
|
186
|
+
*
|
|
187
|
+
* {@link https://prismic.io/docs/route-resolver}
|
|
188
|
+
*/
|
|
189
|
+
routes?: NonNullable<BuildQueryURLArgs["routes"]>
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* The `brokenRoute` option allows you to define the route populated in the
|
|
193
|
+
* `url` property for broken link or content relationship fields. A broken
|
|
194
|
+
* link is a link or content relationship field whose linked document has been
|
|
195
|
+
* unpublished or deleted.
|
|
196
|
+
*
|
|
197
|
+
* {@link https://prismic.io/docs/route-resolver}
|
|
198
|
+
*/
|
|
199
|
+
brokenRoute?: NonNullable<BuildQueryURLArgs["brokenRoute"]>
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Default parameters that will be sent with each query. These parameters can
|
|
203
|
+
* be overridden on each query if needed.
|
|
204
|
+
*/
|
|
205
|
+
defaultParams?: Omit<
|
|
206
|
+
BuildQueryURLArgs,
|
|
207
|
+
"ref" | "integrationFieldsRef" | "accessToken" | "routes" | "brokenRoute"
|
|
208
|
+
>
|
|
209
|
+
} & BaseClientConfig
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Parameters specific to client methods that fetch all documents. These methods
|
|
213
|
+
* start with `getAll` (for example, `getAllByType`).
|
|
214
|
+
*/
|
|
215
|
+
type GetAllParams = {
|
|
216
|
+
/**
|
|
217
|
+
* Limit the number of documents queried. If a number is not provided, there
|
|
218
|
+
* will be no limit and all matching documents will be returned.
|
|
219
|
+
*/
|
|
220
|
+
limit?: number
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Arguments to determine how the URL for a preview session is resolved.
|
|
225
|
+
*/
|
|
226
|
+
type ResolvePreviewArgs<LinkResolverReturnType> = {
|
|
227
|
+
/**
|
|
228
|
+
* A function that maps a Prismic document to a URL within your app.
|
|
229
|
+
*/
|
|
230
|
+
linkResolver?: LinkResolverFunction<LinkResolverReturnType>
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* A fallback URL if the link resolver does not return a value.
|
|
234
|
+
*/
|
|
235
|
+
defaultURL: string
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* The preview token (also known as a ref) that will be used to query preview
|
|
239
|
+
* content from the Prismic repository.
|
|
240
|
+
*/
|
|
241
|
+
previewToken?: string
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* The previewed document that will be used to determine the destination URL.
|
|
245
|
+
*/
|
|
246
|
+
documentID?: string
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* A client that allows querying content from a Prismic repository.
|
|
251
|
+
*
|
|
252
|
+
* If used in an environment where a global `fetch` function is unavailable,
|
|
253
|
+
* such as Node.js, the `fetch` option must be provided as part of the `options`
|
|
254
|
+
* parameter.
|
|
255
|
+
*
|
|
256
|
+
* @typeParam TDocuments - Document types that are registered for the Prismic
|
|
257
|
+
* repository. Query methods will automatically be typed based on this type.
|
|
258
|
+
*/
|
|
259
|
+
export class Client<
|
|
260
|
+
TDocuments extends PrismicDocument = PrismicDocument,
|
|
261
|
+
> extends BaseClient {
|
|
262
|
+
#repositoryName: string | undefined
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* The Prismic repository's name.
|
|
266
|
+
*/
|
|
267
|
+
set repositoryName(value: string) {
|
|
268
|
+
this.#repositoryName = value
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* The Prismic repository's name.
|
|
273
|
+
*/
|
|
274
|
+
get repositoryName(): string {
|
|
275
|
+
if (!this.#repositoryName) {
|
|
276
|
+
throw new PrismicError(
|
|
277
|
+
`A repository name is required for this method but one could not be inferred from the provided API endpoint (\`${this.documentAPIEndpoint}\`). To fix this error, provide a repository name when creating the client. For more details, see ${devMsg("prefer-repository-name")}`,
|
|
278
|
+
undefined,
|
|
279
|
+
undefined,
|
|
280
|
+
)
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
return this.#repositoryName
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* The Prismic REST API V2 endpoint for the repository (use
|
|
288
|
+
* `prismic.getRepositoryEndpoint` for the default endpoint).
|
|
289
|
+
*/
|
|
290
|
+
documentAPIEndpoint: string
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* The Prismic REST API V2 endpoint for the repository (use
|
|
294
|
+
* `prismic.getRepositoryEndpoint` for the default endpoint).
|
|
295
|
+
*
|
|
296
|
+
* @deprecated Use `documentAPIEndpoint` instead.
|
|
297
|
+
*/
|
|
298
|
+
// TODO: Remove in v8.
|
|
299
|
+
set endpoint(value: string) {
|
|
300
|
+
this.documentAPIEndpoint = value
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* The Prismic REST API V2 endpoint for the repository (use
|
|
305
|
+
* `prismic.getRepositoryEndpoint` for the default endpoint).
|
|
306
|
+
*
|
|
307
|
+
* @deprecated Use `documentAPIEndpoint` instead.
|
|
308
|
+
*/
|
|
309
|
+
// TODO: Remove in v8.
|
|
310
|
+
get endpoint(): string {
|
|
311
|
+
return this.documentAPIEndpoint
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* The secure token for accessing the API (only needed if your repository is
|
|
316
|
+
* set to private).
|
|
317
|
+
*
|
|
318
|
+
* {@link https://user-guides.prismic.io/en/articles/1036153-generating-an-access-token}
|
|
319
|
+
*/
|
|
320
|
+
accessToken?: string
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* A list of route resolver objects that define how a document's `url` field
|
|
324
|
+
* is resolved.
|
|
325
|
+
*
|
|
326
|
+
* {@link https://prismic.io/docs/route-resolver}
|
|
327
|
+
*/
|
|
328
|
+
routes?: NonNullable<BuildQueryURLArgs["routes"]>
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* The `brokenRoute` option allows you to define the route populated in the
|
|
332
|
+
* `url` property for broken link or content relationship fields. A broken
|
|
333
|
+
* link is a link or content relationship field whose linked document has been
|
|
334
|
+
* unpublished or deleted.
|
|
335
|
+
*
|
|
336
|
+
* {@link https://prismic.io/docs/route-resolver}
|
|
337
|
+
*/
|
|
338
|
+
brokenRoute?: NonNullable<BuildQueryURLArgs["brokenRoute"]>
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* Default parameters that will be sent with each query. These parameters can
|
|
342
|
+
* be overridden on each query if needed.
|
|
343
|
+
*/
|
|
344
|
+
defaultParams?: Omit<
|
|
345
|
+
BuildQueryURLArgs,
|
|
346
|
+
"ref" | "integrationFieldsRef" | "accessToken" | "routes"
|
|
347
|
+
>
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* The client's ref mode state. This determines which ref is used during
|
|
351
|
+
* queries.
|
|
352
|
+
*/
|
|
353
|
+
private refState: RefState = {
|
|
354
|
+
mode: RefStateMode.Master,
|
|
355
|
+
autoPreviewsEnabled: true,
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* Cached repository value.
|
|
360
|
+
*/
|
|
361
|
+
private cachedRepository: Repository | undefined
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* Timestamp at which the cached repository data is considered stale.
|
|
365
|
+
*/
|
|
366
|
+
private cachedRepositoryExpiration = 0
|
|
367
|
+
|
|
368
|
+
/**
|
|
369
|
+
* Creates a Prismic client that can be used to query a repository.
|
|
370
|
+
*
|
|
371
|
+
* If used in an environment where a global `fetch` function is unavailable,
|
|
372
|
+
* such as in some Node.js versions, the `fetch` option must be provided as
|
|
373
|
+
* part of the `options` parameter.
|
|
374
|
+
*
|
|
375
|
+
* @param repositoryNameOrEndpoint - The Prismic repository name or full Rest
|
|
376
|
+
* API V2 endpoint for the repository.
|
|
377
|
+
* @param options - Configuration that determines how content will be queried
|
|
378
|
+
* from the Prismic repository.
|
|
379
|
+
*
|
|
380
|
+
* @returns A client that can query content from the repository.
|
|
381
|
+
*/
|
|
382
|
+
constructor(repositoryNameOrEndpoint: string, options: ClientConfig = {}) {
|
|
383
|
+
super(options)
|
|
384
|
+
|
|
385
|
+
if (
|
|
386
|
+
(options.documentAPIEndpoint ||
|
|
387
|
+
isRepositoryEndpoint(repositoryNameOrEndpoint)) &&
|
|
388
|
+
process.env.NODE_ENV === "development"
|
|
389
|
+
) {
|
|
390
|
+
const documentAPIEndpoint =
|
|
391
|
+
options.documentAPIEndpoint || repositoryNameOrEndpoint
|
|
392
|
+
|
|
393
|
+
// Matches non-API v2 `.prismic.io` endpoints, see: https://regex101.com/r/xRsavu/1
|
|
394
|
+
if (/\.prismic\.io\/(?!api\/v2\/?)/i.test(documentAPIEndpoint)) {
|
|
395
|
+
throw new PrismicError(
|
|
396
|
+
"@prismicio/client only supports Prismic Rest API V2. Please provide only the repository name to the first createClient() parameter or use the getRepositoryEndpoint() helper to generate a valid Rest API V2 endpoint URL.",
|
|
397
|
+
undefined,
|
|
398
|
+
undefined,
|
|
399
|
+
)
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
const hostname = new URL(documentAPIEndpoint).hostname.toLowerCase()
|
|
403
|
+
|
|
404
|
+
// Matches non-.cdn `.prismic.io` endpoints
|
|
405
|
+
if (
|
|
406
|
+
hostname.endsWith(".prismic.io") &&
|
|
407
|
+
!hostname.endsWith(".cdn.prismic.io")
|
|
408
|
+
) {
|
|
409
|
+
console.warn(
|
|
410
|
+
`[@prismicio/client] The client was created with a non-CDN endpoint. Convert it to the CDN endpoint for better performance. For more details, see ${devMsg("endpoint-must-use-cdn")}`,
|
|
411
|
+
)
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
// Warn if the user provided both a repository endpoint and an `documentAPIEndpoint` and they are different
|
|
415
|
+
if (
|
|
416
|
+
options.documentAPIEndpoint &&
|
|
417
|
+
isRepositoryEndpoint(repositoryNameOrEndpoint) &&
|
|
418
|
+
repositoryNameOrEndpoint !== options.documentAPIEndpoint
|
|
419
|
+
) {
|
|
420
|
+
console.warn(
|
|
421
|
+
`[@prismicio/client] Multiple incompatible endpoints were provided. Create the client using a repository name to prevent this error. For more details, see ${devMsg("prefer-repository-name")}`,
|
|
422
|
+
)
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
if (isRepositoryEndpoint(repositoryNameOrEndpoint)) {
|
|
427
|
+
this.documentAPIEndpoint = repositoryNameOrEndpoint
|
|
428
|
+
try {
|
|
429
|
+
this.repositoryName = getRepositoryName(repositoryNameOrEndpoint)
|
|
430
|
+
} catch (error) {
|
|
431
|
+
console.warn(
|
|
432
|
+
`[@prismicio/client] A repository name could not be inferred from the provided endpoint (\`${repositoryNameOrEndpoint}\`). Some methods will be disabled. Create the client using a repository name to prevent this warning. For more details, see ${devMsg("prefer-repository-name")}`,
|
|
433
|
+
)
|
|
434
|
+
}
|
|
435
|
+
} else {
|
|
436
|
+
this.documentAPIEndpoint =
|
|
437
|
+
options.documentAPIEndpoint ||
|
|
438
|
+
getRepositoryEndpoint(repositoryNameOrEndpoint)
|
|
439
|
+
this.repositoryName = repositoryNameOrEndpoint
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
this.accessToken = options.accessToken
|
|
443
|
+
this.routes = options.routes
|
|
444
|
+
this.brokenRoute = options.brokenRoute
|
|
445
|
+
this.defaultParams = options.defaultParams
|
|
446
|
+
|
|
447
|
+
if (options.ref) {
|
|
448
|
+
this.queryContentFromRef(options.ref)
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
this.graphQLFetch = this.graphQLFetch.bind(this)
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/**
|
|
455
|
+
* Enables the client to automatically query content from a preview session if
|
|
456
|
+
* one is active in browser environments. This is enabled by default in the
|
|
457
|
+
* browser.
|
|
458
|
+
*
|
|
459
|
+
* For server environments, use `enableAutoPreviewsFromReq`.
|
|
460
|
+
*
|
|
461
|
+
* @example
|
|
462
|
+
*
|
|
463
|
+
* ```ts
|
|
464
|
+
* client.enableAutoPreviews()
|
|
465
|
+
* ```
|
|
466
|
+
*
|
|
467
|
+
* @see enableAutoPreviewsFromReq
|
|
468
|
+
*/
|
|
469
|
+
enableAutoPreviews(): void {
|
|
470
|
+
this.refState.autoPreviewsEnabled = true
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
/**
|
|
474
|
+
* Enables the client to automatically query content from a preview session if
|
|
475
|
+
* one is active in server environments. This is disabled by default on the
|
|
476
|
+
* server.
|
|
477
|
+
*
|
|
478
|
+
* For browser environments, use `enableAutoPreviews`.
|
|
479
|
+
*
|
|
480
|
+
* @example
|
|
481
|
+
*
|
|
482
|
+
* ```ts
|
|
483
|
+
* // In an express app
|
|
484
|
+
* app.get("/", function (req, res) {
|
|
485
|
+
* client.enableAutoPreviewsFromReq(req)
|
|
486
|
+
* })
|
|
487
|
+
* ```
|
|
488
|
+
*
|
|
489
|
+
* @param req - An HTTP server request object containing the request's
|
|
490
|
+
* cookies.
|
|
491
|
+
*/
|
|
492
|
+
enableAutoPreviewsFromReq<R extends HttpRequestLike>(req: R): void {
|
|
493
|
+
this.refState.httpRequest = req
|
|
494
|
+
this.refState.autoPreviewsEnabled = true
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
/**
|
|
498
|
+
* Disables the client from automatically querying content from a preview
|
|
499
|
+
* session if one is active.
|
|
500
|
+
*
|
|
501
|
+
* Automatic preview content querying is enabled by default unless this method
|
|
502
|
+
* is called.
|
|
503
|
+
*
|
|
504
|
+
* @example
|
|
505
|
+
*
|
|
506
|
+
* ```ts
|
|
507
|
+
* client.disableAutoPreviews()
|
|
508
|
+
* ```
|
|
509
|
+
*/
|
|
510
|
+
disableAutoPreviews(): void {
|
|
511
|
+
this.refState.autoPreviewsEnabled = false
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
/**
|
|
515
|
+
* Queries content from the Prismic repository.
|
|
516
|
+
*
|
|
517
|
+
* @example
|
|
518
|
+
*
|
|
519
|
+
* ```ts
|
|
520
|
+
* const response = await client.get()
|
|
521
|
+
* ```
|
|
522
|
+
*
|
|
523
|
+
* @typeParam TDocument - Type of Prismic documents returned.
|
|
524
|
+
*
|
|
525
|
+
* @param params - Parameters to filter, sort, and paginate results.
|
|
526
|
+
*
|
|
527
|
+
* @returns A paginated response containing the result of the query.
|
|
528
|
+
*/
|
|
529
|
+
async get<TDocument extends TDocuments>(
|
|
530
|
+
params?: Partial<BuildQueryURLArgs> & FetchParams,
|
|
531
|
+
): Promise<Query<TDocument>> {
|
|
532
|
+
const url = await this.buildQueryURL(params)
|
|
533
|
+
|
|
534
|
+
return await this.fetch<Query<TDocument>>(url, params)
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
/**
|
|
538
|
+
* Queries content from the Prismic repository and returns only the first
|
|
539
|
+
* result, if any.
|
|
540
|
+
*
|
|
541
|
+
* @example
|
|
542
|
+
*
|
|
543
|
+
* ```ts
|
|
544
|
+
* const document = await client.getFirst()
|
|
545
|
+
* ```
|
|
546
|
+
*
|
|
547
|
+
* @typeParam TDocument - Type of the Prismic document returned.
|
|
548
|
+
*
|
|
549
|
+
* @param params - Parameters to filter, sort, and paginate results. @returns
|
|
550
|
+
* The first result of the query, if any.
|
|
551
|
+
*/
|
|
552
|
+
async getFirst<TDocument extends TDocuments>(
|
|
553
|
+
params?: Partial<BuildQueryURLArgs> & FetchParams,
|
|
554
|
+
): Promise<TDocument> {
|
|
555
|
+
const actualParams = { ...params }
|
|
556
|
+
if (!(params && params.page) && !params?.pageSize) {
|
|
557
|
+
actualParams.pageSize = this.defaultParams?.pageSize ?? 1
|
|
558
|
+
}
|
|
559
|
+
const url = await this.buildQueryURL(actualParams)
|
|
560
|
+
const result = await this.fetch<Query<TDocument>>(url, params)
|
|
561
|
+
|
|
562
|
+
const firstResult = result.results[0]
|
|
563
|
+
|
|
564
|
+
if (firstResult) {
|
|
565
|
+
return firstResult
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
throw new NotFoundError("No documents were returned", url, undefined)
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
/**
|
|
572
|
+
* **IMPORTANT**: Avoid using `dangerouslyGetAll` as it may be slower and
|
|
573
|
+
* require more resources than other methods. Prefer using other methods that
|
|
574
|
+
* filter by filters such as `getAllByType`.
|
|
575
|
+
*
|
|
576
|
+
* Queries content from the Prismic repository and returns all matching
|
|
577
|
+
* content. If no filters are provided, all documents will be fetched.
|
|
578
|
+
*
|
|
579
|
+
* This method may make multiple network requests to query all matching
|
|
580
|
+
* content.
|
|
581
|
+
*
|
|
582
|
+
* @example
|
|
583
|
+
*
|
|
584
|
+
* ```ts
|
|
585
|
+
* const response = await client.dangerouslyGetAll()
|
|
586
|
+
* ```
|
|
587
|
+
*
|
|
588
|
+
* @typeParam TDocument - Type of Prismic documents returned.
|
|
589
|
+
*
|
|
590
|
+
* @param params - Parameters to filter, sort, and paginate results.
|
|
591
|
+
*
|
|
592
|
+
* @returns A list of documents matching the query.
|
|
593
|
+
*/
|
|
594
|
+
async dangerouslyGetAll<TDocument extends TDocuments>(
|
|
595
|
+
params: Partial<Omit<BuildQueryURLArgs, "page">> &
|
|
596
|
+
GetAllParams &
|
|
597
|
+
FetchParams = {},
|
|
598
|
+
): Promise<TDocument[]> {
|
|
599
|
+
const { limit = Infinity, ...actualParams } = params
|
|
600
|
+
const resolvedParams = {
|
|
601
|
+
...actualParams,
|
|
602
|
+
pageSize: Math.min(
|
|
603
|
+
limit,
|
|
604
|
+
actualParams.pageSize || this.defaultParams?.pageSize || MAX_PAGE_SIZE,
|
|
605
|
+
),
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
const documents: TDocument[] = []
|
|
609
|
+
let latestResult: Query<TDocument> | undefined
|
|
610
|
+
|
|
611
|
+
while (
|
|
612
|
+
(!latestResult || latestResult.next_page) &&
|
|
613
|
+
documents.length < limit
|
|
614
|
+
) {
|
|
615
|
+
const page = latestResult ? latestResult.page + 1 : undefined
|
|
616
|
+
|
|
617
|
+
latestResult = await this.get<TDocument>({ ...resolvedParams, page })
|
|
618
|
+
documents.push(...latestResult.results)
|
|
619
|
+
|
|
620
|
+
if (latestResult.next_page) {
|
|
621
|
+
await new Promise((res) => setTimeout(res, GET_ALL_QUERY_DELAY))
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
return documents.slice(0, limit)
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
/**
|
|
629
|
+
* Queries a document from the Prismic repository with a specific ID.
|
|
630
|
+
*
|
|
631
|
+
* @remarks
|
|
632
|
+
* A document's UID is different from its ID. An ID is automatically generated
|
|
633
|
+
* for all documents and is made available on its `id` property. A UID is
|
|
634
|
+
* provided in the Prismic editor and is unique among all documents of its
|
|
635
|
+
* custom type.
|
|
636
|
+
*
|
|
637
|
+
* @example
|
|
638
|
+
*
|
|
639
|
+
* ```ts
|
|
640
|
+
* const document = await client.getByID("WW4bKScAAMAqmluX")
|
|
641
|
+
* ```
|
|
642
|
+
*
|
|
643
|
+
* @typeParam TDocument- Type of the Prismic document returned.
|
|
644
|
+
*
|
|
645
|
+
* @param id - ID of the document.
|
|
646
|
+
* @param params - Parameters to filter, sort, and paginate the results.
|
|
647
|
+
*
|
|
648
|
+
* @returns The document with an ID matching the `id` parameter, if a matching
|
|
649
|
+
* document exists.
|
|
650
|
+
*/
|
|
651
|
+
async getByID<TDocument extends TDocuments>(
|
|
652
|
+
id: string,
|
|
653
|
+
params?: Partial<BuildQueryURLArgs> & FetchParams,
|
|
654
|
+
): Promise<TDocument> {
|
|
655
|
+
return await this.getFirst<TDocument>(
|
|
656
|
+
appendFilters(params, filter.at("document.id", id)),
|
|
657
|
+
)
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
/**
|
|
661
|
+
* Queries documents from the Prismic repository with specific IDs.
|
|
662
|
+
*
|
|
663
|
+
* @remarks
|
|
664
|
+
* A document's UID is different from its ID. An ID is automatically generated
|
|
665
|
+
* for all documents and is made available on its `id` property. A UID is
|
|
666
|
+
* provided in the Prismic editor and is unique among all documents of its
|
|
667
|
+
* custom type.
|
|
668
|
+
*
|
|
669
|
+
* @example
|
|
670
|
+
*
|
|
671
|
+
* ```ts
|
|
672
|
+
* const response = await client.getByIDs([
|
|
673
|
+
* "WW4bKScAAMAqmluX",
|
|
674
|
+
* "U1kTRgEAAC8A5ldS",
|
|
675
|
+
* ])
|
|
676
|
+
* ```
|
|
677
|
+
*
|
|
678
|
+
* @typeParam TDocument - Type of Prismic documents returned.
|
|
679
|
+
*
|
|
680
|
+
* @param ids - A list of document IDs.
|
|
681
|
+
* @param params - Parameters to filter, sort, and paginate the results.
|
|
682
|
+
*
|
|
683
|
+
* @returns A paginated response containing documents with IDs matching the
|
|
684
|
+
* `ids` parameter.
|
|
685
|
+
*/
|
|
686
|
+
async getByIDs<TDocument extends TDocuments>(
|
|
687
|
+
ids: string[],
|
|
688
|
+
params?: Partial<BuildQueryURLArgs> & FetchParams,
|
|
689
|
+
): Promise<Query<TDocument>> {
|
|
690
|
+
return await this.get<TDocument>(
|
|
691
|
+
appendFilters(params, filter.in("document.id", ids)),
|
|
692
|
+
)
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
/**
|
|
696
|
+
* Queries all documents from the Prismic repository with specific IDs.
|
|
697
|
+
*
|
|
698
|
+
* This method may make multiple network requests to query all matching
|
|
699
|
+
* content.
|
|
700
|
+
*
|
|
701
|
+
* @remarks
|
|
702
|
+
* A document's UID is different from its ID. An ID is automatically generated
|
|
703
|
+
* for all documents and is made available on its `id` property. A UID is
|
|
704
|
+
* provided in the Prismic editor and is unique among all documents of its
|
|
705
|
+
* custom type.
|
|
706
|
+
*
|
|
707
|
+
* @example
|
|
708
|
+
*
|
|
709
|
+
* ```ts
|
|
710
|
+
* const response = await client.getAllByIDs([
|
|
711
|
+
* "WW4bKScAAMAqmluX",
|
|
712
|
+
* "U1kTRgEAAC8A5ldS",
|
|
713
|
+
* ])
|
|
714
|
+
* ```
|
|
715
|
+
*
|
|
716
|
+
* @typeParam TDocument - Type of Prismic documents returned.
|
|
717
|
+
*
|
|
718
|
+
* @param ids - A list of document IDs.
|
|
719
|
+
* @param params - Parameters to filter, sort, and paginate the results.
|
|
720
|
+
*
|
|
721
|
+
* @returns A list of documents with IDs matching the `ids` parameter.
|
|
722
|
+
*/
|
|
723
|
+
async getAllByIDs<TDocument extends TDocuments>(
|
|
724
|
+
ids: string[],
|
|
725
|
+
params?: Partial<BuildQueryURLArgs> & GetAllParams & FetchParams,
|
|
726
|
+
): Promise<TDocument[]> {
|
|
727
|
+
return await this.dangerouslyGetAll<TDocument>(
|
|
728
|
+
appendFilters(params, filter.in("document.id", ids)),
|
|
729
|
+
)
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
/**
|
|
733
|
+
* Queries a document from the Prismic repository with a specific UID and
|
|
734
|
+
* custom type.
|
|
735
|
+
*
|
|
736
|
+
* @remarks
|
|
737
|
+
* A document's UID is different from its ID. An ID is automatically generated
|
|
738
|
+
* for all documents and is made available on its `id` property. A UID is
|
|
739
|
+
* provided in the Prismic editor and is unique among all documents of its
|
|
740
|
+
* custom type.
|
|
741
|
+
*
|
|
742
|
+
* @example
|
|
743
|
+
*
|
|
744
|
+
* ```ts
|
|
745
|
+
* const document = await client.getByUID("blog_post", "my-first-post")
|
|
746
|
+
* ```
|
|
747
|
+
*
|
|
748
|
+
* @typeParam TDocument - Type of the Prismic document returned.
|
|
749
|
+
*
|
|
750
|
+
* @param documentType - The API ID of the document's custom type.
|
|
751
|
+
* @param uid - UID of the document.
|
|
752
|
+
* @param params - Parameters to filter, sort, and paginate the results.
|
|
753
|
+
*
|
|
754
|
+
* @returns The document with a UID matching the `uid` parameter, if a
|
|
755
|
+
* matching document exists.
|
|
756
|
+
*/
|
|
757
|
+
async getByUID<
|
|
758
|
+
TDocument extends TDocuments,
|
|
759
|
+
TDocumentType extends TDocument["type"] = TDocument["type"],
|
|
760
|
+
>(
|
|
761
|
+
documentType: TDocumentType,
|
|
762
|
+
uid: string,
|
|
763
|
+
params?: Partial<BuildQueryURLArgs> & FetchParams,
|
|
764
|
+
): Promise<ExtractDocumentType<TDocument, TDocumentType>> {
|
|
765
|
+
return await this.getFirst<ExtractDocumentType<TDocument, TDocumentType>>(
|
|
766
|
+
appendFilters(params, [
|
|
767
|
+
typeFilter(documentType),
|
|
768
|
+
filter.at(`my.${documentType}.uid`, uid),
|
|
769
|
+
]),
|
|
770
|
+
)
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
/**
|
|
774
|
+
* Queries document from the Prismic repository with specific UIDs and Custom
|
|
775
|
+
* Type.
|
|
776
|
+
*
|
|
777
|
+
* @remarks
|
|
778
|
+
* A document's UID is different from its ID. An ID is automatically generated
|
|
779
|
+
* for all documents and is made available on its `id` property. A UID is
|
|
780
|
+
* provided in the Prismic editor and is unique among all documents of its
|
|
781
|
+
* custom type.
|
|
782
|
+
*
|
|
783
|
+
* @example
|
|
784
|
+
*
|
|
785
|
+
* ```ts
|
|
786
|
+
* const document = await client.getByUIDs("blog_post", [
|
|
787
|
+
* "my-first-post",
|
|
788
|
+
* "my-second-post",
|
|
789
|
+
* ])
|
|
790
|
+
* ```
|
|
791
|
+
*
|
|
792
|
+
* @typeParam TDocument - Type of the Prismic document returned.
|
|
793
|
+
*
|
|
794
|
+
* @param documentType - The API ID of the document's custom type.
|
|
795
|
+
* @param uids - A list of document UIDs.
|
|
796
|
+
* @param params - Parameters to filter, sort, and paginate the results.
|
|
797
|
+
*
|
|
798
|
+
* @returns A paginated response containing documents with UIDs matching the
|
|
799
|
+
* `uids` parameter.
|
|
800
|
+
*/
|
|
801
|
+
async getByUIDs<
|
|
802
|
+
TDocument extends TDocuments,
|
|
803
|
+
TDocumentType extends TDocument["type"] = TDocument["type"],
|
|
804
|
+
>(
|
|
805
|
+
documentType: TDocumentType,
|
|
806
|
+
uids: string[],
|
|
807
|
+
params?: Partial<BuildQueryURLArgs> & FetchParams,
|
|
808
|
+
): Promise<Query<ExtractDocumentType<TDocument, TDocumentType>>> {
|
|
809
|
+
return await this.get<ExtractDocumentType<TDocument, TDocumentType>>(
|
|
810
|
+
appendFilters(params, [
|
|
811
|
+
typeFilter(documentType),
|
|
812
|
+
filter.in(`my.${documentType}.uid`, uids),
|
|
813
|
+
]),
|
|
814
|
+
)
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
/**
|
|
818
|
+
* Queries all documents from the Prismic repository with specific UIDs and
|
|
819
|
+
* custom type.
|
|
820
|
+
*
|
|
821
|
+
* This method may make multiple network requests to query all matching
|
|
822
|
+
* content.
|
|
823
|
+
*
|
|
824
|
+
* @remarks
|
|
825
|
+
* A document's UID is different from its ID. An ID is automatically generated
|
|
826
|
+
* for all documents and is made available on its `id` property. A UID is
|
|
827
|
+
* provided in the Prismic editor and is unique among all documents of its
|
|
828
|
+
* custom type.
|
|
829
|
+
*
|
|
830
|
+
* @example
|
|
831
|
+
*
|
|
832
|
+
* ```ts
|
|
833
|
+
* const response = await client.getAllByUIDs([
|
|
834
|
+
* "my-first-post",
|
|
835
|
+
* "my-second-post",
|
|
836
|
+
* ])
|
|
837
|
+
* ```
|
|
838
|
+
*
|
|
839
|
+
* @typeParam TDocument - Type of Prismic documents returned.
|
|
840
|
+
*
|
|
841
|
+
* @param documentType - The API ID of the document's custom type.
|
|
842
|
+
* @param uids - A list of document UIDs.
|
|
843
|
+
* @param params - Parameters to filter, sort, and paginate the results.
|
|
844
|
+
*
|
|
845
|
+
* @returns A list of documents with UIDs matching the `uids` parameter.
|
|
846
|
+
*/
|
|
847
|
+
async getAllByUIDs<
|
|
848
|
+
TDocument extends TDocuments,
|
|
849
|
+
TDocumentType extends TDocument["type"] = TDocument["type"],
|
|
850
|
+
>(
|
|
851
|
+
documentType: TDocumentType,
|
|
852
|
+
uids: string[],
|
|
853
|
+
params?: Partial<BuildQueryURLArgs> & GetAllParams & FetchParams,
|
|
854
|
+
): Promise<ExtractDocumentType<TDocument, TDocumentType>[]> {
|
|
855
|
+
return await this.dangerouslyGetAll<
|
|
856
|
+
ExtractDocumentType<TDocument, TDocumentType>
|
|
857
|
+
>(
|
|
858
|
+
appendFilters(params, [
|
|
859
|
+
typeFilter(documentType),
|
|
860
|
+
filter.in(`my.${documentType}.uid`, uids),
|
|
861
|
+
]),
|
|
862
|
+
)
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
/**
|
|
866
|
+
* Queries a singleton document from the Prismic repository for a specific
|
|
867
|
+
* custom type.
|
|
868
|
+
*
|
|
869
|
+
* @remarks
|
|
870
|
+
* A singleton document is one that is configured in Prismic to only allow one
|
|
871
|
+
* instance. For example, a repository may be configured to contain just one
|
|
872
|
+
* Settings document. This is in contrast to a repeatable custom type which
|
|
873
|
+
* allows multiple instances of itself.
|
|
874
|
+
*
|
|
875
|
+
* @example
|
|
876
|
+
*
|
|
877
|
+
* ```ts
|
|
878
|
+
* const document = await client.getSingle("settings")
|
|
879
|
+
* ```
|
|
880
|
+
*
|
|
881
|
+
* @typeParam TDocument - Type of the Prismic document returned.
|
|
882
|
+
*
|
|
883
|
+
* @param documentType - The API ID of the singleton custom type.
|
|
884
|
+
* @param params - Parameters to filter, sort, and paginate the results.
|
|
885
|
+
*
|
|
886
|
+
* @returns The singleton document for the custom type, if a matching document
|
|
887
|
+
* exists.
|
|
888
|
+
*/
|
|
889
|
+
async getSingle<
|
|
890
|
+
TDocument extends TDocuments,
|
|
891
|
+
TDocumentType extends TDocument["type"] = TDocument["type"],
|
|
892
|
+
>(
|
|
893
|
+
documentType: TDocumentType,
|
|
894
|
+
params?: Partial<BuildQueryURLArgs> & FetchParams,
|
|
895
|
+
): Promise<ExtractDocumentType<TDocument, TDocumentType>> {
|
|
896
|
+
return await this.getFirst<ExtractDocumentType<TDocument, TDocumentType>>(
|
|
897
|
+
appendFilters(params, typeFilter(documentType)),
|
|
898
|
+
)
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
/**
|
|
902
|
+
* Queries documents from the Prismic repository for a specific custom type.
|
|
903
|
+
*
|
|
904
|
+
* Use `getAllByType` instead if you need to query all documents for a
|
|
905
|
+
* specific custom type.
|
|
906
|
+
*
|
|
907
|
+
* @example
|
|
908
|
+
*
|
|
909
|
+
* ```ts
|
|
910
|
+
* const response = await client.getByType("blog_post")
|
|
911
|
+
* ```
|
|
912
|
+
*
|
|
913
|
+
* @typeParam TDocument - Type of Prismic documents returned.
|
|
914
|
+
*
|
|
915
|
+
* @param documentType - The API ID of the custom type.
|
|
916
|
+
* @param params - Parameters to filter, sort, and paginate the results.
|
|
917
|
+
*
|
|
918
|
+
* @returns A paginated response containing documents of the custom type.
|
|
919
|
+
*/
|
|
920
|
+
async getByType<
|
|
921
|
+
TDocument extends TDocuments,
|
|
922
|
+
TDocumentType extends TDocument["type"] = TDocument["type"],
|
|
923
|
+
>(
|
|
924
|
+
documentType: TDocumentType,
|
|
925
|
+
params?: Partial<BuildQueryURLArgs> & FetchParams,
|
|
926
|
+
): Promise<Query<ExtractDocumentType<TDocument, TDocumentType>>> {
|
|
927
|
+
return await this.get<ExtractDocumentType<TDocument, TDocumentType>>(
|
|
928
|
+
appendFilters(params, typeFilter(documentType)),
|
|
929
|
+
)
|
|
930
|
+
}
|
|
931
|
+
|
|
932
|
+
/**
|
|
933
|
+
* Queries all documents from the Prismic repository for a specific Custom
|
|
934
|
+
* Type.
|
|
935
|
+
*
|
|
936
|
+
* This method may make multiple network requests to query all matching
|
|
937
|
+
* content.
|
|
938
|
+
*
|
|
939
|
+
* @example
|
|
940
|
+
*
|
|
941
|
+
* ```ts
|
|
942
|
+
* const response = await client.getByType("blog_post")
|
|
943
|
+
* ```
|
|
944
|
+
*
|
|
945
|
+
* @typeParam TDocument - Type of Prismic documents returned.
|
|
946
|
+
*
|
|
947
|
+
* @param documentType - The API ID of the custom type.
|
|
948
|
+
* @param params - Parameters to filter, sort, and paginate the results.
|
|
949
|
+
*
|
|
950
|
+
* @returns A list of all documents of the custom type.
|
|
951
|
+
*/
|
|
952
|
+
async getAllByType<
|
|
953
|
+
TDocument extends TDocuments,
|
|
954
|
+
TDocumentType extends TDocument["type"] = TDocument["type"],
|
|
955
|
+
>(
|
|
956
|
+
documentType: TDocumentType,
|
|
957
|
+
params?: Partial<Omit<BuildQueryURLArgs, "page">> &
|
|
958
|
+
GetAllParams &
|
|
959
|
+
FetchParams,
|
|
960
|
+
): Promise<ExtractDocumentType<TDocument, TDocumentType>[]> {
|
|
961
|
+
return await this.dangerouslyGetAll<
|
|
962
|
+
ExtractDocumentType<TDocument, TDocumentType>
|
|
963
|
+
>(appendFilters(params, typeFilter(documentType)))
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
/**
|
|
967
|
+
* Queries documents from the Prismic repository with a specific tag.
|
|
968
|
+
*
|
|
969
|
+
* Use `getAllByTag` instead if you need to query all documents with a
|
|
970
|
+
* specific tag.
|
|
971
|
+
*
|
|
972
|
+
* @example
|
|
973
|
+
*
|
|
974
|
+
* ```ts
|
|
975
|
+
* const response = await client.getByTag("food")
|
|
976
|
+
* ```
|
|
977
|
+
*
|
|
978
|
+
* @typeParam TDocument - Type of Prismic documents returned.
|
|
979
|
+
*
|
|
980
|
+
* @param tag - The tag that must be included on a document.
|
|
981
|
+
* @param params - Parameters to filter, sort, and paginate the results.
|
|
982
|
+
*
|
|
983
|
+
* @returns A paginated response containing documents with the tag.
|
|
984
|
+
*/
|
|
985
|
+
async getByTag<TDocument extends TDocuments>(
|
|
986
|
+
tag: string,
|
|
987
|
+
params?: Partial<BuildQueryURLArgs> & FetchParams,
|
|
988
|
+
): Promise<Query<TDocument>> {
|
|
989
|
+
return await this.get<TDocument>(appendFilters(params, someTagsFilter(tag)))
|
|
990
|
+
}
|
|
991
|
+
|
|
992
|
+
/**
|
|
993
|
+
* Queries all documents from the Prismic repository with a specific tag.
|
|
994
|
+
*
|
|
995
|
+
* This method may make multiple network requests to query all matching
|
|
996
|
+
* content.
|
|
997
|
+
*
|
|
998
|
+
* @example
|
|
999
|
+
*
|
|
1000
|
+
* ```ts
|
|
1001
|
+
* const response = await client.getAllByTag("food")
|
|
1002
|
+
* ```
|
|
1003
|
+
*
|
|
1004
|
+
* @typeParam TDocument - Type of Prismic documents returned.
|
|
1005
|
+
*
|
|
1006
|
+
* @param tag - The tag that must be included on a document.
|
|
1007
|
+
* @param params - Parameters to filter, sort, and paginate the results.
|
|
1008
|
+
*
|
|
1009
|
+
* @returns A list of all documents with the tag.
|
|
1010
|
+
*/
|
|
1011
|
+
async getAllByTag<TDocument extends TDocuments>(
|
|
1012
|
+
tag: string,
|
|
1013
|
+
params?: Partial<Omit<BuildQueryURLArgs, "page">> &
|
|
1014
|
+
GetAllParams &
|
|
1015
|
+
FetchParams,
|
|
1016
|
+
): Promise<TDocument[]> {
|
|
1017
|
+
return await this.dangerouslyGetAll<TDocument>(
|
|
1018
|
+
appendFilters(params, someTagsFilter(tag)),
|
|
1019
|
+
)
|
|
1020
|
+
}
|
|
1021
|
+
|
|
1022
|
+
/**
|
|
1023
|
+
* Queries documents from the Prismic repository with specific tags. A
|
|
1024
|
+
* document must be tagged with all of the queried tags to be included.
|
|
1025
|
+
*
|
|
1026
|
+
* @example
|
|
1027
|
+
*
|
|
1028
|
+
* ```ts
|
|
1029
|
+
* const response = await client.getByEveryTag(["food", "fruit"])
|
|
1030
|
+
* ```
|
|
1031
|
+
*
|
|
1032
|
+
* @typeParam TDocument - Type of Prismic documents returned.
|
|
1033
|
+
*
|
|
1034
|
+
* @param tags - A list of tags that must be included on a document.
|
|
1035
|
+
* @param params - Parameters to filter, sort, and paginate the results.
|
|
1036
|
+
*
|
|
1037
|
+
* @returns A paginated response containing documents with the tags.
|
|
1038
|
+
*/
|
|
1039
|
+
async getByEveryTag<TDocument extends TDocuments>(
|
|
1040
|
+
tags: string[],
|
|
1041
|
+
params?: Partial<BuildQueryURLArgs> & FetchParams,
|
|
1042
|
+
): Promise<Query<TDocument>> {
|
|
1043
|
+
return await this.get<TDocument>(
|
|
1044
|
+
appendFilters(params, everyTagFilter(tags)),
|
|
1045
|
+
)
|
|
1046
|
+
}
|
|
1047
|
+
|
|
1048
|
+
/**
|
|
1049
|
+
* Queries documents from the Prismic repository with specific tags. A
|
|
1050
|
+
* document must be tagged with all of the queried tags to be included.
|
|
1051
|
+
*
|
|
1052
|
+
* This method may make multiple network requests to query all matching
|
|
1053
|
+
* content.
|
|
1054
|
+
*
|
|
1055
|
+
* @example
|
|
1056
|
+
*
|
|
1057
|
+
* ```ts
|
|
1058
|
+
* const response = await client.getAllByEveryTag(["food", "fruit"])
|
|
1059
|
+
* ```
|
|
1060
|
+
*
|
|
1061
|
+
* @typeParam TDocument - Type of Prismic documents returned.
|
|
1062
|
+
*
|
|
1063
|
+
* @param tags - A list of tags that must be included on a document.
|
|
1064
|
+
* @param params - Parameters to filter, sort, and paginate the results.
|
|
1065
|
+
*
|
|
1066
|
+
* @returns A list of all documents with the tags.
|
|
1067
|
+
*/
|
|
1068
|
+
async getAllByEveryTag<TDocument extends TDocuments>(
|
|
1069
|
+
tags: string[],
|
|
1070
|
+
params?: Partial<Omit<BuildQueryURLArgs, "page">> &
|
|
1071
|
+
GetAllParams &
|
|
1072
|
+
FetchParams,
|
|
1073
|
+
): Promise<TDocument[]> {
|
|
1074
|
+
return await this.dangerouslyGetAll<TDocument>(
|
|
1075
|
+
appendFilters(params, everyTagFilter(tags)),
|
|
1076
|
+
)
|
|
1077
|
+
}
|
|
1078
|
+
|
|
1079
|
+
/**
|
|
1080
|
+
* Queries documents from the Prismic repository with specific tags. A
|
|
1081
|
+
* document must be tagged with at least one of the queried tags to be
|
|
1082
|
+
* included.
|
|
1083
|
+
*
|
|
1084
|
+
* @example
|
|
1085
|
+
*
|
|
1086
|
+
* ```ts
|
|
1087
|
+
* const response = await client.getByEveryTag(["food", "fruit"])
|
|
1088
|
+
* ```
|
|
1089
|
+
*
|
|
1090
|
+
* @typeParam TDocument - Type of Prismic documents returned.
|
|
1091
|
+
*
|
|
1092
|
+
* @param tags - A list of tags that must be included on a document.
|
|
1093
|
+
* @param params - Parameters to filter, sort, and paginate the results.
|
|
1094
|
+
*
|
|
1095
|
+
* @returns A paginated response containing documents with at least one of the
|
|
1096
|
+
* tags.
|
|
1097
|
+
*/
|
|
1098
|
+
async getBySomeTags<TDocument extends TDocuments>(
|
|
1099
|
+
tags: string[],
|
|
1100
|
+
params?: Partial<BuildQueryURLArgs> & FetchParams,
|
|
1101
|
+
): Promise<Query<TDocument>> {
|
|
1102
|
+
return await this.get<TDocument>(
|
|
1103
|
+
appendFilters(params, someTagsFilter(tags)),
|
|
1104
|
+
)
|
|
1105
|
+
}
|
|
1106
|
+
|
|
1107
|
+
/**
|
|
1108
|
+
* Queries documents from the Prismic repository with specific tags. A
|
|
1109
|
+
* document must be tagged with at least one of the queried tags to be
|
|
1110
|
+
* included.
|
|
1111
|
+
*
|
|
1112
|
+
* This method may make multiple network requests to query all matching
|
|
1113
|
+
* content.
|
|
1114
|
+
*
|
|
1115
|
+
* @example
|
|
1116
|
+
*
|
|
1117
|
+
* ```ts
|
|
1118
|
+
* const response = await client.getAllBySomeTags(["food", "fruit"])
|
|
1119
|
+
* ```
|
|
1120
|
+
*
|
|
1121
|
+
* @typeParam TDocument - Type of Prismic documents returned.
|
|
1122
|
+
*
|
|
1123
|
+
* @param tags - A list of tags that must be included on a document.
|
|
1124
|
+
* @param params - Parameters to filter, sort, and paginate the results.
|
|
1125
|
+
*
|
|
1126
|
+
* @returns A list of all documents with at least one of the tags.
|
|
1127
|
+
*/
|
|
1128
|
+
async getAllBySomeTags<TDocument extends TDocuments>(
|
|
1129
|
+
tags: string[],
|
|
1130
|
+
params?: Partial<Omit<BuildQueryURLArgs, "page">> &
|
|
1131
|
+
GetAllParams &
|
|
1132
|
+
FetchParams,
|
|
1133
|
+
): Promise<TDocument[]> {
|
|
1134
|
+
return await this.dangerouslyGetAll<TDocument>(
|
|
1135
|
+
appendFilters(params, someTagsFilter(tags)),
|
|
1136
|
+
)
|
|
1137
|
+
}
|
|
1138
|
+
|
|
1139
|
+
/**
|
|
1140
|
+
* Returns metadata about the Prismic repository, such as its refs, releases,
|
|
1141
|
+
* and custom types.
|
|
1142
|
+
*
|
|
1143
|
+
* @returns Repository metadata.
|
|
1144
|
+
*/
|
|
1145
|
+
async getRepository(params?: FetchParams): Promise<Repository> {
|
|
1146
|
+
// TODO: Restore when Authorization header support works in browsers with CORS.
|
|
1147
|
+
// return await this.fetch<Repository>(this.endpoint);
|
|
1148
|
+
|
|
1149
|
+
const url = new URL(this.documentAPIEndpoint)
|
|
1150
|
+
|
|
1151
|
+
if (this.accessToken) {
|
|
1152
|
+
url.searchParams.set("access_token", this.accessToken)
|
|
1153
|
+
}
|
|
1154
|
+
|
|
1155
|
+
return await this.fetch<Repository>(url.toString(), params)
|
|
1156
|
+
}
|
|
1157
|
+
|
|
1158
|
+
/**
|
|
1159
|
+
* Returns a list of all refs for the Prismic repository.
|
|
1160
|
+
*
|
|
1161
|
+
* Refs are used to identify which version of the repository's content should
|
|
1162
|
+
* be queried. All repositories will have at least one ref pointing to the
|
|
1163
|
+
* latest published content called the "master ref".
|
|
1164
|
+
*
|
|
1165
|
+
* @returns A list of all refs for the Prismic repository.
|
|
1166
|
+
*/
|
|
1167
|
+
async getRefs(params?: FetchParams): Promise<Ref[]> {
|
|
1168
|
+
const repository = await this.getRepository(params)
|
|
1169
|
+
|
|
1170
|
+
return repository.refs
|
|
1171
|
+
}
|
|
1172
|
+
|
|
1173
|
+
/**
|
|
1174
|
+
* Returns a ref for the Prismic repository with a matching ID.
|
|
1175
|
+
*
|
|
1176
|
+
* @param id - ID of the ref.
|
|
1177
|
+
*
|
|
1178
|
+
* @returns The ref with a matching ID, if it exists.
|
|
1179
|
+
*/
|
|
1180
|
+
async getRefByID(id: string, params?: FetchParams): Promise<Ref> {
|
|
1181
|
+
const refs = await this.getRefs(params)
|
|
1182
|
+
|
|
1183
|
+
return findRefByID(refs, id)
|
|
1184
|
+
}
|
|
1185
|
+
|
|
1186
|
+
/**
|
|
1187
|
+
* Returns a ref for the Prismic repository with a matching label.
|
|
1188
|
+
*
|
|
1189
|
+
* @param label - Label of the ref.
|
|
1190
|
+
*
|
|
1191
|
+
* @returns The ref with a matching label, if it exists.
|
|
1192
|
+
*/
|
|
1193
|
+
async getRefByLabel(label: string, params?: FetchParams): Promise<Ref> {
|
|
1194
|
+
const refs = await this.getRefs(params)
|
|
1195
|
+
|
|
1196
|
+
return findRefByLabel(refs, label)
|
|
1197
|
+
}
|
|
1198
|
+
|
|
1199
|
+
/**
|
|
1200
|
+
* Returns the master ref for the Prismic repository. The master ref points to
|
|
1201
|
+
* the repository's latest published content.
|
|
1202
|
+
*
|
|
1203
|
+
* @returns The repository's master ref.
|
|
1204
|
+
*/
|
|
1205
|
+
async getMasterRef(params?: FetchParams): Promise<Ref> {
|
|
1206
|
+
const refs = await this.getRefs(params)
|
|
1207
|
+
|
|
1208
|
+
return findMasterRef(refs)
|
|
1209
|
+
}
|
|
1210
|
+
|
|
1211
|
+
/**
|
|
1212
|
+
* Returns a list of all Releases for the Prismic repository. Releases are
|
|
1213
|
+
* used to group content changes before publishing.
|
|
1214
|
+
*
|
|
1215
|
+
* @returns A list of all Releases for the Prismic repository.
|
|
1216
|
+
*/
|
|
1217
|
+
async getReleases(params?: FetchParams): Promise<Ref[]> {
|
|
1218
|
+
const refs = await this.getRefs(params)
|
|
1219
|
+
|
|
1220
|
+
return refs.filter((ref) => !ref.isMasterRef)
|
|
1221
|
+
}
|
|
1222
|
+
|
|
1223
|
+
/**
|
|
1224
|
+
* Returns a Release for the Prismic repository with a matching ID.
|
|
1225
|
+
*
|
|
1226
|
+
* @param id - ID of the Release.
|
|
1227
|
+
*
|
|
1228
|
+
* @returns The Release with a matching ID, if it exists.
|
|
1229
|
+
*/
|
|
1230
|
+
async getReleaseByID(id: string, params?: FetchParams): Promise<Ref> {
|
|
1231
|
+
const releases = await this.getReleases(params)
|
|
1232
|
+
|
|
1233
|
+
return findRefByID(releases, id)
|
|
1234
|
+
}
|
|
1235
|
+
|
|
1236
|
+
/**
|
|
1237
|
+
* Returns a Release for the Prismic repository with a matching label.
|
|
1238
|
+
*
|
|
1239
|
+
* @param label - Label of the ref.
|
|
1240
|
+
*
|
|
1241
|
+
* @returns The ref with a matching label, if it exists.
|
|
1242
|
+
*/
|
|
1243
|
+
async getReleaseByLabel(label: string, params?: FetchParams): Promise<Ref> {
|
|
1244
|
+
const releases = await this.getReleases(params)
|
|
1245
|
+
|
|
1246
|
+
return findRefByLabel(releases, label)
|
|
1247
|
+
}
|
|
1248
|
+
|
|
1249
|
+
/**
|
|
1250
|
+
* Returns a list of all tags used in the Prismic repository.
|
|
1251
|
+
*
|
|
1252
|
+
* @returns A list of all tags used in the repository.
|
|
1253
|
+
*/
|
|
1254
|
+
async getTags(params?: FetchParams): Promise<string[]> {
|
|
1255
|
+
try {
|
|
1256
|
+
const tagsForm = await this.getCachedRepositoryForm("tags", params)
|
|
1257
|
+
|
|
1258
|
+
const url = new URL(tagsForm.action)
|
|
1259
|
+
|
|
1260
|
+
if (this.accessToken) {
|
|
1261
|
+
url.searchParams.set("access_token", this.accessToken)
|
|
1262
|
+
}
|
|
1263
|
+
|
|
1264
|
+
return await this.fetch<string[]>(url.toString(), params)
|
|
1265
|
+
} catch {
|
|
1266
|
+
const repository = await this.getRepository(params)
|
|
1267
|
+
|
|
1268
|
+
return repository.tags
|
|
1269
|
+
}
|
|
1270
|
+
}
|
|
1271
|
+
|
|
1272
|
+
/**
|
|
1273
|
+
* Builds a URL used to query content from the Prismic repository.
|
|
1274
|
+
*
|
|
1275
|
+
* @param params - Parameters to filter, sort, and paginate the results.
|
|
1276
|
+
*
|
|
1277
|
+
* @returns A URL string that can be requested to query content.
|
|
1278
|
+
*/
|
|
1279
|
+
async buildQueryURL({
|
|
1280
|
+
signal,
|
|
1281
|
+
fetchOptions,
|
|
1282
|
+
...params
|
|
1283
|
+
}: Partial<BuildQueryURLArgs> & FetchParams = {}): Promise<string> {
|
|
1284
|
+
const ref =
|
|
1285
|
+
params.ref || (await this.getResolvedRefString({ signal, fetchOptions }))
|
|
1286
|
+
const integrationFieldsRef =
|
|
1287
|
+
params.integrationFieldsRef ||
|
|
1288
|
+
(await this.getCachedRepository({ signal, fetchOptions }))
|
|
1289
|
+
.integrationFieldsRef ||
|
|
1290
|
+
undefined
|
|
1291
|
+
|
|
1292
|
+
return buildQueryURL(this.documentAPIEndpoint, {
|
|
1293
|
+
...this.defaultParams,
|
|
1294
|
+
...params,
|
|
1295
|
+
ref,
|
|
1296
|
+
integrationFieldsRef,
|
|
1297
|
+
routes: params.routes || this.routes,
|
|
1298
|
+
brokenRoute: params.brokenRoute || this.brokenRoute,
|
|
1299
|
+
accessToken: params.accessToken || this.accessToken,
|
|
1300
|
+
})
|
|
1301
|
+
}
|
|
1302
|
+
|
|
1303
|
+
/**
|
|
1304
|
+
* Determines the URL for a previewed document during an active preview
|
|
1305
|
+
* session. The result of this method should be used to redirect the user to
|
|
1306
|
+
* the document's URL.
|
|
1307
|
+
*
|
|
1308
|
+
* @example
|
|
1309
|
+
*
|
|
1310
|
+
* ```ts
|
|
1311
|
+
* const url = client.resolvePreviewURL({
|
|
1312
|
+
* linkResolver: (document) => `/${document.uid}`
|
|
1313
|
+
* defaultURL: '/'
|
|
1314
|
+
* })
|
|
1315
|
+
* ```
|
|
1316
|
+
*
|
|
1317
|
+
* @param args - Arguments to configure the URL resolving.
|
|
1318
|
+
*
|
|
1319
|
+
* @returns The URL for the previewed document during an active preview
|
|
1320
|
+
* session. The user should be redirected to this URL.
|
|
1321
|
+
*/
|
|
1322
|
+
async resolvePreviewURL<LinkResolverReturnType>(
|
|
1323
|
+
args: ResolvePreviewArgs<LinkResolverReturnType> & FetchParams,
|
|
1324
|
+
): Promise<string> {
|
|
1325
|
+
let documentID: string | undefined | null = args.documentID
|
|
1326
|
+
let previewToken: string | undefined | null = args.previewToken
|
|
1327
|
+
|
|
1328
|
+
if (typeof globalThis.location !== "undefined") {
|
|
1329
|
+
const searchParams = new URLSearchParams(globalThis.location.search)
|
|
1330
|
+
|
|
1331
|
+
documentID = documentID || searchParams.get("documentId")
|
|
1332
|
+
previewToken = previewToken || searchParams.get("token")
|
|
1333
|
+
} else if (this.refState.httpRequest) {
|
|
1334
|
+
if ("query" in this.refState.httpRequest) {
|
|
1335
|
+
documentID =
|
|
1336
|
+
documentID || (this.refState.httpRequest.query?.documentId as string)
|
|
1337
|
+
previewToken =
|
|
1338
|
+
previewToken || (this.refState.httpRequest.query?.token as string)
|
|
1339
|
+
} else if (
|
|
1340
|
+
"url" in this.refState.httpRequest &&
|
|
1341
|
+
this.refState.httpRequest.url
|
|
1342
|
+
) {
|
|
1343
|
+
// Including "missing-host://" by default
|
|
1344
|
+
// handles a case where Next.js Route Handlers
|
|
1345
|
+
// only provide the pathname and search
|
|
1346
|
+
// parameters in the `url` property
|
|
1347
|
+
// (e.g. `/api/preview?foo=bar`).
|
|
1348
|
+
const searchParams = new URL(
|
|
1349
|
+
this.refState.httpRequest.url,
|
|
1350
|
+
"missing-host://",
|
|
1351
|
+
).searchParams
|
|
1352
|
+
|
|
1353
|
+
documentID = documentID || searchParams.get("documentId")
|
|
1354
|
+
previewToken = previewToken || searchParams.get("token")
|
|
1355
|
+
}
|
|
1356
|
+
}
|
|
1357
|
+
|
|
1358
|
+
if (documentID != null && previewToken != null) {
|
|
1359
|
+
const document = await this.getByID(documentID, {
|
|
1360
|
+
ref: previewToken,
|
|
1361
|
+
lang: "*",
|
|
1362
|
+
signal: args.signal,
|
|
1363
|
+
fetchOptions: args.fetchOptions,
|
|
1364
|
+
})
|
|
1365
|
+
|
|
1366
|
+
const url = asLink(document, { linkResolver: args.linkResolver })
|
|
1367
|
+
|
|
1368
|
+
if (typeof url === "string") {
|
|
1369
|
+
return url
|
|
1370
|
+
}
|
|
1371
|
+
}
|
|
1372
|
+
|
|
1373
|
+
return args.defaultURL
|
|
1374
|
+
}
|
|
1375
|
+
|
|
1376
|
+
/**
|
|
1377
|
+
* Configures the client to query the latest published content for all future
|
|
1378
|
+
* queries.
|
|
1379
|
+
*
|
|
1380
|
+
* If the `ref` parameter is provided during a query, it takes priority for
|
|
1381
|
+
* that query.
|
|
1382
|
+
*
|
|
1383
|
+
* @example
|
|
1384
|
+
*
|
|
1385
|
+
* ```ts
|
|
1386
|
+
* await client.queryLatestContent()
|
|
1387
|
+
* const document = await client.getByID("WW4bKScAAMAqmluX")
|
|
1388
|
+
* ```
|
|
1389
|
+
*/
|
|
1390
|
+
queryLatestContent(): void {
|
|
1391
|
+
this.refState.mode = RefStateMode.Master
|
|
1392
|
+
}
|
|
1393
|
+
|
|
1394
|
+
/**
|
|
1395
|
+
* Configures the client to query content from a specific Release identified
|
|
1396
|
+
* by its ID for all future queries.
|
|
1397
|
+
*
|
|
1398
|
+
* If the `ref` parameter is provided during a query, it takes priority for
|
|
1399
|
+
* that query.
|
|
1400
|
+
*
|
|
1401
|
+
* @example
|
|
1402
|
+
*
|
|
1403
|
+
* ```ts
|
|
1404
|
+
* await client.queryContentFromReleaseByID("YLB7OBAAACMA7Cpa")
|
|
1405
|
+
* const document = await client.getByID("WW4bKScAAMAqmluX")
|
|
1406
|
+
* ```
|
|
1407
|
+
*
|
|
1408
|
+
* @param releaseID - The ID of the Release.
|
|
1409
|
+
*/
|
|
1410
|
+
queryContentFromReleaseByID(releaseID: string): void {
|
|
1411
|
+
this.refState = {
|
|
1412
|
+
...this.refState,
|
|
1413
|
+
mode: RefStateMode.ReleaseID,
|
|
1414
|
+
releaseID,
|
|
1415
|
+
}
|
|
1416
|
+
}
|
|
1417
|
+
|
|
1418
|
+
/**
|
|
1419
|
+
* Configures the client to query content from a specific Release identified
|
|
1420
|
+
* by its label for all future queries.
|
|
1421
|
+
*
|
|
1422
|
+
* If the `ref` parameter is provided during a query, it takes priority for
|
|
1423
|
+
* that query.
|
|
1424
|
+
*
|
|
1425
|
+
* @example
|
|
1426
|
+
*
|
|
1427
|
+
* ```ts
|
|
1428
|
+
* await client.queryContentFromReleaseByLabel("My Release")
|
|
1429
|
+
* const document = await client.getByID("WW4bKScAAMAqmluX")
|
|
1430
|
+
* ```
|
|
1431
|
+
*
|
|
1432
|
+
* @param releaseLabel - The label of the Release.
|
|
1433
|
+
*/
|
|
1434
|
+
queryContentFromReleaseByLabel(releaseLabel: string): void {
|
|
1435
|
+
this.refState = {
|
|
1436
|
+
...this.refState,
|
|
1437
|
+
mode: RefStateMode.ReleaseLabel,
|
|
1438
|
+
releaseLabel,
|
|
1439
|
+
}
|
|
1440
|
+
}
|
|
1441
|
+
|
|
1442
|
+
/**
|
|
1443
|
+
* Configures the client to query content from a specific ref. The ref can be
|
|
1444
|
+
* provided as a string or a function.
|
|
1445
|
+
*
|
|
1446
|
+
* If a function is provided, the ref is fetched lazily before each query. The
|
|
1447
|
+
* function may also be asynchronous.
|
|
1448
|
+
*
|
|
1449
|
+
* @example
|
|
1450
|
+
*
|
|
1451
|
+
* ```ts
|
|
1452
|
+
* await client.queryContentFromRef("my-ref")
|
|
1453
|
+
* const document = await client.getByID("WW4bKScAAMAqmluX")
|
|
1454
|
+
* ```
|
|
1455
|
+
*
|
|
1456
|
+
* @param ref - The ref or a function that returns the ref from which to query
|
|
1457
|
+
* content.
|
|
1458
|
+
*/
|
|
1459
|
+
queryContentFromRef(ref: RefStringOrThunk): void {
|
|
1460
|
+
this.refState = {
|
|
1461
|
+
...this.refState,
|
|
1462
|
+
mode: RefStateMode.Manual,
|
|
1463
|
+
ref,
|
|
1464
|
+
}
|
|
1465
|
+
}
|
|
1466
|
+
|
|
1467
|
+
/**
|
|
1468
|
+
* A `fetch()` function to be used with GraphQL clients configured for
|
|
1469
|
+
* Prismic's GraphQL API. It automatically applies the necessary `prismic-ref`
|
|
1470
|
+
* and Authorization headers. Queries will automatically be minified by
|
|
1471
|
+
* removing whitespace where possible.
|
|
1472
|
+
*
|
|
1473
|
+
* @example
|
|
1474
|
+
*
|
|
1475
|
+
* ```ts
|
|
1476
|
+
* const graphQLClient = new ApolloClient({
|
|
1477
|
+
* link: new HttpLink({
|
|
1478
|
+
* uri: prismic.getGraphQLEndpoint(repositoryName),
|
|
1479
|
+
* // Provide `client.graphQLFetch` as the fetch implementation.
|
|
1480
|
+
* fetch: client.graphQLFetch,
|
|
1481
|
+
* // Using GET is required.
|
|
1482
|
+
* useGETForQueries: true,
|
|
1483
|
+
* }),
|
|
1484
|
+
* cache: new InMemoryCache(),
|
|
1485
|
+
* })
|
|
1486
|
+
* ```
|
|
1487
|
+
*
|
|
1488
|
+
* @param input - The `fetch()` `input` parameter. Only strings are supported.
|
|
1489
|
+
* @param init - The `fetch()` `init` parameter. Only plain objects are
|
|
1490
|
+
* supported.
|
|
1491
|
+
*
|
|
1492
|
+
* @returns The `fetch()` Response for the request.
|
|
1493
|
+
*
|
|
1494
|
+
* @experimental
|
|
1495
|
+
*/
|
|
1496
|
+
async graphQLFetch(
|
|
1497
|
+
input: RequestInfo,
|
|
1498
|
+
init?: Omit<RequestInit, "signal"> & { signal?: AbortSignalLike },
|
|
1499
|
+
): Promise<Response> {
|
|
1500
|
+
const cachedRepository = await this.getCachedRepository()
|
|
1501
|
+
const ref = await this.getResolvedRefString()
|
|
1502
|
+
|
|
1503
|
+
const unsanitizedHeaders: Record<string, string> = {
|
|
1504
|
+
"Prismic-ref": ref,
|
|
1505
|
+
Authorization: this.accessToken ? `Token ${this.accessToken}` : "",
|
|
1506
|
+
// Asserting `init.headers` is a Record since popular GraphQL
|
|
1507
|
+
// libraries pass this as a Record. Header objects as input
|
|
1508
|
+
// are unsupported.
|
|
1509
|
+
...(init ? (init.headers as Record<string, string>) : {}),
|
|
1510
|
+
}
|
|
1511
|
+
|
|
1512
|
+
if (cachedRepository.integrationFieldsRef) {
|
|
1513
|
+
unsanitizedHeaders["Prismic-integration-field-ref"] =
|
|
1514
|
+
cachedRepository.integrationFieldsRef
|
|
1515
|
+
}
|
|
1516
|
+
|
|
1517
|
+
// Normalize header keys to lowercase. This prevents header
|
|
1518
|
+
// conflicts between the Prismic client and the GraphQL
|
|
1519
|
+
// client.
|
|
1520
|
+
const headers: Record<string, string> = {}
|
|
1521
|
+
for (const key in unsanitizedHeaders) {
|
|
1522
|
+
if (unsanitizedHeaders[key]) {
|
|
1523
|
+
headers[key.toLowerCase()] =
|
|
1524
|
+
unsanitizedHeaders[key as keyof typeof unsanitizedHeaders]
|
|
1525
|
+
}
|
|
1526
|
+
}
|
|
1527
|
+
|
|
1528
|
+
const url = new URL(
|
|
1529
|
+
// Asserting `input` is a string since popular GraphQL
|
|
1530
|
+
// libraries pass this as a string. Request objects as
|
|
1531
|
+
// input are unsupported.
|
|
1532
|
+
input as string,
|
|
1533
|
+
)
|
|
1534
|
+
|
|
1535
|
+
// This prevents the request from being cached unnecessarily.
|
|
1536
|
+
// Without adding this `ref` param, re-running a query
|
|
1537
|
+
// could return a locally cached response, even if the
|
|
1538
|
+
// `ref` changed. This happens because the URL is
|
|
1539
|
+
// identical when the `ref` is not included. Caches may ignore
|
|
1540
|
+
// headers.
|
|
1541
|
+
//
|
|
1542
|
+
// The Prismic GraphQL API ignores the `ref` param.
|
|
1543
|
+
url.searchParams.set("ref", ref)
|
|
1544
|
+
|
|
1545
|
+
const query = url.searchParams.get("query")
|
|
1546
|
+
if (query) {
|
|
1547
|
+
url.searchParams.set(
|
|
1548
|
+
"query",
|
|
1549
|
+
// Compress the GraphQL query (if it exists) by
|
|
1550
|
+
// removing whitespace. This is done to
|
|
1551
|
+
// optimize the query size and avoid
|
|
1552
|
+
// hitting the upper limit of GET requests
|
|
1553
|
+
// (2048 characters).
|
|
1554
|
+
minifyGraphQLQuery(query),
|
|
1555
|
+
)
|
|
1556
|
+
}
|
|
1557
|
+
|
|
1558
|
+
return (await this.fetchFn(url.toString(), {
|
|
1559
|
+
...init,
|
|
1560
|
+
headers,
|
|
1561
|
+
})) as Response
|
|
1562
|
+
}
|
|
1563
|
+
|
|
1564
|
+
/**
|
|
1565
|
+
* Returns a cached version of `getRepository` with a TTL.
|
|
1566
|
+
*
|
|
1567
|
+
* @returns Cached repository metadata.
|
|
1568
|
+
*/
|
|
1569
|
+
private async getCachedRepository(params?: FetchParams): Promise<Repository> {
|
|
1570
|
+
if (
|
|
1571
|
+
!this.cachedRepository ||
|
|
1572
|
+
Date.now() >= this.cachedRepositoryExpiration
|
|
1573
|
+
) {
|
|
1574
|
+
this.cachedRepositoryExpiration = Date.now() + REPOSITORY_CACHE_TTL
|
|
1575
|
+
this.cachedRepository = await this.getRepository(params)
|
|
1576
|
+
}
|
|
1577
|
+
|
|
1578
|
+
return this.cachedRepository
|
|
1579
|
+
}
|
|
1580
|
+
|
|
1581
|
+
/**
|
|
1582
|
+
* Returns a cached Prismic repository form. Forms are used to determine API
|
|
1583
|
+
* endpoints for types of repository data.
|
|
1584
|
+
*
|
|
1585
|
+
* @param name - Name of the form.
|
|
1586
|
+
*
|
|
1587
|
+
* @returns The repository form.
|
|
1588
|
+
*
|
|
1589
|
+
* @throws If a matching form cannot be found.
|
|
1590
|
+
*/
|
|
1591
|
+
private async getCachedRepositoryForm(
|
|
1592
|
+
name: string,
|
|
1593
|
+
params?: FetchParams,
|
|
1594
|
+
): Promise<Form> {
|
|
1595
|
+
const cachedRepository = await this.getCachedRepository(params)
|
|
1596
|
+
const form = cachedRepository.forms[name]
|
|
1597
|
+
|
|
1598
|
+
if (!form) {
|
|
1599
|
+
throw new PrismicError(
|
|
1600
|
+
`Form with name "${name}" could not be found`,
|
|
1601
|
+
undefined,
|
|
1602
|
+
undefined,
|
|
1603
|
+
)
|
|
1604
|
+
}
|
|
1605
|
+
|
|
1606
|
+
return form
|
|
1607
|
+
}
|
|
1608
|
+
|
|
1609
|
+
/**
|
|
1610
|
+
* Returns the ref needed to query based on the client's current state. This
|
|
1611
|
+
* method may make a network request to fetch a ref or resolve the user's ref
|
|
1612
|
+
* thunk.
|
|
1613
|
+
*
|
|
1614
|
+
* If auto previews are enabled, the preview ref takes priority if available.
|
|
1615
|
+
*
|
|
1616
|
+
* The following strategies are used depending on the client's state:
|
|
1617
|
+
*
|
|
1618
|
+
* - If the user called `queryLatestContent`: Use the repository's master ref.
|
|
1619
|
+
* The ref is cached for 5 seconds. After 5 seconds, a new master ref is
|
|
1620
|
+
* fetched.
|
|
1621
|
+
* - If the user called `queryContentFromReleaseByID`: Use the release's ref.
|
|
1622
|
+
* The ref is cached for 5 seconds. After 5 seconds, a new ref for the
|
|
1623
|
+
* release is fetched.
|
|
1624
|
+
* - If the user called `queryContentFromReleaseByLabel`: Use the release's ref.
|
|
1625
|
+
* The ref is cached for 5 seconds. After 5 seconds, a new ref for the
|
|
1626
|
+
* release is fetched.
|
|
1627
|
+
* - If the user called `queryContentFromRef`: Use the provided ref. Fall back
|
|
1628
|
+
* to the master ref if the ref is not a string.
|
|
1629
|
+
*
|
|
1630
|
+
* @returns The ref to use during a query.
|
|
1631
|
+
*/
|
|
1632
|
+
private async getResolvedRefString(params?: FetchParams): Promise<string> {
|
|
1633
|
+
if (this.refState.autoPreviewsEnabled) {
|
|
1634
|
+
let previewRef: string | undefined
|
|
1635
|
+
|
|
1636
|
+
let cookieJar: string | null | undefined
|
|
1637
|
+
|
|
1638
|
+
if (this.refState.httpRequest?.headers) {
|
|
1639
|
+
if (
|
|
1640
|
+
"get" in this.refState.httpRequest.headers &&
|
|
1641
|
+
typeof this.refState.httpRequest.headers.get === "function"
|
|
1642
|
+
) {
|
|
1643
|
+
// Web API Headers
|
|
1644
|
+
cookieJar = this.refState.httpRequest.headers.get("cookie")
|
|
1645
|
+
} else if ("cookie" in this.refState.httpRequest.headers) {
|
|
1646
|
+
// Express-style headers
|
|
1647
|
+
cookieJar = this.refState.httpRequest.headers.cookie
|
|
1648
|
+
}
|
|
1649
|
+
} else if (globalThis.document?.cookie) {
|
|
1650
|
+
cookieJar = globalThis.document.cookie
|
|
1651
|
+
}
|
|
1652
|
+
|
|
1653
|
+
if (cookieJar) {
|
|
1654
|
+
previewRef = getPreviewCookie(cookieJar)
|
|
1655
|
+
}
|
|
1656
|
+
|
|
1657
|
+
if (previewRef) {
|
|
1658
|
+
return previewRef
|
|
1659
|
+
}
|
|
1660
|
+
}
|
|
1661
|
+
|
|
1662
|
+
const cachedRepository = await this.getCachedRepository(params)
|
|
1663
|
+
|
|
1664
|
+
const refModeType = this.refState.mode
|
|
1665
|
+
if (refModeType === RefStateMode.ReleaseID) {
|
|
1666
|
+
return findRefByID(cachedRepository.refs, this.refState.releaseID).ref
|
|
1667
|
+
} else if (refModeType === RefStateMode.ReleaseLabel) {
|
|
1668
|
+
return findRefByLabel(cachedRepository.refs, this.refState.releaseLabel)
|
|
1669
|
+
.ref
|
|
1670
|
+
} else if (refModeType === RefStateMode.Manual) {
|
|
1671
|
+
const res = await castThunk(this.refState.ref)()
|
|
1672
|
+
|
|
1673
|
+
if (typeof res === "string") {
|
|
1674
|
+
return res
|
|
1675
|
+
}
|
|
1676
|
+
}
|
|
1677
|
+
|
|
1678
|
+
return findMasterRef(cachedRepository.refs).ref
|
|
1679
|
+
}
|
|
1680
|
+
|
|
1681
|
+
/**
|
|
1682
|
+
* Performs a network request using the configured `fetch` function. It
|
|
1683
|
+
* assumes all successful responses will have a JSON content type. It also
|
|
1684
|
+
* normalizes unsuccessful network requests.
|
|
1685
|
+
*
|
|
1686
|
+
* @typeParam T - The JSON response.
|
|
1687
|
+
*
|
|
1688
|
+
* @param url - URL to the resource to fetch.
|
|
1689
|
+
* @param params - Prismic REST API parameters for the network request.
|
|
1690
|
+
*
|
|
1691
|
+
* @returns The JSON response from the network request.
|
|
1692
|
+
*/
|
|
1693
|
+
protected async fetch<T = unknown>(
|
|
1694
|
+
url: string,
|
|
1695
|
+
params: FetchParams = {},
|
|
1696
|
+
): Promise<T> {
|
|
1697
|
+
const res = await super.fetch(url, params)
|
|
1698
|
+
|
|
1699
|
+
if (res.status !== 404 && res.status !== 429 && res.json == null) {
|
|
1700
|
+
throw new PrismicError(undefined, url, res.json || res.text)
|
|
1701
|
+
}
|
|
1702
|
+
|
|
1703
|
+
switch (res.status) {
|
|
1704
|
+
// Successful
|
|
1705
|
+
case 200:
|
|
1706
|
+
case 201: {
|
|
1707
|
+
return res.json
|
|
1708
|
+
}
|
|
1709
|
+
|
|
1710
|
+
// Bad Request
|
|
1711
|
+
// - Invalid filter syntax
|
|
1712
|
+
// - Ref not provided (ignored)
|
|
1713
|
+
case 400: {
|
|
1714
|
+
throw new ParsingError(res.json.message, url, res.json)
|
|
1715
|
+
}
|
|
1716
|
+
|
|
1717
|
+
// Unauthorized
|
|
1718
|
+
// - Missing access token for repository endpoint
|
|
1719
|
+
// - Incorrect access token for repository endpoint
|
|
1720
|
+
case 401:
|
|
1721
|
+
// Forbidden
|
|
1722
|
+
// - Missing access token for query endpoint
|
|
1723
|
+
// - Incorrect access token for query endpoint
|
|
1724
|
+
case 403: {
|
|
1725
|
+
throw new ForbiddenError(
|
|
1726
|
+
res.json.error || res.json.message,
|
|
1727
|
+
url,
|
|
1728
|
+
res.json,
|
|
1729
|
+
)
|
|
1730
|
+
}
|
|
1731
|
+
|
|
1732
|
+
// Not Found
|
|
1733
|
+
// - Incorrect repository name (this response has an empty body)
|
|
1734
|
+
// - Ref does not exist
|
|
1735
|
+
// - Preview token is expired
|
|
1736
|
+
case 404: {
|
|
1737
|
+
if (res.json === undefined) {
|
|
1738
|
+
throw new RepositoryNotFoundError(
|
|
1739
|
+
`Prismic repository not found. Check that "${this.documentAPIEndpoint}" is pointing to the correct repository.`,
|
|
1740
|
+
url,
|
|
1741
|
+
url.startsWith(this.documentAPIEndpoint) ? undefined : res.text,
|
|
1742
|
+
)
|
|
1743
|
+
}
|
|
1744
|
+
|
|
1745
|
+
if (res.json.type === "api_notfound_error") {
|
|
1746
|
+
throw new RefNotFoundError(res.json.message, url, res.json)
|
|
1747
|
+
}
|
|
1748
|
+
|
|
1749
|
+
if (
|
|
1750
|
+
res.json.type === "api_security_error" &&
|
|
1751
|
+
/preview token.*expired/i.test(res.json.message)
|
|
1752
|
+
) {
|
|
1753
|
+
throw new PreviewTokenExpiredError(res.json.message, url, res.json)
|
|
1754
|
+
}
|
|
1755
|
+
|
|
1756
|
+
throw new NotFoundError(res.json.message, url, res.json)
|
|
1757
|
+
}
|
|
1758
|
+
|
|
1759
|
+
// Gone
|
|
1760
|
+
// - Ref is expired
|
|
1761
|
+
case 410: {
|
|
1762
|
+
throw new RefExpiredError(res.json.message, url, res.json)
|
|
1763
|
+
}
|
|
1764
|
+
|
|
1765
|
+
// Too Many Requests
|
|
1766
|
+
// - Exceeded the maximum number of requests per second
|
|
1767
|
+
case 429: {
|
|
1768
|
+
const parsedRetryAfter = Number(res.headers.get("retry-after"))
|
|
1769
|
+
const delay = Number.isNaN(parsedRetryAfter)
|
|
1770
|
+
? DEFUALT_RETRY_AFTER_MS
|
|
1771
|
+
: parsedRetryAfter
|
|
1772
|
+
|
|
1773
|
+
return await new Promise((resolve, reject) => {
|
|
1774
|
+
setTimeout(async () => {
|
|
1775
|
+
try {
|
|
1776
|
+
resolve(await this.fetch(url, params))
|
|
1777
|
+
} catch (error) {
|
|
1778
|
+
reject(error)
|
|
1779
|
+
}
|
|
1780
|
+
}, delay)
|
|
1781
|
+
})
|
|
1782
|
+
}
|
|
1783
|
+
}
|
|
1784
|
+
|
|
1785
|
+
throw new PrismicError(undefined, url, res.json)
|
|
1786
|
+
}
|
|
1787
|
+
}
|