Source: client.js

  1. import GraphQLJSClient from './graphql-client';
  2. import Config from './config';
  3. import ProductResource from './product-resource';
  4. import CollectionResource from './collection-resource';
  5. import ShopResource from './shop-resource';
  6. import CheckoutResource from './checkout-resource';
  7. import ImageResource from './image-resource';
  8. import {version} from '../package.json';
  9. // GraphQL
  10. import types from '../schema.json';
  11. /**
  12. * The JS Buy SDK Client.
  13. * @class
  14. *
  15. * @property {ProductResource} product The property under which product fetching methods live.
  16. * @property {CollectionResource} collection The property under which collection fetching methods live.
  17. * @property {ShopResource} shop The property under which shop fetching methods live.
  18. * @property {CartResource} cart The property under which shop fetching and mutating methods live.
  19. * @property {ImageResource} image The property under which image helper methods live.
  20. */
  21. class Client {
  22. /**
  23. * Primary entry point for building a new Client.
  24. */
  25. static buildClient(config, fetchFunction) {
  26. const newConfig = new Config(config);
  27. const client = new Client(newConfig, GraphQLJSClient, fetchFunction);
  28. client.config = newConfig;
  29. return client;
  30. }
  31. /**
  32. * @constructs Client
  33. * @param {Config} config An instance of {@link Config} used to configure the Client.
  34. */
  35. constructor(config, GraphQLClientClass = GraphQLJSClient, fetchFunction) {
  36. const url = `https://${config.domain}/api/2025-01/graphql`;
  37. const headers = {
  38. 'X-SDK-Variant': 'javascript',
  39. 'X-SDK-Version': version,
  40. 'X-Shopify-Storefront-Access-Token': config.storefrontAccessToken
  41. };
  42. if (config.source) {
  43. headers['X-SDK-Variant-Source'] = config.source;
  44. }
  45. const languageHeader = config.language ? config.language : '*';
  46. headers['Accept-Language'] = languageHeader;
  47. if (fetchFunction) {
  48. headers['Content-Type'] = 'application/json';
  49. headers.Accept = 'application/json';
  50. this.graphQLClient = new GraphQLClientClass(types, {
  51. fetcher: function fetcher(graphQLParams) {
  52. return fetchFunction(url, {
  53. body: JSON.stringify(graphQLParams),
  54. method: 'POST',
  55. mode: 'cors',
  56. headers
  57. }).then((response) => response.json());
  58. }
  59. });
  60. } else {
  61. this.graphQLClient = new GraphQLClientClass(types, {
  62. url,
  63. fetcherOptions: {headers}
  64. });
  65. }
  66. this.product = new ProductResource(this.graphQLClient);
  67. this.collection = new CollectionResource(this.graphQLClient);
  68. this.shop = new ShopResource(this.graphQLClient);
  69. this.checkout = new CheckoutResource(this.graphQLClient);
  70. this.image = new ImageResource(this.graphQLClient);
  71. }
  72. /**
  73. * Fetches the next page of models
  74. *
  75. * @example
  76. * client.fetchNextPage(products).then((nextProducts) => {
  77. * // Do something with the products
  78. * });
  79. *
  80. * @param {models} [Array] The paginated set to fetch the next page of
  81. * @return {Promise|GraphModel[]} A promise resolving with an array of `GraphModel`s of the type provided.
  82. */
  83. fetchNextPage(models) {
  84. return this.graphQLClient.fetchNextPage(models);
  85. }
  86. }
  87. export default Client;