@shopify/hydrogen 2023.4.0 → 2023.4.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.
@@ -402,7 +402,6 @@ async function fetchWithServerCache(url, requestInit, {
402
402
 
403
403
  // src/constants.ts
404
404
  var STOREFRONT_REQUEST_GROUP_ID_HEADER = "Custom-Storefront-Request-Group-ID";
405
- var STOREFRONT_API_BUYER_IP_HEADER = "Shopify-Storefront-Buyer-IP";
406
405
 
407
406
  // src/utils/uuid.ts
408
407
  function generateUUID() {
@@ -423,7 +422,7 @@ var warnOnce = (string) => {
423
422
  };
424
423
 
425
424
  // src/version.ts
426
- var LIB_VERSION = "2023.4.0";
425
+ var LIB_VERSION = "2023.4.2";
427
426
 
428
427
  // src/storefront.ts
429
428
  var StorefrontApiError = class extends Error {
@@ -435,16 +434,17 @@ function minifyQuery(string) {
435
434
  return string.replace(/\s*#.*$/gm, "").replace(/\s+/gm, " ").trim();
436
435
  }
437
436
  var defaultI18n = { language: "EN", country: "US" };
438
- function createStorefrontClient({
439
- storefrontHeaders,
440
- cache,
441
- waitUntil,
442
- buyerIp,
443
- i18n,
444
- requestGroupId,
445
- storefrontId,
446
- ...clientOptions
447
- }) {
437
+ function createStorefrontClient(options) {
438
+ const {
439
+ storefrontHeaders,
440
+ cache,
441
+ waitUntil,
442
+ buyerIp,
443
+ i18n,
444
+ requestGroupId,
445
+ storefrontId,
446
+ ...clientOptions
447
+ } = options;
448
448
  if (!cache) {
449
449
  warnOnce(
450
450
  "Storefront API client created without a cache instance. This may slow down your sub-requests."
@@ -457,10 +457,11 @@ function createStorefrontClient({
457
457
  getShopifyDomain
458
458
  } = hydrogenReact.createStorefrontClient(clientOptions);
459
459
  const getHeaders = clientOptions.privateStorefrontToken ? getPrivateTokenHeaders : getPublicTokenHeaders;
460
- const defaultHeaders = getHeaders({ contentType: "json" });
460
+ const defaultHeaders = getHeaders({
461
+ contentType: "json",
462
+ buyerIp: storefrontHeaders?.buyerIp || buyerIp
463
+ });
461
464
  defaultHeaders[STOREFRONT_REQUEST_GROUP_ID_HEADER] = storefrontHeaders?.requestGroupId || requestGroupId || generateUUID();
462
- if (storefrontHeaders?.buyerIp || buyerIp)
463
- defaultHeaders[STOREFRONT_API_BUYER_IP_HEADER] = storefrontHeaders?.buyerIp || buyerIp || "";
464
465
  if (storefrontId)
465
466
  defaultHeaders[hydrogenReact.SHOPIFY_STOREFRONT_ID_HEADER] = storefrontId;
466
467
  defaultHeaders["user-agent"] = `Hydrogen ${LIB_VERSION}`;
@@ -566,15 +567,15 @@ function throwError(response, errors, ErrorConstructor = Error) {
566
567
  }
567
568
 
568
569
  // src/with-cache.ts
569
- function createWithCache_unstable({
570
- cache,
571
- waitUntil
572
- }) {
573
- return (cacheKey, strategy, actionFn) => runWithCache(cacheKey, actionFn, {
574
- strategy,
575
- cacheInstance: cache,
576
- waitUntil
577
- });
570
+ function createWithCache_unstable(options) {
571
+ const { cache, waitUntil } = options;
572
+ return function withCache(cacheKey, strategy, actionFn) {
573
+ return runWithCache(cacheKey, actionFn, {
574
+ strategy,
575
+ cacheInstance: cache,
576
+ waitUntil
577
+ });
578
+ };
578
579
  }
579
580
 
580
581
  // src/cache/in-memory.ts
@@ -660,11 +661,12 @@ var InMemoryCache = class {
660
661
  return Promise.resolve(cacheKeys);
661
662
  }
662
663
  };
663
- async function storefrontRedirect({
664
- storefront,
665
- request,
666
- response = new Response("Not Found", { status: 404 })
667
- }) {
664
+ async function storefrontRedirect(options) {
665
+ const {
666
+ storefront,
667
+ request,
668
+ response = new Response("Not Found", { status: 404 })
669
+ } = options;
668
670
  const { pathname, search } = new URL(request.url);
669
671
  const redirectFrom = pathname + search;
670
672
  try {
@@ -715,7 +717,9 @@ var REDIRECT_QUERY = `#graphql
715
717
  `;
716
718
 
717
719
  // src/routing/graphiql.ts
718
- function graphiqlLoader({ context }) {
720
+ var graphiqlLoader = async function graphiqlLoader2({
721
+ context
722
+ }) {
719
723
  const storefront = context?.storefront;
720
724
  if (!storefront) {
721
725
  throw new Error(
@@ -782,7 +786,7 @@ function graphiqlLoader({ context }) {
782
786
  `,
783
787
  { status: 200, headers: { "content-type": "text/html" } }
784
788
  );
785
- }
789
+ };
786
790
 
787
791
  // src/seo/generate-seo-tags.ts
788
792
  var ERROR_PREFIX = "Error in SEO input: ";
@@ -1194,6 +1198,148 @@ function recursivelyInvokeOrReturn(value, ...rest) {
1194
1198
  }
1195
1199
  return value;
1196
1200
  }
1201
+ function Pagination({
1202
+ connection,
1203
+ children = () => {
1204
+ console.warn("<Pagination> requires children to work properly");
1205
+ return null;
1206
+ }
1207
+ }) {
1208
+ const transition = react$1.useNavigation();
1209
+ const isLoading = transition.state === "loading";
1210
+ const {
1211
+ endCursor,
1212
+ hasNextPage,
1213
+ hasPreviousPage,
1214
+ nextPageUrl,
1215
+ nodes,
1216
+ previousPageUrl,
1217
+ startCursor
1218
+ } = usePagination(connection);
1219
+ const state = react.useMemo(
1220
+ () => ({
1221
+ pageInfo: {
1222
+ endCursor,
1223
+ hasPreviousPage,
1224
+ startCursor
1225
+ },
1226
+ nodes
1227
+ }),
1228
+ [endCursor, hasPreviousPage, startCursor, nodes]
1229
+ );
1230
+ const NextLink = react.useMemo(
1231
+ () => function NextLink2(props) {
1232
+ return hasNextPage ? react.createElement(react$1.Link, {
1233
+ preventScrollReset: true,
1234
+ ...props,
1235
+ to: nextPageUrl,
1236
+ state,
1237
+ replace: true
1238
+ }) : null;
1239
+ },
1240
+ [hasNextPage, nextPageUrl]
1241
+ );
1242
+ const PreviousLink = react.useMemo(
1243
+ () => function PrevLink(props) {
1244
+ return hasPreviousPage ? react.createElement(react$1.Link, {
1245
+ preventScrollReset: true,
1246
+ ...props,
1247
+ to: previousPageUrl,
1248
+ state,
1249
+ replace: true
1250
+ }) : null;
1251
+ },
1252
+ [hasPreviousPage, previousPageUrl]
1253
+ );
1254
+ return children({
1255
+ state,
1256
+ hasNextPage,
1257
+ hasPreviousPage,
1258
+ isLoading,
1259
+ nextPageUrl,
1260
+ nodes,
1261
+ previousPageUrl,
1262
+ NextLink,
1263
+ PreviousLink
1264
+ });
1265
+ }
1266
+ function usePagination(connection) {
1267
+ const { state, search } = react$1.useLocation();
1268
+ const params = new URLSearchParams(search);
1269
+ const direction = params.get("direction");
1270
+ const isPrevious = direction === "previous";
1271
+ const nodes = react.useMemo(() => {
1272
+ if (!state || !state?.nodes) {
1273
+ return hydrogenReact.flattenConnection(connection);
1274
+ }
1275
+ if (isPrevious) {
1276
+ return [...hydrogenReact.flattenConnection(connection), ...state.nodes];
1277
+ } else {
1278
+ return [...state.nodes, ...hydrogenReact.flattenConnection(connection)];
1279
+ }
1280
+ }, [state, connection]);
1281
+ const currentPageInfo = react.useMemo(() => {
1282
+ let pageStartCursor = state?.pageInfo?.startCursor === void 0 ? connection.pageInfo.startCursor : state.pageInfo.startCursor;
1283
+ let pageEndCursor = state?.pageInfo?.endCursor === void 0 ? connection.pageInfo.endCursor : state.pageInfo.endCursor;
1284
+ if (state?.nodes) {
1285
+ if (isPrevious) {
1286
+ pageStartCursor = connection.pageInfo.startCursor;
1287
+ } else {
1288
+ pageEndCursor = connection.pageInfo.endCursor;
1289
+ }
1290
+ }
1291
+ const previousPageExists = state?.pageInfo?.hasPreviousPage === void 0 ? connection.pageInfo.hasPreviousPage : state.pageInfo.hasPreviousPage;
1292
+ const nextPageExists = state?.pageInfo?.hasNextPage === void 0 ? connection.pageInfo.hasNextPage : state.pageInfo.hasNextPage;
1293
+ return {
1294
+ startCursor: pageStartCursor,
1295
+ endCursor: pageEndCursor,
1296
+ hasPreviousPage: previousPageExists,
1297
+ hasNextPage: nextPageExists
1298
+ };
1299
+ }, [
1300
+ isPrevious,
1301
+ state,
1302
+ connection.pageInfo.hasNextPage,
1303
+ connection.pageInfo.hasPreviousPage,
1304
+ connection.pageInfo.startCursor,
1305
+ connection.pageInfo.endCursor
1306
+ ]);
1307
+ const previousPageUrl = react.useMemo(() => {
1308
+ const params2 = new URLSearchParams(search);
1309
+ params2.set("direction", "previous");
1310
+ currentPageInfo.startCursor && params2.set("cursor", currentPageInfo.startCursor);
1311
+ return `?${params2.toString()}`;
1312
+ }, [search, currentPageInfo.startCursor]);
1313
+ const nextPageUrl = react.useMemo(() => {
1314
+ const params2 = new URLSearchParams(search);
1315
+ params2.set("direction", "next");
1316
+ currentPageInfo.endCursor && params2.set("cursor", currentPageInfo.endCursor);
1317
+ return `?${params2.toString()}`;
1318
+ }, [search, currentPageInfo.endCursor]);
1319
+ return { ...currentPageInfo, previousPageUrl, nextPageUrl, nodes };
1320
+ }
1321
+ function getPaginationVariables(request, options = { pageBy: 20 }) {
1322
+ if (!(request instanceof Request)) {
1323
+ throw new Error(
1324
+ "getPaginationVariables must be called with the Request object passed to your loader function"
1325
+ );
1326
+ }
1327
+ const { pageBy } = options;
1328
+ const searchParams = new URLSearchParams(new URL(request.url).search);
1329
+ const cursor = searchParams.get("cursor") ?? void 0;
1330
+ const direction = searchParams.get("direction") === "previous" ? "previous" : "next";
1331
+ const isPrevious = direction === "previous";
1332
+ const prevPage = {
1333
+ last: pageBy,
1334
+ startCursor: cursor ?? null
1335
+ };
1336
+ const nextPage = {
1337
+ first: pageBy,
1338
+ endCursor: cursor ?? null
1339
+ };
1340
+ const variables = isPrevious ? prevPage : nextPage;
1341
+ return variables;
1342
+ }
1197
1343
 
1198
1344
  Object.defineProperty(exports, 'AnalyticsEventName', {
1199
1345
  enumerable: true,
@@ -1251,6 +1397,10 @@ Object.defineProperty(exports, 'getShopifyCookies', {
1251
1397
  enumerable: true,
1252
1398
  get: function () { return hydrogenReact.getShopifyCookies; }
1253
1399
  });
1400
+ Object.defineProperty(exports, 'parseGid', {
1401
+ enumerable: true,
1402
+ get: function () { return hydrogenReact.parseGid; }
1403
+ });
1254
1404
  Object.defineProperty(exports, 'parseMetafield', {
1255
1405
  enumerable: true,
1256
1406
  get: function () { return hydrogenReact.parseMetafield; }
@@ -1276,10 +1426,12 @@ exports.CacheLong = CacheLong;
1276
1426
  exports.CacheNone = CacheNone;
1277
1427
  exports.CacheShort = CacheShort;
1278
1428
  exports.InMemoryCache = InMemoryCache;
1429
+ exports.Pagination__unstable = Pagination;
1279
1430
  exports.Seo = Seo;
1280
1431
  exports.createStorefrontClient = createStorefrontClient;
1281
1432
  exports.createWithCache_unstable = createWithCache_unstable;
1282
1433
  exports.generateCacheControlHeader = generateCacheControlHeader;
1434
+ exports.getPaginationVariables__unstable = getPaginationVariables;
1283
1435
  exports.graphiqlLoader = graphiqlLoader;
1284
1436
  exports.isStorefrontApiError = isStorefrontApiError;
1285
1437
  exports.storefrontRedirect = storefrontRedirect;