@artsy/img 0.1.2 → 1.0.0
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 +12 -0
- package/dist/services/gemini.d.ts +2 -2
- package/dist/services/gemini.js +2 -2
- package/dist/services/imgix.d.ts +2 -2
- package/dist/services/imgix.js +2 -2
- package/dist/services/lambda.d.ts +2 -2
- package/dist/services/lambda.js +2 -2
- package/dist/services.d.ts +14 -15
- package/dist/services.js +7 -8
- package/package.json +1 -1
- package/src/__tests__/services.test.ts +5 -3
- package/src/services/gemini.ts +8 -4
- package/src/services/imgix.ts +8 -4
- package/src/services/lambda.ts +8 -4
- package/src/services.ts +23 -16
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,15 @@
|
|
|
1
|
+
# v1.0.0 (Fri Dec 09 2022)
|
|
2
|
+
|
|
3
|
+
#### 💥 Breaking Change
|
|
4
|
+
|
|
5
|
+
- refactor: renames functions and types to be less generic [#10](https://github.com/artsy/img/pull/10) ([@dzucconi](https://github.com/dzucconi))
|
|
6
|
+
|
|
7
|
+
#### Authors: 1
|
|
8
|
+
|
|
9
|
+
- Damon ([@dzucconi](https://github.com/dzucconi))
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
1
13
|
# v0.1.2 (Fri Dec 09 2022)
|
|
2
14
|
|
|
3
15
|
#### 🐛 Bug Fix
|
package/dist/services/gemini.js
CHANGED
|
@@ -4,8 +4,8 @@ exports.gemini = void 0;
|
|
|
4
4
|
const services_1 = require("../services");
|
|
5
5
|
const stringify_1 = require("../utils/stringify");
|
|
6
6
|
const gemini = (config) => {
|
|
7
|
-
return (mode, src, { width, height, quality = services_1.
|
|
8
|
-
if (!(0, services_1.
|
|
7
|
+
return (mode, src, { width, height, quality = services_1.DEFAULT_IMG_QUALITY }) => {
|
|
8
|
+
if (!(0, services_1.validateResizeOptions)(mode, { width, height }))
|
|
9
9
|
return src;
|
|
10
10
|
let resizeTo;
|
|
11
11
|
switch (mode) {
|
package/dist/services/imgix.d.ts
CHANGED
package/dist/services/imgix.js
CHANGED
|
@@ -8,8 +8,8 @@ const md5_1 = __importDefault(require("md5"));
|
|
|
8
8
|
const services_1 = require("../services");
|
|
9
9
|
const stringify_1 = require("../utils/stringify");
|
|
10
10
|
const imgix = (config) => {
|
|
11
|
-
return (mode, src, { width, height, quality = services_1.
|
|
12
|
-
if (!(0, services_1.
|
|
11
|
+
return (mode, src, { width, height, quality = services_1.DEFAULT_IMG_QUALITY }) => {
|
|
12
|
+
if (!(0, services_1.validateResizeOptions)(mode, { width, height }))
|
|
13
13
|
return src;
|
|
14
14
|
const params = {
|
|
15
15
|
fit: { crop: "crop", resize: "clip" }[mode],
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ResizeExec } from "../services";
|
|
2
2
|
export type Lambda = {
|
|
3
3
|
endpoint: string;
|
|
4
4
|
sources: {
|
|
@@ -6,4 +6,4 @@ export type Lambda = {
|
|
|
6
6
|
bucket: string;
|
|
7
7
|
}[];
|
|
8
8
|
};
|
|
9
|
-
export declare const lambda: (config: Lambda) =>
|
|
9
|
+
export declare const lambda: (config: Lambda) => ResizeExec;
|
package/dist/services/lambda.js
CHANGED
|
@@ -3,8 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.lambda = void 0;
|
|
4
4
|
const services_1 = require("../services");
|
|
5
5
|
const lambda = (config) => {
|
|
6
|
-
return (mode, src, { width, height, quality = services_1.
|
|
7
|
-
if (!(0, services_1.
|
|
6
|
+
return (mode, src, { width, height, quality = services_1.DEFAULT_IMG_QUALITY }) => {
|
|
7
|
+
if (!(0, services_1.validateResizeOptions)(mode, { width, height }))
|
|
8
8
|
return src;
|
|
9
9
|
const source = config.sources.find((source) => {
|
|
10
10
|
return src.startsWith(source.source);
|
package/dist/services.d.ts
CHANGED
|
@@ -1,27 +1,26 @@
|
|
|
1
1
|
import { Gemini } from "./services/gemini";
|
|
2
2
|
import { Imgix } from "./services/imgix";
|
|
3
3
|
import { Lambda } from "./services/lambda";
|
|
4
|
-
export declare const
|
|
5
|
-
export declare const
|
|
6
|
-
export
|
|
7
|
-
export type
|
|
8
|
-
export type Config = {
|
|
9
|
-
gemini?: Gemini;
|
|
10
|
-
imgix?: Imgix;
|
|
11
|
-
lambda?: Lambda;
|
|
12
|
-
};
|
|
13
|
-
export type Options = {
|
|
4
|
+
export declare const RESIZE_MODES: readonly ["resize", "crop"];
|
|
5
|
+
export declare const DEFAULT_IMG_QUALITY = 80;
|
|
6
|
+
export type ResizeMode = typeof RESIZE_MODES[number];
|
|
7
|
+
export type ResizeOptions = {
|
|
14
8
|
width?: number;
|
|
15
9
|
height?: number;
|
|
16
10
|
quality?: number;
|
|
17
11
|
};
|
|
18
|
-
export type
|
|
19
|
-
|
|
20
|
-
|
|
12
|
+
export type ServiceConfigurations = {
|
|
13
|
+
gemini?: Gemini;
|
|
14
|
+
imgix?: Imgix;
|
|
15
|
+
lambda?: Lambda;
|
|
16
|
+
};
|
|
17
|
+
export type ResizeExec = (mode: ResizeMode, src: string, options: ResizeOptions) => string;
|
|
18
|
+
export type ImageService = {
|
|
19
|
+
exec: ResizeExec;
|
|
21
20
|
};
|
|
22
|
-
export declare const
|
|
21
|
+
export declare const validateResizeOptions: (mode: ResizeMode, { width, height }: ResizeOptions) => boolean;
|
|
23
22
|
/**
|
|
24
23
|
* Returns a list of configured services.
|
|
25
24
|
* All endpoints should *not* have trailing slashes.
|
|
26
25
|
*/
|
|
27
|
-
export declare const
|
|
26
|
+
export declare const configureImageServices: <T extends ServiceConfigurations>(config: T) => Record<keyof T, ImageService>;
|
package/dist/services.js
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.configureImageServices = exports.validateResizeOptions = exports.DEFAULT_IMG_QUALITY = exports.RESIZE_MODES = void 0;
|
|
4
4
|
const gemini_1 = require("./services/gemini");
|
|
5
5
|
const imgix_1 = require("./services/imgix");
|
|
6
6
|
const lambda_1 = require("./services/lambda");
|
|
7
|
-
exports.
|
|
8
|
-
exports.
|
|
9
|
-
|
|
10
|
-
const validate = (mode, { width, height }) => {
|
|
7
|
+
exports.RESIZE_MODES = ["resize", "crop"];
|
|
8
|
+
exports.DEFAULT_IMG_QUALITY = 80;
|
|
9
|
+
const validateResizeOptions = (mode, { width, height }) => {
|
|
11
10
|
if (mode === "crop" && (!width || !height)) {
|
|
12
11
|
console.warn("`crop`requires both `width` and `height`");
|
|
13
12
|
return false;
|
|
@@ -26,12 +25,12 @@ const validate = (mode, { width, height }) => {
|
|
|
26
25
|
}
|
|
27
26
|
return true;
|
|
28
27
|
};
|
|
29
|
-
exports.
|
|
28
|
+
exports.validateResizeOptions = validateResizeOptions;
|
|
30
29
|
/**
|
|
31
30
|
* Returns a list of configured services.
|
|
32
31
|
* All endpoints should *not* have trailing slashes.
|
|
33
32
|
*/
|
|
34
|
-
const
|
|
33
|
+
const configureImageServices = (config) => {
|
|
35
34
|
const services = {};
|
|
36
35
|
if (config.gemini) {
|
|
37
36
|
services.gemini = { exec: (0, gemini_1.gemini)(config.gemini) };
|
|
@@ -44,4 +43,4 @@ const configure = (config) => {
|
|
|
44
43
|
}
|
|
45
44
|
return services;
|
|
46
45
|
};
|
|
47
|
-
exports.
|
|
46
|
+
exports.configureImageServices = configureImageServices;
|
package/package.json
CHANGED
|
@@ -1,16 +1,18 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { configureImageServices } from "../services";
|
|
2
2
|
|
|
3
3
|
const EXAMPLE_SRC =
|
|
4
4
|
"https://d32dm0rphc51dk.cloudfront.net/MFFPXvpJSoGzggU8zujwBw/normalized.jpg";
|
|
5
5
|
|
|
6
6
|
describe("strategies", () => {
|
|
7
7
|
it("only returns the services that are configured", () => {
|
|
8
|
-
const services =
|
|
8
|
+
const services = configureImageServices({
|
|
9
|
+
gemini: { endpoint: "https://example.com" },
|
|
10
|
+
});
|
|
9
11
|
|
|
10
12
|
expect(Object.keys(services)).toEqual(["gemini"]);
|
|
11
13
|
});
|
|
12
14
|
|
|
13
|
-
const { gemini, imgix, lambda } =
|
|
15
|
+
const { gemini, imgix, lambda } = configureImageServices({
|
|
14
16
|
imgix: {
|
|
15
17
|
endpoint: "https://example.imgix.net",
|
|
16
18
|
token: "secret",
|
package/src/services/gemini.ts
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
DEFAULT_IMG_QUALITY,
|
|
3
|
+
ResizeExec,
|
|
4
|
+
validateResizeOptions,
|
|
5
|
+
} from "../services";
|
|
2
6
|
import { stringify } from "../utils/stringify";
|
|
3
7
|
|
|
4
8
|
export type Gemini = {
|
|
5
9
|
endpoint: string;
|
|
6
10
|
};
|
|
7
11
|
|
|
8
|
-
export const gemini = (config: Gemini):
|
|
9
|
-
return (mode, src, { width, height, quality =
|
|
10
|
-
if (!
|
|
12
|
+
export const gemini = (config: Gemini): ResizeExec => {
|
|
13
|
+
return (mode, src, { width, height, quality = DEFAULT_IMG_QUALITY }) => {
|
|
14
|
+
if (!validateResizeOptions(mode, { width, height })) return src;
|
|
11
15
|
|
|
12
16
|
let resizeTo: "width" | "height" | "fit" | "fill";
|
|
13
17
|
|
package/src/services/imgix.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import md5 from "md5";
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
DEFAULT_IMG_QUALITY,
|
|
4
|
+
ResizeExec,
|
|
5
|
+
validateResizeOptions,
|
|
6
|
+
} from "../services";
|
|
3
7
|
import { stringify } from "../utils/stringify";
|
|
4
8
|
|
|
5
9
|
export type Imgix = {
|
|
@@ -7,9 +11,9 @@ export type Imgix = {
|
|
|
7
11
|
token: string;
|
|
8
12
|
};
|
|
9
13
|
|
|
10
|
-
export const imgix = (config: Imgix):
|
|
11
|
-
return (mode, src, { width, height, quality =
|
|
12
|
-
if (!
|
|
14
|
+
export const imgix = (config: Imgix): ResizeExec => {
|
|
15
|
+
return (mode, src, { width, height, quality = DEFAULT_IMG_QUALITY }) => {
|
|
16
|
+
if (!validateResizeOptions(mode, { width, height })) return src;
|
|
13
17
|
|
|
14
18
|
const params = {
|
|
15
19
|
fit: { crop: "crop", resize: "clip" }[mode],
|
package/src/services/lambda.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
DEFAULT_IMG_QUALITY,
|
|
3
|
+
ResizeExec,
|
|
4
|
+
validateResizeOptions,
|
|
5
|
+
} from "../services";
|
|
2
6
|
|
|
3
7
|
export type Lambda = {
|
|
4
8
|
endpoint: string;
|
|
@@ -8,9 +12,9 @@ export type Lambda = {
|
|
|
8
12
|
}[];
|
|
9
13
|
};
|
|
10
14
|
|
|
11
|
-
export const lambda = (config: Lambda):
|
|
12
|
-
return (mode, src, { width, height, quality =
|
|
13
|
-
if (!
|
|
15
|
+
export const lambda = (config: Lambda): ResizeExec => {
|
|
16
|
+
return (mode, src, { width, height, quality = DEFAULT_IMG_QUALITY }) => {
|
|
17
|
+
if (!validateResizeOptions(mode, { width, height })) return src;
|
|
14
18
|
|
|
15
19
|
const source = config.sources.find((source) => {
|
|
16
20
|
return src.startsWith(source.source);
|
package/src/services.ts
CHANGED
|
@@ -2,31 +2,36 @@ import { Gemini, gemini } from "./services/gemini";
|
|
|
2
2
|
import { imgix, Imgix } from "./services/imgix";
|
|
3
3
|
import { lambda, Lambda } from "./services/lambda";
|
|
4
4
|
|
|
5
|
-
export const
|
|
5
|
+
export const RESIZE_MODES = ["resize", "crop"] as const;
|
|
6
6
|
|
|
7
|
-
export const
|
|
7
|
+
export const DEFAULT_IMG_QUALITY = 80;
|
|
8
8
|
|
|
9
|
-
export
|
|
9
|
+
export type ResizeMode = typeof RESIZE_MODES[number];
|
|
10
10
|
|
|
11
|
-
export type
|
|
11
|
+
export type ResizeOptions = {
|
|
12
|
+
width?: number;
|
|
13
|
+
height?: number;
|
|
14
|
+
quality?: number;
|
|
15
|
+
};
|
|
12
16
|
|
|
13
|
-
export type
|
|
17
|
+
export type ServiceConfigurations = {
|
|
14
18
|
gemini?: Gemini;
|
|
15
19
|
imgix?: Imgix;
|
|
16
20
|
lambda?: Lambda;
|
|
17
21
|
};
|
|
18
22
|
|
|
19
|
-
export type
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
export type Exec = (mode: Mode, src: string, options: Options) => string;
|
|
23
|
+
export type ResizeExec = (
|
|
24
|
+
mode: ResizeMode,
|
|
25
|
+
src: string,
|
|
26
|
+
options: ResizeOptions
|
|
27
|
+
) => string;
|
|
26
28
|
|
|
27
|
-
export type
|
|
29
|
+
export type ImageService = { exec: ResizeExec };
|
|
28
30
|
|
|
29
|
-
export const
|
|
31
|
+
export const validateResizeOptions = (
|
|
32
|
+
mode: ResizeMode,
|
|
33
|
+
{ width, height }: ResizeOptions
|
|
34
|
+
) => {
|
|
30
35
|
if (mode === "crop" && (!width || !height)) {
|
|
31
36
|
console.warn("`crop`requires both `width` and `height`");
|
|
32
37
|
return false;
|
|
@@ -54,8 +59,10 @@ export const validate = (mode: Mode, { width, height }: Options) => {
|
|
|
54
59
|
* Returns a list of configured services.
|
|
55
60
|
* All endpoints should *not* have trailing slashes.
|
|
56
61
|
*/
|
|
57
|
-
export const
|
|
58
|
-
|
|
62
|
+
export const configureImageServices = <T extends ServiceConfigurations>(
|
|
63
|
+
config: T
|
|
64
|
+
) => {
|
|
65
|
+
const services = {} as Record<keyof T, ImageService>;
|
|
59
66
|
|
|
60
67
|
if (config.gemini) {
|
|
61
68
|
services.gemini = { exec: gemini(config.gemini) };
|