@prismicio/client 7.21.0-pr.417.960f871 → 7.21.0-pr.418.5699631

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 CHANGED
@@ -1,6 +1,5 @@
1
1
  const require_filter = require('./filter.cjs');
2
2
  const require_appendFilters = require('./lib/appendFilters.cjs');
3
- const require_castThunk = require('./lib/castThunk.cjs');
4
3
  const require_devMsg = require('./lib/devMsg.cjs');
5
4
  const require_everyTagFilter = require('./lib/everyTagFilter.cjs');
6
5
  const require_PrismicError = require('./errors/PrismicError.cjs');
@@ -54,28 +53,6 @@ const GET_ALL_QUERY_DELAY = 500;
54
53
  */
55
54
  const MAX_INVALID_REF_RETRY_ATTEMPTS = 3;
56
55
  /**
57
- * Modes for client ref management.
58
- */
59
- var RefStateMode = /* @__PURE__ */ function(RefStateMode$1) {
60
- /**
61
- * Use the repository's master ref.
62
- */
63
- RefStateMode$1["Master"] = "Master";
64
- /**
65
- * Use a given Release identified by its ID.
66
- */
67
- RefStateMode$1["ReleaseID"] = "ReleaseID";
68
- /**
69
- * Use a given Release identified by its label.
70
- */
71
- RefStateMode$1["ReleaseLabel"] = "ReleaseLabel";
72
- /**
73
- * Use a given ref.
74
- */
75
- RefStateMode$1["Manual"] = "Manual";
76
- return RefStateMode$1;
77
- }(RefStateMode || {});
78
- /**
79
56
  * A client that allows querying content from a Prismic repository.
80
57
  *
81
58
  * If used in an environment where a global `fetch` function is unavailable,
@@ -158,16 +135,17 @@ var Client = class {
158
135
  * be overridden on each query if needed.
159
136
  */
160
137
  defaultParams;
138
+ #getRef;
139
+ #autoPreviews = true;
140
+ #autoPreviewsRequest;
161
141
  /**
162
- * The client's ref mode state. This determines which ref is used during
163
- * queries.
142
+ * Cached repository value.
143
+ */
144
+ cachedRepository;
145
+ /**
146
+ * Timestamp at which the cached repository data is considered stale.
164
147
  */
165
- refState = {
166
- mode: RefStateMode.Master,
167
- autoPreviewsEnabled: true
168
- };
169
- #cachedRepository;
170
- #cachedRepositoryExpiration = 0;
148
+ cachedRepositoryExpiration = 0;
171
149
  /**
172
150
  * Creates a Prismic client that can be used to query a repository.
173
151
  *
@@ -229,7 +207,7 @@ var Client = class {
229
207
  * @see enableAutoPreviewsFromReq
230
208
  */
231
209
  enableAutoPreviews() {
232
- this.refState.autoPreviewsEnabled = true;
210
+ this.#autoPreviews = true;
233
211
  }
234
212
  /**
235
213
  * Enables the client to automatically query content from a preview session if
@@ -250,9 +228,9 @@ var Client = class {
250
228
  * @param req - An HTTP server request object containing the request's
251
229
  * cookies.
252
230
  */
