Skip to content

Commit 6fc690b

Browse files
authored
Add max_result_dimension processing option (#57)
* Add support for max_result_dimension option * Changesets
1 parent ee21907 commit 6fc690b

File tree

6 files changed

+115
-0
lines changed

6 files changed

+115
-0
lines changed

.changeset/fresh-bushes-lick.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@imgproxy/imgproxy-js-core": minor
3+
---
4+
5+
Add support for [max_result_dimension](https://docs.imgproxy.net/latest/usage/processing#max-result-dimension) option

src/options/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export * as keepCopyright from "./keepCopyright";
3333
export * as maxAnimationFrames from "./maxAnimationFrames";
3434
export * as maxAnimationFrameResolution from "./maxAnimationFrameResolution";
3535
export * as maxBytes from "./maxBytes";
36+
export * as maxResultDimension from "./maxResultDimension";
3637
export * as objectPosition from "./objectsPosition";
3738
export * as maxSrcFileSize from "../optionsShared/maxSrcFileSize";
3839
export * as maxSrcResolution from "../optionsShared/maxSrcResolution";

src/options/maxResultDimension.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import type {
2+
MaxResultDimension,
3+
MaxResultDimensionOptionsPartial,
4+
} from "../types/maxResultDimension";
5+
import { guardIsUndef, guardIsNotNum } from "../utils";
6+
7+
const getOpt = (
8+
options: MaxResultDimensionOptionsPartial
9+
): MaxResultDimension | undefined => {
10+
return options.max_result_dimension ?? options.mrd;
11+
};
12+
13+
const test = (options: MaxResultDimensionOptionsPartial): boolean =>
14+
getOpt(options) !== undefined;
15+
16+
const build = (options: MaxResultDimensionOptionsPartial): string => {
17+
const maxResultDimension = getOpt(options);
18+
19+
guardIsUndef(maxResultDimension, "max_result_dimension");
20+
guardIsNotNum(maxResultDimension, "max_result_dimension", {
21+
addParam: { min: 0 },
22+
});
23+
24+
return `mrd:${maxResultDimension}`;
25+
};
26+
27+
export { test, build };

src/types/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import type { KeepCopyrightOptionsPartial } from "./keepCopyright";
3333
import type { MAFROptionsPartial } from "./maxAnimationFrameResolution";
3434
import type { MaxAnimationFramesOptionsPartial } from "./maxAnimationFrames";
3535
import type { MaxBytesOptionsPartial } from "./maxBytes";
36+
import type { MaxResultDimensionOptionsPartial } from "./maxResultDimension";
3637
import type { MaxSrcFileSizeOptionsPartial } from "../typesShared/maxSrcFileSize";
3738
import type { MaxSrcResolutionOptionsPartial } from "../typesShared/maxSrcResolution";
3839
import type { MinHeightOptionsPartial } from "./minHeight";
@@ -110,6 +111,7 @@ export type Options = AdjustOptionsPartial &
110111
MAFROptionsPartial &
111112
MaxAnimationFramesOptionsPartial &
112113
MaxBytesOptionsPartial &
114+
MaxResultDimensionOptionsPartial &
113115
MaxSrcFileSizeOptionsPartial &
114116
MaxSrcResolutionOptionsPartial &
115117
MinHeightOptionsPartial &

src/types/maxResultDimension.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* *Max result dimension*
3+
*
4+
* Allows redefining the `IMGPROXY_MAX_RESULT_DIMENSION` configuration.
5+
* Specifies the maximum allowed dimension for the resulting image.
6+
*
7+
* @warning Since this option allows redefining a security restriction,
8+
* its usage is not allowed unless the `IMGPROXY_ALLOW_SECURITY_OPTIONS` config is set to true.
9+
*
10+
* @see {@link https://docs.imgproxy.net/latest/usage/processing#max-result-dimension | max result dimension imgproxy docs}
11+
*/
12+
type MaxResultDimension = number;
13+
14+
/**
15+
* *Max result dimension*
16+
*
17+
* To describe the max result dimension option, you can use the keyword `max_result_dimension` or `mrd`.
18+
*/
19+
interface MaxResultDimensionOptionsPartial {
20+
max_result_dimension?: MaxResultDimension;
21+
mrd?: MaxResultDimension;
22+
}
23+
24+
export { MaxResultDimension, MaxResultDimensionOptionsPartial };
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { describe, expect, it } from "vitest";
2+
import { test, build } from "../../src/options/maxResultDimension";
3+
4+
describe("maxResultDimension", () => {
5+
describe("test", () => {
6+
it("should return true if max_result_dimension option is defined", () => {
7+
expect(test({ max_result_dimension: 1920 })).toEqual(true);
8+
});
9+
10+
it("should return true if mrd option is defined", () => {
11+
expect(test({ mrd: 2560 })).toEqual(true);
12+
});
13+
14+
it("should return false if max_result_dimension option is undefined", () => {
15+
expect(test({})).toEqual(false);
16+
});
17+
});
18+
19+
describe("build", () => {
20+
it("should throw an error if max_result_dimension option is undefined", () => {
21+
expect(() => build({})).toThrow(
22+
"max_result_dimension option is undefined"
23+
);
24+
});
25+
26+
it("should throw an error if max_result_dimension is not a number", () => {
27+
// @ts-expect-error: Let's ignore an error (check for users with vanilla js).
28+
expect(() => build({ max_result_dimension: "1920" })).toThrow(
29+
"max_result_dimension option is not a number"
30+
);
31+
});
32+
33+
it("should throw an error if max_result_dimension is less than 0", () => {
34+
expect(() => build({ max_result_dimension: -1 })).toThrow(
35+
"max_result_dimension option value can't be less then 0"
36+
);
37+
});
38+
39+
it("should return mrd:0 if max_result_dimension option is 0", () => {
40+
expect(build({ max_result_dimension: 0 })).toEqual("mrd:0");
41+
});
42+
43+
it("should return mrd:1920 if mrd option is 1920", () => {
44+
expect(build({ mrd: 1920 })).toEqual("mrd:1920");
45+
});
46+
47+
it("should return mrd:2560 if max_result_dimension option is 2560", () => {
48+
expect(build({ max_result_dimension: 2560 })).toEqual("mrd:2560");
49+
});
50+
51+
it("should work with 0", () => {
52+
expect(build({ max_result_dimension: 0 })).toEqual("mrd:0");
53+
expect(build({ mrd: 0 })).toEqual("mrd:0");
54+
});
55+
});
56+
});

0 commit comments

Comments
 (0)