@shopify/hydrogen-react 0.0.0-next-3eab955 → 0.0.0-next-c23a62a
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/dev/Metafield.js +6 -0
- package/dist/dev/Metafield.js.map +1 -1
- package/dist/dev/Metafield.mjs +6 -0
- package/dist/dev/Metafield.mjs.map +1 -1
- package/dist/dev/_virtual/index.js +2 -2
- package/dist/dev/_virtual/index.mjs +2 -2
- package/dist/dev/_virtual/index2.js +5 -0
- package/dist/dev/_virtual/index2.js.map +1 -0
- package/dist/dev/_virtual/index2.mjs +5 -0
- package/dist/dev/_virtual/index2.mjs.map +1 -0
- package/dist/dev/index.js +2 -0
- package/dist/dev/index.js.map +1 -1
- package/dist/dev/index.mjs +2 -0
- package/dist/dev/index.mjs.map +1 -1
- package/dist/dev/metafield-parser.js +105 -0
- package/dist/dev/metafield-parser.js.map +1 -0
- package/dist/dev/metafield-parser.mjs +105 -0
- package/dist/dev/metafield-parser.mjs.map +1 -0
- package/dist/dev/node_modules/ts-expect/dist/index.js +13 -0
- package/dist/dev/node_modules/ts-expect/dist/index.js.map +1 -0
- package/dist/dev/node_modules/ts-expect/dist/index.mjs +15 -0
- package/dist/dev/node_modules/ts-expect/dist/index.mjs.map +1 -0
- package/dist/dev/node_modules/use-sync-external-store/shim/index.js +1 -1
- package/dist/dev/node_modules/use-sync-external-store/shim/index.mjs +1 -1
- package/dist/prod/Metafield.js.map +1 -1
- package/dist/prod/Metafield.mjs.map +1 -1
- package/dist/prod/_virtual/index.js +2 -2
- package/dist/prod/_virtual/index.mjs +2 -2
- package/dist/prod/_virtual/index2.js +5 -0
- package/dist/prod/_virtual/index2.js.map +1 -0
- package/dist/prod/_virtual/index2.mjs +5 -0
- package/dist/prod/_virtual/index2.mjs.map +1 -0
- package/dist/prod/index.js +2 -0
- package/dist/prod/index.js.map +1 -1
- package/dist/prod/index.mjs +2 -0
- package/dist/prod/index.mjs.map +1 -1
- package/dist/prod/metafield-parser.js +116 -0
- package/dist/prod/metafield-parser.js.map +1 -0
- package/dist/prod/metafield-parser.mjs +116 -0
- package/dist/prod/metafield-parser.mjs.map +1 -0
- package/dist/prod/node_modules/ts-expect/dist/index.js +13 -0
- package/dist/prod/node_modules/ts-expect/dist/index.js.map +1 -0
- package/dist/prod/node_modules/ts-expect/dist/index.mjs +15 -0
- package/dist/prod/node_modules/ts-expect/dist/index.mjs.map +1 -0
- package/dist/prod/node_modules/use-sync-external-store/shim/index.js +1 -1
- package/dist/prod/node_modules/use-sync-external-store/shim/index.mjs +1 -1
- package/dist/types/Metafield.d.ts +3 -0
- package/dist/types/index.d.cts +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/metafield-parser.d.ts +222 -0
- package/dist/umd/hydrogen-react.dev.js +114 -0
- package/dist/umd/hydrogen-react.dev.js.map +1 -1
- package/dist/umd/hydrogen-react.prod.js +17 -17
- package/dist/umd/hydrogen-react.prod.js.map +1 -1
- package/package.json +2 -1
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { parseJSON } from "./Metafield.mjs";
|
|
2
|
+
import { expectType as expectType_1 } from "./node_modules/ts-expect/dist/index.mjs";
|
|
3
|
+
import { flattenConnection } from "./flatten-connection.mjs";
|
|
4
|
+
function metafieldParser(metafield) {
|
|
5
|
+
var _a, _b, _c, _d;
|
|
6
|
+
if (!metafield.type) {
|
|
7
|
+
const noTypeError = `metafieldParser(): The 'type' field is required in order to parse the Metafield.`;
|
|
8
|
+
{
|
|
9
|
+
console.error(`${noTypeError} Returning 'parsedValue' of 'null'`);
|
|
10
|
+
return {
|
|
11
|
+
...metafield,
|
|
12
|
+
parsedValue: null
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
switch (metafield.type) {
|
|
17
|
+
case "boolean":
|
|
18
|
+
return {
|
|
19
|
+
...metafield,
|
|
20
|
+
parsedValue: metafield.value === "true"
|
|
21
|
+
};
|
|
22
|
+
case "collection_reference":
|
|
23
|
+
case "file_reference":
|
|
24
|
+
case "page_reference":
|
|
25
|
+
case "product_reference":
|
|
26
|
+
case "variant_reference":
|
|
27
|
+
return {
|
|
28
|
+
...metafield,
|
|
29
|
+
parsedValue: metafield.reference
|
|
30
|
+
};
|
|
31
|
+
case "color":
|
|
32
|
+
case "multi_line_text_field":
|
|
33
|
+
case "single_line_text_field":
|
|
34
|
+
case "url":
|
|
35
|
+
return {
|
|
36
|
+
...metafield,
|
|
37
|
+
parsedValue: metafield.value
|
|
38
|
+
};
|
|
39
|
+
case "dimension":
|
|
40
|
+
case "money":
|
|
41
|
+
case "json":
|
|
42
|
+
case "rating":
|
|
43
|
+
case "volume":
|
|
44
|
+
case "weight":
|
|
45
|
+
case "list.color":
|
|
46
|
+
case "list.dimension":
|
|
47
|
+
case "list.number_integer":
|
|
48
|
+
case "list.number_decimal":
|
|
49
|
+
case "list.rating":
|
|
50
|
+
case "list.single_line_text_field":
|
|
51
|
+
case "list.url":
|
|
52
|
+
case "list.volume":
|
|
53
|
+
case "list.weight": {
|
|
54
|
+
let parsedValue = null;
|
|
55
|
+
try {
|
|
56
|
+
parsedValue = parseJSON((_a = metafield.value) != null ? _a : "");
|
|
57
|
+
} catch (err) {
|
|
58
|
+
const parseError = `metafieldParser(): attempted to JSON.parse the 'metafield.value' property, but failed.`;
|
|
59
|
+
{
|
|
60
|
+
console.error(`${parseError} Returning 'null' for 'parsedValue'`);
|
|
61
|
+
}
|
|
62
|
+
parsedValue = null;
|
|
63
|
+
}
|
|
64
|
+
return {
|
|
65
|
+
...metafield,
|
|
66
|
+
parsedValue
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
case "date":
|
|
70
|
+
case "date_time":
|
|
71
|
+
return {
|
|
72
|
+
...metafield,
|
|
73
|
+
parsedValue: new Date((_b = metafield.value) != null ? _b : "")
|
|
74
|
+
};
|
|
75
|
+
case "list.date":
|
|
76
|
+
case "list.date_time": {
|
|
77
|
+
const jsonParseValue = parseJSON((_c = metafield == null ? void 0 : metafield.value) != null ? _c : "");
|
|
78
|
+
return {
|
|
79
|
+
...metafield,
|
|
80
|
+
parsedValue: jsonParseValue.map((dateString) => new Date(dateString))
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
case "number_decimal":
|
|
84
|
+
case "number_integer":
|
|
85
|
+
return {
|
|
86
|
+
...metafield,
|
|
87
|
+
parsedValue: Number(metafield.value)
|
|
88
|
+
};
|
|
89
|
+
case "list.collection_reference":
|
|
90
|
+
case "list.file_reference":
|
|
91
|
+
case "list.page_reference":
|
|
92
|
+
case "list.product_reference":
|
|
93
|
+
case "list.variant_reference":
|
|
94
|
+
return {
|
|
95
|
+
...metafield,
|
|
96
|
+
parsedValue: flattenConnection((_d = metafield.references) != null ? _d : void 0)
|
|
97
|
+
};
|
|
98
|
+
default: {
|
|
99
|
+
const typeNotFoundError = `metafieldParser(): the 'metafield.type' you passed in is not supported. Your type: "${metafield.type}". If you believe this is an error, please open an issue on GitHub.`;
|
|
100
|
+
{
|
|
101
|
+
console.error(
|
|
102
|
+
`${typeNotFoundError} Returning 'parsedValue' of 'null'`
|
|
103
|
+
);
|
|
104
|
+
return {
|
|
105
|
+
...metafield,
|
|
106
|
+
parsedValue: null
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
expectType_1(true);
|
|
113
|
+
export {
|
|
114
|
+
metafieldParser
|
|
115
|
+
};
|
|
116
|
+
//# sourceMappingURL=metafield-parser.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"metafield-parser.mjs","sources":["../../src/metafield-parser.ts"],"sourcesContent":["import type {\n Collection,\n GenericFile,\n Metafield as MetafieldBaseType,\n MoneyV2,\n Page,\n Product,\n ProductVariant,\n} from './storefront-api-types.js';\nimport type {PartialDeep, Simplify} from 'type-fest';\nimport {parseJSON} from './Metafield.js';\nimport {TypeEqual, expectType} from 'ts-expect';\nimport {flattenConnection} from './flatten-connection.js';\n\n/**\n * A temporary function that will be renamed to `parseMetafield()` in a future release.\n *\n * A function that uses `metafield.type` to parse the Metafield's `value` or `reference` or `references` (depending on the `type`) and put it in `metafield.parsedValue`\n *\n * TypeScript developers can use the type `ParsedMetafields` from this package to get the returned object's type correct. For example:\n *\n * ```\n * metafieldParser<ParsedMetafields['boolean']>({type: 'boolean', value: 'false'}\n * ```\n */\nexport function metafieldParser<ReturnGeneric>(\n metafield: PartialDeep<MetafieldBaseType, {recurseIntoArrays: true}>\n): ReturnGeneric {\n if (!metafield.type) {\n const noTypeError = `metafieldParser(): The 'type' field is required in order to parse the Metafield.`;\n if (__HYDROGEN_DEV__) {\n throw new Error(noTypeError);\n } else {\n console.error(`${noTypeError} Returning 'parsedValue' of 'null'`);\n return {\n ...metafield,\n parsedValue: null,\n } as ReturnGeneric;\n }\n }\n\n switch (metafield.type) {\n case 'boolean':\n return {\n ...metafield,\n parsedValue: metafield.value === 'true',\n } as ReturnGeneric;\n\n case 'collection_reference':\n case 'file_reference':\n case 'page_reference':\n case 'product_reference':\n case 'variant_reference':\n return {\n ...metafield,\n parsedValue: metafield.reference,\n } as ReturnGeneric;\n\n case 'color':\n case 'multi_line_text_field':\n case 'single_line_text_field':\n case 'url':\n return {\n ...metafield,\n parsedValue: metafield.value,\n } as ReturnGeneric;\n\n // TODO: 'money' should probably be parsed even further to like `useMoney()`, but that logic needs to be extracted first so it's not a hook\n case 'dimension':\n case 'money':\n case 'json':\n case 'rating':\n case 'volume':\n case 'weight':\n case 'list.color':\n case 'list.dimension':\n case 'list.number_integer':\n case 'list.number_decimal':\n case 'list.rating':\n case 'list.single_line_text_field':\n case 'list.url':\n case 'list.volume':\n case 'list.weight': {\n let parsedValue = null;\n try {\n parsedValue = parseJSON(metafield.value ?? '');\n } catch (err) {\n const parseError = `metafieldParser(): attempted to JSON.parse the 'metafield.value' property, but failed.`;\n if (__HYDROGEN_DEV__) {\n throw new Error(parseError);\n } else {\n console.error(`${parseError} Returning 'null' for 'parsedValue'`);\n }\n parsedValue = null;\n }\n return {\n ...metafield,\n parsedValue,\n } as ReturnGeneric;\n }\n\n case 'date':\n case 'date_time':\n return {\n ...metafield,\n parsedValue: new Date(metafield.value ?? ''),\n } as ReturnGeneric;\n\n case 'list.date':\n case 'list.date_time': {\n const jsonParseValue = parseJSON(metafield?.value ?? '') as string[];\n return {\n ...metafield,\n parsedValue: jsonParseValue.map((dateString) => new Date(dateString)),\n } as ReturnGeneric;\n }\n\n case 'number_decimal':\n case 'number_integer':\n return {\n ...metafield,\n parsedValue: Number(metafield.value),\n } as ReturnGeneric;\n\n case 'list.collection_reference':\n case 'list.file_reference':\n case 'list.page_reference':\n case 'list.product_reference':\n case 'list.variant_reference':\n return {\n ...metafield,\n parsedValue: flattenConnection(metafield.references ?? undefined),\n } as ReturnGeneric;\n\n default: {\n const typeNotFoundError = `metafieldParser(): the 'metafield.type' you passed in is not supported. Your type: \"${metafield.type}\". If you believe this is an error, please open an issue on GitHub.`;\n if (__HYDROGEN_DEV__) {\n throw new Error(typeNotFoundError);\n } else {\n console.error(\n `${typeNotFoundError} Returning 'parsedValue' of 'null'`\n );\n return {\n ...metafield,\n parsedValue: null,\n } as ReturnGeneric;\n }\n }\n }\n}\n\n// taken from https://shopify.dev/apps/metafields/types\nexport const allMetafieldTypesArray = [\n 'boolean',\n 'collection_reference',\n 'color',\n 'date',\n 'date_time',\n 'dimension',\n 'file_reference',\n 'json',\n 'money',\n 'multi_line_text_field',\n 'number_decimal',\n 'number_integer',\n 'page_reference',\n 'product_reference',\n 'rating',\n 'single_line_text_field',\n 'url',\n 'variant_reference',\n 'volume',\n 'weight',\n // list metafields\n 'list.collection_reference',\n 'list.color',\n 'list.date',\n 'list.date_time',\n 'list.dimension',\n 'list.file_reference',\n 'list.number_integer',\n 'list.number_decimal',\n 'list.page_reference',\n 'list.product_reference',\n 'list.rating',\n 'list.single_line_text_field',\n 'list.url',\n 'list.variant_reference',\n 'list.volume',\n 'list.weight',\n] as const;\n\n/** A union of all the supported `metafield.type`s */\nexport type MetafieldTypeTypes = typeof allMetafieldTypesArray[number];\n\n/**\n * A mapping of a Metafield's `type` to the TypeScript type that is returned from `metafieldParser()`\n * For example, when using `metafieldParser()`, the type will be correctly returned when used like the following:\n *\n * ```\n * const parsedMetafield = metafieldParser<ParsedMetafields['boolean']>(metafield);`\n * ```\n * `parsedMetafield.parsedValue`'s type is now `boolean`\n */\nexport type ParsedMetafields<ExtraTypeGeneric = void> = {\n /** A Metafield that's been parsed, with a `parsedValue` of `boolean` */\n boolean: Simplify<BooleanParsedMetafield>;\n /** A Metafield that's been parsed, with a `parsedValue` of a `Collection` object (as defined by the Storefront API) */\n collection_reference: Simplify<CollectionParsedRefMetafield>;\n /** A Metafield that's been parsed, with a `parsedValue` of type `string` */\n color: Simplify<ColorParsedMetafield>;\n /** A Metafield that's been parsed, with a `parsedValue` of type `Date` */\n date: Simplify<DatesParsedMetafield>;\n /** A Metafield that's been parsed, with a `parsedValue` of type `Date` */\n date_time: Simplify<DatesParsedMetafield>;\n /** A Metafield that's been parsed, with a `parsedValue` of type `Measurement` */\n dimension: Simplify<MeasurementParsedMetafield>;\n /** A Metafield that's been parsed, with a `parsedValue` of a `GenericFile` object (as defined by the Storefront API) */\n file_reference: Simplify<FileRefParsedMetafield>;\n /**\n * A Metafield that's been parsed, with a `parsedValue` of type `unknown`, unless you pass in the type as a generic. For example:\n *\n * ```\n * ParsedMetafields<MyJsonType>['json']\n * ```\n */\n json: Simplify<JsonParsedMetafield<ExtraTypeGeneric>>;\n /** A Metafield that's been parsed, with a `parsedValue` of type `Money` */\n money: Simplify<MoneyParsedMetafield>;\n /** A Metafield that's been parsed, with a `parsedValue` of type `string` */\n multi_line_text_field: Simplify<TextParsedMetafield>;\n /** A Metafield that's been parsed, with a `parsedValue` of type `number` */\n number_decimal: Simplify<NumberParsedMetafield>;\n /** A Metafield that's been parsed, with a `parsedValue` of type `number` */\n number_integer: Simplify<NumberParsedMetafield>;\n /** A Metafield that's been parsed, with a `parsedValue` of a `Page` object (as defined by the Storefront API) */\n page_reference: Simplify<PageParsedRefMetafield>;\n /** A Metafield that's been parsed, with a `parsedValue` of a `Product` object (as defined by the Storefront API) */\n product_reference: Simplify<ProductParsedRefMetafield>;\n /** A Metafield that's been parsed, with a `parsedValue` of type `Rating` */\n rating: Simplify<RatingParsedMetafield>;\n /** A Metafield that's been parsed, with a `parsedValue` of type `string` */\n single_line_text_field: Simplify<TextParsedMetafield>;\n /** A Metafield that's been parsed, with a `parsedValue` of type `string` */\n url: Simplify<TextParsedMetafield>;\n /** A Metafield that's been parsed, with a `parsedValue` of a `ProductVariant` object (as defined by the Storefront API) */\n variant_reference: Simplify<VariantParsedRefMetafield>;\n /** A Metafield that's been parsed, with a `parsedValue` of type `Measurement` */\n volume: Simplify<MeasurementParsedMetafield>;\n /** A Metafield that's been parsed, with a `parsedValue` of type `Measurement` */\n weight: Simplify<MeasurementParsedMetafield>;\n // list metafields\n /** A Metafield that's been parsed, with a `parsedValue` of an array of `Collection` objects (as defined by the Storefront API) */\n 'list.collection_reference': Simplify<CollectionListParsedRefMetafield>;\n /** A Metafield that's been parsed, with a `parsedValue` of an array of strings */\n 'list.color': Simplify<ColorListParsedMetafield>;\n /** A Metafield that's been parsed, with a `parsedValue` of an array of Date objects */\n 'list.date': Simplify<DatesListParsedMetafield>;\n /** A Metafield that's been parsed, with a `parsedValue` of an array of Date objects */\n 'list.date_time': Simplify<DatesListParsedMetafield>;\n /** A Metafield that's been parsed, with a `parsedValue` of an array of `Measurement` objects */\n 'list.dimension': Simplify<MeasurementListParsedMetafield>;\n /** A Metafield that's been parsed, with a `parsedValue` of an array of `GenericFile` objects (as defined by the Storefront API) */\n 'list.file_reference': Simplify<FileListParsedRefMetafield>;\n /** A Metafield that's been parsed, with a `parsedValue` of an array of numbers */\n 'list.number_integer': Simplify<NumberListParsedMetafield>;\n /** A Metafield that's been parsed, with a `parsedValue` of an array of numbers */\n 'list.number_decimal': Simplify<NumberListParsedMetafield>;\n /** A Metafield that's been parsed, with a `parsedValue` of an array of `Page` objects (as defined by the Storefront API) */\n 'list.page_reference': Simplify<PageListParsedRefMetafield>;\n /** A Metafield that's been parsed, with a `parsedValue` of an array of `Product` objects (as defined by the Storefront API) */\n 'list.product_reference': Simplify<ProductListParsedRefMetafield>;\n /** A Metafield that's been parsed, with a `parsedValue` of an array of `Rating`s */\n 'list.rating': Simplify<RatingListParsedMetafield>;\n /** A Metafield that's been parsed, with a `parsedValue` of an array of strings */\n 'list.single_line_text_field': Simplify<TextListParsedMetafield>;\n /** A Metafield that's been parsed, with a `parsedValue` of an array of strings */\n 'list.url': Simplify<TextListParsedMetafield>;\n /** A Metafield that's been parsed, with a `parsedValue` of an array of `ProductVariant` objects (as defined by the Storefront API) */\n 'list.variant_reference': Simplify<VariantListParsedRefMetafield>;\n /** A Metafield that's been parsed, with a `parsedValue` of an array of `Measurement`s */\n 'list.volume': Simplify<MeasurementListParsedMetafield>;\n /** A Metafield that's been parsed, with a `parsedValue` of an array of `Measurement`s */\n 'list.weight': Simplify<MeasurementListParsedMetafield>;\n};\n\n// This test is to ensure that ParsedMetafields has a key for every item in 'allMetafieldsTypesArray'\nexpectType<TypeEqual<keyof ParsedMetafields, MetafieldTypeTypes>>(true);\n\ninterface ParsedBase extends MetafieldBaseType {\n type: MetafieldTypeTypes;\n parsedValue: unknown;\n}\n\ninterface BooleanParsedMetafield extends ParsedBase {\n type: 'boolean';\n parsedValue: boolean | null;\n}\ntype CollectionParsedRefMetafield = MetafieldBaseType & {\n type: 'collection_reference';\n parsedValue: Collection | null;\n};\ntype ColorParsedMetafield = MetafieldBaseType & {\n type: 'color';\n parsedValue: string | null;\n};\ntype DatesParsedMetafield = MetafieldBaseType & {\n type: 'date' | 'date_time';\n parsedValue: Date | null;\n};\n\ntype MeasurementParsedMetafield = MetafieldBaseType & {\n type: 'dimension' | 'weight' | 'volume';\n parsedValue: Measurement | null;\n};\n\ntype FileRefParsedMetafield = MetafieldBaseType & {\n type: 'file_reference';\n parsedValue: GenericFile | null;\n};\n\ntype JsonParsedMetafield<JsonTypeGeneric = void> = MetafieldBaseType & {\n type: 'json';\n parsedValue: JsonTypeGeneric extends void ? unknown : JsonTypeGeneric | null;\n};\n\ntype MoneyParsedMetafield = MetafieldBaseType & {\n type: 'money';\n parsedValue: MoneyV2 | null;\n};\n\ntype TextParsedMetafield = MetafieldBaseType & {\n type: 'single_line_text_field' | 'multi_line_text_field' | 'url';\n parsedValue: string | null;\n};\n\ntype NumberParsedMetafield = MetafieldBaseType & {\n type: 'number_decimal' | 'number_integer';\n parsedValue: number | null;\n};\n\ntype PageParsedRefMetafield = MetafieldBaseType & {\n type: 'page_reference';\n parsedValue: Page | null;\n};\n\ntype ProductParsedRefMetafield = MetafieldBaseType & {\n type: 'product_reference';\n parsedValue: Product | null;\n};\n\ntype RatingParsedMetafield = MetafieldBaseType & {\n type: 'rating';\n parsedValue: Rating | null;\n};\n\ntype VariantParsedRefMetafield = MetafieldBaseType & {\n type: 'variant_reference';\n parsedValue: ProductVariant | null;\n};\n\ntype CollectionListParsedRefMetafield = MetafieldBaseType & {\n type: 'list.collection_reference';\n parsedValue: Array<Collection> | null;\n};\n\ntype ColorListParsedMetafield = MetafieldBaseType & {\n type: 'list.color';\n parsedValue: Array<string> | null;\n};\n\ntype DatesListParsedMetafield = MetafieldBaseType & {\n type: 'list.date' | 'list.date_time';\n parsedValue: Array<Date> | null;\n};\n\ntype MeasurementListParsedMetafield = MetafieldBaseType & {\n type: 'list.dimension' | 'list.weight' | 'list.volume';\n parsedValue: Array<Measurement> | null;\n};\n\ntype FileListParsedRefMetafield = MetafieldBaseType & {\n type: 'list.file_reference';\n parsedValue: Array<GenericFile> | null;\n};\n\ntype TextListParsedMetafield = MetafieldBaseType & {\n type: 'list.single_line_text_field' | 'list.url';\n parsedValue: Array<string> | null;\n};\n\ntype NumberListParsedMetafield = MetafieldBaseType & {\n type: 'list.number_decimal' | 'list.number_integer';\n parsedValue: Array<number> | null;\n};\n\ntype PageListParsedRefMetafield = MetafieldBaseType & {\n type: 'list.page_reference';\n parsedValue: Array<Page> | null;\n};\n\ntype ProductListParsedRefMetafield = MetafieldBaseType & {\n type: 'list.product_reference';\n parsedValue: Array<Product> | null;\n};\n\ntype RatingListParsedMetafield = MetafieldBaseType & {\n type: 'list.rating';\n parsedValue: Array<Rating> | null;\n};\n\ntype VariantListParsedRefMetafield = MetafieldBaseType & {\n type: 'list.variant_reference';\n parsedValue: Array<ProductVariant> | null;\n};\n\nexport type Measurement = {\n unit: string;\n value: number;\n};\n\nexport interface Rating {\n value: number;\n scale_min: number;\n scale_max: number;\n}\n"],"names":["expectType"],"mappings":";;;AAyBO,SAAS,gBACd,WACe;;AACX,MAAA,CAAC,UAAU,MAAM;AACnB,UAAM,cAAc;AAGb;AACG,cAAA,MAAM,GAAG,+CAA+C;AACzD,aAAA;AAAA,QACL,GAAG;AAAA,QACH,aAAa;AAAA,MAAA;AAAA,IAEjB;AAAA,EACF;AAEA,UAAQ,UAAU,MAAM;AAAA,IACtB,KAAK;AACI,aAAA;AAAA,QACL,GAAG;AAAA,QACH,aAAa,UAAU,UAAU;AAAA,MAAA;AAAA,IAGrC,KAAK;AAAA,IACL,KAAK;AAAA,IACL,KAAK;AAAA,IACL,KAAK;AAAA,IACL,KAAK;AACI,aAAA;AAAA,QACL,GAAG;AAAA,QACH,aAAa,UAAU;AAAA,MAAA;AAAA,IAG3B,KAAK;AAAA,IACL,KAAK;AAAA,IACL,KAAK;AAAA,IACL,KAAK;AACI,aAAA;AAAA,QACL,GAAG;AAAA,QACH,aAAa,UAAU;AAAA,MAAA;AAAA,IAI3B,KAAK;AAAA,IACL,KAAK;AAAA,IACL,KAAK;AAAA,IACL,KAAK;AAAA,IACL,KAAK;AAAA,IACL,KAAK;AAAA,IACL,KAAK;AAAA,IACL,KAAK;AAAA,IACL,KAAK;AAAA,IACL,KAAK;AAAA,IACL,KAAK;AAAA,IACL,KAAK;AAAA,IACL,KAAK;AAAA,IACL,KAAK;AAAA,IACL,KAAK,eAAe;AAClB,UAAI,cAAc;AACd,UAAA;AACY,sBAAA,WAAU,eAAU,UAAV,YAAmB,EAAE;AAAA,eACtC;AACP,cAAM,aAAa;AAGZ;AACG,kBAAA,MAAM,GAAG,+CAA+C;AAAA,QAClE;AACc,sBAAA;AAAA,MAChB;AACO,aAAA;AAAA,QACL,GAAG;AAAA,QACH;AAAA,MAAA;AAAA,IAEJ;AAAA,IAEA,KAAK;AAAA,IACL,KAAK;AACI,aAAA;AAAA,QACL,GAAG;AAAA,QACH,aAAa,IAAI,MAAK,eAAU,UAAV,YAAmB,EAAE;AAAA,MAAA;AAAA,IAG/C,KAAK;AAAA,IACL,KAAK,kBAAkB;AACrB,YAAM,iBAAiB,WAAU,4CAAW,UAAX,YAAoB,EAAE;AAChD,aAAA;AAAA,QACL,GAAG;AAAA,QACH,aAAa,eAAe,IAAI,CAAC,eAAe,IAAI,KAAK,UAAU,CAAC;AAAA,MAAA;AAAA,IAExE;AAAA,IAEA,KAAK;AAAA,IACL,KAAK;AACI,aAAA;AAAA,QACL,GAAG;AAAA,QACH,aAAa,OAAO,UAAU,KAAK;AAAA,MAAA;AAAA,IAGvC,KAAK;AAAA,IACL,KAAK;AAAA,IACL,KAAK;AAAA,IACL,KAAK;AAAA,IACL,KAAK;AACI,aAAA;AAAA,QACL,GAAG;AAAA,QACH,aAAa,mBAAkB,eAAU,eAAV,YAAwB,MAAS;AAAA,MAAA;AAAA,IAGpE,SAAS;AACD,YAAA,oBAAoB,uFAAuF,UAAU;AAGpH;AACG,gBAAA;AAAA,UACN,GAAG;AAAA,QAAA;AAEE,eAAA;AAAA,UACL,GAAG;AAAA,UACH,aAAa;AAAA,QAAA;AAAA,MAEjB;AAAA,IACF;AAAA,EACF;AACF;AA0IAA,aAAkE,IAAI;"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
|
|
3
|
+
const index = require("../../../_virtual/index.js");
|
|
4
|
+
Object.defineProperty(index.__exports, "__esModule", { value: true });
|
|
5
|
+
index.__exports.expectNever = exports.expectType = index.__exports.expectType = void 0;
|
|
6
|
+
const expectType = (value) => void 0;
|
|
7
|
+
exports.expectType = index.__exports.expectType = expectType;
|
|
8
|
+
const expectNever = (value) => {
|
|
9
|
+
throw new TypeError("Unexpected value: " + value);
|
|
10
|
+
};
|
|
11
|
+
index.__exports.expectNever = expectNever;
|
|
12
|
+
exports.default = index.__exports;
|
|
13
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../../../../../../node_modules/ts-expect/dist/index.js"],"sourcesContent":["\"use strict\";\nObject.defineProperty(exports, \"__esModule\", { value: true });\nexports.expectNever = exports.expectType = void 0;\n/**\n * Asserts the `value` type is assignable to the generic `Type`.\n *\n * ```ts\n * expectType<number>(123);\n * expectType<boolean>(true);\n * ```\n */\nconst expectType = (value) => void 0;\nexports.expectType = expectType;\n/**\n * Asserts the `value` type is `never`, i.e. this function should never be called.\n * If it is called at runtime, it will throw a `TypeError`. The return type is\n * `never` to support returning in exhaustive type checks.\n *\n * ```ts\n * return expectNever(value);\n * ```\n */\nconst expectNever = (value) => {\n throw new TypeError(\"Unexpected value: \" + value);\n};\nexports.expectNever = expectNever;\n//# sourceMappingURL=index.js.map"],"names":["dist","expectType_1"],"mappings":";;;AACA,OAAO,eAAeA,MAAAA,WAAS,cAAc,EAAE,OAAO,KAAI,CAAE;AAC5DA,MAAA,UAAA,cAAsBC,QAAA,aAAAD,gBAAA,aAAqB;AAS3C,MAAM,aAAa,CAAC,UAAU;AACZC,QAAAA,aAAAD,MAAAA,UAAA,aAAG;AAUrB,MAAM,cAAc,CAAC,UAAU;AAC3B,QAAM,IAAI,UAAU,uBAAuB,KAAK;AACpD;AACmBA,MAAA,UAAA,cAAG;;"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { __exports as dist } from "../../../_virtual/index.mjs";
|
|
2
|
+
import { __exports } from "../../../_virtual/index.mjs";
|
|
3
|
+
Object.defineProperty(dist, "__esModule", { value: true });
|
|
4
|
+
dist.expectNever = expectType_1 = dist.expectType = void 0;
|
|
5
|
+
const expectType = (value) => void 0;
|
|
6
|
+
var expectType_1 = dist.expectType = expectType;
|
|
7
|
+
const expectNever = (value) => {
|
|
8
|
+
throw new TypeError("Unexpected value: " + value);
|
|
9
|
+
};
|
|
10
|
+
dist.expectNever = expectNever;
|
|
11
|
+
export {
|
|
12
|
+
__exports as default,
|
|
13
|
+
expectType_1 as expectType
|
|
14
|
+
};
|
|
15
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":["../../../../../../../node_modules/ts-expect/dist/index.js"],"sourcesContent":["\"use strict\";\nObject.defineProperty(exports, \"__esModule\", { value: true });\nexports.expectNever = exports.expectType = void 0;\n/**\n * Asserts the `value` type is assignable to the generic `Type`.\n *\n * ```ts\n * expectType<number>(123);\n * expectType<boolean>(true);\n * ```\n */\nconst expectType = (value) => void 0;\nexports.expectType = expectType;\n/**\n * Asserts the `value` type is `never`, i.e. this function should never be called.\n * If it is called at runtime, it will throw a `TypeError`. The return type is\n * `never` to support returning in exhaustive type checks.\n *\n * ```ts\n * return expectNever(value);\n * ```\n */\nconst expectNever = (value) => {\n throw new TypeError(\"Unexpected value: \" + value);\n};\nexports.expectNever = expectNever;\n//# sourceMappingURL=index.js.map"],"names":[],"mappings":";;AACA,OAAO,eAAe,MAAS,cAAc,EAAE,OAAO,KAAI,CAAE;AAC5D,KAAA,cAAsB,eAAA,KAAA,aAAqB;AAS3C,MAAM,aAAa,CAAC,UAAU;AACZ,IAAA,eAAA,KAAA,aAAG;AAUrB,MAAM,cAAc,CAAC,UAAU;AAC3B,QAAM,IAAI,UAAU,uBAAuB,KAAK;AACpD;AACmB,KAAA,cAAG;"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
|
|
3
|
-
const index = require("../../../_virtual/
|
|
3
|
+
const index = require("../../../_virtual/index2.js");
|
|
4
4
|
const useSyncExternalStoreShim_production_min = require("../cjs/use-sync-external-store-shim.production.min.js");
|
|
5
5
|
const useSyncExternalStoreShim_development = require("../cjs/use-sync-external-store-shim.development.js");
|
|
6
6
|
var hasRequiredShim;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { s as shim } from "../../../_virtual/
|
|
1
|
+
import { s as shim } from "../../../_virtual/index2.mjs";
|
|
2
2
|
import { __require as requireUseSyncExternalStoreShim_production_min } from "../cjs/use-sync-external-store-shim.production.min.mjs";
|
|
3
3
|
import { __require as requireUseSyncExternalStoreShim_development } from "../cjs/use-sync-external-store-shim.development.mjs";
|
|
4
4
|
var hasRequiredShim;
|
|
@@ -22,6 +22,8 @@ export declare function Metafield<ComponentGeneric extends ElementType>(props: M
|
|
|
22
22
|
* The `parseMetafield` utility transforms a [Metafield](https://shopify.dev/api/storefront/reference/common-objects/Metafield)
|
|
23
23
|
* into a new object whose `values` have been parsed according to the metafield `type`.
|
|
24
24
|
* If the metafield is `null`, then it returns `null` back.
|
|
25
|
+
*
|
|
26
|
+
* Note that `parseMetafield()` will have a breaking change in a future version; it will change to behave like `metafieldParser()`.
|
|
25
27
|
*/
|
|
26
28
|
export declare function parseMetafield(
|
|
27
29
|
/** A [Metafield](https://shopify.dev/api/storefront/reference/common-objects/Metafield) or null */
|
|
@@ -32,6 +34,7 @@ metafield: PartialDeep<MetafieldType, {
|
|
|
32
34
|
}> | null;
|
|
33
35
|
/**
|
|
34
36
|
* The `parseMetafieldValue` function parses a [Metafield](https://shopify.dev/api/storefront/reference/common-objects/metafield)'s `value` from a string into a sensible type corresponding to the [Metafield](https://shopify.dev/api/storefront/reference/common-objects/metafield)'s `type`.
|
|
37
|
+
* @deprecated `parseMetafieldValue()` is unsupported and will be removed in a future version.
|
|
35
38
|
*/
|
|
36
39
|
export declare function parseMetafieldValue(metafield: PartialDeep<MetafieldType, {
|
|
37
40
|
recurseIntoArrays: true;
|
package/dist/types/index.d.cts
CHANGED
|
@@ -4,6 +4,7 @@ export { ExternalVideo } from './ExternalVideo.js';
|
|
|
4
4
|
export { flattenConnection } from './flatten-connection.js';
|
|
5
5
|
export { Image } from './Image.js';
|
|
6
6
|
export { MediaFile } from './MediaFile.js';
|
|
7
|
+
export { metafieldParser, type ParsedMetafields } from './metafield-parser.js';
|
|
7
8
|
export { Metafield, parseMetafield, parseMetafieldValue } from './Metafield.js';
|
|
8
9
|
export { ModelViewer } from './ModelViewer.js';
|
|
9
10
|
export { Money } from './Money.js';
|
package/dist/types/index.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ export { ExternalVideo } from './ExternalVideo.js';
|
|
|
4
4
|
export { flattenConnection } from './flatten-connection.js';
|
|
5
5
|
export { Image } from './Image.js';
|
|
6
6
|
export { MediaFile } from './MediaFile.js';
|
|
7
|
+
export { metafieldParser, type ParsedMetafields } from './metafield-parser.js';
|
|
7
8
|
export { Metafield, parseMetafield, parseMetafieldValue } from './Metafield.js';
|
|
8
9
|
export { ModelViewer } from './ModelViewer.js';
|
|
9
10
|
export { Money } from './Money.js';
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
import type { Collection, GenericFile, Metafield as MetafieldBaseType, MoneyV2, Page, Product, ProductVariant } from './storefront-api-types.js';
|
|
2
|
+
import type { PartialDeep, Simplify } from 'type-fest';
|
|
3
|
+
/**
|
|
4
|
+
* A temporary function that will be renamed to `parseMetafield()` in a future release.
|
|
5
|
+
*
|
|
6
|
+
* A function that uses `metafield.type` to parse the Metafield's `value` or `reference` or `references` (depending on the `type`) and put it in `metafield.parsedValue`
|
|
7
|
+
*
|
|
8
|
+
* TypeScript developers can use the type `ParsedMetafields` from this package to get the returned object's type correct. For example:
|
|
9
|
+
*
|
|
10
|
+
* ```
|
|
11
|
+
* metafieldParser<ParsedMetafields['boolean']>({type: 'boolean', value: 'false'}
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export declare function metafieldParser<ReturnGeneric>(metafield: PartialDeep<MetafieldBaseType, {
|
|
15
|
+
recurseIntoArrays: true;
|
|
16
|
+
}>): ReturnGeneric;
|
|
17
|
+
export declare const allMetafieldTypesArray: readonly ["boolean", "collection_reference", "color", "date", "date_time", "dimension", "file_reference", "json", "money", "multi_line_text_field", "number_decimal", "number_integer", "page_reference", "product_reference", "rating", "single_line_text_field", "url", "variant_reference", "volume", "weight", "list.collection_reference", "list.color", "list.date", "list.date_time", "list.dimension", "list.file_reference", "list.number_integer", "list.number_decimal", "list.page_reference", "list.product_reference", "list.rating", "list.single_line_text_field", "list.url", "list.variant_reference", "list.volume", "list.weight"];
|
|
18
|
+
/** A union of all the supported `metafield.type`s */
|
|
19
|
+
export declare type MetafieldTypeTypes = typeof allMetafieldTypesArray[number];
|
|
20
|
+
/**
|
|
21
|
+
* A mapping of a Metafield's `type` to the TypeScript type that is returned from `metafieldParser()`
|
|
22
|
+
* For example, when using `metafieldParser()`, the type will be correctly returned when used like the following:
|
|
23
|
+
*
|
|
24
|
+
* ```
|
|
25
|
+
* const parsedMetafield = metafieldParser<ParsedMetafields['boolean']>(metafield);`
|
|
26
|
+
* ```
|
|
27
|
+
* `parsedMetafield.parsedValue`'s type is now `boolean`
|
|
28
|
+
*/
|
|
29
|
+
export declare type ParsedMetafields<ExtraTypeGeneric = void> = {
|
|
30
|
+
/** A Metafield that's been parsed, with a `parsedValue` of `boolean` */
|
|
31
|
+
boolean: Simplify<BooleanParsedMetafield>;
|
|
32
|
+
/** A Metafield that's been parsed, with a `parsedValue` of a `Collection` object (as defined by the Storefront API) */
|
|
33
|
+
collection_reference: Simplify<CollectionParsedRefMetafield>;
|
|
34
|
+
/** A Metafield that's been parsed, with a `parsedValue` of type `string` */
|
|
35
|
+
color: Simplify<ColorParsedMetafield>;
|
|
36
|
+
/** A Metafield that's been parsed, with a `parsedValue` of type `Date` */
|
|
37
|
+
date: Simplify<DatesParsedMetafield>;
|
|
38
|
+
/** A Metafield that's been parsed, with a `parsedValue` of type `Date` */
|
|
39
|
+
date_time: Simplify<DatesParsedMetafield>;
|
|
40
|
+
/** A Metafield that's been parsed, with a `parsedValue` of type `Measurement` */
|
|
41
|
+
dimension: Simplify<MeasurementParsedMetafield>;
|
|
42
|
+
/** A Metafield that's been parsed, with a `parsedValue` of a `GenericFile` object (as defined by the Storefront API) */
|
|
43
|
+
file_reference: Simplify<FileRefParsedMetafield>;
|
|
44
|
+
/**
|
|
45
|
+
* A Metafield that's been parsed, with a `parsedValue` of type `unknown`, unless you pass in the type as a generic. For example:
|
|
46
|
+
*
|
|
47
|
+
* ```
|
|
48
|
+
* ParsedMetafields<MyJsonType>['json']
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
json: Simplify<JsonParsedMetafield<ExtraTypeGeneric>>;
|
|
52
|
+
/** A Metafield that's been parsed, with a `parsedValue` of type `Money` */
|
|
53
|
+
money: Simplify<MoneyParsedMetafield>;
|
|
54
|
+
/** A Metafield that's been parsed, with a `parsedValue` of type `string` */
|
|
55
|
+
multi_line_text_field: Simplify<TextParsedMetafield>;
|
|
56
|
+
/** A Metafield that's been parsed, with a `parsedValue` of type `number` */
|
|
57
|
+
number_decimal: Simplify<NumberParsedMetafield>;
|
|
58
|
+
/** A Metafield that's been parsed, with a `parsedValue` of type `number` */
|
|
59
|
+
number_integer: Simplify<NumberParsedMetafield>;
|
|
60
|
+
/** A Metafield that's been parsed, with a `parsedValue` of a `Page` object (as defined by the Storefront API) */
|
|
61
|
+
page_reference: Simplify<PageParsedRefMetafield>;
|
|
62
|
+
/** A Metafield that's been parsed, with a `parsedValue` of a `Product` object (as defined by the Storefront API) */
|
|
63
|
+
product_reference: Simplify<ProductParsedRefMetafield>;
|
|
64
|
+
/** A Metafield that's been parsed, with a `parsedValue` of type `Rating` */
|
|
65
|
+
rating: Simplify<RatingParsedMetafield>;
|
|
66
|
+
/** A Metafield that's been parsed, with a `parsedValue` of type `string` */
|
|
67
|
+
single_line_text_field: Simplify<TextParsedMetafield>;
|
|
68
|
+
/** A Metafield that's been parsed, with a `parsedValue` of type `string` */
|
|
69
|
+
url: Simplify<TextParsedMetafield>;
|
|
70
|
+
/** A Metafield that's been parsed, with a `parsedValue` of a `ProductVariant` object (as defined by the Storefront API) */
|
|
71
|
+
variant_reference: Simplify<VariantParsedRefMetafield>;
|
|
72
|
+
/** A Metafield that's been parsed, with a `parsedValue` of type `Measurement` */
|
|
73
|
+
volume: Simplify<MeasurementParsedMetafield>;
|
|
74
|
+
/** A Metafield that's been parsed, with a `parsedValue` of type `Measurement` */
|
|
75
|
+
weight: Simplify<MeasurementParsedMetafield>;
|
|
76
|
+
/** A Metafield that's been parsed, with a `parsedValue` of an array of `Collection` objects (as defined by the Storefront API) */
|
|
77
|
+
'list.collection_reference': Simplify<CollectionListParsedRefMetafield>;
|
|
78
|
+
/** A Metafield that's been parsed, with a `parsedValue` of an array of strings */
|
|
79
|
+
'list.color': Simplify<ColorListParsedMetafield>;
|
|
80
|
+
/** A Metafield that's been parsed, with a `parsedValue` of an array of Date objects */
|
|
81
|
+
'list.date': Simplify<DatesListParsedMetafield>;
|
|
82
|
+
/** A Metafield that's been parsed, with a `parsedValue` of an array of Date objects */
|
|
83
|
+
'list.date_time': Simplify<DatesListParsedMetafield>;
|
|
84
|
+
/** A Metafield that's been parsed, with a `parsedValue` of an array of `Measurement` objects */
|
|
85
|
+
'list.dimension': Simplify<MeasurementListParsedMetafield>;
|
|
86
|
+
/** A Metafield that's been parsed, with a `parsedValue` of an array of `GenericFile` objects (as defined by the Storefront API) */
|
|
87
|
+
'list.file_reference': Simplify<FileListParsedRefMetafield>;
|
|
88
|
+
/** A Metafield that's been parsed, with a `parsedValue` of an array of numbers */
|
|
89
|
+
'list.number_integer': Simplify<NumberListParsedMetafield>;
|
|
90
|
+
/** A Metafield that's been parsed, with a `parsedValue` of an array of numbers */
|
|
91
|
+
'list.number_decimal': Simplify<NumberListParsedMetafield>;
|
|
92
|
+
/** A Metafield that's been parsed, with a `parsedValue` of an array of `Page` objects (as defined by the Storefront API) */
|
|
93
|
+
'list.page_reference': Simplify<PageListParsedRefMetafield>;
|
|
94
|
+
/** A Metafield that's been parsed, with a `parsedValue` of an array of `Product` objects (as defined by the Storefront API) */
|
|
95
|
+
'list.product_reference': Simplify<ProductListParsedRefMetafield>;
|
|
96
|
+
/** A Metafield that's been parsed, with a `parsedValue` of an array of `Rating`s */
|
|
97
|
+
'list.rating': Simplify<RatingListParsedMetafield>;
|
|
98
|
+
/** A Metafield that's been parsed, with a `parsedValue` of an array of strings */
|
|
99
|
+
'list.single_line_text_field': Simplify<TextListParsedMetafield>;
|
|
100
|
+
/** A Metafield that's been parsed, with a `parsedValue` of an array of strings */
|
|
101
|
+
'list.url': Simplify<TextListParsedMetafield>;
|
|
102
|
+
/** A Metafield that's been parsed, with a `parsedValue` of an array of `ProductVariant` objects (as defined by the Storefront API) */
|
|
103
|
+
'list.variant_reference': Simplify<VariantListParsedRefMetafield>;
|
|
104
|
+
/** A Metafield that's been parsed, with a `parsedValue` of an array of `Measurement`s */
|
|
105
|
+
'list.volume': Simplify<MeasurementListParsedMetafield>;
|
|
106
|
+
/** A Metafield that's been parsed, with a `parsedValue` of an array of `Measurement`s */
|
|
107
|
+
'list.weight': Simplify<MeasurementListParsedMetafield>;
|
|
108
|
+
};
|
|
109
|
+
interface ParsedBase extends MetafieldBaseType {
|
|
110
|
+
type: MetafieldTypeTypes;
|
|
111
|
+
parsedValue: unknown;
|
|
112
|
+
}
|
|
113
|
+
interface BooleanParsedMetafield extends ParsedBase {
|
|
114
|
+
type: 'boolean';
|
|
115
|
+
parsedValue: boolean | null;
|
|
116
|
+
}
|
|
117
|
+
declare type CollectionParsedRefMetafield = MetafieldBaseType & {
|
|
118
|
+
type: 'collection_reference';
|
|
119
|
+
parsedValue: Collection | null;
|
|
120
|
+
};
|
|
121
|
+
declare type ColorParsedMetafield = MetafieldBaseType & {
|
|
122
|
+
type: 'color';
|
|
123
|
+
parsedValue: string | null;
|
|
124
|
+
};
|
|
125
|
+
declare type DatesParsedMetafield = MetafieldBaseType & {
|
|
126
|
+
type: 'date' | 'date_time';
|
|
127
|
+
parsedValue: Date | null;
|
|
128
|
+
};
|
|
129
|
+
declare type MeasurementParsedMetafield = MetafieldBaseType & {
|
|
130
|
+
type: 'dimension' | 'weight' | 'volume';
|
|
131
|
+
parsedValue: Measurement | null;
|
|
132
|
+
};
|
|
133
|
+
declare type FileRefParsedMetafield = MetafieldBaseType & {
|
|
134
|
+
type: 'file_reference';
|
|
135
|
+
parsedValue: GenericFile | null;
|
|
136
|
+
};
|
|
137
|
+
declare type JsonParsedMetafield<JsonTypeGeneric = void> = MetafieldBaseType & {
|
|
138
|
+
type: 'json';
|
|
139
|
+
parsedValue: JsonTypeGeneric extends void ? unknown : JsonTypeGeneric | null;
|
|
140
|
+
};
|
|
141
|
+
declare type MoneyParsedMetafield = MetafieldBaseType & {
|
|
142
|
+
type: 'money';
|
|
143
|
+
parsedValue: MoneyV2 | null;
|
|
144
|
+
};
|
|
145
|
+
declare type TextParsedMetafield = MetafieldBaseType & {
|
|
146
|
+
type: 'single_line_text_field' | 'multi_line_text_field' | 'url';
|
|
147
|
+
parsedValue: string | null;
|
|
148
|
+
};
|
|
149
|
+
declare type NumberParsedMetafield = MetafieldBaseType & {
|
|
150
|
+
type: 'number_decimal' | 'number_integer';
|
|
151
|
+
parsedValue: number | null;
|
|
152
|
+
};
|
|
153
|
+
declare type PageParsedRefMetafield = MetafieldBaseType & {
|
|
154
|
+
type: 'page_reference';
|
|
155
|
+
parsedValue: Page | null;
|
|
156
|
+
};
|
|
157
|
+
declare type ProductParsedRefMetafield = MetafieldBaseType & {
|
|
158
|
+
type: 'product_reference';
|
|
159
|
+
parsedValue: Product | null;
|
|
160
|
+
};
|
|
161
|
+
declare type RatingParsedMetafield = MetafieldBaseType & {
|
|
162
|
+
type: 'rating';
|
|
163
|
+
parsedValue: Rating | null;
|
|
164
|
+
};
|
|
165
|
+
declare type VariantParsedRefMetafield = MetafieldBaseType & {
|
|
166
|
+
type: 'variant_reference';
|
|
167
|
+
parsedValue: ProductVariant | null;
|
|
168
|
+
};
|
|
169
|
+
declare type CollectionListParsedRefMetafield = MetafieldBaseType & {
|
|
170
|
+
type: 'list.collection_reference';
|
|
171
|
+
parsedValue: Array<Collection> | null;
|
|
172
|
+
};
|
|
173
|
+
declare type ColorListParsedMetafield = MetafieldBaseType & {
|
|
174
|
+
type: 'list.color';
|
|
175
|
+
parsedValue: Array<string> | null;
|
|
176
|
+
};
|
|
177
|
+
declare type DatesListParsedMetafield = MetafieldBaseType & {
|
|
178
|
+
type: 'list.date' | 'list.date_time';
|
|
179
|
+
parsedValue: Array<Date> | null;
|
|
180
|
+
};
|
|
181
|
+
declare type MeasurementListParsedMetafield = MetafieldBaseType & {
|
|
182
|
+
type: 'list.dimension' | 'list.weight' | 'list.volume';
|
|
183
|
+
parsedValue: Array<Measurement> | null;
|
|
184
|
+
};
|
|
185
|
+
declare type FileListParsedRefMetafield = MetafieldBaseType & {
|
|
186
|
+
type: 'list.file_reference';
|
|
187
|
+
parsedValue: Array<GenericFile> | null;
|
|
188
|
+
};
|
|
189
|
+
declare type TextListParsedMetafield = MetafieldBaseType & {
|
|
190
|
+
type: 'list.single_line_text_field' | 'list.url';
|
|
191
|
+
parsedValue: Array<string> | null;
|
|
192
|
+
};
|
|
193
|
+
declare type NumberListParsedMetafield = MetafieldBaseType & {
|
|
194
|
+
type: 'list.number_decimal' | 'list.number_integer';
|
|
195
|
+
parsedValue: Array<number> | null;
|
|
196
|
+
};
|
|
197
|
+
declare type PageListParsedRefMetafield = MetafieldBaseType & {
|
|
198
|
+
type: 'list.page_reference';
|
|
199
|
+
parsedValue: Array<Page> | null;
|
|
200
|
+
};
|
|
201
|
+
declare type ProductListParsedRefMetafield = MetafieldBaseType & {
|
|
202
|
+
type: 'list.product_reference';
|
|
203
|
+
parsedValue: Array<Product> | null;
|
|
204
|
+
};
|
|
205
|
+
declare type RatingListParsedMetafield = MetafieldBaseType & {
|
|
206
|
+
type: 'list.rating';
|
|
207
|
+
parsedValue: Array<Rating> | null;
|
|
208
|
+
};
|
|
209
|
+
declare type VariantListParsedRefMetafield = MetafieldBaseType & {
|
|
210
|
+
type: 'list.variant_reference';
|
|
211
|
+
parsedValue: Array<ProductVariant> | null;
|
|
212
|
+
};
|
|
213
|
+
export declare type Measurement = {
|
|
214
|
+
unit: string;
|
|
215
|
+
value: number;
|
|
216
|
+
};
|
|
217
|
+
export interface Rating {
|
|
218
|
+
value: number;
|
|
219
|
+
scale_min: number;
|
|
220
|
+
scale_max: number;
|
|
221
|
+
}
|
|
222
|
+
export {};
|
|
@@ -2548,6 +2548,9 @@ fragment ImageFragment on Image {
|
|
|
2548
2548
|
}, (_i = parsedMetafield.value) == null ? void 0 : _i.toString());
|
|
2549
2549
|
}
|
|
2550
2550
|
function parseMetafield(metafield) {
|
|
2551
|
+
{
|
|
2552
|
+
console.info(`'parseMetafield()' will have a breaking change in a future version; its behavior will match that of 'metafieldParser()'`);
|
|
2553
|
+
}
|
|
2551
2554
|
if (!metafield) {
|
|
2552
2555
|
{
|
|
2553
2556
|
console.warn(`'parseMetafield' was not passed any value for the 'metafield' argument`);
|
|
@@ -2563,6 +2566,9 @@ fragment ImageFragment on Image {
|
|
|
2563
2566
|
};
|
|
2564
2567
|
}
|
|
2565
2568
|
function parseMetafieldValue(metafield) {
|
|
2569
|
+
{
|
|
2570
|
+
console.info(`'parseMetafieldValue()' will be removed in a future version`);
|
|
2571
|
+
}
|
|
2566
2572
|
if (!metafield) {
|
|
2567
2573
|
return null;
|
|
2568
2574
|
}
|
|
@@ -2690,6 +2696,113 @@ fragment ImageFragment on Image {
|
|
|
2690
2696
|
throw new Error(`Unit not supported: ${unit}`);
|
|
2691
2697
|
}
|
|
2692
2698
|
}
|
|
2699
|
+
var dist = {};
|
|
2700
|
+
Object.defineProperty(dist, "__esModule", { value: true });
|
|
2701
|
+
dist.expectNever = expectType_1 = dist.expectType = void 0;
|
|
2702
|
+
const expectType = (value) => void 0;
|
|
2703
|
+
var expectType_1 = dist.expectType = expectType;
|
|
2704
|
+
const expectNever = (value) => {
|
|
2705
|
+
throw new TypeError("Unexpected value: " + value);
|
|
2706
|
+
};
|
|
2707
|
+
dist.expectNever = expectNever;
|
|
2708
|
+
function metafieldParser(metafield) {
|
|
2709
|
+
var _a, _b, _c, _d;
|
|
2710
|
+
if (!metafield.type) {
|
|
2711
|
+
const noTypeError = `metafieldParser(): The 'type' field is required in order to parse the Metafield.`;
|
|
2712
|
+
{
|
|
2713
|
+
throw new Error(noTypeError);
|
|
2714
|
+
}
|
|
2715
|
+
}
|
|
2716
|
+
switch (metafield.type) {
|
|
2717
|
+
case "boolean":
|
|
2718
|
+
return {
|
|
2719
|
+
...metafield,
|
|
2720
|
+
parsedValue: metafield.value === "true"
|
|
2721
|
+
};
|
|
2722
|
+
case "collection_reference":
|
|
2723
|
+
case "file_reference":
|
|
2724
|
+
case "page_reference":
|
|
2725
|
+
case "product_reference":
|
|
2726
|
+
case "variant_reference":
|
|
2727
|
+
return {
|
|
2728
|
+
...metafield,
|
|
2729
|
+
parsedValue: metafield.reference
|
|
2730
|
+
};
|
|
2731
|
+
case "color":
|
|
2732
|
+
case "multi_line_text_field":
|
|
2733
|
+
case "single_line_text_field":
|
|
2734
|
+
case "url":
|
|
2735
|
+
return {
|
|
2736
|
+
...metafield,
|
|
2737
|
+
parsedValue: metafield.value
|
|
2738
|
+
};
|
|
2739
|
+
case "dimension":
|
|
2740
|
+
case "money":
|
|
2741
|
+
case "json":
|
|
2742
|
+
case "rating":
|
|
2743
|
+
case "volume":
|
|
2744
|
+
case "weight":
|
|
2745
|
+
case "list.color":
|
|
2746
|
+
case "list.dimension":
|
|
2747
|
+
case "list.number_integer":
|
|
2748
|
+
case "list.number_decimal":
|
|
2749
|
+
case "list.rating":
|
|
2750
|
+
case "list.single_line_text_field":
|
|
2751
|
+
case "list.url":
|
|
2752
|
+
case "list.volume":
|
|
2753
|
+
case "list.weight": {
|
|
2754
|
+
let parsedValue = null;
|
|
2755
|
+
try {
|
|
2756
|
+
parsedValue = parseJSON((_a = metafield.value) != null ? _a : "");
|
|
2757
|
+
} catch (err) {
|
|
2758
|
+
const parseError = `metafieldParser(): attempted to JSON.parse the 'metafield.value' property, but failed.`;
|
|
2759
|
+
{
|
|
2760
|
+
throw new Error(parseError);
|
|
2761
|
+
}
|
|
2762
|
+
}
|
|
2763
|
+
return {
|
|
2764
|
+
...metafield,
|
|
2765
|
+
parsedValue
|
|
2766
|
+
};
|
|
2767
|
+
}
|
|
2768
|
+
case "date":
|
|
2769
|
+
case "date_time":
|
|
2770
|
+
return {
|
|
2771
|
+
...metafield,
|
|
2772
|
+
parsedValue: new Date((_b = metafield.value) != null ? _b : "")
|
|
2773
|
+
};
|
|
2774
|
+
case "list.date":
|
|
2775
|
+
case "list.date_time": {
|
|
2776
|
+
const jsonParseValue = parseJSON((_c = metafield == null ? void 0 : metafield.value) != null ? _c : "");
|
|
2777
|
+
return {
|
|
2778
|
+
...metafield,
|
|
2779
|
+
parsedValue: jsonParseValue.map((dateString) => new Date(dateString))
|
|
2780
|
+
};
|
|
2781
|
+
}
|
|
2782
|
+
case "number_decimal":
|
|
2783
|
+
case "number_integer":
|
|
2784
|
+
return {
|
|
2785
|
+
...metafield,
|
|
2786
|
+
parsedValue: Number(metafield.value)
|
|
2787
|
+
};
|
|
2788
|
+
case "list.collection_reference":
|
|
2789
|
+
case "list.file_reference":
|
|
2790
|
+
case "list.page_reference":
|
|
2791
|
+
case "list.product_reference":
|
|
2792
|
+
case "list.variant_reference":
|
|
2793
|
+
return {
|
|
2794
|
+
...metafield,
|
|
2795
|
+
parsedValue: flattenConnection((_d = metafield.references) != null ? _d : void 0)
|
|
2796
|
+
};
|
|
2797
|
+
default: {
|
|
2798
|
+
const typeNotFoundError = `metafieldParser(): the 'metafield.type' you passed in is not supported. Your type: "${metafield.type}". If you believe this is an error, please open an issue on GitHub.`;
|
|
2799
|
+
{
|
|
2800
|
+
throw new Error(typeNotFoundError);
|
|
2801
|
+
}
|
|
2802
|
+
}
|
|
2803
|
+
}
|
|
2804
|
+
}
|
|
2805
|
+
expectType_1(true);
|
|
2693
2806
|
function useMoney(money) {
|
|
2694
2807
|
const {
|
|
2695
2808
|
locale
|
|
@@ -3167,6 +3280,7 @@ fragment ImageFragment on Image {
|
|
|
3167
3280
|
exports2.Video = Video;
|
|
3168
3281
|
exports2.createStorefrontClient = createStorefrontClient;
|
|
3169
3282
|
exports2.flattenConnection = flattenConnection;
|
|
3283
|
+
exports2.metafieldParser = metafieldParser;
|
|
3170
3284
|
exports2.parseMetafield = parseMetafield;
|
|
3171
3285
|
exports2.parseMetafieldValue = parseMetafieldValue;
|
|
3172
3286
|
exports2.useCart = useCart;
|