From e4e9333c597b5e31308d093e1336ff1804231739 Mon Sep 17 00:00:00 2001 From: Steve Date: Mon, 23 Sep 2024 09:33:14 -0400 Subject: [PATCH] feat: Added optional param of gateway to createSignedURL --- src/core/gateway/createSignedURL.ts | 13 ++++++++++++- src/core/types.ts | 1 + tests/gateway/createSignedURL.test.ts | 22 ++++++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/core/gateway/createSignedURL.ts b/src/core/gateway/createSignedURL.ts index 634e272..478bda9 100644 --- a/src/core/gateway/createSignedURL.ts +++ b/src/core/gateway/createSignedURL.ts @@ -19,7 +19,17 @@ export const createSignedURL = async ( throw new ValidationError("Pinata configuration is missing"); } - let newUrl: string = `${config?.pinataGateway}/files/${options.cid}`; + let baseUrl: string | undefined; + + if (options?.gateway) { + baseUrl = options.gateway.startsWith("https://") + ? options.gateway + : `https://${options.gateway}`; + } else { + baseUrl = config.pinataGateway; + } + + let newUrl: string = `${baseUrl}/files/${options.cid}`; const params = new URLSearchParams(); @@ -41,6 +51,7 @@ export const createSignedURL = async ( } const queryString = params.toString(); + if (queryString) { newUrl += `?${queryString}`; } diff --git a/src/core/types.ts b/src/core/types.ts index e9b31ef..44d770e 100644 --- a/src/core/types.ts +++ b/src/core/types.ts @@ -162,6 +162,7 @@ export type SignedUrlOptions = { cid: string; date?: number; expires: number; + gateway?: string; }; export type GatewayAnalyticsQuery = { diff --git a/tests/gateway/createSignedURL.test.ts b/tests/gateway/createSignedURL.test.ts index 5e39757..dd0d065 100644 --- a/tests/gateway/createSignedURL.test.ts +++ b/tests/gateway/createSignedURL.test.ts @@ -169,4 +169,26 @@ describe("createSignedURL function", () => { }), ); }); + + it("should use custom gateway if provided in options", async () => { + const customGatewayOptions = { + ...mockOptions, + gateway: "https://custom.gateway.com", + }; + global.fetch = jest.fn().mockResolvedValueOnce({ + ok: true, + json: () => Promise.resolve({ data: "signed_url" }), + }); + + await createSignedURL(mockConfig, customGatewayOptions, mockImageOpts); + + expect(global.fetch).toHaveBeenCalledWith( + expect.any(String), + expect.objectContaining({ + body: expect.stringContaining( + '"url":"https://custom.gateway.com/files/QmTest...?img-width=100&img-height=100&img-dpr=2&img-fit=contain&img-gravity=auto&img-quality=80&img-format=webp&img-anim=true&img-sharpen=3&img-onerror=redirect&img-metadata=keep"', + ), + }), + ); + }); });