253
- enableAutoPreviewsFromReq(req) {
254
- this.refState.httpRequest = req;
255
- this.refState.autoPreviewsEnabled = true;
231
+ enableAutoPreviewsFromReq(request$1) {
232
+ this.enableAutoPreviews();
233
+ this.#autoPreviewsRequest = request$1;
256
234
  }
257
235
  /**
258
236
  * Disables the client from automatically querying content from a preview
@@ -268,7 +246,8 @@ var Client = class {
268
246
  * ```
269
247
  */
270
248
  disableAutoPreviews() {
271
- this.refState.autoPreviewsEnabled = false;
249
+ this.#autoPreviews = false;
250
+ this.#autoPreviewsRequest = void 0;
272
251
  }
273
252
  /**
274
253
  * Queries content from the Prismic repository.
@@ -749,16 +728,12 @@ var Client = class {
749
728
  * @returns Repository metadata.
750
729
  */
751
730
  async getRepository(params) {
752
- if (this.#cachedRepository && this.#cachedRepositoryExpiration > Date.now()) return this.#cachedRepository;
753
731
  const url = new URL(this.documentAPIEndpoint);
754
732
  const accessToken = (params === null || params === void 0 ? void 0 : params.accessToken) || this.accessToken;
755
733
  if (accessToken) url.searchParams.set("access_token", accessToken);
756
734
  const response = await this.#request(url, params);
757
735
  switch (response.status) {
758
- case 200:
759
- this.#cachedRepository = await response.json();
760
- this.#cachedRepositoryExpiration = Date.now() + REPOSITORY_CACHE_TTL;
761
- return this.#cachedRepository;
736
+ case 200: return await response.json();
762
737
  case 401: {
763
738
  const json = await response.json();
764
739
  throw new require_ForbiddenError.ForbiddenError(json.error, url.toString(), json);
@@ -843,15 +818,14 @@ var Client = class {
843
818
  * @returns A list of all tags used in the repository.
844
819
  */
845
820
  async getTags(params) {
846
- const repository = await this.getRepository(params);
847
- const form = repository.forms.tags;
821
+ const form = (await this.getCachedRepository(params)).forms.tags;
848
822
  if (form) {
849
823
  const url = new URL(form.action);
850
824
  if (this.accessToken) url.searchParams.set("access_token", this.accessToken);
851
825
  const response = await this.#request(url, params);
852
826
  if (response.ok) return await response.json();
853
827
  }
854
- return repository.tags;
828
+ return (await this.getRepository(params)).tags;
855
829
  }
856
830
  /**
857
831
  * Builds a URL used to query content from the Prismic repository.
@@ -861,12 +835,12 @@ var Client = class {
861
835
  * @returns A URL string that can be requested to query content.
862
836
  */
863
837
  async buildQueryURL({ signal, fetchOptions,...params } = {}) {
864
- const ref = params.ref || await this.getResolvedRefString({
838
+ const ref = params.ref || await this.#getResolvedRef({
865
839
  accessToken: params.accessToken,
866
840
  signal,
867
841
  fetchOptions
868
842
  });
869
- const integrationFieldsRef = params.integrationFieldsRef || (await this.getRepository({
843
+ const integrationFieldsRef = params.integrationFieldsRef || (await this.getCachedRepository({
870
844
  accessToken: params.accessToken,
871
845
  signal,
872
846
  fetchOptions
@@ -907,13 +881,13 @@ var Client = class {
907
881
  const searchParams = new URLSearchParams(globalThis.location.search);
908
882
  documentID = documentID || searchParams.get("documentId");
909
883
  previewToken = previewToken || searchParams.get("token");
910
- } else if (this.refState.httpRequest) {
911
- if ("query" in this.refState.httpRequest) {
912
- var _this$refState$httpRe, _this$refState$httpRe2;
913
- documentID = documentID || ((_this$refState$httpRe = this.refState.httpRequest.query) === null || _this$refState$httpRe === void 0 ? void 0 : _this$refState$httpRe.documentId);
914
- previewToken = previewToken || ((_this$refState$httpRe2 = this.refState.httpRequest.query) === null || _this$refState$httpRe2 === void 0 ? void 0 : _this$refState$httpRe2.token);
915
- } else if ("url" in this.refState.httpRequest && this.refState.httpRequest.url) {
916
- const searchParams = new URL(this.refState.httpRequest.url, "missing-host://").searchParams;
884
+ } else if (this.#autoPreviewsRequest) {
885
+ if ("query" in this.#autoPreviewsRequest) {
886
+ var _this$autoPreviewsReq, _this$autoPreviewsReq2;
887
+ documentID = documentID || ((_this$autoPreviewsReq = this.#autoPreviewsRequest.query) === null || _this$autoPreviewsReq === void 0 ? void 0 : _this$autoPreviewsReq.documentId);
888
+ previewToken = previewToken || ((_this$autoPreviewsReq2 = this.#autoPreviewsRequest.query) === null || _this$autoPreviewsReq2 === void 0 ? void 0 : _this$autoPreviewsReq2.token);
889
+ } else if ("url" in this.#autoPreviewsRequest && this.#autoPreviewsRequest.url) {
890
+ const searchParams = new URL(this.#autoPreviewsRequest.url, "missing-host://").searchParams;
917
891
  documentID = documentID || searchParams.get("documentId");
918
892
  previewToken = previewToken || searchParams.get("token");
919
893
  }
@@ -944,7 +918,7 @@ var Client = class {
944
918
  * ```
945
919
  */
946
920
  queryLatestContent() {
947
- this.refState.mode = RefStateMode.Master;
921
+ this.#getRef = void 0;
948
922
  }
949
923
  /**
950
924
  * Configures the client to query content from a specific Release identified
@@ -962,11 +936,9 @@ var Client = class {
962
936
  *
963
937
  * @param releaseID - The ID of the Release.
964
938
  */
965
- queryContentFromReleaseByID(releaseID) {
966
- this.refState = {
967
- ...this.refState,
968
- mode: RefStateMode.ReleaseID,
969
- releaseID
939
+ queryContentFromReleaseByID(id) {
940
+ this.#getRef = async (params) => {
941
+ return (await this.getReleaseByID(id, params)).ref;
970
942
  };
971
943
  }
972
944
  /**
@@ -985,11 +957,9 @@ var Client = class {
985
957
  *
986
958
  * @param releaseLabel - The label of the Release.
987
959
  */
988
- queryContentFromReleaseByLabel(releaseLabel) {
989
- this.refState = {
990
- ...this.refState,
991
- mode: RefStateMode.ReleaseLabel,
992
- releaseLabel
960
+ queryContentFromReleaseByLabel(label) {
961
+ this.#getRef = async (params) => {
962
+ return (await this.getReleaseByLabel(label, params)).ref;
993
963
  };
994
964
  }
995
965
  /**
@@ -1010,11 +980,7 @@ var Client = class {
1010
980
  * content.
1011
981
  */
1012
982
  queryContentFromRef(ref) {
1013
- this.refState = {
1014
- ...this.refState,
1015
- mode: RefStateMode.Manual,
1016
- ref
1017
- };
983
+ this.#getRef = typeof ref === "string" ? () => ref : ref;
1018
984
  }
1019
985
  /**
1020
986
  * A `fetch()` function to be used with GraphQL clients configured for
@@ -1046,14 +1012,14 @@ var Client = class {
1046
1012
  * @experimental
1047
1013
  */
1048
1014
  async graphQLFetch(input, init) {
1049
- const repository = await this.getRepository();
1050
- const ref = await this.getResolvedRefString();
1015
+ const cachedRepository = await this.getCachedRepository();
1016
+ const ref = await this.#getResolvedRef();
1051
1017
  const unsanitizedHeaders = {
1052
1018
  "Prismic-ref": ref,
1053
1019
  Authorization: this.accessToken ? `Token ${this.accessToken}` : "",
1054
1020
  ...init ? init.headers : {}
1055
1021
  };
1056
- if (repository.integrationFieldsRef) unsanitizedHeaders["Prismic-integration-field-ref"] = repository.integrationFieldsRef;
1022
+ if (cachedRepository.integrationFieldsRef) unsanitizedHeaders["Prismic-integration-field-ref"] = cachedRepository.integrationFieldsRef;
1057
1023
  const headers = {};
1058
1024
  for (const key in unsanitizedHeaders) if (unsanitizedHeaders[key]) headers[key.toLowerCase()] = unsanitizedHeaders[key];
1059
1025
  const url = new URL(input);
@@ -1066,6 +1032,18 @@ var Client = class {
1066
1032
  });
1067
1033
  }
1068
1034
  /**
1035
+ * Returns a cached version of `getRepository` with a TTL.
1036
+ *
1037
+ * @returns Cached repository metadata.
1038
+ */
1039
+ async getCachedRepository(params) {
1040
+ if (!this.cachedRepository || Date.now() >= this.cachedRepositoryExpiration) {
1041
+ this.cachedRepository = await this.getRepository(params);
1042
+ this.cachedRepositoryExpiration = Date.now() + REPOSITORY_CACHE_TTL;
1043
+ }
1044
+ return this.cachedRepository;
1045
+ }
1046
+ /**
1069
1047
  * Returns the ref needed to query based on the client's current state. This
1070
1048
  * method may make a network request to fetch a ref or resolve the user's ref
1071
1049
  * thunk.
@@ -1088,27 +1066,16 @@ var Client = class {
1088
1066
  *
1089
1067
  * @returns The ref to use during a query.
1090
1068
  */
1091
- async getResolvedRefString(params) {
1092
- if (this.refState.autoPreviewsEnabled) {
1093
- var _this$refState$httpRe3, _globalThis$document;
1094
- let previewRef;
1095
- let cookieJar;
1096
- if ((_this$refState$httpRe3 = this.refState.httpRequest) === null || _this$refState$httpRe3 === void 0 ? void 0 : _this$refState$httpRe3.headers) {
1097
- if ("get" in this.refState.httpRequest.headers && typeof this.refState.httpRequest.headers.get === "function") cookieJar = this.refState.httpRequest.headers.get("cookie");
1098
- else if ("cookie" in this.refState.httpRequest.headers) cookieJar = this.refState.httpRequest.headers.cookie;
1099
- } else if ((_globalThis$document = globalThis.document) === null || _globalThis$document === void 0 ? void 0 : _globalThis$document.cookie) cookieJar = globalThis.document.cookie;
1100
- if (cookieJar) previewRef = require_getPreviewCookie.getPreviewCookie(cookieJar);
1069
+ async #getResolvedRef(params) {
1070
+ var _this$getRef;
1071
+ if (this.#autoPreviews) {
1072
+ var _this$autoPreviewsReq3, _globalThis$document;
1073
+ const previewRef = require_getPreviewCookie.getPreviewCookie((((_this$autoPreviewsReq3 = this.#autoPreviewsRequest) === null || _this$autoPreviewsReq3 === void 0 ? void 0 : _this$autoPreviewsReq3.headers) ? "get" in this.#autoPreviewsRequest.headers ? this.#autoPreviewsRequest.headers.get("cookie") : this.#autoPreviewsRequest.headers.cookie : (_globalThis$document = globalThis.document) === null || _globalThis$document === void 0 ? void 0 : _globalThis$document.cookie) ?? "");
1101
1074
  if (previewRef) return previewRef;
1102
1075
  }
1103
- const repository = await this.getRepository(params);
1104
- const refModeType = this.refState.mode;
1105
- if (refModeType === RefStateMode.ReleaseID) return require_findRefByID.findRefByID(repository.refs, this.refState.releaseID).ref;
1106
- else if (refModeType === RefStateMode.ReleaseLabel) return require_findRefByLabel.findRefByLabel(repository.refs, this.refState.releaseLabel).ref;
1107
- else if (refModeType === RefStateMode.Manual) {
1108
- const res = await require_castThunk.castThunk(this.refState.ref)();
1109
- if (typeof res === "string") return res;
1110
- }
1111
- return require_findMasterRef.findMasterRef(repository.refs).ref;
1076
+ const ref = await ((_this$getRef = this.#getRef) === null || _this$getRef === void 0 ? void 0 : _this$getRef.call(this, params));
1077
+ if (ref) return ref;
1078
+ return (await this.getMasterRef(params)).ref;
1112
1079
  }
1113
1080
  /**
1114
1081
  * The private implementation of `this.get`. It returns the API response and
@@ -1164,8 +1131,8 @@ var Client = class {
1164
1131
  } catch (error) {
1165
1132
  var _error$message$match;
1166
1133
  if (!(error instanceof require_RefNotFoundError.RefNotFoundError || error instanceof require_RefExpiredError.RefExpiredError) || attemptCount >= MAX_INVALID_REF_RETRY_ATTEMPTS - 1) throw error;
1167
- if (!(params === null || params === void 0 ? void 0 : params.ref)) this.#cachedRepository = void 0;
1168
- const masterRef = (_error$message$match = error.message.match(/master ref is: (?<ref>.*)$/i)) === null || _error$message$match === void 0 || (_error$message$match = _error$message$match.groups) === null || _error$message$match === void 0 ? void 0 : _error$message$match.ref;
1134
+ if (!(params === null || params === void 0 ? void 0 : params.ref)) this.cachedRepository = void 0;
1135
+ const masterRef = (_error$message$match = error.message.match(/Master ref is: (?<ref>.*)$/)) === null || _error$message$match === void 0 || (_error$message$match = _error$message$match.groups) === null || _error$message$match === void 0 ? void 0 : _error$message$match.ref;
1169
1136
  if (!masterRef) throw error;
1170
1137
  require_throttledLog.throttledLog(`[@prismicio/client] The ref (${new URL(url).searchParams.get("ref")}) was ${error instanceof require_RefNotFoundError.RefNotFoundError ? "invalid" : "expired"}. Now retrying with the latest master ref (${masterRef}). If you were previewing content, the response will not include draft content.`, { level: "warn" });
1171
1138
  return await this._get({