Skip to content

Commit

Permalink
Merge pull request #36 from PinataCloud/feat/add-signedurl-gateway-param
Browse files Browse the repository at this point in the history
feat: Added optional param of gateway to createSignedURL
  • Loading branch information
stevedylandev authored Sep 23, 2024
2 parents 9e88b76 + e4e9333 commit d2dc887
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/core/gateway/createSignedURL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -41,6 +51,7 @@ export const createSignedURL = async (
}

const queryString = params.toString();

if (queryString) {
newUrl += `?${queryString}`;
}
Expand Down
1 change: 1 addition & 0 deletions src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ export type SignedUrlOptions = {
cid: string;
date?: number;
expires: number;
gateway?: string;
};

export type GatewayAnalyticsQuery = {
Expand Down
22 changes: 22 additions & 0 deletions tests/gateway/createSignedURL.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"',
),
}),
);
});
});

0 comments on commit d2dc887

Please sign in to comment.