|
1 |
| -import { joinURL, hasProtocol } from 'ufo' |
2 |
| -import { createOperationsGenerator } from '../utils/index' |
3 |
| -import { defineProvider } from '../utils/provider' |
| 1 | +import { joinURL, hasProtocol } from "ufo"; |
| 2 | +import { createOperationsGenerator } from "../utils/index"; |
| 3 | +import { defineProvider } from "../utils/provider"; |
4 | 4 |
|
5 | 5 | const operationsGenerator = createOperationsGenerator({
|
6 | 6 | keyMap: {
|
7 |
| - fit: 'func', |
8 |
| - quality: 'q', |
9 |
| - format: 'force_format', |
| 7 | + fit: "func", |
| 8 | + quality: "q", |
| 9 | + format: "force_format", |
10 | 10 | },
|
11 | 11 | valueMap: {
|
12 | 12 | fit: {
|
13 |
| - cover: 'crop', |
14 |
| - contain: 'fit', |
15 |
| - fill: 'cover', |
16 |
| - inside: 'bound', |
17 |
| - outside: 'boundmin', |
| 13 | + cover: "crop", |
| 14 | + contain: "fit", |
| 15 | + fill: "cover", |
| 16 | + inside: "bound", |
| 17 | + outside: "boundmin", |
18 | 18 | },
|
19 | 19 | },
|
20 |
| -}) |
| 20 | +}); |
21 | 21 |
|
22 | 22 | interface CloudimageOptions {
|
23 |
| - baseURL: string |
24 |
| - token: string |
25 |
| - apiVersion?: string |
26 |
| - cdnURL?: string |
| 23 | + token: string; |
| 24 | + apiVersion?: string; |
| 25 | + baseURL?: string; |
| 26 | + cdnURL?: string; |
27 | 27 | }
|
28 | 28 |
|
29 | 29 | // https://docs.cloudimage.io/go/cloudimage-documentation-v7/en/introduction
|
30 | 30 | export default defineProvider<CloudimageOptions>({
|
31 |
| - getImage: (src, { |
32 |
| - modifiers, |
33 |
| - baseURL, |
34 |
| - token = '', |
35 |
| - apiVersion = '', |
36 |
| - cdnURL = '', |
37 |
| - }) => { |
38 |
| - const operations = operationsGenerator(modifiers) |
39 |
| - const query = (operations ? ('?' + operations) : '') |
| 31 | + getImage: ( |
| 32 | + src, |
| 33 | + { modifiers, baseURL, token = "", apiVersion = "", cdnURL = "" } |
| 34 | + ) => { |
| 35 | + const operations = operationsGenerator(modifiers); |
| 36 | + const query = operations ? "?" + operations : ""; |
40 | 37 |
|
41 | 38 | if (import.meta.dev) {
|
42 |
| - const warning = [] |
43 |
| - |
44 |
| - if (!baseURL) { |
45 |
| - warning.push('<baseURL>') |
46 |
| - } |
| 39 | + const warning = []; |
47 | 40 |
|
48 | 41 | if (!token && !cdnURL) {
|
49 |
| - warning.push('<token> or <cdnURL>') |
| 42 | + warning.push("<token> or <cdnURL>"); |
50 | 43 | }
|
51 | 44 |
|
52 | 45 | if (warning.length > 0) {
|
53 |
| - console.warn(`[cloudimage] ${warning.join(', ')} is required to build image URL`) |
| 46 | + console.warn( |
| 47 | + `[cloudimage] ${warning.join(", ")} is required to build image URL` |
| 48 | + ); |
54 | 49 | return {
|
55 |
| - url: joinURL('<token>', '<baseURL>', src) + query, |
56 |
| - } |
| 50 | + url: joinURL("<token>", "<baseURL>", src) + query, |
| 51 | + }; |
57 | 52 | }
|
58 | 53 | }
|
59 | 54 |
|
60 | 55 | if (!cdnURL) {
|
61 |
| - cdnURL = `https://${token}.cloudimg.io/${apiVersion}` |
| 56 | + cdnURL = `https://${token}.cloudimg.io/${apiVersion}`; |
62 | 57 | }
|
63 | 58 |
|
64 |
| - if (hasProtocol(src)) { |
| 59 | + if (hasProtocol(src) || !baseURL) { |
65 | 60 | return {
|
66 |
| - url: joinURL(src) + query, |
67 |
| - } |
| 61 | + url: joinURL(cdnURL, src) + query, |
| 62 | + }; |
68 | 63 | }
|
69 | 64 |
|
70 | 65 | return {
|
71 | 66 | url: joinURL(cdnURL, baseURL, src) + query,
|
72 |
| - } |
| 67 | + }; |
73 | 68 | },
|
74 |
| -}) |
| 69 | +}); |
0 commit comments