diff --git a/packages/data-provider-base/src/authenticated-formdata-provider.ts b/packages/data-provider-base/src/authenticated-formdata-provider.ts index 2f280b5..39c54dc 100644 --- a/packages/data-provider-base/src/authenticated-formdata-provider.ts +++ b/packages/data-provider-base/src/authenticated-formdata-provider.ts @@ -31,6 +31,7 @@ import { */ export class AuthenticatedFormDataUploader extends BaseFormDataUploader { /** + * Return the request options used for the fetch call. * * @param dataContent - FormData content to be sent (ignored in this case) * @param meta - Optional additional meta data (ignored in this case) @@ -60,6 +61,7 @@ export class AuthenticatedFormDataUploader extends BaseFormDataUploader { }; } /** + * Extract the IPFS URL from the upload result. * * @param result - JSON object returned from FormData post. * @returns URL referring to the uploaded data diff --git a/packages/data-provider-base/src/compatability.ts b/packages/data-provider-base/src/compatability.ts index 5f1d634..fe4589e 100644 --- a/packages/data-provider-base/src/compatability.ts +++ b/packages/data-provider-base/src/compatability.ts @@ -1,6 +1,11 @@ // @ts-expect-error - check for browser const isNode = typeof window === "undefined"; +/** + * Return the FormData implementation in a way that works in node and browser + * + * @returns FormData implementation + */ export async function getFormData(): Promise { // Use the appropriate FormData implementation depending on the environment return typeof FormData !== "undefined" || "browser" in process @@ -10,6 +15,11 @@ export async function getFormData(): Promise { )) as typeof FormData); } +/** + * Return the fetch implementation in a way that works in node and browser + * + * @returns The fetch implementation + */ export async function getFetch(): Promise { // Use the browser's fetch if available, otherwise use node-fetch return typeof fetch !== "undefined" || "browser" in process @@ -17,12 +27,21 @@ export async function getFetch(): Promise { : ((await import("node-fetch")) as unknown as typeof fetch); } +/** + * Return the blob implementation in a way that works in node and browser + * + * @returns The Blob implementation + */ export async function getBlob(): Promise { return typeof Blob !== "undefined" || "browser" in process ? Blob : ((await import("formdata-node").then(({ Blob }) => Blob)) as typeof Blob); } +/** + * Wrap a stream so that a readstream can be detected in node without + * loading the module in the browser. + */ export async function wrapStream(data: any): Promise { if (isNode) { const { ReadStream } = await import("node:fs"); diff --git a/packages/data-provider-base/src/formdata-base-provider.ts b/packages/data-provider-base/src/formdata-base-provider.ts index b1be49d..ee157f1 100644 --- a/packages/data-provider-base/src/formdata-base-provider.ts +++ b/packages/data-provider-base/src/formdata-base-provider.ts @@ -88,6 +88,11 @@ export class BaseFormDataUploader { return meta; } + /** + * Wrap ReadStream in a Blob for node if required. + * @param data - data to wrap + * @returns + */ protected async wrapStream(data: any): Promise { return wrapStream(data); } diff --git a/packages/data-provider-ipfs-http-client/src/index.ts b/packages/data-provider-ipfs-http-client/src/index.ts index aa2de5e..5bf7b57 100644 --- a/packages/data-provider-ipfs-http-client/src/index.ts +++ b/packages/data-provider-ipfs-http-client/src/index.ts @@ -5,8 +5,8 @@ import { } from "@lukso/data-provider-base"; /** - * Data provider for uploading using the ipfs-http-client - * directly. This is backward compatible with the previous ipfs implementation, + * Data provider for uploading compatible with the ipfs-http-client + * directly. This is backward compatible with the most ipfs pinning APIs, * but ipfs-http-client has been deprecated in favor of Helia see https://github.com/ipfs/js-ipfs/issues/4336 * @public */ @@ -14,6 +14,7 @@ export class IPFSHttpClientUploader extends BaseFormDataUploader { /** * Construct a provider using the ipfs-http-client * @param gateway - accepts a string, URL or options compatible for the create method + * @param options - additional options for the fetch call * @public */ constructor( @@ -23,14 +24,32 @@ export class IPFSHttpClientUploader extends BaseFormDataUploader { super(); } + /** + * Get configured endpoint + * + * @returns Inject the endpoint this uploader is configued to use. + */ getEndpoint(): string { return this.gateway; } + /** + * Resolve the URL during uploading + * + * @param result - extract the ipfs URL from the POST result + * @returns + */ resolveUrl(result: any): string { return `ipfs://${result.Hash}`; } + /** + * Add additional arguments needed as part of the POST fetch request. + * + * @param dataContent + * @param meta + * @returns The request options for the fetch call. + */ async getRequestOptions( dataContent: FormData, meta?: FormDataPostHeaders diff --git a/packages/data-provider-pinata/src/index.ts b/packages/data-provider-pinata/src/index.ts index 2111764..05b2786 100644 --- a/packages/data-provider-pinata/src/index.ts +++ b/packages/data-provider-pinata/src/index.ts @@ -105,6 +105,14 @@ export class PinataUploader extends BaseFormDataUploader { constructor(private pinataConfig: PinataConfig) { super(); } + + /** + * Extract fetch request options. + * + * @param _dataContent - FormData content to be sent + * @param meta - Metadata from File or Blob object + * @returns fetch request options + */ async getRequestOptions( // eslint-disable-next-line @typescript-eslint/no-unused-vars _dataContent: FormData, @@ -117,6 +125,14 @@ export class PinataUploader extends BaseFormDataUploader { ); return { ...root, ...rest, headers: { ...headers } }; } + + /** + * Add additional pinata specific form item if piniataMetadata is provider. + * Most of the time this is not necessary and not supported by other uploaders. + * + * @param dataContent - FormData content to be send + * @param meta - Metadata from File or Blob object + */ async addMetadata(dataContent: FormData, meta?: FormDataPostHeaders) { if (meta?.pinataMetadata) { validateMetadata(meta); @@ -126,9 +142,22 @@ export class PinataUploader extends BaseFormDataUploader { ); } } + + /** + * Return standard pinata pinning endpoint supported for all JWT and API keys. + * + * @returns Return the endpoint to be used for pinata + */ getEndpoint(): string { return "https://api.pinata.cloud/pinning/pinFileToIPFS"; } + + /** + * Decode IPFS URL from POST results. + * + * @param result - JSON result from upload + * @returns ipfs URL + */ resolveUrl(result: any): string { return `ipfs://${result.IpfsHash}`; }