@artsy/img 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +24 -0
- package/dist/services/gemini.js +5 -6
- package/dist/services/imgix.js +2 -2
- package/dist/utils/stringify.d.ts +1 -0
- package/dist/utils/stringify.js +16 -0
- package/package.json +2 -3
- package/src/__tests__/services.test.ts +37 -33
- package/src/services/gemini.ts +5 -6
- package/src/services/imgix.ts +1 -1
- package/src/utils/stringify.ts +15 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,27 @@
|
|
|
1
|
+
# v0.1.2 (Fri Dec 09 2022)
|
|
2
|
+
|
|
3
|
+
#### 🐛 Bug Fix
|
|
4
|
+
|
|
5
|
+
- fix(gemini): fixes crop url mode [#9](https://github.com/artsy/img/pull/9) ([@dzucconi](https://github.com/dzucconi))
|
|
6
|
+
|
|
7
|
+
#### Authors: 1
|
|
8
|
+
|
|
9
|
+
- Damon ([@dzucconi](https://github.com/dzucconi))
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
# v0.1.1 (Fri Dec 09 2022)
|
|
14
|
+
|
|
15
|
+
#### 🐛 Bug Fix
|
|
16
|
+
|
|
17
|
+
- fix: removes qs dependency; fixes gemini width/height modes [#8](https://github.com/artsy/img/pull/8) ([@dzucconi](https://github.com/dzucconi))
|
|
18
|
+
|
|
19
|
+
#### Authors: 1
|
|
20
|
+
|
|
21
|
+
- Damon ([@dzucconi](https://github.com/dzucconi))
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
1
25
|
# v0.1.0 (Thu Dec 08 2022)
|
|
2
26
|
|
|
3
27
|
#### 🚀 Enhancement
|
package/dist/services/gemini.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.gemini = void 0;
|
|
4
|
-
const qs_1 = require("qs");
|
|
5
4
|
const services_1 = require("../services");
|
|
5
|
+
const stringify_1 = require("../utils/stringify");
|
|
6
6
|
const gemini = (config) => {
|
|
7
7
|
return (mode, src, { width, height, quality = services_1.DEFAULT_QUALITY }) => {
|
|
8
8
|
if (!(0, services_1.validate)(mode, { width, height }))
|
|
@@ -11,13 +11,16 @@ const gemini = (config) => {
|
|
|
11
11
|
switch (mode) {
|
|
12
12
|
case "crop": {
|
|
13
13
|
resizeTo = "fill";
|
|
14
|
+
break;
|
|
14
15
|
}
|
|
15
16
|
case "resize": {
|
|
16
17
|
if (width && !height) {
|
|
17
18
|
resizeTo = "width";
|
|
19
|
+
break;
|
|
18
20
|
}
|
|
19
21
|
if (height && !width) {
|
|
20
22
|
resizeTo = "height";
|
|
23
|
+
break;
|
|
21
24
|
}
|
|
22
25
|
resizeTo = "fit";
|
|
23
26
|
}
|
|
@@ -29,11 +32,7 @@ const gemini = (config) => {
|
|
|
29
32
|
width,
|
|
30
33
|
quality,
|
|
31
34
|
};
|
|
32
|
-
const query = (0,
|
|
33
|
-
arrayFormat: "brackets",
|
|
34
|
-
skipNulls: true,
|
|
35
|
-
sort: (a, b) => a.localeCompare(b),
|
|
36
|
-
});
|
|
35
|
+
const query = (0, stringify_1.stringify)(params);
|
|
37
36
|
return `${config.endpoint}?${query}`;
|
|
38
37
|
};
|
|
39
38
|
};
|
package/dist/services/imgix.js
CHANGED
|
@@ -5,8 +5,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.imgix = void 0;
|
|
7
7
|
const md5_1 = __importDefault(require("md5"));
|
|
8
|
-
const qs_1 = require("qs");
|
|
9
8
|
const services_1 = require("../services");
|
|
9
|
+
const stringify_1 = require("../utils/stringify");
|
|
10
10
|
const imgix = (config) => {
|
|
11
11
|
return (mode, src, { width, height, quality = services_1.DEFAULT_QUALITY }) => {
|
|
12
12
|
if (!(0, services_1.validate)(mode, { width, height }))
|
|
@@ -19,7 +19,7 @@ const imgix = (config) => {
|
|
|
19
19
|
auto: "format",
|
|
20
20
|
};
|
|
21
21
|
const path = `/${encodeURIComponent(src)}`;
|
|
22
|
-
const query = `?${(0,
|
|
22
|
+
const query = `?${(0, stringify_1.stringify)(params)}`;
|
|
23
23
|
const signature = (0, md5_1.default)(config.token + path + query);
|
|
24
24
|
return `${config.endpoint}${path}${query}&s=${signature}`;
|
|
25
25
|
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const stringify: (params: Record<string, string | number | undefined | null | boolean>) => string;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.stringify = void 0;
|
|
4
|
+
const stringify = (params) => {
|
|
5
|
+
const keys = Object.keys(params)
|
|
6
|
+
// Alphabetize the keys
|
|
7
|
+
.sort((a, b) => a.localeCompare(b))
|
|
8
|
+
// Filter out null or undefined values
|
|
9
|
+
.filter((key) => params[key] != null);
|
|
10
|
+
return keys
|
|
11
|
+
.map((key) => {
|
|
12
|
+
return `${key}=${encodeURIComponent(`${params[key]}`)}`;
|
|
13
|
+
})
|
|
14
|
+
.join("&");
|
|
15
|
+
};
|
|
16
|
+
exports.stringify = stringify;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@artsy/img",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "Logic for constructing image proxy URLs",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -30,7 +30,6 @@
|
|
|
30
30
|
"test": "jest"
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"md5": "^2.3.0"
|
|
34
|
-
"qs": "^6.11.0"
|
|
33
|
+
"md5": "^2.3.0"
|
|
35
34
|
}
|
|
36
35
|
}
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { configure } from "../services";
|
|
2
2
|
|
|
3
|
+
const EXAMPLE_SRC =
|
|
4
|
+
"https://d32dm0rphc51dk.cloudfront.net/MFFPXvpJSoGzggU8zujwBw/normalized.jpg";
|
|
5
|
+
|
|
3
6
|
describe("strategies", () => {
|
|
4
7
|
it("only returns the services that are configured", () => {
|
|
5
8
|
const services = configure({ gemini: { endpoint: "https://example.com" } });
|
|
@@ -28,63 +31,68 @@ describe("strategies", () => {
|
|
|
28
31
|
|
|
29
32
|
describe("gemini", () => {
|
|
30
33
|
it("returns a resized url", () => {
|
|
31
|
-
const img = gemini.exec(
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
);
|
|
34
|
+
const img = gemini.exec("resize", EXAMPLE_SRC, {
|
|
35
|
+
width: 200,
|
|
36
|
+
height: 200,
|
|
37
|
+
});
|
|
36
38
|
|
|
37
39
|
expect(img).toEqual(
|
|
38
40
|
"https://d7hftxdivxxvm.cloudfront.net?height=200&quality=80&resize_to=fit&src=https%3A%2F%2Fd32dm0rphc51dk.cloudfront.net%2FMFFPXvpJSoGzggU8zujwBw%2Fnormalized.jpg&width=200"
|
|
39
41
|
);
|
|
40
42
|
});
|
|
41
43
|
|
|
42
|
-
it("returns a
|
|
43
|
-
const img = gemini.exec(
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
44
|
+
it("returns a resized url with only width specified", () => {
|
|
45
|
+
const img = gemini.exec("resize", EXAMPLE_SRC, { width: 200 });
|
|
46
|
+
|
|
47
|
+
expect(img).toEqual(
|
|
48
|
+
"https://d7hftxdivxxvm.cloudfront.net?quality=80&resize_to=width&src=https%3A%2F%2Fd32dm0rphc51dk.cloudfront.net%2FMFFPXvpJSoGzggU8zujwBw%2Fnormalized.jpg&width=200"
|
|
47
49
|
);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("returns a resized url with only height specified", () => {
|
|
53
|
+
const img = gemini.exec("resize", EXAMPLE_SRC, { height: 200 });
|
|
48
54
|
|
|
49
55
|
expect(img).toEqual(
|
|
50
|
-
"https://d7hftxdivxxvm.cloudfront.net?height=200&quality=80&resize_to=
|
|
56
|
+
"https://d7hftxdivxxvm.cloudfront.net?height=200&quality=80&resize_to=height&src=https%3A%2F%2Fd32dm0rphc51dk.cloudfront.net%2FMFFPXvpJSoGzggU8zujwBw%2Fnormalized.jpg"
|
|
57
|
+
);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("returns a cropped url", () => {
|
|
61
|
+
const img = gemini.exec("crop", EXAMPLE_SRC, { width: 200, height: 200 });
|
|
62
|
+
|
|
63
|
+
expect(img).toEqual(
|
|
64
|
+
"https://d7hftxdivxxvm.cloudfront.net?height=200&quality=80&resize_to=fill&src=https%3A%2F%2Fd32dm0rphc51dk.cloudfront.net%2FMFFPXvpJSoGzggU8zujwBw%2Fnormalized.jpg&width=200"
|
|
51
65
|
);
|
|
52
66
|
});
|
|
53
67
|
});
|
|
54
68
|
|
|
55
69
|
describe("imgix", () => {
|
|
56
70
|
it("returns a resized url", () => {
|
|
57
|
-
const img = imgix.exec(
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
);
|
|
71
|
+
const img = imgix.exec("resize", EXAMPLE_SRC, {
|
|
72
|
+
width: 200,
|
|
73
|
+
height: 200,
|
|
74
|
+
});
|
|
62
75
|
|
|
63
76
|
expect(img).toEqual(
|
|
64
|
-
"https://example.imgix.net/https%3A%2F%2Fd32dm0rphc51dk.cloudfront.net%2FMFFPXvpJSoGzggU8zujwBw%2Fnormalized.jpg?fit=clip&height=200&
|
|
77
|
+
"https://example.imgix.net/https%3A%2F%2Fd32dm0rphc51dk.cloudfront.net%2FMFFPXvpJSoGzggU8zujwBw%2Fnormalized.jpg?auto=format&fit=clip&height=200&quality=80&width=200&s=1d931e11fbfb846dbe5f1ffa895017d9"
|
|
65
78
|
);
|
|
66
79
|
});
|
|
67
80
|
|
|
68
81
|
it("returns a cropped url", () => {
|
|
69
|
-
const img = imgix.exec(
|
|
70
|
-
"crop",
|
|
71
|
-
"https://d32dm0rphc51dk.cloudfront.net/MFFPXvpJSoGzggU8zujwBw/normalized.jpg",
|
|
72
|
-
{ width: 200, height: 200 }
|
|
73
|
-
);
|
|
82
|
+
const img = imgix.exec("crop", EXAMPLE_SRC, { width: 200, height: 200 });
|
|
74
83
|
|
|
75
84
|
expect(img).toEqual(
|
|
76
|
-
"https://example.imgix.net/https%3A%2F%2Fd32dm0rphc51dk.cloudfront.net%2FMFFPXvpJSoGzggU8zujwBw%2Fnormalized.jpg?fit=crop&height=200&
|
|
85
|
+
"https://example.imgix.net/https%3A%2F%2Fd32dm0rphc51dk.cloudfront.net%2FMFFPXvpJSoGzggU8zujwBw%2Fnormalized.jpg?auto=format&fit=crop&height=200&quality=80&width=200&s=fd511845304eb4a5cad016b52aee939b"
|
|
77
86
|
);
|
|
78
87
|
});
|
|
79
88
|
});
|
|
80
89
|
|
|
81
90
|
describe("lambda", () => {
|
|
82
91
|
it("returns a resized url", () => {
|
|
83
|
-
const img = lambda.exec(
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
);
|
|
92
|
+
const img = lambda.exec("resize", EXAMPLE_SRC, {
|
|
93
|
+
width: 200,
|
|
94
|
+
height: 200,
|
|
95
|
+
});
|
|
88
96
|
|
|
89
97
|
expect(img).toEqual(
|
|
90
98
|
"https://d1j88w5k23s1nr.cloudfront.net/eyJidWNrZXQiOiJhcnRzeS1tZWRpYS1hc3NldHMiLCJrZXkiOiJNRkZQWHZwSlNvR3pnZ1U4enVqd0J3L25vcm1hbGl6ZWQuanBnIiwiZWRpdHMiOnsicmVzaXplIjp7IndpZHRoIjoyMDAsImhlaWdodCI6MjAwLCJmaXQiOiJpbnNpZGUifSwid2VicCI6eyJxdWFsaXR5Ijo4MH0sImpwZWciOnsicXVhbGl0eSI6ODB9LCJyb3RhdGUiOm51bGx9fQ=="
|
|
@@ -92,11 +100,7 @@ describe("strategies", () => {
|
|
|
92
100
|
});
|
|
93
101
|
|
|
94
102
|
it("returns a cropped url", () => {
|
|
95
|
-
const img = lambda.exec(
|
|
96
|
-
"crop",
|
|
97
|
-
"https://d32dm0rphc51dk.cloudfront.net/MFFPXvpJSoGzggU8zujwBw/normalized.jpg",
|
|
98
|
-
{ width: 200, height: 200 }
|
|
99
|
-
);
|
|
103
|
+
const img = lambda.exec("crop", EXAMPLE_SRC, { width: 200, height: 200 });
|
|
100
104
|
|
|
101
105
|
expect(img).toEqual(
|
|
102
106
|
"https://d1j88w5k23s1nr.cloudfront.net/eyJidWNrZXQiOiJhcnRzeS1tZWRpYS1hc3NldHMiLCJrZXkiOiJNRkZQWHZwSlNvR3pnZ1U4enVqd0J3L25vcm1hbGl6ZWQuanBnIiwiZWRpdHMiOnsicmVzaXplIjp7IndpZHRoIjoyMDAsImhlaWdodCI6MjAwLCJmaXQiOiJjb3ZlciJ9LCJ3ZWJwIjp7InF1YWxpdHkiOjgwfSwianBlZyI6eyJxdWFsaXR5Ijo4MH0sInJvdGF0ZSI6bnVsbH19"
|
package/src/services/gemini.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { stringify } from "qs";
|
|
2
1
|
import { DEFAULT_QUALITY, Exec, validate } from "../services";
|
|
2
|
+
import { stringify } from "../utils/stringify";
|
|
3
3
|
|
|
4
4
|
export type Gemini = {
|
|
5
5
|
endpoint: string;
|
|
@@ -14,15 +14,18 @@ export const gemini = (config: Gemini): Exec => {
|
|
|
14
14
|
switch (mode) {
|
|
15
15
|
case "crop": {
|
|
16
16
|
resizeTo = "fill";
|
|
17
|
+
break;
|
|
17
18
|
}
|
|
18
19
|
|
|
19
20
|
case "resize": {
|
|
20
21
|
if (width && !height) {
|
|
21
22
|
resizeTo = "width";
|
|
23
|
+
break;
|
|
22
24
|
}
|
|
23
25
|
|
|
24
26
|
if (height && !width) {
|
|
25
27
|
resizeTo = "height";
|
|
28
|
+
break;
|
|
26
29
|
}
|
|
27
30
|
|
|
28
31
|
resizeTo = "fit";
|
|
@@ -37,11 +40,7 @@ export const gemini = (config: Gemini): Exec => {
|
|
|
37
40
|
quality,
|
|
38
41
|
};
|
|
39
42
|
|
|
40
|
-
const query = stringify(params
|
|
41
|
-
arrayFormat: "brackets",
|
|
42
|
-
skipNulls: true,
|
|
43
|
-
sort: (a, b) => a.localeCompare(b),
|
|
44
|
-
});
|
|
43
|
+
const query = stringify(params);
|
|
45
44
|
|
|
46
45
|
return `${config.endpoint}?${query}`;
|
|
47
46
|
};
|
package/src/services/imgix.ts
CHANGED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export const stringify = (
|
|
2
|
+
params: Record<string, string | number | undefined | null | boolean>
|
|
3
|
+
): string => {
|
|
4
|
+
const keys = Object.keys(params)
|
|
5
|
+
// Alphabetize the keys
|
|
6
|
+
.sort((a, b) => a.localeCompare(b))
|
|
7
|
+
// Filter out null or undefined values
|
|
8
|
+
.filter((key) => params[key] != null);
|
|
9
|
+
|
|
10
|
+
return keys
|
|
11
|
+
.map((key) => {
|
|
12
|
+
return `${key}=${encodeURIComponent(`${params[key]}`)}`;
|
|
13
|
+
})
|
|
14
|
+
.join("&");
|
|
15
|
+
};
|