@prismicio/client 7.19.2 → 7.20.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/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 +95 -111
- package/dist/Client.cjs.map +1 -1
- package/dist/Client.d.ts +27 -66
- package/dist/Client.js +95 -111
- package/dist/Client.js.map +1 -1
- package/dist/WriteClient.cjs +100 -151
- package/dist/WriteClient.cjs.map +1 -1
- package/dist/WriteClient.d.ts +26 -2
- package/dist/WriteClient.js +100 -151
- package/dist/WriteClient.js.map +1 -1
- package/dist/errors/ParsingError.cjs.map +1 -1
- package/dist/errors/ParsingError.d.ts +4 -6
- package/dist/errors/ParsingError.js.map +1 -1
- package/dist/helpers/isFilled.cjs.map +1 -1
- package/dist/helpers/isFilled.d.ts +4 -4
- package/dist/helpers/isFilled.js.map +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +5 -3
- package/dist/index.js.map +1 -1
- package/dist/package.json.cjs +1 -1
- package/dist/package.json.js +1 -1
- package/dist/types/integration.d.ts +28 -0
- package/dist/types/value/integration.d.ts +8 -2
- package/package.json +1 -1
- package/src/BaseClient.ts +330 -0
- package/src/Client.ts +171 -201
- package/src/WriteClient.ts +157 -214
- package/src/errors/ParsingError.ts +4 -6
- package/src/helpers/isFilled.ts +5 -4
- package/src/index.ts +13 -8
- package/src/types/integration.ts +34 -0
- package/src/types/value/integration.ts +9 -2
- package/dist/errors/InvalidDataError.cjs +0 -7
- package/dist/errors/InvalidDataError.cjs.map +0 -1
- package/dist/errors/InvalidDataError.d.ts +0 -3
- package/dist/errors/InvalidDataError.js +0 -7
- package/dist/errors/InvalidDataError.js.map +0 -1
- package/dist/lib/request.cjs +0 -43
- package/dist/lib/request.cjs.map +0 -1
- package/dist/lib/request.d.ts +0 -73
- package/dist/lib/request.js +0 -43
- package/dist/lib/request.js.map +0 -1
- package/src/errors/InvalidDataError.ts +0 -5
- package/src/lib/request.ts +0 -166
package/src/Client.ts
CHANGED
|
@@ -7,20 +7,13 @@ import { findRefByID } from "./lib/findRefByID"
|
|
|
7
7
|
import { findRefByLabel } from "./lib/findRefByLabel"
|
|
8
8
|
import { getPreviewCookie } from "./lib/getPreviewCookie"
|
|
9
9
|
import { minifyGraphQLQuery } from "./lib/minifyGraphQLQuery"
|
|
10
|
-
import type { ResponseLike } from "./lib/request"
|
|
11
|
-
import {
|
|
12
|
-
type AbortSignalLike,
|
|
13
|
-
type FetchLike,
|
|
14
|
-
type RequestInitLike,
|
|
15
|
-
request,
|
|
16
|
-
} from "./lib/request"
|
|
17
10
|
import { someTagsFilter } from "./lib/someTagsFilter"
|
|
18
11
|
import { throttledLog } from "./lib/throttledLog"
|
|
19
12
|
import { typeFilter } from "./lib/typeFilter"
|
|
20
13
|
|
|
21
14
|
import type { Query } from "./types/api/query"
|
|
22
15
|
import type { Ref } from "./types/api/ref"
|
|
23
|
-
import type { Repository } from "./types/api/repository"
|
|
16
|
+
import type { Form, Repository } from "./types/api/repository"
|
|
24
17
|
import type { PrismicDocument } from "./types/value/document"
|
|
25
18
|
|
|
26
19
|
import { ForbiddenError } from "./errors/ForbiddenError"
|
|
@@ -35,6 +28,13 @@ import { RepositoryNotFoundError } from "./errors/RepositoryNotFoundError"
|
|
|
35
28
|
import type { LinkResolverFunction } from "./helpers/asLink"
|
|
36
29
|
import { asLink } from "./helpers/asLink"
|
|
37
30
|
|
|
31
|
+
import {
|
|
32
|
+
type AbortSignalLike,
|
|
33
|
+
BaseClient,
|
|
34
|
+
type BaseClientConfig,
|
|
35
|
+
type FetchParams,
|
|
36
|
+
type HttpRequestLike,
|
|
37
|
+
} from "./BaseClient"
|
|
38
38
|
import type { BuildQueryURLArgs } from "./buildQueryURL"
|
|
39
39
|
import { buildQueryURL } from "./buildQueryURL"
|
|
40
40
|
import { filter } from "./filter"
|
|
@@ -66,11 +66,20 @@ export const REPOSITORY_CACHE_TTL = 5000
|
|
|
66
66
|
export const GET_ALL_QUERY_DELAY = 500
|
|
67
67
|
|
|
68
68
|
/**
|
|
69
|
-
* The
|
|
69
|
+
* The default number of milliseconds to wait before retrying a rate-limited
|
|
70
|
+
* `fetch()` request (429 response code). The default value is only used if the
|
|
71
|
+
* response does not include a `retry-after` header.
|
|
72
|
+
*
|
|
73
|
+
* The API allows up to 200 requests per second.
|
|
74
|
+
*/
|
|
75
|
+
const DEFUALT_RETRY_AFTER_MS = 1000
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* The maximum number of attemps to retry a query with an invalid ref. We allow
|
|
70
79
|
* multiple attempts since each attempt may use a different (and possibly
|
|
71
|
-
* invalid) ref. Capping the number of
|
|
80
|
+
* invalid) ref. Capping the number of attemps prevents infinite loops.
|
|
72
81
|
*/
|
|
73
|
-
const
|
|
82
|
+
const MAX_INVALID_REF_RETRY_ATTEMPS = 3
|
|
74
83
|
|
|
75
84
|
/**
|
|
76
85
|
* Extracts one or more Prismic document types that match a given Prismic
|
|
@@ -113,33 +122,6 @@ enum RefStateMode {
|
|
|
113
122
|
Manual = "Manual",
|
|
114
123
|
}
|
|
115
124
|
|
|
116
|
-
/**
|
|
117
|
-
* The minimum required properties to treat as an HTTP Request for automatic
|
|
118
|
-
* Prismic preview support.
|
|
119
|
-
*/
|
|
120
|
-
export type HttpRequestLike =
|
|
121
|
-
| /**
|
|
122
|
-
* Web API Request
|
|
123
|
-
*
|
|
124
|
-
* @see http://developer.mozilla.org/en-US/docs/Web/API/Request
|
|
125
|
-
*/
|
|
126
|
-
{
|
|
127
|
-
headers?: {
|
|
128
|
-
get(name: string): string | null
|
|
129
|
-
}
|
|
130
|
-
url?: string
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* Express-style Request
|
|
135
|
-
*/
|
|
136
|
-
| {
|
|
137
|
-
headers?: {
|
|
138
|
-
cookie?: string
|
|
139
|
-
}
|
|
140
|
-
query?: Record<string, unknown>
|
|
141
|
-
}
|
|
142
|
-
|
|
143
125
|
/**
|
|
144
126
|
* An object containing stateful information about a client's ref strategy.
|
|
145
127
|
*/
|
|
@@ -181,28 +163,6 @@ type RefStringOrThunk =
|
|
|
181
163
|
| string
|
|
182
164
|
| (() => string | undefined | Promise<string | undefined>)
|
|
183
165
|
|
|
184
|
-
/**
|
|
185
|
-
* Parameters for any client method that use `fetch()`.
|
|
186
|
-
*/
|
|
187
|
-
export type FetchParams = {
|
|
188
|
-
/**
|
|
189
|
-
* Options provided to the client's `fetch()` on all network requests. These
|
|
190
|
-
* options will be merged with internally required options. They can also be
|
|
191
|
-
* overriden on a per-query basis using the query's `fetchOptions` parameter.
|
|
192
|
-
*/
|
|
193
|
-
fetchOptions?: RequestInitLike
|
|
194
|
-
|
|
195
|
-
/**
|
|
196
|
-
* An `AbortSignal` provided by an `AbortController`. This allows the network
|
|
197
|
-
* request to be cancelled if necessary.
|
|
198
|
-
*
|
|
199
|
-
* @deprecated Move the `signal` parameter into `fetchOptions.signal`:
|
|
200
|
-
*
|
|
201
|
-
* @see \<https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal\>
|
|
202
|
-
*/
|
|
203
|
-
signal?: AbortSignalLike
|
|
204
|
-
}
|
|
205
|
-
|
|
206
166
|
/**
|
|
207
167
|
* Configuration for clients that determine how content is queried.
|
|
208
168
|
*/
|
|
@@ -254,21 +214,7 @@ export type ClientConfig = {
|
|
|
254
214
|
BuildQueryURLArgs,
|
|
255
215
|
"ref" | "integrationFieldsRef" | "accessToken" | "routes" | "brokenRoute"
|
|
256
216
|
>
|
|
257
|
-
|
|
258
|
-
/**
|
|
259
|
-
* The function used to make network requests to the Prismic REST API. In
|
|
260
|
-
* environments where a global `fetch` function does not exist, such as
|
|
261
|
-
* Node.js, this function must be provided.
|
|
262
|
-
*/
|
|
263
|
-
fetch?: FetchLike
|
|
264
|
-
|
|
265
|
-
/**
|
|
266
|
-
* Options provided to the client's `fetch()` on all network requests. These
|
|
267
|
-
* options will be merged with internally required options. They can also be
|
|
268
|
-
* overriden on a per-query basis using the query's `fetchOptions` parameter.
|
|
269
|
-
*/
|
|
270
|
-
fetchOptions?: RequestInitLike
|
|
271
|
-
}
|
|
217
|
+
} & BaseClientConfig
|
|
272
218
|
|
|
273
219
|
/**
|
|
274
220
|
* Parameters specific to client methods that fetch all documents. These methods
|
|
@@ -318,16 +264,9 @@ type ResolvePreviewArgs<LinkResolverReturnType> = {
|
|
|
318
264
|
* @typeParam TDocuments - Document types that are registered for the Prismic
|
|
319
265
|
* repository. Query methods will automatically be typed based on this type.
|
|
320
266
|
*/
|
|
321
|
-
export class Client<
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
* environments where a global `fetch` function does not exist, such as
|
|
325
|
-
* Node.js, this function must be provided.
|
|
326
|
-
*/
|
|
327
|
-
fetchFn: FetchLike
|
|
328
|
-
|
|
329
|
-
fetchOptions?: RequestInitLike
|
|
330
|
-
|
|
267
|
+
export class Client<
|
|
268
|
+
TDocuments extends PrismicDocument = PrismicDocument,
|
|
269
|
+
> extends BaseClient {
|
|
331
270
|
#repositoryName: string | undefined
|
|
332
271
|
|
|
333
272
|
/**
|
|
@@ -449,24 +388,7 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
|
|
|
449
388
|
* @returns A client that can query content from the repository.
|
|
450
389
|
*/
|
|
451
390
|
constructor(repositoryNameOrEndpoint: string, options: ClientConfig = {}) {
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
if (typeof options.fetch === "function") {
|
|
455
|
-
this.fetchFn = options.fetch
|
|
456
|
-
} else if (typeof globalThis.fetch === "function") {
|
|
457
|
-
this.fetchFn = globalThis.fetch as FetchLike
|
|
458
|
-
} else {
|
|
459
|
-
throw new PrismicError(
|
|
460
|
-
"A valid fetch implementation was not provided. In environments where fetch is not available (including Node.js), a fetch implementation must be provided via a polyfill or the `fetch` option.",
|
|
461
|
-
undefined,
|
|
462
|
-
undefined,
|
|
463
|
-
)
|
|
464
|
-
}
|
|
465
|
-
|
|
466
|
-
// If the global fetch function is used, we must bind it to the global scope.
|
|
467
|
-
if (this.fetchFn === globalThis.fetch) {
|
|
468
|
-
this.fetchFn = this.fetchFn.bind(globalThis)
|
|
469
|
-
}
|
|
391
|
+
super(options)
|
|
470
392
|
|
|
471
393
|
if (
|
|
472
394
|
(options.documentAPIEndpoint ||
|
|
@@ -1233,32 +1155,16 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
|
|
|
1233
1155
|
* @returns Repository metadata.
|
|
1234
1156
|
*/
|
|
1235
1157
|
async getRepository(params?: FetchParams): Promise<Repository> {
|
|
1158
|
+
// TODO: Restore when Authorization header support works in browsers with CORS.
|
|
1159
|
+
// return await this.fetch<Repository>(this.endpoint);
|
|
1160
|
+
|
|
1236
1161
|
const url = new URL(this.documentAPIEndpoint)
|
|
1237
1162
|
|
|
1238
1163
|
if (this.accessToken) {
|
|
1239
1164
|
url.searchParams.set("access_token", this.accessToken)
|
|
1240
1165
|
}
|
|
1241
1166
|
|
|
1242
|
-
|
|
1243
|
-
switch (response.status) {
|
|
1244
|
-
case 200: {
|
|
1245
|
-
return (await response.json()) as Repository
|
|
1246
|
-
}
|
|
1247
|
-
case 401: {
|
|
1248
|
-
const json = await response.json()
|
|
1249
|
-
throw new ForbiddenError(json.error, url.toString(), json)
|
|
1250
|
-
}
|
|
1251
|
-
case 404: {
|
|
1252
|
-
throw new RepositoryNotFoundError(
|
|
1253
|
-
`Prismic repository not found. Check that "${this.documentAPIEndpoint}" is pointing to the correct repository.`,
|
|
1254
|
-
url.toString(),
|
|
1255
|
-
undefined,
|
|
1256
|
-
)
|
|
1257
|
-
}
|
|
1258
|
-
default: {
|
|
1259
|
-
throw new PrismicError(undefined, url.toString(), await response.text())
|
|
1260
|
-
}
|
|
1261
|
-
}
|
|
1167
|
+
return await this.fetch<Repository>(url.toString(), params)
|
|
1262
1168
|
}
|
|
1263
1169
|
|
|
1264
1170
|
/**
|
|
@@ -1358,23 +1264,21 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
|
|
|
1358
1264
|
* @returns A list of all tags used in the repository.
|
|
1359
1265
|
*/
|
|
1360
1266
|
async getTags(params?: FetchParams): Promise<string[]> {
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
const url = new URL(
|
|
1267
|
+
try {
|
|
1268
|
+
const tagsForm = await this.getCachedRepositoryForm("tags", params)
|
|
1269
|
+
|
|
1270
|
+
const url = new URL(tagsForm.action)
|
|
1271
|
+
|
|
1365
1272
|
if (this.accessToken) {
|
|
1366
1273
|
url.searchParams.set("access_token", this.accessToken)
|
|
1367
1274
|
}
|
|
1368
1275
|
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
}
|
|
1373
|
-
}
|
|
1374
|
-
|
|
1375
|
-
const repository = await this.getRepository(params)
|
|
1276
|
+
return await this.fetch<string[]>(url.toString(), params)
|
|
1277
|
+
} catch {
|
|
1278
|
+
const repository = await this.getRepository(params)
|
|
1376
1279
|
|
|
1377
|
-
|
|
1280
|
+
return repository.tags
|
|
1281
|
+
}
|
|
1378
1282
|
}
|
|
1379
1283
|
|
|
1380
1284
|
/**
|
|
@@ -1679,13 +1583,41 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
|
|
|
1679
1583
|
!this.cachedRepository ||
|
|
1680
1584
|
Date.now() >= this.cachedRepositoryExpiration
|
|
1681
1585
|
) {
|
|
1682
|
-
this.cachedRepository = await this.getRepository(params)
|
|
1683
1586
|
this.cachedRepositoryExpiration = Date.now() + REPOSITORY_CACHE_TTL
|
|
1587
|
+
this.cachedRepository = await this.getRepository(params)
|
|
1684
1588
|
}
|
|
1685
1589
|
|
|
1686
1590
|
return this.cachedRepository
|
|
1687
1591
|
}
|
|
1688
1592
|
|
|
1593
|
+
/**
|
|
1594
|
+
* Returns a cached Prismic repository form. Forms are used to determine API
|
|
1595
|
+
* endpoints for types of repository data.
|
|
1596
|
+
*
|
|
1597
|
+
* @param name - Name of the form.
|
|
1598
|
+
*
|
|
1599
|
+
* @returns The repository form.
|
|
1600
|
+
*
|
|
1601
|
+
* @throws If a matching form cannot be found.
|
|
1602
|
+
*/
|
|
1603
|
+
private async getCachedRepositoryForm(
|
|
1604
|
+
name: string,
|
|
1605
|
+
params?: FetchParams,
|
|
1606
|
+
): Promise<Form> {
|
|
1607
|
+
const cachedRepository = await this.getCachedRepository(params)
|
|
1608
|
+
const form = cachedRepository.forms[name]
|
|
1609
|
+
|
|
1610
|
+
if (!form) {
|
|
1611
|
+
throw new PrismicError(
|
|
1612
|
+
`Form with name "${name}" could not be found`,
|
|
1613
|
+
undefined,
|
|
1614
|
+
undefined,
|
|
1615
|
+
)
|
|
1616
|
+
}
|
|
1617
|
+
|
|
1618
|
+
return form
|
|
1619
|
+
}
|
|
1620
|
+
|
|
1689
1621
|
/**
|
|
1690
1622
|
* Returns the ref needed to query based on the client's current state. This
|
|
1691
1623
|
* method may make a network request to fetch a ref or resolve the user's ref
|
|
@@ -1781,56 +1713,15 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
|
|
|
1781
1713
|
const url = await this.buildQueryURL(params)
|
|
1782
1714
|
|
|
1783
1715
|
try {
|
|
1784
|
-
const
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
try {
|
|
1788
|
-
return {
|
|
1789
|
-
data: (await response.clone().json()) as Query<TDocument>,
|
|
1790
|
-
url,
|
|
1791
|
-
}
|
|
1792
|
-
} catch {
|
|
1793
|
-
throw new PrismicError(undefined, url, await response.text())
|
|
1794
|
-
}
|
|
1795
|
-
}
|
|
1796
|
-
case 400: {
|
|
1797
|
-
const json = await response.json()
|
|
1798
|
-
throw new ParsingError(json.message, url, json)
|
|
1799
|
-
}
|
|
1800
|
-
case 401: {
|
|
1801
|
-
const json = await response.json()
|
|
1802
|
-
throw new ForbiddenError(json.message, url, json)
|
|
1803
|
-
}
|
|
1804
|
-
case 404: {
|
|
1805
|
-
const json = await response.json()
|
|
1806
|
-
switch (json.type) {
|
|
1807
|
-
case "api_notfound_error": {
|
|
1808
|
-
throw new RefNotFoundError(json.message, url, json)
|
|
1809
|
-
}
|
|
1810
|
-
case "api_security_error": {
|
|
1811
|
-
if (/preview token.*expired/i.test(json.message)) {
|
|
1812
|
-
throw new PreviewTokenExpiredError(json.message, url, json)
|
|
1813
|
-
}
|
|
1814
|
-
}
|
|
1815
|
-
default: {
|
|
1816
|
-
throw new NotFoundError(json.message, url, json)
|
|
1817
|
-
}
|
|
1818
|
-
}
|
|
1819
|
-
}
|
|
1820
|
-
case 410: {
|
|
1821
|
-
const json = await response.json()
|
|
1822
|
-
throw new RefExpiredError(json.message, url, json)
|
|
1823
|
-
}
|
|
1824
|
-
default: {
|
|
1825
|
-
throw new PrismicError(undefined, url, await response.text())
|
|
1826
|
-
}
|
|
1827
|
-
}
|
|
1716
|
+
const data = await this.fetch<Query<TDocument>>(url, params)
|
|
1717
|
+
|
|
1718
|
+
return { data, url }
|
|
1828
1719
|
} catch (error) {
|
|
1829
1720
|
if (
|
|
1830
1721
|
!(
|
|
1831
1722
|
error instanceof RefNotFoundError || error instanceof RefExpiredError
|
|
1832
1723
|
) ||
|
|
1833
|
-
attemptCount >=
|
|
1724
|
+
attemptCount >= MAX_INVALID_REF_RETRY_ATTEMPS - 1
|
|
1834
1725
|
) {
|
|
1835
1726
|
throw error
|
|
1836
1727
|
}
|
|
@@ -1861,30 +1752,109 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
|
|
|
1861
1752
|
}
|
|
1862
1753
|
|
|
1863
1754
|
/**
|
|
1864
|
-
*
|
|
1865
|
-
*
|
|
1755
|
+
* Performs a network request using the configured `fetch` function. It
|
|
1756
|
+
* assumes all successful responses will have a JSON content type. It also
|
|
1757
|
+
* normalizes unsuccessful network requests.
|
|
1866
1758
|
*
|
|
1867
|
-
* @
|
|
1868
|
-
* @param params - Fetch options.
|
|
1759
|
+
* @typeParam T - The JSON response.
|
|
1869
1760
|
*
|
|
1870
|
-
* @
|
|
1761
|
+
* @param url - URL to the resource to fetch.
|
|
1762
|
+
* @param params - Prismic REST API parameters for the network request.
|
|
1763
|
+
*
|
|
1764
|
+
* @returns The JSON response from the network request.
|
|
1871
1765
|
*/
|
|
1872
|
-
async
|
|
1873
|
-
|
|
1874
|
-
|
|
1766
|
+
protected async fetch<T = unknown>(
|
|
1767
|
+
url: string,
|
|
1768
|
+
params: FetchParams = {},
|
|
1769
|
+
): Promise<T> {
|
|
1770
|
+
const res = await super.fetch(url, params)
|
|
1875
1771
|
|
|
1876
|
-
|
|
1877
|
-
|
|
1878
|
-
...this.fetchOptions,
|
|
1879
|
-
...params?.fetchOptions,
|
|
1880
|
-
headers: {
|
|
1881
|
-
...this.fetchOptions?.headers,
|
|
1882
|
-
...params?.fetchOptions?.headers,
|
|
1883
|
-
},
|
|
1884
|
-
signal:
|
|
1885
|
-
params?.fetchOptions?.signal ||
|
|
1886
|
-
params?.signal ||
|
|
1887
|
-
this.fetchOptions?.signal,
|
|
1772
|
+
if (res.status !== 404 && res.status !== 429 && res.json == null) {
|
|
1773
|
+
throw new PrismicError(undefined, url, res.json || res.text)
|
|
1888
1774
|
}
|
|
1775
|
+
|
|
1776
|
+
switch (res.status) {
|
|
1777
|
+
// Successful
|
|
1778
|
+
case 200:
|
|
1779
|
+
case 201: {
|
|
1780
|
+
return res.json
|
|
1781
|
+
}
|
|
1782
|
+
|
|
1783
|
+
// Bad Request
|
|
1784
|
+
// - Invalid filter syntax
|
|
1785
|
+
// - Ref not provided (ignored)
|
|
1786
|
+
case 400: {
|
|
1787
|
+
throw new ParsingError(res.json.message, url, res.json)
|
|
1788
|
+
}
|
|
1789
|
+
|
|
1790
|
+
// Unauthorized
|
|
1791
|
+
// - Missing access token for repository endpoint
|
|
1792
|
+
// - Incorrect access token for repository endpoint
|
|
1793
|
+
case 401:
|
|
1794
|
+
// Forbidden
|
|
1795
|
+
// - Missing access token for query endpoint
|
|
1796
|
+
// - Incorrect access token for query endpoint
|
|
1797
|
+
case 403: {
|
|
1798
|
+
throw new ForbiddenError(
|
|
1799
|
+
res.json.error || res.json.message,
|
|
1800
|
+
url,
|
|
1801
|
+
res.json,
|
|
1802
|
+
)
|
|
1803
|
+
}
|
|
1804
|
+
|
|
1805
|
+
// Not Found
|
|
1806
|
+
// - Incorrect repository name (this response has an empty body)
|
|
1807
|
+
// - Ref does not exist
|
|
1808
|
+
// - Preview token is expired
|
|
1809
|
+
case 404: {
|
|
1810
|
+
if (res.json === undefined) {
|
|
1811
|
+
throw new RepositoryNotFoundError(
|
|
1812
|
+
`Prismic repository not found. Check that "${this.documentAPIEndpoint}" is pointing to the correct repository.`,
|
|
1813
|
+
url,
|
|
1814
|
+
url.startsWith(this.documentAPIEndpoint) ? undefined : res.text,
|
|
1815
|
+
)
|
|
1816
|
+
}
|
|
1817
|
+
|
|
1818
|
+
if (res.json.type === "api_notfound_error") {
|
|
1819
|
+
throw new RefNotFoundError(res.json.message, url, res.json)
|
|
1820
|
+
}
|
|
1821
|
+
|
|
1822
|
+
if (
|
|
1823
|
+
res.json.type === "api_security_error" &&
|
|
1824
|
+
/preview token.*expired/i.test(res.json.message)
|
|
1825
|
+
) {
|
|
1826
|
+
throw new PreviewTokenExpiredError(res.json.message, url, res.json)
|
|
1827
|
+
}
|
|
1828
|
+
|
|
1829
|
+
throw new NotFoundError(res.json.message, url, res.json)
|
|
1830
|
+
}
|
|
1831
|
+
|
|
1832
|
+
// Gone
|
|
1833
|
+
// - Ref is expired
|
|
1834
|
+
case 410: {
|
|
1835
|
+
throw new RefExpiredError(res.json.message, url, res.json)
|
|
1836
|
+
}
|
|
1837
|
+
|
|
1838
|
+
// Too Many Requests
|
|
1839
|
+
// - Exceeded the maximum number of requests per second
|
|
1840
|
+
case 429: {
|
|
1841
|
+
const parsedRetryAfter = Number(res.headers.get("retry-after"))
|
|
1842
|
+
const delay = Number.isNaN(parsedRetryAfter)
|
|
1843
|
+
? DEFUALT_RETRY_AFTER_MS
|
|
1844
|
+
: parsedRetryAfter
|
|
1845
|
+
|
|
1846
|
+
return await new Promise((resolve, reject) => {
|
|
1847
|
+
setTimeout(async () => {
|
|
1848
|
+
try {
|
|
1849
|
+
resolve(await this.fetch(url, params))
|
|
1850
|
+
} catch (error) {
|
|
1851
|
+
reject(error)
|
|
1852
|
+
}
|
|
1853
|
+
}, delay)
|
|
1854
|
+
})
|
|
1855
|
+
}
|
|
1856
|
+
}
|
|
1857
|
+
|
|
1858
|
+
throw new PrismicError(undefined, url, res.json)
|
|
1889
1859
|
}
|
|
1890
1860
|
}
|