Skip to content

Commit 3679ee6

Browse files
authored
feat: export IPXModifiers (#272)
1 parent d50c8e1 commit 3679ee6

1 file changed

Lines changed: 72 additions & 4 deletions

File tree

src/ipx.ts

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,74 @@ type IPXSourceMeta = {
2222
maxAge?: number;
2323
};
2424

25+
type FormatModifier =
26+
| "jpeg"
27+
| "jpg"
28+
| "png"
29+
| "webp"
30+
| "avif"
31+
| "gif"
32+
| "heif"
33+
| "tiff"
34+
| "auto"
35+
| (string & {});
36+
37+
export interface IPXModifiers {
38+
format: FormatModifier;
39+
// alias for format
40+
f: FormatModifier;
41+
fit: "contain" | "cover" | "fill" | "inside" | "outside" | (string & {});
42+
resize: string;
43+
// alias for resize
44+
s: string;
45+
quality: number | string;
46+
// alias for quality
47+
q: number | string;
48+
background: string;
49+
b: string;
50+
position: string;
51+
pos: string;
52+
enlarge: true | "true";
53+
kernel:
54+
| "nearest"
55+
| "cubic"
56+
| "mitchell"
57+
| "lanczos2"
58+
| "lanczos3"
59+
| (string & {});
60+
trim: number | string;
61+
extend: string;
62+
extract: string;
63+
crop: string;
64+
rotate: number | string;
65+
flip: true | "true";
66+
flop: true | "true";
67+
sharpen: number | string;
68+
median: number | string;
69+
blur: number | string;
70+
flatten: true | "true";
71+
gamma: string;
72+
negate: true | "true";
73+
normalize: true | "true";
74+
threshold: number | string;
75+
modulate: string;
76+
tint: number | string;
77+
grayscale: true | "true";
78+
animated: true | "true";
79+
// alias for animated
80+
a: true | "true";
81+
width: string | number;
82+
w: string | number;
83+
height: string | number;
84+
h: string | number;
85+
}
86+
87+
// ensure we have types for all modifiers
88+
({}) as IPXModifiers satisfies Record<
89+
HandlerName,
90+
string | number | boolean | undefined
91+
>;
92+
2593
/**
2694
* A function type that defines an IPX image processing instance.
2795
*
@@ -41,9 +109,7 @@ type IPXSourceMeta = {
41109
*/
42110
export type IPX = (
43111
id: string,
44-
modifiers?: Partial<
45-
Record<HandlerName | "f" | "format" | "a" | "animated", string>
46-
>,
112+
modifiers?: Partial<IPXModifiers>,
47113
requestOptions?: any,
48114
) => {
49115
getSourceMeta: () => Promise<IPXSourceMeta>;
@@ -282,7 +348,9 @@ export function createIPX(userOptions: IPXOptions): IPX {
282348
// Apply handlers
283349
const handlerContext: any = { meta: imageMeta };
284350
for (const h of handlers) {
285-
sharp = applyHandler(handlerContext, sharp, h.handler, h.args) || sharp;
351+
sharp =
352+
applyHandler(handlerContext, sharp, h.handler, h.args.toString()) ||
353+
sharp;
286354
}
287355

288356
// Apply format

0 commit comments

Comments
 (0)