Skip to content

Commit 049f3f2

Browse files
author
Maya Shavin
committed
feat: expose transformer api, add types
1 parent 3507969 commit 049f3f2

9 files changed

Lines changed: 116 additions & 33 deletions

File tree

packages/url/__tests__/transformers.test.ts

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import { transform, getBorder, getTransformations, getResize, toTransformationStr } from '../lib/transformers'
1+
import { transform, getBorder, getTransformations, getResize, toTransformationStr, getPosition } from '../lib/transformers'
22
import { resize } from '../lib/transformers/resize'
33
import { border } from '../lib/transformers/border'
4+
import { position } from '../lib/transformers/position'
5+
import { effect } from '../lib/transformers/effect'
46

57
describe('Modifiers', () => {
68
describe('transform()', () => {
@@ -112,14 +114,15 @@ describe('Modifiers', () => {
112114
expect(getResize(options)).toEqual('h_20')
113115
})
114116

115-
it('should support width, height and crop field', () => {
117+
it('should support width, height, aspectRatio and crop field', () => {
116118
const options = {
117119
height: 20,
118120
width: 20,
119-
crop: 'scale'
121+
crop: 'scale',
122+
aspectRatio: '1:1'
120123
}
121124

122-
expect(getResize(options)).toEqual('c_scale,w_20,h_20')
125+
expect(getResize(options)).toEqual('c_scale,w_20,h_20,ar_1:1')
123126
})
124127
})
125128

@@ -148,6 +151,31 @@ describe('Modifiers', () => {
148151
)
149152
})
150153
})
154+
155+
describe('getPosition()', () => {
156+
it('should prioritize position field', () => {
157+
const options = {
158+
position: {
159+
x: 10,
160+
y: 10,
161+
},
162+
x: 20,
163+
y: 20
164+
}
165+
166+
expect(getPosition(options)).toEqual('x_10,y_10')
167+
})
168+
169+
it('should take x field', () => {
170+
expect(getPosition({
171+
x: 10
172+
})).toBe('x_10')
173+
})
174+
175+
it('should take y field', () => {
176+
expect(getPosition({ y: 10 })).toBe('y_10')
177+
})
178+
})
151179
})
152180

