@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/dist/Client.js
CHANGED
|
@@ -8,8 +8,7 @@ var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot
|
|
|
8
8
|
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
9
9
|
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
10
10
|
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
|
|
11
|
-
var
|
|
12
|
-
var _repositoryName, _Client_instances, request_fn;
|
|
11
|
+
var _repositoryName;
|
|
13
12
|
import { appendFilters } from "./lib/appendFilters.js";
|
|
14
13
|
import { castThunk } from "./lib/castThunk.js";
|
|
15
14
|
import { devMsg } from "./lib/devMsg.js";
|
|
@@ -19,7 +18,6 @@ import { findRefByID } from "./lib/findRefByID.js";
|
|
|
19
18
|
import { findRefByLabel } from "./lib/findRefByLabel.js";
|
|
20
19
|
import { getPreviewCookie } from "./lib/getPreviewCookie.js";
|
|
21
20
|
import { minifyGraphQLQuery } from "./lib/minifyGraphQLQuery.js";
|
|
22
|
-
import { request } from "./lib/request.js";
|
|
23
21
|
import { someTagsFilter } from "./lib/someTagsFilter.js";
|
|
24
22
|
import { throttledLog } from "./lib/throttledLog.js";
|
|
25
23
|
import { typeFilter } from "./lib/typeFilter.js";
|
|
@@ -32,6 +30,7 @@ import { RefExpiredError } from "./errors/RefExpiredError.js";
|
|
|
32
30
|
import { RefNotFoundError } from "./errors/RefNotFoundError.js";
|
|
33
31
|
import { RepositoryNotFoundError } from "./errors/RepositoryNotFoundError.js";
|
|
34
32
|
import { asLink } from "./helpers/asLink.js";
|
|
33
|
+
import { BaseClient } from "./BaseClient.js";
|
|
35
34
|
import { buildQueryURL } from "./buildQueryURL.js";
|
|
36
35
|
import { filter } from "./filter.js";
|
|
37
36
|
import { getRepositoryEndpoint } from "./getRepositoryEndpoint.js";
|
|
@@ -40,7 +39,8 @@ import { isRepositoryEndpoint } from "./isRepositoryEndpoint.js";
|
|
|
40
39
|
const MAX_PAGE_SIZE = 100;
|
|
41
40
|
const REPOSITORY_CACHE_TTL = 5e3;
|
|
42
41
|
const GET_ALL_QUERY_DELAY = 500;
|
|
43
|
-
const
|
|
42
|
+
const DEFUALT_RETRY_AFTER_MS = 1e3;
|
|
43
|
+
const MAX_INVALID_REF_RETRY_ATTEMPS = 3;
|
|
44
44
|
var RefStateMode;
|
|
45
45
|
(function(RefStateMode2) {
|
|
46
46
|
RefStateMode2["Master"] = "Master";
|
|
@@ -48,7 +48,7 @@ var RefStateMode;
|
|
|
48
48
|
RefStateMode2["ReleaseLabel"] = "ReleaseLabel";
|
|
49
49
|
RefStateMode2["Manual"] = "Manual";
|
|
50
50
|
})(RefStateMode || (RefStateMode = {}));
|
|
51
|
-
class Client {
|
|
51
|
+
class Client extends BaseClient {
|
|
52
52
|
/**
|
|
53
53
|
* Creates a Prismic client that can be used to query a repository.
|
|
54
54
|
*
|
|
@@ -64,14 +64,7 @@ class Client {
|
|
|
64
64
|
* @returns A client that can query content from the repository.
|
|
65
65
|
*/
|
|
66
66
|
constructor(repositoryNameOrEndpoint, options = {}) {
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* The function used to make network requests to the Prismic REST API. In
|
|
70
|
-
* environments where a global `fetch` function does not exist, such as
|
|
71
|
-
* Node.js, this function must be provided.
|
|
72
|
-
*/
|
|
73
|
-
__publicField(this, "fetchFn");
|
|
74
|
-
__publicField(this, "fetchOptions");
|
|
67
|
+
super(options);
|
|
75
68
|
__privateAdd(this, _repositoryName);
|
|
76
69
|
/**
|
|
77
70
|
* The Prismic REST API V2 endpoint for the repository (use
|
|
@@ -122,17 +115,6 @@ class Client {
|
|
|
122
115
|
* Timestamp at which the cached repository data is considered stale.
|
|
123
116
|
*/
|
|
124
117
|
__publicField(this, "cachedRepositoryExpiration", 0);
|
|
125
|
-
this.fetchOptions = options.fetchOptions;
|
|
126
|
-
if (typeof options.fetch === "function") {
|
|
127
|
-
this.fetchFn = options.fetch;
|
|
128
|
-
} else if (typeof globalThis.fetch === "function") {
|
|
129
|
-
this.fetchFn = globalThis.fetch;
|
|
130
|
-
} else {
|
|
131
|
-
throw new PrismicError("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.", void 0, void 0);
|
|
132
|
-
}
|
|
133
|
-
if (this.fetchFn === globalThis.fetch) {
|
|
134
|
-
this.fetchFn = this.fetchFn.bind(globalThis);
|
|
135
|
-
}
|
|
136
118
|
if ((options.documentAPIEndpoint || isRepositoryEndpoint(repositoryNameOrEndpoint)) && process.env.NODE_ENV === "development") {
|
|
137
119
|
const documentAPIEndpoint = options.documentAPIEndpoint || repositoryNameOrEndpoint;
|
|
138
120
|
if (/\.prismic\.io\/(?!api\/v2\/?)/i.test(documentAPIEndpoint)) {
|
|
@@ -751,22 +733,7 @@ class Client {
|
|
|
751
733
|
if (this.accessToken) {
|
|
752
734
|
url.searchParams.set("access_token", this.accessToken);
|
|
753
735
|
}
|
|
754
|
-
|
|
755
|
-
switch (response.status) {
|
|
756
|
-
case 200: {
|
|
757
|
-
return await response.json();
|
|
758
|
-
}
|
|
759
|
-
case 401: {
|
|
760
|
-
const json = await response.json();
|
|
761
|
-
throw new ForbiddenError(json.error, url.toString(), json);
|
|
762
|
-
}
|
|
763
|
-
case 404: {
|
|
764
|
-
throw new RepositoryNotFoundError(`Prismic repository not found. Check that "${this.documentAPIEndpoint}" is pointing to the correct repository.`, url.toString(), void 0);
|
|
765
|
-
}
|
|
766
|
-
default: {
|
|
767
|
-
throw new PrismicError(void 0, url.toString(), await response.text());
|
|
768
|
-
}
|
|
769
|
-
}
|
|
736
|
+
return await this.fetch(url.toString(), params);
|
|
770
737
|
}
|
|
771
738
|
/**
|
|
772
739
|
* Returns a list of all refs for the Prismic repository.
|
|
@@ -851,20 +818,17 @@ class Client {
|
|
|
851
818
|
* @returns A list of all tags used in the repository.
|
|
852
819
|
*/
|
|
853
820
|
async getTags(params) {
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
const url = new URL(form.action);
|
|
821
|
+
try {
|
|
822
|
+
const tagsForm = await this.getCachedRepositoryForm("tags", params);
|
|
823
|
+
const url = new URL(tagsForm.action);
|
|
858
824
|
if (this.accessToken) {
|
|
859
825
|
url.searchParams.set("access_token", this.accessToken);
|
|
860
826
|
}
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
827
|
+
return await this.fetch(url.toString(), params);
|
|
828
|
+
} catch {
|
|
829
|
+
const repository = await this.getRepository(params);
|
|
830
|
+
return repository.tags;
|
|
865
831
|
}
|
|
866
|
-
const repository = await this.getRepository(params);
|
|
867
|
-
return repository.tags;
|
|
868
832
|
}
|
|
869
833
|
/**
|
|
870
834
|
* Builds a URL used to query content from the Prismic repository.
|
|
@@ -1104,11 +1068,29 @@ class Client {
|
|
|
1104
1068
|
*/
|
|
1105
1069
|
async getCachedRepository(params) {
|
|
1106
1070
|
if (!this.cachedRepository || Date.now() >= this.cachedRepositoryExpiration) {
|
|
1107
|
-
this.cachedRepository = await this.getRepository(params);
|
|
1108
1071
|
this.cachedRepositoryExpiration = Date.now() + REPOSITORY_CACHE_TTL;
|
|
1072
|
+
this.cachedRepository = await this.getRepository(params);
|
|
1109
1073
|
}
|
|
1110
1074
|
return this.cachedRepository;
|
|
1111
1075
|
}
|
|
1076
|
+
/**
|
|
1077
|
+
* Returns a cached Prismic repository form. Forms are used to determine API
|
|
1078
|
+
* endpoints for types of repository data.
|
|
1079
|
+
*
|
|
1080
|
+
* @param name - Name of the form.
|
|
1081
|
+
*
|
|
1082
|
+
* @returns The repository form.
|
|
1083
|
+
*
|
|
1084
|
+
* @throws If a matching form cannot be found.
|
|
1085
|
+
*/
|
|
1086
|
+
async getCachedRepositoryForm(name, params) {
|
|
1087
|
+
const cachedRepository = await this.getCachedRepository(params);
|
|
1088
|
+
const form = cachedRepository.forms[name];
|
|
1089
|
+
if (!form) {
|
|
1090
|
+
throw new PrismicError(`Form with name "${name}" could not be found`, void 0, void 0);
|
|
1091
|
+
}
|
|
1092
|
+
return form;
|
|
1093
|
+
}
|
|
1112
1094
|
/**
|
|
1113
1095
|
* Returns the ref needed to query based on the client's current state. This
|
|
1114
1096
|
* method may make a network request to fetch a ref or resolve the user's ref
|
|
@@ -1187,52 +1169,10 @@ class Client {
|
|
|
1187
1169
|
var _a, _b;
|
|
1188
1170
|
const url = await this.buildQueryURL(params);
|
|
1189
1171
|
try {
|
|
1190
|
-
const
|
|
1191
|
-
|
|
1192
|
-
case 200: {
|
|
1193
|
-
try {
|
|
1194
|
-
return {
|
|
1195
|
-
data: await response.clone().json(),
|
|
1196
|
-
url
|
|
1197
|
-
};
|
|
1198
|
-
} catch {
|
|
1199
|
-
throw new PrismicError(void 0, url, await response.text());
|
|
1200
|
-
}
|
|
1201
|
-
}
|
|
1202
|
-
case 400: {
|
|
1203
|
-
const json = await response.json();
|
|
1204
|
-
throw new ParsingError(json.message, url, json);
|
|
1205
|
-
}
|
|
1206
|
-
case 401: {
|
|
1207
|
-
const json = await response.json();
|
|
1208
|
-
throw new ForbiddenError(json.message, url, json);
|
|
1209
|
-
}
|
|
1210
|
-
case 404: {
|
|
1211
|
-
const json = await response.json();
|
|
1212
|
-
switch (json.type) {
|
|
1213
|
-
case "api_notfound_error": {
|
|
1214
|
-
throw new RefNotFoundError(json.message, url, json);
|
|
1215
|
-
}
|
|
1216
|
-
case "api_security_error": {
|
|
1217
|
-
if (/preview token.*expired/i.test(json.message)) {
|
|
1218
|
-
throw new PreviewTokenExpiredError(json.message, url, json);
|
|
1219
|
-
}
|
|
1220
|
-
}
|
|
1221
|
-
default: {
|
|
1222
|
-
throw new NotFoundError(json.message, url, json);
|
|
1223
|
-
}
|
|
1224
|
-
}
|
|
1225
|
-
}
|
|
1226
|
-
case 410: {
|
|
1227
|
-
const json = await response.json();
|
|
1228
|
-
throw new RefExpiredError(json.message, url, json);
|
|
1229
|
-
}
|
|
1230
|
-
default: {
|
|
1231
|
-
throw new PrismicError(void 0, url, await response.text());
|
|
1232
|
-
}
|
|
1233
|
-
}
|
|
1172
|
+
const data = await this.fetch(url, params);
|
|
1173
|
+
return { data, url };
|
|
1234
1174
|
} catch (error) {
|
|
1235
|
-
if (!(error instanceof RefNotFoundError || error instanceof RefExpiredError) || attemptCount >=
|
|
1175
|
+
if (!(error instanceof RefNotFoundError || error instanceof RefExpiredError) || attemptCount >= MAX_INVALID_REF_RETRY_ATTEMPS - 1) {
|
|
1236
1176
|
throw error;
|
|
1237
1177
|
}
|
|
1238
1178
|
if (!(params == null ? void 0 : params.ref)) {
|
|
@@ -1248,24 +1188,68 @@ class Client {
|
|
|
1248
1188
|
return await this._get({ ...params, ref: masterRef }, attemptCount + 1);
|
|
1249
1189
|
}
|
|
1250
1190
|
}
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1191
|
+
/**
|
|
1192
|
+
* Performs a network request using the configured `fetch` function. It
|
|
1193
|
+
* assumes all successful responses will have a JSON content type. It also
|
|
1194
|
+
* normalizes unsuccessful network requests.
|
|
1195
|
+
*
|
|
1196
|
+
* @typeParam T - The JSON response.
|
|
1197
|
+
*
|
|
1198
|
+
* @param url - URL to the resource to fetch.
|
|
1199
|
+
* @param params - Prismic REST API parameters for the network request.
|
|
1200
|
+
*
|
|
1201
|
+
* @returns The JSON response from the network request.
|
|
1202
|
+
*/
|
|
1203
|
+
async fetch(url, params = {}) {
|
|
1204
|
+
const res = await super.fetch(url, params);
|
|
1205
|
+
if (res.status !== 404 && res.status !== 429 && res.json == null) {
|
|
1206
|
+
throw new PrismicError(void 0, url, res.json || res.text);
|
|
1207
|
+
}
|
|
1208
|
+
switch (res.status) {
|
|
1209
|
+
case 200:
|
|
1210
|
+
case 201: {
|
|
1211
|
+
return res.json;
|
|
1212
|
+
}
|
|
1213
|
+
case 400: {
|
|
1214
|
+
throw new ParsingError(res.json.message, url, res.json);
|
|
1215
|
+
}
|
|
1216
|
+
case 401:
|
|
1217
|
+
case 403: {
|
|
1218
|
+
throw new ForbiddenError(res.json.error || res.json.message, url, res.json);
|
|
1219
|
+
}
|
|
1220
|
+
case 404: {
|
|
1221
|
+
if (res.json === void 0) {
|
|
1222
|
+
throw new RepositoryNotFoundError(`Prismic repository not found. Check that "${this.documentAPIEndpoint}" is pointing to the correct repository.`, url, url.startsWith(this.documentAPIEndpoint) ? void 0 : res.text);
|
|
1223
|
+
}
|
|
1224
|
+
if (res.json.type === "api_notfound_error") {
|
|
1225
|
+
throw new RefNotFoundError(res.json.message, url, res.json);
|
|
1226
|
+
}
|
|
1227
|
+
if (res.json.type === "api_security_error" && /preview token.*expired/i.test(res.json.message)) {
|
|
1228
|
+
throw new PreviewTokenExpiredError(res.json.message, url, res.json);
|
|
1229
|
+
}
|
|
1230
|
+
throw new NotFoundError(res.json.message, url, res.json);
|
|
1231
|
+
}
|
|
1232
|
+
case 410: {
|
|
1233
|
+
throw new RefExpiredError(res.json.message, url, res.json);
|
|
1234
|
+
}
|
|
1235
|
+
case 429: {
|
|
1236
|
+
const parsedRetryAfter = Number(res.headers.get("retry-after"));
|
|
1237
|
+
const delay = Number.isNaN(parsedRetryAfter) ? DEFUALT_RETRY_AFTER_MS : parsedRetryAfter;
|
|
1238
|
+
return await new Promise((resolve, reject) => {
|
|
1239
|
+
setTimeout(async () => {
|
|
1240
|
+
try {
|
|
1241
|
+
resolve(await this.fetch(url, params));
|
|
1242
|
+
} catch (error) {
|
|
1243
|
+
reject(error);
|
|
1244
|
+
}
|
|
1245
|
+
}, delay);
|
|
1246
|
+
});
|
|
1247
|
+
}
|
|
1248
|
+
}
|
|
1249
|
+
throw new PrismicError(void 0, url, res.json);
|
|
1262
1250
|
}
|
|
1263
1251
|
}
|
|
1264
1252
|
_repositoryName = new WeakMap();
|
|
1265
|
-
_Client_instances = new WeakSet();
|
|
1266
|
-
request_fn = async function(url, params) {
|
|
1267
|
-
return await request(url, this._buildRequestInit(params), this.fetchFn);
|
|
1268
|
-
};
|
|
1269
1253
|
export {
|
|
1270
1254
|
Client,
|
|
1271
1255
|
GET_ALL_QUERY_DELAY,
|