Skip to content

Commit a976f89

Browse files
authored
feat(astro): support custom endpoint (#148)
1 parent 15d04d4 commit a976f89

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

src/transformers/astro.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,20 @@ Deno.test("astro", async (t) => {
4848
"/_image?href=https%3A%2F%2Fimages.ctfassets.net%2Faaaa%2Fxxxx%2Fyyyy%2Fhow-to-wow-a-customer.jpg&w=200&h=100",
4949
);
5050
});
51+
52+
await t.step("should format a URL with custom endpoint", () => {
53+
const result = transform({
54+
url: img,
55+
width: 200,
56+
height: 100,
57+
cdnOptions: { astro: { endpoint: "/_image/" } },
58+
});
59+
assertEquals(
60+
result?.toString(),
61+
"/_image/?href=https%3A%2F%2Fimages.ctfassets.net%2Faaaa%2Fxxxx%2Fyyyy%2Fhow-to-wow-a-customer.jpg&w=200&h=100",
62+
);
63+
});
64+
5165
await t.step("should not set height if not provided", () => {
5266
const result = transform({ url: img, width: 200 });
5367
assertEquals(

src/transformers/astro.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export const parse: UrlParser<AstroParams> = (url) => {
5858
};
5959

6060
export const transform: UrlTransformer = (
61-
{ url: originalUrl, width, height, format },
61+
{ url: originalUrl, width, height, format, cdnOptions },
6262
) => {
6363
const parsedUrl = toUrl(originalUrl);
6464
const href = toCanonicalUrlString(
@@ -71,5 +71,7 @@ export const transform: UrlTransformer = (
7171
setParamIfDefined(url, "h", height, true, true);
7272
setParamIfDefined(url, "f", format);
7373

74-
return `/_image?${url.searchParams.toString()}`;
74+
const endpoint = cdnOptions?.astro?.endpoint ?? "/_image";
75+
76+
return `${endpoint}?${url.searchParams.toString()}`;
7577
};

0 commit comments

Comments
 (0)