Skip to content

Commit 7a7fba6

Browse files
authored
feat(astro): support cropping (#150)
* feat(astro): support cropping * Chore: fix test
1 parent 6f9639c commit 7a7fba6

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

src/transformers/astro.test.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Deno.test("astro", async (t) => {
4545
});
4646
assertEquals(
4747
result?.toString(),
48-
"/_image?href=https%3A%2F%2Fimages.ctfassets.net%2Faaaa%2Fxxxx%2Fyyyy%2Fhow-to-wow-a-customer.jpg&w=200&h=100",
48+
"/_image?href=https%3A%2F%2Fimages.ctfassets.net%2Faaaa%2Fxxxx%2Fyyyy%2Fhow-to-wow-a-customer.jpg&w=200&h=100&fit=cover",
4949
);
5050
});
5151

@@ -58,24 +58,24 @@ Deno.test("astro", async (t) => {
5858
});
5959
assertEquals(
6060
result?.toString(),
61-
"/_image/?href=https%3A%2F%2Fimages.ctfassets.net%2Faaaa%2Fxxxx%2Fyyyy%2Fhow-to-wow-a-customer.jpg&w=200&h=100",
61+
"/_image/?href=https%3A%2F%2Fimages.ctfassets.net%2Faaaa%2Fxxxx%2Fyyyy%2Fhow-to-wow-a-customer.jpg&w=200&h=100&fit=cover",
6262
);
6363
});
6464

6565
await t.step("should not set height if not provided", () => {
6666
const result = transform({ url: img, width: 200 });
6767
assertEquals(
6868
result?.toString(),
69-
"/_image?href=https%3A%2F%2Fimages.ctfassets.net%2Faaaa%2Fxxxx%2Fyyyy%2Fhow-to-wow-a-customer.jpg&w=200",
69+
"/_image?href=https%3A%2F%2Fimages.ctfassets.net%2Faaaa%2Fxxxx%2Fyyyy%2Fhow-to-wow-a-customer.jpg&w=200&fit=cover",
7070
);
7171
});
7272
await t.step("should delete height if not set", () => {
7373
const url = new URL(img);
74-
url.searchParams.set("h", "100");
74+
url.searchParams.set("h", "100&fit=cover");
7575
const result = transform({ url, width: 200 });
7676
assertEquals(
7777
result?.toString(),
78-
"/_image?href=https%3A%2F%2Fimages.ctfassets.net%2Faaaa%2Fxxxx%2Fyyyy%2Fhow-to-wow-a-customer.jpg&w=200",
78+
"/_image?href=https%3A%2F%2Fimages.ctfassets.net%2Faaaa%2Fxxxx%2Fyyyy%2Fhow-to-wow-a-customer.jpg&w=200&fit=cover",
7979
);
8080
});
8181

@@ -87,7 +87,7 @@ Deno.test("astro", async (t) => {
8787
});
8888
assertEquals(
8989
result?.toString(),
90-
"/_image?href=https%3A%2F%2Fimages.ctfassets.net%2Faaaa%2Fxxxx%2Fyyyy%2Fhow-to-wow-a-customer.jpg&w=201&h=100",
90+
"/_image?href=https%3A%2F%2Fimages.ctfassets.net%2Faaaa%2Fxxxx%2Fyyyy%2Fhow-to-wow-a-customer.jpg&w=201&h=100&fit=cover",
9191
);
9292
});
9393

@@ -100,7 +100,7 @@ Deno.test("astro", async (t) => {
100100
});
101101
assertEquals(
102102
result?.toString(),
103-
"/_image?href=%2Fstatic%2Fmoose.png&w=100&h=200&f=webp",
103+
"/_image?href=%2Fstatic%2Fmoose.png&w=100&h=200&f=webp&fit=cover",
104104
);
105105
});
106106
});

src/transformers/astro.ts

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { ShouldDelegateUrl, UrlParser, UrlTransformer } from "../types.ts";
22
import {
33
getNumericParam,
44
setParamIfDefined,
5+
setParamIfUndefined,
56
toCanonicalUrlString,
67
toUrl,
78
} from "../utils.ts";
@@ -70,6 +71,7 @@ export const transform: UrlTransformer = (
7071
setParamIfDefined(url, "w", width, true, true);
7172
setParamIfDefined(url, "h", height, true, true);
7273
setParamIfDefined(url, "f", format);
74+
setParamIfUndefined(url, "fit", "cover");
7375

7476
const endpoint = cdnOptions?.astro?.endpoint ?? "/_image";
7577

0 commit comments

Comments
 (0)