Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add kontent.ai CDN #19

Merged
merged 7 commits into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ is not auto-detected.
- Bunny.net
- Storyblok
- Cloudflare
- Kontent.ai

## FAQs

Expand Down
3 changes: 2 additions & 1 deletion data/subdomains.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"imgix.net": "imgix",
"files.wordpress.com": "wordpress",
"b-cdn.net": "bunny",
"storyblok.com": "storyblok"
"storyblok.com": "storyblok",
"kc-usercontent.com": "kontent.ai"
}
4 changes: 4 additions & 0 deletions demo/src/examples.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,9 @@
[
"Cloudflare",
"https://assets.brevity.io/cdn-cgi/image/background=red,width=128,height=128,f=auto/uploads/generic/avatar-sample.jpeg"
],
[
"Kontent.ai",
"https://assets-us-01.kc-usercontent.com/b744f382-bfc7-434d-93e7-a65d51249bc7/cc0afdc7-23d7-4fde-be2c-f58ad54d2934/daylight.jpg"
]
]
2 changes: 2 additions & 0 deletions src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { transform as cloudinary } from "./transformers/cloudinary.ts";
import { transform as cloudflare } from "./transformers/cloudflare.ts";
import { transform as bunny } from "./transformers/bunny.ts";
import { transform as storyblok } from "./transformers/storyblok.ts";
import { transform as kontentai } from "./transformers/kontentai.ts";
import { ImageCdn, SupportedImageCdn, UrlTransformer } from "./types.ts";

export const transformers = {
Expand All @@ -20,6 +21,7 @@ export const transformers = {
bunny,
storyblok,
cloudflare,
"kontent.ai": kontentai
};

export const cdnIsSupportedForTransform = (
Expand Down
65 changes: 65 additions & 0 deletions src/transformers/kontentai.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { assertEquals } from "https://deno.land/std@0.172.0/testing/asserts.ts";

import { transform } from "./kontentai.ts";

const img =
"https://assets-us-01.kc-usercontent.com/b744f382-bfc7-434d-93e7-a65d51249bc7/cc0afdc7-23d7-4fde-be2c-f58ad54d2934/daylight.jpg"

Deno.test("kontent.ai", async (t) => {
await t.step("should format a URL", () => {
const result = transform({
url: img,
width: 200,
height: 100,
format: 'webp'
});
assertEquals(
result?.toString(),
"https://assets-us-01.kc-usercontent.com/b744f382-bfc7-434d-93e7-a65d51249bc7/cc0afdc7-23d7-4fde-be2c-f58ad54d2934/daylight.jpg?w=200&h=100&fm=webp&fit=crop",
);
});
await t.step("should not set height if not provided", () => {
const result = transform({ url: img, width: 200 });
assertEquals(
result?.toString(),
"https://assets-us-01.kc-usercontent.com/b744f382-bfc7-434d-93e7-a65d51249bc7/cc0afdc7-23d7-4fde-be2c-f58ad54d2934/daylight.jpg?w=200&fit=crop",
);
});

await t.step("should round non-integer params", () => {
const result = transform({
url: img,
width: 200.6,
height: 100.2,
});
assertEquals(
result?.toString(),
"https://assets-us-01.kc-usercontent.com/b744f382-bfc7-434d-93e7-a65d51249bc7/cc0afdc7-23d7-4fde-be2c-f58ad54d2934/daylight.jpg?w=201&h=100&fit=crop",
);
});

await t.step("should add fit=scale when height or width (or both) provided and no other fit setting", () => {
const result = transform({
url: img,
width: 200,
height: 100,
});
assertEquals(
result?.toString(),
"https://assets-us-01.kc-usercontent.com/b744f382-bfc7-434d-93e7-a65d51249bc7/cc0afdc7-23d7-4fde-be2c-f58ad54d2934/daylight.jpg?w=200&h=100&fit=crop",
);
});
await t.step("should not set fit=scale if another value exists", () => {
const url = new URL(img);
url.searchParams.set("fit", "scale");
const result = transform({
url: url,
width: 200,
height: 100
});
assertEquals(
result?.toString(),
"https://assets-us-01.kc-usercontent.com/b744f382-bfc7-434d-93e7-a65d51249bc7/cc0afdc7-23d7-4fde-be2c-f58ad54d2934/daylight.jpg?fit=scale&w=200&h=100",
);
});
});
39 changes: 39 additions & 0 deletions src/transformers/kontentai.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { UrlParser, UrlTransformer } from "../types.ts";
import {
getNumericParam,
setParamIfDefined,
setParamIfUndefined
} from "../utils.ts";

export const parse: UrlParser<{ fit?: string }> = (url) => {
const parsedUrl = new URL(url);

const fit = parsedUrl.searchParams.get("fit") || undefined;
const width = getNumericParam(parsedUrl, "w");
const height = getNumericParam(parsedUrl, "h");
const quality = getNumericParam(parsedUrl, "q");
const format = parsedUrl.searchParams.get("fm") || undefined;

parsedUrl.search = "";
return {
width,
height,
format,
base: parsedUrl.toString(),
params: { fit, quality },
cdn: "kontent.ai",
};
};

export const transform: UrlTransformer = (
{ url: originalUrl, width, height, format },
) => {
const url = new URL(originalUrl);
setParamIfDefined(url, "w", width, true, true);
setParamIfDefined(url, "h", height, true, true);
setParamIfDefined(url, "fm", format, true);
if (width || height) {
setParamIfUndefined(url, "fit", "crop");
}
return url;
};
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export type ImageCdn =
| "shopify"
| "wordpress"
| "bunny"
| "storyblok";
| "storyblok"
| "kontent.ai";

export type SupportedImageCdn = ImageCdn;