Skip to content

Commit 5f01906

Browse files
committed
Add onlyPresets setting
1 parent df3940d commit 5f01906

File tree

6 files changed

+44
-7
lines changed

6 files changed

+44
-7
lines changed

src/methods/generateImageInfoUrl.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,16 @@ describe("generateImageInfoUrl", () => {
8686
"/avg:t:f/do:t/dc:t:t/exp:1729409825/iptc:t/p:6/pr:test:test2/s:t/plain/https://example.com/host/pic.png"
8787
);
8888
});
89+
90+
it("should work with `onlyPresets` setting", () => {
91+
expect(
92+
generateUrl(
93+
{ value: "https://example.com/host/pic.png", type: "plain" },
94+
{
95+
preset: ["test", "test2"],
96+
},
97+
{ onlyPresets: true }
98+
)
99+
).toEqual("/test:test2/plain/https://example.com/host/pic.png");
100+
});
89101
});

src/methods/generateImageInfoUrl.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { OptionsImageInfo } from "../typesImageInfo";
22
import * as optionModules from "../optionsImageInfo";
33
import { guardIsUndef, guardIsValidVal } from "../utils";
4+
import { Settings } from "../settings";
45

56
const correctUrlTypes = {
67
plain: true,
@@ -15,7 +16,8 @@ export type URLImageInfo = {
1516

1617
const generateImageInfoUrl = (
1718
url: URLImageInfo,
18-
options?: OptionsImageInfo
19+
options?: OptionsImageInfo,
20+
settings?: Settings
1921
): string => {
2022
guardIsUndef(url.value, "url.value", "Must be a string");
2123
guardIsUndef(
@@ -30,7 +32,7 @@ const generateImageInfoUrl = (
3032
for (const [, optionModule] of Object.entries(optionModules)) {
3133
if (optionModule.test(options)) {
3234
optsPart += "/";
33-
optsPart += optionModule.build(options);
35+
optsPart += optionModule.build(options, settings);
3436
}
3537
}
3638
}

src/methods/generateUrl.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,19 @@ import { Options } from "../types";
22
import * as optionModules from "../options";
33
import { guardIsUndef, guardIsValidVal } from "../utils";
44
import type { URLImageInfo } from "./generateImageInfoUrl";
5+
import { Settings } from "../settings";
56

67
const correctUrlTypes = {
78
plain: true,
89
base64: true,
910
encrypted: true,
1011
};
1112

12-
const generateUrl = (url: URLImageInfo, options?: Options): string => {
13+
const generateUrl = (
14+
url: URLImageInfo,
15+
options?: Options,
16+
settings?: Settings
17+
): string => {
1318
guardIsUndef(url.value, "url.value", "Must be a string");
1419
guardIsUndef(
1520
url.type,
@@ -23,7 +28,7 @@ const generateUrl = (url: URLImageInfo, options?: Options): string => {
2328
for (const [, optionModule] of Object.entries(optionModules)) {
2429
if (optionModule.test(options)) {
2530
optsPart += "/";
26-
optsPart += optionModule.build(options);
31+
optsPart += optionModule.build(options, settings);
2732
}
2833
}
2934
}

src/optionsShared/preset.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Settings } from "../settings";
12
import { Preset, PresetOptionsPartial } from "../typesShared/preset";
23
import { guardIsUndef, guardIsNotArray } from "../utils";
34

@@ -7,8 +8,8 @@ const getOpt = (options: PresetOptionsPartial): Preset | undefined =>
78
const test = (options: PresetOptionsPartial): boolean =>
89
Boolean(getOpt(options));
910

10-
const build = (options: PresetOptionsPartial): string => {
11-
const preset = getOpt(options);
11+
const build = (options: PresetOptionsPartial, settings?: Settings): string => {
12+
let preset = getOpt(options);
1213

1314
guardIsUndef(preset, "preset");
1415
guardIsNotArray(preset, "preset");
@@ -17,7 +18,11 @@ const build = (options: PresetOptionsPartial): string => {
1718
throw new Error("preset option should contain only strings");
1819
}
1920

20-
return `pr:${preset.join(":")}`;
21+
if (!settings?.onlyPresets) {
22+
preset = ["pr", ...preset];
23+
}
24+
25+
return `${preset.join(":")}`;
2126
};
2227

2328
export { test, build };

src/settings.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export type Settings = {
2+
/**
3+
* Setting `onlyPresets` to true switches imgproxy into presets-only mode. In this mode, imgproxy accepts a presets list as processing options.
4+
* @see https://docs.imgproxy.net/usage/presets#only-presets
5+
*/
6+
onlyPresets?: boolean;
7+
};

tests/optionsShared/preset.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,11 @@ describe("preset", () => {
4646
it("should return 'pr:paddings:jpg' if pr option is ['paddings', 'jpg']", () => {
4747
expect(build({ pr: ["paddings", "jpg"] })).toEqual("pr:paddings:jpg");
4848
});
49+
50+
it("should work with `onlyPresets` setting ", () => {
51+
expect(build({ pr: ["paddings", "jpg"] }, { onlyPresets: true })).toEqual(
52+
"paddings:jpg"
53+
);
54+
});
4955
});
5056
});

0 commit comments

Comments
 (0)