Skip to content
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

feat: add support for updating metadata on pinata pin #6

Merged
merged 1 commit into from
Jan 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}`)
}
}
}