@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 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
@@ -1,5 +1,5 @@
1
- import { Exec } from "../services";
1
+ import { ResizeExec } from "../services";
2
2
  export type Gemini = {
3
3
  endpoint: string;
4
4
  };
5
- export declare const gemini: (config: Gemini) => Exec;
5
+ export declare const gemini: (config: Gemini) => ResizeExec;
@@ -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.DEFAULT_QUALITY }) => {
8
- if (!(0, services_1.validate)(mode, { width, height }))
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) {
@@ -1,6 +1,6 @@
1
- import { Exec } from "../services";
1
+ import { ResizeExec } from "../services";
2
2
  export type Imgix = {
3
3
  endpoint: string;
4
4
  token: string;
5
5
  };
6
- export declare const imgix: (config: Imgix) => Exec;
6
+ export declare const imgix: (config: Imgix) => ResizeExec;
@@ -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.DEFAULT_QUALITY }) => {
12
- if (!(0, services_1.validate)(mode, { width, height }))
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 { Exec } from "../services";
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) => Exec;
9
+ export declare const lambda: (config: Lambda) => ResizeExec;
@@ -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.DEFAULT_QUALITY }) => {
7
- if (!(0, services_1.validate)(mode, { width, height }))
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);
@@ -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 SERVICES: readonly ["gemini", "imgix", "lambda"];
5
- export declare const MODES: readonly ["resize", "crop"];
6
- export declare const DEFAULT_QUALITY = 80;
7
- export type Mode = typeof MODES[number];
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 Exec = (mode: Mode, src: string, options: Options) => string;
19
- export type Service = {
20
- exec: Exec;
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 validate: (mode: Mode, { width, height }: Options) => boolean;
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 configure: <T extends Config>(config: T) => Record<keyof T, Service>;
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.configure = exports.validate = exports.DEFAULT_QUALITY = exports.MODES = exports.SERVICES = void 0;
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.SERVICES = ["gemini", "imgix", "lambda"];
8
- exports.MODES = ["resize", "crop"];
9
- exports.DEFAULT_QUALITY = 80;
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.validate = validate;
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 configure = (config) => {
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.configure = configure;
46
+ exports.configureImageServices = configureImageServices;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@artsy/img",
3
- "version": "0.1.2",
3
+ "version": "1.0.0",
4
4
  "description": "Logic for constructing image proxy URLs",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -1,16 +1,18 @@
1
- import { configure } from "../services";
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 = configure({ gemini: { endpoint: "https://example.com" } });
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 } = configure({
15
+ const { gemini, imgix, lambda } = configureImageServices({
14
16
  imgix: {
15
17
  endpoint: "https://example.imgix.net",
16
18
  token: "secret",
@@ -1,13 +1,17 @@
1
- import { DEFAULT_QUALITY, Exec, validate } from "../services";
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): Exec => {
9
- return (mode, src, { width, height, quality = DEFAULT_QUALITY }) => {
10
- if (!validate(mode, { width, height })) return src;
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
 
@@ -1,5 +1,9 @@
1
1
  import md5 from "md5";
2
- import { DEFAULT_QUALITY, Exec, validate } from "../services";
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): Exec => {
11
- return (mode, src, { width, height, quality = DEFAULT_QUALITY }) => {
12
- if (!validate(mode, { width, height })) return src;
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],
@@ -1,4 +1,8 @@
1
- import { DEFAULT_QUALITY, Exec, validate } from "../services";
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): Exec => {
12
- return (mode, src, { width, height, quality = DEFAULT_QUALITY }) => {
13
- if (!validate(mode, { width, height })) return src;
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 SERVICES = ["gemini", "imgix", "lambda"] as const;
5
+ export const RESIZE_MODES = ["resize", "crop"] as const;
6
6
 
7
- export const MODES = ["resize", "crop"] as const;
7
+ export const DEFAULT_IMG_QUALITY = 80;
8
8
 
9
- export const DEFAULT_QUALITY = 80;
9
+ export type ResizeMode = typeof RESIZE_MODES[number];
10
10
 
11
- export type Mode = typeof MODES[number];
11
+ export type ResizeOptions = {
12
+ width?: number;
13
+ height?: number;
14
+ quality?: number;
15
+ };
12
16
 
13
- export type Config = {
17
+ export type ServiceConfigurations = {
14
18
  gemini?: Gemini;
15
19
  imgix?: Imgix;
16
20
  lambda?: Lambda;
17
21
  };
18
22
 
19
- export type Options = {
20
- width?: number;
21
- height?: number;
22
- quality?: number;
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 Service = { exec: Exec };
29
+ export type ImageService = { exec: ResizeExec };
28
30
 
29
- export const validate = (mode: Mode, { width, height }: Options) => {
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 configure = <T extends Config>(config: T) => {
58
- const services = {} as Record<keyof T, Service>;
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) };