Skip to content

Commit 2017aab

Browse files
fix v1 url generation to correctly support border objects and strings (#384)
* fix v1 url generation to correctly support border objects and strings
1 parent 365d925 commit 2017aab

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

__TESTS__/backwardsComaptibility/createV1URL.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,4 +238,25 @@ describe('Create v1 urls', () => {
238238
expect(createCloudinaryV1URL(source, {cloud_name: 'demo'})).toEqual(`https://res.cloudinary.com/demo/image/upload/${target}`);
239239
});
240240
});
241+
242+
it("Should generate transformation with border objects", function () {
243+
const url = createCloudinaryV1URL("sample", {
244+
cloud_name: 'demo',
245+
border: {
246+
width: 5,
247+
color: '#ffaabbdd'
248+
}
249+
});
250+
251+
expect(url).toContain(`image/upload/bo_5px_solid_rgb:ffaabbdd/sample`);
252+
});
253+
254+
it("Should generate transformation with border string", function () {
255+
const url = createCloudinaryV1URL("sample", {
256+
cloud_name: 'demo',
257+
border: '4px_solid_white'
258+
});
259+
260+
expect(url).toContain(`image/upload/bo_4px_solid_white/sample`);
261+
});
241262
});

src/backwards/generateTransformationString.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ export function generateTransformationString(transformationOptions: V1ITransfora
6666
let namedTransformations = [];
6767
let childTransformations = toArray(transformationOptions.transformation || []);
6868
let effect = transformationOptions.effect;
69-
let border = transformationOptions.border;
7069

7170
// TODO, Do we need this?
7271
const no_html_sizes = hasLayer || angle || crop === "fit" || crop === "limit";
@@ -118,9 +117,15 @@ export function generateTransformationString(transformationOptions: V1ITransfora
118117
);
119118
}
120119

121-
let borderParam;
120+
let border = transformationOptions.border;
122121
if (isObject(border)) {
123-
borderParam = `${border.width != null ? border.width : 2}px_solid_${(border.color != null ? border.color : "black").replace(/^#/, 'rgb:')}`;
122+
border = `${border.width != null ? border.width : 2}px_solid_${(border.color != null ? border.color : "black").replace(/^#/, 'rgb:')}`;
123+
} else {
124+
// @ts-ignore
125+
if (/^\d+$/.exec(border)) { // fallback to html border attributes
126+
transformationOptions.border = border;
127+
border = void 0;
128+
}
124129
}
125130

126131
if (Array.isArray(fps)) {

src/backwards/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ export interface V1ITransforamtionOptions {
274274
border?: {
275275
width?: stringOrNumber;
276276
color?: string;
277-
};
277+
} | string;
278278
default_image?: string;
279279
density?: stringOrNumber;
280280
format?: ImageFormat;

src/backwards/utils/isObject.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
*
44
* @param a
55
*/
6-
export function isObject(a: any): boolean {
6+
export function isObject(a: any): a is Record<string, any> {
77
return typeof a === 'object' && a !== null;
88
}

0 commit comments

Comments
 (0)