From e30f7935d5b3c2daaa67c6852545ccbb2f5ff7a5 Mon Sep 17 00:00:00 2001 From: Neil Campbell Date: Mon, 1 Jan 2024 14:55:23 +0800 Subject: [PATCH] feat: add support for updating metadata on pinata pin --- src/ipfs.ts | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/ipfs.ts b/src/ipfs.ts index 69d2826..0814172 100644 --- a/src/ipfs.ts +++ b/src/ipfs.ts @@ -145,7 +145,12 @@ export class CacheOnlyIPFS implements IPFS { } } -export type PinataUploadResponse = { IpfsHash: string } +type PinataUploadResponse = { IpfsHash: string } + +type PinataMetadata = { + name?: string + [key: string]: string | undefined +} export class PinataStorageWithCache implements IPFS { private cache: ObjectCache @@ -267,4 +272,27 @@ export class PinataStorageWithCache implements IPFS { return cid.toString() } } + + async updateMetadata(cid: string, metadata: PinataMetadata): Promise { + const { name, ...keyvalues } = metadata + const response = await fetch(`${this.pinataBaseUrl}/hashMetadata`, { + method: 'PUT', + headers: { + 'Content-Type': 'application/json', + Accept: 'application/json', + Authorization: `Bearer ${this.token}`, + }, + body: JSON.stringify({ + ipfsPinHash: cid, + ...(name ? { name } : undefined), + ...(keyvalues && Object.keys(keyvalues).length > 0 ? { keyvalues } : undefined), + }), + }) + + if (!response.ok) { + const statusCode = response.status + const errorResponse = await response.text() + throw new Error(`${statusCode} ${errorResponse}`) + } + } }