forked from ascorbic/unpic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.ts
More file actions
67 lines (62 loc) · 2.12 KB
/
Copy pathparse.ts
File metadata and controls
67 lines (62 loc) · 2.12 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
import { getImageCdnForUrl } from "./detect.ts";
import { parse as contentful } from "./transformers/contentful.ts";
import { parse as builder } from "./transformers/builder.ts";
import { parse as imgix } from "./transformers/imgix.ts";
import { parse as shopify } from "./transformers/shopify.ts";
import { parse as wordpress } from "./transformers/wordpress.ts";
import { parse as cloudinary } from "./transformers/cloudinary.ts";
import { parse as cloudflare } from "./transformers/cloudflare.ts";
import { parse as bunny } from "./transformers/bunny.ts";
import { parse as storyblok } from "./transformers/storyblok.ts";
import { ImageCdn, ParsedUrl, SupportedImageCdn, UrlParser } from "./types.ts";
export const parsers = {
imgix,
contentful,
"builder.io": builder,
shopify,
wordpress,
cloudinary,
cloudflare,
bunny,
storyblok,
};
export const cdnIsSupportedForParse = (
cdn: ImageCdn | false,
): cdn is SupportedImageCdn => cdn && cdn in parsers;
/**
* Returns a parser function if the given URL is from a known image CDN
* @param url
*/
export const getParserForUrl = <TParams extends Record<string, unknown>>(
url: string | URL,
): UrlParser<TParams> | undefined =>
getParserForCdn<TParams>(getImageCdnForUrl(url));
export const getParserForCdn = <TParams extends Record<string, unknown>>(
cdn: ImageCdn | false | undefined,
): UrlParser<TParams> | undefined => {
if (!cdn || !cdnIsSupportedForParse(cdn)) {
return undefined;
}
return parsers[cdn] as UrlParser<TParams>;
};
/**
* Parses an image URL into its components.
* If the URL is not from a known image CDN it returns undefined.
* @param url
*/
export const parseUrl = <TParams = Record<string, unknown>>(
url: string | URL,
cdn?: ImageCdn,
): ParsedUrl<TParams> | undefined => {
if (cdn) {
return getParserForCdn(cdn)?.(url) as ParsedUrl<TParams>;
}
const detectedCdn = getImageCdnForUrl(url);
if (!detectedCdn) {
return undefined;
}
if (!cdnIsSupportedForParse(detectedCdn)) {
return { cdn: detectedCdn, base: url.toString() } as ParsedUrl<TParams>;
}
return getParserForCdn(detectedCdn)?.(url) as ParsedUrl<TParams>;
};