diff --git a/src/core/gateway/createSignedURL.ts b/src/core/gateway/createSignedURL.ts index 769cd67..634e272 100644 --- a/src/core/gateway/createSignedURL.ts +++ b/src/core/gateway/createSignedURL.ts @@ -13,6 +13,7 @@ import { export const createSignedURL = async ( config: PinataConfig | undefined, options: SignedUrlOptions, + imgOpts: OptimizeImageOptions, ): Promise => { if (!config) { throw new ValidationError("Pinata configuration is missing"); @@ -20,6 +21,30 @@ export const createSignedURL = async ( let newUrl: string = `${config?.pinataGateway}/files/${options.cid}`; + const params = new URLSearchParams(); + + if (imgOpts) { + if (imgOpts.width) params.append("img-width", imgOpts.width.toString()); + if (imgOpts.height) params.append("img-height", imgOpts.height.toString()); + if (imgOpts.dpr) params.append("img-dpr", imgOpts.dpr.toString()); + if (imgOpts.fit) params.append("img-fit", imgOpts.fit); + if (imgOpts.gravity) params.append("img-gravity", imgOpts.gravity); + if (imgOpts.quality) + params.append("img-quality", imgOpts.quality.toString()); + if (imgOpts.format) params.append("img-format", imgOpts.format); + if (imgOpts.animation !== undefined) + params.append("img-anim", imgOpts.animation.toString()); + if (imgOpts.sharpen) + params.append("img-sharpen", imgOpts.sharpen.toString()); + if (imgOpts.onError === true) params.append("img-onerror", "redirect"); + if (imgOpts.metadata) params.append("img-metadata", imgOpts.metadata); + } + + const queryString = params.toString(); + if (queryString) { + newUrl += `?${queryString}`; + } + const date = options?.date || Math.floor(new Date().getTime() / 1000); const payload = JSON.stringify({ @@ -35,33 +60,6 @@ export const createSignedURL = async ( endpoint = config.endpointUrl; } - // const params = new URLSearchParams(); - // if (config?.pinataGatewayKey) { - // params.append("pinataGatewayToken", config.pinataGatewayKey); - // } - - // if (options) { - // if (options.width) params.append("img-width", options.width.toString()); - // if (options.height) params.append("img-height", options.height.toString()); - // if (options.dpr) params.append("img-dpr", options.dpr.toString()); - // if (options.fit) params.append("img-fit", options.fit); - // if (options.gravity) params.append("img-gravity", options.gravity); - // if (options.quality) - // params.append("img-quality", options.quality.toString()); - // if (options.format) params.append("img-format", options.format); - // if (options.animation !== undefined) - // params.append("img-anim", options.animation.toString()); - // if (options.sharpen) - // params.append("img-sharpen", options.sharpen.toString()); - // if (options.onError === true) params.append("img-onerror", "redirect"); - // if (options.metadata) params.append("img-metadata", options.metadata); - // } - - // const queryString = params.toString(); - // if (queryString) { - // newUrl += `?${queryString}`; - // } - try { const request = await fetch(`${endpoint}/files/sign`, { method: "POST", diff --git a/src/core/gateway/getCid.ts b/src/core/gateway/getCid.ts index deb15cb..6c6307c 100644 --- a/src/core/gateway/getCid.ts +++ b/src/core/gateway/getCid.ts @@ -77,6 +77,8 @@ export const getCid = async ( const date = Math.floor(new Date().getTime() / 1000); + console.log(newUrl); + const payload = JSON.stringify({ url: newUrl, date: date, @@ -84,20 +86,25 @@ export const getCid = async ( method: "GET", }); - const signedUrlRequest = await fetch( - "https://api.pinata.cloud/v3/files/sign", - { - method: "POST", - headers: { - "Content-Type": "application/json", - Authorization: `Bearer ${config?.pinataJwt}`, - }, - body: payload, + let endpoint: string = "https://api.pinata.cloud/v3"; + + if (config.endpointUrl) { + endpoint = config.endpointUrl; + } + + const signedUrlRequest = await fetch(`${endpoint}/files/sign`, { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${config?.pinataJwt}`, }, - ); + body: payload, + }); const signedUrl = await signedUrlRequest.json(); + console.log(signedUrl); + // if (config?.pinataGatewayKey) { // params.append("pinataGatewayToken", config.pinataGatewayKey); // } diff --git a/src/core/pinataSDK.ts b/src/core/pinataSDK.ts index ba1872f..8f42915 100644 --- a/src/core/pinataSDK.ts +++ b/src/core/pinataSDK.ts @@ -375,12 +375,12 @@ class Gateways { this.config = newConfig; } - get(cid: string): OptimizeImage { - return new OptimizeImage(this.config, cid); + get(cid: string): OptimizeImageGetCid { + return new OptimizeImageGetCid(this.config, cid); } - createSignedURL(options: SignedUrlOptions): Promise { - return createSignedURL(this.config, options); + createSignedURL(options: SignedUrlOptions): OptimizeImageCreateSignedURL { + return new OptimizeImageCreateSignedURL(this.config, options); } // get(cid: string): OptimizeImage { @@ -446,7 +446,7 @@ class Gateways { // } } -class OptimizeImage { +class OptimizeImageGetCid { private config: PinataConfig | undefined; private cid: string; private options: OptimizeImageOptions = {}; @@ -456,7 +456,7 @@ class OptimizeImage { this.cid = cid; } - optimizeImage(options: OptimizeImageOptions): OptimizeImage { + optimizeImage(options: OptimizeImageOptions): OptimizeImageGetCid { this.options = { ...this.options, ...options }; return this; } @@ -466,6 +466,28 @@ class OptimizeImage { } } +class OptimizeImageCreateSignedURL { + private config: PinataConfig | undefined; + private urlOpts: SignedUrlOptions; + private imgOpts: OptimizeImageOptions = {}; + + constructor(config: PinataConfig | undefined, urlOpts: SignedUrlOptions) { + this.config = config; + this.urlOpts = urlOpts; + } + + optimizeImage(options: OptimizeImageOptions): OptimizeImageCreateSignedURL { + this.imgOpts = { ...this.imgOpts, ...options }; + return this; + } + + then(onfulfilled?: ((value: string) => any) | null): Promise { + return createSignedURL(this.config, this.urlOpts, this.imgOpts).then( + onfulfilled, + ); + } +} + // class Usage { // config: PinataConfig | undefined; diff --git a/src/core/types.ts b/src/core/types.ts index d25a7f5..0a98d8c 100644 --- a/src/core/types.ts +++ b/src/core/types.ts @@ -162,7 +162,6 @@ export type SignedUrlOptions = { cid: string; date?: number; expires: number; - imageOpt?: OptimizeImageOptions; }; export type GatewayAnalyticsQuery = {