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