Skip to content

Commit 0cdb756

Browse files
Elio-SwelloGamalta11bit
authored
Pro: Add pages_number option to generateImageInfoUrl() (#63)
* feat: add `pages_number` option to `generateImageInfoUrl()` * chore: lint * chore: changeset * Fix readme * Update changesets --------- Co-authored-by: Gamalta <[email protected]> Co-authored-by: Ivan Buryak <[email protected]>
1 parent db664f5 commit 0cdb756

File tree

8 files changed

+127
-17
lines changed

8 files changed

+127
-17
lines changed

.changeset/sad-dryers-flow.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 `page_number` option to generateImageInfoUrl(). Thanks @Elio-Swello

package-lock.json

Lines changed: 2 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/methods/generateImageInfoUrl.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,15 @@ describe("generateImageInfoUrl", () => {
7676
detect_objects: true,
7777
dominant_colors: { dominant_colors: 1, build_missed: 1 },
7878
iptc: "t",
79+
pages_number: "t",
7980
palette: 6,
8081
expires: 1729409825,
8182
size: 1,
8283
preset: ["test", "test2"],
8384
}
8485
)
8586
).toEqual(
86-
"/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"
87+
"/avg:t:f/do:t/dc:t:t/exp:1729409825/iptc:t/pn:t/p:6/pr:test:test2/s:t/plain/https://example.com/host/pic.png"
8788
);
8889
});
8990

src/optionsImageInfo/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export * as iptc from "./iptc";
1313
export * as maxSrcFileSize from "../optionsShared/maxSrcFileSize";
1414
export * as maxSrcResolution from "../optionsShared/maxSrcResolution";
1515
export * as page from "../optionsShared/page";
16+
export * as pagesNumber from "../optionsImageInfo/pagesNumber";
1617
export * as palette from "./palette";
1718
export * as preset from "../optionsShared/preset";
1819
export * as size from "./size";
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import type {
2+
NeedPagesNumber,
3+
PagesNumberInfoOptionsPartial,
4+
} from "../typesImageInfo/pagesNumber";
5+
import { guardIsUndef, normalizeBoolean } from "../utils";
6+
7+
const getOpt = (
8+
options: PagesNumberInfoOptionsPartial
9+
): NeedPagesNumber | undefined => {
10+
if ("pages_number" in options) {
11+
return options.pages_number;
12+
} else if ("pn" in options) {
13+
return options.pn;
14+
}
15+
16+
return undefined;
17+
};
18+
19+
const test = (options: PagesNumberInfoOptionsPartial): boolean =>
20+
getOpt(options) !== undefined;
21+
22+
const build = (options: PagesNumberInfoOptionsPartial): string => {
23+
const pagesNumberOpts = getOpt(options);
24+
guardIsUndef(pagesNumberOpts, "pages_number");
25+
return `pn:${normalizeBoolean(pagesNumberOpts)}`;
26+
};
27+
28+
export { test, build };

src/typesImageInfo/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type { IptcImageInfoOptionsPartial } from "./iptc";
1313
import type { MaxSrcFileSizeOptionsPartial } from "../typesShared/maxSrcFileSize";
1414
import type { MaxSrcResolutionOptionsPartial } from "../typesShared/maxSrcResolution";
1515
import type { PageOptionsPartial } from "../typesShared/page";
16+
import type { PagesNumberInfoOptionsPartial } from "./pagesNumber";
1617
import type { PaletteImageInfoOptionsPartial } from "./palette";
1718
import type { PresetOptionsPartial } from "../typesShared/preset";
1819
import type { SizeImageInfoOptionsPartial } from "./size";
@@ -36,6 +37,7 @@ export type OptionsImageInfo = AverageImageInfoOptionsPartial &
3637
MaxSrcFileSizeOptionsPartial &
3738
MaxSrcResolutionOptionsPartial &
3839
PageOptionsPartial &
40+
PagesNumberInfoOptionsPartial &
3941
PaletteImageInfoOptionsPartial &
4042
PresetOptionsPartial &
4143
SizeImageInfoOptionsPartial &

src/typesImageInfo/pagesNumber.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* *Pages number option*
3+
*
4+
* When set to `1`, `"t"` or `true`, imgproxy will return the number of pages the image contains.
5+
* For animated images, it'll return the number of animation frames.
6+
*
7+
*
8+
* @note If any value other than `1`, `"t"`, or `true` is passed, it will be recognized as `false`.
9+
*
10+
* Response example:
11+
* {
12+
* "pages_number": 10
13+
* }
14+
*
15+
* @default false
16+
*
17+
*
18+
* @see {@link https://docs.imgproxy.net/usage/getting_info?#pages-number | Pages number imgproxy docs}
19+
*/
20+
type NeedPagesNumber = 1 | "t" | true | false | string;
21+
22+
/**
23+
* *Pages number option*
24+
*
25+
* To describe the Pages number option, you can use the keyword `pages_number` or `pn`.
26+
*
27+
* @see https://docs.imgproxy.net/usage/getting_info?#pages-number
28+
*/
29+
interface PagesNumberInfoOptionsPartial {
30+
pages_number?: NeedPagesNumber;
31+
pn?: NeedPagesNumber;
32+
}
33+
34+
export { NeedPagesNumber, PagesNumberInfoOptionsPartial };
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { describe, expect, it } from "vitest";
2+
import { test, build } from "../../src/optionsImageInfo/pagesNumber";
3+
4+
describe("pages_number", () => {
5+
describe("test", () => {
6+
it("should return true if pages_number option is defined", () => {
7+
expect(test({ pages_number: 1 })).toEqual(true);
8+
});
9+
10+
it("should return true if pages_number option is false", () => {
11+
expect(test({ pn: false })).toEqual(true);
12+
});
13+
14+
it("should return false if pages_number option is undefined", () => {
15+
expect(test({})).toEqual(false);
16+
});
17+
18+
it("should return true if pn option is defined", () => {
19+
expect(test({ pn: "t" })).toEqual(true);
20+
});
21+
});
22+
23+
describe("build", () => {
24+
it("should throw an error if pages_number option is undefined", () => {
25+
expect(() => build({})).toThrow("pages_number option is undefined");
26+
});
27+
28+
it("should return 't' if pages_number option is 1", () => {
29+
expect(build({ pages_number: 1 })).toEqual("pn:t");
30+
});
31+
32+
it("should return 't' if s option is 't'", () => {
33+
expect(build({ pn: "t" })).toEqual("pn:t");
34+
});
35+
36+
it("should return 't' if pages_number option is true", () => {
37+
expect(build({ pages_number: true })).toEqual("pn:t");
38+
});
39+
40+
it("should return 'f' if pages_number option is false", () => {
41+
expect(build({ pages_number: false })).toEqual("pn:f");
42+
});
43+
44+
it("should return 'f' if pages_number option is 0", () => {
45+
// @ts-expect-error: Let's ignore an error.
46+
expect(build({ pages_number: 0 })).toEqual("pn:f");
47+
});
48+
49+
it("should return 'f' if pages_number option is string (except 't')", () => {
50+
expect(build({ pages_number: "true" })).toEqual("pn:f");
51+
});
52+
});
53+
});

0 commit comments

Comments
 (0)