-
-
Notifications
You must be signed in to change notification settings - Fork 187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: add updateNft fct #4008
fix: add updateNft fct #4008
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi great work here! I had a few suggestions for improving typing.
// lib.es2020.promise.d.ts does not export its types so we're using a simple type. | ||
const success = nftMetadataResults.filter( | ||
(promise) => promise.status === 'fulfilled', | ||
) as { status: 'fulfilled'; value: NftUpdate }[]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This type assertion is avoidable by using a type guard as the predicate for the filter operation.
This allows the compiler to narrow down to PromiseFulfilledResult
at the type level.
// lib.es2020.promise.d.ts does not export its types so we're using a simple type. | |
const success = nftMetadataResults.filter( | |
(promise) => promise.status === 'fulfilled', | |
) as { status: 'fulfilled'; value: NftUpdate }[]; | |
const success = nftMetadataResults.filter( | |
(result): result is PromiseFulfilledResult<NftUpdate> => | |
result.status === 'fulfilled', | |
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's nice thnx! 🙏
// TODO: Replace `any` with type | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
.stub(nftController, 'getNftInformation' as any) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any
is unnecessary here since the compiler tells us exactly what the as
assertion target type should be
Argument of type '"getNftInformation"' is not assignable to parameter of type 'keyof NftController'.ts(2345)
// TODO: Replace `any` with type | |
// eslint-disable-next-line @typescript-eslint/no-explicit-any | |
.stub(nftController, 'getNftInformation' as any) | |
.stub(nftController, 'getNftInformation' as keyof typeof nftController) |
In general, shared libraries is trying to avoid introducing new any
types except under a few very limited circumstances. An as
assertion even to an approximate type is always preferred.
userAddress = this.config.selectedAddress, | ||
) { | ||
const chainId = this.getCorrectChainId({ networkClientId }); | ||
const nftsWithChecksumAdr = nfts.map((nft: Nft) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Type annotations should be avoided unless they narrow an inferred type. There's an entry in the new TypeScript guidelines that covers this topic.
const nftsWithChecksumAdr = nfts.map((nft: Nft) => { | |
const nftsWithChecksumAdr = nfts.map((nft) => { |
// lib.es2020.promise.d.ts does not export its types so we're using a simple type. | ||
const success = nftMetadataResults.filter( | ||
(promise) => promise.status === 'fulfilled', | ||
) as { status: 'fulfilled'; value: NftUpdate }[]; | ||
|
||
if (success.length !== 0) { | ||
success.map((elm) => | ||
this.updateNft( | ||
elm.value.nft, | ||
elm.value.newMetadata, | ||
userAddress, | ||
chainId, | ||
), | ||
); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Follow-up from #4008 (comment))
Nit: This could be refactored to be more concise.
I also prefer forEach here instead of map to prevent confusion, since the return value is not being used and the updateNft
operation relies on side effects.
// lib.es2020.promise.d.ts does not export its types so we're using a simple type. | |
const success = nftMetadataResults.filter( | |
(promise) => promise.status === 'fulfilled', | |
) as { status: 'fulfilled'; value: NftUpdate }[]; | |
if (success.length !== 0) { | |
success.map((elm) => | |
this.updateNft( | |
elm.value.nft, | |
elm.value.newMetadata, | |
userAddress, | |
chainId, | |
), | |
); | |
} | |
nftMetadataResults | |
.filter( | |
(result): result is PromiseFulfilledResult<NftUpdate> => | |
result.status === 'fulfilled', | |
) | |
.forEach((elm) => | |
this.updateNft( | |
elm.value.nft, | |
elm.value.newMetadata, | |
userAddress, | |
chainId, | |
), | |
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
e583a68
to
4476447
Compare
This is the release candidate for 128.0.0. It adds a new function on nftController to update the nft metadata. #4008
Explanation
When a user imports an NFT when the display NFT media toggle is off and IPFS toggle is off, then enables the toggles, the image of the NFT will not be displayed.
This PR adds a new fct that fetches the NFT metadata and udpates the state. This fct will be called from extension based on the toggles and if the name/description/image attributes are null
References
Changelog
@metamask/assets-controllers
Checklist