Skip to content

Commit 1e2bcea

Browse files
Add setter methods to CloudinaryFile for some asset fields (#340)
* Add setter methods to CloudinaryFile for some asset fields * Tests - Add tests for the setters of CloudinaryFile
1 parent a164479 commit 1e2bcea

File tree

3 files changed

+47
-6
lines changed

3 files changed

+47
-6
lines changed

__TESTS__/unit/asset/CloudinaryFile.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import {CloudinaryFile} from "../../../src/assets/CloudinaryFile";
2+
import {Underlay} from "../../../src/actions/underlay";
3+
import {Source} from "../../../src/qualifiers/source";
24

35
describe('Tests for CloudinaryFile', () => {
46
let cloudinaryFile: CloudinaryFile = null;
@@ -21,5 +23,16 @@ describe('Tests for CloudinaryFile', () => {
2123
it('Can be turned to a URL', () => {
2224
expect(cloudinaryFile.toURL()).toBe('https://res.cloudinary.com/demo/image/upload/sample');
2325
});
26+
27+
it('Can set private fields', () => {
28+
cloudinaryFile
29+
.setPublicID('sample')
30+
.setSuffix('foo')
31+
.setAssetType('video')
32+
.setStorageType('fetch')
33+
.setVersion('12345');
34+
35+
expect(cloudinaryFile.toURL()).toContain('video/fetch/v12345/sample');
36+
});
2437
});
2538

src/assets/CloudinaryFile.ts

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class CloudinaryFile {
2323
protected authToken: IAuthTokenConfig; // populated from the cloud config
2424
protected urlConfig: IURLConfig;
2525

26-
public version: number;
26+
public version: number | string;
2727
public publicID: string;
2828
public extension: string;
2929
public suffix: string;
@@ -43,6 +43,30 @@ class CloudinaryFile {
4343
return this;
4444
}
4545

46+
setStorageType(newType: string): this {
47+
this.storageType = newType;
48+
return this;
49+
}
50+
51+
setSuffix(newSuffix: string): this {
52+
this.suffix = newSuffix;
53+
return this;
54+
}
55+
56+
setVersion(newVersion: number | string): this {
57+
if (newVersion) {
58+
this.version = newVersion;
59+
}
60+
return this;
61+
}
62+
63+
setAssetType(newType: string): this {
64+
if (newType) {
65+
this.assetType = newType;
66+
}
67+
return this;
68+
}
69+
4670
sign(): this {
4771
return this;
4872
}
@@ -57,7 +81,7 @@ class CloudinaryFile {
5781
* @description Creates a fully qualified CloudinaryURL
5882
* @return {string} CloudinaryURL
5983
*/
60-
createCloudinaryURL(transformation?: Transformation): string {
84+
createCloudinaryURL(transformation?: Transformation | string): string {
6185
if (typeof this.cloudName === 'undefined') {
6286
throw 'You must supply a cloudName in either toURL() or when initializing the asset';
6387
}
@@ -78,9 +102,13 @@ class CloudinaryFile {
78102
.filter((a) => a)
79103
.join('/');
80104

81-
return encodeURI(url)
82-
.replace(/\?/g, '%3F')
83-
.replace(/=/g, '%3D');
105+
if (typeof transformation === 'string') {
106+
return url;
107+
} else {
108+
return encodeURI(url)
109+
.replace(/\?/g, '%3F')
110+
.replace(/=/g, '%3D');
111+
}
84112
}
85113
}
86114

src/internal/url/cloudinaryURL.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ function handleStorageType(storageType: string): string {
8282
* @param {number} version
8383
* @param {boolean} forceVersion
8484
*/
85-
function getUrlVersion(publicID: string, version: number, forceVersion:boolean): string {
85+
function getUrlVersion(publicID: string, version: number | string, forceVersion:boolean): string {
8686
const shouldForceVersion = forceVersion !== false;
8787

8888
if (version) {

0 commit comments

Comments
 (0)