1+ import { prepareGatewayUrls } from "../common" ;
12import {
23 extractObjectFiles ,
34 replaceObjectFilesWithUris ,
45 replaceObjectGatewayUrlsWithSchemes ,
56 replaceObjectSchemesWithGatewayUrls ,
7+ replaceSchemeWithGatewayUrl ,
68} from "../common/utils" ;
79import {
810 FileOrBuffer ,
911 FileOrBufferOrStringArraySchema ,
12+ GatewayUrls ,
1013 IpfsUploadBatchOptions ,
1114 IStorageDownloader ,
1215 IStorageUploader ,
@@ -28,24 +31,52 @@ import { IpfsUploader } from "./uploaders/ipfs-uploader";
2831 * const uri = await storage.upload(data);
2932 * const result = await storage.download(uri);
3033 *
31- * // Or configure a custom uploader and downloader
34+ * // Or configure a custom uploader, downloader, and gateway URLs
35+ * const gatewayUrls = {
36+ * // We define a mapping of schemes to gateway URLs
37+ * "ipfs://": [
38+ * "https://gateway.ipfscdn.io/ipfs/",
39+ * "https://cloudflare-ipfs.com/ipfs/",
40+ * "https://ipfs.io/ipfs/",
41+ * ],
42+ * };
3243 * const downloader = new StorageDownloader();
3344 * const uploader = new IpfsUploader();
34- * const storage = new ThirdwebStorage(uploader, downloader)
45+ * const storage = new ThirdwebStorage(uploader, downloader, gatewayUrls )
3546 * ```
3647 *
3748 * @public
3849 */
3950export class ThirdwebStorage < T extends UploadOptions = IpfsUploadBatchOptions > {
4051 private uploader : IStorageUploader < T > ;
4152 private downloader : IStorageDownloader ;
53+ public gatewayUrls : GatewayUrls ;
4254
4355 constructor (
4456 uploader : IStorageUploader < T > = new IpfsUploader ( ) ,
4557 downloader : IStorageDownloader = new StorageDownloader ( ) ,
58+ gatewayUrls ?: GatewayUrls ,
4659 ) {
4760 this . uploader = uploader ;
4861 this . downloader = downloader ;
62+ this . gatewayUrls = prepareGatewayUrls ( gatewayUrls ) ;
63+ }
64+
65+ /**
66+ * Resolve any scheme on a URL to get a retrievable URL for the data
67+ *
68+ * @param url - The URL to resolve the scheme of
69+ * @returns The URL with its scheme resolved
70+ *
71+ * @example
72+ * ```jsx
73+ * const uri = "ipfs://example";
74+ * const url = storage.resolveScheme(uri);
75+ * console.log(url);
76+ * ```
77+ */
78+ resolveScheme ( url : string ) : string {
79+ return replaceSchemeWithGatewayUrl ( url , this . gatewayUrls ) ;
4980 }
5081
5182 /**
@@ -61,7 +92,7 @@ export class ThirdwebStorage<T extends UploadOptions = IpfsUploadBatchOptions> {
6192 * ```
6293 */
6394 async download ( url : string ) : Promise < Response > {
64- return this . downloader . download ( url ) ;
95+ return this . downloader . download ( url , this . gatewayUrls ) ;
6596 }
6697
6798 /**
@@ -73,7 +104,7 @@ export class ThirdwebStorage<T extends UploadOptions = IpfsUploadBatchOptions> {
73104 *
74105 * @example
75106 * ```jsx
76- * const uri = "ipfs://example"
107+ * const uri = "ipfs://example";
77108 * const json = await storage.downloadJSON(uri);
78109 * ```
79110 */
@@ -82,10 +113,7 @@ export class ThirdwebStorage<T extends UploadOptions = IpfsUploadBatchOptions> {
82113
83114 // If we get a JSON object, recursively replace any schemes with gatewayUrls
84115 const json = await res . json ( ) ;
85- return replaceObjectSchemesWithGatewayUrls (
86- json ,
87- this . downloader . gatewayUrls ,
88- ) as TJSON ;
116+ return replaceObjectSchemesWithGatewayUrls ( json , this . gatewayUrls ) as TJSON ;
89117 }
90118
91119 /**
@@ -175,19 +203,19 @@ export class ThirdwebStorage<T extends UploadOptions = IpfsUploadBatchOptions> {
175203 ) : Promise < Json [ ] > {
176204 let cleaned = data ;
177205 // TODO: Gateway URLs should probably be top-level since both uploader and downloader need them
178- if ( this . uploader . gatewayUrls || this . downloader . gatewayUrls ) {
206+ if ( this . gatewayUrls ) {
179207 // Replace any gateway URLs with their hashes
180208 cleaned = replaceObjectGatewayUrlsWithSchemes (
181209 cleaned ,
182- this . uploader . gatewayUrls || this . downloader . gatewayUrls ,
210+ this . gatewayUrls ,
183211 ) as Json [ ] ;
184212
185213 if ( options ?. uploadWithGatewayUrl || this . uploader . uploadWithGatewayUrl ) {
186214 // If flag is set, replace all schemes with their preferred gateway URL
187215 // Ex: used for Solana, where services don't resolve schemes for you, so URLs must be useable by default
188216 cleaned = replaceObjectSchemesWithGatewayUrls (
189217 cleaned ,
190- this . uploader . gatewayUrls || this . downloader . gatewayUrls ,
218+ this . gatewayUrls ,
191219 ) as Json [ ] ;
192220 }
193221 }
0 commit comments