Skip to content

Commit

Permalink
feat: add support for updating metadata on pinata pin (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
neilcampbell authored Jan 1, 2024
1 parent 48a3771 commit 561af6b
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/ipfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -267,4 +272,27 @@ export class PinataStorageWithCache implements IPFS {
return cid.toString()
}
}

async updateMetadata(cid: string, metadata: PinataMetadata): Promise<void> {
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}`)
}
}
}

0 comments on commit 561af6b

Please sign in to comment.