-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathtransform.ts
More file actions
104 lines (100 loc) · 3.47 KB
/
transform.ts
File metadata and controls
104 lines (100 loc) · 3.47 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { getImageCdnForUrl } from "./detect.ts";
import { transform as contentful } from "./transformers/contentful.ts";
import { transform as builderio } from "./transformers/builder.io.ts";
import { transform as imgix } from "./transformers/imgix.ts";
import { transform as shopify } from "./transformers/shopify.ts";
import { transform as wordpress } from "./transformers/wordpress.ts";
import { transform as cloudimage } from "./transformers/cloudimage.ts";
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/kontent.ai.ts";
import { transform as vercel } from "./transformers/vercel.ts";
import { transform as nextjs } from "./transformers/nextjs.ts";
import { transform as scene7 } from "./transformers/scene7.ts";
import { transform as keycdn } from "./transformers/keycdn.ts";
import { transform as directus } from "./transformers/directus.ts";
import { transform as imageengine } from "./transformers/imageengine.ts";
import { transform as contentstack } from "./transformers/contentstack.ts";
import { transform as cloudflareImages } from "./transformers/cloudflare_images.ts";
import { transform as ipx } from "./transformers/ipx.ts";
import { transform as astro } from "./transformers/astro.ts";
import { transform as netlify } from "./transformers/netlify.ts";
import { transform as imagekit } from "./transformers/imagekit.ts";
import { transform as uploadcare } from "./transformers/uploadcare.ts";
import { transform as supabase } from "./transformers/supabase.ts";
import { transform as hygraph } from "./transformers/hygraph.ts";
import { transform as basehub } from "./transformers/basehub.ts";
import { ImageCdn, UrlTransformer } from "./types.ts";
import { getCanonicalCdnForUrl } from "./canonical.ts";
export const getTransformer = (cdn: ImageCdn) => ({
imgix,
contentful,
"builder.io": builderio,
shopify,
wordpress,
cloudimage,
cloudinary,
bunny,
storyblok,
cloudflare,
vercel,
nextjs,
scene7,
"kontent.ai": kontentai,
keycdn,
directus,
imageengine,
contentstack,
"cloudflare_images": cloudflareImages,
ipx,
astro,
netlify,
imagekit,
uploadcare,
supabase,
hygraph,
basehub,
}[cdn]);
/**
* Returns a transformer function if the given CDN is supported
*/
export const getTransformerForCdn = (
cdn: ImageCdn | false | undefined,
): UrlTransformer | undefined => {
if (!cdn) {
return undefined;
}
return getTransformer(cdn);
};
/**
* Transforms an image URL to a new URL with the given options.
* If the URL is not from a known image CDN it returns undefined.
*/
export const transformUrl: UrlTransformer = (options) => {
const cdn = options?.cdn ?? getImageCdnForUrl(options.url);
// Default to recursive
if (!(options.recursive ?? true)) {
return getTransformerForCdn(cdn)?.(options);
}
const canonical = getCanonicalCdnForUrl(
options.url,
cdn,
);
if (!canonical || !canonical.cdn) {
return undefined;
}
return getTransformer(canonical.cdn)?.({
...options,
url: canonical.url,
});
};
/**
* Returns a transformer function if the given URL is from a known image CDN
*
* @deprecated Use `getCanonicalCdnForUrl` and `getTransformerForCdn` instead
*/
export const getTransformerForUrl = (
url: string | URL,
): UrlTransformer | undefined => getTransformerForCdn(getImageCdnForUrl(url));