-
Notifications
You must be signed in to change notification settings - Fork 295
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Save progress on a new metafield parser * Fix type issue, and get some generics passed in * Fix tests and add additional TS docs to the types * handle no 'type' field, and prep for list metafields * Save progress on metafield parser and tests * Finished the tests and docs * Deprecate parseMetafield; add changelog; export functions * fix bad variable name * Update packages/react/src/metafield-parser.test.ts Co-authored-by: Daniel Rios <ieldanr@gmail.com> * Clarify wording around naming and deprecation * remove todo Co-authored-by: Daniel Rios <ieldanr@gmail.com>
- Loading branch information
1 parent
3eab955
commit 1ccbd1c
Showing
8 changed files
with
1,056 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
--- | ||
'@shopify/hydrogen-react': patch | ||
--- | ||
|
||
Introducing the new `metafieldParser()` function and `ParsedMetafield` type. | ||
|
||
## `metafieldParser()` | ||
|
||
`metafieldParser()` is a temporary name; it will be renamed to `parseMetafield()` in a future release. | ||
|
||
The `metafieldParser()` function is an improvement and enhancement upon the existing `parseMetafield()` and `parseMetafieldValue()` functions. `metafieldParser()` now supports all Metafield types as outlined in the [Storefront API](https://shopify.dev/apps/metafields/types) documentation, including the list types! | ||
|
||
The parsed value can be found on the newly-added `parsedValue` property of the returned object from `metafieldParser()`. For example: | ||
|
||
```js | ||
const parsed = metafieldParser(metafield); | ||
|
||
console.log(parsed.parsedValue); | ||
``` | ||
|
||
`parseMetafieldValue()` has been marked as deprecated and will be removed in a future version of Hydrogen-UI. | ||
|
||
## The `ParsedMetafield` type | ||
|
||
For TypeScript developers, we also introduce the new `ParsedMetafield` type to help improve your experience. The `ParsedMetafield` type is an object in which the keys map to the type that will be returned from `metafieldParser()`. For example: | ||
|
||
```ts | ||
ParsedMetafield['boolean']; | ||
// or | ||
ParsedMetafield['list.collection']; | ||
``` | ||
|
||
When used in conjunction with `metafieldParser()`, it will help TypeScript to understand what the returned object's `parsedValue` type is: | ||
|
||
```ts | ||
const parsed = metafieldParser<ParsedMetafield['boolean']>(booleanMetafield) | ||
|
||
// type of `parsedValue` is `boolean | null` | ||
if(parsed.parsedValue) { | ||
... | ||
} | ||
``` | ||
|
||
or | ||
|
||
```ts | ||
const parsed = metafieldParser<ParsedMetafield['list.collection']>( | ||
listCollectionMetafield | ||
); | ||
|
||
// type of `parsedValue` is `Array<Collection> | null` | ||
parsed.parsedValue?.map((collection) => { | ||
console.log(collection?.name); | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.