153181
describe('resize()', () => {
@@ -176,4 +204,36 @@ describe('border', () => {
176204
it('should return options', () => {
177205
expect(border({ type: 'dotted', color: 'blue', width: 10 })).toEqual('bo_10px_dotted_blue')
178206
})
207+
})
208+
209+
describe('position', () => {
210+
it('should return full options string', () => {
211+
expect(position({ x: 10, y: 10 })).toBe('x_10,y_10')
212+
})
213+
214+
it('should return x only', () => {
215+
expect(position({ x: 10 })).toBe('x_10')
216+
})
217+
218+
it('should return y only', () => {
219+
expect(position({ y: 10 })).toBe('y_10')
220+
})
221+
222+
it('should return empty string', () => {
223+
expect(position({})).toBe('')
224+
})
225+
})
226+
227+
describe('effect', () => {
228+
it('should return legal string for array input', () => {
229+
expect(effect(['grayscale', 'tint:80'])).toBe('e_grayscale:tint:80')
230+
})
231+
232+
it('should return legal string for string input', () => {
233+
expect(effect('grayscale:tint')).toBe('e_grayscale:tint')
234+
})
235+
236+
it('should return empty string', () => {
237+
expect(effect('')).toBe('')
238+
})
179239
})

packages/url/lib/constants.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ export const SEO_TYPES = {
66
"video/upload": "videos"
77
};
88

9+
export const ROTATION_MODES = {
10+
AUTO_RIGHT: 'auto_right',
11+
AUTO_LEFT: 'auto_left',
12+
VERTICAL_FLIP: 'vflip',
13+
HORIZONTAL_FLIP: 'hflip',
14+
IGNORE_EXIF_DATA: 'ignore'
15+
}
16+
917
export const IMAGE_CONDITIONAL_OPERATORS = {
1018
equal: "eq",
1119
notEqual: "ne",
@@ -116,7 +124,7 @@ export const TRANSFORMERS = {
116124
videoCodec: 'vc',
117125
videoSampling: 'vs',
118126
// width: 'w',
119-
x: 'x',
120-
y: 'y',
127+
// x: 'x',
128+
// y: 'y',
121129
zoom: 'z'
122130
}

packages/url/lib/transformers/effect.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
export const effect = (value: string | string[]):string => {
1+
export type Effect = string | string[]
2+
3+
export const effect = (value: Effect):string => {
24
if (!value || value.length === 0) return ''
35

46
const effectValue = typeof value === 'string' ? value : value.join(':')

packages/url/lib/transformers/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,20 @@ export const getBorder = (options):string => {
2424
return borderModification
2525
}
2626

27+
export const getPosition = (options): string => {
28+
if (!options.x && !options.y && !options.position) return ''
29+
30+
const { position } = require('./position')
31+
32+
return position(options.position || { x: options.x, y: options.y })
33+
}
34+
2735
export const getTransformations = (options):string[] => {
2836
const result = []
2937

3038
result.push(getResize(options))
3139
result.push(getBorder(options))
40+
result.push(getPosition(options))
3241

3342
for (let modifier in options) {
3443
const value = options[modifier]

packages/url/lib/types/CldOptions.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ import { Resize } from '../transformers/resize'
44
import { Border } from '../transformers/border'
55
import { CustomFunction } from '../transformers/customFunc'
66
import { Offset } from '../transformers/video/offset'
7+
import { Position } from '../transformers/position'
8+
import { Effect } from '../transformers/effect'
9+
import { ROTATION_MODES } from '../constants'
10+
11+
export type Radius = number | string
12+
13+
export type Flag = string | string[]
14+
15+
export type Rotation = number | typeof ROTATION_MODES[keyof typeof ROTATION_MODES]
716

817
export interface CldOptions {
918
cloud?: CloudConfig,
@@ -30,11 +39,10 @@ export interface CloudConfig {
3039
}
3140

3241
export interface TransformerOption {
33-
rotation?: string | number,
34-
aspectRatio?: string,
42+
rotation?: Rotation,
3543
background?: string,
3644
border?: Border | string,
37-
effect?: string | string[],
45+
effect?: Effect,
3846
color?: string,
3947
resize?: Resize,
4048
colorSpace?: string,
@@ -49,22 +57,21 @@ export interface TransformerOption {
4957
fetchFormat?: string,
5058
gravity?: string,
5159
if?: string,
52-
flags?: string | string[],
60+
flags?: Flag,
5361
opacity?: number,
5462
overlay?: string,
5563
page?: string,
5664
prefix?: string,
5765
quality?: string,
58-
radius?: number | string,
66+
radius?: Radius,
5967
rawTransformation?: string,
6068
transformation?: string,
6169
chaining?: TransformerOption[],
6270
underlay?: string,
6371
variable?: string,
6472
variables?: string,
65-
x?: number,
66-
y?: number,
67-
zoom?: string | number
73+
position?: Position,
74+
zoom?: number
6875
}
6976

7077
export interface TransformerVideoOption extends TransformerOption{

packages/url/lib/url.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,6 @@ export const getResourceType = ({
9595

9696
const isUrl = (str) => str && !!str.match(/^https?:\//)
9797

98-
const isFetchRemote = (type?: string) => type === 'fetch'
99-
100-
const makeUrl = (str: string) => {
101-
const midPath = str.startsWith('/') ? '' : '/'
102-
103-
return `${document.location.protocol}//${document.location.host}${str.startsWith('?') ? document.location.pathname : midPath}${str}`
104-
}
105-
106-
const convertPublicIdToUrl = (publicId: string, type?: string) => !isUrl(publicId) && isFetchRemote(type) ? makeUrl(publicId) : publicId
107-
10898
export const getPathToAsset = (publicId: string, { urlSuffix = '', format = '' } : { urlSuffix?: string, format?: string }):string => {
10999
if (isUrl(publicId)) return encodePublicId(publicId)
110100

@@ -136,7 +126,7 @@ export const url = (publicId: string, cloud: CloudConfig = { cloudName: ''}, opt
136126

137127
const format = options.fetchFormat || options.format || 'auto'
138128

139-
const $options = {
129+
const $options:TransformerOption | TransformerVideoOption = {
140130
quality: 'auto',
141131
...options,
142132
format

packages/url/package.json

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
{
22
"name": "cloudinary-build-url",
3-
"version": "0.0.4",
3+
"version": "0.0.5",
44
"description": "Lighter Url generator for Cloudinary",
55
"author": "Maya Shavin <maya@cloudinary.com>",
66
"license": "MIT",
7-
"main": "dist/index.js",
8-
"types": "dist/index.d.ts",
7+
"main": "dist/cjs/index.js",
8+
"module": "dist/esm/index.js",
9+
"types": "dist/cjs/index.d.ts",
910
"directories": {
1011
"lib": "lib",
1112
"test": "__tests__"
@@ -16,9 +17,10 @@
1617
"scripts": {
1718
"build": "npm run clean && npm run compile",
1819
"clean": "rm -rf ./dist",
19-
"compile": "tsc -p tsconfig.build.json",
20+
"compile": "tsc -p tsconfig.build.json && tsc -p tsconfig.json",
2021
"test:unit": "jest __tests__ --reporters default",
21-
"tsc": "tsc"
22+
"tsc": "tsc -p tsconfig.build.json && tsc -p tsconfig.json",
23+
"prepublishOnly": "agadoo dist/"
2224
},
2325
"keywords": [
2426
"cloudinary",
@@ -34,6 +36,7 @@
3436
"devDependencies": {
3537
"@types/jest": "^26.0.15",
3638
"@types/node": "^14.14.7",
39+
"agadoo": "^2.0.0",
3740
"jest": "^26.6.3",
3841
"jest-html-reporters": "^2.1.0",
3942
"ts-jest": "^26.4.4",

packages/url/tsconfig.build.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
"lib/**/*"
55
],
66
"compilerOptions": {
7-
"outDir": "./dist"
7+
"outDir": "./dist/cjs"
88
}
99
}

packages/url/tsconfig.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
{
2-
"extends": "./tsconfig.build.json"
2+
"extends": "./tsconfig.build.json",
3+
"compilerOptions": {
4+
"outDir": "./dist/esm",
5+
"module": "ES2015"
6+
}
37
}

0 commit comments

Comments
 (0)