Enhance StorageClient upload method to support optional pinning

- Updated the upload method to accept an options parameter for controlling the pinning behavior of uploaded content.
- Default pinning behavior remains true, but can be set to false through the options.
- Improved documentation to reflect the new options parameter and its usage in examples.
This commit is contained in:
anonpenguin23 2025-11-08 08:34:29 +02:00
parent 06d58fe85b
commit a02c7474ab

View File

@ -35,11 +35,13 @@ export class StorageClient {
} }
/** /**
* Upload content to IPFS and pin it. * Upload content to IPFS and optionally pin it.
* Supports both File objects (browser) and Buffer/ReadableStream (Node.js). * Supports both File objects (browser) and Buffer/ReadableStream (Node.js).
* *
* @param file - File to upload (File, Blob, or Buffer) * @param file - File to upload (File, Blob, or Buffer)
* @param name - Optional filename * @param name - Optional filename
* @param options - Optional upload options
* @param options.pin - Whether to pin the content (default: true). Pinning happens asynchronously on the backend.
* @returns Upload result with CID * @returns Upload result with CID
* *
* @example * @example
@ -53,12 +55,15 @@ export class StorageClient {
* // Node.js * // Node.js
* const fs = require('fs'); * const fs = require('fs');
* const fileBuffer = fs.readFileSync('image.jpg'); * const fileBuffer = fs.readFileSync('image.jpg');
* const result = await client.storage.upload(fileBuffer, 'image.jpg'); * const result = await client.storage.upload(fileBuffer, 'image.jpg', { pin: true });
* ``` * ```
*/ */
async upload( async upload(
file: File | Blob | ArrayBuffer | Uint8Array | ReadableStream<Uint8Array>, file: File | Blob | ArrayBuffer | Uint8Array | ReadableStream<Uint8Array>,
name?: string name?: string,
options?: {
pin?: boolean;
}
): Promise<StorageUploadResponse> { ): Promise<StorageUploadResponse> {
// Create FormData for multipart upload // Create FormData for multipart upload
const formData = new FormData(); const formData = new FormData();
@ -101,6 +106,10 @@ export class StorageClient {
); );
} }
// Add pin flag (default: true)
const shouldPin = options?.pin !== false; // Default to true
formData.append("pin", shouldPin ? "true" : "false");
return this.httpClient.uploadFile<StorageUploadResponse>( return this.httpClient.uploadFile<StorageUploadResponse>(
"/v1/storage/upload", "/v1/storage/upload",
formData, formData,