-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathbasehub.ts
More file actions
42 lines (36 loc) · 1.03 KB
/
basehub.ts
File metadata and controls
42 lines (36 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { UrlParser, UrlTransformer } from "../types.ts";
import {
getNumericParam,
setParamIfDefined,
setParamIfUndefined,
toUrl,
} from "../utils.ts";
export const parse: UrlParser<{ fit?: string }> = (url) => {
const parsedUrl = toUrl(url);
const fit = parsedUrl.searchParams.get("fit") || "cover";
const width = getNumericParam(parsedUrl, "w");
const height = getNumericParam(parsedUrl, "h");
const quality = getNumericParam(parsedUrl, "q");
const format = parsedUrl.searchParams.get("format") || undefined;
parsedUrl.search = "";
return {
width,
height,
format,
base: parsedUrl.toString(),
params: { fit, quality },
cdn: "basehub",
};
};
export const transform: UrlTransformer = (
{ url: originalUrl, width, height, format },
) => {
const url = toUrl(originalUrl);
setParamIfDefined(url, "w", width, true, true);
setParamIfDefined(url, "h", height, true, true);
setParamIfDefined(url, "format", format === "jpg" ? "jpeg" : format);
if (width && height) {
setParamIfUndefined(url, "fit", "cover");
}
return url;
};