-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adding kontent.ai CDN * Update README.md * Fix tests * Fix tests + fix fit for kontent.ai * Change scale to crop as default fit --------- Co-authored-by: Thomas Ledoux <thomas_ledoux@epam.com> Co-authored-by: Matt Kane <m@mk.gg>
- Loading branch information
1 parent
b360429
commit 7475b58
Showing
7 changed files
with
115 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -106,6 +106,7 @@ is not auto-detected. | |
- Bunny.net | ||
- Storyblok | ||
- Cloudflare | ||
- Kontent.ai | ||
|
||
## FAQs | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters