Skip to content

Commit

Permalink
fix: Add some more docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
richtera committed Feb 6, 2024
1 parent 20bac42 commit 08b8a50
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions packages/data-provider-base/src/compatability.ts
Original file line number Diff line number Diff line change
@@ -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<typeof FormData> {
// Use the appropriate FormData implementation depending on the environment
return typeof FormData !== "undefined" || "browser" in process
Expand All @@ -10,19 +15,33 @@ export async function getFormData(): Promise<typeof FormData> {
)) 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<typeof fetch> {
// Use the browser's fetch if available, otherwise use node-fetch
return typeof fetch !== "undefined" || "browser" in process
? fetch
: ((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<typeof Blob> {
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<any> {
if (isNode) {
const { ReadStream } = await import("node:fs");
Expand Down
5 changes: 5 additions & 0 deletions packages/data-provider-base/src/formdata-base-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any> {
return wrapStream(data);
}
Expand Down
23 changes: 21 additions & 2 deletions packages/data-provider-ipfs-http-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ 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
*/
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(
Expand All @@ -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
Expand Down
29 changes: 29 additions & 0 deletions packages/data-provider-pinata/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand All @@ -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}`;
}
Expand Down

0 comments on commit 08b8a50

Please sign in to comment.