@anker-in/shopify-sdk 0.1.1-beta.12 → 0.1.1-beta.13

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/index.mjs CHANGED
@@ -1,6 +1,71 @@
1
1
  import { SOLD_OUT_PRICE } from '@anker-in/shopify-core';
2
2
  export * from '@anker-in/shopify-core';
3
3
 
4
+ // src/utils.ts
5
+ function constructMetafieldIdentifiersQueryParams(metafieldIdentifiers = {}, metafieldNamespacePrefix) {
6
+ const identifiers = Object.entries(metafieldIdentifiers).reduce(
7
+ (queryInput, [key, value]) => {
8
+ const metafieldIdentifiers2 = value;
9
+ queryInput[`${key}MetafieldIdentifiers`] = metafieldIdentifiers2.map((item) => ({
10
+ namespace: `${metafieldNamespacePrefix}combo`,
11
+ key: item.namespace
12
+ }));
13
+ return queryInput;
14
+ },
15
+ {}
16
+ );
17
+ return identifiers;
18
+ }
19
+ var parseMetafield = (item, previewData, resource_type) => {
20
+ const type = item?.type && item?.type.toLowerCase();
21
+ switch (type) {
22
+ case "json":
23
+ case "json_string":
24
+ case "rating":
25
+ case "volume":
26
+ case "weight":
27
+ case "dimension":
28
+ return JSON.parse(item.value);
29
+ default:
30
+ return item?.value || item;
31
+ }
32
+ };
33
+ var normalizeMetafields = (metafields) => {
34
+ return metafields?.reduce(
35
+ (prev, cur) => {
36
+ if (cur) {
37
+ const namespace = cur.key;
38
+ prev[namespace] = prev[namespace] || {};
39
+ const parsedMetafields = parseMetafield(cur);
40
+ if (parsedMetafields) {
41
+ Object.entries(parsedMetafields).forEach(([key, innerMetafields]) => {
42
+ prev[namespace][key] = prev[namespace][key] ?? (innerMetafields ? parseMetafield(innerMetafields) : null);
43
+ });
44
+ }
45
+ }
46
+ return prev;
47
+ },
48
+ {}
49
+ );
50
+ };
51
+ function addInContextDirective(query, country, language) {
52
+ if (!country || !language) {
53
+ return query;
54
+ }
55
+ const operationRegex = /((?:query|mutation)\s+\w+)(\s*\([^)]*\))?\s*(\{)/;
56
+ const match = query.match(operationRegex);
57
+ if (!match) {
58
+ const braceIndex = query.indexOf("{");
59
+ if (braceIndex === -1) return query;
60
+ const beforeBrace = query.substring(0, braceIndex).trim();
61
+ const afterBrace = query.substring(braceIndex);
62
+ return `${beforeBrace} @inContext(country: ${country}, language: ${language}) ${afterBrace}`;
63
+ }
64
+ const beforeDirective = match[1] + (match[2] || "");
65
+ const afterDirective = query.substring(match.index + match[0].length - 1);
66
+ return `${beforeDirective} @inContext(country: ${country}, language: ${language}) ${afterDirective}`;
67
+ }
68
+
4
69
  // src/client/client.ts
5
70
  var ShopifyClient = class _ShopifyClient {
6
71
  config;
@@ -13,8 +78,11 @@ var ShopifyClient = class _ShopifyClient {
13
78
  * Execute a GraphQL request
14
79
  */
15
80
  async request(request, options = {}) {
16
- const { query, variables, operationName } = request;
81
+ let { query, variables, operationName } = request;
17
82
  const { tags = [], ...fetchOptions } = options;
83
+ const country = this.config.getCountryCode(this.locale);
84
+ const language = this.config.getLanguageCode(this.locale);
85
+ query = addInContextDirective(query, country, language);
18
86
  const apiUrl = this.config.getApiUrl(this.locale);
19
87
  const token = this.config.getStorefrontToken(this.locale);
20
88
  const headers = {
@@ -36,9 +104,7 @@ var ShopifyClient = class _ShopifyClient {
36
104
  ...tags.length > 0 && { next: { tags } }
37
105
  });
38
106
  if (!response.ok) {
39
- throw new Error(
40
- `HTTP ${response.status}: ${response.statusText}`
41
- );
107
+ throw new Error(`HTTP ${response.status}: ${response.statusText}`);
42
108
  }
43
109
  const json = await response.json();
44
110
  if (json.errors && json.errors.length > 0) {
@@ -54,10 +120,7 @@ var ShopifyClient = class _ShopifyClient {
54
120
  * Convenience method for simple queries
55
121
  */
56
122
  async query(query, variables, options) {
57
- const response = await this.request(
58
- { query, variables },
59
- options
60
- );
123
+ const response = await this.request({ query, variables }, options);
61
124
  return response.data;
62
125
  }
63
126
  /**
@@ -1011,54 +1074,6 @@ var updateCartDeliveryOptionsMutation = (
1011
1074
  `
1012
1075
  );
1013
1076
 
1014
- // src/utils.ts
1015
- function constructMetafieldIdentifiersQueryParams(metafieldIdentifiers = {}, metafieldNamespacePrefix) {
1016
- const identifiers = Object.entries(metafieldIdentifiers).reduce(
1017
- (queryInput, [key, value]) => {
1018
- const metafieldIdentifiers2 = value;
1019
- queryInput[`${key}MetafieldIdentifiers`] = metafieldIdentifiers2.map((item) => ({
1020
- namespace: `${metafieldNamespacePrefix}combo`,
1021
- key: item.namespace
1022
- }));
1023
- return queryInput;
1024
- },
1025
- {}
1026
- );
1027
- return identifiers;
1028
- }
1029
- var parseMetafield = (item, previewData, resource_type) => {
1030
- const type = item?.type && item?.type.toLowerCase();
1031
- switch (type) {
1032
- case "json":
1033
- case "json_string":
1034
- case "rating":
1035
- case "volume":
1036
- case "weight":
1037
- case "dimension":
1038
- return JSON.parse(item.value);
1039
- default:
1040
- return item?.value || item;
1041
- }
1042
- };
1043
- var normalizeMetafields = (metafields) => {
1044
- return metafields?.reduce(
1045
- (prev, cur) => {
1046
- if (cur) {
1047
- const namespace = cur.key;
1048
- prev[namespace] = prev[namespace] || {};
1049
- const parsedMetafields = parseMetafield(cur);
1050
- if (parsedMetafields) {
1051
- Object.entries(parsedMetafields).forEach(([key, innerMetafields]) => {
1052
- prev[namespace][key] = prev[namespace][key] ?? (innerMetafields ? parseMetafield(innerMetafields) : null);
1053
- });
1054
- }
1055
- }
1056
- return prev;
1057
- },
1058
- {}
1059
- );
1060
- };
1061
-
1062
1077
  // src/api/product/normalize.ts
1063
1078
  var normalizeSellingPlan = ({ edges }) => {
1064
1079
  return edges?.map(({ node }) => node);