Skip to content

Commit c77916b

Browse files
committed
feat(provider/cloudimage): make baseURL optional and route absolute src via CDN
1 parent 61587e6 commit c77916b

File tree

2 files changed

+42
-45
lines changed

2 files changed

+42
-45
lines changed

docs/content/3.providers/cloudimage.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ links:
1010

1111
Integration between [Cloudimage](https://www.cloudimage.io/en/home) and the image module.
1212

13-
To use this provider you need to specify your Cloudimage `token` and the `baseURL` of your image storage.
13+
To use this provider you need to specify either your Cloudimage `token` (with optional `apiVersion`) or a full `cdnURL`. The `baseURL` of your image storage is optional and is only used when the passed `src` is not an absolute URL.
1414

1515
```ts [nuxt.config.ts]
1616
export default defineNuxtConfig({
1717
image: {
1818
cloudimage: {
1919
token: 'your_cloudimage_token',
20-
baseURL: 'origin_image_url' // or alias
20+
baseURL: 'origin_image_url' // or alias (optional)
2121
}
2222
}
2323
})
@@ -33,9 +33,9 @@ Your Cloudimage customer token. [Register](https://www.cloudimage.io/en/register
3333

3434
### `baseURL`
3535

36-
- Type: **String** (required)
36+
- Type: **String** (optional)
3737

38-
Your origin image URL or storage alias that allows to shorten your origin image URLs.
38+
Your origin image URL or storage alias that allows to shorten your origin image URLs. If not provided, the `src` will be joined directly with the computed `cdnURL`.
3939

4040
```ts [nuxt.config.ts]
4141
export default defineNuxtConfig({
@@ -93,7 +93,7 @@ export default defineNuxtConfig({
9393
- Type: **String**
9494
- Default: `https://{token}.cloudimg.io/{apiVersion}`
9595

96-
Replaces the dynamically built URL
96+
Replaces the dynamically built URL. Useful when you prefer not to provide a `token` or need a custom CDN hostname.
9797

9898
```ts [nuxt.config.ts]
9999
export default defineNuxtConfig({
@@ -105,6 +105,8 @@ export default defineNuxtConfig({
105105
})
106106
```
107107

108+
When only `cdnURL` is provided and `baseURL` is omitted, relative sources are resolved directly against the CDN URL, for example `src: '/test.png'` becomes `https://demo.cloudimg.io/v7/test.png`.
109+
108110
## Cloudimage Modifiers
109111

110112
Beside the [standard modifiers](/usage/nuxt-img#modifiers), also you can pass Cloudimage-specific [Cloudimage-specific transformation](https://docs.cloudimage.io/go/cloudimage-documentation-v7/en/introduction) params to `modifiers` prop.
Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,69 @@
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";
44

55
const operationsGenerator = createOperationsGenerator({
66
keyMap: {
7-
fit: 'func',
8-
quality: 'q',
9-
format: 'force_format',
7+
fit: "func",
8+
quality: "q",
9+
format: "force_format",
1010
},
1111
valueMap: {
1212
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",
1818
},
1919
},
20-
})
20+
});
2121

2222
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;
2727
}
2828

2929
// https://docs.cloudimage.io/go/cloudimage-documentation-v7/en/introduction
3030
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 : "";
4037

4138
if (import.meta.dev) {
42-
const warning = []
43-
44-
if (!baseURL) {
45-
warning.push('<baseURL>')
46-
}
39+
const warning = [];
4740

4841
if (!token && !cdnURL) {
49-
warning.push('<token> or <cdnURL>')
42+
warning.push("<token> or <cdnURL>");
5043
}
5144

5245
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+
);
5449
return {
55-
url: joinURL('<token>', '<baseURL>', src) + query,
56-
}
50+
url: joinURL("<token>", "<baseURL>", src) + query,
51+
};
5752
}
5853
}
5954

6055
if (!cdnURL) {
61-
cdnURL = `https://${token}.cloudimg.io/${apiVersion}`
56+
cdnURL = `https://${token}.cloudimg.io/${apiVersion}`;
6257
}
6358

64-
if (hasProtocol(src)) {
59+
if (hasProtocol(src) || !baseURL) {
6560
return {
66-
url: joinURL(src) + query,
67-
}
61+
url: joinURL(cdnURL, src) + query,
62+
};
6863
}
6964

7065
return {
7166
url: joinURL(cdnURL, baseURL, src) + query,
72-
}
67+
};
7368
},
74-
})
69+
});

0 commit comments

Comments
 (0)