Skip to content

Commit 0b7f891

Browse files
authored
Merge pull request #403 from cloudinary/fix/cloudinary-file-class
Fix/cloudinary file class
2 parents 4283cef + c6cb7b0 commit 0b7f891

File tree

5 files changed

+68
-11
lines changed

5 files changed

+68
-11
lines changed

__TESTS__/unit/asset/CloudinaryFile.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ describe('Tests for CloudinaryFile', () => {
2727
.setPublicID('sample')
2828
.setSuffix('foo')
2929
.setAssetType('image')
30-
.setStorageType('private')
30+
.setDeliveryType('private')
3131
.setVersion('12345');
3232

3333
expect(cloudinaryFile.toURL()).toContain('private_images/v12345/sample/foo');

__TESTS__/unit/config/cloudinaryConfig.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ describe('Tests for CloudinaryConfiguration', () => {
185185

186186
it('should support url_suffix for private uploads', () => {
187187
const img = createNewImage('sample');
188-
img.setStorageType('private');
188+
img.setDeliveryType('private');
189189
img.setSuffix('SOME_SUFFIX');
190190

191191
expect(img.toURL()).toBe("https://res.cloudinary.com/demo/private_images/sample/SOME_SUFFIX");
@@ -214,7 +214,7 @@ describe('Tests for CloudinaryConfiguration', () => {
214214
const img = createNewImage('sample', {}, {
215215
useRootPath: true,
216216
privateCdn: true
217-
}).setStorageType('private');
217+
}).setDeliveryType('private');
218218
img.toURL();
219219
}).toThrow();
220220

__TESTS__/unit/url/encoding.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,15 @@ describe('Tests for Encoding the URL', () => {
6464

6565
it('Fetch: should not encode ("$:/") signs', () => {
6666
const img = createNewImage('https://res.cloudinary.com/demo/image/upload/sample?a=b');
67-
img.setStorageType('fetch');
67+
img.setDeliveryType('fetch');
6868

6969
expect(img.toURL())
7070
.toBe('https://res.cloudinary.com/demo/image/fetch/https://res.cloudinary.com/demo/image/upload/sample%3Fa%3Db');
7171
});
7272

7373
it('Should correctly encode youtube URLs', () => {
7474
const img = createNewImage('https://www.youtube.com/watch?v=d9NF2edxy-M');
75-
img.setStorageType('youtube');
75+
img.setDeliveryType('youtube');
7676

7777
expect(img.toURL())
7878
.toBe('https://res.cloudinary.com/demo/image/youtube/https://www.youtube.com/watch%3Fv%3Dd9NF2edxy-M');

__TESTS__/unit/url/url.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ describe('Tests for URL configuration', () => {
4040
// Suffix only works with predefined SEO_TYPES
4141
Object.keys(SEO_TYPES).forEach((resourceType) => {
4242
const [assetType, storageType] = resourceType.split('/');
43-
image.setStorageType(assetType)
44-
.setStorageType(storageType);
43+
image.setDeliveryType(assetType)
44+
.setDeliveryType(storageType);
4545

4646
expect(image.toURL.bind(image)).not.toThrow();
4747
});
4848

4949
// Any other storage type should throw
50-
image.setStorageType('fff');
50+
image.setDeliveryType('fff');
5151
expect(image.toURL.bind(image)).toThrow();
5252
});
5353

src/assets/CloudinaryFile.ts

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,32 @@ export const SEO_TYPES: Record<string, string> = {
2424
"video/upload": "videos"
2525
};
2626

27+
/**
28+
* Supported delivery type options.
29+
*/
30+
type DELIVERY_TYPE =
31+
'key'|
32+
'upload'|
33+
'private_delivery'|
34+
'public_delivery'|
35+
'authenticated'|
36+
'fetch'|
37+
'sprite'|
38+
'text'|
39+
'multi'|
40+
'facebook'|
41+
'twitter'|
42+
'twitter_name'|
43+
'gravatar'|
44+
'youtube'|
45+
'hulu'|
46+
'vimeo'|
47+
'animoto'|
48+
'worldstarhiphop'|
49+
'dailymotion';
2750

2851
/**
29-
* @desc Cloudinary file without a transformation
52+
* @description Cloudinary file without a transformation
3053
* @summary SDK
3154
* @memberOf SDK
3255
*/
@@ -54,35 +77,65 @@ class CloudinaryFile {
5477
this.urlConfig = new URLConfig(urlConfig);
5578
}
5679

80+
/**
81+
* @description Sets the public ID of the asset.
82+
* @param {string} publicID The public ID of the asset.
83+
* @return {this}
84+
*/
5785
setPublicID(publicID: string): this {
5886
// PublicID must be a string!
5987
this.publicID = publicID ? publicID.toString() : '';
6088
return this;
6189
}
6290

63-
setStorageType(newType: string): this {
91+
/**
92+
* @description Sets the delivery type of the asset.
93+
* @param {DELIVERY_TYPE | string} newType The type of the asset.
94+
* @return {this}
95+
*/
96+
setDeliveryType(newType: DELIVERY_TYPE|string): this {
6497
this.storageType = newType;
6598
return this;
6699
}
67100

101+
/**
102+
* @description Sets the URL SEO suffix of the asset.
103+
* @param {string} newSuffix The SEO suffix.
104+
* @return {this}
105+
*/
68106
setSuffix(newSuffix: string): this {
69107
this.suffix = newSuffix;
70108
return this;
71109
}
72110

111+
/**
112+
* @description Sets the signature of the asset.
113+
* @param {string} signature The signature.
114+
* @return {this}
115+
*/
73116
setSignature(signature: string): this {
74117
this.signature = signature;
75118
return this;
76119
}
77120

121+
/**
122+
* @description Sets the version of the asset.
123+
* @param {string} newVersion The version of the asset.
124+
* @return {this}
125+
*/
78126
setVersion(newVersion: number | string): this {
79127
if (newVersion) {
80128
this.version = newVersion;
81129
}
82130
return this;
83131
}
84132

85-
setAssetType(newType: string): this {
133+
/**
134+
* @description Sets the asset type.
135+
* @param {string} newType The type of the asset.
136+
* @return {this}
137+
*/
138+
setAssetType(newType: 'key'|'image'|'video'|'raw'|'auto'|'all'|string): this {
86139
if (newType) {
87140
this.assetType = newType;
88141
}
@@ -93,6 +146,10 @@ class CloudinaryFile {
93146
return this;
94147
}
95148

149+
/**
150+
* @description Serializes to URL string
151+
* @param overwriteOptions
152+
*/
96153
toURL(overwriteOptions: {trackedAnalytics?: Partial<ITrackedPropertiesThroughAnalytics>} = {}): string {
97154
return this.createCloudinaryURL(null, overwriteOptions.trackedAnalytics);
98155
}

0 commit comments

Comments
 (0)